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;
29 import javax.ws.rs.core.Response;
31 import org.collectionspace.services.jaxb.AbstractCommonList;
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;
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.PermissionRoleClient;
44 import org.collectionspace.services.client.PermissionRoleFactory;
45 import org.collectionspace.services.client.RoleClient;
46 import org.collectionspace.services.client.RoleFactory;
48 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
49 import org.collectionspace.services.client.test.ServiceRequestType;
51 import org.jboss.resteasy.client.ClientResponse;
53 import org.testng.Assert;
54 import org.testng.annotations.Test;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58 import org.testng.annotations.AfterClass;
59 import org.testng.annotations.BeforeClass;
62 * PermissionServiceTest, carries out tests against a
63 * deployed and running Permission, Role and PermissionRole Services.
65 * $LastChangedRevision: 917 $
66 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
68 public class PermissionRoleServiceTest extends AbstractServiceTestImpl {
70 /** The Constant logger. */
71 private final static String CLASS_NAME = PermissionRoleServiceTest.class.getName();
72 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
73 // Instance variables specific to this test.
74 /** The known resource id. */
75 private String knownResourceId = null;
76 /** The all resource ids created. */
77 private List<String> allResourceIdsCreated = new ArrayList<String>();
78 final private static String TEST_MARKER = "_PermissionRoleServiceTest";
79 final private static String TEST_SERVICE_NAME = "fakeservice";
80 final private static String NO_REL_SUFFIX = "-no-rel";
81 /** The perm values. */
82 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
83 /** The role values. */
84 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
86 * This method is called only by the parent class, AbstractServiceTestImpl
90 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
93 protected String getServicePathComponent() {
94 return new PermissionRoleClient().getServicePathComponent();
98 protected String getServiceName() {
99 return PermissionClient.SERVICE_NAME; //Since we're a sub-resource of permission service return its name?
105 @BeforeClass(alwaysRun = true)
106 public void seedData() {
107 String ra = TEST_SERVICE_NAME + TEST_MARKER;
108 String accPermId = createPermission(ra, EffectType.PERMIT);
109 PermissionValue pva = new PermissionValue();
110 pva.setResourceName(ra);
111 pva.setPermissionId(accPermId);
112 permValues.put(pva.getResourceName(), pva);
114 String rc = TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX;
115 String coPermId = createPermission(rc, EffectType.DENY);
116 PermissionValue pvc = new PermissionValue();
117 pvc.setResourceName(rc);
118 pvc.setPermissionId(coPermId);
119 permValues.put(pvc.getResourceName(), pvc);
121 // String ri = "intakes";
122 // String iPermId = createPermission(ri, EffectType.DENY);
123 // PermissionValue pvi = new PermissionValue();
124 // pvi.setResourceName(ri);
125 // pvi.setPermissionId(iPermId);
126 // permValues.put(pvi.getResourceName(), pvi);
128 String rn1 = "ROLE_CO1" + TEST_MARKER;
129 String r1RoleId = createRole(rn1);
130 RoleValue rv1 = new RoleValue();
131 rv1.setRoleId(r1RoleId);
132 rv1.setRoleName(rn1);
133 roleValues.put(rv1.getRoleName(), rv1);
135 String rn2 = "ROLE_CO2" + TEST_MARKER;
136 String r2RoleId = createRole(rn2);
137 RoleValue rv2 = new RoleValue();
138 rv2.setRoleId(r2RoleId);
139 rv2.setRoleName(rn2);
140 roleValues.put(rv2.getRoleName(), rv2);
144 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
147 protected CollectionSpaceClient getClientInstance() {
148 return new PermissionRoleClient();
152 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
155 protected AbstractCommonList getAbstractCommonList(
156 ClientResponse<AbstractCommonList> response) {
157 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
158 throw new UnsupportedOperationException();
162 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
164 @Test(dataProvider = "testName")
166 public void readPaginatedList(String testName) throws Exception {
167 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
169 // ---------------------------------------------------------------
170 // CRUD tests : CREATE tests
171 // ---------------------------------------------------------------
174 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
178 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
179 public void create(String testName) throws Exception {
181 if (logger.isDebugEnabled()) {
182 logger.debug(testBanner(testName, CLASS_NAME));
185 // Perform setup, such as initializing the type of service request
186 // (e.g. CREATE, DELETE), its valid and expected status codes, and
187 // its associated HTTP method name (e.g. POST, DELETE).
190 // Submit the request to the service and store the response.
191 PermissionValue pv = permValues.get(TEST_SERVICE_NAME + TEST_MARKER);
192 PermissionRole permRole = createPermissionRoleInstance(pv,
193 roleValues.values(), true, true);
194 PermissionRoleClient client = new PermissionRoleClient();
195 ClientResponse<Response> res = null;
197 res = client.create(pv.getPermissionId(), permRole);
198 int statusCode = res.getStatus();
200 if (logger.isDebugEnabled()) {
201 logger.debug(testName + ": status = " + statusCode);
203 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
204 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
205 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
206 res.releaseConnection();
207 // Store the ID returned from this create operation
208 // for additional tests below.
209 //this is is not important in case of this relationship
210 knownResourceId = extractId(res);
211 if (logger.isDebugEnabled()) {
212 logger.debug(testName + ": knownResourceId=" + knownResourceId);
216 res.releaseConnection();
221 //to not cause uniqueness violation for permRole, createList is removed
223 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
226 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
227 dependsOnMethods = {"create"})
228 public void createList(String testName) throws Exception {
229 //Should this really be empty?
233 // Placeholders until the three tests below can be uncommented.
234 // See Issue CSPACE-401.
236 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
239 public void createWithEmptyEntityBody(String testName) throws Exception {
240 //Should this really be empty?
244 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
247 public void createWithMalformedXml(String testName) throws Exception {
248 //Should this really be empty?
252 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
255 public void createWithWrongXmlSchema(String testName) throws Exception {
256 //Should this really be empty?
259 // ---------------------------------------------------------------
260 // CRUD tests : READ tests
261 // ---------------------------------------------------------------
264 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
267 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
268 dependsOnMethods = {"create"})
269 public void read(String testName) throws Exception {
271 if (logger.isDebugEnabled()) {
272 logger.debug(testBanner(testName, CLASS_NAME));
277 // Submit the request to the service and store the response.
278 PermissionRoleClient client = new PermissionRoleClient();
279 ClientResponse<PermissionRole> res = null;
282 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
283 int statusCode = res.getStatus();
285 // Check the status code of the response: does it match
286 // the expected response(s)?
287 if (logger.isDebugEnabled()) {
288 logger.debug(testName + ": status = " + statusCode);
290 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
291 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
292 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
294 PermissionRole output = (PermissionRole) res.getEntity();
295 Assert.assertNotNull(output);
298 res.releaseConnection();
306 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
309 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
310 public void readNonExistent(String testName) throws Exception {
312 if (logger.isDebugEnabled()) {
313 logger.debug(testBanner(testName, CLASS_NAME));
316 setupReadNonExistent();
318 // Submit the request to the service and store the response.
319 PermissionRoleClient client = new PermissionRoleClient();
320 ClientResponse<PermissionRole> res = null;
322 res = client.read(NON_EXISTENT_ID);
323 int statusCode = res.getStatus();
325 // Check the status code of the response: does it match
326 // the expected response(s)?
327 if (logger.isDebugEnabled()) {
328 logger.debug(testName + ": status = " + statusCode);
330 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
331 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
332 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
335 res.releaseConnection();
340 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
341 dependsOnMethods = {"create"})
342 public void readNoRelationship(String testName) throws Exception {
344 if (logger.isDebugEnabled()) {
345 logger.debug(testBanner(testName, CLASS_NAME));
350 // Submit the request to the service and store the response.
351 PermissionRoleClient client = new PermissionRoleClient();
352 ClientResponse<PermissionRole> res = null;
355 permValues.get(TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX).getPermissionId());
356 int statusCode = res.getStatus();
358 // Check the status code of the response: does it match
359 // the expected response(s)?
360 if (logger.isDebugEnabled()) {
361 logger.debug(testName + ": status = " + statusCode);
363 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
364 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
365 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
367 PermissionRole output = (PermissionRole) res.getEntity();
369 String sOutput = objectAsXmlString(output, PermissionRole.class);
370 if (logger.isDebugEnabled()) {
371 logger.debug(testName + " received " + sOutput);
375 res.releaseConnection();
380 // ---------------------------------------------------------------
381 // CRUD tests : READ_LIST tests
382 // ---------------------------------------------------------------
385 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
389 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
390 dependsOnMethods = {"createList", "read"})
391 public void readList(String testName) throws Exception {
392 //Should this really be empty?
397 // ---------------------------------------------------------------
398 // CRUD tests : UPDATE tests
399 // ---------------------------------------------------------------
402 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
405 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
406 dependsOnMethods = {"read", "readList", "readNonExistent"})
407 public void update(String testName) throws Exception {
408 //Should this really be empty?
412 // Placeholders until the three tests below can be uncommented.
413 // See Issue CSPACE-401.
415 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
418 public void updateWithEmptyEntityBody(String testName) throws Exception {
419 //Should this really be empty?
423 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
426 public void updateWithMalformedXml(String testName) throws Exception {
427 //Should this really be empty?
431 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
434 public void updateWithWrongXmlSchema(String testName) throws Exception {
435 //Should this really be empty?
439 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
442 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
443 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
444 public void updateNonExistent(String testName) throws Exception {
445 //Should this really be empty?
448 // ---------------------------------------------------------------
449 // CRUD tests : DELETE tests
450 // ---------------------------------------------------------------
453 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
456 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
457 dependsOnMethods = {"read"})
458 public void delete(String testName) throws Exception {
460 if (logger.isDebugEnabled()) {
461 logger.debug(testBanner(testName, CLASS_NAME));
469 PermissionRoleClient client = new PermissionRoleClient();
470 ClientResponse<PermissionRole> readResponse = client.read(
471 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
472 PermissionRole toDelete = null;
474 toDelete = readResponse.getEntity();
476 readResponse.releaseConnection();
479 ClientResponse<Response> res = client.delete(
480 toDelete.getPermission().get(0).getPermissionId(), toDelete);
482 int statusCode = res.getStatus();
483 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
484 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
485 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
488 res.releaseConnection();
493 // recreate 'TEST_SERVICE_NAME + TEST_MARKER' account and roles
499 // Lookup a know permission, and delete all of its role relationships
501 readResponse = client.read(
502 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
505 toDelete = readResponse.getEntity();
507 readResponse.releaseConnection();
510 res = client.delete(toDelete.getPermission().get(0).getPermissionId());
512 int statusCode = res.getStatus();
513 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
514 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
515 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
517 res.releaseConnection();
524 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
527 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
528 public void deleteNonExistent(String testName) throws Exception {
529 //ignoring this test as the service side returns 200 now even if it does
530 //not find a record in the db
533 // ---------------------------------------------------------------
535 // ---------------------------------------------------------------
538 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
539 public void searchWorkflowDeleted(String testName) throws Exception {
540 // Fixme: null test for now, overriding test in base class
543 // ---------------------------------------------------------------
544 // Utility tests : tests of code used in tests above
545 // ---------------------------------------------------------------
547 * Tests the code for manually submitting data that is used by several
548 * of the methods above.
551 @Test(dependsOnMethods = {"create"})
552 public void testSubmitRequest() throws Exception { //FIXME: REM - This is testing /permissions not /permissions/permroles
554 // Expected status code: 200 OK
555 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
557 // Submit the request to the service and store the response.
558 String method = ServiceRequestType.READ.httpMethodName();
559 String url = getResourceURL(permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
560 int statusCode = submitRequest(method, url);
562 // Check the status code of the response: does it match
563 // the expected response(s)?
564 if (logger.isDebugEnabled()) {
565 logger.debug("testSubmitRequest: url=" + url
566 + " status=" + statusCode);
568 Assert.assertEquals(statusCode, EXPECTED_STATUS);
573 // ---------------------------------------------------------------
574 // Utility methods used by tests above
575 // ---------------------------------------------------------------
577 * create permRolerole instance
578 * @param pv permissionvalue
579 * @param rvs rolevalue array
582 * @return PermissionRole
584 public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
585 Collection<RoleValue> rvs,
589 List<RoleValue> rvls = new ArrayList<RoleValue>();
591 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
592 pv, rvls, usePermId, useRoleId);
593 if (logger.isDebugEnabled()) {
594 logger.debug("to be created, permRole");
595 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
603 @AfterClass(alwaysRun = true)
605 public void cleanUp() {
607 String noTest = System.getProperty("noTestCleanup");
608 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
609 if (logger.isDebugEnabled()) {
610 logger.debug("Skipping Cleanup phase ...");
614 if (logger.isDebugEnabled()) {
615 logger.debug("Cleaning up temporary resources created for testing ...");
618 for (PermissionValue pv : permValues.values()) {
619 deletePermission(pv.getPermissionId());
621 for (RoleValue rv : roleValues.values()) {
622 deleteRole(rv.getRoleId());
627 * Creates the permission.
629 * @param resName the res name
630 * @param effect the effect
633 private String createPermission(String resName, EffectType effect) {
634 if (logger.isDebugEnabled()) {
635 logger.debug(testBanner("createPermission"));
638 PermissionClient permClient = new PermissionClient();
639 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
640 Permission permission = PermissionFactory.createPermissionInstance(resName,
641 "default permissions for " + resName,
642 actions, effect, true, true, true);
644 ClientResponse<Response> res = null;
646 res = permClient.create(permission);
648 int statusCode = res.getStatus();
649 if (logger.isDebugEnabled()) {
650 logger.debug("createPermission: resName=" + resName
651 + " status = " + statusCode);
653 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
654 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
655 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
659 res.releaseConnection();
668 * @param permId the perm id
670 private void deletePermission(String permId) {
671 if (logger.isDebugEnabled()) {
672 logger.debug(testBanner("deletePermission"));
675 PermissionClient permClient = new PermissionClient();
676 ClientResponse<Response> res = null;
678 res = permClient.delete(permId);
679 int statusCode = res.getStatus();
680 if (logger.isDebugEnabled()) {
681 logger.debug("deletePermission: delete permission id="
682 + permId + " status=" + statusCode);
684 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
685 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
686 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
688 res.releaseConnection();
696 * @param roleName the role name
699 private String createRole(String roleName) {
700 if (logger.isDebugEnabled()) {
701 logger.debug(testBanner("createRole"));
704 RoleClient roleClient = new RoleClient();
706 Role role = RoleFactory.createRoleInstance(roleName,
707 roleName, //the display name
708 "role for " + roleName, true);
709 ClientResponse<Response> res = null;
712 res = roleClient.create(role);
713 int statusCode = res.getStatus();
714 if (logger.isDebugEnabled()) {
715 logger.debug("createRole: name=" + roleName
716 + " status = " + statusCode);
718 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
719 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
720 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
724 res.releaseConnection();
732 * @param roleId the role id
734 private void deleteRole(String roleId) {
735 if (logger.isDebugEnabled()) {
736 logger.debug(testBanner("deleteRole"));
739 RoleClient roleClient = new RoleClient();
740 ClientResponse<Response> res = null;
742 res = roleClient.delete(roleId);
743 int statusCode = res.getStatus();
744 if (logger.isDebugEnabled()) {
745 logger.debug("deleteRole: delete role id=" + roleId
746 + " status=" + statusCode);
748 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
749 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
750 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
752 res.releaseConnection();