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.client.CollectionSpaceClient;
31 import org.collectionspace.services.account.AccountsCommon;
32 import org.collectionspace.services.account.AccountsCommonList;
33 import org.collectionspace.services.account.Status;
34 import org.collectionspace.services.client.AccountFactory;
35 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
36 import org.collectionspace.services.client.test.ServiceRequestType;
37 import org.collectionspace.services.jaxb.AbstractCommonList;
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 static 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 static boolean addTenant = true;
64 * This method is called only by the parent class, AbstractServiceTestImpl
67 protected String getServicePathComponent() {
68 return new AccountClient().getServicePathComponent();
72 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
75 protected CollectionSpaceClient getClientInstance() {
76 return new AccountClient();
80 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
83 protected AbstractCommonList getAbstractCommonList(
84 ClientResponse<AbstractCommonList> response) {
85 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
86 throw new UnsupportedOperationException();
90 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
92 @Test(dataProvider = "testName")
94 public void readPaginatedList(String testName) throws Exception {
95 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
98 // ---------------------------------------------------------------
99 // CRUD tests : CREATE tests
100 // ---------------------------------------------------------------
103 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
104 public void create(String testName) throws Exception {
106 // Perform setup, such as initializing the type of service request
107 // (e.g. CREATE, DELETE), its valid and expected status codes, and
108 // its associated HTTP method name (e.g. POST, DELETE).
109 setupCreate(testName);
111 // Submit the request to the service and store the response.
112 AccountsCommon account =
113 createAccountInstance("barney", "barney", "hithere08", "barney@dinoland.com",
114 true, false, true, true);
115 AccountClient client = new AccountClient();
116 ClientResponse<Response> res = client.create(account);
117 int statusCode = res.getStatus();
119 // Check the status code of the response: does it match
120 // the expected response(s)?
123 // Does it fall within the set of valid status codes?
124 // Does it exactly match the expected status code?
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, EXPECTED_STATUS_CODE);
132 // Store the ID returned from this create operation
133 // for additional tests below.
134 knownResourceId = extractId(res);
135 if (logger.isDebugEnabled()) {
136 logger.debug(testName + ": knownResourceId=" + knownResourceId);
140 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
141 dependsOnMethods = {"create"})
142 public void createForUniqueUser(String testName) throws Exception {
144 setupCreate(testName);
146 // Submit the request to the service and store the response.
147 AccountsCommon account =
148 createAccountInstance("barney1", "barney", "hithere08", "barney@dinoland.com",
149 true, false, true, true);
150 AccountClient client = new AccountClient();
151 ClientResponse<Response> res = client.create(account);
152 int statusCode = res.getStatus();
153 if (logger.isDebugEnabled()) {
154 logger.debug(testName + ": status = " + statusCode);
156 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
157 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
158 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
161 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
162 dependsOnMethods = {"create"})
163 public void createWithInvalidTenant(String testName) throws Exception {
165 setupCreate(testName);
167 // Submit the request to the service and store the response.
168 AccountsCommon account =
169 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
170 true, true, true, true);
171 AccountClient client = new AccountClient();
172 ClientResponse<Response> res = client.create(account);
173 int statusCode = res.getStatus();
174 // Does it exactly match the expected status code?
175 if (logger.isDebugEnabled()) {
176 logger.debug(testName + ": status = " + statusCode);
178 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
184 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
185 dependsOnMethods = {"create"})
186 public void createWithoutUser(String testName) throws Exception {
188 setupCreate(testName);
190 // Submit the request to the service and store the response.
191 AccountsCommon account =
192 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
193 true, false, false, true);
194 AccountClient client = new AccountClient();
195 ClientResponse<Response> res = client.create(account);
196 int statusCode = res.getStatus();
197 // Does it exactly match the expected status code?
198 if (logger.isDebugEnabled()) {
199 logger.debug(testName + ": status = " + statusCode);
201 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
202 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
203 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
206 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
207 dependsOnMethods = {"create"})
208 public void createWithInvalidEmail(String testName) throws Exception {
210 setupCreate(testName);
212 // Submit the request to the service and store the response.
213 AccountsCommon account =
214 createAccountInstance("babybop", "babybop", "hithere08", "babybop.dinoland.com",
215 true, false, true, true);
216 AccountClient client = new AccountClient();
217 ClientResponse<Response> res = client.create(account);
218 int statusCode = res.getStatus();
219 // Does it exactly match the expected status code?
220 if (logger.isDebugEnabled()) {
221 logger.debug(testName + ": status = " + statusCode);
223 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
224 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
225 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
228 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
229 dependsOnMethods = {"create"})
230 public void createWithoutScreenName(String testName) throws Exception {
232 setupCreate(testName);
234 // Submit the request to the service and store the response.
235 AccountsCommon account =
236 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
237 false, false, true, true);
238 AccountClient client = new AccountClient();
239 ClientResponse<Response> res = client.create(account);
240 int statusCode = res.getStatus();
241 // Does it exactly match the expected status code?
242 if (logger.isDebugEnabled()) {
243 logger.debug(testName + ": status = " + statusCode);
245 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
246 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
247 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
250 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
251 dependsOnMethods = {"create"})
252 public void createWithInvalidPassword(String testName) throws Exception {
254 setupCreate(testName);
256 // Submit the request to the service and store the response.
257 AccountsCommon account =
258 createAccountInstance("babybop", "babybop", "shpswd", "babybop@dinoland.com",
259 true, false, true, true);
260 AccountClient client = new AccountClient();
261 ClientResponse<Response> res = client.create(account);
262 int statusCode = res.getStatus();
263 // Does it exactly match the expected status code?
264 if (logger.isDebugEnabled()) {
265 logger.debug(testName + ": status = " + statusCode);
267 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
268 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
269 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
272 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
273 dependsOnMethods = {"create"})
274 public void createWithMostInvalid(String testName) throws Exception {
276 setupCreate(testName);
278 // Submit the request to the service and store the response.
279 AccountsCommon account =
280 createAccountInstance("babybop", "babybop", "hithere08", "babybop/dinoland.com",
281 false, true, false, false);
282 AccountClient client = new AccountClient();
283 ClientResponse<Response> res = client.create(account);
284 int statusCode = res.getStatus();
285 // Does it exactly match the expected status code?
286 if (logger.isDebugEnabled()) {
287 logger.debug(testName + ": status = " + statusCode);
289 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
290 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
291 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
294 //to not cause uniqueness violation for account, createList is removed
296 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
297 dependsOnMethods = {"create"})
298 public void createList(String testName) throws Exception {
300 setupCreate(testName);
301 // Submit the request to the service and store the response.
302 AccountsCommon account1 =
303 createAccountInstance("curious", "curious", "hithere08", "curious@george.com",
304 true, false, true, true);
305 AccountClient client = new AccountClient();
306 ClientResponse<Response> res = client.create(account1);
307 int statusCode = res.getStatus();
308 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
309 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
310 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
311 allResourceIdsCreated.add(extractId(res));
313 AccountsCommon account2 =
314 createAccountInstance("tom", "tom", "hithere09", "tom@jerry.com",
315 true, false, true, true);
316 res = client.create(account2);
317 statusCode = res.getStatus();
318 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
319 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
320 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
321 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
322 allResourceIdsCreated.add(extractId(res));
324 AccountsCommon account3 =
325 createAccountInstance("mj", "mj", "hithere10", "mj@dinoland.com",
326 true, false, true, true);
327 res = client.create(account3);
328 statusCode = res.getStatus();
329 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
330 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
331 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
332 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
333 allResourceIdsCreated.add(extractId(res));
337 // Placeholders until the three tests below can be uncommented.
338 // See Issue CSPACE-401.
340 public void createWithEmptyEntityBody(String testName) throws Exception {
341 //FIXME: Should this test really be empty? If so, please comment accordingly.
345 public void createWithMalformedXml(String testName) throws Exception {
346 //FIXME: Should this test really be empty? If so, please comment accordingly.
350 public void createWithWrongXmlSchema(String testName) throws Exception {
351 //FIXME: Should this test really be empty? If so, please comment accordingly.
354 // ---------------------------------------------------------------
355 // CRUD tests : READ tests
356 // ---------------------------------------------------------------
359 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
360 dependsOnMethods = {"create"})
361 public void read(String testName) throws Exception {
366 // Submit the request to the service and store the response.
367 AccountClient client = new AccountClient();
368 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
369 int statusCode = res.getStatus();
371 // Check the status code of the response: does it match
372 // the expected response(s)?
373 if (logger.isDebugEnabled()) {
374 logger.debug(testName + ": status = " + statusCode);
376 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
377 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
378 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
380 AccountsCommon output = (AccountsCommon) res.getEntity();
381 Assert.assertNotNull(output);
386 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
387 dependsOnMethods = {"read"})
388 public void readNonExistent(String testName) throws Exception {
391 setupReadNonExistent(testName);
393 // Submit the request to the service and store the response.
394 AccountClient client = new AccountClient();
395 ClientResponse<AccountsCommon> res = client.read(NON_EXISTENT_ID);
396 int statusCode = res.getStatus();
398 // Check the status code of the response: does it match
399 // the expected response(s)?
400 if (logger.isDebugEnabled()) {
401 logger.debug(testName + ": status = " + statusCode);
403 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
404 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
405 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
408 // ---------------------------------------------------------------
409 // CRUD tests : READ_LIST tests
410 // ---------------------------------------------------------------
413 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
414 dependsOnMethods = {"createList", "read"})
415 public void readList(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 = client.readList();
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);
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 searchScreenName(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("tom", null, 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 searchUserId(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, "tom", null);
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(1, 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 searchEmail(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(null, null, "dinoland");
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(2, 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);
532 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
533 dependsOnMethods = {"createList", "read"})
534 public void searchScreenNameEmail(String testName) throws Exception {
537 setupReadList(testName);
539 // Submit the request to the service and store the response.
540 AccountClient client = new AccountClient();
541 ClientResponse<AccountsCommonList> res =
542 client.readSearchList("tom", null, "jerry");
543 AccountsCommonList list = res.getEntity();
544 int statusCode = res.getStatus();
546 // Check the status code of the response: does it match
547 // the expected response(s)?
548 if (logger.isDebugEnabled()) {
549 logger.debug(testName + ": status = " + statusCode);
551 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
552 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
553 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
554 Assert.assertEquals(1, list.getAccountListItem().size());
555 // Optionally output additional data about list members for debugging.
556 boolean iterateThroughList = true;
557 if (iterateThroughList && logger.isDebugEnabled()) {
558 printList(testName, list);
564 // ---------------------------------------------------------------
565 // CRUD tests : UPDATE tests
566 // ---------------------------------------------------------------
569 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
570 dependsOnMethods = {"read", "readList", "readNonExistent"})
571 public void update(String testName) throws Exception {
574 setupUpdate(testName);
576 AccountClient client = new AccountClient();
577 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
578 if (logger.isDebugEnabled()) {
579 logger.debug(testName + ": read status = " + res.getStatus());
581 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
583 if (logger.isDebugEnabled()) {
584 logger.debug("got object to update with ID: " + knownResourceId);
586 AccountsCommon accountFound =
587 (AccountsCommon) res.getEntity();
588 Assert.assertNotNull(accountFound);
590 //create a new account object to test partial updates
591 AccountsCommon accountToUpdate = new AccountsCommon();
592 accountToUpdate.setCsid(knownResourceId);
593 accountToUpdate.setUserId(accountFound.getUserId());
594 // Update the content of this resource.
595 accountToUpdate.setEmail("updated-" + accountFound.getEmail());
596 if (logger.isDebugEnabled()) {
597 logger.debug("updated object");
598 logger.debug(objectAsXmlString(accountFound,
599 AccountsCommon.class));
602 // Submit the request to the service and store the response.
603 res = client.update(knownResourceId, accountToUpdate);
604 int statusCode = res.getStatus();
605 // Check the status code of the response: does it match the expected response(s)?
606 if (logger.isDebugEnabled()) {
607 logger.debug(testName + ": status = " + statusCode);
609 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
610 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
611 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
613 AccountsCommon accountUpdated = (AccountsCommon) res.getEntity();
614 Assert.assertNotNull(accountUpdated);
616 Assert.assertEquals(accountUpdated.getEmail(),
617 accountToUpdate.getEmail(),
618 "Data in updated object did not match submitted data.");
621 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
622 dependsOnMethods = {"update"})
623 public void updatePassword(String testName) throws Exception {
626 setupUpdate(testName);
628 AccountClient client = new AccountClient();
629 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
630 if (logger.isDebugEnabled()) {
631 logger.debug(testName + ": read status = " + res.getStatus());
633 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
635 if (logger.isDebugEnabled()) {
636 logger.debug(testName + ": got object to update password with ID: " + knownResourceId);
638 AccountsCommon accountFound =
639 (AccountsCommon) res.getEntity();
640 Assert.assertNotNull(accountFound);
642 //create a new account object to test partial updates
643 AccountsCommon accountToUpdate = new AccountsCommon();
644 accountToUpdate.setCsid(knownResourceId);
645 accountToUpdate.setUserId(accountFound.getUserId());
647 accountToUpdate.setPassword("imagination".getBytes());
648 if (logger.isDebugEnabled()) {
649 logger.debug(testName + ": updated object");
650 logger.debug(objectAsXmlString(accountToUpdate,
651 AccountsCommon.class));
654 // Submit the request to the service and store the response.
655 res = client.update(knownResourceId, accountToUpdate);
656 int statusCode = res.getStatus();
657 // Check the status code of the response: does it match the expected response(s)?
658 if (logger.isDebugEnabled()) {
659 logger.debug(testName + ": status = " + statusCode);
661 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
662 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
663 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
666 AccountsCommon accountUpdated = (AccountsCommon) res.getEntity();
667 Assert.assertNotNull(accountUpdated);
669 // Assert.assertEquals(accountUpdated.getPassword(),
670 // accountFound.getPassword(),
671 // "Data in updated object did not match submitted data.");
674 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
675 dependsOnMethods = {"update"})
676 public void updatePasswordWithoutUser(String testName) throws Exception {
679 setupUpdate(testName);
681 AccountsCommon accountToUpdate = new AccountsCommon();
682 accountToUpdate.setCsid(knownResourceId);
683 accountToUpdate.setUserId(null);
685 accountToUpdate.setPassword("imagination".getBytes());
686 if (logger.isDebugEnabled()) {
687 logger.debug(testName + " : updated object");
688 logger.debug(objectAsXmlString(accountToUpdate,
689 AccountsCommon.class));
692 AccountClient client = new AccountClient();
693 // Submit the request to the service and store the response.
694 ClientResponse<AccountsCommon> res = client.update(knownResourceId, accountToUpdate);
695 int statusCode = res.getStatus();
696 // Check the status code of the response: does it match the expected response(s)?
697 if (logger.isDebugEnabled()) {
698 logger.debug(testName + ": status = " + statusCode);
700 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
701 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
702 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
706 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
707 dependsOnMethods = {"update"})
708 public void updateInvalidPassword(String testName) throws Exception {
711 setupUpdate(testName);
712 AccountClient client = new AccountClient();
713 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
714 if (logger.isDebugEnabled()) {
715 logger.debug(testName + ": read status = " + res.getStatus());
717 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
719 if (logger.isDebugEnabled()) {
720 logger.debug(testName + ": got object to update password with ID: " + knownResourceId);
722 AccountsCommon accountFound = (AccountsCommon) res.getEntity();
724 AccountsCommon accountToUpdate = new AccountsCommon();
725 accountToUpdate.setCsid(knownResourceId);
726 accountToUpdate.setUserId(accountFound.getUserId());
727 Assert.assertNotNull(accountToUpdate);
730 accountToUpdate.setPassword("abc123".getBytes());
731 if (logger.isDebugEnabled()) {
732 logger.debug(testName + ": updated object");
733 logger.debug(objectAsXmlString(accountToUpdate,
734 AccountsCommon.class));
737 // Submit the request to the service and store the response.
738 res = client.update(knownResourceId, accountToUpdate);
739 int statusCode = res.getStatus();
740 // Check the status code of the response: does it match the expected response(s)?
741 if (logger.isDebugEnabled()) {
742 logger.debug(testName + ": status = " + statusCode);
744 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
745 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
746 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
750 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
751 dependsOnMethods = {"updatePasswordWithoutUser"})
752 public void deactivate(String testName) throws Exception {
755 setupUpdate(testName);
757 AccountClient client = new AccountClient();
758 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
759 if (logger.isDebugEnabled()) {
760 logger.debug(testName + ": read status = " + res.getStatus());
762 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
764 if (logger.isDebugEnabled()) {
765 logger.debug("got object to update with ID: " + knownResourceId);
767 AccountsCommon accountFound = (AccountsCommon) res.getEntity();
769 //create a new account object to test partial updates
770 AccountsCommon accountToUpdate = new AccountsCommon();
771 accountToUpdate.setCsid(knownResourceId);
772 accountToUpdate.setUserId(accountFound.getUserId());
774 // Update the content of this resource.
775 accountToUpdate.setStatus(Status.INACTIVE);
776 if (logger.isDebugEnabled()) {
777 logger.debug("updated object");
778 logger.debug(objectAsXmlString(accountToUpdate,
779 AccountsCommon.class));
782 // Submit the request to the service and store the response.
783 res = client.update(knownResourceId, accountToUpdate);
784 int statusCode = res.getStatus();
785 // Check the status code of the response: does it match the expected response(s)?
786 if (logger.isDebugEnabled()) {
787 logger.debug(testName + ": status = " + statusCode);
789 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
790 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
791 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
793 AccountsCommon accountUpdated = (AccountsCommon) res.getEntity();
794 Assert.assertNotNull(accountUpdated);
796 Assert.assertEquals(accountUpdated.getStatus(),
797 accountToUpdate.getStatus(),
798 "Data in updated object did not match submitted data.");
803 // Placeholders until the three tests below can be uncommented.
804 // See Issue CSPACE-401.
806 public void updateWithEmptyEntityBody(String testName) throws Exception {
807 //FIXME: Should this test really be empty? If so, please comment accordingly.
811 public void updateWithMalformedXml(String testName) throws Exception {
812 //FIXME: Should this test really be empty? If so, please comment accordingly.
816 public void updateWithWrongXmlSchema(String testName) throws Exception {
817 //FIXME: Should this test really be empty? If so, please comment accordingly.
821 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
822 dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
823 public void updateNonExistent(String testName) throws Exception {
826 setupUpdateNonExistent(testName);
828 // Submit the request to the service and store the response.
830 // Note: The ID used in this 'create' call may be arbitrary.
831 // The only relevant ID may be the one used in updateAccount(), below.
832 AccountClient client = new AccountClient();
833 AccountsCommon account =
834 createAccountInstance("simba", "simba", "tiger", "simba@lionking.com",
835 true, false, true, true);
836 ClientResponse<AccountsCommon> res =
837 client.update(NON_EXISTENT_ID, account);
838 int statusCode = res.getStatus();
840 // Check the status code of the response: does it match
841 // the expected response(s)?
842 if (logger.isDebugEnabled()) {
843 logger.debug(testName + ": status = " + statusCode);
845 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
846 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
847 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
850 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
851 dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
852 public void updateWrongUser(String testName) throws Exception {
856 // Submit the request to the service and store the response.
858 // Note: The ID used in this 'create' call may be arbitrary.
859 // The only relevant ID may be the one used in updateAccount(), below.
860 AccountClient client = new AccountClient();
861 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
862 if (logger.isDebugEnabled()) {
863 logger.debug(testName + ": read status = " + res.getStatus());
865 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
867 if (logger.isDebugEnabled()) {
868 logger.debug("got object to update with ID: " + knownResourceId);
870 AccountsCommon accountToUpdate =
871 (AccountsCommon) res.getEntity();
872 Assert.assertNotNull(accountToUpdate);
874 accountToUpdate.setUserId("barneyFake");
875 if (logger.isDebugEnabled()) {
876 logger.debug("updated object with wrongUser");
877 logger.debug(objectAsXmlString(accountToUpdate,
878 AccountsCommon.class));
881 res = client.update(knownResourceId, accountToUpdate);
882 int statusCode = res.getStatus();
884 // Check the status code of the response: does it match
885 // the expected response(s)?
886 if (logger.isDebugEnabled()) {
887 logger.debug(testName + ": status = " + statusCode);
889 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
890 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
891 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
894 // ---------------------------------------------------------------
895 // CRUD tests : DELETE tests
896 // ---------------------------------------------------------------
899 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
900 dependsOnMethods = {"testSubmitRequest", "updateWrongUser"})
901 public void delete(String testName) throws Exception {
904 setupDelete(testName);
906 // Submit the request to the service and store the response.
907 AccountClient client = new AccountClient();
908 ClientResponse<Response> res = client.delete(knownResourceId);
909 int statusCode = res.getStatus();
911 // Check the status code of the response: does it match
912 // the expected response(s)?
913 if (logger.isDebugEnabled()) {
914 logger.debug(testName + ": status = " + statusCode);
916 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
917 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
918 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
923 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
924 dependsOnMethods = {"delete"})
925 public void deleteNonExistent(String testName) throws Exception {
928 setupDeleteNonExistent(testName);
930 // Submit the request to the service and store the response.
931 AccountClient client = new AccountClient();
932 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
933 int statusCode = res.getStatus();
935 // Check the status code of the response: does it match
936 // the expected response(s)?
937 if (logger.isDebugEnabled()) {
938 logger.debug(testName + ": status = " + statusCode);
940 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
941 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
942 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
945 // ---------------------------------------------------------------
946 // Utility tests : tests of code used in tests above
947 // ---------------------------------------------------------------
949 * Tests the code for manually submitting data that is used by several
950 * of the methods above.
952 @Test(dependsOnMethods = {"create", "read"})
953 public void testSubmitRequest() throws Exception {
955 // Expected status code: 200 OK
956 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
958 // Submit the request to the service and store the response.
959 String method = ServiceRequestType.READ.httpMethodName();
960 String url = getResourceURL(knownResourceId);
961 int statusCode = submitRequest(method, url);
963 // Check the status code of the response: does it match
964 // the expected response(s)?
965 if (logger.isDebugEnabled()) {
966 logger.debug("testSubmitRequest: url=" + url
967 + " status=" + statusCode);
969 Assert.assertEquals(statusCode, EXPECTED_STATUS);
973 // ---------------------------------------------------------------
974 // Utility methods used by tests above
975 // ---------------------------------------------------------------
977 * create account instance
982 * @param useScreenName
983 * @param invalidTenant
988 AccountsCommon createAccountInstance(String screenName,
989 String userName, String passwd, String email,
990 boolean useScreenName, boolean invalidTenant, boolean useUser, boolean usePassword) {
992 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
993 userName, passwd, email, useScreenName,
994 addTenant, invalidTenant, useUser, usePassword);
996 if (logger.isDebugEnabled()) {
997 logger.debug("to be created, account common");
998 logger.debug(objectAsXmlString(account,
999 AccountsCommon.class));
1005 @AfterClass(alwaysRun = true)
1006 public void cleanUp() {
1007 setupDelete("delete");
1008 String noTest = System.getProperty("noTestCleanup");
1009 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1010 if (logger.isDebugEnabled()) {
1011 logger.debug("Skipping Cleanup phase ...");
1015 if (logger.isDebugEnabled()) {
1016 logger.debug("Cleaning up temporary resources created for testing ...");
1018 AccountClient client = new AccountClient();
1019 for (String resourceId : allResourceIdsCreated) {
1020 // Note: Any non-success responses are ignored and not reported.
1021 ClientResponse<Response> res = client.delete(resourceId);
1022 int statusCode = res.getStatus();
1023 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1024 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1025 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1029 private void printList(String testName, AccountsCommonList list) {
1030 List<AccountsCommonList.AccountListItem> items =
1031 list.getAccountListItem();
1034 for (AccountsCommonList.AccountListItem item : items) {
1035 logger.debug(testName + ": list-item[" + i + "] csid="
1037 logger.debug(testName + ": list-item[" + i + "] screenName="
1038 + item.getScreenName());
1039 logger.debug(testName + ": list-item[" + i + "] URI="