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 {
110 String rn1 = getRoleName();
111 String r1RoleId = createRole(rn1);
112 RoleValue rv1 = new RoleValue();
113 rv1.setRoleId(r1RoleId);
114 rv1.setRoleName(rn1);
115 roleValues.put(rv1.getRoleName(), rv1);
117 String rn2 = getRoleName() + NO_REL_SUFFIX;
118 String r2RoleId = createRole(rn2);
119 RoleValue rv2 = new RoleValue();
120 rv2.setRoleId(r2RoleId);
121 rv2.setRoleName(rn2);
122 roleValues.put(rv2.getRoleName(), rv2);
124 String ra1 = "fooService" + TEST_MARKER;
125 String permId1 = createPermission(ra1, EffectType.PERMIT);
126 PermissionValue pva1 = new PermissionValue();
127 pva1.setResourceName(ra1);
128 pva1.setPermissionId(permId1);
129 permValues.put(pva1.getResourceName(), pva1);
131 String ra2 = "barService" + TEST_MARKER;
132 String permId2 = createPermission(ra1, EffectType.PERMIT);
133 PermissionValue pva2 = new PermissionValue();
134 pva2.setResourceName(ra2);
135 pva2.setPermissionId(permId2);
136 permValues.put(pva2.getResourceName(), pva2);
140 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
143 protected CollectionSpaceClient getClientInstance() throws Exception {
144 return new RolePermissionClient();
148 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
149 return new RolePermissionClient(clientPropertiesFilename);
153 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
155 // @Test(dataProvider = "testName")
157 public void readPaginatedList(String testName) throws Exception {
158 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
161 // ---------------------------------------------------------------
162 // CRUD tests : CREATE tests
163 // ---------------------------------------------------------------
166 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
170 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
171 public void create(String testName) throws Exception {
172 // Perform setup, such as initializing the type of service request
173 // (e.g. CREATE, DELETE), its valid and expected status codes, and
174 // its associated HTTP method name (e.g. POST, DELETE).
177 // Submit the request to the service and store the response.
178 RoleValue rv = roleValues.get(getRoleName());
179 PermissionRole permRole = createPermissionRoleInstance(rv,
180 permValues.values(), true, true);
181 RolePermissionClient client = new RolePermissionClient();
182 Response res = client.create(rv.getRoleId(), permRole);
184 int statusCode = res.getStatus();
185 if (logger.isDebugEnabled()) {
186 logger.debug(testName + ": status = " + statusCode);
188 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
189 invalidStatusCodeMessage(testRequestType, statusCode));
190 Assert.assertEquals(statusCode, testExpectedStatusCode);
192 knownResourceId = extractId(res); //This is meaningless in this test, see getKnowResourceId() method for details
193 if (logger.isDebugEnabled()) {
194 logger.debug(testName + ": knownResourceId=" + knownResourceId);
203 //to not cause uniqueness violation for permRole, createList is removed
205 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
208 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
209 // dependsOnMethods = {"create"})
210 public void createList(String testName) throws Exception {
211 //Should this really be empty?
215 // Placeholders until the three tests below can be uncommented.
216 // See Issue CSPACE-401.
218 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
221 public void createWithEmptyEntityBody(String testName) throws Exception {
222 //Should this really be empty?
226 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
229 public void createWithMalformedXml(String testName) throws Exception {
230 //Should this really be empty?
234 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
237 public void createWithWrongXmlSchema(String testName) throws Exception {
238 //Should this really be empty?
241 // ---------------------------------------------------------------
242 // CRUD tests : READ tests
243 // ---------------------------------------------------------------
246 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
249 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
250 // dependsOnMethods = {"create"})
251 public void read(String testName) throws Exception {
255 // Submit the request to the service and store the response.
256 RolePermissionClient client = new RolePermissionClient();
257 Response res = client.read(roleValues.get(getRoleName()).getRoleId());
259 int statusCode = res.getStatus();
261 // Check the status code of the response: does it match
262 // the expected response(s)?
263 if (logger.isDebugEnabled()) {
264 logger.debug(testName + ": status = " + statusCode);
266 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
267 invalidStatusCodeMessage(testRequestType, statusCode));
268 Assert.assertEquals(statusCode, testExpectedStatusCode);
270 PermissionRole output = res.readEntity(PermissionRole.class);
271 Assert.assertNotNull(output);
281 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
284 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
285 public void readNonExistent(String testName) throws Exception {
287 setupReadNonExistent();
289 // Submit the request to the service and store the response.
290 RolePermissionClient client = new RolePermissionClient();
291 Response res = client.read(NON_EXISTENT_ID);
293 int statusCode = res.getStatus();
295 // Check the status code of the response: does it match
296 // the expected response(s)?
297 if (logger.isDebugEnabled()) {
298 logger.debug(testName + ": status = " + statusCode);
300 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
301 invalidStatusCodeMessage(testRequestType, statusCode));
302 Assert.assertEquals(statusCode, testExpectedStatusCode);
310 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
311 public void readNoRelationship(String testName) throws Exception {
313 // Submit the request to the service and store the response.
314 RolePermissionClient client = new RolePermissionClient();
315 Response res = client.read(roleValues.get(getRoleName() + NO_REL_SUFFIX).getRoleId());
317 int statusCode = res.getStatus();
319 // Check the status code of the response: does it match
320 // the expected response(s)?
321 if (logger.isDebugEnabled()) {
322 logger.debug(testName + ": status = " + statusCode);
324 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
325 invalidStatusCodeMessage(testRequestType, statusCode));
326 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
327 PermissionRole output = res.readEntity(PermissionRole.class);
329 String sOutput = objectAsXmlString(output, PermissionRole.class);
330 if (logger.isDebugEnabled()) {
331 logger.debug(testName + " received " + sOutput);
340 // ---------------------------------------------------------------
341 // CRUD tests : READ_LIST tests
342 // ---------------------------------------------------------------
345 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
348 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
349 // dependsOnMethods = {"createList", "read"})
350 public void readList(String testName) throws Exception {
351 //Should this really be empty?
356 // ---------------------------------------------------------------
357 // CRUD tests : UPDATE tests
358 // ---------------------------------------------------------------
361 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
364 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
365 // dependsOnMethods = {"read", "readList", "readNonExistent"})
366 public void update(String testName) throws Exception {
367 //Should this really be empty?
371 // Placeholders until the three tests below can be uncommented.
372 // See Issue CSPACE-401.
374 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
377 public void updateWithEmptyEntityBody(String testName) throws Exception {
378 //Should this really be empty?
382 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
385 public void updateWithMalformedXml(String testName) throws Exception {
386 //Should this really be empty?
390 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
393 public void updateWithWrongXmlSchema(String testName) throws Exception {
394 //Should this really be empty?
398 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
401 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
402 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
403 public void updateNonExistent(String testName) throws Exception {
404 //Should this really be empty?
407 // ---------------------------------------------------------------
408 // CRUD tests : DELETE tests
409 // ---------------------------------------------------------------
412 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
415 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
416 // dependsOnMethods = {"read"})
417 public void delete(String testName) throws Exception {
421 // Submit the request to the service and store the response.
422 RolePermissionClient client = new RolePermissionClient();
423 RoleValue rv = roleValues.get(getRoleName());
424 Response delRes = client.delete(rv.getRoleId());
426 int statusCode = delRes.getStatus();
427 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
428 invalidStatusCodeMessage(testRequestType, statusCode));
429 Assert.assertEquals(statusCode, testExpectedStatusCode);
431 if (delRes != null) {
436 // reset for next delete
440 rv = roleValues.get(getRoleName());
441 Response readResponse = client.read(rv.getRoleId());
442 PermissionRole toDelete = null;
444 toDelete = readResponse.readEntity(PermissionRole.class);
446 readResponse.close();
449 rv = toDelete.getRole().get(0);
450 Response res = client.delete(rv.getRoleId(), toDelete);
452 int statusCode = res.getStatus();
453 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
454 invalidStatusCodeMessage(testRequestType, statusCode));
455 Assert.assertEquals(statusCode, testExpectedStatusCode);
465 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
468 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
469 public void deleteNonExistent(String testName) throws Exception {
470 //ignoring this test as the service side returns 200 now even if it does
471 //not find a record in the db
474 // ---------------------------------------------------------------
476 // ---------------------------------------------------------------
479 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
480 public void searchWorkflowDeleted(String testName) throws Exception {
481 // Fixme: null test for now, overriding test in base class
484 // ---------------------------------------------------------------
485 // Utility tests : tests of code used in tests above
486 // ---------------------------------------------------------------
488 * Tests the code for manually submitting data that is used by several
489 * of the methods above.
492 // @Test(dependsOnMethods = {"create"}) //FIXME: REM - This is not a test of a submit to the permroles service, but to just authorization/roles
493 public void testSubmitRequest() {
495 // Expected status code: 200 OK
496 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
498 // Submit the request to the service and store the response.
499 String method = ServiceRequestType.READ.httpMethodName();
500 String url = getResourceURL(roleValues.get(getRoleName()).getRoleId());
501 int statusCode = submitRequest(method, url);
503 // Check the status code of the response: does it match
504 // the expected response(s)?
505 if (logger.isDebugEnabled()) {
506 logger.debug("testSubmitRequest: url=" + url
507 + " status=" + statusCode);
509 Assert.assertEquals(statusCode, EXPECTED_STATUS);
512 // ---------------------------------------------------------------
513 // Utility methods used by tests above
514 // ---------------------------------------------------------------
516 * create PermissionRole instance
517 * @param rv rolevalue
518 * @param pvs permission value array
521 * @return PermissionRole
523 public static PermissionRole createPermissionRoleInstance(RoleValue rv,
524 Collection<PermissionValue> pvs,
527 List<PermissionValue> pvls = new ArrayList<PermissionValue>();
529 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
530 rv, pvls, usePermId, useRoleId);
531 if (logger.isDebugEnabled()) {
534 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
543 @AfterClass(alwaysRun = true)
545 public void cleanUp() throws Exception {
547 String noTest = System.getProperty("noTestCleanup");
548 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
549 if (logger.isDebugEnabled()) {
550 logger.debug("Skipping Cleanup phase ...");
554 if (logger.isDebugEnabled()) {
555 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
573 private String createPermission(String resName, EffectType effect) throws Exception {
575 PermissionClient permClient = new PermissionClient();
576 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
577 Permission permission = PermissionFactory.createPermissionInstance(resName,
578 "default permissions for " + resName,
579 actions, effect, true, true, true);
583 res = permClient.create(permission);
584 int statusCode = res.getStatus();
585 if (logger.isDebugEnabled()) {
586 logger.debug("createPermission: resName=" + resName
587 + " status = " + statusCode);
589 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
590 invalidStatusCodeMessage(testRequestType, statusCode));
591 Assert.assertEquals(statusCode, testExpectedStatusCode);
604 * @param permId the perm id
607 private void deletePermission(String permId) throws Exception {
609 PermissionClient permClient = new PermissionClient();
611 Response res = permClient.delete(permId);
613 int statusCode = res.getStatus();
614 if (logger.isDebugEnabled()) {
615 logger.debug("deletePermission: delete permission id="
616 + permId + " status=" + statusCode);
618 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
619 invalidStatusCodeMessage(testRequestType, statusCode));
620 Assert.assertEquals(statusCode, testExpectedStatusCode);
632 * @param roleName the role name
636 private String createRole(String roleName) throws Exception {
638 RoleClient roleClient = new RoleClient();
640 Role role = RoleFactory.createRoleInstance(roleName,
641 roleName, //the display name
642 "role for " + roleName, true);
643 role.setRoleGroup("something");
647 res = roleClient.create(role);
648 int statusCode = res.getStatus();
649 if (logger.isDebugEnabled()) {
650 logger.debug("createRole: name=" + roleName
651 + " status = " + statusCode);
653 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
654 invalidStatusCodeMessage(testRequestType, statusCode));
655 Assert.assertEquals(statusCode, testExpectedStatusCode);
668 * @param roleId the role id
671 private void deleteRole(String roleId) throws Exception {
673 RoleClient roleClient = new RoleClient();
676 res = roleClient.delete(roleId);
677 int statusCode = res.getStatus();
678 if (logger.isDebugEnabled()) {
679 logger.debug("deleteRole: delete role id=" + roleId
680 + " status=" + statusCode);
682 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
683 invalidStatusCodeMessage(testRequestType, statusCode));
684 Assert.assertEquals(statusCode, testExpectedStatusCode);
692 protected PermissionRole createInstance(String commonPartName,
694 // TODO Auto-generated method stub
699 protected PermissionRole updateInstance(PermissionRole commonPartObject) {
700 // TODO Auto-generated method stub
705 protected void compareUpdatedInstances(PermissionRole original,
706 PermissionRole updated) throws Exception {
707 // TODO Auto-generated method stub
712 protected Class<PermissionRole> getCommonListType() {
713 // TODO Auto-generated method stub
718 protected String getKnowResourceId() {
719 return roleValues.get(getRoleName()).getRoleId();
723 protected long getSizeOfList(PermissionRole permRole) {
724 // TODO Auto-generated method stub
725 throw new UnsupportedOperationException("Method getSizeOfList() is not implemented because this service does not support lists.");
729 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
730 * refer to this method in their @Test annotation declarations.
733 @Test(dataProvider = "testName",
735 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
736 public void CRUDTests(String testName) {
737 // Do nothing. Simply here to for a TestNG execution order for our tests