]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
bf5ac4ab839eeb9af6be2bfde2ef7191c4b8735d
[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.collectionspace.services.account.Status;
33 import org.jboss.resteasy.client.ClientResponse;
34
35 import org.testng.Assert;
36 import org.testng.annotations.Test;
37
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * AccountServiceTest, carries out tests against a
43  * deployed and running Account Service.
44  * 
45  * $LastChangedRevision: 917 $
46  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
47  */
48 public class AccountServiceTest extends AbstractServiceTest {
49
50     private final Logger logger =
51             LoggerFactory.getLogger(AccountServiceTest.class);
52     // Instance variables specific to this test.
53     private AccountClient client = new AccountClient();
54     private String knownResourceId = null;
55
56     /*
57      * This method is called only by the parent class, AbstractServiceTest
58      */
59     @Override
60     protected String getServicePathComponent() {
61         return client.getServicePathComponent();
62     }
63
64     // ---------------------------------------------------------------
65     // CRUD tests : CREATE tests
66     // ---------------------------------------------------------------
67     // Success outcomes
68     @Override
69     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class)
70     public void create(String testName) throws Exception {
71
72         // Perform setup, such as initializing the type of service request
73         // (e.g. CREATE, DELETE), its valid and expected status codes, and
74         // its associated HTTP method name (e.g. POST, DELETE).
75         setupCreate(testName);
76
77         // Submit the request to the service and store the response.
78         AccountsCommon account =
79                 createAccountInstance("barney", "dino", "barney", "hithere08", "barney@dinoland.com");
80         ClientResponse<Response> res = client.create(account);
81         int statusCode = res.getStatus();
82
83         // Check the status code of the response: does it match
84         // the expected response(s)?
85         //
86         // Specifically:
87         // Does it fall within the set of valid status codes?
88         // Does it exactly match the expected status code?
89         if (logger.isDebugEnabled()) {
90             logger.debug(testName + ": status = " + statusCode);
91         }
92         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
93                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
94         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
95
96         // Store the ID returned from this create operation
97         // for additional tests below.
98         knownResourceId = extractId(res);
99         if (logger.isDebugEnabled()) {
100             logger.debug(testName + ": knownResourceId=" + knownResourceId);
101         }
102     }
103
104     //to not cause uniqueness violation for account, createList is removed
105     @Override
106     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
107     dependsOnMethods = {"create"})
108     public void createList(String testName) throws Exception {
109     }
110
111     // Failure outcomes
112     // Placeholders until the three tests below can be uncommented.
113     // See Issue CSPACE-401.
114     @Override
115     public void createWithEmptyEntityBody(String testName) throws Exception {
116     }
117
118     @Override
119     public void createWithMalformedXml(String testName) throws Exception {
120     }
121
122     @Override
123     public void createWithWrongXmlSchema(String testName) throws Exception {
124     }
125
126     // ---------------------------------------------------------------
127     // CRUD tests : READ tests
128     // ---------------------------------------------------------------
129     // Success outcomes
130     @Override
131     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
132     dependsOnMethods = {"create"})
133     public void read(String testName) throws Exception {
134
135         // Perform setup.
136         setupRead(testName);
137
138         // Submit the request to the service and store the response.
139         ClientResponse<AccountsCommon> res = client.read(knownResourceId);
140         int statusCode = res.getStatus();
141
142         // Check the status code of the response: does it match
143         // the expected response(s)?
144         if (logger.isDebugEnabled()) {
145             logger.debug(testName + ": status = " + statusCode);
146         }
147         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
148                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
149         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
150
151         AccountsCommon output = (AccountsCommon) res.getEntity();
152         Assert.assertNotNull(output);
153     }
154
155     // Failure outcomes
156     @Override
157     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
158     dependsOnMethods = {"read"})
159     public void readNonExistent(String testName) throws Exception {
160
161         // Perform setup.
162         setupReadNonExistent(testName);
163
164         // Submit the request to the service and store the response.
165         ClientResponse<AccountsCommon> res = client.read(NON_EXISTENT_ID);
166         int statusCode = res.getStatus();
167
168         // Check the status code of the response: does it match
169         // the expected response(s)?
170         if (logger.isDebugEnabled()) {
171             logger.debug(testName + ": status = " + statusCode);
172         }
173         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
174                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
175         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
176     }
177
178     // ---------------------------------------------------------------
179     // CRUD tests : READ_LIST tests
180     // ---------------------------------------------------------------
181     // Success outcomes
182     @Override
183     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
184     dependsOnMethods = {"createList", "read"})
185     public void readList(String testName) throws Exception {
186
187         // Perform setup.
188         setupReadList(testName);
189
190         // Submit the request to the service and store the response.
191         ClientResponse<AccountsCommonList> res = client.readList();
192         AccountsCommonList list = res.getEntity();
193         int statusCode = res.getStatus();
194
195         // Check the status code of the response: does it match
196         // the expected response(s)?
197         if (logger.isDebugEnabled()) {
198             logger.debug(testName + ": status = " + statusCode);
199         }
200         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
201                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
202         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
203
204         // Optionally output additional data about list members for debugging.
205         boolean iterateThroughList = true;
206         if (iterateThroughList && logger.isDebugEnabled()) {
207             List<AccountsCommonList.AccountListItem> items =
208                     list.getAccountListItem();
209             int i = 0;
210
211             for (AccountsCommonList.AccountListItem item : items) {
212                 logger.debug(testName + ": list-item[" + i + "] csid=" +
213                         item.getCsid());
214                 logger.debug(testName + ": list-item[" + i + "] screenName=" +
215                         item.getScreenName());
216                 logger.debug(testName + ": list-item[" + i + "] URI=" +
217                         item.getUri());
218                 i++;
219
220             }
221         }
222     }
223
224     // Failure outcomes
225     // None at present.
226     // ---------------------------------------------------------------
227     // CRUD tests : UPDATE tests
228     // ---------------------------------------------------------------
229     // Success outcomes
230     @Override
231     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
232     dependsOnMethods = {"read", "readList", "readNonExistent"})
233     public void update(String testName) throws Exception {
234
235         // Perform setup.
236         setupUpdate(testName);
237
238
239         ClientResponse<AccountsCommon> res =
240                 client.read(knownResourceId);
241         if (logger.isDebugEnabled()) {
242             logger.debug(testName + ": read status = " + res.getStatus());
243         }
244         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
245
246         if (logger.isDebugEnabled()) {
247             logger.debug("got object to update with ID: " + knownResourceId);
248         }
249         AccountsCommon toUpdateAccount =
250                 (AccountsCommon) res.getEntity();
251         Assert.assertNotNull(toUpdateAccount);
252
253         // Update the content of this resource.
254         toUpdateAccount.setEmail("updated-" + toUpdateAccount.getEmail());
255         if (logger.isDebugEnabled()) {
256             logger.debug("updated object");
257             logger.debug(objectAsXmlString(toUpdateAccount,
258                     AccountsCommon.class));
259         }
260
261         // Submit the request to the service and store the response.
262         res = client.update(knownResourceId, toUpdateAccount);
263         int statusCode = res.getStatus();
264         // Check the status code of the response: does it match the expected response(s)?
265         if (logger.isDebugEnabled()) {
266             logger.debug(testName + ": status = " + statusCode);
267         }
268         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
269                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
270         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
271
272
273         AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
274         Assert.assertNotNull(updatedAccount);
275
276         Assert.assertEquals(updatedAccount.getEmail(),
277                 toUpdateAccount.getEmail(),
278                 "Data in updated object did not match submitted data.");
279     }
280
281     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
282     dependsOnMethods = {"update"})
283     public void updatePassword(String testName) throws Exception {
284
285         // Perform setup.
286         setupUpdate(testName);
287
288         ClientResponse<AccountsCommon> res =
289                 client.read(knownResourceId);
290         if (logger.isDebugEnabled()) {
291             logger.debug(testName + ": read status = " + res.getStatus());
292         }
293         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
294
295         if (logger.isDebugEnabled()) {
296             logger.debug("got object to update with ID: " + knownResourceId);
297         }
298         AccountsCommon toUpdateAccount =
299                 (AccountsCommon) res.getEntity();
300         Assert.assertNotNull(toUpdateAccount);
301
302         //change password
303         toUpdateAccount.setPassword(Base64.encodeBase64("imagination".getBytes()));
304         if (logger.isDebugEnabled()) {
305             logger.debug("updated object");
306             logger.debug(objectAsXmlString(toUpdateAccount,
307                     AccountsCommon.class));
308         }
309
310         // Submit the request to the service and store the response.
311         res = client.update(knownResourceId, toUpdateAccount);
312         int statusCode = res.getStatus();
313         // Check the status code of the response: does it match the expected response(s)?
314         if (logger.isDebugEnabled()) {
315             logger.debug(testName + ": status = " + statusCode);
316         }
317         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
318                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
319         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
320
321
322         AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
323         Assert.assertNotNull(updatedAccount);
324
325 //        Assert.assertEquals(updatedAccount.getPassword(),
326 //                toUpdateAccount.getPassword(),
327 //                "Data in updated object did not match submitted data.");
328     }
329
330     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
331     dependsOnMethods = {"updatePassword"})
332     public void deactivate(String testName) throws Exception {
333
334         // Perform setup.
335         setupUpdate(testName);
336
337         ClientResponse<AccountsCommon> res =
338                 client.read(knownResourceId);
339         if (logger.isDebugEnabled()) {
340             logger.debug(testName + ": read status = " + res.getStatus());
341         }
342         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
343
344         if (logger.isDebugEnabled()) {
345             logger.debug("got object to update with ID: " + knownResourceId);
346         }
347         AccountsCommon toUpdateAccount =
348                 (AccountsCommon) res.getEntity();
349         Assert.assertNotNull(toUpdateAccount);
350
351         // Update the content of this resource.
352         toUpdateAccount.setStatus(Status.INACTIVE);
353         if (logger.isDebugEnabled()) {
354             logger.debug("updated object");
355             logger.debug(objectAsXmlString(toUpdateAccount,
356                     AccountsCommon.class));
357         }
358
359         // Submit the request to the service and store the response.
360         res = client.update(knownResourceId, toUpdateAccount);
361         int statusCode = res.getStatus();
362         // Check the status code of the response: does it match the expected response(s)?
363         if (logger.isDebugEnabled()) {
364             logger.debug(testName + ": status = " + statusCode);
365         }
366         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
367                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
368         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
369
370
371         AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
372         Assert.assertNotNull(updatedAccount);
373
374         Assert.assertEquals(updatedAccount.getStatus(),
375                 toUpdateAccount.getStatus(),
376                 "Data in updated object did not match submitted data.");
377
378     }
379
380     // Failure outcomes
381     // Placeholders until the three tests below can be uncommented.
382     // See Issue CSPACE-401.
383     @Override
384     public void updateWithEmptyEntityBody(String testName) throws Exception {
385     }
386
387     @Override
388     public void updateWithMalformedXml(String testName) throws Exception {
389     }
390
391     @Override
392     public void updateWithWrongXmlSchema(String testName) throws Exception {
393     }
394
395     @Override
396     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
397     dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
398     public void updateNonExistent(String testName) throws Exception {
399
400         // Perform setup.
401         setupUpdateNonExistent(testName);
402
403         // Submit the request to the service and store the response.
404         //
405         // Note: The ID used in this 'create' call may be arbitrary.
406         // The only relevant ID may be the one used in updateAccount(), below.
407         AccountsCommon account =
408                 createAccountInstance("simba", "mufasa", "simba", "tiger", "simba@lionking.com");
409         ClientResponse<AccountsCommon> res =
410                 client.update(NON_EXISTENT_ID, account);
411         int statusCode = res.getStatus();
412
413         // Check the status code of the response: does it match
414         // the expected response(s)?
415         if (logger.isDebugEnabled()) {
416             logger.debug(testName + ": status = " + statusCode);
417         }
418         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
419                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
420         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
421     }
422
423     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
424     dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
425     public void updateWrongUser(String testName) throws Exception {
426
427         setupUpdate();
428         // Submit the request to the service and store the response.
429         //
430         // Note: The ID used in this 'create' call may be arbitrary.
431         // The only relevant ID may be the one used in updateAccount(), below.
432          ClientResponse<AccountsCommon> res =
433                 client.read(knownResourceId);
434         if (logger.isDebugEnabled()) {
435             logger.debug(testName + ": read status = " + res.getStatus());
436         }
437         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
438
439         if (logger.isDebugEnabled()) {
440             logger.debug("got object to update with ID: " + knownResourceId);
441         }
442         AccountsCommon toUpdateAccount =
443                 (AccountsCommon) res.getEntity();
444         Assert.assertNotNull(toUpdateAccount);
445
446         toUpdateAccount.setUserId("barneyFake");
447         if (logger.isDebugEnabled()) {
448             logger.debug("updated object with wrongUser");
449             logger.debug(objectAsXmlString(toUpdateAccount,
450                     AccountsCommon.class));
451         }
452                 EXPECTED_STATUS_CODE = Response.Status.BAD_REQUEST.getStatusCode();
453         res = client.update(knownResourceId, toUpdateAccount);
454         int statusCode = res.getStatus();
455
456         // Check the status code of the response: does it match
457         // the expected response(s)?
458         if (logger.isDebugEnabled()) {
459             logger.debug(testName + ": status = " + statusCode);
460         }
461         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
462                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
463         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
464     }
465
466     // ---------------------------------------------------------------
467     // CRUD tests : DELETE tests
468     // ---------------------------------------------------------------
469     // Success outcomes
470     @Override
471     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
472     dependsOnMethods = {"testSubmitRequest", "updateNonExistent"})
473     public void delete(String testName) throws Exception {
474
475         // Perform setup.
476         setupDelete(testName);
477
478         // Submit the request to the service and store the response.
479         ClientResponse<Response> res = client.delete(knownResourceId);
480         int statusCode = res.getStatus();
481
482         // Check the status code of the response: does it match
483         // the expected response(s)?
484         if (logger.isDebugEnabled()) {
485             logger.debug(testName + ": status = " + statusCode);
486         }
487         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
488                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
489         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
490     }
491
492     // Failure outcomes
493     @Override
494     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
495     dependsOnMethods = {"delete"})
496     public void deleteNonExistent(String testName) throws Exception {
497
498         // Perform setup.
499         setupDeleteNonExistent(testName);
500
501         // Submit the request to the service and store the response.
502         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
503         int statusCode = res.getStatus();
504
505         // Check the status code of the response: does it match
506         // the expected response(s)?
507         if (logger.isDebugEnabled()) {
508             logger.debug(testName + ": status = " + statusCode);
509         }
510         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
511                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
512         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
513     }
514
515     // ---------------------------------------------------------------
516     // Utility tests : tests of code used in tests above
517     // ---------------------------------------------------------------
518     /**
519      * Tests the code for manually submitting data that is used by several
520      * of the methods above.
521      */
522     @Test(dependsOnMethods = {"create", "read"})
523     public void testSubmitRequest() throws Exception {
524
525         // Expected status code: 200 OK
526         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
527
528         // Submit the request to the service and store the response.
529         String method = ServiceRequestType.READ.httpMethodName();
530         String url = getResourceURL(knownResourceId);
531         int statusCode = submitRequest(method, url);
532
533         // Check the status code of the response: does it match
534         // the expected response(s)?
535         if (logger.isDebugEnabled()) {
536             logger.debug("testSubmitRequest: url=" + url +
537                     " status=" + statusCode);
538         }
539         Assert.assertEquals(statusCode, EXPECTED_STATUS);
540
541     }
542
543     // ---------------------------------------------------------------
544     // Utility methods used by tests above
545     // ---------------------------------------------------------------
546     private AccountsCommon createAccountInstance(String firstName, String lastName, String screenName,
547             String passwd, String email) {
548
549         AccountsCommon account = new AccountsCommon();
550         account.setFirstName(firstName);
551         account.setLastName(lastName);
552         account.setScreenName(screenName);
553         account.setUserId(screenName);
554         account.setPassword(Base64.encodeBase64(passwd.getBytes()));
555         account.setEmail(email);
556         account.setPhone("1234567890");
557         if (logger.isDebugEnabled()) {
558             logger.debug("to be created, account common");
559             logger.debug(objectAsXmlString(account,
560                     AccountsCommon.class));
561         }
562         return account;
563
564     }
565 }