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;
30 import org.collectionspace.services.authorization.EffectType;
32 import org.collectionspace.services.authorization.Permission;
33 import org.collectionspace.services.authorization.PermissionAction;
34 import org.collectionspace.services.authorization.PermissionRole;
35 import org.collectionspace.services.authorization.PermissionValue;
36 import org.collectionspace.services.authorization.Role;
37 import org.collectionspace.services.authorization.RoleValue;
38 import org.collectionspace.services.client.CollectionSpaceClient;
39 import org.collectionspace.services.client.PermissionClient;
40 import org.collectionspace.services.client.PermissionFactory;
41 import org.collectionspace.services.client.RolePermissionClient;
42 import org.collectionspace.services.client.PermissionRoleFactory;
43 import org.collectionspace.services.client.RoleClient;
44 import org.collectionspace.services.client.RoleFactory;
45 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
46 import org.collectionspace.services.client.test.ServiceRequestType;
47 import org.collectionspace.services.jaxb.AbstractCommonList;
48 import org.jboss.resteasy.client.ClientResponse;
50 import org.testng.Assert;
51 import org.testng.annotations.Test;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.testng.annotations.AfterClass;
56 import org.testng.annotations.BeforeClass;
59 * PermissionServiceTest, carries out tests against a
60 * deployed and running Permission, Role and PermissionRole Services.
62 * $LastChangedRevision: 917 $
63 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
65 public class RolePermissionServiceTest extends AbstractServiceTestImpl {
67 /** The Constant logger. */
68 private final static String CLASS_NAME = RolePermissionServiceTest.class.getName();
69 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 /** The perm values. */
79 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
80 /** The role values. */
81 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
83 * This method is called only by the parent class, AbstractServiceTestImpl
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
90 protected String getServicePathComponent() {
91 return new RolePermissionClient().getServicePathComponent();
97 @BeforeClass(alwaysRun = true)
98 public void seedData() {
100 String rn1 = TEST_ROLE_NAME + TEST_MARKER;
101 String r1RoleId = createRole(rn1);
102 RoleValue rv1 = new RoleValue();
103 rv1.setRoleId(r1RoleId);
104 rv1.setRoleName(rn1);
105 roleValues.put(rv1.getRoleName(), rv1);
107 String ra1 = "fooService" + TEST_MARKER;
108 String permId1 = createPermission(ra1, EffectType.PERMIT);
109 PermissionValue pva1 = new PermissionValue();
110 pva1.setResourceName(ra1);
111 pva1.setPermissionId(permId1);
112 permValues.put(pva1.getResourceName(), pva1);
114 String ra2 = "barService" + TEST_MARKER;
115 String permId2 = createPermission(ra1, EffectType.PERMIT);
116 PermissionValue pva2 = new PermissionValue();
117 pva2.setResourceName(ra2);
118 pva2.setPermissionId(permId2);
119 permValues.put(pva2.getResourceName(), pva2);
123 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
126 protected CollectionSpaceClient getClientInstance() {
127 return new RolePermissionClient();
131 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
134 protected AbstractCommonList getAbstractCommonList(
135 ClientResponse<AbstractCommonList> response) {
136 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
137 throw new UnsupportedOperationException();
141 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
143 @Test(dataProvider = "testName")
145 public void readPaginatedList(String testName) throws Exception {
146 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
148 // ---------------------------------------------------------------
149 // CRUD tests : CREATE tests
150 // ---------------------------------------------------------------
153 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
157 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
158 public void create(String testName) throws Exception {
160 if (logger.isDebugEnabled()) {
161 logger.debug(testBanner(testName, CLASS_NAME));
163 // Perform setup, such as initializing the type of service request
164 // (e.g. CREATE, DELETE), its valid and expected status codes, and
165 // its associated HTTP method name (e.g. POST, DELETE).
168 // Submit the request to the service and store the response.
169 RoleValue rv = roleValues.get(TEST_ROLE_NAME + TEST_MARKER);
170 PermissionRole permRole = createPermissionRoleInstance(rv,
171 permValues.values(), true, true);
172 RolePermissionClient client = new RolePermissionClient();
173 ClientResponse<Response> res = null;
175 res = client.create(rv.getRoleId(), permRole);
177 int statusCode = res.getStatus();
179 if (logger.isDebugEnabled()) {
180 logger.debug(testName + ": status = " + statusCode);
182 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
183 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
184 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
185 // Store the ID returned from this create operation
186 // for additional tests below.
187 //this is is not important in case of this relationship
188 knownResourceId = extractId(res);
189 if (logger.isDebugEnabled()) {
190 logger.debug(testName + ": knownResourceId=" + knownResourceId);
194 res.releaseConnection();
199 //to not cause uniqueness violation for permRole, createList is removed
201 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
204 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
205 dependsOnMethods = {"create"})
206 public void createList(String testName) throws Exception {
207 //Should this really be empty?
211 // Placeholders until the three tests below can be uncommented.
212 // See Issue CSPACE-401.
214 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
217 public void createWithEmptyEntityBody(String testName) throws Exception {
218 //Should this really be empty?
222 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
225 public void createWithMalformedXml(String testName) throws Exception {
226 //Should this really be empty?
230 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
233 public void createWithWrongXmlSchema(String testName) throws Exception {
234 //Should this really be empty?
237 // ---------------------------------------------------------------
238 // CRUD tests : READ tests
239 // ---------------------------------------------------------------
242 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
245 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
246 dependsOnMethods = {"create"})
247 public void read(String testName) throws Exception {
249 if (logger.isDebugEnabled()) {
250 logger.debug(testBanner(testName, CLASS_NAME));
255 // Submit the request to the service and store the response.
256 RolePermissionClient client = new RolePermissionClient();
257 ClientResponse<PermissionRole> res = null;
260 roleValues.get(TEST_ROLE_NAME + TEST_MARKER).getRoleId(), "123");
261 int statusCode = res.getStatus();
263 // Check the status code of the response: does it match
264 // the expected response(s)?
265 if (logger.isDebugEnabled()) {
266 logger.debug(testName + ": status = " + statusCode);
268 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
269 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
270 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
272 PermissionRole output = (PermissionRole) res.getEntity();
273 Assert.assertNotNull(output);
276 res.releaseConnection();
283 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
286 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
287 public void readNonExistent(String testName) throws Exception {
289 if (logger.isDebugEnabled()) {
290 logger.debug(testBanner(testName, CLASS_NAME));
293 setupReadNonExistent();
295 // Submit the request to the service and store the response.
296 RolePermissionClient client = new RolePermissionClient();
297 ClientResponse<PermissionRole> res = null;
300 res = client.read(NON_EXISTENT_ID, "123");
301 int statusCode = res.getStatus();
303 // Check the status code of the response: does it match
304 // the expected response(s)?
305 if (logger.isDebugEnabled()) {
306 logger.debug(testName + ": status = " + statusCode);
308 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
309 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
310 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
313 res.releaseConnection();
318 // ---------------------------------------------------------------
319 // CRUD tests : READ_LIST tests
320 // ---------------------------------------------------------------
323 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
326 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
327 dependsOnMethods = {"createList", "read"})
328 public void readList(String testName) throws Exception {
329 //Should this really be empty?
334 // ---------------------------------------------------------------
335 // CRUD tests : UPDATE tests
336 // ---------------------------------------------------------------
339 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
342 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
343 dependsOnMethods = {"read", "readList", "readNonExistent"})
344 public void update(String testName) throws Exception {
345 //Should this really be empty?
349 // Placeholders until the three tests below can be uncommented.
350 // See Issue CSPACE-401.
352 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
355 public void updateWithEmptyEntityBody(String testName) throws Exception {
356 //Should this really be empty?
360 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
363 public void updateWithMalformedXml(String testName) throws Exception {
364 //Should this really be empty?
368 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
371 public void updateWithWrongXmlSchema(String testName) throws Exception {
372 //Should this really be empty?
376 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
379 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
380 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
381 public void updateNonExistent(String testName) throws Exception {
382 //Should this really be empty?
385 // ---------------------------------------------------------------
386 // CRUD tests : DELETE tests
387 // ---------------------------------------------------------------
390 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
393 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
394 dependsOnMethods = {"read"})
395 public void delete(String testName) throws Exception {
397 if (logger.isDebugEnabled()) {
398 logger.debug(testBanner(testName, CLASS_NAME));
403 // Submit the request to the service and store the response.
404 RolePermissionClient client = new RolePermissionClient();
405 ClientResponse<Response> res = null;
408 roleValues.get(TEST_ROLE_NAME + TEST_MARKER).getRoleId(), "123");
409 int statusCode = res.getStatus();
411 // Check the status code of the response: does it match
412 // the expected response(s)?
413 if (logger.isDebugEnabled()) {
414 logger.debug(testName + ": status = " + statusCode);
416 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
417 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
418 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
421 res.releaseConnection();
428 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
431 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
432 public void deleteNonExistent(String testName) throws Exception {
433 //ignoring this test as the service side returns 200 now even if it does
434 //not find a record in the db
437 // ---------------------------------------------------------------
438 // Utility tests : tests of code used in tests above
439 // ---------------------------------------------------------------
441 * Tests the code for manually submitting data that is used by several
442 * of the methods above.
445 @Test(dependsOnMethods = {"create"})
446 public void testSubmitRequest() throws Exception {
448 // Expected status code: 200 OK
449 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
451 // Submit the request to the service and store the response.
452 String method = ServiceRequestType.READ.httpMethodName();
453 String url = getResourceURL(roleValues.get(TEST_ROLE_NAME + TEST_MARKER).getRoleId());
454 int statusCode = submitRequest(method, url);
456 // Check the status code of the response: does it match
457 // the expected response(s)?
458 if (logger.isDebugEnabled()) {
459 logger.debug("testSubmitRequest: url=" + url
460 + " status=" + statusCode);
462 Assert.assertEquals(statusCode, EXPECTED_STATUS);
467 // ---------------------------------------------------------------
468 // Utility methods used by tests above
469 // ---------------------------------------------------------------
471 * create PermissionRole instance
472 * @param rv rolevalue
473 * @param pvs permission value array
476 * @return PermissionRole
478 public static PermissionRole createPermissionRoleInstance(RoleValue rv,
479 Collection<PermissionValue> pvs,
482 List<PermissionValue> pvls = new ArrayList<PermissionValue>();
484 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
485 rv, pvls, usePermId, useRoleId);
486 if (logger.isDebugEnabled()) {
487 logger.debug("to be created, permRole");
488 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
496 @AfterClass(alwaysRun = true)
498 public void cleanUp() {
500 String noTest = System.getProperty("noTestCleanup");
501 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
502 if (logger.isDebugEnabled()) {
503 logger.debug("Skipping Cleanup phase ...");
507 if (logger.isDebugEnabled()) {
508 logger.debug("Cleaning up temporary resources created for testing ...");
511 RolePermissionClient client = new RolePermissionClient();
512 for (String resourceId : allResourceIdsCreated) {
514 ClientResponse<Response> res = client.delete(resourceId, "123");
515 int statusCode = res.getStatus();
517 if (logger.isDebugEnabled()) {
518 logger.debug("cleanup: delete relationships for permission id="
519 + resourceId + " status=" + statusCode);
521 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
522 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
523 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
525 res.releaseConnection();
529 for (PermissionValue pv : permValues.values()) {
530 deletePermission(pv.getPermissionId());
532 for (RoleValue rv : roleValues.values()) {
533 deleteRole(rv.getRoleId());
538 * Creates the permission.
540 * @param resName the res name
541 * @param effect the effect
544 private String createPermission(String resName, EffectType effect) {
546 PermissionClient permClient = new PermissionClient();
547 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
548 Permission permission = PermissionFactory.createPermissionInstance(resName,
549 "default permissions for " + resName,
550 actions, effect, true, true, true);
551 ClientResponse<Response> res = null;
554 res = permClient.create(permission);
555 int statusCode = res.getStatus();
556 if (logger.isDebugEnabled()) {
557 logger.debug("createPermission: resName=" + resName
558 + " status = " + statusCode);
560 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
561 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
562 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
566 res.releaseConnection();
575 * @param permId the perm id
577 private void deletePermission(String permId) {
579 PermissionClient permClient = new PermissionClient();
581 ClientResponse<Response> res = null;
583 res = permClient.delete(permId);
584 int statusCode = res.getStatus();
585 if (logger.isDebugEnabled()) {
586 logger.debug("deletePermission: delete permission id="
587 + permId + " status=" + statusCode);
589 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
590 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
591 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
595 res.releaseConnection();
603 * @param roleName the role name
606 private String createRole(String roleName) {
608 RoleClient roleClient = new RoleClient();
610 Role role = RoleFactory.createRoleInstance(roleName,
611 "role for " + roleName, true);
612 ClientResponse<Response> res = null;
615 res = roleClient.create(role);
616 int statusCode = res.getStatus();
617 if (logger.isDebugEnabled()) {
618 logger.debug("createRole: name=" + roleName
619 + " status = " + statusCode);
621 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
622 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
623 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
627 res.releaseConnection();
636 * @param roleId the role id
638 private void deleteRole(String roleId) {
640 RoleClient roleClient = new RoleClient();
641 ClientResponse<Response> res = null;
643 res = roleClient.delete(roleId);
644 int statusCode = res.getStatus();
645 if (logger.isDebugEnabled()) {
646 logger.debug("deleteRole: delete role id=" + roleId
647 + " status=" + statusCode);
649 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
650 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
651 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
653 res.releaseConnection();