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 permRoles and
21 * limitations under the License.
23 package org.collectionspace.services.authorization.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.perms.EffectType;
32 import org.collectionspace.services.authorization.perms.Permission;
33 import org.collectionspace.services.authorization.perms.PermissionAction;
34 import org.collectionspace.services.authorization.PermissionRole;
35 import org.collectionspace.services.authorization.PermissionValue;
36 import org.collectionspace.services.authorization.Role;
37 import org.collectionspace.services.authorization.RoleValue;
39 import org.collectionspace.services.client.CollectionSpaceClient;
40 import org.collectionspace.services.client.PermissionClient;
41 import org.collectionspace.services.client.PermissionFactory;
42 import org.collectionspace.services.client.PermissionRoleClient;
43 import org.collectionspace.services.client.PermissionRoleFactory;
44 import org.collectionspace.services.client.RoleClient;
45 import org.collectionspace.services.client.RoleFactory;
47 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
48 import org.collectionspace.services.client.test.ServiceRequestType;
50 import org.jboss.resteasy.client.ClientResponse;
52 import org.testng.Assert;
53 import org.testng.annotations.Test;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.testng.annotations.AfterClass;
58 import org.testng.annotations.BeforeClass;
61 * PermissionServiceTest, carries out tests against a
62 * deployed and running Permission, Role and PermissionRole Services.
64 * $LastChangedRevision: 917 $
65 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
67 public class PermissionRoleServiceTest extends AbstractServiceTestImpl<PermissionRole, PermissionRole,
68 PermissionRole, PermissionRole> {
70 /** The Constant logger. */
71 private final static String CLASS_NAME = PermissionRoleServiceTest.class.getName();
72 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
74 // Instance variables specific to this test.
75 final private static String TEST_MARKER = "_PermissionRoleServiceTest";
76 final private static String TEST_SERVICE_NAME = "fakeservice";
77 final private static String NO_REL_SUFFIX = "-no-rel";
78 /** The perm values. */
79 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
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 PermissionRoleClient().getServicePathComponent();
95 protected String getServiceName() {
96 return PermissionClient.SERVICE_NAME; //Since we're a sub-resource of permission service return its name?
102 @BeforeClass(alwaysRun = true)
103 public void seedData() {
104 String ra = TEST_SERVICE_NAME + TEST_MARKER;
105 String accPermId = createPermission(ra, EffectType.PERMIT);
106 PermissionValue pva = new PermissionValue();
107 pva.setResourceName(ra);
108 pva.setPermissionId(accPermId);
109 permValues.put(pva.getResourceName(), pva);
111 String rc = TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX;
112 String coPermId = createPermission(rc, EffectType.DENY);
113 PermissionValue pvc = new PermissionValue();
114 pvc.setResourceName(rc);
115 pvc.setPermissionId(coPermId);
116 permValues.put(pvc.getResourceName(), pvc);
118 // String ri = "intakes";
119 // String iPermId = createPermission(ri, EffectType.DENY);
120 // PermissionValue pvi = new PermissionValue();
121 // pvi.setResourceName(ri);
122 // pvi.setPermissionId(iPermId);
123 // permValues.put(pvi.getResourceName(), pvi);
125 String rn1 = "ROLE_CO1" + TEST_MARKER;
126 String r1RoleId = createRole(rn1);
127 RoleValue rv1 = new RoleValue();
128 rv1.setRoleId(r1RoleId);
129 rv1.setRoleName(rn1);
130 roleValues.put(rv1.getRoleName(), rv1);
132 String rn2 = "ROLE_CO2" + TEST_MARKER;
133 String r2RoleId = createRole(rn2);
134 RoleValue rv2 = new RoleValue();
135 rv2.setRoleId(r2RoleId);
136 rv2.setRoleName(rn2);
137 roleValues.put(rv2.getRoleName(), rv2);
141 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
144 protected CollectionSpaceClient getClientInstance() {
145 return new PermissionRoleClient();
149 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
152 protected PermissionRole getCommonList(
153 ClientResponse<PermissionRole> response) {
154 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
155 throw new UnsupportedOperationException();
159 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
162 public void readPaginatedList(String testName) throws Exception {
163 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
165 // ---------------------------------------------------------------
166 // CRUD tests : CREATE tests
167 // ---------------------------------------------------------------
170 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
174 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
175 public void create(String testName) throws Exception {
176 // Perform setup, such as initializing the type of service request
177 // (e.g. CREATE, DELETE), its valid and expected status codes, and
178 // its associated HTTP method name (e.g. POST, DELETE).
181 // Submit the request to the service and store the response.
182 PermissionValue pv = permValues.get(TEST_SERVICE_NAME + TEST_MARKER);
183 PermissionRole permRole = createPermissionRoleInstance(pv,
184 roleValues.values(), true, true);
185 PermissionRoleClient client = new PermissionRoleClient();
186 ClientResponse<Response> res = null;
188 res = client.create(pv.getPermissionId(), permRole);
189 int statusCode = res.getStatus();
191 if (logger.isDebugEnabled()) {
192 logger.debug(testName + ": status = " + statusCode);
194 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
195 invalidStatusCodeMessage(testRequestType, statusCode));
196 Assert.assertEquals(statusCode, testExpectedStatusCode);
198 knownResourceId = extractId(res); //This is meaningless in this test, see getKnowResourceId() method for details
199 if (logger.isDebugEnabled()) {
200 logger.debug(testName + ": knownResourceId=" + knownResourceId);
204 res.releaseConnection();
209 //to not cause uniqueness violation for permRole, createList is removed
211 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
214 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
215 // dependsOnMethods = {"create"})
216 public void createList(String testName) throws Exception {
217 //Should this really be empty?
221 // Placeholders until the three tests below can be uncommented.
222 // See Issue CSPACE-401.
224 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
227 public void createWithEmptyEntityBody(String testName) throws Exception {
228 //Should this really be empty?
232 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
235 public void createWithMalformedXml(String testName) throws Exception {
236 //Should this really be empty?
240 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
243 public void createWithWrongXmlSchema(String testName) throws Exception {
244 //Should this really be empty?
247 // ---------------------------------------------------------------
248 // CRUD tests : READ tests
249 // ---------------------------------------------------------------
252 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
255 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
256 // dependsOnMethods = {"create"})
257 public void read(String testName) throws Exception {
261 // Submit the request to the service and store the response.
262 PermissionRoleClient client = new PermissionRoleClient();
263 ClientResponse<PermissionRole> res = null;
266 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
267 int statusCode = res.getStatus();
269 // Check the status code of the response: does it match
270 // the expected response(s)?
271 if (logger.isDebugEnabled()) {
272 logger.debug(testName + ": status = " + statusCode);
274 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
275 invalidStatusCodeMessage(testRequestType, statusCode));
276 Assert.assertEquals(statusCode, testExpectedStatusCode);
278 PermissionRole output = (PermissionRole) res.getEntity();
279 Assert.assertNotNull(output);
282 res.releaseConnection();
290 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
293 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
294 public void readNonExistent(String testName) throws Exception {
296 setupReadNonExistent();
298 // Submit the request to the service and store the response.
299 PermissionRoleClient client = new PermissionRoleClient();
300 ClientResponse<PermissionRole> res = null;
302 res = client.read(NON_EXISTENT_ID);
303 int statusCode = res.getStatus();
305 // Check the status code of the response: does it match
306 // the expected response(s)?
307 if (logger.isDebugEnabled()) {
308 logger.debug(testName + ": status = " + statusCode);
310 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
311 invalidStatusCodeMessage(testRequestType, statusCode));
312 Assert.assertEquals(statusCode, testExpectedStatusCode);
315 res.releaseConnection();
320 @Test(dataProvider = "testName",
321 dependsOnMethods = {"CRUDTests"})
322 public void readNoRelationship(String testName) throws Exception {
326 // Submit the request to the service and store the response.
327 PermissionRoleClient client = new PermissionRoleClient();
328 ClientResponse<PermissionRole> res = null;
331 permValues.get(TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX).getPermissionId());
332 // Check the status code of the response: does it match
333 // the expected response(s)?
334 assertStatusCode(res, testName);
335 PermissionRole output = (PermissionRole) res.getEntity();
337 String sOutput = objectAsXmlString(output, PermissionRole.class);
338 if (logger.isDebugEnabled()) {
339 logger.debug(testName + " received " + sOutput);
343 res.releaseConnection();
348 // ---------------------------------------------------------------
349 // CRUD tests : READ_LIST tests
350 // ---------------------------------------------------------------
353 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
357 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
358 // dependsOnMethods = {"createList", "read"})
359 public void readList(String testName) throws Exception {
360 //Should this really be empty?
365 // ---------------------------------------------------------------
366 // CRUD tests : UPDATE tests
367 // ---------------------------------------------------------------
370 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
373 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
374 // dependsOnMethods = {"read", "readList", "readNonExistent"})
375 public void update(String testName) throws Exception {
376 //Should this really be empty?
380 // Placeholders until the three tests below can be uncommented.
381 // See Issue CSPACE-401.
383 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
386 public void updateWithEmptyEntityBody(String testName) throws Exception {
387 //Should this really be empty?
391 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
394 public void updateWithMalformedXml(String testName) throws Exception {
395 //Should this really be empty?
399 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
402 public void updateWithWrongXmlSchema(String testName) throws Exception {
403 //Should this really be empty?
407 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
410 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
411 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
412 public void updateNonExistent(String testName) throws Exception {
413 //Should this really be empty?
416 // ---------------------------------------------------------------
417 // CRUD tests : DELETE tests
418 // ---------------------------------------------------------------
421 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
424 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
425 // dependsOnMethods = {"read"})
426 public void delete(String testName) throws Exception {
433 PermissionRoleClient client = new PermissionRoleClient();
434 ClientResponse<PermissionRole> readResponse = client.read(
435 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
436 PermissionRole toDelete = null;
438 toDelete = readResponse.getEntity();
440 readResponse.releaseConnection();
443 ClientResponse<Response> res = client.delete(
444 toDelete.getPermission().get(0).getPermissionId(), toDelete);
446 int statusCode = res.getStatus();
447 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
448 invalidStatusCodeMessage(testRequestType, statusCode));
449 Assert.assertEquals(statusCode, testExpectedStatusCode);
452 res.releaseConnection();
457 // recreate 'TEST_SERVICE_NAME + TEST_MARKER' account and roles
463 // Lookup a know permission, and delete all of its role relationships
465 readResponse = client.read(
466 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
469 toDelete = readResponse.getEntity();
471 readResponse.releaseConnection();
474 res = client.delete(toDelete.getPermission().get(0).getPermissionId());
476 int statusCode = res.getStatus();
477 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
478 invalidStatusCodeMessage(testRequestType, statusCode));
479 Assert.assertEquals(statusCode, testExpectedStatusCode);
481 res.releaseConnection();
488 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
491 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
492 public void deleteNonExistent(String testName) throws Exception {
493 //ignoring this test as the service side returns 200 now even if it does
494 //not find a record in the db
497 // ---------------------------------------------------------------
499 // ---------------------------------------------------------------
502 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
503 public void searchWorkflowDeleted(String testName) throws Exception {
504 // Fixme: null test for now, overriding test in base class
508 protected String getKnowResourceId() {
509 return permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId();
512 // ---------------------------------------------------------------
513 // Utility methods used by tests above
514 // ---------------------------------------------------------------
516 * create permRolerole instance
517 * @param pv permissionvalue
518 * @param rvs rolevalue array
521 * @return PermissionRole
523 public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
524 Collection<RoleValue> rvs,
528 List<RoleValue> rvls = new ArrayList<RoleValue>();
530 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
531 pv, rvls, usePermId, useRoleId);
532 if (logger.isDebugEnabled()) {
533 logger.debug("to be created, permRole");
534 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
542 @AfterClass(alwaysRun = true)
544 public void cleanUp() {
546 String noTest = System.getProperty("noTestCleanup");
547 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
548 if (logger.isDebugEnabled()) {
549 logger.debug("Skipping Cleanup phase ...");
553 if (logger.isDebugEnabled()) {
554 logger.debug("Cleaning up temporary resources created for testing ...");
557 for (PermissionValue pv : permValues.values()) {
558 deletePermission(pv.getPermissionId());
560 for (RoleValue rv : roleValues.values()) {
561 deleteRole(rv.getRoleId());
566 * Creates the permission.
568 * @param resName the res name
569 * @param effect the effect
572 private String createPermission(String resName, EffectType effect) {
573 if (logger.isDebugEnabled()) {
574 logger.debug(getTestBanner("createPermission"));
577 PermissionClient permClient = new PermissionClient();
578 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
579 Permission permission = PermissionFactory.createPermissionInstance(resName,
580 "default permissions for " + resName,
581 actions, effect, true, true, true);
583 ClientResponse<Response> res = null;
585 res = permClient.create(permission);
587 int statusCode = res.getStatus();
588 if (logger.isDebugEnabled()) {
589 logger.debug("createPermission: resName=" + resName
590 + " status = " + statusCode);
592 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
593 invalidStatusCodeMessage(testRequestType, statusCode));
594 Assert.assertEquals(statusCode, testExpectedStatusCode);
598 res.releaseConnection();
607 * @param permId the perm id
609 private void deletePermission(String permId) {
610 if (logger.isDebugEnabled()) {
611 logger.debug(getTestBanner("deletePermission"));
614 PermissionClient permClient = new PermissionClient();
615 ClientResponse<Response> res = null;
617 res = permClient.delete(permId);
618 int statusCode = res.getStatus();
619 if (logger.isDebugEnabled()) {
620 logger.debug("deletePermission: delete permission id="
621 + permId + " status=" + statusCode);
623 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
624 invalidStatusCodeMessage(testRequestType, statusCode));
625 Assert.assertEquals(statusCode, testExpectedStatusCode);
627 res.releaseConnection();
635 * @param roleName the role name
638 private String createRole(String roleName) {
639 if (logger.isDebugEnabled()) {
640 logger.debug(getTestBanner("createRole"));
643 RoleClient roleClient = new RoleClient();
645 Role role = RoleFactory.createRoleInstance(roleName,
646 roleName, //the display name
647 "role for " + roleName, true);
648 ClientResponse<Response> res = null;
651 res = roleClient.create(role);
652 int statusCode = res.getStatus();
653 if (logger.isDebugEnabled()) {
654 logger.debug("createRole: name=" + roleName
655 + " status = " + statusCode);
657 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
658 invalidStatusCodeMessage(testRequestType, statusCode));
659 Assert.assertEquals(statusCode, testExpectedStatusCode);
663 res.releaseConnection();
671 * @param roleId the role id
673 private void deleteRole(String roleId) {
674 if (logger.isDebugEnabled()) {
675 logger.debug(getTestBanner("deleteRole"));
678 RoleClient roleClient = new RoleClient();
679 ClientResponse<Response> res = null;
681 res = roleClient.delete(roleId);
682 int statusCode = res.getStatus();
683 if (logger.isDebugEnabled()) {
684 logger.debug("deleteRole: delete role id=" + roleId
685 + " status=" + statusCode);
687 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
688 invalidStatusCodeMessage(testRequestType, statusCode));
689 Assert.assertEquals(statusCode, testExpectedStatusCode);
691 res.releaseConnection();
696 protected PermissionRole createInstance(String commonPartName,
698 // TODO Auto-generated method stub
703 protected PermissionRole updateInstance(PermissionRole commonPartObject) {
704 // TODO Auto-generated method stub
709 protected void compareUpdatedInstances(PermissionRole original,
710 PermissionRole updated) throws Exception {
711 // TODO Auto-generated method stub
716 protected Class<PermissionRole> getCommonListType() {
717 return PermissionRole.class;
721 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
722 * refer to this method in their @Test annotation declarations.
725 @Test(dataProvider = "testName",
727 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
728 public void CRUDTests(String testName) {
729 // Do nothing. Simply here to for a TestNG execution order for our tests