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