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.collectionspace.services.jaxb.AbstractCommonList;
49 import org.jboss.resteasy.client.ClientResponse;
51 import org.testng.Assert;
52 import org.testng.annotations.Test;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.testng.annotations.AfterClass;
57 import org.testng.annotations.BeforeClass;
60 * PermissionServiceTest, carries out tests against a
61 * deployed and running Permission, Role and PermissionRole Services.
63 * $LastChangedRevision: 917 $
64 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
66 public class RolePermissionServiceTest extends AbstractServiceTestImpl {
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 /** The known resource id. */
73 private String knownResourceId = null;
74 /** The all resource ids created. */
75 private List<String> allResourceIdsCreated = new ArrayList<String>();
76 final private static String TEST_MARKER = "_RolePermissionServiceTest";
77 final private static String TEST_ROLE_NAME = "ROLE";
78 final private static String NO_REL_SUFFIX = "-no-rel";
79 /** The perm values. */
80 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
81 /** The role values. */
82 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
83 private Date now = new Date();
86 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
89 protected String getServicePathComponent() {
90 return new RolePermissionClient().getServicePathComponent();
94 public String getServiceName() {
95 return RoleClient.SERVICE_NAME; // RolePermission is a sub-resource of 'roles' resource.
98 private String getRoleName() {
99 return TEST_ROLE_NAME + TEST_MARKER + now.toString();
105 @BeforeClass(alwaysRun = true)
106 public void seedData() {
108 String rn1 = getRoleName();
109 String r1RoleId = createRole(rn1);
110 RoleValue rv1 = new RoleValue();
111 rv1.setRoleId(r1RoleId);
112 rv1.setRoleName(rn1);
113 roleValues.put(rv1.getRoleName(), rv1);
115 String rn2 = getRoleName() + NO_REL_SUFFIX;
116 String r2RoleId = createRole(rn2);
117 RoleValue rv2 = new RoleValue();
118 rv2.setRoleId(r2RoleId);
119 rv2.setRoleName(rn2);
120 roleValues.put(rv2.getRoleName(), rv2);
122 String ra1 = "fooService" + TEST_MARKER;
123 String permId1 = createPermission(ra1, EffectType.PERMIT);
124 PermissionValue pva1 = new PermissionValue();
125 pva1.setResourceName(ra1);
126 pva1.setPermissionId(permId1);
127 permValues.put(pva1.getResourceName(), pva1);
129 String ra2 = "barService" + TEST_MARKER;
130 String permId2 = createPermission(ra1, EffectType.PERMIT);
131 PermissionValue pva2 = new PermissionValue();
132 pva2.setResourceName(ra2);
133 pva2.setPermissionId(permId2);
134 permValues.put(pva2.getResourceName(), pva2);
138 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
141 protected CollectionSpaceClient getClientInstance() {
142 return new RolePermissionClient();
146 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
149 protected AbstractCommonList getAbstractCommonList(
150 ClientResponse<AbstractCommonList> response) {
151 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
152 throw new UnsupportedOperationException();
156 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
158 @Test(dataProvider = "testName")
160 public void readPaginatedList(String testName) throws Exception {
161 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
163 // ---------------------------------------------------------------
164 // CRUD tests : CREATE tests
165 // ---------------------------------------------------------------
168 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
172 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
173 public void create(String testName) throws Exception {
175 if (logger.isDebugEnabled()) {
176 logger.debug(testBanner(testName, CLASS_NAME));
179 // Perform setup, such as initializing the type of service request
180 // (e.g. CREATE, DELETE), its valid and expected status codes, and
181 // its associated HTTP method name (e.g. POST, DELETE).
184 // Submit the request to the service and store the response.
185 RoleValue rv = roleValues.get(getRoleName());
186 PermissionRole permRole = createPermissionRoleInstance(rv,
187 permValues.values(), true, true);
188 RolePermissionClient client = new RolePermissionClient();
189 ClientResponse<Response> res = null;
191 res = client.create(rv.getRoleId(), permRole);
192 int statusCode = res.getStatus();
193 if (logger.isDebugEnabled()) {
194 logger.debug(testName + ": status = " + statusCode);
196 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
197 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
198 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
199 // Store the ID returned from this create operation
200 // for additional tests below.
201 //this is is not important in case of this relationship
202 knownResourceId = extractId(res);
203 if (logger.isDebugEnabled()) {
204 logger.debug(testName + ": knownResourceId=" + knownResourceId);
208 res.releaseConnection();
213 //to not cause uniqueness violation for permRole, createList is removed
215 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
218 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
219 dependsOnMethods = {"create"})
220 public void createList(String testName) throws Exception {
221 //Should this really be empty?
225 // Placeholders until the three tests below can be uncommented.
226 // See Issue CSPACE-401.
228 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
231 public void createWithEmptyEntityBody(String testName) throws Exception {
232 //Should this really be empty?
236 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
239 public void createWithMalformedXml(String testName) throws Exception {
240 //Should this really be empty?
244 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
247 public void createWithWrongXmlSchema(String testName) throws Exception {
248 //Should this really be empty?
251 // ---------------------------------------------------------------
252 // CRUD tests : READ tests
253 // ---------------------------------------------------------------
256 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
259 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
260 dependsOnMethods = {"create"})
261 public void read(String testName) throws Exception {
263 if (logger.isDebugEnabled()) {
264 logger.debug(testBanner(testName, CLASS_NAME));
270 // Submit the request to the service and store the response.
271 RolePermissionClient client = new RolePermissionClient();
272 ClientResponse<PermissionRole> res = null;
274 res = client.read(roleValues.get(getRoleName()).getRoleId());
275 int statusCode = res.getStatus();
277 // Check the status code of the response: does it match
278 // the expected response(s)?
279 if (logger.isDebugEnabled()) {
280 logger.debug(testName + ": status = " + statusCode);
282 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
283 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
284 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
286 PermissionRole output = (PermissionRole) res.getEntity();
287 Assert.assertNotNull(output);
290 res.releaseConnection();
297 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
300 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
301 public void readNonExistent(String testName) throws Exception {
303 if (logger.isDebugEnabled()) {
304 logger.debug(testBanner(testName, CLASS_NAME));
308 setupReadNonExistent();
310 // Submit the request to the service and store the response.
311 RolePermissionClient client = new RolePermissionClient();
312 ClientResponse<PermissionRole> res = null;
315 res = client.read(NON_EXISTENT_ID);
316 int statusCode = res.getStatus();
318 // Check the status code of the response: does it match
319 // the expected response(s)?
320 if (logger.isDebugEnabled()) {
321 logger.debug(testName + ": status = " + statusCode);
323 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
324 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
325 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
328 res.releaseConnection();
333 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
334 public void readNoRelationship(String testName) throws Exception {
336 if (logger.isDebugEnabled()) {
337 logger.debug(testBanner(testName, CLASS_NAME));
341 // Submit the request to the service and store the response.
342 RolePermissionClient client = new RolePermissionClient();
343 ClientResponse<PermissionRole> res = null;
346 res = client.read(roleValues.get(getRoleName() + NO_REL_SUFFIX).getRoleId());
347 int statusCode = res.getStatus();
349 // Check the status code of the response: does it match
350 // the expected response(s)?
351 if (logger.isDebugEnabled()) {
352 logger.debug(testName + ": status = " + statusCode);
354 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
355 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
356 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
357 PermissionRole output = (PermissionRole) res.getEntity();
359 String sOutput = objectAsXmlString(output, PermissionRole.class);
360 if (logger.isDebugEnabled()) {
361 logger.debug(testName + " received " + sOutput);
365 res.releaseConnection();
370 // ---------------------------------------------------------------
371 // CRUD tests : READ_LIST tests
372 // ---------------------------------------------------------------
375 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
378 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
379 dependsOnMethods = {"createList", "read"})
380 public void readList(String testName) throws Exception {
381 //Should this really be empty?
386 // ---------------------------------------------------------------
387 // CRUD tests : UPDATE tests
388 // ---------------------------------------------------------------
391 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
394 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
395 dependsOnMethods = {"read", "readList", "readNonExistent"})
396 public void update(String testName) throws Exception {
397 //Should this really be empty?
401 // Placeholders until the three tests below can be uncommented.
402 // See Issue CSPACE-401.
404 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
407 public void updateWithEmptyEntityBody(String testName) throws Exception {
408 //Should this really be empty?
412 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
415 public void updateWithMalformedXml(String testName) throws Exception {
416 //Should this really be empty?
420 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
423 public void updateWithWrongXmlSchema(String testName) throws Exception {
424 //Should this really be empty?
428 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
431 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
432 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
433 public void updateNonExistent(String testName) throws Exception {
434 //Should this really be empty?
437 // ---------------------------------------------------------------
438 // CRUD tests : DELETE tests
439 // ---------------------------------------------------------------
442 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
445 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
446 dependsOnMethods = {"read"})
447 public void delete(String testName) throws Exception {
449 if (logger.isDebugEnabled()) {
450 logger.debug(testBanner(testName, CLASS_NAME));
456 // Submit the request to the service and store the response.
457 RolePermissionClient client = new RolePermissionClient();
458 RoleValue rv = roleValues.get(getRoleName());
459 ClientResponse<Response> delRes = null;
461 delRes = client.delete(rv.getRoleId());
462 int statusCode = delRes.getStatus();
463 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
464 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
465 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
467 if (delRes != null) {
468 delRes.releaseConnection();
472 // reset for next delete
476 rv = roleValues.get(getRoleName());
477 ClientResponse<PermissionRole> readResponse = client.read(rv.getRoleId());
478 PermissionRole toDelete = readResponse.getEntity();
479 readResponse.releaseConnection();
481 rv = toDelete.getRole().get(0);
482 ClientResponse<Response> res = null;
485 rv.getRoleId(), toDelete);
486 int statusCode = res.getStatus();
487 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
488 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
489 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
492 res.releaseConnection();
499 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
502 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
503 public void deleteNonExistent(String testName) throws Exception {
504 //ignoring this test as the service side returns 200 now even if it does
505 //not find a record in the db
508 // ---------------------------------------------------------------
510 // ---------------------------------------------------------------
513 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
514 public void searchWorkflowDeleted(String testName) throws Exception {
515 // Fixme: null test for now, overriding test in base class
518 // ---------------------------------------------------------------
519 // Utility tests : tests of code used in tests above
520 // ---------------------------------------------------------------
522 * Tests the code for manually submitting data that is used by several
523 * of the methods above.
526 @Test(dependsOnMethods = {"create"}) //FIXME: REM - This is not a test of a submit to the permroles service, but to just authorization/roles
527 public void testSubmitRequest() throws Exception {
529 // Expected status code: 200 OK
530 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
532 // Submit the request to the service and store the response.
533 String method = ServiceRequestType.READ.httpMethodName();
534 String url = getResourceURL(roleValues.get(getRoleName()).getRoleId());
535 int statusCode = submitRequest(method, url);
537 // Check the status code of the response: does it match
538 // the expected response(s)?
539 if (logger.isDebugEnabled()) {
540 logger.debug("testSubmitRequest: url=" + url
541 + " status=" + statusCode);
543 Assert.assertEquals(statusCode, EXPECTED_STATUS);
548 // ---------------------------------------------------------------
549 // Utility methods used by tests above
550 // ---------------------------------------------------------------
552 * create PermissionRole instance
553 * @param rv rolevalue
554 * @param pvs permission value array
557 * @return PermissionRole
559 public static PermissionRole createPermissionRoleInstance(RoleValue rv,
560 Collection<PermissionValue> pvs,
563 List<PermissionValue> pvls = new ArrayList<PermissionValue>();
565 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
566 rv, pvls, usePermId, useRoleId);
567 if (logger.isDebugEnabled()) {
570 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
578 @AfterClass(alwaysRun = true)
580 public void cleanUp() {
582 String noTest = System.getProperty("noTestCleanup");
583 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
584 if (logger.isDebugEnabled()) {
585 logger.debug("Skipping Cleanup phase ...");
589 if (logger.isDebugEnabled()) {
590 logger.debug("Cleaning up temporary resources created for testing ...");
592 for (PermissionValue pv : permValues.values()) {
593 deletePermission(pv.getPermissionId());
595 for (RoleValue rv : roleValues.values()) {
596 deleteRole(rv.getRoleId());
601 * Creates the permission.
603 * @param resName the res name
604 * @param effect the effect
607 private String createPermission(String resName, EffectType effect) {
609 PermissionClient permClient = new PermissionClient();
610 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
611 Permission permission = PermissionFactory.createPermissionInstance(resName,
612 "default permissions for " + resName,
613 actions, effect, true, true, true);
614 ClientResponse<Response> res = null;
617 res = permClient.create(permission);
618 int statusCode = res.getStatus();
619 if (logger.isDebugEnabled()) {
620 logger.debug("createPermission: resName=" + resName
621 + " status = " + statusCode);
623 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
624 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
625 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
629 res.releaseConnection();
638 * @param permId the perm id
640 private void deletePermission(String permId) {
642 PermissionClient permClient = new PermissionClient();
644 ClientResponse<Response> res = null;
646 res = permClient.delete(permId);
647 int statusCode = res.getStatus();
648 if (logger.isDebugEnabled()) {
649 logger.debug("deletePermission: delete permission id="
650 + permId + " status=" + statusCode);
652 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
653 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
654 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
658 res.releaseConnection();
666 * @param roleName the role name
669 private String createRole(String roleName) {
671 RoleClient roleClient = new RoleClient();
673 Role role = RoleFactory.createRoleInstance(roleName,
674 roleName, //the display name
675 "role for " + roleName, true);
676 role.setRoleGroup("something");
677 ClientResponse<Response> res = null;
680 res = roleClient.create(role);
681 int statusCode = res.getStatus();
682 if (logger.isDebugEnabled()) {
683 logger.debug("createRole: name=" + roleName
684 + " status = " + statusCode);
686 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
687 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
688 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
692 res.releaseConnection();
701 * @param roleId the role id
703 private void deleteRole(String roleId) {
705 RoleClient roleClient = new RoleClient();
706 ClientResponse<Response> res = null;
708 res = roleClient.delete(roleId);
709 int statusCode = res.getStatus();
710 if (logger.isDebugEnabled()) {
711 logger.debug("deleteRole: delete role id=" + roleId
712 + " status=" + statusCode);
714 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
715 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
716 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
718 res.releaseConnection();