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;
30 import javax.ws.rs.core.Response;
31 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.RolePermissionClient;
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.collectionspace.services.client.test.ServiceRequestType;
48 import org.jboss.resteasy.client.ClientResponse;
50 import org.testng.Assert;
51 import org.testng.annotations.Test;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.testng.annotations.AfterClass;
56 import org.testng.annotations.BeforeClass;
59 * PermissionServiceTest, carries out tests against a
60 * deployed and running Permission, Role and PermissionRole Services.
62 * $LastChangedRevision: 917 $
63 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
65 public class RolePermissionServiceTest extends AbstractServiceTestImpl<PermissionRole, PermissionRole,
66 PermissionRole, PermissionRole> {
68 /** The Constant logger. */
69 private final static String CLASS_NAME = RolePermissionServiceTest.class.getName();
70 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
71 // Instance variables specific to this test.
72 final private static String TEST_MARKER = "_RolePermissionServiceTest";
73 final private static String TEST_ROLE_NAME = "ROLE";
74 final private static String NO_REL_SUFFIX = "-no-rel";
75 /** The perm values. */
76 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
77 /** The role values. */
78 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
79 private Date now = new Date();
82 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
85 protected String getServicePathComponent() {
86 return new RolePermissionClient().getServicePathComponent();
90 public String getServiceName() {
91 return RoleClient.SERVICE_NAME; // RolePermission is a sub-resource of 'roles' resource.
94 private String getRoleName() {
95 return TEST_ROLE_NAME + TEST_MARKER + now.toString();
99 * The entity type expected from the JAX-RS Response object
101 public Class<PermissionRole> getEntityResponseType() {
102 return PermissionRole.class;
108 @BeforeClass(alwaysRun = true)
109 public void seedData() {
111 String rn1 = getRoleName();
112 String r1RoleId = createRole(rn1);
113 RoleValue rv1 = new RoleValue();
114 rv1.setRoleId(r1RoleId);
115 rv1.setRoleName(rn1);
116 roleValues.put(rv1.getRoleName(), rv1);
118 String rn2 = getRoleName() + NO_REL_SUFFIX;
119 String r2RoleId = createRole(rn2);
120 RoleValue rv2 = new RoleValue();
121 rv2.setRoleId(r2RoleId);
122 rv2.setRoleName(rn2);
123 roleValues.put(rv2.getRoleName(), rv2);
125 String ra1 = "fooService" + TEST_MARKER;
126 String permId1 = createPermission(ra1, EffectType.PERMIT);
127 PermissionValue pva1 = new PermissionValue();
128 pva1.setResourceName(ra1);
129 pva1.setPermissionId(permId1);
130 permValues.put(pva1.getResourceName(), pva1);
132 String ra2 = "barService" + TEST_MARKER;
133 String permId2 = createPermission(ra1, EffectType.PERMIT);
134 PermissionValue pva2 = new PermissionValue();
135 pva2.setResourceName(ra2);
136 pva2.setPermissionId(permId2);
137 permValues.put(pva2.getResourceName(), pva2);
141 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
144 protected CollectionSpaceClient getClientInstance() {
145 return new RolePermissionClient();
149 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
151 // @Test(dataProvider = "testName")
153 public void readPaginatedList(String testName) throws Exception {
154 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
157 // ---------------------------------------------------------------
158 // CRUD tests : CREATE tests
159 // ---------------------------------------------------------------
162 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
166 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
167 public void create(String testName) throws Exception {
168 // Perform setup, such as initializing the type of service request
169 // (e.g. CREATE, DELETE), its valid and expected status codes, and
170 // its associated HTTP method name (e.g. POST, DELETE).
173 // Submit the request to the service and store the response.
174 RoleValue rv = roleValues.get(getRoleName());
175 PermissionRole permRole = createPermissionRoleInstance(rv,
176 permValues.values(), true, true);
177 RolePermissionClient client = new RolePermissionClient();
178 ClientResponse<Response> res = null;
180 res = client.create(rv.getRoleId(), permRole);
181 int statusCode = res.getStatus();
182 if (logger.isDebugEnabled()) {
183 logger.debug(testName + ": status = " + statusCode);
185 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
186 invalidStatusCodeMessage(testRequestType, statusCode));
187 Assert.assertEquals(statusCode, testExpectedStatusCode);
189 knownResourceId = extractId(res); //This is meaningless in this test, see getKnowResourceId() method for details
190 if (logger.isDebugEnabled()) {
191 logger.debug(testName + ": knownResourceId=" + knownResourceId);
195 res.releaseConnection();
200 //to not cause uniqueness violation for permRole, createList is removed
202 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
205 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
206 // dependsOnMethods = {"create"})
207 public void createList(String testName) throws Exception {
208 //Should this really be empty?
212 // Placeholders until the three tests below can be uncommented.
213 // See Issue CSPACE-401.
215 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
218 public void createWithEmptyEntityBody(String testName) throws Exception {
219 //Should this really be empty?
223 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
226 public void createWithMalformedXml(String testName) throws Exception {
227 //Should this really be empty?
231 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
234 public void createWithWrongXmlSchema(String testName) throws Exception {
235 //Should this really be empty?
238 // ---------------------------------------------------------------
239 // CRUD tests : READ tests
240 // ---------------------------------------------------------------
243 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
246 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
247 // dependsOnMethods = {"create"})
248 public void read(String testName) throws Exception {
252 // Submit the request to the service and store the response.
253 RolePermissionClient client = new RolePermissionClient();
254 ClientResponse<PermissionRole> res = null;
256 res = client.read(roleValues.get(getRoleName()).getRoleId());
257 int statusCode = res.getStatus();
259 // Check the status code of the response: does it match
260 // the expected response(s)?
261 if (logger.isDebugEnabled()) {
262 logger.debug(testName + ": status = " + statusCode);
264 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
265 invalidStatusCodeMessage(testRequestType, statusCode));
266 Assert.assertEquals(statusCode, testExpectedStatusCode);
268 PermissionRole output = (PermissionRole) res.getEntity();
269 Assert.assertNotNull(output);
272 res.releaseConnection();
279 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
282 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
283 public void readNonExistent(String testName) throws Exception {
285 setupReadNonExistent();
287 // Submit the request to the service and store the response.
288 RolePermissionClient client = new RolePermissionClient();
289 ClientResponse<PermissionRole> res = null;
292 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);
305 res.releaseConnection();
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 ClientResponse<PermissionRole> res = null;
318 res = client.read(roleValues.get(getRoleName() + NO_REL_SUFFIX).getRoleId());
319 int statusCode = res.getStatus();
321 // Check the status code of the response: does it match
322 // the expected response(s)?
323 if (logger.isDebugEnabled()) {
324 logger.debug(testName + ": status = " + statusCode);
326 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
327 invalidStatusCodeMessage(testRequestType, statusCode));
328 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
329 PermissionRole output = (PermissionRole) res.getEntity();
331 String sOutput = objectAsXmlString(output, PermissionRole.class);
332 if (logger.isDebugEnabled()) {
333 logger.debug(testName + " received " + sOutput);
337 res.releaseConnection();
342 // ---------------------------------------------------------------
343 // CRUD tests : READ_LIST tests
344 // ---------------------------------------------------------------
347 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
350 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
351 // dependsOnMethods = {"createList", "read"})
352 public void readList(String testName) throws Exception {
353 //Should this really be empty?
358 // ---------------------------------------------------------------
359 // CRUD tests : UPDATE tests
360 // ---------------------------------------------------------------
363 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
366 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
367 // dependsOnMethods = {"read", "readList", "readNonExistent"})
368 public void update(String testName) throws Exception {
369 //Should this really be empty?
373 // Placeholders until the three tests below can be uncommented.
374 // See Issue CSPACE-401.
376 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
379 public void updateWithEmptyEntityBody(String testName) throws Exception {
380 //Should this really be empty?
384 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
387 public void updateWithMalformedXml(String testName) throws Exception {
388 //Should this really be empty?
392 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
395 public void updateWithWrongXmlSchema(String testName) throws Exception {
396 //Should this really be empty?
400 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
403 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
404 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
405 public void updateNonExistent(String testName) throws Exception {
406 //Should this really be empty?
409 // ---------------------------------------------------------------
410 // CRUD tests : DELETE tests
411 // ---------------------------------------------------------------
414 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
417 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
418 // dependsOnMethods = {"read"})
419 public void delete(String testName) throws Exception {
423 // Submit the request to the service and store the response.
424 RolePermissionClient client = new RolePermissionClient();
425 RoleValue rv = roleValues.get(getRoleName());
426 Response delRes = client.delete(rv.getRoleId());
428 int statusCode = delRes.getStatus();
429 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
430 invalidStatusCodeMessage(testRequestType, statusCode));
431 Assert.assertEquals(statusCode, testExpectedStatusCode);
433 if (delRes != null) {
438 // reset for next delete
442 rv = roleValues.get(getRoleName());
443 ClientResponse<PermissionRole> readResponse = client.read(rv.getRoleId());
444 PermissionRole toDelete = readResponse.getEntity();
445 readResponse.releaseConnection();
447 rv = toDelete.getRole().get(0);
448 ClientResponse<Response> res = null;
451 rv.getRoleId(), toDelete);
452 int statusCode = res.getStatus();
453 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
454 invalidStatusCodeMessage(testRequestType, statusCode));
455 Assert.assertEquals(statusCode, testExpectedStatusCode);
458 res.releaseConnection();
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));
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 ...");
556 for (PermissionValue pv : permValues.values()) {
557 deletePermission(pv.getPermissionId());
559 for (RoleValue rv : roleValues.values()) {
560 deleteRole(rv.getRoleId());
565 * Creates the permission.
567 * @param resName the res name
568 * @param effect the effect
571 private String createPermission(String resName, EffectType effect) {
573 PermissionClient permClient = new PermissionClient();
574 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
575 Permission permission = PermissionFactory.createPermissionInstance(resName,
576 "default permissions for " + resName,
577 actions, effect, true, true, true);
578 ClientResponse<Response> res = null;
581 res = permClient.create(permission);
582 int statusCode = res.getStatus();
583 if (logger.isDebugEnabled()) {
584 logger.debug("createPermission: resName=" + resName
585 + " status = " + statusCode);
587 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
588 invalidStatusCodeMessage(testRequestType, statusCode));
589 Assert.assertEquals(statusCode, testExpectedStatusCode);
593 res.releaseConnection();
602 * @param permId the perm id
604 private void deletePermission(String permId) {
606 PermissionClient permClient = new PermissionClient();
608 Response res = permClient.delete(permId);
610 int statusCode = res.getStatus();
611 if (logger.isDebugEnabled()) {
612 logger.debug("deletePermission: delete permission id="
613 + permId + " status=" + statusCode);
615 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
616 invalidStatusCodeMessage(testRequestType, statusCode));
617 Assert.assertEquals(statusCode, testExpectedStatusCode);
629 * @param roleName the role name
632 private String createRole(String roleName) {
634 RoleClient roleClient = new RoleClient();
636 Role role = RoleFactory.createRoleInstance(roleName,
637 roleName, //the display name
638 "role for " + roleName, true);
639 role.setRoleGroup("something");
640 ClientResponse<Response> res = null;
643 res = roleClient.create(role);
644 int statusCode = res.getStatus();
645 if (logger.isDebugEnabled()) {
646 logger.debug("createRole: name=" + roleName
647 + " status = " + statusCode);
649 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
650 invalidStatusCodeMessage(testRequestType, statusCode));
651 Assert.assertEquals(statusCode, testExpectedStatusCode);
664 * @param roleId the role id
666 private void deleteRole(String roleId) {
668 RoleClient roleClient = new RoleClient();
671 res = roleClient.delete(roleId);
672 int statusCode = res.getStatus();
673 if (logger.isDebugEnabled()) {
674 logger.debug("deleteRole: delete role id=" + roleId
675 + " status=" + statusCode);
677 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
678 invalidStatusCodeMessage(testRequestType, statusCode));
679 Assert.assertEquals(statusCode, testExpectedStatusCode);
687 protected PermissionRole createInstance(String commonPartName,
689 // TODO Auto-generated method stub
694 protected PermissionRole updateInstance(PermissionRole commonPartObject) {
695 // TODO Auto-generated method stub
700 protected void compareUpdatedInstances(PermissionRole original,
701 PermissionRole updated) throws Exception {
702 // TODO Auto-generated method stub
707 protected Class<PermissionRole> getCommonListType() {
708 // TODO Auto-generated method stub
713 protected String getKnowResourceId() {
714 return roleValues.get(getRoleName()).getRoleId();
718 protected long getSizeOfList(PermissionRole permRole) {
719 // TODO Auto-generated method stub
720 throw new UnsupportedOperationException("Method getSizeOfList() is not implemented because this service does not support lists.");
724 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
725 * refer to this method in their @Test annotation declarations.
728 @Test(dataProvider = "testName",
730 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
731 public void CRUDTests(String testName) {
732 // Do nothing. Simply here to for a TestNG execution order for our tests