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 assertStatusCode(res, testName);
276 PermissionRole output = (PermissionRole) res.getEntity();
277 Assert.assertNotNull(output);
280 res.releaseConnection();
287 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
290 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
291 public void readNonExistent(String testName) throws Exception {
293 if (logger.isDebugEnabled()) {
294 logger.debug(testBanner(testName, CLASS_NAME));
298 setupReadNonExistent();
300 // Submit the request to the service and store the response.
301 RolePermissionClient client = new RolePermissionClient();
302 ClientResponse<PermissionRole> res = null;
305 res = client.read(NON_EXISTENT_ID);
306 int statusCode = res.getStatus();
308 // Check the status code of the response: does it match
309 // the expected response(s)?
310 if (logger.isDebugEnabled()) {
311 logger.debug(testName + ": status = " + statusCode);
313 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
314 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
315 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
318 res.releaseConnection();
323 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
324 public void readNoRelationship(String testName) throws Exception {
326 if (logger.isDebugEnabled()) {
327 logger.debug(testBanner(testName, CLASS_NAME));
331 // Submit the request to the service and store the response.
332 RolePermissionClient client = new RolePermissionClient();
333 ClientResponse<PermissionRole> res = null;
336 res = client.read(roleValues.get(getRoleName() + NO_REL_SUFFIX).getRoleId());
337 assertStatusCode(res, testName);
338 PermissionRole output = (PermissionRole) res.getEntity();
340 String sOutput = objectAsXmlString(output, PermissionRole.class);
341 if (logger.isDebugEnabled()) {
342 logger.debug(testName + " received " + sOutput);
346 res.releaseConnection();
351 // ---------------------------------------------------------------
352 // CRUD tests : READ_LIST tests
353 // ---------------------------------------------------------------
356 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
359 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
360 dependsOnMethods = {"createList", "read"})
361 public void readList(String testName) throws Exception {
362 //Should this really be empty?
367 // ---------------------------------------------------------------
368 // CRUD tests : UPDATE tests
369 // ---------------------------------------------------------------
372 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
375 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
376 dependsOnMethods = {"read", "readList", "readNonExistent"})
377 public void update(String testName) throws Exception {
378 //Should this really be empty?
382 // Placeholders until the three tests below can be uncommented.
383 // See Issue CSPACE-401.
385 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
388 public void updateWithEmptyEntityBody(String testName) throws Exception {
389 //Should this really be empty?
393 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
396 public void updateWithMalformedXml(String testName) throws Exception {
397 //Should this really be empty?
401 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
404 public void updateWithWrongXmlSchema(String testName) throws Exception {
405 //Should this really be empty?
409 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
412 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
413 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
414 public void updateNonExistent(String testName) throws Exception {
415 //Should this really be empty?
418 // ---------------------------------------------------------------
419 // CRUD tests : DELETE tests
420 // ---------------------------------------------------------------
423 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
426 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
427 dependsOnMethods = {"read"})
428 public void delete(String testName) throws Exception {
430 if (logger.isDebugEnabled()) {
431 logger.debug(testBanner(testName, CLASS_NAME));
437 // Submit the request to the service and store the response.
438 RolePermissionClient client = new RolePermissionClient();
439 RoleValue rv = roleValues.get(getRoleName());
440 ClientResponse<Response> delRes = null;
442 delRes = client.delete(rv.getRoleId());
443 int statusCode = delRes.getStatus();
444 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
445 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
446 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
448 if (delRes != null) {
449 delRes.releaseConnection();
453 // reset for next delete
457 rv = roleValues.get(getRoleName());
458 ClientResponse<PermissionRole> readResponse = client.read(rv.getRoleId());
459 PermissionRole toDelete = readResponse.getEntity();
460 readResponse.releaseConnection();
462 rv = toDelete.getRole().get(0);
463 ClientResponse<Response> res = null;
466 rv.getRoleId(), toDelete);
467 int statusCode = res.getStatus();
468 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
469 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
470 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
473 res.releaseConnection();
480 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
483 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
484 public void deleteNonExistent(String testName) throws Exception {
485 //ignoring this test as the service side returns 200 now even if it does
486 //not find a record in the db
489 // ---------------------------------------------------------------
491 // ---------------------------------------------------------------
494 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
495 public void searchWorkflowDeleted(String testName) throws Exception {
496 // Fixme: null test for now, overriding test in base class
499 // ---------------------------------------------------------------
500 // Utility tests : tests of code used in tests above
501 // ---------------------------------------------------------------
503 * Tests the code for manually submitting data that is used by several
504 * of the methods above.
507 @Test(dependsOnMethods = {"create"}) //FIXME: REM - This is not a test of a submit to the permroles service, but to just authorization/roles
508 public void testSubmitRequest() throws Exception {
510 // Expected status code: 200 OK
511 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
513 // Submit the request to the service and store the response.
514 String method = ServiceRequestType.READ.httpMethodName();
515 String url = getResourceURL(roleValues.get(getRoleName()).getRoleId());
516 int statusCode = submitRequest(method, url);
518 // Check the status code of the response: does it match
519 // the expected response(s)?
520 if (logger.isDebugEnabled()) {
521 logger.debug("testSubmitRequest: url=" + url
522 + " status=" + statusCode);
524 Assert.assertEquals(statusCode, EXPECTED_STATUS);
529 // ---------------------------------------------------------------
530 // Utility methods used by tests above
531 // ---------------------------------------------------------------
533 * create PermissionRole instance
534 * @param rv rolevalue
535 * @param pvs permission value array
538 * @return PermissionRole
540 public static PermissionRole createPermissionRoleInstance(RoleValue rv,
541 Collection<PermissionValue> pvs,
544 List<PermissionValue> pvls = new ArrayList<PermissionValue>();
546 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
547 rv, pvls, usePermId, useRoleId);
548 if (logger.isDebugEnabled()) {
551 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
559 @AfterClass(alwaysRun = true)
561 public void cleanUp() {
563 String noTest = System.getProperty("noTestCleanup");
564 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
565 if (logger.isDebugEnabled()) {
566 logger.debug("Skipping Cleanup phase ...");
570 if (logger.isDebugEnabled()) {
571 logger.debug("Cleaning up temporary resources created for testing ...");
573 for (PermissionValue pv : permValues.values()) {
574 deletePermission(pv.getPermissionId());
576 for (RoleValue rv : roleValues.values()) {
577 deleteRole(rv.getRoleId());
582 * Creates the permission.
584 * @param resName the res name
585 * @param effect the effect
588 private String createPermission(String resName, EffectType effect) {
590 PermissionClient permClient = new PermissionClient();
591 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
592 Permission permission = PermissionFactory.createPermissionInstance(resName,
593 "default permissions for " + resName,
594 actions, effect, true, true, true);
595 ClientResponse<Response> res = null;
598 res = permClient.create(permission);
599 int statusCode = res.getStatus();
600 if (logger.isDebugEnabled()) {
601 logger.debug("createPermission: resName=" + resName
602 + " status = " + statusCode);
604 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
605 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
606 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
610 res.releaseConnection();
619 * @param permId the perm id
621 private void deletePermission(String permId) {
623 PermissionClient permClient = new PermissionClient();
625 ClientResponse<Response> res = null;
627 res = permClient.delete(permId);
628 int statusCode = res.getStatus();
629 if (logger.isDebugEnabled()) {
630 logger.debug("deletePermission: delete permission id="
631 + permId + " status=" + statusCode);
633 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
634 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
635 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
639 res.releaseConnection();
647 * @param roleName the role name
650 private String createRole(String roleName) {
652 RoleClient roleClient = new RoleClient();
654 Role role = RoleFactory.createRoleInstance(roleName,
655 roleName, //the display name
656 "role for " + roleName, true);
657 role.setRoleGroup("something");
658 ClientResponse<Response> res = null;
661 res = roleClient.create(role);
662 int statusCode = res.getStatus();
663 if (logger.isDebugEnabled()) {
664 logger.debug("createRole: name=" + roleName
665 + " status = " + statusCode);
667 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
668 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
669 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
673 res.releaseConnection();
682 * @param roleId the role id
684 private void deleteRole(String roleId) {
686 RoleClient roleClient = new RoleClient();
687 ClientResponse<Response> res = null;
689 res = roleClient.delete(roleId);
690 int statusCode = res.getStatus();
691 if (logger.isDebugEnabled()) {
692 logger.debug("deleteRole: delete role id=" + roleId
693 + " status=" + statusCode);
695 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
696 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
697 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
699 res.releaseConnection();