]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f009e76e1f8d9a2e2babf37325fb86f6d0793dab
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.collectionspace.services.client.test;
24
25 import java.util.List;
26 import javax.ws.rs.core.Response;
27
28 import org.apache.commons.codec.binary.Base64;
29 import org.collectionspace.services.client.AccountClient;
30 import org.collectionspace.services.account.AccountsCommon;
31 import org.collectionspace.services.account.AccountsCommonList;
32 import org.jboss.resteasy.client.ClientResponse;
33
34 import org.testng.Assert;
35 import org.testng.annotations.Test;
36
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * AccountServiceTest, carries out tests against a
42  * deployed and running Account Service.
43  * 
44  * $LastChangedRevision: 917 $
45  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
46  */
47 public class AccountServiceTest extends AbstractServiceTest {
48
49     private final Logger logger =
50             LoggerFactory.getLogger(AccountServiceTest.class);
51     // Instance variables specific to this test.
52     private AccountClient client = new AccountClient();
53     private String knownResourceId = null;
54
55     /*
56      * This method is called only by the parent class, AbstractServiceTest
57      */
58     @Override
59     protected String getServicePathComponent() {
60         return client.getServicePathComponent();
61     }
62
63     // ---------------------------------------------------------------
64     // CRUD tests : CREATE tests
65     // ---------------------------------------------------------------
66     // Success outcomes
67     @Override
68     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class)
69     public void create(String testName) throws Exception {
70
71         // Perform setup, such as initializing the type of service request
72         // (e.g. CREATE, DELETE), its valid and expected status codes, and
73         // its associated HTTP method name (e.g. POST, DELETE).
74         setupCreate(testName);
75
76         // Submit the request to the service and store the response.
77         AccountsCommon account =
78                 createAccountInstance("barney", "dino", "barney", "hello", "barney@dinoland.com");
79         ClientResponse<Response> res = client.create(account);
80         int statusCode = res.getStatus();
81
82         // Check the status code of the response: does it match
83         // the expected response(s)?
84         //
85         // Specifically:
86         // Does it fall within the set of valid status codes?
87         // Does it exactly match the expected status code?
88         if (logger.isDebugEnabled()) {
89             logger.debug(testName + ": status = " + statusCode);
90         }
91         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
92                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
93         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
94
95         // Store the ID returned from this create operation
96         // for additional tests below.
97         knownResourceId = extractId(res);
98         if (logger.isDebugEnabled()) {
99             logger.debug(testName + ": knownResourceId=" + knownResourceId);
100         }
101     }
102
103     /* (non-Javadoc)
104      * @see org.collectionspace.services.client.test.ServiceTest#createList()
105      */
106     @Override
107     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
108     dependsOnMethods = {"create"})
109     public void createList(String testName) throws Exception {
110         for (int i = 0; i < 3; i++) {
111             create(testName);
112         }
113     }
114
115     // Failure outcomes
116     // Placeholders until the three tests below can be uncommented.
117     // See Issue CSPACE-401.
118     @Override
119     public void createWithEmptyEntityBody(String testName) throws Exception {
120     }
121
122     @Override
123     public void createWithMalformedXml(String testName) throws Exception {
124     }
125
126     @Override
127     public void createWithWrongXmlSchema(String testName) throws Exception {
128     }
129
130     // ---------------------------------------------------------------
131     // CRUD tests : READ tests
132     // ---------------------------------------------------------------
133     // Success outcomes
134     @Override
135     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
136     dependsOnMethods = {"create"})
137     public void read(String testName) throws Exception {
138
139         // Perform setup.
140         setupRead(testName);
141
142         // Submit the request to the service and store the response.
143         ClientResponse<AccountsCommon> res = client.read(knownResourceId);
144         int statusCode = res.getStatus();
145
146         // Check the status code of the response: does it match
147         // the expected response(s)?
148         if (logger.isDebugEnabled()) {
149             logger.debug(testName + ": status = " + statusCode);
150         }
151         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
152                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
153         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
154
155         AccountsCommon output = (AccountsCommon) res.getEntity();
156         Assert.assertNotNull(output);
157     }
158
159     // Failure outcomes
160     @Override
161     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
162     dependsOnMethods = {"read"})
163     public void readNonExistent(String testName) throws Exception {
164
165         // Perform setup.
166         setupReadNonExistent(testName);
167
168         // Submit the request to the service and store the response.
169         ClientResponse<AccountsCommon> res = client.read(NON_EXISTENT_ID);
170         int statusCode = res.getStatus();
171
172         // Check the status code of the response: does it match
173         // the expected response(s)?
174         if (logger.isDebugEnabled()) {
175             logger.debug(testName + ": status = " + statusCode);
176         }
177         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
178                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
179         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
180     }
181
182     // ---------------------------------------------------------------
183     // CRUD tests : READ_LIST tests
184     // ---------------------------------------------------------------
185     // Success outcomes
186     @Override
187     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
188     dependsOnMethods = {"createList", "read"})
189     public void readList(String testName) throws Exception {
190
191         // Perform setup.
192         setupReadList(testName);
193
194         // Submit the request to the service and store the response.
195         ClientResponse<AccountsCommonList> res = client.readList();
196         AccountsCommonList list = res.getEntity();
197         int statusCode = res.getStatus();
198
199         // Check the status code of the response: does it match
200         // the expected response(s)?
201         if (logger.isDebugEnabled()) {
202             logger.debug(testName + ": status = " + statusCode);
203         }
204         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
205                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
206         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
207
208         // Optionally output additional data about list members for debugging.
209         boolean iterateThroughList = true;
210         if (iterateThroughList && logger.isDebugEnabled()) {
211             List<AccountsCommonList.AccountListItem> items =
212                     list.getAccountListItem();
213             int i = 0;
214
215             for (AccountsCommonList.AccountListItem item : items) {
216                 logger.debug(testName + ": list-item[" + i + "] csid=" +
217                         item.getCsid());
218                 logger.debug(testName + ": list-item[" + i + "] anchorName=" +
219                         item.getAnchorName());
220                 logger.debug(testName + ": list-item[" + i + "] URI=" +
221                         item.getUri());
222                 i++;
223
224             }
225         }
226     }
227
228     // Failure outcomes
229     // None at present.
230     // ---------------------------------------------------------------
231     // CRUD tests : UPDATE tests
232     // ---------------------------------------------------------------
233     // Success outcomes
234     @Override
235     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
236     dependsOnMethods = {"read", "readNonExistent"})
237     public void update(String testName) throws Exception {
238
239         // Perform setup.
240         setupUpdate(testName);
241
242         ClientResponse<AccountsCommon> res =
243                 client.read(knownResourceId);
244         if (logger.isDebugEnabled()) {
245             logger.debug(testName + ": read status = " + res.getStatus());
246         }
247         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
248
249         if (logger.isDebugEnabled()) {
250             logger.debug("got object to update with ID: " + knownResourceId);
251         }
252         AccountsCommon toUpdateAccount =
253                 (AccountsCommon) res.getEntity();
254         Assert.assertNotNull(toUpdateAccount);
255
256         // Update the content of this resource.
257         toUpdateAccount.setEmail("updated-" + toUpdateAccount.getEmail());
258         toUpdateAccount.setPhone("updated-" + toUpdateAccount.getPhone());
259         if (logger.isDebugEnabled()) {
260             logger.debug("updated object");
261             logger.debug(objectAsXmlString(toUpdateAccount,
262                     AccountsCommon.class));
263         }
264
265         // Submit the request to the service and store the response.
266         res = client.update(knownResourceId, toUpdateAccount);
267         int statusCode = res.getStatus();
268         // Check the status code of the response: does it match the expected response(s)?
269         if (logger.isDebugEnabled()) {
270             logger.debug(testName + ": status = " + statusCode);
271         }
272         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
273                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
274         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
275
276
277         AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
278         Assert.assertNotNull(updatedAccount);
279
280         Assert.assertEquals(updatedAccount.getEmail(),
281                 toUpdateAccount.getEmail(),
282                 "Data in updated object did not match submitted data.");
283
284     }
285
286     // Failure outcomes
287     // Placeholders until the three tests below can be uncommented.
288     // See Issue CSPACE-401.
289     @Override
290     public void updateWithEmptyEntityBody(String testName) throws Exception {
291     }
292
293     @Override
294     public void updateWithMalformedXml(String testName) throws Exception {
295     }
296
297     @Override
298     public void updateWithWrongXmlSchema(String testName) throws Exception {
299     }
300
301     @Override
302     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
303     dependsOnMethods = {"update", "readNonExistent", "testSubmitRequest"})
304     public void updateNonExistent(String testName) throws Exception {
305
306         // Perform setup.
307         setupUpdateNonExistent(testName);
308
309         // Submit the request to the service and store the response.
310         //
311         // Note: The ID used in this 'create' call may be arbitrary.
312         // The only relevant ID may be the one used in updateAccount(), below.
313         AccountsCommon account =
314                 createAccountInstance("simba", "mufasa", "simba", "tiger", "simba@lionking.com");
315         ClientResponse<AccountsCommon> res =
316                 client.update(NON_EXISTENT_ID, account);
317         int statusCode = res.getStatus();
318
319         // Check the status code of the response: does it match
320         // the expected response(s)?
321         if (logger.isDebugEnabled()) {
322             logger.debug(testName + ": status = " + statusCode);
323         }
324         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
325                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
326         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
327     }
328
329     // ---------------------------------------------------------------
330     // CRUD tests : DELETE tests
331     // ---------------------------------------------------------------
332     // Success outcomes
333     @Override
334     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
335     dependsOnMethods = {"create", "readList", "testSubmitRequest", "update", "updateNonExistent"})
336     public void delete(String testName) throws Exception {
337
338         // Perform setup.
339         setupDelete(testName);
340
341         // Submit the request to the service and store the response.
342         ClientResponse<Response> res = client.delete(knownResourceId);
343         int statusCode = res.getStatus();
344
345         // Check the status code of the response: does it match
346         // the expected response(s)?
347         if (logger.isDebugEnabled()) {
348             logger.debug(testName + ": status = " + statusCode);
349         }
350         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
351                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
352         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
353     }
354
355     // Failure outcomes
356     @Override
357     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
358     dependsOnMethods = {"delete"})
359     public void deleteNonExistent(String testName) throws Exception {
360
361         // Perform setup.
362         setupDeleteNonExistent(testName);
363
364         // Submit the request to the service and store the response.
365         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
366         int statusCode = res.getStatus();
367
368         // Check the status code of the response: does it match
369         // the expected response(s)?
370         if (logger.isDebugEnabled()) {
371             logger.debug(testName + ": status = " + statusCode);
372         }
373         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
374                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
375         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
376     }
377
378     // ---------------------------------------------------------------
379     // Utility tests : tests of code used in tests above
380     // ---------------------------------------------------------------
381     /**
382      * Tests the code for manually submitting data that is used by several
383      * of the methods above.
384      */
385     @Test(dependsOnMethods = {"create", "read"})
386     public void testSubmitRequest() throws Exception {
387
388         // Expected status code: 200 OK
389         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
390
391         // Submit the request to the service and store the response.
392         String method = ServiceRequestType.READ.httpMethodName();
393         String url = getResourceURL(knownResourceId);
394         int statusCode = submitRequest(method, url);
395
396         // Check the status code of the response: does it match
397         // the expected response(s)?
398         if (logger.isDebugEnabled()) {
399             logger.debug("testSubmitRequest: url=" + url +
400                     " status=" + statusCode);
401         }
402         Assert.assertEquals(statusCode, EXPECTED_STATUS);
403
404     }
405
406     // ---------------------------------------------------------------
407     // Utility methods used by tests above
408     // ---------------------------------------------------------------
409     private AccountsCommon createAccountInstance(String firstName, String lastName, String anchorName,
410             String passwd, String email) {
411
412         AccountsCommon account = new AccountsCommon();
413         account.setFirstName(firstName);
414         account.setLastName(lastName);
415         account.setAnchorName(anchorName);
416         account.setUserName(anchorName);
417         byte[] b64passwd = Base64.encodeBase64(passwd.getBytes());
418         account.setPassword(b64passwd);
419         account.setEmail(email);
420         if (logger.isDebugEnabled()) {
421             logger.debug("to be created, account common");
422             logger.debug(objectAsXmlString(account,
423                     AccountsCommon.class));
424         }
425         return account;
426
427     }
428 }