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 static private final Logger logger =
69 LoggerFactory.getLogger(RolePermissionServiceTest.class);
70 // Instance variables specific to this test.
71 /** The known resource id. */
72 private String knownResourceId = null;
73 /** The all resource ids created. */
74 private List<String> allResourceIdsCreated = new ArrayList<String>();
75 final private static String TEST_MARKER = "_RolePermissionServiceTest";
76 final private static String TEST_ROLE_NAME = "ROLE";
77 /** The perm values. */
78 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
79 /** The role values. */
80 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
82 * This method is called only by the parent class, AbstractServiceTestImpl
86 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
89 protected String getServicePathComponent() {
90 return new RolePermissionClient().getServicePathComponent();
96 @BeforeClass(alwaysRun = true)
97 public void seedData() {
99 String rn1 = TEST_ROLE_NAME + TEST_MARKER;
100 String r1RoleId = createRole(rn1);
101 RoleValue rv1 = new RoleValue();
102 rv1.setRoleId(r1RoleId);
103 rv1.setRoleName(rn1);
104 roleValues.put(rv1.getRoleName(), rv1);
106 String ra1 = "fooService" + TEST_MARKER;
107 String permId1 = createPermission(ra1, EffectType.PERMIT);
108 PermissionValue pva1 = new PermissionValue();
109 pva1.setResourceName(ra1);
110 pva1.setPermissionId(permId1);
111 permValues.put(pva1.getResourceName(), pva1);
113 String ra2 = "barService" + TEST_MARKER;
114 String permId2 = createPermission(ra1, EffectType.PERMIT);
115 PermissionValue pva2 = new PermissionValue();
116 pva2.setResourceName(ra2);
117 pva2.setPermissionId(permId2);
118 permValues.put(pva2.getResourceName(), pva2);
122 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
125 protected CollectionSpaceClient getClientInstance() {
126 return new RolePermissionClient();
130 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
133 protected AbstractCommonList getAbstractCommonList(
134 ClientResponse<AbstractCommonList> response) {
135 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
136 throw new UnsupportedOperationException();
140 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
142 @Test(dataProvider = "testName")
144 public void readPaginatedList(String testName) throws Exception {
145 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
147 // ---------------------------------------------------------------
148 // CRUD tests : CREATE tests
149 // ---------------------------------------------------------------
152 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
156 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
157 public void create(String testName) throws Exception {
159 // Perform setup, such as initializing the type of service request
160 // (e.g. CREATE, DELETE), its valid and expected status codes, and
161 // its associated HTTP method name (e.g. POST, DELETE).
162 setupCreate(testName);
164 // Submit the request to the service and store the response.
165 RoleValue rv = roleValues.get(TEST_ROLE_NAME + TEST_MARKER);
166 PermissionRole permRole = createPermissionRoleInstance(rv,
167 permValues.values(), true, true);
168 RolePermissionClient client = new RolePermissionClient();
169 ClientResponse<Response> res = null;
171 res = client.create(rv.getRoleId(), permRole);
173 int statusCode = res.getStatus();
175 if (logger.isDebugEnabled()) {
176 logger.debug(testName + ": status = " + statusCode);
178 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
181 // Store the ID returned from this create operation
182 // for additional tests below.
183 //this is is not important in case of this relationship
184 knownResourceId = extractId(res);
185 if (logger.isDebugEnabled()) {
186 logger.debug(testName + ": knownResourceId=" + knownResourceId);
190 res.releaseConnection();
195 //to not cause uniqueness violation for permRole, createList is removed
197 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
200 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
201 dependsOnMethods = {"create"})
202 public void createList(String testName) throws Exception {
203 //Should this really be empty?
207 // Placeholders until the three tests below can be uncommented.
208 // See Issue CSPACE-401.
210 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
213 public void createWithEmptyEntityBody(String testName) throws Exception {
214 //Should this really be empty?
218 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
221 public void createWithMalformedXml(String testName) throws Exception {
222 //Should this really be empty?
226 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
229 public void createWithWrongXmlSchema(String testName) throws Exception {
230 //Should this really be empty?
233 // ---------------------------------------------------------------
234 // CRUD tests : READ tests
235 // ---------------------------------------------------------------
238 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
241 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
242 dependsOnMethods = {"create"})
243 public void read(String testName) throws Exception {
248 // Submit the request to the service and store the response.
249 RolePermissionClient client = new RolePermissionClient();
250 ClientResponse<PermissionRole> res = null;
253 roleValues.get(TEST_ROLE_NAME + TEST_MARKER).getRoleId(), "123");
254 int statusCode = res.getStatus();
256 // Check the status code of the response: does it match
257 // the expected response(s)?
258 if (logger.isDebugEnabled()) {
259 logger.debug(testName + ": status = " + statusCode);
261 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
262 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
263 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
265 PermissionRole output = (PermissionRole) res.getEntity();
266 Assert.assertNotNull(output);
269 res.releaseConnection();
276 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
279 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
280 public void readNonExistent(String testName) throws Exception {
283 setupReadNonExistent(testName);
285 // Submit the request to the service and store the response.
286 RolePermissionClient client = new RolePermissionClient();
287 ClientResponse<PermissionRole> res = null;
290 res = client.read(NON_EXISTENT_ID, "123");
291 int statusCode = res.getStatus();
293 // Check the status code of the response: does it match
294 // the expected response(s)?
295 if (logger.isDebugEnabled()) {
296 logger.debug(testName + ": status = " + statusCode);
298 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
299 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
300 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
303 res.releaseConnection();
308 // ---------------------------------------------------------------
309 // CRUD tests : READ_LIST tests
310 // ---------------------------------------------------------------
313 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
316 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
317 dependsOnMethods = {"createList", "read"})
318 public void readList(String testName) throws Exception {
319 //Should this really be empty?
324 // ---------------------------------------------------------------
325 // CRUD tests : UPDATE tests
326 // ---------------------------------------------------------------
329 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
332 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
333 dependsOnMethods = {"read", "readList", "readNonExistent"})
334 public void update(String testName) throws Exception {
335 //Should this really be empty?
339 // Placeholders until the three tests below can be uncommented.
340 // See Issue CSPACE-401.
342 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
345 public void updateWithEmptyEntityBody(String testName) throws Exception {
346 //Should this really be empty?
350 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
353 public void updateWithMalformedXml(String testName) throws Exception {
354 //Should this really be empty?
358 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
361 public void updateWithWrongXmlSchema(String testName) throws Exception {
362 //Should this really be empty?
366 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
369 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
370 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
371 public void updateNonExistent(String testName) throws Exception {
372 //Should this really be empty?
375 // ---------------------------------------------------------------
376 // CRUD tests : DELETE tests
377 // ---------------------------------------------------------------
380 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
383 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
384 dependsOnMethods = {"read"})
385 public void delete(String testName) throws Exception {
388 setupDelete(testName);
390 // Submit the request to the service and store the response.
391 RolePermissionClient client = new RolePermissionClient();
392 ClientResponse<Response> res = null;
395 roleValues.get(TEST_ROLE_NAME + TEST_MARKER).getRoleId(), "123");
396 int statusCode = res.getStatus();
398 // Check the status code of the response: does it match
399 // the expected response(s)?
400 if (logger.isDebugEnabled()) {
401 logger.debug(testName + ": status = " + statusCode);
403 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
404 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
405 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
408 res.releaseConnection();
415 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
418 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
419 public void deleteNonExistent(String testName) throws Exception {
420 //ignoring this test as the service side returns 200 now even if it does
421 //not find a record in the db
424 // ---------------------------------------------------------------
425 // Utility tests : tests of code used in tests above
426 // ---------------------------------------------------------------
428 * Tests the code for manually submitting data that is used by several
429 * of the methods above.
432 @Test(dependsOnMethods = {"create"})
433 public void testSubmitRequest() throws Exception {
435 // Expected status code: 200 OK
436 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
438 // Submit the request to the service and store the response.
439 String method = ServiceRequestType.READ.httpMethodName();
440 String url = getResourceURL(roleValues.get(TEST_ROLE_NAME + TEST_MARKER).getRoleId());
441 int statusCode = submitRequest(method, url);
443 // Check the status code of the response: does it match
444 // the expected response(s)?
445 if (logger.isDebugEnabled()) {
446 logger.debug("testSubmitRequest: url=" + url
447 + " status=" + statusCode);
449 Assert.assertEquals(statusCode, EXPECTED_STATUS);
454 // ---------------------------------------------------------------
455 // Utility methods used by tests above
456 // ---------------------------------------------------------------
458 * create PermissionRole instance
459 * @param rv rolevalue
460 * @param pvs permission value array
463 * @return PermissionRole
465 public static PermissionRole createPermissionRoleInstance(RoleValue rv,
466 Collection<PermissionValue> pvs,
469 List<PermissionValue> pvls = new ArrayList<PermissionValue>();
471 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
472 rv, pvls, usePermId, useRoleId);
473 if (logger.isDebugEnabled()) {
474 logger.debug("to be created, permRole");
475 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
483 @AfterClass(alwaysRun = true)
485 public void cleanUp() {
486 setupDelete("cleanUp");
487 String noTest = System.getProperty("noTestCleanup");
488 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
489 if (logger.isDebugEnabled()) {
490 logger.debug("Skipping Cleanup phase ...");
494 if (logger.isDebugEnabled()) {
495 logger.debug("Cleaning up temporary resources created for testing ...");
498 RolePermissionClient client = new RolePermissionClient();
499 for (String resourceId : allResourceIdsCreated) {
501 ClientResponse<Response> res = client.delete(resourceId, "123");
502 int statusCode = res.getStatus();
504 if (logger.isDebugEnabled()) {
505 logger.debug("cleanup: delete relationships for permission id="
506 + resourceId + " status=" + statusCode);
508 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
509 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
510 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
512 res.releaseConnection();
516 for (PermissionValue pv : permValues.values()) {
517 deletePermission(pv.getPermissionId());
519 for (RoleValue rv : roleValues.values()) {
520 deleteRole(rv.getRoleId());
525 * Creates the permission.
527 * @param resName the res name
528 * @param effect the effect
531 private String createPermission(String resName, EffectType effect) {
532 setupCreate("createPermission");
533 PermissionClient permClient = new PermissionClient();
534 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
535 Permission permission = PermissionFactory.createPermissionInstance(resName,
536 "default permissions for " + resName,
537 actions, effect, true, true, true);
538 ClientResponse<Response> res = null;
541 res = permClient.create(permission);
542 int statusCode = res.getStatus();
543 if (logger.isDebugEnabled()) {
544 logger.debug("createPermission: resName=" + resName
545 + " status = " + statusCode);
547 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
548 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
549 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
553 res.releaseConnection();
562 * @param permId the perm id
564 private void deletePermission(String permId) {
565 setupDelete("deletePermission");
566 PermissionClient permClient = new PermissionClient();
568 ClientResponse<Response> res = null;
570 res = permClient.delete(permId);
571 int statusCode = res.getStatus();
572 if (logger.isDebugEnabled()) {
573 logger.debug("deletePermission: delete permission id="
574 + permId + " status=" + statusCode);
576 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
577 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
578 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
582 res.releaseConnection();
590 * @param roleName the role name
593 private String createRole(String roleName) {
594 setupCreate("createRole");
595 RoleClient roleClient = new RoleClient();
597 Role role = RoleFactory.createRoleInstance(roleName,
598 "role for " + roleName, true);
599 ClientResponse<Response> res = null;
602 res = roleClient.create(role);
603 int statusCode = res.getStatus();
604 if (logger.isDebugEnabled()) {
605 logger.debug("createRole: name=" + roleName
606 + " status = " + statusCode);
608 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
609 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
610 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
614 res.releaseConnection();
623 * @param roleId the role id
625 private void deleteRole(String roleId) {
626 setupDelete("deleteRole");
627 RoleClient roleClient = new RoleClient();
628 ClientResponse<Response> res = null;
630 res = roleClient.delete(roleId);
631 int statusCode = res.getStatus();
632 if (logger.isDebugEnabled()) {
633 logger.debug("deleteRole: delete role id=" + roleId
634 + " status=" + statusCode);
636 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
637 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
638 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
640 res.releaseConnection();