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);
190 int statusCode = res.getStatus();
191 if (logger.isDebugEnabled()) {
192 logger.debug(testName + ": status = " + statusCode);
194 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
195 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
196 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
197 // Store the ID returned from this create operation
198 // for additional tests below.
199 //this is is not important in case of this relationship
200 knownResourceId = extractId(res);
201 if (logger.isDebugEnabled()) {
202 logger.debug(testName + ": knownResourceId=" + knownResourceId);
206 res.releaseConnection();
211 //to not cause uniqueness violation for permRole, createList is removed
213 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
216 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
217 dependsOnMethods = {"create"})
218 public void createList(String testName) throws Exception {
219 //Should this really be empty?
223 // Placeholders until the three tests below can be uncommented.
224 // See Issue CSPACE-401.
226 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
229 public void createWithEmptyEntityBody(String testName) throws Exception {
230 //Should this really be empty?
234 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
237 public void createWithMalformedXml(String testName) throws Exception {
238 //Should this really be empty?
242 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
245 public void createWithWrongXmlSchema(String testName) throws Exception {
246 //Should this really be empty?
249 // ---------------------------------------------------------------
250 // CRUD tests : READ tests
251 // ---------------------------------------------------------------
254 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
257 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
258 dependsOnMethods = {"create"})
259 public void read(String testName) throws Exception {
261 if (logger.isDebugEnabled()) {
262 logger.debug(testBanner(testName, CLASS_NAME));
268 // Submit the request to the service and store the response.
269 RolePermissionClient client = new RolePermissionClient();
270 ClientResponse<PermissionRole> res = null;
273 roleValues.get(getRoleName()).getRoleId(), "123");
274 int statusCode = res.getStatus();
276 // Check the status code of the response: does it match
277 // the expected response(s)?
278 if (logger.isDebugEnabled()) {
279 logger.debug(testName + ": status = " + statusCode);
281 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
282 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
283 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
285 PermissionRole output = (PermissionRole) res.getEntity();
286 Assert.assertNotNull(output);
289 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));
307 setupReadNonExistent();
309 // Submit the request to the service and store the response.
310 RolePermissionClient client = new RolePermissionClient();
311 ClientResponse<PermissionRole> res = null;
314 res = client.read(NON_EXISTENT_ID, "123");
315 int statusCode = res.getStatus();
317 // Check the status code of the response: does it match
318 // the expected response(s)?
319 if (logger.isDebugEnabled()) {
320 logger.debug(testName + ": status = " + statusCode);
322 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
323 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
324 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
327 res.releaseConnection();
332 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
333 public void readNoRelationship(String testName) throws Exception {
335 if (logger.isDebugEnabled()) {
336 logger.debug(testBanner(testName, CLASS_NAME));
340 // Submit the request to the service and store the response.
341 RolePermissionClient client = new RolePermissionClient();
342 ClientResponse<PermissionRole> res = null;
345 res = client.read(roleValues.get(getRoleName() + NO_REL_SUFFIX).getRoleId(), "123");
346 int statusCode = res.getStatus();
348 // Check the status code of the response: does it match
349 // the expected response(s)?
350 if (logger.isDebugEnabled()) {
351 logger.debug(testName + ": status = " + statusCode);
353 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
354 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
355 Assert.assertEquals(statusCode, Response.Status.OK.getStatusCode());
356 PermissionRole output = (PermissionRole) res.getEntity();
358 String sOutput = objectAsXmlString(output, PermissionRole.class);
359 if (logger.isDebugEnabled()) {
360 logger.debug(testName + " received " + sOutput);
364 res.releaseConnection();
369 // ---------------------------------------------------------------
370 // CRUD tests : READ_LIST tests
371 // ---------------------------------------------------------------
374 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
377 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
378 dependsOnMethods = {"createList", "read"})
379 public void readList(String testName) throws Exception {
380 //Should this really be empty?
385 // ---------------------------------------------------------------
386 // CRUD tests : UPDATE tests
387 // ---------------------------------------------------------------
390 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
393 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
394 dependsOnMethods = {"read", "readList", "readNonExistent"})
395 public void update(String testName) throws Exception {
396 //Should this really be empty?
400 // Placeholders until the three tests below can be uncommented.
401 // See Issue CSPACE-401.
403 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
406 public void updateWithEmptyEntityBody(String testName) throws Exception {
407 //Should this really be empty?
411 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
414 public void updateWithMalformedXml(String testName) throws Exception {
415 //Should this really be empty?
419 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
422 public void updateWithWrongXmlSchema(String testName) throws Exception {
423 //Should this really be empty?
427 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
430 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
431 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
432 public void updateNonExistent(String testName) throws Exception {
433 //Should this really be empty?
436 // ---------------------------------------------------------------
437 // CRUD tests : DELETE tests
438 // ---------------------------------------------------------------
441 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
444 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
445 dependsOnMethods = {"read"})
446 public void delete(String testName) throws Exception {
448 if (logger.isDebugEnabled()) {
449 logger.debug(testBanner(testName, CLASS_NAME));
455 // Submit the request to the service and store the response.
456 RolePermissionClient client = new RolePermissionClient();
457 ClientResponse<Response> res = null;
459 RoleValue rv = roleValues.get(getRoleName());
460 PermissionRole permRole = createPermissionRoleInstance(rv,
461 permValues.values(), true, true);
463 roleValues.get(getRoleName()).getRoleId(), permRole);
464 int statusCode = res.getStatus();
466 // Check the status code of the response: does it match
467 // the expected response(s)?
468 if (logger.isDebugEnabled()) {
469 logger.debug(testName + ": status = " + statusCode);
471 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
472 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
473 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
476 res.releaseConnection();
483 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
486 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
487 public void deleteNonExistent(String testName) throws Exception {
488 //ignoring this test as the service side returns 200 now even if it does
489 //not find a record in the db
492 // ---------------------------------------------------------------
493 // Utility tests : tests of code used in tests above
494 // ---------------------------------------------------------------
496 * Tests the code for manually submitting data that is used by several
497 * of the methods above.
500 @Test(dependsOnMethods = {"create"})
501 public void testSubmitRequest() throws Exception {
503 // Expected status code: 200 OK
504 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
506 // Submit the request to the service and store the response.
507 String method = ServiceRequestType.READ.httpMethodName();
508 String url = getResourceURL(roleValues.get(getRoleName()).getRoleId());
509 int statusCode = submitRequest(method, url);
511 // Check the status code of the response: does it match
512 // the expected response(s)?
513 if (logger.isDebugEnabled()) {
514 logger.debug("testSubmitRequest: url=" + url
515 + " status=" + statusCode);
517 Assert.assertEquals(statusCode, EXPECTED_STATUS);
522 // ---------------------------------------------------------------
523 // Utility methods used by tests above
524 // ---------------------------------------------------------------
526 * create PermissionRole instance
527 * @param rv rolevalue
528 * @param pvs permission value array
531 * @return PermissionRole
533 public static PermissionRole createPermissionRoleInstance(RoleValue rv,
534 Collection<PermissionValue> pvs,
537 List<PermissionValue> pvls = new ArrayList<PermissionValue>();
539 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
540 rv, pvls, usePermId, useRoleId);
541 if (logger.isDebugEnabled()) {
544 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
552 @AfterClass(alwaysRun = true)
554 public void cleanUp() {
556 String noTest = System.getProperty("noTestCleanup");
557 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
558 if (logger.isDebugEnabled()) {
559 logger.debug("Skipping Cleanup phase ...");
563 if (logger.isDebugEnabled()) {
564 logger.debug("Cleaning up temporary resources created for testing ...");
566 for (PermissionValue pv : permValues.values()) {
567 deletePermission(pv.getPermissionId());
569 for (RoleValue rv : roleValues.values()) {
570 deleteRole(rv.getRoleId());
575 * Creates the permission.
577 * @param resName the res name
578 * @param effect the effect
581 private String createPermission(String resName, EffectType effect) {
583 PermissionClient permClient = new PermissionClient();
584 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
585 Permission permission = PermissionFactory.createPermissionInstance(resName,
586 "default permissions for " + resName,
587 actions, effect, true, true, true);
588 ClientResponse<Response> res = null;
591 res = permClient.create(permission);
592 int statusCode = res.getStatus();
593 if (logger.isDebugEnabled()) {
594 logger.debug("createPermission: resName=" + resName
595 + " status = " + statusCode);
597 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
598 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
599 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
603 res.releaseConnection();
612 * @param permId the perm id
614 private void deletePermission(String permId) {
616 PermissionClient permClient = new PermissionClient();
618 ClientResponse<Response> res = null;
620 res = permClient.delete(permId);
621 int statusCode = res.getStatus();
622 if (logger.isDebugEnabled()) {
623 logger.debug("deletePermission: delete permission id="
624 + permId + " status=" + statusCode);
626 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
627 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
628 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
632 res.releaseConnection();
640 * @param roleName the role name
643 private String createRole(String roleName) {
645 RoleClient roleClient = new RoleClient();
647 Role role = RoleFactory.createRoleInstance(roleName,
648 "role for " + roleName, true);
649 role.setRoleGroup("something");
650 ClientResponse<Response> res = null;
653 res = roleClient.create(role);
654 int statusCode = res.getStatus();
655 if (logger.isDebugEnabled()) {
656 logger.debug("createRole: name=" + roleName
657 + " status = " + statusCode);
659 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
660 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
661 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
665 res.releaseConnection();
674 * @param roleId the role id
676 private void deleteRole(String roleId) {
678 RoleClient roleClient = new RoleClient();
679 ClientResponse<Response> res = null;
681 res = roleClient.delete(roleId);
682 int statusCode = res.getStatus();
683 if (logger.isDebugEnabled()) {
684 logger.debug("deleteRole: delete role id=" + roleId
685 + " status=" + statusCode);
687 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
688 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
689 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
691 res.releaseConnection();