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.EffectType;
33 import org.collectionspace.services.authorization.Permission;
34 import org.collectionspace.services.authorization.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();
100 @BeforeClass(alwaysRun = true)
101 public void seedData() {
102 String ra = TEST_SERVICE_NAME + TEST_MARKER;
103 String accPermId = createPermission(ra, EffectType.PERMIT);
104 PermissionValue pva = new PermissionValue();
105 pva.setResourceName(ra);
106 pva.setPermissionId(accPermId);
107 permValues.put(pva.getResourceName(), pva);
109 String rc = TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX;
110 String coPermId = createPermission(rc, EffectType.DENY);
111 PermissionValue pvc = new PermissionValue();
112 pvc.setResourceName(rc);
113 pvc.setPermissionId(coPermId);
114 permValues.put(pvc.getResourceName(), pvc);
116 // String ri = "intakes";
117 // String iPermId = createPermission(ri, EffectType.DENY);
118 // PermissionValue pvi = new PermissionValue();
119 // pvi.setResourceName(ri);
120 // pvi.setPermissionId(iPermId);
121 // permValues.put(pvi.getResourceName(), pvi);
123 String rn1 = "ROLE_CO1" + TEST_MARKER;
124 String r1RoleId = createRole(rn1);
125 RoleValue rv1 = new RoleValue();
126 rv1.setRoleId(r1RoleId);
127 rv1.setRoleName(rn1);
128 roleValues.put(rv1.getRoleName(), rv1);
130 String rn2 = "ROLE_CO2" + TEST_MARKER;
131 String r2RoleId = createRole(rn2);
132 RoleValue rv2 = new RoleValue();
133 rv2.setRoleId(r2RoleId);
134 rv2.setRoleName(rn2);
135 roleValues.put(rv2.getRoleName(), rv2);
139 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
142 protected CollectionSpaceClient getClientInstance() {
143 return new PermissionRoleClient();
147 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
150 protected AbstractCommonList getAbstractCommonList(
151 ClientResponse<AbstractCommonList> response) {
152 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
153 throw new UnsupportedOperationException();
157 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
159 @Test(dataProvider = "testName")
161 public void readPaginatedList(String testName) throws Exception {
162 //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 {
176 if (logger.isDebugEnabled()) {
177 logger.debug(testBanner(testName, CLASS_NAME));
180 // Perform setup, such as initializing the type of service request
181 // (e.g. CREATE, DELETE), its valid and expected status codes, and
182 // its associated HTTP method name (e.g. POST, DELETE).
185 // Submit the request to the service and store the response.
186 PermissionValue pv = permValues.get(TEST_SERVICE_NAME + TEST_MARKER);
187 PermissionRole permRole = createPermissionRoleInstance(pv,
188 roleValues.values(), true, true);
189 PermissionRoleClient client = new PermissionRoleClient();
190 ClientResponse<Response> res = null;
192 res = client.create(pv.getPermissionId(), permRole);
193 int statusCode = res.getStatus();
195 if (logger.isDebugEnabled()) {
196 logger.debug(testName + ": status = " + statusCode);
198 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
199 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
200 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
201 res.releaseConnection();
202 // Store the ID returned from this create operation
203 // for additional tests below.
204 //this is is not important in case of this relationship
205 knownResourceId = extractId(res);
206 if (logger.isDebugEnabled()) {
207 logger.debug(testName + ": knownResourceId=" + knownResourceId);
211 res.releaseConnection();
216 //to not cause uniqueness violation for permRole, createList is removed
218 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
221 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
222 dependsOnMethods = {"create"})
223 public void createList(String testName) throws Exception {
224 //Should this really be empty?
228 // Placeholders until the three tests below can be uncommented.
229 // See Issue CSPACE-401.
231 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
234 public void createWithEmptyEntityBody(String testName) throws Exception {
235 //Should this really be empty?
239 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
242 public void createWithMalformedXml(String testName) throws Exception {
243 //Should this really be empty?
247 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
250 public void createWithWrongXmlSchema(String testName) throws Exception {
251 //Should this really be empty?
254 // ---------------------------------------------------------------
255 // CRUD tests : READ tests
256 // ---------------------------------------------------------------
259 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
262 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
263 dependsOnMethods = {"create"})
264 public void read(String testName) throws Exception {
266 if (logger.isDebugEnabled()) {
267 logger.debug(testBanner(testName, CLASS_NAME));
272 // Submit the request to the service and store the response.
273 PermissionRoleClient client = new PermissionRoleClient();
274 ClientResponse<PermissionRole> res = null;
277 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
278 int statusCode = res.getStatus();
280 // Check the status code of the response: does it match
281 // the expected response(s)?
282 if (logger.isDebugEnabled()) {
283 logger.debug(testName + ": status = " + statusCode);
285 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
286 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
287 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
289 PermissionRole output = (PermissionRole) res.getEntity();
290 Assert.assertNotNull(output);
293 res.releaseConnection();
301 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
304 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
305 public void readNonExistent(String testName) throws Exception {
307 if (logger.isDebugEnabled()) {
308 logger.debug(testBanner(testName, CLASS_NAME));
311 setupReadNonExistent();
313 // Submit the request to the service and store the response.
314 PermissionRoleClient client = new PermissionRoleClient();
315 ClientResponse<PermissionRole> res = null;
317 res = client.read(NON_EXISTENT_ID);
318 int statusCode = res.getStatus();
320 // Check the status code of the response: does it match
321 // the expected response(s)?
322 if (logger.isDebugEnabled()) {
323 logger.debug(testName + ": status = " + statusCode);
325 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
326 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
327 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
330 res.releaseConnection();
335 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
336 dependsOnMethods = {"create"})
337 public void readNoRelationship(String testName) throws Exception {
339 if (logger.isDebugEnabled()) {
340 logger.debug(testBanner(testName, CLASS_NAME));
345 // Submit the request to the service and store the response.
346 PermissionRoleClient client = new PermissionRoleClient();
347 ClientResponse<PermissionRole> res = null;
350 permValues.get(TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX).getPermissionId());
351 int statusCode = res.getStatus();
353 // Check the status code of the response: does it match
354 // the expected response(s)?
355 if (logger.isDebugEnabled()) {
356 logger.debug(testName + ": status = " + statusCode);
358 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
359 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
360 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
362 PermissionRole output = (PermissionRole) res.getEntity();
364 String sOutput = objectAsXmlString(output, PermissionRole.class);
365 if (logger.isDebugEnabled()) {
366 logger.debug(testName + " received " + sOutput);
370 res.releaseConnection();
375 // ---------------------------------------------------------------
376 // CRUD tests : READ_LIST tests
377 // ---------------------------------------------------------------
380 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
384 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
385 dependsOnMethods = {"createList", "read"})
386 public void readList(String testName) throws Exception {
387 //Should this really be empty?
392 // ---------------------------------------------------------------
393 // CRUD tests : UPDATE tests
394 // ---------------------------------------------------------------
397 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
400 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
401 dependsOnMethods = {"read", "readList", "readNonExistent"})
402 public void update(String testName) throws Exception {
403 //Should this really be empty?
407 // Placeholders until the three tests below can be uncommented.
408 // See Issue CSPACE-401.
410 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
413 public void updateWithEmptyEntityBody(String testName) throws Exception {
414 //Should this really be empty?
418 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
421 public void updateWithMalformedXml(String testName) throws Exception {
422 //Should this really be empty?
426 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
429 public void updateWithWrongXmlSchema(String testName) throws Exception {
430 //Should this really be empty?
434 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
437 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
438 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
439 public void updateNonExistent(String testName) throws Exception {
440 //Should this really be empty?
443 // ---------------------------------------------------------------
444 // CRUD tests : DELETE tests
445 // ---------------------------------------------------------------
448 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
451 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
452 dependsOnMethods = {"read"})
453 public void delete(String testName) throws Exception {
455 if (logger.isDebugEnabled()) {
456 logger.debug(testBanner(testName, CLASS_NAME));
464 PermissionRoleClient client = new PermissionRoleClient();
465 ClientResponse<PermissionRole> readResponse = client.read(
466 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
467 PermissionRole toDelete = null;
469 toDelete = readResponse.getEntity();
471 readResponse.releaseConnection();
474 ClientResponse<Response> res = client.delete(
475 toDelete.getPermissions().get(0).getPermissionId(), toDelete);
477 int statusCode = res.getStatus();
478 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
479 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
480 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
483 res.releaseConnection();
488 // recreate 'TEST_SERVICE_NAME + TEST_MARKER' account and roles
494 // Lookup a know permission, and delete all of its role relationships
496 readResponse = client.read(
497 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
500 toDelete = readResponse.getEntity();
502 readResponse.releaseConnection();
505 res = client.delete(toDelete.getPermissions().get(0).getPermissionId());
507 int statusCode = res.getStatus();
508 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
509 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
510 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
512 res.releaseConnection();
519 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
522 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
523 public void deleteNonExistent(String testName) throws Exception {
524 //ignoring this test as the service side returns 200 now even if it does
525 //not find a record in the db
528 // ---------------------------------------------------------------
529 // Utility tests : tests of code used in tests above
530 // ---------------------------------------------------------------
532 * Tests the code for manually submitting data that is used by several
533 * of the methods above.
536 @Test(dependsOnMethods = {"create"})
537 public void testSubmitRequest() throws Exception {
539 // Expected status code: 200 OK
540 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
542 // Submit the request to the service and store the response.
543 String method = ServiceRequestType.READ.httpMethodName();
544 String url = getResourceURL(permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
545 int statusCode = submitRequest(method, url);
547 // Check the status code of the response: does it match
548 // the expected response(s)?
549 if (logger.isDebugEnabled()) {
550 logger.debug("testSubmitRequest: url=" + url
551 + " status=" + statusCode);
553 Assert.assertEquals(statusCode, EXPECTED_STATUS);
558 // ---------------------------------------------------------------
559 // Utility methods used by tests above
560 // ---------------------------------------------------------------
562 * create permRolerole instance
563 * @param pv permissionvalue
564 * @param rvs rolevalue array
567 * @return PermissionRole
569 public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
570 Collection<RoleValue> rvs,
574 List<RoleValue> rvls = new ArrayList<RoleValue>();
576 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
577 pv, rvls, usePermId, useRoleId);
578 if (logger.isDebugEnabled()) {
579 logger.debug("to be created, permRole");
580 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
588 @AfterClass(alwaysRun = true)
590 public void cleanUp() {
592 String noTest = System.getProperty("noTestCleanup");
593 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
594 if (logger.isDebugEnabled()) {
595 logger.debug("Skipping Cleanup phase ...");
599 if (logger.isDebugEnabled()) {
600 logger.debug("Cleaning up temporary resources created for testing ...");
603 for (PermissionValue pv : permValues.values()) {
604 deletePermission(pv.getPermissionId());
606 for (RoleValue rv : roleValues.values()) {
607 deleteRole(rv.getRoleId());
612 * Creates the permission.
614 * @param resName the res name
615 * @param effect the effect
618 private String createPermission(String resName, EffectType effect) {
619 if (logger.isDebugEnabled()) {
620 logger.debug(testBanner("createPermission"));
623 PermissionClient permClient = new PermissionClient();
624 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
625 Permission permission = PermissionFactory.createPermissionInstance(resName,
626 "default permissions for " + resName,
627 actions, effect, true, true, true);
629 ClientResponse<Response> res = null;
631 res = permClient.create(permission);
633 int statusCode = res.getStatus();
634 if (logger.isDebugEnabled()) {
635 logger.debug("createPermission: resName=" + resName
636 + " status = " + statusCode);
638 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
639 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
640 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
644 res.releaseConnection();
653 * @param permId the perm id
655 private void deletePermission(String permId) {
656 if (logger.isDebugEnabled()) {
657 logger.debug(testBanner("deletePermission"));
660 PermissionClient permClient = new PermissionClient();
661 ClientResponse<Response> res = null;
663 res = permClient.delete(permId);
664 int statusCode = res.getStatus();
665 if (logger.isDebugEnabled()) {
666 logger.debug("deletePermission: delete permission id="
667 + permId + " status=" + statusCode);
669 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
670 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
671 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
673 res.releaseConnection();
681 * @param roleName the role name
684 private String createRole(String roleName) {
685 if (logger.isDebugEnabled()) {
686 logger.debug(testBanner("createRole"));
689 RoleClient roleClient = new RoleClient();
691 Role role = RoleFactory.createRoleInstance(roleName,
692 "role for " + roleName, true);
693 ClientResponse<Response> res = null;
696 res = roleClient.create(role);
697 int statusCode = res.getStatus();
698 if (logger.isDebugEnabled()) {
699 logger.debug("createRole: name=" + roleName
700 + " status = " + statusCode);
702 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
703 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
704 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
708 res.releaseConnection();
716 * @param roleId the role id
718 private void deleteRole(String roleId) {
719 if (logger.isDebugEnabled()) {
720 logger.debug(testBanner("deleteRole"));
723 RoleClient roleClient = new RoleClient();
724 ClientResponse<Response> res = null;
726 res = roleClient.delete(roleId);
727 int statusCode = res.getStatus();
728 if (logger.isDebugEnabled()) {
729 logger.debug("deleteRole: delete role id=" + roleId
730 + " status=" + statusCode);
732 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
733 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
734 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
736 res.releaseConnection();