]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
aecab1147968ef69628292f36d1bcf95123d50ac
[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.account.client.test;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.UUID;
28 import javax.ws.rs.core.Response;
29
30 import org.apache.commons.codec.binary.Base64;
31 import org.collectionspace.services.client.AccountClient;
32 import org.collectionspace.services.account.AccountsCommon;
33 import org.collectionspace.services.account.AccountsCommonList;
34 import org.collectionspace.services.account.Status;
35 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
36 import org.collectionspace.services.client.test.ServiceRequestType;
37 import org.jboss.resteasy.client.ClientResponse;
38
39 import org.testng.Assert;
40 import org.testng.annotations.Test;
41
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * AccountServiceTest, carries out tests against a
47  * deployed and running Account Service.
48  * 
49  * $LastChangedRevision: 917 $
50  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
51  */
52 public class AccountServiceTest extends AbstractServiceTestImpl {
53
54     private final Logger logger =
55             LoggerFactory.getLogger(AccountServiceTest.class);
56     // Instance variables specific to this test.
57     private AccountClient client = new AccountClient();
58     private String knownResourceId = null;
59     private String resource1Id = null;
60     private String resource2Id = null;
61     private String resource3Id = null;
62     /*
63      * This method is called only by the parent class, AbstractServiceTestImpl
64      */
65
66     @Override
67     protected String getServicePathComponent() {
68         return client.getServicePathComponent();
69     }
70
71     // ---------------------------------------------------------------
72     // CRUD tests : CREATE tests
73     // ---------------------------------------------------------------
74     // Success outcomes
75     @Override
76     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
77     public void create(String testName) throws Exception {
78
79         // Perform setup, such as initializing the type of service request
80         // (e.g. CREATE, DELETE), its valid and expected status codes, and
81         // its associated HTTP method name (e.g. POST, DELETE).
82         setupCreate(testName);
83
84         // Submit the request to the service and store the response.
85         AccountsCommon account =
86                 createAccountInstance("barney", "barney", "hithere08", "barney@dinoland.com",
87                 true, true, true, true);
88         ClientResponse<Response> res = client.create(account);
89         int statusCode = res.getStatus();
90
91         // Check the status code of the response: does it match
92         // the expected response(s)?
93         //
94         // Specifically:
95         // Does it fall within the set of valid status codes?
96         // Does it exactly match the expected status code?
97         if (logger.isDebugEnabled()) {
98             logger.debug(testName + ": status = " + statusCode);
99         }
100         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
101                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
102         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
103
104         // Store the ID returned from this create operation
105         // for additional tests below.
106         knownResourceId = extractId(res);
107         if (logger.isDebugEnabled()) {
108             logger.debug(testName + ": knownResourceId=" + knownResourceId);
109         }
110     }
111
112     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
113     dependsOnMethods = {"create"})
114     public void createForUniqueUser(String testName) throws Exception {
115
116         setupCreate(testName);
117
118         // Submit the request to the service and store the response.
119         AccountsCommon account =
120                 createAccountInstance("barney1", "barney", "hithere08", "barney@dinoland.com",
121                 true, true, true, true);
122         ClientResponse<Response> res = client.create(account);
123         int statusCode = res.getStatus();
124
125         if (logger.isDebugEnabled()) {
126             logger.debug(testName + ": status = " + statusCode);
127         }
128         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
129                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
130         Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
131     }
132
133     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
134     dependsOnMethods = {"create"})
135     public void createForUniqueScreenName(String testName) throws Exception {
136
137         setupCreate(testName);
138
139         // Submit the request to the service and store the response.
140         AccountsCommon account =
141                 createAccountInstance("barney", "otherUser", "hithere08", "barney@dinoland.com",
142                 true, true, true, true);
143         ClientResponse<Response> res = client.create(account);
144         int statusCode = res.getStatus();
145
146         if (logger.isDebugEnabled()) {
147             logger.debug(testName + ": status = " + statusCode);
148         }
149         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
150                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
151         Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
152     }
153
154     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
155     dependsOnMethods = {"create"})
156     public void createWithoutTenant(String testName) throws Exception {
157
158         setupCreate(testName);
159
160         // Submit the request to the service and store the response.
161         AccountsCommon account =
162                 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
163                 true, false, true, true);
164         ClientResponse<Response> res = client.create(account);
165         int statusCode = res.getStatus();
166         // Does it exactly match the expected status code?
167         if (logger.isDebugEnabled()) {
168             logger.debug(testName + ": status = " + statusCode);
169         }
170         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
171                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
172         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
173
174     }
175
176     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
177     dependsOnMethods = {"create"})
178     public void createWithoutUser(String testName) throws Exception {
179
180         setupCreate(testName);
181
182         // Submit the request to the service and store the response.
183         AccountsCommon account =
184                 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
185                 true, true, false, true);
186         ClientResponse<Response> res = client.create(account);
187         int statusCode = res.getStatus();
188         // Does it exactly match the expected status code?
189         if (logger.isDebugEnabled()) {
190             logger.debug(testName + ": status = " + statusCode);
191         }
192         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
193                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
194         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
195     }
196
197     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
198     dependsOnMethods = {"create"})
199     public void createWithInvalidEmail(String testName) throws Exception {
200
201         setupCreate(testName);
202
203         // Submit the request to the service and store the response.
204         AccountsCommon account =
205                 createAccountInstance("babybop", "babybop", "hithere08", "babybop.dinoland.com",
206                 true, true, true, true);
207         ClientResponse<Response> res = client.create(account);
208         int statusCode = res.getStatus();
209         // Does it exactly match the expected status code?
210         if (logger.isDebugEnabled()) {
211             logger.debug(testName + ": status = " + statusCode);
212         }
213         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
214                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
215         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
216     }
217
218     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
219     dependsOnMethods = {"create"})
220     public void createWithoutScreenName(String testName) throws Exception {
221
222         setupCreate(testName);
223
224         // Submit the request to the service and store the response.
225         AccountsCommon account =
226                 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
227                 false, true, true, true);
228         ClientResponse<Response> res = client.create(account);
229         int statusCode = res.getStatus();
230         // Does it exactly match the expected status code?
231         if (logger.isDebugEnabled()) {
232             logger.debug(testName + ": status = " + statusCode);
233         }
234         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
235                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
236         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
237     }
238
239     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
240     dependsOnMethods = {"create"})
241     public void createWithMostInvalid(String testName) throws Exception {
242
243         setupCreate(testName);
244
245         // Submit the request to the service and store the response.
246         AccountsCommon account =
247                 createAccountInstance("babybop", "babybop", "hithere08", "babybop/dinoland.com",
248                 false, false, false, false);
249         ClientResponse<Response> res = client.create(account);
250         int statusCode = res.getStatus();
251         // Does it exactly match the expected status code?
252         if (logger.isDebugEnabled()) {
253             logger.debug(testName + ": status = " + statusCode);
254         }
255         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
256                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
257         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
258     }
259
260     //to not cause uniqueness violation for account, createList is removed
261     @Override
262     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
263     dependsOnMethods = {"create"})
264     public void createList(String testName) throws Exception {
265
266         setupCreate(testName);
267         // Submit the request to the service and store the response.
268         AccountsCommon account1 =
269                 createAccountInstance("curious", "curious", "hithere08", "curious@george.com",
270                 true, true, true, true);
271         ClientResponse<Response> res = client.create(account1);
272         int statusCode = res.getStatus();
273         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
274                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
275         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
276         resource1Id = extractId(res);
277
278         AccountsCommon account2 =
279                 createAccountInstance("tom", "tom", "hithere09", "tom@jerry.com",
280                 true, true, true, true);
281         res = client.create(account2);
282         statusCode = res.getStatus();
283         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
284                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
285         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
286         resource2Id = extractId(res);
287
288         AccountsCommon account3 =
289                 createAccountInstance("mj", "mj", "hithere10", "mj@dinoland.com",
290                 true, true, true, true);
291         res = client.create(account3);
292         statusCode = res.getStatus();
293         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
294                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
295         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
296         resource3Id = extractId(res);
297     }
298
299     // Failure outcomes
300     // Placeholders until the three tests below can be uncommented.
301     // See Issue CSPACE-401.
302     @Override
303     public void createWithEmptyEntityBody(String testName) throws Exception {
304     }
305
306     @Override
307     public void createWithMalformedXml(String testName) throws Exception {
308     }
309
310     @Override
311     public void createWithWrongXmlSchema(String testName) throws Exception {
312     }
313
314     // ---------------------------------------------------------------
315     // CRUD tests : READ tests
316     // ---------------------------------------------------------------
317     // Success outcomes
318     @Override
319     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
320     dependsOnMethods = {"create"})
321     public void read(String testName) throws Exception {
322
323         // Perform setup.
324         setupRead(testName);
325
326         // Submit the request to the service and store the response.
327         ClientResponse<AccountsCommon> res = client.read(knownResourceId);
328         int statusCode = res.getStatus();
329
330         // Check the status code of the response: does it match
331         // the expected response(s)?
332         if (logger.isDebugEnabled()) {
333             logger.debug(testName + ": status = " + statusCode);
334         }
335         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
336                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
337         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
338
339         AccountsCommon output = (AccountsCommon) res.getEntity();
340         Assert.assertNotNull(output);
341     }
342
343     // Failure outcomes
344     @Override
345     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
346     dependsOnMethods = {"read"})
347     public void readNonExistent(String testName) throws Exception {
348
349         // Perform setup.
350         setupReadNonExistent(testName);
351
352         // Submit the request to the service and store the response.
353         ClientResponse<AccountsCommon> res = client.read(NON_EXISTENT_ID);
354         int statusCode = res.getStatus();
355
356         // Check the status code of the response: does it match
357         // the expected response(s)?
358         if (logger.isDebugEnabled()) {
359             logger.debug(testName + ": status = " + statusCode);
360         }
361         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
362                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
363         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
364     }
365
366     // ---------------------------------------------------------------
367     // CRUD tests : READ_LIST tests
368     // ---------------------------------------------------------------
369     // Success outcomes
370     @Override
371     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
372     dependsOnMethods = {"createList", "read"})
373     public void readList(String testName) throws Exception {
374
375         // Perform setup.
376         setupReadList(testName);
377
378         // Submit the request to the service and store the response.
379         ClientResponse<AccountsCommonList> res = client.readList();
380         AccountsCommonList list = res.getEntity();
381         int statusCode = res.getStatus();
382
383         // Check the status code of the response: does it match
384         // the expected response(s)?
385         if (logger.isDebugEnabled()) {
386             logger.debug(testName + ": status = " + statusCode);
387         }
388         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
389                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
390         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
391
392         // Optionally output additional data about list members for debugging.
393         boolean iterateThroughList = true;
394         if (iterateThroughList && logger.isDebugEnabled()) {
395             printList(testName, list);
396         }
397     }
398
399     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
400     dependsOnMethods = {"createList", "read"})
401     public void searchScreenName(String testName) throws Exception {
402
403         // Perform setup.
404         setupReadList(testName);
405
406         // Submit the request to the service and store the response.
407         ClientResponse<AccountsCommonList> res = client.readSearchList("tom", null, null);
408         AccountsCommonList list = res.getEntity();
409         int statusCode = res.getStatus();
410
411         // Check the status code of the response: does it match
412         // the expected response(s)?
413         if (logger.isDebugEnabled()) {
414             logger.debug(testName + ": status = " + statusCode);
415         }
416         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
417                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
418         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
419         Assert.assertEquals(1, list.getAccountListItem().size());
420         // Optionally output additional data about list members for debugging.
421         boolean iterateThroughList = true;
422         if (iterateThroughList && logger.isDebugEnabled()) {
423             printList(testName, list);
424         }
425     }
426
427     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
428     dependsOnMethods = {"createList", "read"})
429     public void searchUserId(String testName) throws Exception {
430
431         // Perform setup.
432         setupReadList(testName);
433
434         // Submit the request to the service and store the response.
435         ClientResponse<AccountsCommonList> res = client.readSearchList(null, "tom", null);
436         AccountsCommonList list = res.getEntity();
437         int statusCode = res.getStatus();
438
439         // Check the status code of the response: does it match
440         // the expected response(s)?
441         if (logger.isDebugEnabled()) {
442             logger.debug(testName + ": status = " + statusCode);
443         }
444         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
445                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
446         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
447         Assert.assertEquals(1, list.getAccountListItem().size());
448         // Optionally output additional data about list members for debugging.
449         boolean iterateThroughList = true;
450         if (iterateThroughList && logger.isDebugEnabled()) {
451             printList(testName, list);
452         }
453     }
454
455     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
456     dependsOnMethods = {"createList", "read"})
457     public void searchEmail(String testName) throws Exception {
458
459         // Perform setup.
460         setupReadList(testName);
461
462         // Submit the request to the service and store the response.
463         ClientResponse<AccountsCommonList> res = client.readSearchList(null, null, "dinoland");
464         AccountsCommonList list = res.getEntity();
465         int statusCode = res.getStatus();
466
467         // Check the status code of the response: does it match
468         // the expected response(s)?
469         if (logger.isDebugEnabled()) {
470             logger.debug(testName + ": status = " + statusCode);
471         }
472         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
473                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
474         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
475         Assert.assertEquals(2, list.getAccountListItem().size());
476         // Optionally output additional data about list members for debugging.
477         boolean iterateThroughList = true;
478         if (iterateThroughList && logger.isDebugEnabled()) {
479             printList(testName, list);
480         }
481     }
482
483     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
484     dependsOnMethods = {"createList", "read"})
485     public void searchScreenNameEmail(String testName) throws Exception {
486
487         // Perform setup.
488         setupReadList(testName);
489
490         // Submit the request to the service and store the response.
491         ClientResponse<AccountsCommonList> res = client.readSearchList("tom", null, "jerry");
492         AccountsCommonList list = res.getEntity();
493         int statusCode = res.getStatus();
494
495         // Check the status code of the response: does it match
496         // the expected response(s)?
497         if (logger.isDebugEnabled()) {
498             logger.debug(testName + ": status = " + statusCode);
499         }
500         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
501                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
502         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
503         Assert.assertEquals(1, list.getAccountListItem().size());
504         // Optionally output additional data about list members for debugging.
505         boolean iterateThroughList = true;
506         if (iterateThroughList && logger.isDebugEnabled()) {
507             printList(testName, list);
508         }
509     }
510
511     // Failure outcomes
512     // None at present.
513     // ---------------------------------------------------------------
514     // CRUD tests : UPDATE tests
515     // ---------------------------------------------------------------
516     // Success outcomes
517     @Override
518     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
519     dependsOnMethods = {"read", "readList", "readNonExistent"})
520     public void update(String testName) throws Exception {
521
522         // Perform setup.
523         setupUpdate(testName);
524
525
526         ClientResponse<AccountsCommon> res =
527                 client.read(knownResourceId);
528         if (logger.isDebugEnabled()) {
529             logger.debug(testName + ": read status = " + res.getStatus());
530         }
531         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
532
533         if (logger.isDebugEnabled()) {
534             logger.debug("got object to update with ID: " + knownResourceId);
535         }
536         AccountsCommon toUpdateAccount =
537                 (AccountsCommon) res.getEntity();
538         Assert.assertNotNull(toUpdateAccount);
539
540         // Update the content of this resource.
541         toUpdateAccount.setEmail("updated-" + toUpdateAccount.getEmail());
542         if (logger.isDebugEnabled()) {
543             logger.debug("updated object");
544             logger.debug(objectAsXmlString(toUpdateAccount,
545                     AccountsCommon.class));
546         }
547
548         // Submit the request to the service and store the response.
549         res = client.update(knownResourceId, toUpdateAccount);
550         int statusCode = res.getStatus();
551         // Check the status code of the response: does it match the expected response(s)?
552         if (logger.isDebugEnabled()) {
553             logger.debug(testName + ": status = " + statusCode);
554         }
555         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
556                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
557         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
558
559
560         AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
561         Assert.assertNotNull(updatedAccount);
562
563         Assert.assertEquals(updatedAccount.getEmail(),
564                 toUpdateAccount.getEmail(),
565                 "Data in updated object did not match submitted data.");
566     }
567
568     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
569     dependsOnMethods = {"update"})
570     public void updatePassword(String testName) throws Exception {
571
572         // Perform setup.
573         setupUpdate(testName);
574
575         ClientResponse<AccountsCommon> res =
576                 client.read(knownResourceId);
577         if (logger.isDebugEnabled()) {
578             logger.debug(testName + ": read status = " + res.getStatus());
579         }
580         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
581
582         if (logger.isDebugEnabled()) {
583             logger.debug(testName + ": got object to update password with ID: " + knownResourceId);
584         }
585         AccountsCommon toUpdateAccount =
586                 (AccountsCommon) res.getEntity();
587         Assert.assertNotNull(toUpdateAccount);
588
589         //change password
590         toUpdateAccount.setPassword(Base64.encodeBase64("imagination".getBytes()));
591         if (logger.isDebugEnabled()) {
592             logger.debug(testName + ": updated object");
593             logger.debug(objectAsXmlString(toUpdateAccount,
594                     AccountsCommon.class));
595         }
596
597         // Submit the request to the service and store the response.
598         res = client.update(knownResourceId, toUpdateAccount);
599         int statusCode = res.getStatus();
600         // Check the status code of the response: does it match the expected response(s)?
601         if (logger.isDebugEnabled()) {
602             logger.debug(testName + ": status = " + statusCode);
603         }
604         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
605                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
606         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
607
608
609         AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
610         Assert.assertNotNull(updatedAccount);
611
612 //        Assert.assertEquals(updatedAccount.getPassword(),
613 //                toUpdateAccount.getPassword(),
614 //                "Data in updated object did not match submitted data.");
615     }
616
617     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
618     dependsOnMethods = {"update"})
619     public void updatePasswordWithoutUser(String testName) throws Exception {
620
621         // Perform setup.
622         setupUpdate(testName);
623
624         ClientResponse<AccountsCommon> res =
625                 client.read(knownResourceId);
626         if (logger.isDebugEnabled()) {
627             logger.debug(testName + ": read status = " + res.getStatus());
628         }
629         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
630
631         if (logger.isDebugEnabled()) {
632             logger.debug(testName + " : got object to update with ID: " + knownResourceId);
633         }
634         AccountsCommon toUpdateAccount =
635                 (AccountsCommon) res.getEntity();
636         Assert.assertNotNull(toUpdateAccount);
637
638         toUpdateAccount.setUserId(null);
639         //change password
640         toUpdateAccount.setPassword(Base64.encodeBase64("imagination".getBytes()));
641         if (logger.isDebugEnabled()) {
642             logger.debug(testName + " : updated object");
643             logger.debug(objectAsXmlString(toUpdateAccount,
644                     AccountsCommon.class));
645         }
646
647         // Submit the request to the service and store the response.
648         res = client.update(knownResourceId, toUpdateAccount);
649         int statusCode = res.getStatus();
650         // Check the status code of the response: does it match the expected response(s)?
651         if (logger.isDebugEnabled()) {
652             logger.debug(testName + ": status = " + statusCode);
653         }
654         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
655                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
656         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
657
658     }
659
660     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
661     dependsOnMethods = {"updatePasswordWithoutUser"})
662     public void deactivate(String testName) throws Exception {
663
664         // Perform setup.
665         setupUpdate(testName);
666
667         ClientResponse<AccountsCommon> res =
668                 client.read(knownResourceId);
669         if (logger.isDebugEnabled()) {
670             logger.debug(testName + ": read status = " + res.getStatus());
671         }
672         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
673
674         if (logger.isDebugEnabled()) {
675             logger.debug("got object to update with ID: " + knownResourceId);
676         }
677         AccountsCommon toUpdateAccount =
678                 (AccountsCommon) res.getEntity();
679         Assert.assertNotNull(toUpdateAccount);
680
681         // Update the content of this resource.
682         toUpdateAccount.setStatus(Status.INACTIVE);
683         if (logger.isDebugEnabled()) {
684             logger.debug("updated object");
685             logger.debug(objectAsXmlString(toUpdateAccount,
686                     AccountsCommon.class));
687         }
688
689         // Submit the request to the service and store the response.
690         res = client.update(knownResourceId, toUpdateAccount);
691         int statusCode = res.getStatus();
692         // Check the status code of the response: does it match the expected response(s)?
693         if (logger.isDebugEnabled()) {
694             logger.debug(testName + ": status = " + statusCode);
695         }
696         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
697                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
698         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
699
700
701         AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
702         Assert.assertNotNull(updatedAccount);
703
704         Assert.assertEquals(updatedAccount.getStatus(),
705                 toUpdateAccount.getStatus(),
706                 "Data in updated object did not match submitted data.");
707
708     }
709
710     // Failure outcomes
711     // Placeholders until the three tests below can be uncommented.
712     // See Issue CSPACE-401.
713     @Override
714     public void updateWithEmptyEntityBody(String testName) throws Exception {
715     }
716
717     @Override
718     public void updateWithMalformedXml(String testName) throws Exception {
719     }
720
721     @Override
722     public void updateWithWrongXmlSchema(String testName) throws Exception {
723     }
724
725     @Override
726     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
727     dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
728     public void updateNonExistent(String testName) throws Exception {
729
730         // Perform setup.
731         setupUpdateNonExistent(testName);
732
733         // Submit the request to the service and store the response.
734         //
735         // Note: The ID used in this 'create' call may be arbitrary.
736         // The only relevant ID may be the one used in updateAccount(), below.
737         AccountsCommon account =
738                 createAccountInstance("simba", "simba", "tiger", "simba@lionking.com",
739                 true, true, true, true);
740         ClientResponse<AccountsCommon> res =
741                 client.update(NON_EXISTENT_ID, account);
742         int statusCode = res.getStatus();
743
744         // Check the status code of the response: does it match
745         // the expected response(s)?
746         if (logger.isDebugEnabled()) {
747             logger.debug(testName + ": status = " + statusCode);
748         }
749         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
750                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
751         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
752     }
753
754     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
755     dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
756     public void updateWrongUser(String testName) throws Exception {
757
758         setupUpdate();
759         // Submit the request to the service and store the response.
760         //
761         // Note: The ID used in this 'create' call may be arbitrary.
762         // The only relevant ID may be the one used in updateAccount(), below.
763         ClientResponse<AccountsCommon> res =
764                 client.read(knownResourceId);
765         if (logger.isDebugEnabled()) {
766             logger.debug(testName + ": read status = " + res.getStatus());
767         }
768         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
769
770         if (logger.isDebugEnabled()) {
771             logger.debug("got object to update with ID: " + knownResourceId);
772         }
773         AccountsCommon toUpdateAccount =
774                 (AccountsCommon) res.getEntity();
775         Assert.assertNotNull(toUpdateAccount);
776
777         toUpdateAccount.setUserId("barneyFake");
778         if (logger.isDebugEnabled()) {
779             logger.debug("updated object with wrongUser");
780             logger.debug(objectAsXmlString(toUpdateAccount,
781                     AccountsCommon.class));
782         }
783
784         res = client.update(knownResourceId, toUpdateAccount);
785         int statusCode = res.getStatus();
786
787         // Check the status code of the response: does it match
788         // the expected response(s)?
789         if (logger.isDebugEnabled()) {
790             logger.debug(testName + ": status = " + statusCode);
791         }
792         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
793                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
794         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
795     }
796
797     // ---------------------------------------------------------------
798     // CRUD tests : DELETE tests
799     // ---------------------------------------------------------------
800     // Success outcomes
801     @Override
802     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
803     dependsOnMethods = {"testSubmitRequest", "updateWrongUser"})
804     public void delete(String testName) throws Exception {
805
806         // Perform setup.
807         setupDelete(testName);
808
809         // Submit the request to the service and store the response.
810         ClientResponse<Response> res = client.delete(knownResourceId);
811         int statusCode = res.getStatus();
812
813         // Check the status code of the response: does it match
814         // the expected response(s)?
815         if (logger.isDebugEnabled()) {
816             logger.debug(testName + ": status = " + statusCode);
817         }
818         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
819                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
820         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
821     }
822
823     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
824     dependsOnMethods = {"testSubmitRequest", "updateWrongUser"})
825     public void deleteList(String testName) throws Exception {
826
827         // Perform setup.
828         setupDelete(testName);
829
830         ClientResponse<Response> res = client.delete(resource1Id);
831         int statusCode = res.getStatus();
832         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
833                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
834         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
835
836         res = client.delete(resource2Id);
837         statusCode = res.getStatus();
838         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
839                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
840         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
841
842         res = client.delete(resource3Id);
843         statusCode = res.getStatus();
844         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
845                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
846         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
847     }
848
849     // Failure outcomes
850     @Override
851     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
852     dependsOnMethods = {"delete"})
853     public void deleteNonExistent(String testName) throws Exception {
854
855         // Perform setup.
856         setupDeleteNonExistent(testName);
857
858         // Submit the request to the service and store the response.
859         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
860         int statusCode = res.getStatus();
861
862         // Check the status code of the response: does it match
863         // the expected response(s)?
864         if (logger.isDebugEnabled()) {
865             logger.debug(testName + ": status = " + statusCode);
866         }
867         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
868                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
869         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
870     }
871
872     // ---------------------------------------------------------------
873     // Utility tests : tests of code used in tests above
874     // ---------------------------------------------------------------
875     /**
876      * Tests the code for manually submitting data that is used by several
877      * of the methods above.
878      */
879     @Test(dependsOnMethods = {"create", "read"})
880     public void testSubmitRequest() throws Exception {
881
882         // Expected status code: 200 OK
883         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
884
885         // Submit the request to the service and store the response.
886         String method = ServiceRequestType.READ.httpMethodName();
887         String url = getResourceURL(knownResourceId);
888         int statusCode = submitRequest(method, url);
889
890         // Check the status code of the response: does it match
891         // the expected response(s)?
892         if (logger.isDebugEnabled()) {
893             logger.debug("testSubmitRequest: url=" + url
894                     + " status=" + statusCode);
895         }
896         Assert.assertEquals(statusCode, EXPECTED_STATUS);
897
898     }
899
900     // ---------------------------------------------------------------
901     // Utility methods used by tests above
902     // ---------------------------------------------------------------
903     /**
904      * create account instance
905      * @param screenName
906      * @param userName
907      * @param passwd
908      * @param email
909      * @param useScreenName
910      * @param useTenant
911      * @param useUser
912      * @param usePassword
913      * @return
914      */
915     private AccountsCommon createAccountInstance(String screenName,
916             String userName, String passwd, String email,
917             boolean useScreenName, boolean useTenant, boolean useUser, boolean usePassword) {
918
919         AccountsCommon account = new AccountsCommon();
920         if (useScreenName) {
921             account.setScreenName(screenName);
922         }
923         if (useUser) {
924             account.setUserId(userName);
925         }
926         if (usePassword) {
927             account.setPassword(Base64.encodeBase64(passwd.getBytes()));
928         }
929         account.setPersonRefName(screenName);
930         account.setEmail(email);
931         account.setPhone("1234567890");
932         if (useTenant) {
933             List<AccountsCommon.Tenant> atl = new ArrayList<AccountsCommon.Tenant>();
934             AccountsCommon.Tenant at = new AccountsCommon.Tenant();
935             at.setId(UUID.randomUUID().toString());
936             at.setName("movingimages.us");
937             atl.add(at);
938
939             AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
940             at2.setId(UUID.randomUUID().toString());
941             at2.setName("collectionspace.org");
942             atl.add(at2);
943             account.setTenant(atl);
944         }
945         if (logger.isDebugEnabled()) {
946             logger.debug("to be created, account common");
947             logger.debug(objectAsXmlString(account,
948                     AccountsCommon.class));
949         }
950         return account;
951
952     }
953
954     private void printList(String testName, AccountsCommonList list) {
955         List<AccountsCommonList.AccountListItem> items =
956                 list.getAccountListItem();
957         int i = 0;
958
959         for (AccountsCommonList.AccountListItem item : items) {
960             logger.debug(testName + ": list-item[" + i + "] csid="
961                     + item.getCsid());
962             logger.debug(testName + ": list-item[" + i + "] screenName="
963                     + item.getScreenName());
964             logger.debug(testName + ": list-item[" + i + "] URI="
965                     + item.getUri());
966             i++;
967
968         }
969     }
970 }