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.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;
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();
85 * This method is called only by the parent class, AbstractServiceTestImpl
89 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
92 protected String getServicePathComponent() {
93 return new RolePermissionClient().getServicePathComponent();
96 private String getRoleName() {
97 return TEST_ROLE_NAME + TEST_MARKER + now.toString();
103 @BeforeClass(alwaysRun = true)
104 public void seedData() {
106 String rn1 = getRoleName();
107 String r1RoleId = createRole(rn1);
108 RoleValue rv1 = new RoleValue();
109 rv1.setRoleId(r1RoleId);
110 rv1.setRoleName(rn1);
111 roleValues.put(rv1.getRoleName(), rv1);
113 String rn2 = getRoleName() + NO_REL_SUFFIX;
114 String r2RoleId = createRole(rn2);
115 RoleValue rv2 = new RoleValue();
116 rv2.setRoleId(r2RoleId);
117 rv2.setRoleName(rn2);
118 roleValues.put(rv2.getRoleName(), rv2);
120 String ra1 = "fooService" + TEST_MARKER;
121 String permId1 = createPermission(ra1, EffectType.PERMIT);
122 PermissionValue pva1 = new PermissionValue();
123 pva1.setResourceName(ra1);
124 pva1.setPermissionId(permId1);
125 permValues.put(pva1.getResourceName(), pva1);
127 String ra2 = "barService" + TEST_MARKER;
128 String permId2 = createPermission(ra1, EffectType.PERMIT);
129 PermissionValue pva2 = new PermissionValue();
130 pva2.setResourceName(ra2);
131 pva2.setPermissionId(permId2);
132 permValues.put(pva2.getResourceName(), pva2);
136 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
139 protected CollectionSpaceClient getClientInstance() {
140 return new RolePermissionClient();
144 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
147 protected AbstractCommonList getAbstractCommonList(
148 ClientResponse<AbstractCommonList> response) {
149 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
150 throw new UnsupportedOperationException();
154 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
156 @Test(dataProvider = "testName")
158 public void readPaginatedList(String testName) throws Exception {
159 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
161 // ---------------------------------------------------------------
162 // CRUD tests : CREATE tests
163 // ---------------------------------------------------------------
166 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
170 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
171 public void create(String testName) throws Exception {
173 if (logger.isDebugEnabled()) {
174 logger.debug(testBanner(testName, CLASS_NAME));
177 // Perform setup, such as initializing the type of service request
178 // (e.g. CREATE, DELETE), its valid and expected status codes, and
179 // its associated HTTP method name (e.g. POST, DELETE).
182 // Submit the request to the service and store the response.
183 RoleValue rv = roleValues.get(getRoleName());
184 PermissionRole permRole = createPermissionRoleInstance(rv,
185 permValues.values(), true, true);
186 RolePermissionClient client = new RolePermissionClient();
187 ClientResponse<Response> res = null;
189 res = client.create(rv.getRoleId(), permRole);
191 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;
275 roleValues.get(getRoleName()).getRoleId(), "123");
276 int statusCode = res.getStatus();
278 // Check the status code of the response: does it match
279 // the expected response(s)?
280 if (logger.isDebugEnabled()) {
281 logger.debug(testName + ": status = " + statusCode);
283 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
284 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
285 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
287 PermissionRole output = (PermissionRole) res.getEntity();
288 Assert.assertNotNull(output);
291 res.releaseConnection();
298 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
301 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
302 public void readNonExistent(String testName) throws Exception {
304 if (logger.isDebugEnabled()) {
305 logger.debug(testBanner(testName, CLASS_NAME));
309 setupReadNonExistent();
311 // Submit the request to the service and store the response.
312 RolePermissionClient client = new RolePermissionClient();
313 ClientResponse<PermissionRole> res = null;
316 res = client.read(NON_EXISTENT_ID, "123");
317 int statusCode = res.getStatus();
319 // Check the status code of the response: does it match
320 // the expected response(s)?
321 if (logger.isDebugEnabled()) {
322 logger.debug(testName + ": status = " + statusCode);
324 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
325 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
326 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
329 res.releaseConnection();
334 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
335 public void readNoRelationship(String testName) throws Exception {
337 if (logger.isDebugEnabled()) {
338 logger.debug(testBanner(testName, CLASS_NAME));
342 // Submit the request to the service and store the response.
343 RolePermissionClient client = new RolePermissionClient();
344 ClientResponse<PermissionRole> res = null;
347 res = client.read(roleValues.get(getRoleName() + NO_REL_SUFFIX).getRoleId(), "123");
348 int statusCode = res.getStatus();
350 // Check the status code of the response: does it match
351 // the expected response(s)?
352 if (logger.isDebugEnabled()) {
353 logger.debug(testName + ": status = " + statusCode);
355 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
356 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
357 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
358 PermissionRole output = (PermissionRole) res.getEntity();
360 String sOutput = objectAsXmlString(output, PermissionRole.class);
361 if(logger.isDebugEnabled()) {
362 logger.debug(testName + " received " + sOutput);
366 res.releaseConnection();
371 // ---------------------------------------------------------------
372 // CRUD tests : READ_LIST tests
373 // ---------------------------------------------------------------
376 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
379 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
380 dependsOnMethods = {"createList", "read"})
381 public void readList(String testName) throws Exception {
382 //Should this really be empty?
387 // ---------------------------------------------------------------
388 // CRUD tests : UPDATE tests
389 // ---------------------------------------------------------------
392 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
395 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
396 dependsOnMethods = {"read", "readList", "readNonExistent"})
397 public void update(String testName) throws Exception {
398 //Should this really be empty?
402 // Placeholders until the three tests below can be uncommented.
403 // See Issue CSPACE-401.
405 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
408 public void updateWithEmptyEntityBody(String testName) throws Exception {
409 //Should this really be empty?
413 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
416 public void updateWithMalformedXml(String testName) throws Exception {
417 //Should this really be empty?
421 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
424 public void updateWithWrongXmlSchema(String testName) throws Exception {
425 //Should this really be empty?
429 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
432 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
433 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
434 public void updateNonExistent(String testName) throws Exception {
435 //Should this really be empty?
438 // ---------------------------------------------------------------
439 // CRUD tests : DELETE tests
440 // ---------------------------------------------------------------
443 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
446 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
447 dependsOnMethods = {"read"})
448 public void delete(String testName) throws Exception {
450 if (logger.isDebugEnabled()) {
451 logger.debug(testBanner(testName, CLASS_NAME));
457 // Submit the request to the service and store the response.
458 RolePermissionClient client = new RolePermissionClient();
459 ClientResponse<Response> res = null;
462 roleValues.get(getRoleName()).getRoleId(), "123");
463 int statusCode = res.getStatus();
465 // Check the status code of the response: does it match
466 // the expected response(s)?
467 if (logger.isDebugEnabled()) {
468 logger.debug(testName + ": status = " + statusCode);
470 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
471 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
472 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
475 res.releaseConnection();
482 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
485 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
486 public void deleteNonExistent(String testName) throws Exception {
487 //ignoring this test as the service side returns 200 now even if it does
488 //not find a record in the db
491 // ---------------------------------------------------------------
492 // Utility tests : tests of code used in tests above
493 // ---------------------------------------------------------------
495 * Tests the code for manually submitting data that is used by several
496 * of the methods above.
499 @Test(dependsOnMethods = {"create"})
500 public void testSubmitRequest() throws Exception {
502 // Expected status code: 200 OK
503 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
505 // Submit the request to the service and store the response.
506 String method = ServiceRequestType.READ.httpMethodName();
507 String url = getResourceURL(roleValues.get(getRoleName()).getRoleId());
508 int statusCode = submitRequest(method, url);
510 // Check the status code of the response: does it match
511 // the expected response(s)?
512 if (logger.isDebugEnabled()) {
513 logger.debug("testSubmitRequest: url=" + url
514 + " status=" + statusCode);
516 Assert.assertEquals(statusCode, EXPECTED_STATUS);
521 // ---------------------------------------------------------------
522 // Utility methods used by tests above
523 // ---------------------------------------------------------------
525 * create PermissionRole instance
526 * @param rv rolevalue
527 * @param pvs permission value array
530 * @return PermissionRole
532 public static PermissionRole createPermissionRoleInstance(RoleValue rv,
533 Collection<PermissionValue> pvs,
536 List<PermissionValue> pvls = new ArrayList<PermissionValue>();
538 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
539 rv, pvls, usePermId, useRoleId);
540 if (logger.isDebugEnabled()) {
541 logger.debug("to be created, permRole");
542 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
550 @AfterClass(alwaysRun = true)
552 public void cleanUp() {
554 String noTest = System.getProperty("noTestCleanup");
555 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
556 if (logger.isDebugEnabled()) {
557 logger.debug("Skipping Cleanup phase ...");
561 if (logger.isDebugEnabled()) {
562 logger.debug("Cleaning up temporary resources created for testing ...");
565 RolePermissionClient client = new RolePermissionClient();
566 for (String resourceId : allResourceIdsCreated) {
568 ClientResponse<Response> res = client.delete(resourceId, "123");
569 int statusCode = res.getStatus();
571 if (logger.isDebugEnabled()) {
572 logger.debug("cleanup: delete relationships for permission id="
573 + resourceId + " status=" + statusCode);
575 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
576 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
577 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
579 res.releaseConnection();
583 for (PermissionValue pv : permValues.values()) {
584 deletePermission(pv.getPermissionId());
586 for (RoleValue rv : roleValues.values()) {
587 deleteRole(rv.getRoleId());
592 * Creates the permission.
594 * @param resName the res name
595 * @param effect the effect
598 private String createPermission(String resName, EffectType effect) {
600 PermissionClient permClient = new PermissionClient();
601 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
602 Permission permission = PermissionFactory.createPermissionInstance(resName,
603 "default permissions for " + resName,
604 actions, effect, true, true, true);
605 ClientResponse<Response> res = null;
608 res = permClient.create(permission);
609 int statusCode = res.getStatus();
610 if (logger.isDebugEnabled()) {
611 logger.debug("createPermission: resName=" + resName
612 + " status = " + statusCode);
614 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
615 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
616 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
620 res.releaseConnection();
629 * @param permId the perm id
631 private void deletePermission(String permId) {
633 PermissionClient permClient = new PermissionClient();
635 ClientResponse<Response> res = null;
637 res = permClient.delete(permId);
638 int statusCode = res.getStatus();
639 if (logger.isDebugEnabled()) {
640 logger.debug("deletePermission: delete permission id="
641 + permId + " status=" + statusCode);
643 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
644 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
645 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
649 res.releaseConnection();
657 * @param roleName the role name
660 private String createRole(String roleName) {
662 RoleClient roleClient = new RoleClient();
664 Role role = RoleFactory.createRoleInstance(roleName,
665 "role for " + roleName, true);
666 role.setRoleGroup("something");
667 ClientResponse<Response> res = null;
670 res = roleClient.create(role);
671 int statusCode = res.getStatus();
672 if (logger.isDebugEnabled()) {
673 logger.debug("createRole: name=" + roleName
674 + " status = " + statusCode);
676 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
677 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
678 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
682 res.releaseConnection();
691 * @param roleId the role id
693 private void deleteRole(String roleId) {
695 RoleClient roleClient = new RoleClient();
696 ClientResponse<Response> res = null;
698 res = roleClient.delete(roleId);
699 int statusCode = res.getStatus();
700 if (logger.isDebugEnabled()) {
701 logger.debug("deleteRole: delete role id=" + roleId
702 + " status=" + statusCode);
704 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
705 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
706 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
708 res.releaseConnection();