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