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 AccountClient client = new AccountClient();
60 private String knownResourceId = null;
61 private List<String> allResourceIdsCreated = new ArrayList();
62 boolean addTenant = true;
64 * This method is called only by the parent class, AbstractServiceTestImpl
68 protected String getServicePathComponent() {
69 return client.getServicePathComponent();
72 // ---------------------------------------------------------------
73 // CRUD tests : CREATE tests
74 // ---------------------------------------------------------------
77 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
78 public void create(String testName) throws Exception {
80 // Perform setup, such as initializing the type of service request
81 // (e.g. CREATE, DELETE), its valid and expected status codes, and
82 // its associated HTTP method name (e.g. POST, DELETE).
83 setupCreate(testName);
85 // Submit the request to the service and store the response.
86 AccountsCommon account =
87 createAccountInstance("barney", "barney", "hithere08", "barney@dinoland.com",
88 true, false, true, true);
89 ClientResponse<Response> res = client.create(account);
90 int statusCode = res.getStatus();
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 ClientResponse<Response> res = client.create(account);
124 int statusCode = res.getStatus();
126 if (logger.isDebugEnabled()) {
127 logger.debug(testName + ": status = " + statusCode);
129 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
130 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
131 Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
134 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
135 dependsOnMethods = {"create"})
136 public void createForUniqueScreenName(String testName) throws Exception {
138 setupCreate(testName);
140 // Submit the request to the service and store the response.
141 AccountsCommon account =
142 createAccountInstance("barney", "otherUser", "hithere08", "barney@dinoland.com",
143 true, false, true, true);
144 ClientResponse<Response> res = client.create(account);
145 int statusCode = res.getStatus();
147 if (logger.isDebugEnabled()) {
148 logger.debug(testName + ": status = " + statusCode);
150 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
151 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
152 Assert.assertEquals(statusCode, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
155 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
156 dependsOnMethods = {"create"})
157 public void createWithInvalidTenant(String testName) throws Exception {
159 setupCreate(testName);
161 // Submit the request to the service and store the response.
162 AccountsCommon account =
163 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
164 true, true, true, true);
165 ClientResponse<Response> res = client.create(account);
166 int statusCode = res.getStatus();
167 // Does it exactly match the expected status code?
168 if (logger.isDebugEnabled()) {
169 logger.debug(testName + ": status = " + statusCode);
171 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
172 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
173 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
177 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
178 dependsOnMethods = {"create"})
179 public void createWithoutUser(String testName) throws Exception {
181 setupCreate(testName);
183 // Submit the request to the service and store the response.
184 AccountsCommon account =
185 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
186 true, false, false, true);
187 ClientResponse<Response> res = client.create(account);
188 int statusCode = res.getStatus();
189 // Does it exactly match the expected status code?
190 if (logger.isDebugEnabled()) {
191 logger.debug(testName + ": status = " + statusCode);
193 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
194 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
195 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
198 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
199 dependsOnMethods = {"create"})
200 public void createWithInvalidEmail(String testName) throws Exception {
202 setupCreate(testName);
204 // Submit the request to the service and store the response.
205 AccountsCommon account =
206 createAccountInstance("babybop", "babybop", "hithere08", "babybop.dinoland.com",
207 true, false, true, true);
208 ClientResponse<Response> res = client.create(account);
209 int statusCode = res.getStatus();
210 // Does it exactly match the expected status code?
211 if (logger.isDebugEnabled()) {
212 logger.debug(testName + ": status = " + statusCode);
214 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
215 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
216 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
219 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
220 dependsOnMethods = {"create"})
221 public void createWithoutScreenName(String testName) throws Exception {
223 setupCreate(testName);
225 // Submit the request to the service and store the response.
226 AccountsCommon account =
227 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
228 false, false, true, true);
229 ClientResponse<Response> res = client.create(account);
230 int statusCode = res.getStatus();
231 // Does it exactly match the expected status code?
232 if (logger.isDebugEnabled()) {
233 logger.debug(testName + ": status = " + statusCode);
235 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
236 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
237 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
240 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
241 dependsOnMethods = {"create"})
242 public void createWithMostInvalid(String testName) throws Exception {
244 setupCreate(testName);
246 // Submit the request to the service and store the response.
247 AccountsCommon account =
248 createAccountInstance("babybop", "babybop", "hithere08", "babybop/dinoland.com",
249 false, true, false, false);
250 ClientResponse<Response> res = client.create(account);
251 int statusCode = res.getStatus();
252 // Does it exactly match the expected status code?
253 if (logger.isDebugEnabled()) {
254 logger.debug(testName + ": status = " + statusCode);
256 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
257 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
258 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
261 //to not cause uniqueness violation for account, createList is removed
263 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
264 dependsOnMethods = {"create"})
265 public void createList(String testName) throws Exception {
267 setupCreate(testName);
268 // Submit the request to the service and store the response.
269 AccountsCommon account1 =
270 createAccountInstance("curious", "curious", "hithere08", "curious@george.com",
271 true, false, true, true);
272 ClientResponse<Response> res = client.create(account1);
273 int statusCode = res.getStatus();
274 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
275 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
276 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
277 allResourceIdsCreated.add(extractId(res));
279 AccountsCommon account2 =
280 createAccountInstance("tom", "tom", "hithere09", "tom@jerry.com",
281 true, false, true, true);
282 res = client.create(account2);
283 statusCode = res.getStatus();
284 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
285 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
286 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
287 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
288 allResourceIdsCreated.add(extractId(res));
290 AccountsCommon account3 =
291 createAccountInstance("mj", "mj", "hithere10", "mj@dinoland.com",
292 true, false, true, true);
293 res = client.create(account3);
294 statusCode = res.getStatus();
295 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
296 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
297 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
298 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
299 allResourceIdsCreated.add(extractId(res));
303 // Placeholders until the three tests below can be uncommented.
304 // See Issue CSPACE-401.
306 public void createWithEmptyEntityBody(String testName) throws Exception {
310 public void createWithMalformedXml(String testName) throws Exception {
314 public void createWithWrongXmlSchema(String testName) throws Exception {
317 // ---------------------------------------------------------------
318 // CRUD tests : READ tests
319 // ---------------------------------------------------------------
322 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
323 dependsOnMethods = {"create"})
324 public void read(String testName) throws Exception {
329 // Submit the request to the service and store the response.
330 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
331 int statusCode = res.getStatus();
333 // Check the status code of the response: does it match
334 // the expected response(s)?
335 if (logger.isDebugEnabled()) {
336 logger.debug(testName + ": status = " + statusCode);
338 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
339 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
340 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
342 AccountsCommon output = (AccountsCommon) res.getEntity();
343 Assert.assertNotNull(output);
348 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
349 dependsOnMethods = {"read"})
350 public void readNonExistent(String testName) throws Exception {
353 setupReadNonExistent(testName);
355 // Submit the request to the service and store the response.
356 ClientResponse<AccountsCommon> res = client.read(NON_EXISTENT_ID);
357 int statusCode = res.getStatus();
359 // Check the status code of the response: does it match
360 // the expected response(s)?
361 if (logger.isDebugEnabled()) {
362 logger.debug(testName + ": status = " + statusCode);
364 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
365 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
366 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
369 // ---------------------------------------------------------------
370 // CRUD tests : READ_LIST tests
371 // ---------------------------------------------------------------
374 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
375 dependsOnMethods = {"createList", "read"})
376 public void readList(String testName) throws Exception {
379 setupReadList(testName);
381 // Submit the request to the service and store the response.
382 ClientResponse<AccountsCommonList> res = client.readList();
383 AccountsCommonList list = res.getEntity();
384 int statusCode = res.getStatus();
386 // Check the status code of the response: does it match
387 // the expected response(s)?
388 if (logger.isDebugEnabled()) {
389 logger.debug(testName + ": status = " + statusCode);
391 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
392 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
393 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
395 // Optionally output additional data about list members for debugging.
396 boolean iterateThroughList = true;
397 if (iterateThroughList && logger.isDebugEnabled()) {
398 printList(testName, list);
402 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
403 dependsOnMethods = {"createList", "read"})
404 public void searchScreenName(String testName) throws Exception {
407 setupReadList(testName);
409 // Submit the request to the service and store the response.
410 ClientResponse<AccountsCommonList> res = client.readSearchList("tom", null, null);
411 AccountsCommonList list = res.getEntity();
412 int statusCode = res.getStatus();
414 // Check the status code of the response: does it match
415 // the expected response(s)?
416 if (logger.isDebugEnabled()) {
417 logger.debug(testName + ": status = " + statusCode);
419 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
420 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
421 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
422 Assert.assertEquals(1, list.getAccountListItem().size());
423 // Optionally output additional data about list members for debugging.
424 boolean iterateThroughList = true;
425 if (iterateThroughList && logger.isDebugEnabled()) {
426 printList(testName, list);
430 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
431 dependsOnMethods = {"createList", "read"})
432 public void searchUserId(String testName) throws Exception {
435 setupReadList(testName);
437 // Submit the request to the service and store the response.
438 ClientResponse<AccountsCommonList> res = client.readSearchList(null, "tom", null);
439 AccountsCommonList list = res.getEntity();
440 int statusCode = res.getStatus();
442 // Check the status code of the response: does it match
443 // the expected response(s)?
444 if (logger.isDebugEnabled()) {
445 logger.debug(testName + ": status = " + statusCode);
447 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
448 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
449 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
450 Assert.assertEquals(1, list.getAccountListItem().size());
451 // Optionally output additional data about list members for debugging.
452 boolean iterateThroughList = true;
453 if (iterateThroughList && logger.isDebugEnabled()) {
454 printList(testName, list);
458 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
459 dependsOnMethods = {"createList", "read"})
460 public void searchEmail(String testName) throws Exception {
463 setupReadList(testName);
465 // Submit the request to the service and store the response.
466 ClientResponse<AccountsCommonList> res = client.readSearchList(null, null, "dinoland");
467 AccountsCommonList list = res.getEntity();
468 int statusCode = res.getStatus();
470 // Check the status code of the response: does it match
471 // the expected response(s)?
472 if (logger.isDebugEnabled()) {
473 logger.debug(testName + ": status = " + statusCode);
475 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
476 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
477 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
478 Assert.assertEquals(2, list.getAccountListItem().size());
479 // Optionally output additional data about list members for debugging.
480 boolean iterateThroughList = true;
481 if (iterateThroughList && logger.isDebugEnabled()) {
482 printList(testName, list);
486 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
487 dependsOnMethods = {"createList", "read"})
488 public void searchScreenNameEmail(String testName) throws Exception {
491 setupReadList(testName);
493 // Submit the request to the service and store the response.
494 ClientResponse<AccountsCommonList> res = client.readSearchList("tom", null, "jerry");
495 AccountsCommonList list = res.getEntity();
496 int statusCode = res.getStatus();
498 // Check the status code of the response: does it match
499 // the expected response(s)?
500 if (logger.isDebugEnabled()) {
501 logger.debug(testName + ": status = " + statusCode);
503 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
504 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
505 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
506 Assert.assertEquals(1, list.getAccountListItem().size());
507 // Optionally output additional data about list members for debugging.
508 boolean iterateThroughList = true;
509 if (iterateThroughList && logger.isDebugEnabled()) {
510 printList(testName, list);
516 // ---------------------------------------------------------------
517 // CRUD tests : UPDATE tests
518 // ---------------------------------------------------------------
521 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
522 dependsOnMethods = {"read", "readList", "readNonExistent"})
523 public void update(String testName) throws Exception {
526 setupUpdate(testName);
529 ClientResponse<AccountsCommon> res =
530 client.read(knownResourceId);
531 if (logger.isDebugEnabled()) {
532 logger.debug(testName + ": read status = " + res.getStatus());
534 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
536 if (logger.isDebugEnabled()) {
537 logger.debug("got object to update with ID: " + knownResourceId);
539 AccountsCommon toUpdateAccount =
540 (AccountsCommon) res.getEntity();
541 Assert.assertNotNull(toUpdateAccount);
543 // Update the content of this resource.
544 toUpdateAccount.setEmail("updated-" + toUpdateAccount.getEmail());
545 if (logger.isDebugEnabled()) {
546 logger.debug("updated object");
547 logger.debug(objectAsXmlString(toUpdateAccount,
548 AccountsCommon.class));
551 // Submit the request to the service and store the response.
552 res = client.update(knownResourceId, toUpdateAccount);
553 int statusCode = res.getStatus();
554 // Check the status code of the response: does it match the expected response(s)?
555 if (logger.isDebugEnabled()) {
556 logger.debug(testName + ": status = " + statusCode);
558 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
559 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
560 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
563 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
564 Assert.assertNotNull(updatedAccount);
566 Assert.assertEquals(updatedAccount.getEmail(),
567 toUpdateAccount.getEmail(),
568 "Data in updated object did not match submitted data.");
571 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
572 dependsOnMethods = {"update"})
573 public void updatePassword(String testName) throws Exception {
576 setupUpdate(testName);
578 ClientResponse<AccountsCommon> res =
579 client.read(knownResourceId);
580 if (logger.isDebugEnabled()) {
581 logger.debug(testName + ": read status = " + res.getStatus());
583 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
585 if (logger.isDebugEnabled()) {
586 logger.debug(testName + ": got object to update password with ID: " + knownResourceId);
588 AccountsCommon toUpdateAccount =
589 (AccountsCommon) res.getEntity();
590 Assert.assertNotNull(toUpdateAccount);
593 toUpdateAccount.setPassword(Base64.encodeBase64("imagination".getBytes()));
594 if (logger.isDebugEnabled()) {
595 logger.debug(testName + ": updated object");
596 logger.debug(objectAsXmlString(toUpdateAccount,
597 AccountsCommon.class));
600 // Submit the request to the service and store the response.
601 res = client.update(knownResourceId, toUpdateAccount);
602 int statusCode = res.getStatus();
603 // Check the status code of the response: does it match the expected response(s)?
604 if (logger.isDebugEnabled()) {
605 logger.debug(testName + ": status = " + statusCode);
607 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
608 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
609 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
612 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
613 Assert.assertNotNull(updatedAccount);
615 // Assert.assertEquals(updatedAccount.getPassword(),
616 // toUpdateAccount.getPassword(),
617 // "Data in updated object did not match submitted data.");
620 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
621 dependsOnMethods = {"update"})
622 public void updatePasswordWithoutUser(String testName) throws Exception {
625 setupUpdate(testName);
627 ClientResponse<AccountsCommon> res =
628 client.read(knownResourceId);
629 if (logger.isDebugEnabled()) {
630 logger.debug(testName + ": read status = " + res.getStatus());
632 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
634 if (logger.isDebugEnabled()) {
635 logger.debug(testName + " : got object to update with ID: " + knownResourceId);
637 AccountsCommon toUpdateAccount =
638 (AccountsCommon) res.getEntity();
639 Assert.assertNotNull(toUpdateAccount);
641 toUpdateAccount.setUserId(null);
643 toUpdateAccount.setPassword(Base64.encodeBase64("imagination".getBytes()));
644 if (logger.isDebugEnabled()) {
645 logger.debug(testName + " : updated object");
646 logger.debug(objectAsXmlString(toUpdateAccount,
647 AccountsCommon.class));
650 // Submit the request to the service and store the response.
651 res = client.update(knownResourceId, toUpdateAccount);
652 int statusCode = res.getStatus();
653 // Check the status code of the response: does it match the expected response(s)?
654 if (logger.isDebugEnabled()) {
655 logger.debug(testName + ": status = " + statusCode);
657 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
658 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
659 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
663 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
664 dependsOnMethods = {"updatePasswordWithoutUser"})
665 public void deactivate(String testName) throws Exception {
668 setupUpdate(testName);
670 ClientResponse<AccountsCommon> res =
671 client.read(knownResourceId);
672 if (logger.isDebugEnabled()) {
673 logger.debug(testName + ": read status = " + res.getStatus());
675 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
677 if (logger.isDebugEnabled()) {
678 logger.debug("got object to update with ID: " + knownResourceId);
680 AccountsCommon toUpdateAccount =
681 (AccountsCommon) res.getEntity();
682 Assert.assertNotNull(toUpdateAccount);
684 // Update the content of this resource.
685 toUpdateAccount.setStatus(Status.INACTIVE);
686 if (logger.isDebugEnabled()) {
687 logger.debug("updated object");
688 logger.debug(objectAsXmlString(toUpdateAccount,
689 AccountsCommon.class));
692 // Submit the request to the service and store the response.
693 res = client.update(knownResourceId, toUpdateAccount);
694 int statusCode = res.getStatus();
695 // Check the status code of the response: does it match the expected response(s)?
696 if (logger.isDebugEnabled()) {
697 logger.debug(testName + ": status = " + statusCode);
699 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
700 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
701 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
704 AccountsCommon updatedAccount = (AccountsCommon) res.getEntity();
705 Assert.assertNotNull(updatedAccount);
707 Assert.assertEquals(updatedAccount.getStatus(),
708 toUpdateAccount.getStatus(),
709 "Data in updated object did not match submitted data.");
714 // Placeholders until the three tests below can be uncommented.
715 // See Issue CSPACE-401.
717 public void updateWithEmptyEntityBody(String testName) throws Exception {
721 public void updateWithMalformedXml(String testName) throws Exception {
725 public void updateWithWrongXmlSchema(String testName) throws Exception {
729 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
730 dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
731 public void updateNonExistent(String testName) throws Exception {
734 setupUpdateNonExistent(testName);
736 // Submit the request to the service and store the response.
738 // Note: The ID used in this 'create' call may be arbitrary.
739 // The only relevant ID may be the one used in updateAccount(), below.
740 AccountsCommon account =
741 createAccountInstance("simba", "simba", "tiger", "simba@lionking.com",
742 true, false, true, true);
743 ClientResponse<AccountsCommon> res =
744 client.update(NON_EXISTENT_ID, account);
745 int statusCode = res.getStatus();
747 // Check the status code of the response: does it match
748 // the expected response(s)?
749 if (logger.isDebugEnabled()) {
750 logger.debug(testName + ": status = " + statusCode);
752 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
753 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
754 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
757 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
758 dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
759 public void updateWrongUser(String testName) throws Exception {
762 // Submit the request to the service and store the response.
764 // Note: The ID used in this 'create' call may be arbitrary.
765 // The only relevant ID may be the one used in updateAccount(), below.
766 ClientResponse<AccountsCommon> res =
767 client.read(knownResourceId);
768 if (logger.isDebugEnabled()) {
769 logger.debug(testName + ": read status = " + res.getStatus());
771 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
773 if (logger.isDebugEnabled()) {
774 logger.debug("got object to update with ID: " + knownResourceId);
776 AccountsCommon toUpdateAccount =
777 (AccountsCommon) res.getEntity();
778 Assert.assertNotNull(toUpdateAccount);
780 toUpdateAccount.setUserId("barneyFake");
781 if (logger.isDebugEnabled()) {
782 logger.debug("updated object with wrongUser");
783 logger.debug(objectAsXmlString(toUpdateAccount,
784 AccountsCommon.class));
787 res = client.update(knownResourceId, toUpdateAccount);
788 int statusCode = res.getStatus();
790 // Check the status code of the response: does it match
791 // the expected response(s)?
792 if (logger.isDebugEnabled()) {
793 logger.debug(testName + ": status = " + statusCode);
795 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
796 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
797 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
800 // ---------------------------------------------------------------
801 // CRUD tests : DELETE tests
802 // ---------------------------------------------------------------
805 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
806 dependsOnMethods = {"testSubmitRequest", "updateWrongUser"})
807 public void delete(String testName) throws Exception {
810 setupDelete(testName);
812 // Submit the request to the service and store the response.
813 ClientResponse<Response> res = client.delete(knownResourceId);
814 int statusCode = res.getStatus();
816 // Check the status code of the response: does it match
817 // the expected response(s)?
818 if (logger.isDebugEnabled()) {
819 logger.debug(testName + ": status = " + statusCode);
821 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
822 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
823 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
828 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
829 dependsOnMethods = {"delete"})
830 public void deleteNonExistent(String testName) throws Exception {
833 setupDeleteNonExistent(testName);
835 // Submit the request to the service and store the response.
836 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
837 int statusCode = res.getStatus();
839 // Check the status code of the response: does it match
840 // the expected response(s)?
841 if (logger.isDebugEnabled()) {
842 logger.debug(testName + ": status = " + statusCode);
844 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
845 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
846 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
849 // ---------------------------------------------------------------
850 // Utility tests : tests of code used in tests above
851 // ---------------------------------------------------------------
853 * Tests the code for manually submitting data that is used by several
854 * of the methods above.
856 @Test(dependsOnMethods = {"create", "read"})
857 public void testSubmitRequest() throws Exception {
859 // Expected status code: 200 OK
860 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
862 // Submit the request to the service and store the response.
863 String method = ServiceRequestType.READ.httpMethodName();
864 String url = getResourceURL(knownResourceId);
865 int statusCode = submitRequest(method, url);
867 // Check the status code of the response: does it match
868 // the expected response(s)?
869 if (logger.isDebugEnabled()) {
870 logger.debug("testSubmitRequest: url=" + url
871 + " status=" + statusCode);
873 Assert.assertEquals(statusCode, EXPECTED_STATUS);
878 // ---------------------------------------------------------------
879 // Utility methods used by tests above
880 // ---------------------------------------------------------------
882 * create account instance
887 * @param useScreenName
888 * @param invalidTenant
893 private AccountsCommon createAccountInstance(String screenName,
894 String userName, String passwd, String email,
895 boolean useScreenName, boolean invalidTenant, boolean useUser, boolean usePassword) {
897 AccountsCommon account = new AccountsCommon();
899 account.setScreenName(screenName);
902 account.setUserId(userName);
905 account.setPassword(Base64.encodeBase64(passwd.getBytes()));
907 account.setPersonRefName(screenName);
908 account.setEmail(email);
909 account.setPhone("1234567890");
910 List<AccountTenant> atList = new ArrayList<AccountTenant>();
911 AccountTenant at = new AccountTenant();
912 if (!invalidTenant) {
913 //tenant is not required to be added during create, service layer
914 //picks up tenant from security context if needed
918 account.setTenants(atList);
919 addTenant = !addTenant;
922 //use invalid tenant id...called from validation test
923 at.setTenantId(UUID.randomUUID().toString());
925 account.setTenants(atList);
928 if (logger.isDebugEnabled()) {
929 logger.debug("to be created, account common");
930 logger.debug(objectAsXmlString(account,
931 AccountsCommon.class));
937 @AfterClass(alwaysRun = true)
938 public void cleanUp() {
939 setupDelete("delete");
940 if (logger.isDebugEnabled()) {
941 logger.debug("Cleaning up temporary resources created for testing ...");
943 for (String resourceId : allResourceIdsCreated) {
944 // Note: Any non-success responses are ignored and not reported.
945 ClientResponse<Response> res = client.delete(resourceId);
946 int statusCode = res.getStatus();
947 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
948 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
949 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
953 private void printList(String testName, AccountsCommonList list) {
954 List<AccountsCommonList.AccountListItem> items =
955 list.getAccountListItem();
958 for (AccountsCommonList.AccountListItem item : items) {
959 logger.debug(testName + ": list-item[" + i + "] csid="
961 logger.debug(testName + ": list-item[" + i + "] screenName="
962 + item.getScreenName());
963 logger.debug(testName + ": list-item[" + i + "] URI="