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.Collection;
27 import java.util.Hashtable;
28 import java.util.List;
29 import javax.ws.rs.core.Response;
31 //import org.collectionspace.services.authorization.AccountRolesList;
32 import org.collectionspace.services.account.AccountsCommon;
33 import org.collectionspace.services.account.AccountsCommonList;
34 import org.collectionspace.services.authorization.AccountRole;
35 import org.collectionspace.services.authorization.AccountValue;
36 import org.collectionspace.services.authorization.Role;
37 import org.collectionspace.services.authorization.RoleValue;
38 import org.collectionspace.services.client.AccountClient;
39 import org.collectionspace.services.client.AccountFactory;
40 import org.collectionspace.services.client.AccountRoleClient;
41 import org.collectionspace.services.client.AccountRoleFactory;
42 import org.collectionspace.services.client.CollectionSpaceClient;
43 import org.collectionspace.services.client.RoleClient;
44 import org.collectionspace.services.client.RoleFactory;
45 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
46 import org.collectionspace.services.client.test.ServiceRequestType;
47 import org.collectionspace.services.jaxb.AbstractCommonList;
48 import org.jboss.resteasy.client.ClientResponse;
51 import org.testng.Assert;
52 import org.testng.annotations.Test;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.testng.annotations.AfterClass;
57 import org.testng.annotations.BeforeClass;
60 * AccountServiceTest, carries out tests against a
61 * deployed and running Account, Role and AccountRole Services.
63 * $LastChangedRevision: 917 $
64 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
66 public class AccountRoleServiceTest extends AbstractServiceTestImpl {
68 /** The Constant logger. */
69 private final static String CLASS_NAME = AccountRoleServiceTest.class.getName();
70 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
71 // Instance variables specific to this test.
72 /** The known resource id. */
73 private String knownResourceId = null;
74 private String prebuiltAdminCSID = null;
75 private String prebuiltAdminUserId = "admin@core.collectionspace.org";
76 /** The all resource ids created. */
77 private List<String> allResourceIdsCreated = new ArrayList<String>();
78 /** The acc values. */
79 private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
80 /** The role values. */
81 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
83 * This method is called only by the parent class, AbstractServiceTestImpl
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
90 protected String getServicePathComponent() {
91 return new AccountRoleClient().getServicePathComponent();
97 @BeforeClass(alwaysRun = true)
98 public void seedData() {
99 String userId = "acc-role-user1";
100 String accId = createAccount(userId, "acc-role-user1-test@cspace.org");
101 AccountValue ava = new AccountValue();
102 ava.setScreenName(userId);
103 ava.setUserId(userId);
104 ava.setAccountId(accId);
105 accValues.put(ava.getScreenName(), ava);
107 String userId2 = "acc-role-user2";
108 String coAccId = createAccount(userId2, "acc-role-user2-test@cspace.org");
109 AccountValue avc = new AccountValue();
110 avc.setScreenName(userId2);
111 avc.setUserId(userId2);
112 avc.setAccountId(coAccId);
113 accValues.put(avc.getScreenName(), avc);
115 String rn1 = "ROLE_CO1";
116 String r1RoleId = createRole(rn1);
117 RoleValue rv1 = new RoleValue();
118 rv1.setRoleId(r1RoleId);
119 rv1.setRoleName(rn1);
120 roleValues.put(rv1.getRoleName(), rv1);
122 String rn2 = "ROLE_CO2";
123 String r2RoleId = createRole(rn2);
124 RoleValue rv2 = new RoleValue();
125 rv2.setRoleId(r2RoleId);
126 rv2.setRoleName(rn2);
127 roleValues.put(rv2.getRoleName(), rv2);
131 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
134 protected CollectionSpaceClient getClientInstance() {
135 return new AccountRoleClient();
139 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
142 protected AbstractCommonList getAbstractCommonList(
143 ClientResponse<AbstractCommonList> response) {
144 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
145 throw new UnsupportedOperationException();
149 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
151 @Test(dataProvider = "testName")
153 public void readPaginatedList(String testName) throws Exception {
154 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
157 // ---------------------------------------------------------------
158 // CRUD tests : CREATE tests
159 // ---------------------------------------------------------------
162 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
165 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
166 public void create(String testName) throws Exception {
168 if (logger.isDebugEnabled()) {
169 testBanner(testName, CLASS_NAME);
171 // Perform setup, such as initializing the type of service request
172 // (e.g. CREATE, DELETE), its valid and expected status codes, and
173 // its associated HTTP method name (e.g. POST, DELETE).
176 // Submit the request to the service and store the response.
177 AccountValue av = accValues.get("acc-role-user1");
178 AccountRole accRole = createAccountRoleInstance(av,
179 roleValues.values(), true, true);
180 AccountRoleClient client = new AccountRoleClient();
181 ClientResponse<Response> res = client.create(av.getAccountId(), accRole);
183 int statusCode = res.getStatus();
185 if (logger.isDebugEnabled()) {
186 logger.debug(testName + ": status = " + statusCode);
188 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
189 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
190 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
192 // Store the ID returned from this create operation
193 // for additional tests below.
194 //this is is not important in case of this relationship
195 knownResourceId = extractId(res);
196 if (logger.isDebugEnabled()) {
197 logger.debug(testName + ": knownResourceId=" + knownResourceId);
200 res.releaseConnection();
204 //to not cause uniqueness violation for accRole, createList is removed
206 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
208 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
209 dependsOnMethods = {"create"})
211 public void createList(String testName) throws Exception {
212 //FIXME: Should this test really be empty? If so, please comment accordingly.
216 // Placeholders until the three tests below can be uncommented.
217 // See Issue CSPACE-401.
219 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
222 public void createWithEmptyEntityBody(String testName) throws Exception {
223 //FIXME: Should this test really be empty? If so, please comment accordingly.
227 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
230 public void createWithMalformedXml(String testName) throws Exception {
231 //FIXME: Should this test really be empty? If so, please comment accordingly.
235 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
238 public void createWithWrongXmlSchema(String testName) throws Exception {
239 //FIXME: Should this test really be empty? If so, please comment accordingly.
242 // ---------------------------------------------------------------
243 // CRUD tests : READ tests
244 // ---------------------------------------------------------------
247 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
250 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
251 dependsOnMethods = {"create"})
252 public void read(String testName) throws Exception {
254 if (logger.isDebugEnabled()) {
255 testBanner(testName, CLASS_NAME);
261 // Submit the request to the service and store the response.
262 AccountRoleClient client = new AccountRoleClient();
263 ClientResponse<AccountRole> res = client.read(
264 accValues.get("acc-role-user1").getAccountId());
265 int statusCode = res.getStatus();
267 // Check the status code of the response: does it match
268 // the expected response(s)?
269 if (logger.isDebugEnabled()) {
270 logger.debug(testName + ": status = " + statusCode);
272 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
273 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
274 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
276 AccountRole output = res.getEntity();
277 Assert.assertNotNull(output);
279 res.releaseConnection();
285 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
288 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
289 public void readNonExistent(String testName) throws Exception {
291 if (logger.isDebugEnabled()) {
292 testBanner(testName, CLASS_NAME);
296 setupReadNonExistent();
298 // Submit the request to the service and store the response.
299 AccountRoleClient client = new AccountRoleClient();
300 ClientResponse<AccountRole> res = client.read(this.NON_EXISTENT_ID);
301 int statusCode = res.getStatus();
303 // Check the status code of the response: does it match
304 // the expected response(s)?
305 if (logger.isDebugEnabled()) {
306 logger.debug(testName + ": status = " + statusCode);
308 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
309 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
310 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
312 res.releaseConnection();
316 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
317 dependsOnMethods = {"create"})
318 public void readNoRelationship(String testName) throws Exception {
320 if (logger.isDebugEnabled()) {
321 testBanner(testName, CLASS_NAME);
327 // Submit the request to the service and store the response.
328 AccountRoleClient client = new AccountRoleClient();
329 ClientResponse<AccountRole> res = client.read(
330 accValues.get("acc-role-user2").getAccountId());
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, Response.Status.OK.getStatusCode());
341 AccountRole output = res.getEntity();
343 String sOutput = objectAsXmlString(output, AccountRole.class);
344 if(logger.isDebugEnabled()) {
345 logger.debug(testName + " received " + sOutput);
348 res.releaseConnection();
352 // ---------------------------------------------------------------
353 // CRUD tests : READ_LIST tests
354 // ---------------------------------------------------------------
357 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
360 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
361 dependsOnMethods = {"createList", "read"})
362 public void readList(String testName) throws Exception {
363 //FIXME: Should this test really be empty? If so, please comment accordingly.
368 // ---------------------------------------------------------------
369 // CRUD tests : UPDATE tests
370 // ---------------------------------------------------------------
373 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
376 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
377 dependsOnMethods = {"read", "readList", "readNonExistent"})
378 public void update(String testName) throws Exception {
379 //FIXME: Should this test really be empty? If so, please comment accordingly.
383 // Placeholders until the three tests below can be uncommented.
384 // See Issue CSPACE-401.
386 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
389 public void updateWithEmptyEntityBody(String testName) throws Exception {
390 //FIXME: Should this test really be empty? If so, please comment accordingly.
394 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
397 public void updateWithMalformedXml(String testName) throws Exception {
398 //FIXME: Should this test really be empty? If so, please comment accordingly.
402 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
405 public void updateWithWrongXmlSchema(String testName) throws Exception {
406 //FIXME: Should this test really be empty? If so, please comment accordingly.
410 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
413 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
414 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
415 public void updateNonExistent(String testName) throws Exception {
416 //FIXME: Should this test really be empty? If so, please comment accordingly.
419 // ---------------------------------------------------------------
420 // CRUD tests : DELETE tests
421 // ---------------------------------------------------------------
424 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
427 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
428 dependsOnMethods = {"read"})
429 public void delete(String testName) throws Exception {
431 if (logger.isDebugEnabled()) {
432 testBanner(testName, CLASS_NAME);
439 // Lookup a know account, and delete all of its role relationships
441 AccountRoleClient client = new AccountRoleClient();
442 ClientResponse<AccountRole> readResponse = client.read(
443 accValues.get("acc-role-user1").getAccountId());
444 AccountRole toDelete = null;
446 toDelete = readResponse.getEntity();
448 readResponse.releaseConnection();
451 ClientResponse<Response> res = client.delete(
452 toDelete.getAccounts().get(0).getAccountId(), toDelete);
454 int statusCode = res.getStatus();
455 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
456 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
457 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
459 res.releaseConnection();
463 // recreate 'acc-role-user1' account and roles
469 // Lookup a know account, and delete all of its role relationships
471 readResponse = client.read(
472 accValues.get("acc-role-user1").getAccountId());
475 toDelete = readResponse.getEntity();
477 readResponse.releaseConnection();
480 res = client.delete(toDelete.getAccounts().get(0).getAccountId());
482 int statusCode = res.getStatus();
483 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
484 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
485 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
487 res.releaseConnection();
492 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
493 dependsOnMethods = {"read"})
494 public void deleteLockedAccount(String testName) throws Exception {
496 if (logger.isDebugEnabled()) {
497 testBanner(testName, CLASS_NAME);
500 findPrebuiltAdminAccount();
503 EXPECTED_STATUS_CODE = Response.Status.FORBIDDEN.getStatusCode();
504 REQUEST_TYPE = ServiceRequestType.DELETE;
505 testSetup(EXPECTED_STATUS_CODE, REQUEST_TYPE);
507 AccountRoleClient client = new AccountRoleClient();
508 ClientResponse<Response> res = client.delete(prebuiltAdminCSID);
510 int statusCode = res.getStatus();
511 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
512 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
513 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
515 res.releaseConnection();
521 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
524 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
525 public void deleteNonExistent(String testName) throws Exception {
526 //ignoring this test as the service side returns 200 now even if it does
527 //not find a record in the db
530 // ---------------------------------------------------------------
532 // ---------------------------------------------------------------
535 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
536 public void searchWorkflowDeleted(String testName) throws Exception {
537 // Fixme: null test for now, overriding test in base class
540 // ---------------------------------------------------------------
541 // Utility tests : tests of code used in tests above
542 // ---------------------------------------------------------------
544 * Tests the code for manually submitting data that is used by several
545 * of the methods above.
548 @Test(dependsOnMethods = {"create"})
549 public void testSubmitRequest() throws Exception { //FIXME: REM - This is not testing a submit /accounts/*/accountroles, but instead just to /accounts
551 // Expected status code: 200 OK
552 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
554 // Submit the request to the service and store the response.
555 String method = ServiceRequestType.READ.httpMethodName();
556 String url = getResourceURL(accValues.get("acc-role-user1").getAccountId());
557 int statusCode = submitRequest(method, url);
559 // Check the status code of the response: does it match
560 // the expected response(s)?
561 if (logger.isDebugEnabled()) {
562 logger.debug("testSubmitRequest: url=" + url
563 + " status=" + statusCode);
565 Assert.assertEquals(statusCode, EXPECTED_STATUS);
569 // ---------------------------------------------------------------
570 // Utility methods used by tests above
571 // ---------------------------------------------------------------
573 * Creates the account role instance.
577 * @param usePermId the use perm id
578 * @param useRoleId the use role id
579 * @return the account role
581 static public AccountRole createAccountRoleInstance(AccountValue pv,
582 Collection<RoleValue> rvs,
586 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
587 pv, rvs, usePermId, useRoleId);
589 if (logger.isDebugEnabled()) {
590 logger.debug("to be created, accRole common");
591 logger.debug(objectAsXmlString(accRole, AccountRole.class));
599 @AfterClass(alwaysRun = true)
601 public void cleanUp() {
605 String noTest = System.getProperty("noTestCleanup");
606 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
607 if (logger.isDebugEnabled()) {
608 logger.debug("Skipping Cleanup phase ...");
612 if (logger.isDebugEnabled()) {
613 logger.debug("Cleaning up temporary resources created for testing ...");
617 for (AccountValue pv : accValues.values()) {
618 deleteAccount(pv.getAccountId());
621 for (RoleValue rv : roleValues.values()) {
622 deleteRole(rv.getRoleId());
627 * Creates the account.
629 * @param userName the user name
630 * @param email the email
633 private String createAccount(String userName, String email) {
635 if (logger.isDebugEnabled()) {
636 testBanner("createAccount");
641 AccountClient accClient = new AccountClient();
642 AccountsCommon account = AccountFactory.createAccountInstance(
643 userName, userName, userName, email, accClient.getTenantId(),
644 true, false, true, true);
645 ClientResponse<Response> res = accClient.create(account);
646 int statusCode = res.getStatus();
647 if (logger.isDebugEnabled()) {
648 logger.debug("createAccount: userName=" + userName
649 + " status = " + statusCode);
651 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
652 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
653 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
654 res.releaseConnection();
655 return extractId(res);
658 private void findPrebuiltAdminAccount() {
659 // Search for the prebuilt admin user and then hold its CSID
660 if(prebuiltAdminCSID == null) {
662 AccountClient client = new AccountClient();
663 ClientResponse<AccountsCommonList> res =
664 client.readSearchList(null, this.prebuiltAdminUserId, null);
665 AccountsCommonList list = res.getEntity();
666 int statusCode = res.getStatus();
668 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
669 List<AccountsCommonList.AccountListItem> items = list.getAccountListItem();
670 Assert.assertEquals(1, items.size(), "Found more than one Admin account!");
671 AccountsCommonList.AccountListItem item = items.get(0);
672 prebuiltAdminCSID = item.getCsid();
673 if (logger.isDebugEnabled()) {
674 logger.debug("Found Admin Account with ID: " + prebuiltAdminCSID);
682 * @param accId the acc id
684 private void deleteAccount(String accId) {
686 if (logger.isDebugEnabled()) {
687 testBanner("deleteAccount");
692 AccountClient accClient = new AccountClient();
693 ClientResponse<Response> res = accClient.delete(accId);
694 int statusCode = res.getStatus();
696 if (logger.isDebugEnabled()) {
697 logger.debug("deleteAccount: delete account id="
698 + accId + " status=" + statusCode);
700 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
701 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
702 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
704 res.releaseConnection();
711 * @param roleName the role name
714 private String createRole(String roleName) {
715 if (logger.isDebugEnabled()) {
716 testBanner("createRole");
719 RoleClient roleClient = new RoleClient();
721 Role role = RoleFactory.createRoleInstance(roleName,
722 roleName, //the display name
723 "role for " + roleName, true);
724 ClientResponse<Response> res = roleClient.create(role);
725 int statusCode = res.getStatus();
726 if (logger.isDebugEnabled()) {
727 logger.debug("createRole: name=" + roleName
728 + " status = " + statusCode);
730 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
731 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
732 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
733 res.releaseConnection();
734 return extractId(res);
740 * @param roleId the role id
742 private void deleteRole(String roleId) {
743 if (logger.isDebugEnabled()) {
744 testBanner("deleteRole");
747 RoleClient roleClient = new RoleClient();
748 ClientResponse<Response> res = roleClient.delete(roleId);
749 int statusCode = res.getStatus();
751 if (logger.isDebugEnabled()) {
752 logger.debug("deleteRole: delete role id=" + roleId
753 + " status=" + statusCode);
755 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
756 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
757 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
759 res.releaseConnection();
764 protected String getServiceName() {
765 // AccountRoles service is a sub-service of the Account service, so we return Account's service name
766 return AccountClient.SERVICE_NAME;