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