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.Date;
28 import java.util.Hashtable;
29 import java.util.List;
31 import javax.ws.rs.core.Response;
33 import org.collectionspace.services.authorization.perms.EffectType;
34 import org.collectionspace.services.authorization.perms.Permission;
35 import org.collectionspace.services.authorization.perms.PermissionAction;
36 import org.collectionspace.services.authorization.PermissionRole;
37 import org.collectionspace.services.authorization.PermissionValue;
38 import org.collectionspace.services.authorization.Role;
39 import org.collectionspace.services.authorization.RoleValue;
40 import org.collectionspace.services.client.CollectionSpaceClient;
41 import org.collectionspace.services.client.PermissionClient;
42 import org.collectionspace.services.client.PermissionFactory;
43 import org.collectionspace.services.client.RolePermissionClient;
44 import org.collectionspace.services.client.PermissionRoleFactory;
45 import org.collectionspace.services.client.RoleClient;
46 import org.collectionspace.services.client.RoleFactory;
47 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
48 import org.collectionspace.services.client.test.ServiceRequestType;
49 import org.testng.Assert;
50 import org.testng.annotations.Test;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.testng.annotations.AfterClass;
54 import org.testng.annotations.BeforeClass;
57 * PermissionServiceTest, carries out tests against a
58 * deployed and running Permission, Role and PermissionRole Services.
60 * $LastChangedRevision: 917 $
61 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
63 public class RolePermissionServiceTest extends AbstractServiceTestImpl<PermissionRole, PermissionRole,
64 PermissionRole, PermissionRole> {
66 /** The Constant logger. */
67 private final static String CLASS_NAME = RolePermissionServiceTest.class.getName();
68 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
69 // Instance variables specific to this test.
70 final private static String TEST_MARKER = "_RolePermissionServiceTest";
71 final private static String TEST_ROLE_NAME = "ROLE";
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>();
77 private Date now = new Date();
80 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
83 protected String getServicePathComponent() throws Exception {
84 return new RolePermissionClient().getServicePathComponent();
88 public String getServiceName() {
89 return RoleClient.SERVICE_NAME; // RolePermission is a sub-resource of 'roles' resource.
92 private String getRoleName() {
93 return TEST_ROLE_NAME + TEST_MARKER + now.toString();
97 * The entity type expected from the JAX-RS Response object
99 public Class<PermissionRole> getEntityResponseType() {
100 return PermissionRole.class;
107 @BeforeClass(alwaysRun = true)
108 public void seedData() throws Exception {
109 String rn1 = getRoleName();
110 String r1RoleId = createRole(rn1);
111 RoleValue rv1 = new RoleValue();
112 rv1.setRoleId(r1RoleId);
113 rv1.setRoleName(rn1);
114 roleValues.put(rv1.getRoleName(), rv1);
116 String rn2 = getRoleName() + NO_REL_SUFFIX;
117 String r2RoleId = createRole(rn2);
118 RoleValue rv2 = new RoleValue();
119 rv2.setRoleId(r2RoleId);
120 rv2.setRoleName(rn2);
121 roleValues.put(rv2.getRoleName(), rv2);
123 String permId1 = createPermission("fooService" + TEST_MARKER);
124 Permission persistedPerm = readPermission(permId1);
125 PermissionValue pva1 = new PermissionValue();
126 pva1.setResourceName(persistedPerm.getResourceName());
127 pva1.setActionGroup(persistedPerm.getActionGroup());
128 pva1.setTenantId(persistedPerm.getTenantId());
129 pva1.setPermissionId(permId1);
130 permValues.put(pva1.getResourceName(), pva1);
132 String permId2 = createPermission("barService" + TEST_MARKER);
133 persistedPerm = readPermission(permId2);
134 PermissionValue pva2 = new PermissionValue();
135 pva2.setResourceName(persistedPerm.getResourceName());
136 pva2.setActionGroup(persistedPerm.getActionGroup());
137 pva2.setTenantId(persistedPerm.getTenantId());
138 pva2.setPermissionId(permId2);
139 permValues.put(pva2.getResourceName(), pva2);
143 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
146 protected CollectionSpaceClient getClientInstance() throws Exception {
147 return new RolePermissionClient();
151 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
152 return new RolePermissionClient(clientPropertiesFilename);
156 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
158 // @Test(dataProvider = "testName")
160 public void readPaginatedList(String testName) throws Exception {
161 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
164 // ---------------------------------------------------------------
165 // CRUD tests : CREATE tests
166 // ---------------------------------------------------------------
169 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
173 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
174 public void create(String testName) throws Exception {
175 // Perform setup, such as initializing the type of service request
176 // (e.g. CREATE, DELETE), its valid and expected status codes, and
177 // its associated HTTP method name (e.g. POST, DELETE).
180 // Submit the request to the service and store the response.
181 RoleValue rv = roleValues.get(getRoleName());
182 PermissionRole permRole = createPermissionRoleInstance(rv,
183 permValues.values(), true, true);
184 RolePermissionClient client = new RolePermissionClient();
185 Response res = client.create(rv.getRoleId(), permRole);
187 int statusCode = res.getStatus();
188 if (logger.isDebugEnabled()) {
189 logger.debug(testName + ": status = " + statusCode);
191 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
192 invalidStatusCodeMessage(testRequestType, statusCode));
193 Assert.assertEquals(statusCode, testExpectedStatusCode);
195 knownResourceId = extractId(res); //This is meaningless in this test, see getKnowResourceId() method for details
196 if (logger.isDebugEnabled()) {
197 logger.debug(testName + ": knownResourceId=" + knownResourceId);
206 //to not cause uniqueness violation for permRole, createList is removed
208 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
211 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
212 // dependsOnMethods = {"create"})
213 public void createList(String testName) throws Exception {
214 //Should this really be empty?
218 // Placeholders until the three tests below can be uncommented.
219 // See Issue CSPACE-401.
221 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
224 public void createWithEmptyEntityBody(String testName) throws Exception {
225 //Should this really be empty?
229 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
232 public void createWithMalformedXml(String testName) throws Exception {
233 //Should this really be empty?
237 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
240 public void createWithWrongXmlSchema(String testName) throws Exception {
241 //Should this really be empty?
244 // ---------------------------------------------------------------
245 // CRUD tests : READ tests
246 // ---------------------------------------------------------------
249 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
252 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
253 // dependsOnMethods = {"create"})
254 public void read(String testName) throws Exception {
258 // Submit the request to the service and store the response.
259 RolePermissionClient client = new RolePermissionClient();
260 Response res = client.read(roleValues.get(getRoleName()).getRoleId());
262 int statusCode = res.getStatus();
264 // Check the status code of the response: does it match
265 // the expected response(s)?
266 if (logger.isDebugEnabled()) {
267 logger.debug(testName + ": status = " + statusCode);
269 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
270 invalidStatusCodeMessage(testRequestType, statusCode));
271 Assert.assertEquals(statusCode, testExpectedStatusCode);
273 PermissionRole output = res.readEntity(PermissionRole.class);
274 Assert.assertNotNull(output);
284 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
287 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
288 public void readNonExistent(String testName) throws Exception {
290 setupReadNonExistent();
292 // Submit the request to the service and store the response.
293 RolePermissionClient client = new RolePermissionClient();
294 Response res = client.read(NON_EXISTENT_ID);
296 int statusCode = res.getStatus();
298 // Check the status code of the response: does it match
299 // the expected response(s)?
300 if (logger.isDebugEnabled()) {
301 logger.debug(testName + ": status = " + statusCode);
303 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
304 invalidStatusCodeMessage(testRequestType, statusCode));
305 Assert.assertEquals(statusCode, testExpectedStatusCode);
313 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
314 public void readNoRelationship(String testName) throws Exception {
316 // Submit the request to the service and store the response.
317 RolePermissionClient client = new RolePermissionClient();
318 Response res = client.read(roleValues.get(getRoleName() + NO_REL_SUFFIX).getRoleId());
320 int statusCode = res.getStatus();
322 // Check the status code of the response: does it match
323 // the expected response(s)?
324 if (logger.isDebugEnabled()) {
325 logger.debug(testName + ": status = " + statusCode);
327 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
328 invalidStatusCodeMessage(testRequestType, statusCode));
329 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
330 PermissionRole output = res.readEntity(PermissionRole.class);
332 String sOutput = objectAsXmlString(output, PermissionRole.class);
333 if (logger.isDebugEnabled()) {
334 logger.debug(testName + " received " + sOutput);
343 // ---------------------------------------------------------------
344 // CRUD tests : READ_LIST tests
345 // ---------------------------------------------------------------
348 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
351 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
352 // dependsOnMethods = {"createList", "read"})
353 public void readList(String testName) throws Exception {
354 //Should this really be empty?
359 // ---------------------------------------------------------------
360 // CRUD tests : UPDATE tests
361 // ---------------------------------------------------------------
364 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
367 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
368 // dependsOnMethods = {"read", "readList", "readNonExistent"})
369 public void update(String testName) throws Exception {
370 //Should this really be empty?
374 // Placeholders until the three tests below can be uncommented.
375 // See Issue CSPACE-401.
377 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
380 public void updateWithEmptyEntityBody(String testName) throws Exception {
381 //Should this really be empty?
385 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
388 public void updateWithMalformedXml(String testName) throws Exception {
389 //Should this really be empty?
393 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
396 public void updateWithWrongXmlSchema(String testName) throws Exception {
397 //Should this really be empty?
401 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
404 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
405 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
406 public void updateNonExistent(String testName) throws Exception {
407 //Should this really be empty?
410 // ---------------------------------------------------------------
411 // CRUD tests : DELETE tests
412 // ---------------------------------------------------------------
415 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
418 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
419 // dependsOnMethods = {"read"})
420 public void delete(String testName) throws Exception {
424 // Submit the request to the service and store the response.
425 RolePermissionClient client = new RolePermissionClient();
426 RoleValue rv = roleValues.get(getRoleName());
427 Response delRes = client.delete(rv.getRoleId());
429 int statusCode = delRes.getStatus();
430 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
431 invalidStatusCodeMessage(testRequestType, statusCode));
432 Assert.assertEquals(statusCode, testExpectedStatusCode);
434 if (delRes != null) {
439 // reset for next delete
443 rv = roleValues.get(getRoleName());
444 Response readResponse = client.read(rv.getRoleId());
445 PermissionRole toDelete = null;
447 toDelete = readResponse.readEntity(PermissionRole.class);
449 readResponse.close();
452 rv = toDelete.getRole().get(0);
453 Response res = client.delete(rv.getRoleId(), toDelete);
455 int statusCode = res.getStatus();
456 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
457 invalidStatusCodeMessage(testRequestType, statusCode));
458 Assert.assertEquals(statusCode, testExpectedStatusCode);
468 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
471 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
472 public void deleteNonExistent(String testName) throws Exception {
473 //ignoring this test as the service side returns 200 now even if it does
474 //not find a record in the db
477 // ---------------------------------------------------------------
479 // ---------------------------------------------------------------
482 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
483 public void searchWorkflowDeleted(String testName) throws Exception {
484 // Fixme: null test for now, overriding test in base class
487 // ---------------------------------------------------------------
488 // Utility tests : tests of code used in tests above
489 // ---------------------------------------------------------------
491 * Tests the code for manually submitting data that is used by several
492 * of the methods above.
495 // @Test(dependsOnMethods = {"create"}) //FIXME: REM - This is not a test of a submit to the permroles service, but to just authorization/roles
496 public void testSubmitRequest() {
498 // Expected status code: 200 OK
499 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
501 // Submit the request to the service and store the response.
502 String method = ServiceRequestType.READ.httpMethodName();
503 String url = getResourceURL(roleValues.get(getRoleName()).getRoleId());
504 int statusCode = submitRequest(method, url);
506 // Check the status code of the response: does it match
507 // the expected response(s)?
508 if (logger.isDebugEnabled()) {
509 logger.debug("testSubmitRequest: url=" + url
510 + " status=" + statusCode);
512 Assert.assertEquals(statusCode, EXPECTED_STATUS);
515 // ---------------------------------------------------------------
516 // Utility methods used by tests above
517 // ---------------------------------------------------------------
519 * create PermissionRole instance
520 * @param rv rolevalue
521 * @param pvs permission value array
524 * @return PermissionRole
526 public static PermissionRole createPermissionRoleInstance(RoleValue rv,
527 Collection<PermissionValue> pvs,
530 List<PermissionValue> pvls = new ArrayList<PermissionValue>();
532 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
533 rv, pvls, usePermId, useRoleId);
534 if (logger.isDebugEnabled()) {
537 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
546 @AfterClass(alwaysRun = true)
548 public void cleanUp() throws Exception {
550 String noTest = System.getProperty("noTestCleanup");
551 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
552 if (logger.isDebugEnabled()) {
553 logger.debug("Skipping Cleanup phase ...");
557 if (logger.isDebugEnabled()) {
558 logger.debug("Cleaning up temporary resources created for testing ...");
560 for (PermissionValue pv : permValues.values()) {
561 deletePermission(pv.getPermissionId());
563 for (RoleValue rv : roleValues.values()) {
564 deleteRole(rv.getRoleId());
569 * Creates the permission.
571 * @param resName the res name
572 * @param effect the effect
576 private String createPermission(String resName) throws Exception {
578 PermissionClient permClient = new PermissionClient();
579 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
580 Permission permission = PermissionFactory.createPermissionInstance(resName,
581 "default permissions for " + resName,
582 actions, EffectType.PERMIT, true, true, true);
586 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);
604 private Permission readPermission(String csid) throws Exception {
605 Permission result = null;
608 PermissionClient permClient = new PermissionClient();
612 res = permClient.read(csid);
613 int statusCode = res.getStatus();
614 if (logger.isDebugEnabled()) {
615 logger.debug("readPermission: csid=" + csid
616 + " status = " + statusCode);
618 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
619 invalidStatusCodeMessage(testRequestType, statusCode));
620 Assert.assertEquals(statusCode, testExpectedStatusCode);
621 result = (Permission) res.readEntity(Permission.class);
633 * @param permId the perm id
636 private void deletePermission(String permId) throws Exception {
638 PermissionClient permClient = new PermissionClient();
640 Response res = permClient.delete(permId);
642 int statusCode = res.getStatus();
643 if (logger.isDebugEnabled()) {
644 logger.debug("deletePermission: delete permission id="
645 + permId + " status=" + statusCode);
647 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
648 invalidStatusCodeMessage(testRequestType, statusCode));
649 Assert.assertEquals(statusCode, testExpectedStatusCode);
661 * @param roleName the role name
665 private String createRole(String roleName) throws Exception {
667 RoleClient roleClient = new RoleClient();
669 Role role = RoleFactory.createRoleInstance(roleName,
670 roleName, //the display name
671 "role for " + roleName, true, RoleFactory.EMPTY_PERMVALUE_LIST);
672 role.setRoleGroup("something");
676 res = roleClient.create(role);
677 int statusCode = res.getStatus();
678 if (logger.isDebugEnabled()) {
679 logger.debug("createRole: name=" + roleName
680 + " status = " + statusCode);
682 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
683 invalidStatusCodeMessage(testRequestType, statusCode));
684 Assert.assertEquals(statusCode, testExpectedStatusCode);
697 * @param roleId the role id
700 private void deleteRole(String roleId) throws Exception {
702 RoleClient roleClient = new RoleClient();
705 res = roleClient.delete(roleId);
706 int statusCode = res.getStatus();
707 if (logger.isDebugEnabled()) {
708 logger.debug("deleteRole: delete role id=" + roleId
709 + " status=" + statusCode);
711 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
712 invalidStatusCodeMessage(testRequestType, statusCode));
713 Assert.assertEquals(statusCode, testExpectedStatusCode);
721 protected PermissionRole createInstance(String commonPartName,
723 // TODO Auto-generated method stub
728 protected PermissionRole updateInstance(PermissionRole commonPartObject) {
729 // TODO Auto-generated method stub
734 protected void compareUpdatedInstances(PermissionRole original,
735 PermissionRole updated) throws Exception {
736 // TODO Auto-generated method stub
741 protected Class<PermissionRole> getCommonListType() {
742 // TODO Auto-generated method stub
747 protected String getKnowResourceId() {
748 return roleValues.get(getRoleName()).getRoleId();
752 protected long getSizeOfList(PermissionRole permRole) {
753 // TODO Auto-generated method stub
754 throw new UnsupportedOperationException("Method getSizeOfList() is not implemented because this service does not support lists.");
758 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
759 * refer to this method in their @Test annotation declarations.
762 @Test(dataProvider = "testName",
764 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
765 public void CRUDTests(String testName) {
766 // Do nothing. Simply here to for a TestNG execution order for our tests