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();
101 @BeforeClass(alwaysRun = true)
102 public void seedData() {
104 String rn1 = getRoleName();
105 String r1RoleId = createRole(rn1);
106 RoleValue rv1 = new RoleValue();
107 rv1.setRoleId(r1RoleId);
108 rv1.setRoleName(rn1);
109 roleValues.put(rv1.getRoleName(), rv1);
111 String rn2 = getRoleName() + NO_REL_SUFFIX;
112 String r2RoleId = createRole(rn2);
113 RoleValue rv2 = new RoleValue();
114 rv2.setRoleId(r2RoleId);
115 rv2.setRoleName(rn2);
116 roleValues.put(rv2.getRoleName(), rv2);
118 String ra1 = "fooService" + TEST_MARKER;
119 String permId1 = createPermission(ra1, EffectType.PERMIT);
120 PermissionValue pva1 = new PermissionValue();
121 pva1.setResourceName(ra1);
122 pva1.setPermissionId(permId1);
123 permValues.put(pva1.getResourceName(), pva1);
125 String ra2 = "barService" + TEST_MARKER;
126 String permId2 = createPermission(ra1, EffectType.PERMIT);
127 PermissionValue pva2 = new PermissionValue();
128 pva2.setResourceName(ra2);
129 pva2.setPermissionId(permId2);
130 permValues.put(pva2.getResourceName(), pva2);
134 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
137 protected CollectionSpaceClient getClientInstance() {
138 return new RolePermissionClient();
142 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
144 // @Test(dataProvider = "testName")
146 public void readPaginatedList(String testName) throws Exception {
147 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
150 // ---------------------------------------------------------------
151 // CRUD tests : CREATE tests
152 // ---------------------------------------------------------------
155 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
159 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
160 public void create(String testName) throws Exception {
161 // Perform setup, such as initializing the type of service request
162 // (e.g. CREATE, DELETE), its valid and expected status codes, and
163 // its associated HTTP method name (e.g. POST, DELETE).
166 // Submit the request to the service and store the response.
167 RoleValue rv = roleValues.get(getRoleName());
168 PermissionRole permRole = createPermissionRoleInstance(rv,
169 permValues.values(), true, true);
170 RolePermissionClient client = new RolePermissionClient();
171 ClientResponse<Response> res = null;
173 res = client.create(rv.getRoleId(), permRole);
174 int statusCode = res.getStatus();
175 if (logger.isDebugEnabled()) {
176 logger.debug(testName + ": status = " + statusCode);
178 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(testRequestType, statusCode));
180 Assert.assertEquals(statusCode, testExpectedStatusCode);
182 knownResourceId = extractId(res); //This is meaningless in this test, see getKnowResourceId() method for details
183 if (logger.isDebugEnabled()) {
184 logger.debug(testName + ": knownResourceId=" + knownResourceId);
188 res.releaseConnection();
193 //to not cause uniqueness violation for permRole, createList is removed
195 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
198 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
199 // dependsOnMethods = {"create"})
200 public void createList(String testName) throws Exception {
201 //Should this really be empty?
205 // Placeholders until the three tests below can be uncommented.
206 // See Issue CSPACE-401.
208 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
211 public void createWithEmptyEntityBody(String testName) throws Exception {
212 //Should this really be empty?
216 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
219 public void createWithMalformedXml(String testName) throws Exception {
220 //Should this really be empty?
224 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
227 public void createWithWrongXmlSchema(String testName) throws Exception {
228 //Should this really be empty?
231 // ---------------------------------------------------------------
232 // CRUD tests : READ tests
233 // ---------------------------------------------------------------
236 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
239 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
240 // dependsOnMethods = {"create"})
241 public void read(String testName) throws Exception {
245 // Submit the request to the service and store the response.
246 RolePermissionClient client = new RolePermissionClient();
247 ClientResponse<PermissionRole> res = null;
249 res = client.read(roleValues.get(getRoleName()).getRoleId());
250 int statusCode = res.getStatus();
252 // Check the status code of the response: does it match
253 // the expected response(s)?
254 if (logger.isDebugEnabled()) {
255 logger.debug(testName + ": status = " + statusCode);
257 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
258 invalidStatusCodeMessage(testRequestType, statusCode));
259 Assert.assertEquals(statusCode, testExpectedStatusCode);
261 PermissionRole output = (PermissionRole) res.getEntity();
262 Assert.assertNotNull(output);
265 res.releaseConnection();
272 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
275 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
276 public void readNonExistent(String testName) throws Exception {
278 setupReadNonExistent();
280 // Submit the request to the service and store the response.
281 RolePermissionClient client = new RolePermissionClient();
282 ClientResponse<PermissionRole> res = null;
285 res = client.read(NON_EXISTENT_ID);
286 int statusCode = res.getStatus();
288 // Check the status code of the response: does it match
289 // the expected response(s)?
290 if (logger.isDebugEnabled()) {
291 logger.debug(testName + ": status = " + statusCode);
293 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
294 invalidStatusCodeMessage(testRequestType, statusCode));
295 Assert.assertEquals(statusCode, testExpectedStatusCode);
298 res.releaseConnection();
303 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
304 public void readNoRelationship(String testName) throws Exception {
306 // Submit the request to the service and store the response.
307 RolePermissionClient client = new RolePermissionClient();
308 ClientResponse<PermissionRole> res = null;
311 res = client.read(roleValues.get(getRoleName() + NO_REL_SUFFIX).getRoleId());
312 int statusCode = res.getStatus();
314 // Check the status code of the response: does it match
315 // the expected response(s)?
316 if (logger.isDebugEnabled()) {
317 logger.debug(testName + ": status = " + statusCode);
319 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
320 invalidStatusCodeMessage(testRequestType, statusCode));
321 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
322 PermissionRole output = (PermissionRole) res.getEntity();
324 String sOutput = objectAsXmlString(output, PermissionRole.class);
325 if (logger.isDebugEnabled()) {
326 logger.debug(testName + " received " + sOutput);
330 res.releaseConnection();
335 // ---------------------------------------------------------------
336 // CRUD tests : READ_LIST tests
337 // ---------------------------------------------------------------
340 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
343 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
344 // dependsOnMethods = {"createList", "read"})
345 public void readList(String testName) throws Exception {
346 //Should this really be empty?
351 // ---------------------------------------------------------------
352 // CRUD tests : UPDATE tests
353 // ---------------------------------------------------------------
356 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
359 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
360 // dependsOnMethods = {"read", "readList", "readNonExistent"})
361 public void update(String testName) throws Exception {
362 //Should this really be empty?
366 // Placeholders until the three tests below can be uncommented.
367 // See Issue CSPACE-401.
369 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
372 public void updateWithEmptyEntityBody(String testName) throws Exception {
373 //Should this really be empty?
377 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
380 public void updateWithMalformedXml(String testName) throws Exception {
381 //Should this really be empty?
385 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
388 public void updateWithWrongXmlSchema(String testName) throws Exception {
389 //Should this really be empty?
393 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
396 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
397 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
398 public void updateNonExistent(String testName) throws Exception {
399 //Should this really be empty?
402 // ---------------------------------------------------------------
403 // CRUD tests : DELETE tests
404 // ---------------------------------------------------------------
407 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
410 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
411 // dependsOnMethods = {"read"})
412 public void delete(String testName) throws Exception {
416 // Submit the request to the service and store the response.
417 RolePermissionClient client = new RolePermissionClient();
418 RoleValue rv = roleValues.get(getRoleName());
419 ClientResponse<Response> delRes = null;
421 delRes = client.delete(rv.getRoleId());
422 int statusCode = delRes.getStatus();
423 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
424 invalidStatusCodeMessage(testRequestType, statusCode));
425 Assert.assertEquals(statusCode, testExpectedStatusCode);
427 if (delRes != null) {
428 delRes.releaseConnection();
432 // reset for next delete
436 rv = roleValues.get(getRoleName());
437 ClientResponse<PermissionRole> readResponse = client.read(rv.getRoleId());
438 PermissionRole toDelete = readResponse.getEntity();
439 readResponse.releaseConnection();
441 rv = toDelete.getRole().get(0);
442 ClientResponse<Response> res = null;
445 rv.getRoleId(), toDelete);
446 int statusCode = res.getStatus();
447 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
448 invalidStatusCodeMessage(testRequestType, statusCode));
449 Assert.assertEquals(statusCode, testExpectedStatusCode);
452 res.releaseConnection();
459 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
462 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
463 public void deleteNonExistent(String testName) throws Exception {
464 //ignoring this test as the service side returns 200 now even if it does
465 //not find a record in the db
468 // ---------------------------------------------------------------
470 // ---------------------------------------------------------------
473 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
474 public void searchWorkflowDeleted(String testName) throws Exception {
475 // Fixme: null test for now, overriding test in base class
478 // ---------------------------------------------------------------
479 // Utility tests : tests of code used in tests above
480 // ---------------------------------------------------------------
482 * Tests the code for manually submitting data that is used by several
483 * of the methods above.
486 // @Test(dependsOnMethods = {"create"}) //FIXME: REM - This is not a test of a submit to the permroles service, but to just authorization/roles
487 public void testSubmitRequest() {
489 // Expected status code: 200 OK
490 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
492 // Submit the request to the service and store the response.
493 String method = ServiceRequestType.READ.httpMethodName();
494 String url = getResourceURL(roleValues.get(getRoleName()).getRoleId());
495 int statusCode = submitRequest(method, url);
497 // Check the status code of the response: does it match
498 // the expected response(s)?
499 if (logger.isDebugEnabled()) {
500 logger.debug("testSubmitRequest: url=" + url
501 + " status=" + statusCode);
503 Assert.assertEquals(statusCode, EXPECTED_STATUS);
506 // ---------------------------------------------------------------
507 // Utility methods used by tests above
508 // ---------------------------------------------------------------
510 * create PermissionRole instance
511 * @param rv rolevalue
512 * @param pvs permission value array
515 * @return PermissionRole
517 public static PermissionRole createPermissionRoleInstance(RoleValue rv,
518 Collection<PermissionValue> pvs,
521 List<PermissionValue> pvls = new ArrayList<PermissionValue>();
523 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
524 rv, pvls, usePermId, useRoleId);
525 if (logger.isDebugEnabled()) {
528 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
536 @AfterClass(alwaysRun = true)
538 public void cleanUp() {
540 String noTest = System.getProperty("noTestCleanup");
541 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
542 if (logger.isDebugEnabled()) {
543 logger.debug("Skipping Cleanup phase ...");
547 if (logger.isDebugEnabled()) {
548 logger.debug("Cleaning up temporary resources created for testing ...");
550 for (PermissionValue pv : permValues.values()) {
551 deletePermission(pv.getPermissionId());
553 for (RoleValue rv : roleValues.values()) {
554 deleteRole(rv.getRoleId());
559 * Creates the permission.
561 * @param resName the res name
562 * @param effect the effect
565 private String createPermission(String resName, EffectType effect) {
567 PermissionClient permClient = new PermissionClient();
568 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
569 Permission permission = PermissionFactory.createPermissionInstance(resName,
570 "default permissions for " + resName,
571 actions, effect, true, true, true);
572 ClientResponse<Response> res = null;
575 res = permClient.create(permission);
576 int statusCode = res.getStatus();
577 if (logger.isDebugEnabled()) {
578 logger.debug("createPermission: resName=" + resName
579 + " status = " + statusCode);
581 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
582 invalidStatusCodeMessage(testRequestType, statusCode));
583 Assert.assertEquals(statusCode, testExpectedStatusCode);
587 res.releaseConnection();
596 * @param permId the perm id
598 private void deletePermission(String permId) {
600 PermissionClient permClient = new PermissionClient();
602 ClientResponse<Response> res = null;
604 res = permClient.delete(permId);
605 int statusCode = res.getStatus();
606 if (logger.isDebugEnabled()) {
607 logger.debug("deletePermission: delete permission id="
608 + permId + " status=" + statusCode);
610 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
611 invalidStatusCodeMessage(testRequestType, statusCode));
612 Assert.assertEquals(statusCode, testExpectedStatusCode);
616 res.releaseConnection();
624 * @param roleName the role name
627 private String createRole(String roleName) {
629 RoleClient roleClient = new RoleClient();
631 Role role = RoleFactory.createRoleInstance(roleName,
632 roleName, //the display name
633 "role for " + roleName, true);
634 role.setRoleGroup("something");
635 ClientResponse<Response> res = null;
638 res = roleClient.create(role);
639 int statusCode = res.getStatus();
640 if (logger.isDebugEnabled()) {
641 logger.debug("createRole: name=" + roleName
642 + " status = " + statusCode);
644 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
645 invalidStatusCodeMessage(testRequestType, statusCode));
646 Assert.assertEquals(statusCode, testExpectedStatusCode);
650 res.releaseConnection();
659 * @param roleId the role id
661 private void deleteRole(String roleId) {
663 RoleClient roleClient = new RoleClient();
664 ClientResponse<Response> res = null;
666 res = roleClient.delete(roleId);
667 int statusCode = res.getStatus();
668 if (logger.isDebugEnabled()) {
669 logger.debug("deleteRole: delete role id=" + roleId
670 + " status=" + statusCode);
672 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
673 invalidStatusCodeMessage(testRequestType, statusCode));
674 Assert.assertEquals(statusCode, testExpectedStatusCode);
676 res.releaseConnection();
682 protected PermissionRole createInstance(String commonPartName,
684 // TODO Auto-generated method stub
689 protected PermissionRole updateInstance(PermissionRole commonPartObject) {
690 // TODO Auto-generated method stub
695 protected void compareUpdatedInstances(PermissionRole original,
696 PermissionRole updated) throws Exception {
697 // TODO Auto-generated method stub
702 protected Class<PermissionRole> getCommonListType() {
703 // TODO Auto-generated method stub
708 protected String getKnowResourceId() {
709 return roleValues.get(getRoleName()).getRoleId();
713 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
714 * refer to this method in their @Test annotation declarations.
717 @Test(dataProvider = "testName",
719 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
720 public void CRUDTests(String testName) {
721 // Do nothing. Simply here to for a TestNG execution order for our tests