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