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;
47 import org.testng.Assert;
48 import org.testng.annotations.Test;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.testng.annotations.AfterClass;
52 import org.testng.annotations.BeforeClass;
55 * PermissionServiceTest, carries out tests against a
56 * deployed and running Permission, Role and PermissionRole Services.
58 * $LastChangedRevision: 917 $
59 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
61 public class PermissionRoleServiceTest extends AbstractServiceTestImpl<PermissionRole, PermissionRole,
62 PermissionRole, PermissionRole> {
64 /** The Constant logger. */
65 private final static String CLASS_NAME = PermissionRoleServiceTest.class.getName();
66 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
68 // Instance variables specific to this test.
69 final private static String TEST_MARKER = "_PermissionRoleServiceTest";
70 final private static String TEST_SERVICE_NAME = "fakeservice";
71 final private static String NO_REL_SUFFIX = "-no-rel";
72 /** The perm values. */
73 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
74 /** The role values. */
75 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
77 * This method is called only by the parent class, AbstractServiceTestImpl
81 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
84 protected String getServicePathComponent() throws Exception {
85 return new PermissionRoleClient().getServicePathComponent();
89 protected String getServiceName() {
90 return PermissionClient.SERVICE_NAME; //Since we're a sub-resource of permission service return its name?
94 * The entity type expected from the JAX-RS Response object
96 public Class<PermissionRole> getEntityResponseType() {
97 return PermissionRole.class;
104 @BeforeClass(alwaysRun = true)
105 public void seedData() throws Exception {
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() throws Exception {
147 return new PermissionRoleClient();
151 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
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));
548 @AfterClass(alwaysRun = true)
550 public void cleanUp() throws Exception {
552 String noTest = System.getProperty("noTestCleanup");
553 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
554 if (logger.isDebugEnabled()) {
555 logger.debug("Skipping Cleanup phase ...");
559 if (logger.isDebugEnabled()) {
560 logger.debug("Cleaning up temporary resources created for testing ...");
563 for (PermissionValue pv : permValues.values()) {
564 deletePermission(pv.getPermissionId());
566 for (RoleValue rv : roleValues.values()) {
567 deleteRole(rv.getRoleId());
572 * Creates the permission.
574 * @param resName the res name
575 * @param effect the effect
579 private String createPermission(String resName, EffectType effect) throws Exception {
580 if (logger.isDebugEnabled()) {
581 logger.debug(getTestBanner("createPermission"));
584 PermissionClient permClient = new PermissionClient();
585 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
586 Permission permission = PermissionFactory.createPermissionInstance(resName,
587 "default permissions for " + resName,
588 actions, effect, true, true, true);
590 Response res = permClient.create(permission);
592 int statusCode = res.getStatus();
593 if (logger.isDebugEnabled()) {
594 logger.debug("createPermission: resName=" + resName
595 + " status = " + statusCode);
597 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
598 invalidStatusCodeMessage(testRequestType, statusCode));
599 Assert.assertEquals(statusCode, testExpectedStatusCode);
612 * @param permId the perm id
615 private void deletePermission(String permId) throws Exception {
616 if (logger.isDebugEnabled()) {
617 logger.debug(getTestBanner("deletePermission"));
620 PermissionClient permClient = new PermissionClient();
621 Response res = permClient.delete(permId);
623 int statusCode = res.getStatus();
624 if (logger.isDebugEnabled()) {
625 logger.debug("deletePermission: delete permission id="
626 + permId + " status=" + statusCode);
628 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
629 invalidStatusCodeMessage(testRequestType, statusCode));
630 Assert.assertEquals(statusCode, testExpectedStatusCode);
640 * @param roleName the role name
644 private String createRole(String roleName) throws Exception {
645 if (logger.isDebugEnabled()) {
646 logger.debug(getTestBanner("createRole"));
649 RoleClient roleClient = new RoleClient();
651 Role role = RoleFactory.createRoleInstance(roleName,
652 roleName, //the display name
653 "role for " + roleName, true, RoleFactory.EMPTY_PERMVALUE_LIST);
657 res = roleClient.create(role);
658 int statusCode = res.getStatus();
659 if (logger.isDebugEnabled()) {
660 logger.debug("createRole: name=" + roleName
661 + " status = " + statusCode);
663 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
664 invalidStatusCodeMessage(testRequestType, statusCode));
665 Assert.assertEquals(statusCode, testExpectedStatusCode);
677 * @param roleId the role id
680 private void deleteRole(String roleId) throws Exception {
681 if (logger.isDebugEnabled()) {
682 logger.debug(getTestBanner("deleteRole"));
685 RoleClient roleClient = new RoleClient();
686 Response res = roleClient.delete(roleId);
688 int statusCode = res.getStatus();
689 if (logger.isDebugEnabled()) {
690 logger.debug("deleteRole: delete role id=" + roleId
691 + " status=" + statusCode);
693 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
694 invalidStatusCodeMessage(testRequestType, statusCode));
695 Assert.assertEquals(statusCode, testExpectedStatusCode);
702 protected PermissionRole createInstance(String commonPartName,
704 // TODO Auto-generated method stub
709 protected PermissionRole updateInstance(PermissionRole commonPartObject) {
710 // TODO Auto-generated method stub
715 protected void compareUpdatedInstances(PermissionRole original,
716 PermissionRole updated) throws Exception {
717 // TODO Auto-generated method stub
722 protected Class<PermissionRole> getCommonListType() {
723 return PermissionRole.class;
727 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
728 * refer to this method in their @Test annotation declarations.
731 @Test(dataProvider = "testName",
733 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
734 public void CRUDTests(String testName) {
735 // Do nothing. Simply here to for a TestNG execution order for our tests
739 protected long getSizeOfList(PermissionRole list) {
740 // TODO Auto-generated method stub
741 throw new UnsupportedOperationException("Method getSizeOfList() is not implemented because this service does not support lists.");