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;
30 import javax.ws.rs.core.Response;
32 import org.collectionspace.services.authorization.perms.EffectType;
33 import org.collectionspace.services.authorization.perms.Permission;
34 import org.collectionspace.services.authorization.perms.PermissionAction;
35 import org.collectionspace.services.authorization.PermissionRole;
36 import org.collectionspace.services.authorization.PermissionValue;
37 import org.collectionspace.services.authorization.Role;
38 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;
46 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
48 import org.testng.Assert;
49 import org.testng.annotations.Test;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.testng.annotations.AfterClass;
53 import org.testng.annotations.BeforeClass;
56 * PermissionServiceTest, carries out tests against a
57 * deployed and running Permission, Role and PermissionRole Services.
59 * $LastChangedRevision: 917 $
60 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
62 public class PermissionRoleServiceTest extends AbstractServiceTestImpl<PermissionRole, PermissionRole,
63 PermissionRole, PermissionRole> {
65 /** The Constant logger. */
66 private final static String CLASS_NAME = PermissionRoleServiceTest.class.getName();
67 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
69 // Instance variables specific to this test.
70 final private static String TEST_MARKER = "_PermissionRoleServiceTest";
71 final private static String TEST_SERVICE_NAME = "fakeservice";
72 final private static String NO_REL_SUFFIX = "-no-rel";
73 /** The perm values. */
74 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
75 /** The role values. */
76 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
78 * This method is called only by the parent class, AbstractServiceTestImpl
82 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
85 protected String getServicePathComponent() {
86 return new PermissionRoleClient().getServicePathComponent();
90 protected String getServiceName() {
91 return PermissionClient.SERVICE_NAME; //Since we're a sub-resource of permission service return its name?
95 * The entity type expected from the JAX-RS Response object
97 public Class<PermissionRole> getEntityResponseType() {
98 return PermissionRole.class;
104 @BeforeClass(alwaysRun = true)
105 public void seedData() {
106 String ra = TEST_SERVICE_NAME + TEST_MARKER;
107 String accPermId = createPermission(ra, EffectType.PERMIT);
108 PermissionValue pva = new PermissionValue();
109 pva.setResourceName(ra);
110 pva.setPermissionId(accPermId);
111 permValues.put(pva.getResourceName(), pva);
113 String rc = TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX;
114 String coPermId = createPermission(rc, EffectType.DENY);
115 PermissionValue pvc = new PermissionValue();
116 pvc.setResourceName(rc);
117 pvc.setPermissionId(coPermId);
118 permValues.put(pvc.getResourceName(), pvc);
120 // String ri = "intakes";
121 // String iPermId = createPermission(ri, EffectType.DENY);
122 // PermissionValue pvi = new PermissionValue();
123 // pvi.setResourceName(ri);
124 // pvi.setPermissionId(iPermId);
125 // permValues.put(pvi.getResourceName(), pvi);
127 String rn1 = "ROLE_CO1" + TEST_MARKER;
128 String r1RoleId = createRole(rn1);
129 RoleValue rv1 = new RoleValue();
130 rv1.setRoleId(r1RoleId);
131 rv1.setRoleName(rn1);
132 roleValues.put(rv1.getRoleName(), rv1);
134 String rn2 = "ROLE_CO2" + TEST_MARKER;
135 String r2RoleId = createRole(rn2);
136 RoleValue rv2 = new RoleValue();
137 rv2.setRoleId(r2RoleId);
138 rv2.setRoleName(rn2);
139 roleValues.put(rv2.getRoleName(), rv2);
143 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
146 protected CollectionSpaceClient getClientInstance() {
147 return new PermissionRoleClient();
151 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
152 return new PermissionRoleClient(clientPropertiesFilename);
156 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
159 protected PermissionRole getCommonList(Response response) {
160 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
161 throw new UnsupportedOperationException();
165 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
168 public void readPaginatedList(String testName) throws Exception {
169 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
171 // ---------------------------------------------------------------
172 // CRUD tests : CREATE tests
173 // ---------------------------------------------------------------
176 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
180 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
181 public void create(String testName) throws Exception {
182 // Perform setup, such as initializing the type of service request
183 // (e.g. CREATE, DELETE), its valid and expected status codes, and
184 // its associated HTTP method name (e.g. POST, DELETE).
187 // Submit the request to the service and store the response.
188 PermissionValue pv = permValues.get(TEST_SERVICE_NAME + TEST_MARKER);
189 PermissionRole permRole = createPermissionRoleInstance(pv,
190 roleValues.values(), true, true);
191 PermissionRoleClient client = new PermissionRoleClient();
193 Response res = client.create(pv.getPermissionId(), permRole);
195 int statusCode = res.getStatus();
197 if (logger.isDebugEnabled()) {
198 logger.debug(testName + ": status = " + statusCode);
200 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
201 invalidStatusCodeMessage(testRequestType, statusCode));
202 Assert.assertEquals(statusCode, testExpectedStatusCode);
204 knownResourceId = extractId(res); //This is meaningless in this test, see getKnowResourceId() method for details
205 if (logger.isDebugEnabled()) {
206 logger.debug(testName + ": knownResourceId=" + knownResourceId);
215 //to not cause uniqueness violation for permRole, createList is removed
217 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
220 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
221 // dependsOnMethods = {"create"})
222 public void createList(String testName) throws Exception {
223 //Should this really be empty?
227 // Placeholders until the three tests below can be uncommented.
228 // See Issue CSPACE-401.
230 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
233 public void createWithEmptyEntityBody(String testName) throws Exception {
234 //Should this really be empty?
238 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
241 public void createWithMalformedXml(String testName) throws Exception {
242 //Should this really be empty?
246 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
249 public void createWithWrongXmlSchema(String testName) throws Exception {
250 //Should this really be empty?
253 // ---------------------------------------------------------------
254 // CRUD tests : READ tests
255 // ---------------------------------------------------------------
258 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
261 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
262 // dependsOnMethods = {"create"})
263 public void read(String testName) throws Exception {
267 // Submit the request to the service and store the response.
268 PermissionRoleClient client = new PermissionRoleClient();
269 Response res = client.read(
270 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
272 int statusCode = res.getStatus();
274 // Check the status code of the response: does it match
275 // the expected response(s)?
276 if (logger.isDebugEnabled()) {
277 logger.debug(testName + ": status = " + statusCode);
279 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
280 invalidStatusCodeMessage(testRequestType, statusCode));
281 Assert.assertEquals(statusCode, testExpectedStatusCode);
283 PermissionRole output = res.readEntity(PermissionRole.class);
284 Assert.assertNotNull(output);
295 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
298 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
299 public void readNonExistent(String testName) throws Exception {
301 setupReadNonExistent();
303 // Submit the request to the service and store the response.
304 PermissionRoleClient client = new PermissionRoleClient();
307 res = client.read(NON_EXISTENT_ID);
308 int statusCode = res.getStatus();
310 // Check the status code of the response: does it match
311 // the expected response(s)?
312 if (logger.isDebugEnabled()) {
313 logger.debug(testName + ": status = " + statusCode);
315 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
316 invalidStatusCodeMessage(testRequestType, statusCode));
317 Assert.assertEquals(statusCode, testExpectedStatusCode);
325 @Test(dataProvider = "testName",
326 dependsOnMethods = {"CRUDTests"})
327 public void readNoRelationship(String testName) throws Exception {
331 // Submit the request to the service and store the response.
332 PermissionRoleClient client = new PermissionRoleClient();
336 permValues.get(TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX).getPermissionId());
337 // Check the status code of the response: does it match
338 // the expected response(s)?
339 assertStatusCode(res, testName);
340 PermissionRole output = res.readEntity(PermissionRole.class);
342 String sOutput = objectAsXmlString(output, PermissionRole.class);
343 if (logger.isDebugEnabled()) {
344 logger.debug(testName + " received " + sOutput);
353 // ---------------------------------------------------------------
354 // CRUD tests : READ_LIST tests
355 // ---------------------------------------------------------------
358 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
362 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
363 // dependsOnMethods = {"createList", "read"})
364 public void readList(String testName) throws Exception {
365 //Should this really be empty?
370 // ---------------------------------------------------------------
371 // CRUD tests : UPDATE tests
372 // ---------------------------------------------------------------
375 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
378 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
379 // dependsOnMethods = {"read", "readList", "readNonExistent"})
380 public void update(String testName) throws Exception {
381 //Should this really be empty?
385 // Placeholders until the three tests below can be uncommented.
386 // See Issue CSPACE-401.
388 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
391 public void updateWithEmptyEntityBody(String testName) throws Exception {
392 //Should this really be empty?
396 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
399 public void updateWithMalformedXml(String testName) throws Exception {
400 //Should this really be empty?
404 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
407 public void updateWithWrongXmlSchema(String testName) throws Exception {
408 //Should this really be empty?
412 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
415 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
416 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
417 public void updateNonExistent(String testName) throws Exception {
418 //Should this really be empty?
421 // ---------------------------------------------------------------
422 // CRUD tests : DELETE tests
423 // ---------------------------------------------------------------
426 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
429 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
430 // dependsOnMethods = {"read"})
431 public void delete(String testName) throws Exception {
438 PermissionRoleClient client = new PermissionRoleClient();
439 Response readResponse = client.read(
440 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
441 PermissionRole toDelete = null;
443 toDelete = readResponse.readEntity(PermissionRole.class);
445 readResponse.close();
448 Response res = client.delete(
449 toDelete.getPermission().get(0).getPermissionId(), toDelete);
451 int statusCode = res.getStatus();
452 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
453 invalidStatusCodeMessage(testRequestType, statusCode));
454 Assert.assertEquals(statusCode, testExpectedStatusCode);
462 // recreate 'TEST_SERVICE_NAME + TEST_MARKER' account and roles
468 // Lookup a know permission, and delete all of its role relationships
470 readResponse = client.read(
471 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
474 toDelete = readResponse.readEntity(PermissionRole.class);
476 readResponse.close();
479 Response deleteRes = client.delete(toDelete.getPermission().get(0).getPermissionId());
481 int statusCode = deleteRes.getStatus();
482 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
483 invalidStatusCodeMessage(testRequestType, statusCode));
484 Assert.assertEquals(statusCode, testExpectedStatusCode);
493 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
496 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
497 public void deleteNonExistent(String testName) throws Exception {
498 //ignoring this test as the service side returns 200 now even if it does
499 //not find a record in the db
502 // ---------------------------------------------------------------
504 // ---------------------------------------------------------------
507 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
508 public void searchWorkflowDeleted(String testName) throws Exception {
509 // Fixme: null test for now, overriding test in base class
513 protected String getKnowResourceId() {
514 return permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId();
517 // ---------------------------------------------------------------
518 // Utility methods used by tests above
519 // ---------------------------------------------------------------
521 * create permRolerole instance
522 * @param pv permissionvalue
523 * @param rvs rolevalue array
526 * @return PermissionRole
528 public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
529 Collection<RoleValue> rvs,
533 List<RoleValue> rvls = new ArrayList<RoleValue>();
535 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
536 pv, rvls, usePermId, useRoleId);
537 if (logger.isDebugEnabled()) {
538 logger.debug("to be created, permRole");
539 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
547 @AfterClass(alwaysRun = true)
549 public void cleanUp() {
551 String noTest = System.getProperty("noTestCleanup");
552 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
553 if (logger.isDebugEnabled()) {
554 logger.debug("Skipping Cleanup phase ...");
558 if (logger.isDebugEnabled()) {
559 logger.debug("Cleaning up temporary resources created for testing ...");
562 for (PermissionValue pv : permValues.values()) {
563 deletePermission(pv.getPermissionId());
565 for (RoleValue rv : roleValues.values()) {
566 deleteRole(rv.getRoleId());
571 * Creates the permission.
573 * @param resName the res name
574 * @param effect the effect
577 private String createPermission(String resName, EffectType effect) {
578 if (logger.isDebugEnabled()) {
579 logger.debug(getTestBanner("createPermission"));
582 PermissionClient permClient = new PermissionClient();
583 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
584 Permission permission = PermissionFactory.createPermissionInstance(resName,
585 "default permissions for " + resName,
586 actions, effect, true, true, true);
588 Response res = permClient.create(permission);
590 int statusCode = res.getStatus();
591 if (logger.isDebugEnabled()) {
592 logger.debug("createPermission: resName=" + resName
593 + " status = " + statusCode);
595 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
596 invalidStatusCodeMessage(testRequestType, statusCode));
597 Assert.assertEquals(statusCode, testExpectedStatusCode);
610 * @param permId the perm id
612 private void deletePermission(String permId) {
613 if (logger.isDebugEnabled()) {
614 logger.debug(getTestBanner("deletePermission"));
617 PermissionClient permClient = new PermissionClient();
618 Response res = permClient.delete(permId);
620 int statusCode = res.getStatus();
621 if (logger.isDebugEnabled()) {
622 logger.debug("deletePermission: delete permission id="
623 + permId + " status=" + statusCode);
625 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
626 invalidStatusCodeMessage(testRequestType, statusCode));
627 Assert.assertEquals(statusCode, testExpectedStatusCode);
637 * @param roleName the role name
640 private String createRole(String roleName) {
641 if (logger.isDebugEnabled()) {
642 logger.debug(getTestBanner("createRole"));
645 RoleClient roleClient = new RoleClient();
647 Role role = RoleFactory.createRoleInstance(roleName,
648 roleName, //the display name
649 "role for " + roleName, true);
653 res = roleClient.create(role);
654 int statusCode = res.getStatus();
655 if (logger.isDebugEnabled()) {
656 logger.debug("createRole: name=" + roleName
657 + " status = " + statusCode);
659 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
660 invalidStatusCodeMessage(testRequestType, statusCode));
661 Assert.assertEquals(statusCode, testExpectedStatusCode);
673 * @param roleId the role id
675 private void deleteRole(String roleId) {
676 if (logger.isDebugEnabled()) {
677 logger.debug(getTestBanner("deleteRole"));
680 RoleClient roleClient = new RoleClient();
681 Response res = roleClient.delete(roleId);
683 int statusCode = res.getStatus();
684 if (logger.isDebugEnabled()) {
685 logger.debug("deleteRole: delete role id=" + roleId
686 + " status=" + statusCode);
688 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
689 invalidStatusCodeMessage(testRequestType, statusCode));
690 Assert.assertEquals(statusCode, testExpectedStatusCode);
697 protected PermissionRole createInstance(String commonPartName,
699 // TODO Auto-generated method stub
704 protected PermissionRole updateInstance(PermissionRole commonPartObject) {
705 // TODO Auto-generated method stub
710 protected void compareUpdatedInstances(PermissionRole original,
711 PermissionRole updated) throws Exception {
712 // TODO Auto-generated method stub
717 protected Class<PermissionRole> getCommonListType() {
718 return PermissionRole.class;
722 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
723 * refer to this method in their @Test annotation declarations.
726 @Test(dataProvider = "testName",
728 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
729 public void CRUDTests(String testName) {
730 // Do nothing. Simply here to for a TestNG execution order for our tests
734 protected long getSizeOfList(PermissionRole list) {
735 // TODO Auto-generated method stub
736 throw new UnsupportedOperationException("Method getSizeOfList() is not implemented because this service does not support lists.");