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