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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.account.client.test;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.UUID;
28 import javax.ws.rs.core.Response;
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;
40 import org.testng.Assert;
41 import org.testng.annotations.Test;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.testng.annotations.AfterClass;
48 * AccountServiceTest, carries out tests against a
49 * deployed and running Account Service.
51 * $LastChangedRevision: 917 $
52 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
54 public class AccountServiceTest extends AbstractServiceTestImpl {
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;
63 * This method is called only by the parent class, AbstractServiceTestImpl
67 protected String getServicePathComponent() {
68 return new AccountClient().getServicePathComponent();
71 // ---------------------------------------------------------------
72 // CRUD tests : CREATE tests
73 // ---------------------------------------------------------------
76 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
77 public void create(String testName) throws Exception {
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);
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();
92 // Check the status code of the response: does it match
93 // the expected response(s)?
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);
101 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
102 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
103 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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);
113 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
114 dependsOnMethods = {"create"})
115 public void createForUniqueUser(String testName) throws Exception {
117 setupCreate(testName);
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();
127 if (logger.isDebugEnabled()) {
128 logger.debug(testName + ": status = " + statusCode);
130 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
131 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
132 Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
135 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
136 dependsOnMethods = {"create"})
137 public void createForUniqueScreenName(String testName) throws Exception {
139 setupCreate(testName);
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();
149 if (logger.isDebugEnabled()) {
150 logger.debug(testName + ": status = " + statusCode);
152 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
153 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
154 Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
157 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
158 dependsOnMethods = {"create"})
159 public void createWithInvalidTenant(String testName) throws Exception {
161 setupCreate(testName);
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);
174 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
175 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
176 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
180 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
181 dependsOnMethods = {"create"})
182 public void createWithoutUser(String testName) throws Exception {
184 setupCreate(testName);
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);
197 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
198 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
199 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
202 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
203 dependsOnMethods = {"create"})
204 public void createWithInvalidEmail(String testName) throws Exception {
206 setupCreate(testName);
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);
219 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
220 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
221 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
224 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
225 dependsOnMethods = {"create"})
226 public void createWithoutScreenName(String testName) throws Exception {
228 setupCreate(testName);
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);
241 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
242 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
243 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
246 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
247 dependsOnMethods = {"create"})
248 public void createWithMostInvalid(String testName) throws Exception {
250 setupCreate(testName);
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);
263 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
264 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
265 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
268 //to not cause uniqueness violation for account, createList is removed
270 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
271 dependsOnMethods = {"create"})
272 public void createList(String testName) throws Exception {
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));
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));
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));
311 // Placeholders until the three tests below can be uncommented.
312 // See Issue CSPACE-401.
314 public void createWithEmptyEntityBody(String testName) throws Exception {
318 public void createWithMalformedXml(String testName) throws Exception {
322 public void createWithWrongXmlSchema(String testName) throws Exception {
325 // ---------------------------------------------------------------
326 // CRUD tests : READ tests
327 // ---------------------------------------------------------------
330 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
331 dependsOnMethods = {"create"})
332 public void read(String testName) throws Exception {
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();
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);
347 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
348 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
349 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
351 AccountsCommon output = (AccountsCommon) res.getEntity();
352 Assert.assertNotNull(output);
357 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
358 dependsOnMethods = {"read"})
359 public void readNonExistent(String testName) throws Exception {
362 setupReadNonExistent(testName);
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();
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);
374 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
375 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
376 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
379 // ---------------------------------------------------------------
380 // CRUD tests : READ_LIST tests
381 // ---------------------------------------------------------------
384 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
385 dependsOnMethods = {"createList", "read"})
386 public void readList(String testName) throws Exception {
389 setupReadList(testName);
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();
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);
402 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
403 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
404 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
406 // Optionally output additional data about list members for debugging.
407 boolean iterateThroughList = true;
408 if (iterateThroughList && logger.isDebugEnabled()) {
409 printList(testName, list);
413 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
414 dependsOnMethods = {"createList", "read"})
415 public void searchScreenName(String testName) throws Exception {
418 setupReadList(testName);
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();
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);
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);
443 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
444 dependsOnMethods = {"createList", "read"})
445 public void searchUserId(String testName) throws Exception {
448 setupReadList(testName);
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();
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);
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);
473 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
474 dependsOnMethods = {"createList", "read"})
475 public void searchEmail(String testName) throws Exception {
478 setupReadList(testName);
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();
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);
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);
503 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
504 dependsOnMethods = {"createList", "read"})
505 public void searchScreenNameEmail(String testName) throws Exception {
508 setupReadList(testName);
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();
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);
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);
535 // ---------------------------------------------------------------
536 // CRUD tests : UPDATE tests
537 // ---------------------------------------------------------------
540 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
541 dependsOnMethods = {"read", "readList", "readNonExistent"})
542 public void update(String testName) throws Exception {
545 setupUpdate(testName);
547 AccountClient client = new AccountClient();
548 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
549 if (logger.isDebugEnabled()) {
550 logger.debug(testName + ": read status = " + res.getStatus());
552 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
554 if (logger.isDebugEnabled()) {
555 logger.debug("got object to update with ID: " + knownResourceId);
557 AccountsCommon toUpdateAccount =
558 (AccountsCommon) res.getEntity();
559 Assert.assertNotNull(toUpdateAccount);
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));
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);
576 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
577 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
578 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
581 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
582 Assert.assertNotNull(updatedAccount);
584 Assert.assertEquals(updatedAccount.getEmail(),
585 toUpdateAccount.getEmail(),
586 "Data in updated object did not match submitted data.");
589 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
590 dependsOnMethods = {"update"})
591 public void updatePassword(String testName) throws Exception {
594 setupUpdate(testName);
596 AccountClient client = new AccountClient();
597 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
598 if (logger.isDebugEnabled()) {
599 logger.debug(testName + ": read status = " + res.getStatus());
601 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
603 if (logger.isDebugEnabled()) {
604 logger.debug(testName + ": got object to update password with ID: " + knownResourceId);
606 AccountsCommon toUpdateAccount =
607 (AccountsCommon) res.getEntity();
608 Assert.assertNotNull(toUpdateAccount);
611 toUpdateAccount.setPassword("imagination".getBytes());
612 if (logger.isDebugEnabled()) {
613 logger.debug(testName + ": updated object");
614 logger.debug(objectAsXmlString(toUpdateAccount,
615 AccountsCommon.class));
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);
625 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
626 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
627 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
630 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
631 Assert.assertNotNull(updatedAccount);
633 // Assert.assertEquals(updatedAccount.getPassword(),
634 // toUpdateAccount.getPassword(),
635 // "Data in updated object did not match submitted data.");
638 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
639 dependsOnMethods = {"update"})
640 public void updatePasswordWithoutUser(String testName) throws Exception {
643 setupUpdate(testName);
645 AccountClient client = new AccountClient();
646 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
647 if (logger.isDebugEnabled()) {
648 logger.debug(testName + ": read status = " + res.getStatus());
650 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
652 if (logger.isDebugEnabled()) {
653 logger.debug(testName + " : got object to update with ID: " + knownResourceId);
655 AccountsCommon toUpdateAccount =
656 (AccountsCommon) res.getEntity();
657 Assert.assertNotNull(toUpdateAccount);
659 toUpdateAccount.setUserId(null);
661 toUpdateAccount.setPassword("imagination".getBytes());
662 if (logger.isDebugEnabled()) {
663 logger.debug(testName + " : updated object");
664 logger.debug(objectAsXmlString(toUpdateAccount,
665 AccountsCommon.class));
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);
675 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
676 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
677 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
681 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
682 dependsOnMethods = {"updatePasswordWithoutUser"})
683 public void deactivate(String testName) throws Exception {
686 setupUpdate(testName);
688 AccountClient client = new AccountClient();
689 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
690 if (logger.isDebugEnabled()) {
691 logger.debug(testName + ": read status = " + res.getStatus());
693 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
695 if (logger.isDebugEnabled()) {
696 logger.debug("got object to update with ID: " + knownResourceId);
698 AccountsCommon toUpdateAccount =
699 (AccountsCommon) res.getEntity();
700 Assert.assertNotNull(toUpdateAccount);
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));
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);
717 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
718 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
719 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
722 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
723 Assert.assertNotNull(updatedAccount);
725 Assert.assertEquals(updatedAccount.getStatus(),
726 toUpdateAccount.getStatus(),
727 "Data in updated object did not match submitted data.");
732 // Placeholders until the three tests below can be uncommented.
733 // See Issue CSPACE-401.
735 public void updateWithEmptyEntityBody(String testName) throws Exception {
739 public void updateWithMalformedXml(String testName) throws Exception {
743 public void updateWithWrongXmlSchema(String testName) throws Exception {
747 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
748 dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
749 public void updateNonExistent(String testName) throws Exception {
752 setupUpdateNonExistent(testName);
754 // Submit the request to the service and store the response.
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();
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);
771 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
772 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
773 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
776 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
777 dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
778 public void updateWrongUser(String testName) throws Exception {
782 // Submit the request to the service and store the response.
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());
791 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
793 if (logger.isDebugEnabled()) {
794 logger.debug("got object to update with ID: " + knownResourceId);
796 AccountsCommon toUpdateAccount =
797 (AccountsCommon) res.getEntity();
798 Assert.assertNotNull(toUpdateAccount);
800 toUpdateAccount.setUserId("barneyFake");
801 if (logger.isDebugEnabled()) {
802 logger.debug("updated object with wrongUser");
803 logger.debug(objectAsXmlString(toUpdateAccount,
804 AccountsCommon.class));
807 res = client.update(knownResourceId, toUpdateAccount);
808 int statusCode = res.getStatus();
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);
815 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
816 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
817 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
820 // ---------------------------------------------------------------
821 // CRUD tests : DELETE tests
822 // ---------------------------------------------------------------
825 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
826 dependsOnMethods = {"testSubmitRequest", "updateWrongUser"})
827 public void delete(String testName) throws Exception {
830 setupDelete(testName);
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();
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);
842 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
843 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
844 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
849 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
850 dependsOnMethods = {"delete"})
851 public void deleteNonExistent(String testName) throws Exception {
854 setupDeleteNonExistent(testName);
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();
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);
866 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
867 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
868 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
871 // ---------------------------------------------------------------
872 // Utility tests : tests of code used in tests above
873 // ---------------------------------------------------------------
875 * Tests the code for manually submitting data that is used by several
876 * of the methods above.
878 @Test(dependsOnMethods = {"create", "read"})
879 public void testSubmitRequest() throws Exception {
881 // Expected status code: 200 OK
882 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
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);
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);
895 Assert.assertEquals(statusCode, EXPECTED_STATUS);
899 // ---------------------------------------------------------------
900 // Utility methods used by tests above
901 // ---------------------------------------------------------------
903 * create account instance
908 * @param useScreenName
909 * @param invalidTenant
914 private AccountsCommon createAccountInstance(String screenName,
915 String userName, String passwd, String email,
916 boolean useScreenName, boolean invalidTenant, boolean useUser, boolean usePassword) {
918 AccountsCommon account = new AccountsCommon();
920 account.setScreenName(screenName);
923 account.setUserId(userName);
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);
935 //jaxb encodes password too
936 account.setPassword(passwd.getBytes());
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
950 account.setTenants(atList);
951 addTenant = !addTenant;
954 //use invalid tenant id...called from validation test
955 at.setTenantId(UUID.randomUUID().toString());
957 account.setTenants(atList);
960 if (logger.isDebugEnabled()) {
961 logger.debug("to be created, account common");
962 logger.debug(objectAsXmlString(account,
963 AccountsCommon.class));
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 ...");
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);
986 private void printList(String testName, AccountsCommonList list) {
987 List<AccountsCommonList.AccountListItem> items =
988 list.getAccountListItem();
991 for (AccountsCommonList.AccountListItem item : items) {
992 logger.debug(testName + ": list-item[" + i + "] csid="
994 logger.debug(testName + ": list-item[" + i + "] screenName="
995 + item.getScreenName());
996 logger.debug(testName + ": list-item[" + i + "] URI="