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 assertStatusCode(res, testName);
284 PermissionRole output = (PermissionRole) res.getEntity();
285 Assert.assertNotNull(output);
288 res.releaseConnection();
296 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
299 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
300 public void readNonExistent(String testName) throws Exception {
302 if (logger.isDebugEnabled()) {
303 logger.debug(testBanner(testName, CLASS_NAME));
306 setupReadNonExistent();
308 // Submit the request to the service and store the response.
309 PermissionRoleClient client = new PermissionRoleClient();
310 ClientResponse<PermissionRole> res = null;
312 res = client.read(NON_EXISTENT_ID);
313 int statusCode = res.getStatus();
315 // Check the status code of the response: does it match
316 // the expected response(s)?
317 if (logger.isDebugEnabled()) {
318 logger.debug(testName + ": status = " + statusCode);
320 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
321 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
322 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
325 res.releaseConnection();
330 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
331 dependsOnMethods = {"create"})
332 public void readNoRelationship(String testName) throws Exception {
334 if (logger.isDebugEnabled()) {
335 logger.debug(testBanner(testName, CLASS_NAME));
340 // Submit the request to the service and store the response.
341 PermissionRoleClient client = new PermissionRoleClient();
342 ClientResponse<PermissionRole> res = null;
345 permValues.get(TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX).getPermissionId());
346 assertStatusCode(res, testName);
348 PermissionRole output = (PermissionRole) res.getEntity();
350 String sOutput = objectAsXmlString(output, PermissionRole.class);
351 if (logger.isDebugEnabled()) {
352 logger.debug(testName + " received " + sOutput);
356 res.releaseConnection();
361 // ---------------------------------------------------------------
362 // CRUD tests : READ_LIST tests
363 // ---------------------------------------------------------------
366 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
370 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
371 dependsOnMethods = {"createList", "read"})
372 public void readList(String testName) throws Exception {
373 //Should this really be empty?
378 // ---------------------------------------------------------------
379 // CRUD tests : UPDATE tests
380 // ---------------------------------------------------------------
383 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
386 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
387 dependsOnMethods = {"read", "readList", "readNonExistent"})
388 public void update(String testName) throws Exception {
389 //Should this really be empty?
393 // Placeholders until the three tests below can be uncommented.
394 // See Issue CSPACE-401.
396 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
399 public void updateWithEmptyEntityBody(String testName) throws Exception {
400 //Should this really be empty?
404 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
407 public void updateWithMalformedXml(String testName) throws Exception {
408 //Should this really be empty?
412 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
415 public void updateWithWrongXmlSchema(String testName) throws Exception {
416 //Should this really be empty?
420 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
423 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
424 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
425 public void updateNonExistent(String testName) throws Exception {
426 //Should this really be empty?
429 // ---------------------------------------------------------------
430 // CRUD tests : DELETE tests
431 // ---------------------------------------------------------------
434 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
437 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
438 dependsOnMethods = {"read"})
439 public void delete(String testName) throws Exception {
441 if (logger.isDebugEnabled()) {
442 logger.debug(testBanner(testName, CLASS_NAME));
450 PermissionRoleClient client = new PermissionRoleClient();
451 ClientResponse<PermissionRole> readResponse = client.read(
452 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
453 PermissionRole toDelete = null;
455 toDelete = readResponse.getEntity();
457 readResponse.releaseConnection();
460 ClientResponse<Response> res = client.delete(
461 toDelete.getPermission().get(0).getPermissionId(), toDelete);
463 int statusCode = res.getStatus();
464 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
465 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
466 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
469 res.releaseConnection();
474 // recreate 'TEST_SERVICE_NAME + TEST_MARKER' account and roles
480 // Lookup a know permission, and delete all of its role relationships
482 readResponse = client.read(
483 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
486 toDelete = readResponse.getEntity();
488 readResponse.releaseConnection();
491 res = client.delete(toDelete.getPermission().get(0).getPermissionId());
493 int statusCode = res.getStatus();
494 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
495 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
496 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
498 res.releaseConnection();
505 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
508 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
509 public void deleteNonExistent(String testName) throws Exception {
510 //ignoring this test as the service side returns 200 now even if it does
511 //not find a record in the db
514 // ---------------------------------------------------------------
516 // ---------------------------------------------------------------
519 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
520 public void searchWorkflowDeleted(String testName) throws Exception {
521 // Fixme: null test for now, overriding test in base class
524 // ---------------------------------------------------------------
525 // Utility tests : tests of code used in tests above
526 // ---------------------------------------------------------------
528 * Tests the code for manually submitting data that is used by several
529 * of the methods above.
532 @Test(dependsOnMethods = {"create"})
533 public void testSubmitRequest() throws Exception { //FIXME: REM - This is testing /permissions not /permissions/permroles
535 // Expected status code: 200 OK
536 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
538 // Submit the request to the service and store the response.
539 String method = ServiceRequestType.READ.httpMethodName();
540 String url = getResourceURL(permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
541 int statusCode = submitRequest(method, url);
543 // Check the status code of the response: does it match
544 // the expected response(s)?
545 if (logger.isDebugEnabled()) {
546 logger.debug("testSubmitRequest: url=" + url
547 + " status=" + statusCode);
549 Assert.assertEquals(statusCode, EXPECTED_STATUS);
554 // ---------------------------------------------------------------
555 // Utility methods used by tests above
556 // ---------------------------------------------------------------
558 * create permRolerole instance
559 * @param pv permissionvalue
560 * @param rvs rolevalue array
563 * @return PermissionRole
565 public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
566 Collection<RoleValue> rvs,
570 List<RoleValue> rvls = new ArrayList<RoleValue>();
572 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
573 pv, rvls, usePermId, useRoleId);
574 if (logger.isDebugEnabled()) {
575 logger.debug("to be created, permRole");
576 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
584 @AfterClass(alwaysRun = true)
586 public void cleanUp() {
588 String noTest = System.getProperty("noTestCleanup");
589 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
590 if (logger.isDebugEnabled()) {
591 logger.debug("Skipping Cleanup phase ...");
595 if (logger.isDebugEnabled()) {
596 logger.debug("Cleaning up temporary resources created for testing ...");
599 for (PermissionValue pv : permValues.values()) {
600 deletePermission(pv.getPermissionId());
602 for (RoleValue rv : roleValues.values()) {
603 deleteRole(rv.getRoleId());
608 * Creates the permission.
610 * @param resName the res name
611 * @param effect the effect
614 private String createPermission(String resName, EffectType effect) {
615 if (logger.isDebugEnabled()) {
616 logger.debug(testBanner("createPermission"));
619 PermissionClient permClient = new PermissionClient();
620 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
621 Permission permission = PermissionFactory.createPermissionInstance(resName,
622 "default permissions for " + resName,
623 actions, effect, true, true, true);
625 ClientResponse<Response> res = null;
627 res = permClient.create(permission);
629 int statusCode = res.getStatus();
630 if (logger.isDebugEnabled()) {
631 logger.debug("createPermission: resName=" + resName
632 + " status = " + statusCode);
634 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
635 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
636 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
640 res.releaseConnection();
649 * @param permId the perm id
651 private void deletePermission(String permId) {
652 if (logger.isDebugEnabled()) {
653 logger.debug(testBanner("deletePermission"));
656 PermissionClient permClient = new PermissionClient();
657 ClientResponse<Response> res = null;
659 res = permClient.delete(permId);
660 int statusCode = res.getStatus();
661 if (logger.isDebugEnabled()) {
662 logger.debug("deletePermission: delete permission id="
663 + permId + " status=" + statusCode);
665 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
666 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
667 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
669 res.releaseConnection();
677 * @param roleName the role name
680 private String createRole(String roleName) {
681 if (logger.isDebugEnabled()) {
682 logger.debug(testBanner("createRole"));
685 RoleClient roleClient = new RoleClient();
687 Role role = RoleFactory.createRoleInstance(roleName,
688 roleName, //the display name
689 "role for " + roleName, true);
690 ClientResponse<Response> res = null;
693 res = roleClient.create(role);
694 int statusCode = res.getStatus();
695 if (logger.isDebugEnabled()) {
696 logger.debug("createRole: name=" + roleName
697 + " status = " + statusCode);
699 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
700 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
701 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
705 res.releaseConnection();
713 * @param roleId the role id
715 private void deleteRole(String roleId) {
716 if (logger.isDebugEnabled()) {
717 logger.debug(testBanner("deleteRole"));
720 RoleClient roleClient = new RoleClient();
721 ClientResponse<Response> res = null;
723 res = roleClient.delete(roleId);
724 int statusCode = res.getStatus();
725 if (logger.isDebugEnabled()) {
726 logger.debug("deleteRole: delete role id=" + roleId
727 + " status=" + statusCode);
729 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
730 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
731 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
733 res.releaseConnection();