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();
154 if (logger.isDebugEnabled()) {
155 logger.debug(testName + ": status = " + statusCode);
157 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
158 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
159 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
162 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
163 dependsOnMethods = {"create"})
164 public void createWithInvalidTenant(String testName) throws Exception {
166 setupCreate(testName);
168 // Submit the request to the service and store the response.
169 AccountsCommon account =
170 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
171 true, true, true, true);
172 AccountClient client = new AccountClient();
173 ClientResponse<Response> res = client.create(account);
174 int statusCode = res.getStatus();
175 // Does it exactly match the expected status code?
176 if (logger.isDebugEnabled()) {
177 logger.debug(testName + ": status = " + statusCode);
179 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
180 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
181 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
185 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
186 dependsOnMethods = {"create"})
187 public void createWithoutUser(String testName) throws Exception {
189 setupCreate(testName);
191 // Submit the request to the service and store the response.
192 AccountsCommon account =
193 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
194 true, false, false, true);
195 AccountClient client = new AccountClient();
196 ClientResponse<Response> res = client.create(account);
197 int statusCode = res.getStatus();
198 // Does it exactly match the expected status code?
199 if (logger.isDebugEnabled()) {
200 logger.debug(testName + ": status = " + statusCode);
202 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
203 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
204 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
207 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
208 dependsOnMethods = {"create"})
209 public void createWithInvalidEmail(String testName) throws Exception {
211 setupCreate(testName);
213 // Submit the request to the service and store the response.
214 AccountsCommon account =
215 createAccountInstance("babybop", "babybop", "hithere08", "babybop.dinoland.com",
216 true, false, true, true);
217 AccountClient client = new AccountClient();
218 ClientResponse<Response> res = client.create(account);
219 int statusCode = res.getStatus();
220 // Does it exactly match the expected status code?
221 if (logger.isDebugEnabled()) {
222 logger.debug(testName + ": status = " + statusCode);
224 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
225 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
226 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
229 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
230 dependsOnMethods = {"create"})
231 public void createWithoutScreenName(String testName) throws Exception {
233 setupCreate(testName);
235 // Submit the request to the service and store the response.
236 AccountsCommon account =
237 createAccountInstance("babybop", "babybop", "hithere08", "babybop@dinoland.com",
238 false, false, true, true);
239 AccountClient client = new AccountClient();
240 ClientResponse<Response> res = client.create(account);
241 int statusCode = res.getStatus();
242 // Does it exactly match the expected status code?
243 if (logger.isDebugEnabled()) {
244 logger.debug(testName + ": status = " + statusCode);
246 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
247 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
248 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
251 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
252 dependsOnMethods = {"create"})
253 public void createWithInvalidPassword(String testName) throws Exception {
255 setupCreate(testName);
257 // Submit the request to the service and store the response.
258 AccountsCommon account =
259 createAccountInstance("babybop", "babybop", "shpswd", "babybop@dinoland.com",
260 true, false, true, true);
261 AccountClient client = new AccountClient();
262 ClientResponse<Response> res = client.create(account);
263 int statusCode = res.getStatus();
264 // Does it exactly match the expected status code?
265 if (logger.isDebugEnabled()) {
266 logger.debug(testName + ": status = " + statusCode);
268 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
269 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
270 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
273 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
274 dependsOnMethods = {"create"})
275 public void createWithMostInvalid(String testName) throws Exception {
277 setupCreate(testName);
279 // Submit the request to the service and store the response.
280 AccountsCommon account =
281 createAccountInstance("babybop", "babybop", "hithere08", "babybop/dinoland.com",
282 false, true, false, false);
283 AccountClient client = new AccountClient();
284 ClientResponse<Response> res = client.create(account);
285 int statusCode = res.getStatus();
286 // Does it exactly match the expected status code?
287 if (logger.isDebugEnabled()) {
288 logger.debug(testName + ": status = " + statusCode);
290 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
291 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
292 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
295 //to not cause uniqueness violation for account, createList is removed
297 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
298 dependsOnMethods = {"create"})
299 public void createList(String testName) throws Exception {
301 setupCreate(testName);
302 // Submit the request to the service and store the response.
303 AccountsCommon account1 =
304 createAccountInstance("curious", "curious", "hithere08", "curious@george.com",
305 true, false, true, true);
306 AccountClient client = new AccountClient();
307 ClientResponse<Response> res = client.create(account1);
308 int statusCode = res.getStatus();
309 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
310 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
311 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
312 allResourceIdsCreated.add(extractId(res));
314 AccountsCommon account2 =
315 createAccountInstance("tom", "tom", "hithere09", "tom@jerry.com",
316 true, false, true, true);
317 res = client.create(account2);
318 statusCode = res.getStatus();
319 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
320 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
321 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
322 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
323 allResourceIdsCreated.add(extractId(res));
325 AccountsCommon account3 =
326 createAccountInstance("mj", "mj", "hithere10", "mj@dinoland.com",
327 true, false, true, true);
328 res = client.create(account3);
329 statusCode = res.getStatus();
330 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
331 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
332 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
333 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
334 allResourceIdsCreated.add(extractId(res));
338 // Placeholders until the three tests below can be uncommented.
339 // See Issue CSPACE-401.
341 public void createWithEmptyEntityBody(String testName) throws Exception {
342 //FIXME: Should this test really be empty? If so, please comment accordingly.
346 public void createWithMalformedXml(String testName) throws Exception {
347 //FIXME: Should this test really be empty? If so, please comment accordingly.
351 public void createWithWrongXmlSchema(String testName) throws Exception {
352 //FIXME: Should this test really be empty? If so, please comment accordingly.
355 // ---------------------------------------------------------------
356 // CRUD tests : READ tests
357 // ---------------------------------------------------------------
360 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
361 dependsOnMethods = {"create"})
362 public void read(String testName) throws Exception {
367 // Submit the request to the service and store the response.
368 AccountClient client = new AccountClient();
369 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
370 int statusCode = res.getStatus();
372 // Check the status code of the response: does it match
373 // the expected response(s)?
374 if (logger.isDebugEnabled()) {
375 logger.debug(testName + ": status = " + statusCode);
377 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
378 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
379 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
381 AccountsCommon output = (AccountsCommon) res.getEntity();
382 Assert.assertNotNull(output);
387 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
388 dependsOnMethods = {"read"})
389 public void readNonExistent(String testName) throws Exception {
392 setupReadNonExistent(testName);
394 // Submit the request to the service and store the response.
395 AccountClient client = new AccountClient();
396 ClientResponse<AccountsCommon> res = client.read(NON_EXISTENT_ID);
397 int statusCode = res.getStatus();
399 // Check the status code of the response: does it match
400 // the expected response(s)?
401 if (logger.isDebugEnabled()) {
402 logger.debug(testName + ": status = " + statusCode);
404 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
405 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
406 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
409 // ---------------------------------------------------------------
410 // CRUD tests : READ_LIST tests
411 // ---------------------------------------------------------------
414 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
415 dependsOnMethods = {"createList", "read"})
416 public void readList(String testName) throws Exception {
419 setupReadList(testName);
421 // Submit the request to the service and store the response.
422 AccountClient client = new AccountClient();
423 ClientResponse<AccountsCommonList> res = client.readList();
424 AccountsCommonList list = res.getEntity();
425 int statusCode = res.getStatus();
427 // Check the status code of the response: does it match
428 // the expected response(s)?
429 if (logger.isDebugEnabled()) {
430 logger.debug(testName + ": status = " + statusCode);
432 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
433 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
434 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
436 // Optionally output additional data about list members for debugging.
437 boolean iterateThroughList = true;
438 if (iterateThroughList && logger.isDebugEnabled()) {
439 printList(testName, list);
443 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
444 dependsOnMethods = {"createList", "read"})
445 public void searchScreenName(String testName) throws Exception {
448 setupReadList(testName);
450 // Submit the request to the service and store the response.
451 AccountClient client = new AccountClient();
452 ClientResponse<AccountsCommonList> res =
453 client.readSearchList("tom", null, null);
454 AccountsCommonList list = res.getEntity();
455 int statusCode = res.getStatus();
457 // Check the status code of the response: does it match
458 // the expected response(s)?
459 if (logger.isDebugEnabled()) {
460 logger.debug(testName + ": status = " + statusCode);
462 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
463 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
464 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
465 Assert.assertEquals(1, list.getAccountListItem().size());
466 // Optionally output additional data about list members for debugging.
467 boolean iterateThroughList = true;
468 if (iterateThroughList && logger.isDebugEnabled()) {
469 printList(testName, list);
473 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
474 dependsOnMethods = {"createList", "read"})
475 public void searchUserId(String testName) throws Exception {
478 setupReadList(testName);
480 // Submit the request to the service and store the response.
481 AccountClient client = new AccountClient();
482 ClientResponse<AccountsCommonList> res =
483 client.readSearchList(null, "tom", null);
484 AccountsCommonList list = res.getEntity();
485 int statusCode = res.getStatus();
487 // Check the status code of the response: does it match
488 // the expected response(s)?
489 if (logger.isDebugEnabled()) {
490 logger.debug(testName + ": status = " + statusCode);
492 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
493 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
494 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
495 Assert.assertEquals(1, list.getAccountListItem().size());
496 // Optionally output additional data about list members for debugging.
497 boolean iterateThroughList = true;
498 if (iterateThroughList && logger.isDebugEnabled()) {
499 printList(testName, list);
503 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
504 dependsOnMethods = {"createList", "read"})
505 public void searchEmail(String testName) throws Exception {
508 setupReadList(testName);
510 // Submit the request to the service and store the response.
511 AccountClient client = new AccountClient();
512 ClientResponse<AccountsCommonList> res =
513 client.readSearchList(null, null, "dinoland");
514 AccountsCommonList list = res.getEntity();
515 int statusCode = res.getStatus();
517 // Check the status code of the response: does it match
518 // the expected response(s)?
519 if (logger.isDebugEnabled()) {
520 logger.debug(testName + ": status = " + statusCode);
522 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
523 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
524 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
525 Assert.assertEquals(2, list.getAccountListItem().size());
526 // Optionally output additional data about list members for debugging.
527 boolean iterateThroughList = true;
528 if (iterateThroughList && logger.isDebugEnabled()) {
529 printList(testName, list);
533 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
534 dependsOnMethods = {"createList", "read"})
535 public void searchScreenNameEmail(String testName) throws Exception {
538 setupReadList(testName);
540 // Submit the request to the service and store the response.
541 AccountClient client = new AccountClient();
542 ClientResponse<AccountsCommonList> res =
543 client.readSearchList("tom", null, "jerry");
544 AccountsCommonList list = res.getEntity();
545 int statusCode = res.getStatus();
547 // Check the status code of the response: does it match
548 // the expected response(s)?
549 if (logger.isDebugEnabled()) {
550 logger.debug(testName + ": status = " + statusCode);
552 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
553 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
554 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
555 Assert.assertEquals(1, list.getAccountListItem().size());
556 // Optionally output additional data about list members for debugging.
557 boolean iterateThroughList = true;
558 if (iterateThroughList && logger.isDebugEnabled()) {
559 printList(testName, list);
565 // ---------------------------------------------------------------
566 // CRUD tests : UPDATE tests
567 // ---------------------------------------------------------------
570 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
571 dependsOnMethods = {"read", "readList", "readNonExistent"})
572 public void update(String testName) throws Exception {
575 setupUpdate(testName);
577 AccountClient client = new AccountClient();
578 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
579 if (logger.isDebugEnabled()) {
580 logger.debug(testName + ": read status = " + res.getStatus());
582 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
584 if (logger.isDebugEnabled()) {
585 logger.debug("got object to update with ID: " + knownResourceId);
587 AccountsCommon accountFound =
588 (AccountsCommon) res.getEntity();
589 Assert.assertNotNull(accountFound);
591 //create a new account object to test partial updates
592 AccountsCommon accountToUpdate = new AccountsCommon();
593 accountToUpdate.setCsid(knownResourceId);
594 accountToUpdate.setUserId(accountFound.getUserId());
595 // Update the content of this resource.
596 accountToUpdate.setEmail("updated-" + accountFound.getEmail());
597 if (logger.isDebugEnabled()) {
598 logger.debug("updated object");
599 logger.debug(objectAsXmlString(accountFound,
600 AccountsCommon.class));
603 // Submit the request to the service and store the response.
604 res = client.update(knownResourceId, accountToUpdate);
605 int statusCode = res.getStatus();
606 // Check the status code of the response: does it match the expected response(s)?
607 if (logger.isDebugEnabled()) {
608 logger.debug(testName + ": status = " + statusCode);
610 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
611 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
612 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
614 AccountsCommon accountUpdated = (AccountsCommon) res.getEntity();
615 Assert.assertNotNull(accountUpdated);
617 Assert.assertEquals(accountUpdated.getEmail(),
618 accountToUpdate.getEmail(),
619 "Data in updated object did not match submitted data.");
622 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
623 dependsOnMethods = {"update"})
624 public void updatePassword(String testName) throws Exception {
627 setupUpdate(testName);
629 AccountClient client = new AccountClient();
630 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
631 if (logger.isDebugEnabled()) {
632 logger.debug(testName + ": read status = " + res.getStatus());
634 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
636 if (logger.isDebugEnabled()) {
637 logger.debug(testName + ": got object to update password with ID: " + knownResourceId);
639 AccountsCommon accountFound =
640 (AccountsCommon) res.getEntity();
641 Assert.assertNotNull(accountFound);
643 //create a new account object to test partial updates
644 AccountsCommon accountToUpdate = new AccountsCommon();
645 accountToUpdate.setCsid(knownResourceId);
646 accountToUpdate.setUserId(accountFound.getUserId());
648 accountToUpdate.setPassword("imagination".getBytes());
649 if (logger.isDebugEnabled()) {
650 logger.debug(testName + ": updated object");
651 logger.debug(objectAsXmlString(accountToUpdate,
652 AccountsCommon.class));
655 // Submit the request to the service and store the response.
656 res = client.update(knownResourceId, accountToUpdate);
657 int statusCode = res.getStatus();
658 // Check the status code of the response: does it match the expected response(s)?
659 if (logger.isDebugEnabled()) {
660 logger.debug(testName + ": status = " + statusCode);
662 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
663 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
664 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
667 AccountsCommon accountUpdated = (AccountsCommon) res.getEntity();
668 Assert.assertNotNull(accountUpdated);
670 // Assert.assertEquals(accountUpdated.getPassword(),
671 // accountFound.getPassword(),
672 // "Data in updated object did not match submitted data.");
675 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
676 dependsOnMethods = {"update"})
677 public void updatePasswordWithoutUser(String testName) throws Exception {
680 setupUpdate(testName);
682 AccountsCommon accountToUpdate = new AccountsCommon();
683 accountToUpdate.setCsid(knownResourceId);
684 accountToUpdate.setUserId(null);
686 accountToUpdate.setPassword("imagination".getBytes());
687 if (logger.isDebugEnabled()) {
688 logger.debug(testName + " : updated object");
689 logger.debug(objectAsXmlString(accountToUpdate,
690 AccountsCommon.class));
693 AccountClient client = new AccountClient();
694 // Submit the request to the service and store the response.
695 ClientResponse<AccountsCommon> res = client.update(knownResourceId, accountToUpdate);
696 int statusCode = res.getStatus();
697 // Check the status code of the response: does it match the expected response(s)?
698 if (logger.isDebugEnabled()) {
699 logger.debug(testName + ": status = " + statusCode);
701 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
702 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
703 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
707 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
708 dependsOnMethods = {"update"})
709 public void updateInvalidPassword(String testName) throws Exception {
712 setupUpdate(testName);
713 AccountClient client = new AccountClient();
714 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
715 if (logger.isDebugEnabled()) {
716 logger.debug(testName + ": read status = " + res.getStatus());
718 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
720 if (logger.isDebugEnabled()) {
721 logger.debug(testName + ": got object to update password with ID: " + knownResourceId);
723 AccountsCommon accountFound = (AccountsCommon) res.getEntity();
725 AccountsCommon accountToUpdate = new AccountsCommon();
726 accountToUpdate.setCsid(knownResourceId);
727 accountToUpdate.setUserId(accountFound.getUserId());
728 Assert.assertNotNull(accountToUpdate);
731 accountToUpdate.setPassword("abc123".getBytes());
732 if (logger.isDebugEnabled()) {
733 logger.debug(testName + ": updated object");
734 logger.debug(objectAsXmlString(accountToUpdate,
735 AccountsCommon.class));
738 // Submit the request to the service and store the response.
739 res = client.update(knownResourceId, accountToUpdate);
740 int statusCode = res.getStatus();
741 // Check the status code of the response: does it match the expected response(s)?
742 if (logger.isDebugEnabled()) {
743 logger.debug(testName + ": status = " + statusCode);
745 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
746 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
747 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
751 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
752 dependsOnMethods = {"updatePasswordWithoutUser"})
753 public void deactivate(String testName) throws Exception {
756 setupUpdate(testName);
758 AccountClient client = new AccountClient();
759 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
760 if (logger.isDebugEnabled()) {
761 logger.debug(testName + ": read status = " + res.getStatus());
763 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
765 if (logger.isDebugEnabled()) {
766 logger.debug("got object to update with ID: " + knownResourceId);
768 AccountsCommon accountFound = (AccountsCommon) res.getEntity();
770 //create a new account object to test partial updates
771 AccountsCommon accountToUpdate = new AccountsCommon();
772 accountToUpdate.setCsid(knownResourceId);
773 accountToUpdate.setUserId(accountFound.getUserId());
775 // Update the content of this resource.
776 accountToUpdate.setStatus(Status.INACTIVE);
777 if (logger.isDebugEnabled()) {
778 logger.debug("updated object");
779 logger.debug(objectAsXmlString(accountToUpdate,
780 AccountsCommon.class));
783 // Submit the request to the service and store the response.
784 res = client.update(knownResourceId, accountToUpdate);
785 int statusCode = res.getStatus();
786 // Check the status code of the response: does it match the expected response(s)?
787 if (logger.isDebugEnabled()) {
788 logger.debug(testName + ": status = " + statusCode);
790 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
791 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
792 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
794 AccountsCommon accountUpdated = (AccountsCommon) res.getEntity();
795 Assert.assertNotNull(accountUpdated);
797 Assert.assertEquals(accountUpdated.getStatus(),
798 accountToUpdate.getStatus(),
799 "Data in updated object did not match submitted data.");
804 // Placeholders until the three tests below can be uncommented.
805 // See Issue CSPACE-401.
807 public void updateWithEmptyEntityBody(String testName) throws Exception {
808 //FIXME: Should this test really be empty? If so, please comment accordingly.
812 public void updateWithMalformedXml(String testName) throws Exception {
813 //FIXME: Should this test really be empty? If so, please comment accordingly.
817 public void updateWithWrongXmlSchema(String testName) throws Exception {
818 //FIXME: Should this test really be empty? If so, please comment accordingly.
822 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
823 dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
824 public void updateNonExistent(String testName) throws Exception {
827 setupUpdateNonExistent(testName);
829 // Submit the request to the service and store the response.
831 // Note: The ID used in this 'create' call may be arbitrary.
832 // The only relevant ID may be the one used in updateAccount(), below.
833 AccountClient client = new AccountClient();
834 AccountsCommon account =
835 createAccountInstance("simba", "simba", "tiger", "simba@lionking.com",
836 true, false, true, true);
837 ClientResponse<AccountsCommon> res =
838 client.update(NON_EXISTENT_ID, account);
839 int statusCode = res.getStatus();
841 // Check the status code of the response: does it match
842 // the expected response(s)?
843 if (logger.isDebugEnabled()) {
844 logger.debug(testName + ": status = " + statusCode);
846 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
847 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
848 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
851 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
852 dependsOnMethods = {"deactivate", "readNonExistent", "testSubmitRequest"})
853 public void updateWrongUser(String testName) throws Exception {
857 // Submit the request to the service and store the response.
859 // Note: The ID used in this 'create' call may be arbitrary.
860 // The only relevant ID may be the one used in updateAccount(), below.
861 AccountClient client = new AccountClient();
862 ClientResponse<AccountsCommon> res = client.read(knownResourceId);
863 if (logger.isDebugEnabled()) {
864 logger.debug(testName + ": read status = " + res.getStatus());
866 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
868 if (logger.isDebugEnabled()) {
869 logger.debug("got object to update with ID: " + knownResourceId);
871 AccountsCommon accountToUpdate =
872 (AccountsCommon) res.getEntity();
873 Assert.assertNotNull(accountToUpdate);
875 accountToUpdate.setUserId("barneyFake");
876 if (logger.isDebugEnabled()) {
877 logger.debug("updated object with wrongUser");
878 logger.debug(objectAsXmlString(accountToUpdate,
879 AccountsCommon.class));
882 res = client.update(knownResourceId, accountToUpdate);
883 int statusCode = res.getStatus();
885 // Check the status code of the response: does it match
886 // the expected response(s)?
887 if (logger.isDebugEnabled()) {
888 logger.debug(testName + ": status = " + statusCode);
890 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
891 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
892 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
895 // ---------------------------------------------------------------
896 // CRUD tests : DELETE tests
897 // ---------------------------------------------------------------
900 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
901 dependsOnMethods = {"testSubmitRequest", "updateWrongUser"})
902 public void delete(String testName) throws Exception {
905 setupDelete(testName);
907 // Submit the request to the service and store the response.
908 AccountClient client = new AccountClient();
909 ClientResponse<Response> res = client.delete(knownResourceId);
910 int statusCode = res.getStatus();
912 // Check the status code of the response: does it match
913 // the expected response(s)?
914 if (logger.isDebugEnabled()) {
915 logger.debug(testName + ": status = " + statusCode);
917 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
918 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
919 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
924 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
925 dependsOnMethods = {"delete"})
926 public void deleteNonExistent(String testName) throws Exception {
929 setupDeleteNonExistent(testName);
931 // Submit the request to the service and store the response.
932 AccountClient client = new AccountClient();
933 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
934 int statusCode = res.getStatus();
936 // Check the status code of the response: does it match
937 // the expected response(s)?
938 if (logger.isDebugEnabled()) {
939 logger.debug(testName + ": status = " + statusCode);
941 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
942 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
943 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
946 // ---------------------------------------------------------------
947 // Utility tests : tests of code used in tests above
948 // ---------------------------------------------------------------
950 * Tests the code for manually submitting data that is used by several
951 * of the methods above.
953 @Test(dependsOnMethods = {"create", "read"})
954 public void testSubmitRequest() throws Exception {
956 // Expected status code: 200 OK
957 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
959 // Submit the request to the service and store the response.
960 String method = ServiceRequestType.READ.httpMethodName();
961 String url = getResourceURL(knownResourceId);
962 int statusCode = submitRequest(method, url);
964 // Check the status code of the response: does it match
965 // the expected response(s)?
966 if (logger.isDebugEnabled()) {
967 logger.debug("testSubmitRequest: url=" + url
968 + " status=" + statusCode);
970 Assert.assertEquals(statusCode, EXPECTED_STATUS);
974 // ---------------------------------------------------------------
975 // Utility methods used by tests above
976 // ---------------------------------------------------------------
978 * create account instance
983 * @param useScreenName
984 * @param invalidTenant
989 AccountsCommon createAccountInstance(String screenName,
990 String userName, String passwd, String email,
991 boolean useScreenName, boolean invalidTenant, boolean useUser, boolean usePassword) {
993 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
994 userName, passwd, email, useScreenName,
995 addTenant, invalidTenant, useUser, usePassword);
997 if (logger.isDebugEnabled()) {
998 logger.debug("to be created, account common");
999 logger.debug(objectAsXmlString(account,
1000 AccountsCommon.class));
1006 @AfterClass(alwaysRun = true)
1007 public void cleanUp() {
1008 setupDelete("delete");
1009 String noTest = System.getProperty("noTestCleanup");
1010 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
1011 if (logger.isDebugEnabled()) {
1012 logger.debug("Skipping Cleanup phase ...");
1016 if (logger.isDebugEnabled()) {
1017 logger.debug("Cleaning up temporary resources created for testing ...");
1019 AccountClient client = new AccountClient();
1020 for (String resourceId : allResourceIdsCreated) {
1021 // Note: Any non-success responses are ignored and not reported.
1022 ClientResponse<Response> res = client.delete(resourceId);
1023 int statusCode = res.getStatus();
1024 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
1025 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
1026 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
1030 private void printList(String testName, AccountsCommonList list) {
1031 List<AccountsCommonList.AccountListItem> items =
1032 list.getAccountListItem();
1035 for (AccountsCommonList.AccountListItem item : items) {
1036 logger.debug(testName + ": list-item[" + i + "] csid="
1038 logger.debug(testName + ": list-item[" + i + "] screenName="
1039 + item.getScreenName());
1040 logger.debug(testName + ": list-item[" + i + "] URI="