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 javax.ws.rs.core.Response;
29 import org.collectionspace.services.client.AccountClient;
30 import org.collectionspace.services.account.AccountsCommon;
31 import org.collectionspace.services.account.AccountsCommonList;
32 import org.collectionspace.services.account.Status;
33 import org.collectionspace.services.client.AccountFactory;
34 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
35 import org.collectionspace.services.client.test.ServiceRequestType;
36 import org.jboss.resteasy.client.ClientResponse;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.testng.annotations.AfterClass;
46 * AccountServiceTest, carries out tests against a
47 * deployed and running Account Service.
49 * $LastChangedRevision: 917 $
50 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
52 public class AccountServiceTest extends AbstractServiceTestImpl {
54 static private final Logger logger =
55 LoggerFactory.getLogger(AccountServiceTest.class);
56 // Instance variables specific to this test.
57 private String knownResourceId = null;
58 private List<String> allResourceIdsCreated = new ArrayList();
59 static boolean addTenant = true;
61 * This method is called only by the parent class, AbstractServiceTestImpl
65 protected String getServicePathComponent() {
66 return new AccountClient().getServicePathComponent();
69 // ---------------------------------------------------------------
70 // CRUD tests : CREATE tests
71 // ---------------------------------------------------------------
74 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
75 public void create(String testName) throws Exception {
77 // Perform setup, such as initializing the type of service request
78 // (e.g. CREATE, DELETE), its valid and expected status codes, and
79 // its associated HTTP method name (e.g. POST, DELETE).
80 setupCreate(testName);
82 // Submit the request to the service and store the response.
83 AccountsCommon account =
84 createAccountInstance("barney", "barney", "hithere08", "barney@dinoland.com",
85 true, false, true, true);
86 AccountClient client = new AccountClient();
87 ClientResponse<Response> res = client.create(account);
88 int statusCode = res.getStatus();
90 // Check the status code of the response: does it match
91 // the expected response(s)?
94 // Does it fall within the set of valid status codes?
95 // Does it exactly match the expected status code?
96 if (logger.isDebugEnabled()) {
97 logger.debug(testName + ": status = " + statusCode);
99 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
100 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
101 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
103 // Store the ID returned from this create operation
104 // for additional tests below.
105 knownResourceId = extractId(res);
106 if (logger.isDebugEnabled()) {
107 logger.debug(testName + ": knownResourceId=" + knownResourceId);
111 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
112 dependsOnMethods = {"create"})
113 public void createForUniqueUser(String testName) throws Exception {
115 setupCreate(testName);
117 // Submit the request to the service and store the response.
118 AccountsCommon account =
119 createAccountInstance("barney1", "barney", "hithere08", "barney@dinoland.com",
120 true, false, true, true);
121 AccountClient client = new AccountClient();
122 ClientResponse<Response> res = client.create(account);
123 int statusCode = res.getStatus();
125 if (logger.isDebugEnabled()) {
126 logger.debug(testName + ": status = " + statusCode);
128 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
129 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
130 Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
134 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
135 dependsOnMethods = {"create"})
136 public void createWithInvalidTenant(String testName) throws Exception {
138 setupCreate(testName);
140 // Submit the request to the service and store the response.
141 AccountsCommon account =
142 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
143 true, true, true, true);
144 AccountClient client = new AccountClient();
145 ClientResponse<Response> res = client.create(account);
146 int statusCode = res.getStatus();
147 // Does it exactly match the expected status code?
148 if (logger.isDebugEnabled()) {
149 logger.debug(testName + ": status = " + statusCode);
151 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
152 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
153 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
157 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
158 dependsOnMethods = {"create"})
159 public void createWithoutUser(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, false, false, 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());
179 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
180 dependsOnMethods = {"create"})
181 public void createWithInvalidEmail(String testName) throws Exception {
183 setupCreate(testName);
185 // Submit the request to the service and store the response.
186 AccountsCommon account =
187 createAccountInstance("babybop", "babybop", "hithere08", "babybop.dinoland.com",
188 true, false, true, true);
189 AccountClient client = new AccountClient();
190 ClientResponse<Response> res = client.create(account);
191 int statusCode = res.getStatus();
192 // Does it exactly match the expected status code?
193 if (logger.isDebugEnabled()) {
194 logger.debug(testName + ": status = " + statusCode);
196 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
197 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
198 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
201 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
202 dependsOnMethods = {"create"})
203 public void createWithoutScreenName(String testName) throws Exception {
205 setupCreate(testName);
207 // Submit the request to the service and store the response.
208 AccountsCommon account =
209 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
210 false, false, true, true);
211 AccountClient client = new AccountClient();
212 ClientResponse<Response> res = client.create(account);
213 int statusCode = res.getStatus();
214 // Does it exactly match the expected status code?
215 if (logger.isDebugEnabled()) {
216 logger.debug(testName + ": status = " + statusCode);
218 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
220 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
223 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
224 dependsOnMethods = {"create"})
225 public void createWithInvalidPassword(String testName) throws Exception {
227 setupCreate(testName);
229 // Submit the request to the service and store the response.
230 AccountsCommon account =
231 createAccountInstance("babybop", "babybop", "shpswd", "babybop@dinoland.com",
232 true, false, true, true);
233 AccountClient client = new AccountClient();
234 ClientResponse<Response> res = client.create(account);
235 int statusCode = res.getStatus();
236 // Does it exactly match the expected status code?
237 if (logger.isDebugEnabled()) {
238 logger.debug(testName + ": status = " + statusCode);
240 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
241 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
242 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
245 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
246 dependsOnMethods = {"create"})
247 public void createWithMostInvalid(String testName) throws Exception {
249 setupCreate(testName);
251 // Submit the request to the service and store the response.
252 AccountsCommon account =
253 createAccountInstance("babybop", "babybop", "hithere08", "babybop/dinoland.com",
254 false, true, false, false);
255 AccountClient client = new AccountClient();
256 ClientResponse<Response> res = client.create(account);
257 int statusCode = res.getStatus();
258 // Does it exactly match the expected status code?
259 if (logger.isDebugEnabled()) {
260 logger.debug(testName + ": status = " + statusCode);
262 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
263 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
264 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
267 //to not cause uniqueness violation for account, createList is removed
269 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
270 dependsOnMethods = {"create"})
271 public void createList(String testName) throws Exception {
273 setupCreate(testName);
274 // Submit the request to the service and store the response.
275 AccountsCommon account1 =
276 createAccountInstance("curious", "curious", "hithere08", "curious@george.com",
277 true, false, true, true);
278 AccountClient client = new AccountClient();
279 ClientResponse<Response> res = client.create(account1);
280 int statusCode = res.getStatus();
281 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
282 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
283 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
284 allResourceIdsCreated.add(extractId(res));
286 AccountsCommon account2 =
287 createAccountInstance("tom", "tom", "hithere09", "tom@jerry.com",
288 true, false, true, true);
289 res = client.create(account2);
290 statusCode = res.getStatus();
291 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
292 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
293 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
294 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
295 allResourceIdsCreated.add(extractId(res));
297 AccountsCommon account3 =
298 createAccountInstance("mj", "mj", "hithere10", "mj@dinoland.com",
299 true, false, true, true);
300 res = client.create(account3);
301 statusCode = res.getStatus();
302 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
303 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
304 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
305 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
306 allResourceIdsCreated.add(extractId(res));
310 // Placeholders until the three tests below can be uncommented.
311 // See Issue CSPACE-401.
313 public void createWithEmptyEntityBody(String testName) throws Exception {
317 public void createWithMalformedXml(String testName) throws Exception {
321 public void createWithWrongXmlSchema(String testName) throws Exception {
324 // ---------------------------------------------------------------
325 // CRUD tests : READ tests
326 // ---------------------------------------------------------------
329 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
330 dependsOnMethods = {"create"})
331 public void read(String testName) throws Exception {
336 // Submit the request to the service and store the response.
337 AccountClient client = new AccountClient();
338 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
339 int statusCode = res.getStatus();
341 // Check the status code of the response: does it match
342 // the expected response(s)?
343 if (logger.isDebugEnabled()) {
344 logger.debug(testName + ": status = " + statusCode);
346 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
347 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
348 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
350 AccountsCommon output = (AccountsCommon) res.getEntity();
351 Assert.assertNotNull(output);
356 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
357 dependsOnMethods = {"read"})
358 public void readNonExistent(String testName) throws Exception {
361 setupReadNonExistent(testName);
363 // Submit the request to the service and store the response.
364 AccountClient client = new AccountClient();
365 ClientResponse<AccountsCommon> res = client.read(NON_EXISTENT_ID);
366 int statusCode = res.getStatus();
368 // Check the status code of the response: does it match
369 // the expected response(s)?
370 if (logger.isDebugEnabled()) {
371 logger.debug(testName + ": status = " + statusCode);
373 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
374 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
375 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
378 // ---------------------------------------------------------------
379 // CRUD tests : READ_LIST tests
380 // ---------------------------------------------------------------
383 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
384 dependsOnMethods = {"createList", "read"})
385 public void readList(String testName) throws Exception {
388 setupReadList(testName);
390 // Submit the request to the service and store the response.
391 AccountClient client = new AccountClient();
392 ClientResponse<AccountsCommonList> res = client.readList();
393 AccountsCommonList list = res.getEntity();
394 int statusCode = res.getStatus();
396 // Check the status code of the response: does it match
397 // the expected response(s)?
398 if (logger.isDebugEnabled()) {
399 logger.debug(testName + ": status = " + statusCode);
401 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
402 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
403 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
405 // Optionally output additional data about list members for debugging.
406 boolean iterateThroughList = true;
407 if (iterateThroughList && logger.isDebugEnabled()) {
408 printList(testName, list);
412 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
413 dependsOnMethods = {"createList", "read"})
414 public void searchScreenName(String testName) throws Exception {
417 setupReadList(testName);
419 // Submit the request to the service and store the response.
420 AccountClient client = new AccountClient();
421 ClientResponse<AccountsCommonList> res =
422 client.readSearchList("tom", null, null);
423 AccountsCommonList list = res.getEntity();
424 int statusCode = res.getStatus();
426 // Check the status code of the response: does it match
427 // the expected response(s)?
428 if (logger.isDebugEnabled()) {
429 logger.debug(testName + ": status = " + statusCode);
431 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
432 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
433 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
434 Assert.assertEquals(1, list.getAccountListItem().size());
435 // Optionally output additional data about list members for debugging.
436 boolean iterateThroughList = true;
437 if (iterateThroughList && logger.isDebugEnabled()) {
438 printList(testName, list);
442 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
443 dependsOnMethods = {"createList", "read"})
444 public void searchUserId(String testName) throws Exception {
447 setupReadList(testName);
449 // Submit the request to the service and store the response.
450 AccountClient client = new AccountClient();
451 ClientResponse<AccountsCommonList> res =
452 client.readSearchList(null, "tom", null);
453 AccountsCommonList list = res.getEntity();
454 int statusCode = res.getStatus();
456 // Check the status code of the response: does it match
457 // the expected response(s)?
458 if (logger.isDebugEnabled()) {
459 logger.debug(testName + ": status = " + statusCode);
461 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
462 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
463 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
464 Assert.assertEquals(1, list.getAccountListItem().size());
465 // Optionally output additional data about list members for debugging.
466 boolean iterateThroughList = true;
467 if (iterateThroughList && logger.isDebugEnabled()) {
468 printList(testName, list);
472 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
473 dependsOnMethods = {"createList", "read"})
474 public void searchEmail(String testName) throws Exception {
477 setupReadList(testName);
479 // Submit the request to the service and store the response.
480 AccountClient client = new AccountClient();
481 ClientResponse<AccountsCommonList> res =
482 client.readSearchList(null, null, "dinoland");
483 AccountsCommonList list = res.getEntity();
484 int statusCode = res.getStatus();
486 // Check the status code of the response: does it match
487 // the expected response(s)?
488 if (logger.isDebugEnabled()) {
489 logger.debug(testName + ": status = " + statusCode);
491 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
492 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
493 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
494 Assert.assertEquals(2, list.getAccountListItem().size());
495 // Optionally output additional data about list members for debugging.
496 boolean iterateThroughList = true;
497 if (iterateThroughList && logger.isDebugEnabled()) {
498 printList(testName, list);
502 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
503 dependsOnMethods = {"createList", "read"})
504 public void searchScreenNameEmail(String testName) throws Exception {
507 setupReadList(testName);
509 // Submit the request to the service and store the response.
510 AccountClient client = new AccountClient();
511 ClientResponse<AccountsCommonList> res =
512 client.readSearchList("tom", null, "jerry");
513 AccountsCommonList list = res.getEntity();
514 int statusCode = res.getStatus();
516 // Check the status code of the response: does it match
517 // the expected response(s)?
518 if (logger.isDebugEnabled()) {
519 logger.debug(testName + ": status = " + statusCode);
521 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
522 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
523 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
524 Assert.assertEquals(1, list.getAccountListItem().size());
525 // Optionally output additional data about list members for debugging.
526 boolean iterateThroughList = true;
527 if (iterateThroughList && logger.isDebugEnabled()) {
528 printList(testName, list);
534 // ---------------------------------------------------------------
535 // CRUD tests : UPDATE tests
536 // ---------------------------------------------------------------
539 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
540 dependsOnMethods = {"read", "readList", "readNonExistent"})
541 public void update(String testName) throws Exception {
544 setupUpdate(testName);
546 AccountClient client = new AccountClient();
547 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
548 if (logger.isDebugEnabled()) {
549 logger.debug(testName + ": read status = " + res.getStatus());
551 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
553 if (logger.isDebugEnabled()) {
554 logger.debug("got object to update with ID: " + knownResourceId);
556 AccountsCommon toUpdateAccount =
557 (AccountsCommon) res.getEntity();
558 Assert.assertNotNull(toUpdateAccount);
560 // Update the content of this resource.
561 toUpdateAccount.setEmail("updated-" + toUpdateAccount.getEmail());
562 if (logger.isDebugEnabled()) {
563 logger.debug("updated object");
564 logger.debug(objectAsXmlString(toUpdateAccount,
565 AccountsCommon.class));
568 // Submit the request to the service and store the response.
569 res = client.update(knownResourceId, toUpdateAccount);
570 int statusCode = res.getStatus();
571 // Check the status code of the response: does it match the expected response(s)?
572 if (logger.isDebugEnabled()) {
573 logger.debug(testName + ": status = " + statusCode);
575 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
576 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
577 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
580 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
581 Assert.assertNotNull(updatedAccount);
583 Assert.assertEquals(updatedAccount.getEmail(),
584 toUpdateAccount.getEmail(),
585 "Data in updated object did not match submitted data.");
588 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
589 dependsOnMethods = {"update"})
590 public void updatePassword(String testName) throws Exception {
593 setupUpdate(testName);
595 AccountClient client = new AccountClient();
596 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
597 if (logger.isDebugEnabled()) {
598 logger.debug(testName + ": read status = " + res.getStatus());
600 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
602 if (logger.isDebugEnabled()) {
603 logger.debug(testName + ": got object to update password with ID: " + knownResourceId);
605 AccountsCommon toUpdateAccount =
606 (AccountsCommon) res.getEntity();
607 Assert.assertNotNull(toUpdateAccount);
610 toUpdateAccount.setPassword("imagination".getBytes());
611 if (logger.isDebugEnabled()) {
612 logger.debug(testName + ": updated object");
613 logger.debug(objectAsXmlString(toUpdateAccount,
614 AccountsCommon.class));
617 // Submit the request to the service and store the response.
618 res = client.update(knownResourceId, toUpdateAccount);
619 int statusCode = res.getStatus();
620 // Check the status code of the response: does it match the expected response(s)?
621 if (logger.isDebugEnabled()) {
622 logger.debug(testName + ": status = " + statusCode);
624 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
625 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
626 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
629 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
630 Assert.assertNotNull(updatedAccount);
632 // Assert.assertEquals(updatedAccount.getPassword(),
633 // toUpdateAccount.getPassword(),
634 // "Data in updated object did not match submitted data.");
637 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
638 dependsOnMethods = {"update"})
639 public void updatePasswordWithoutUser(String testName) throws Exception {
642 setupUpdate(testName);
644 AccountClient client = new AccountClient();
645 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
646 if (logger.isDebugEnabled()) {
647 logger.debug(testName + ": read status = " + res.getStatus());
649 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
651 if (logger.isDebugEnabled()) {
652 logger.debug(testName + " : got object to update with ID: " + knownResourceId);
654 AccountsCommon toUpdateAccount =
655 (AccountsCommon) res.getEntity();
656 Assert.assertNotNull(toUpdateAccount);
658 toUpdateAccount.setUserId(null);
660 toUpdateAccount.setPassword("imagination".getBytes());
661 if (logger.isDebugEnabled()) {
662 logger.debug(testName + " : updated object");
663 logger.debug(objectAsXmlString(toUpdateAccount,
664 AccountsCommon.class));
667 // Submit the request to the service and store the response.
668 res = client.update(knownResourceId, toUpdateAccount);
669 int statusCode = res.getStatus();
670 // Check the status code of the response: does it match the expected response(s)?
671 if (logger.isDebugEnabled()) {
672 logger.debug(testName + ": status = " + statusCode);
674 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
675 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
676 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
680 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
681 dependsOnMethods = {"update"})
682 public void updateInvalidPassword(String testName) throws Exception {
685 setupUpdate(testName);
687 AccountClient client = new AccountClient();
688 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
689 if (logger.isDebugEnabled()) {
690 logger.debug(testName + ": read status = " + res.getStatus());
692 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
694 if (logger.isDebugEnabled()) {
695 logger.debug(testName + ": got object to update password with ID: " + knownResourceId);
697 AccountsCommon toUpdateAccount =
698 (AccountsCommon) res.getEntity();
699 Assert.assertNotNull(toUpdateAccount);
702 toUpdateAccount.setPassword("abc123".getBytes());
703 if (logger.isDebugEnabled()) {
704 logger.debug(testName + ": updated object");
705 logger.debug(objectAsXmlString(toUpdateAccount,
706 AccountsCommon.class));
709 // Submit the request to the service and store the response.
710 res = client.update(knownResourceId, toUpdateAccount);
711 int statusCode = res.getStatus();
712 // Check the status code of the response: does it match the expected response(s)?
713 if (logger.isDebugEnabled()) {
714 logger.debug(testName + ": status = " + statusCode);
716 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
717 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
718 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
722 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
723 dependsOnMethods = {"updatePasswordWithoutUser"})
724 public void deactivate(String testName) throws Exception {
727 setupUpdate(testName);
729 AccountClient client = new AccountClient();
730 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
731 if (logger.isDebugEnabled()) {
732 logger.debug(testName + ": read status = " + res.getStatus());
734 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
736 if (logger.isDebugEnabled()) {
737 logger.debug("got object to update with ID: " + knownResourceId);
739 AccountsCommon toUpdateAccount =
740 (AccountsCommon) res.getEntity();
741 Assert.assertNotNull(toUpdateAccount);
743 // Update the content of this resource.
744 toUpdateAccount.setStatus(Status.INACTIVE);
745 if (logger.isDebugEnabled()) {
746 logger.debug("updated object");
747 logger.debug(objectAsXmlString(toUpdateAccount,
748 AccountsCommon.class));
751 // Submit the request to the service and store the response.
752 res = client.update(knownResourceId, toUpdateAccount);
753 int statusCode = res.getStatus();
754 // Check the status code of the response: does it match the expected response(s)?
755 if (logger.isDebugEnabled()) {
756 logger.debug(testName + ": status = " + statusCode);
758 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
759 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
760 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
763 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
764 Assert.assertNotNull(updatedAccount);
766 Assert.assertEquals(updatedAccount.getStatus(),
767 toUpdateAccount.getStatus(),
768 "Data in updated object did not match submitted data.");
773 // Placeholders until the three tests below can be uncommented.
774 // See Issue CSPACE-401.
776 public void updateWithEmptyEntityBody(String testName) throws Exception {
780 public void updateWithMalformedXml(String testName) throws Exception {
784 public void updateWithWrongXmlSchema(String testName) throws Exception {
788 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
789 dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
790 public void updateNonExistent(String testName) throws Exception {
793 setupUpdateNonExistent(testName);
795 // Submit the request to the service and store the response.
797 // Note: The ID used in this 'create' call may be arbitrary.
798 // The only relevant ID may be the one used in updateAccount(), below.
799 AccountClient client = new AccountClient();
800 AccountsCommon account =
801 createAccountInstance("simba", "simba", "tiger", "simba@lionking.com",
802 true, false, true, true);
803 ClientResponse<AccountsCommon> res =
804 client.update(NON_EXISTENT_ID, account);
805 int statusCode = res.getStatus();
807 // Check the status code of the response: does it match
808 // the expected response(s)?
809 if (logger.isDebugEnabled()) {
810 logger.debug(testName + ": status = " + statusCode);
812 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
813 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
814 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
817 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
818 dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
819 public void updateWrongUser(String testName) throws Exception {
823 // Submit the request to the service and store the response.
825 // Note: The ID used in this 'create' call may be arbitrary.
826 // The only relevant ID may be the one used in updateAccount(), below.
827 AccountClient client = new AccountClient();
828 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
829 if (logger.isDebugEnabled()) {
830 logger.debug(testName + ": read status = " + res.getStatus());
832 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
834 if (logger.isDebugEnabled()) {
835 logger.debug("got object to update with ID: " + knownResourceId);
837 AccountsCommon toUpdateAccount =
838 (AccountsCommon) res.getEntity();
839 Assert.assertNotNull(toUpdateAccount);
841 toUpdateAccount.setUserId("barneyFake");
842 if (logger.isDebugEnabled()) {
843 logger.debug("updated object with wrongUser");
844 logger.debug(objectAsXmlString(toUpdateAccount,
845 AccountsCommon.class));
848 res = client.update(knownResourceId, toUpdateAccount);
849 int statusCode = res.getStatus();
851 // Check the status code of the response: does it match
852 // the expected response(s)?
853 if (logger.isDebugEnabled()) {
854 logger.debug(testName + ": status = " + statusCode);
856 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
857 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
858 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
861 // ---------------------------------------------------------------
862 // CRUD tests : DELETE tests
863 // ---------------------------------------------------------------
866 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
867 dependsOnMethods = {"testSubmitRequest", "updateWrongUser"})
868 public void delete(String testName) throws Exception {
871 setupDelete(testName);
873 // Submit the request to the service and store the response.
874 AccountClient client = new AccountClient();
875 ClientResponse<Response> res = client.delete(knownResourceId);
876 int statusCode = res.getStatus();
878 // Check the status code of the response: does it match
879 // the expected response(s)?
880 if (logger.isDebugEnabled()) {
881 logger.debug(testName + ": status = " + statusCode);
883 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
884 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
885 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
890 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
891 dependsOnMethods = {"delete"})
892 public void deleteNonExistent(String testName) throws Exception {
895 setupDeleteNonExistent(testName);
897 // Submit the request to the service and store the response.
898 AccountClient client = new AccountClient();
899 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
900 int statusCode = res.getStatus();
902 // Check the status code of the response: does it match
903 // the expected response(s)?
904 if (logger.isDebugEnabled()) {
905 logger.debug(testName + ": status = " + statusCode);
907 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
908 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
909 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
912 // ---------------------------------------------------------------
913 // Utility tests : tests of code used in tests above
914 // ---------------------------------------------------------------
916 * Tests the code for manually submitting data that is used by several
917 * of the methods above.
919 @Test(dependsOnMethods = {"create", "read"})
920 public void testSubmitRequest() throws Exception {
922 // Expected status code: 200 OK
923 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
925 // Submit the request to the service and store the response.
926 String method = ServiceRequestType.READ.httpMethodName();
927 String url = getResourceURL(knownResourceId);
928 int statusCode = submitRequest(method, url);
930 // Check the status code of the response: does it match
931 // the expected response(s)?
932 if (logger.isDebugEnabled()) {
933 logger.debug("testSubmitRequest: url=" + url
934 + " status=" + statusCode);
936 Assert.assertEquals(statusCode, EXPECTED_STATUS);
940 // ---------------------------------------------------------------
941 // Utility methods used by tests above
942 // ---------------------------------------------------------------
944 * create account instance
949 * @param useScreenName
950 * @param invalidTenant
955 AccountsCommon createAccountInstance(String screenName,
956 String userName, String passwd, String email,
957 boolean useScreenName, boolean invalidTenant, boolean useUser, boolean usePassword) {
959 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
960 userName, passwd, email, useScreenName,
961 addTenant, invalidTenant, useUser, usePassword);
963 if (logger.isDebugEnabled()) {
964 logger.debug("to be created, account common");
965 logger.debug(objectAsXmlString(account,
966 AccountsCommon.class));
972 @AfterClass(alwaysRun = true)
973 public void cleanUp() {
974 setupDelete("delete");
975 if (logger.isDebugEnabled()) {
976 logger.debug("Cleaning up temporary resources created for testing ...");
978 AccountClient client = new AccountClient();
979 for (String resourceId : allResourceIdsCreated) {
980 // Note: Any non-success responses are ignored and not reported.
981 ClientResponse<Response> res = client.delete(resourceId);
982 int statusCode = res.getStatus();
983 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
984 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
985 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
989 private void printList(String testName, AccountsCommonList list) {
990 List<AccountsCommonList.AccountListItem> items =
991 list.getAccountListItem();
994 for (AccountsCommonList.AccountListItem item : items) {
995 logger.debug(testName + ": list-item[" + i + "] csid="
997 logger.debug(testName + ": list-item[" + i + "] screenName="
998 + item.getScreenName());
999 logger.debug(testName + ": list-item[" + i + "] URI="