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