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.PermissionRoleClient;
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 PermissionRoleServiceTest extends AbstractServiceTestImpl {
67 /** The Constant logger. */
68 private final static String CLASS_NAME = PermissionRoleServiceTest.class.getName();
69 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
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 = "_PermissionRoleServiceTest";
76 final private static String TEST_SERVICE_NAME = "fakeservice";
77 final private static String NO_REL_SUFFIX = "-no-rel";
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 PermissionRoleClient().getServicePathComponent();
97 @BeforeClass(alwaysRun = true)
98 public void seedData() {
99 String ra = TEST_SERVICE_NAME + TEST_MARKER;
100 String accPermId = createPermission(ra, EffectType.PERMIT);
101 PermissionValue pva = new PermissionValue();
102 pva.setResourceName(ra);
103 pva.setPermissionId(accPermId);
104 permValues.put(pva.getResourceName(), pva);
106 String rc = TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX;
107 String coPermId = createPermission(rc, EffectType.DENY);
108 PermissionValue pvc = new PermissionValue();
109 pvc.setResourceName(rc);
110 pvc.setPermissionId(coPermId);
111 permValues.put(pvc.getResourceName(), pvc);
113 // String ri = "intakes";
114 // String iPermId = createPermission(ri, EffectType.DENY);
115 // PermissionValue pvi = new PermissionValue();
116 // pvi.setResourceName(ri);
117 // pvi.setPermissionId(iPermId);
118 // permValues.put(pvi.getResourceName(), pvi);
120 String rn1 = "ROLE_CO1" + TEST_MARKER;
121 String r1RoleId = createRole(rn1);
122 RoleValue rv1 = new RoleValue();
123 rv1.setRoleId(r1RoleId);
124 rv1.setRoleName(rn1);
125 roleValues.put(rv1.getRoleName(), rv1);
127 String rn2 = "ROLE_CO2" + TEST_MARKER;
128 String r2RoleId = createRole(rn2);
129 RoleValue rv2 = new RoleValue();
130 rv2.setRoleId(r2RoleId);
131 rv2.setRoleName(rn2);
132 roleValues.put(rv2.getRoleName(), rv2);
136 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
139 protected CollectionSpaceClient getClientInstance() {
140 return new PermissionRoleClient();
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 PermissionValue pv = permValues.get(TEST_SERVICE_NAME + TEST_MARKER);
184 PermissionRole permRole = createPermissionRoleInstance(pv,
185 roleValues.values(), true, true);
186 PermissionRoleClient client = new PermissionRoleClient();
187 ClientResponse<Response> res = null;
189 res = client.create(pv.getPermissionId(), permRole);
190 int statusCode = res.getStatus();
192 if (logger.isDebugEnabled()) {
193 logger.debug(testName + ": status = " + statusCode);
195 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
198 res.releaseConnection();
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));
269 // Submit the request to the service and store the response.
270 PermissionRoleClient client = new PermissionRoleClient();
271 ClientResponse<PermissionRole> res = null;
274 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId(), "123");
275 int statusCode = res.getStatus();
277 // Check the status code of the response: does it match
278 // the expected response(s)?
279 if (logger.isDebugEnabled()) {
280 logger.debug(testName + ": status = " + statusCode);
282 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
283 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
284 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
286 PermissionRole output = (PermissionRole) res.getEntity();
287 Assert.assertNotNull(output);
290 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));
308 setupReadNonExistent();
310 // Submit the request to the service and store the response.
311 PermissionRoleClient client = new PermissionRoleClient();
312 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 dependsOnMethods = {"create"})
334 public void readNoRelationship(String testName) throws Exception {
336 if (logger.isDebugEnabled()) {
337 logger.debug(testBanner(testName, CLASS_NAME));
342 // Submit the request to the service and store the response.
343 PermissionRoleClient client = new PermissionRoleClient();
344 ClientResponse<PermissionRole> res = null;
347 permValues.get(TEST_SERVICE_NAME + TEST_MARKER + NO_REL_SUFFIX).getPermissionId(), "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());
359 PermissionRole output = (PermissionRole) res.getEntity();
361 String sOutput = objectAsXmlString(output, PermissionRole.class);
362 if (logger.isDebugEnabled()) {
363 logger.debug(testName + " received " + sOutput);
367 res.releaseConnection();
372 // ---------------------------------------------------------------
373 // CRUD tests : READ_LIST tests
374 // ---------------------------------------------------------------
377 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
381 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
382 dependsOnMethods = {"createList", "read"})
383 public void readList(String testName) throws Exception {
384 //Should this really be empty?
389 // ---------------------------------------------------------------
390 // CRUD tests : UPDATE tests
391 // ---------------------------------------------------------------
394 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
397 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
398 dependsOnMethods = {"read", "readList", "readNonExistent"})
399 public void update(String testName) throws Exception {
400 //Should this really be empty?
404 // Placeholders until the three tests below can be uncommented.
405 // See Issue CSPACE-401.
407 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
410 public void updateWithEmptyEntityBody(String testName) throws Exception {
411 //Should this really be empty?
415 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
418 public void updateWithMalformedXml(String testName) throws Exception {
419 //Should this really be empty?
423 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
426 public void updateWithWrongXmlSchema(String testName) throws Exception {
427 //Should this really be empty?
431 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
434 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
435 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
436 public void updateNonExistent(String testName) throws Exception {
437 //Should this really be empty?
440 // ---------------------------------------------------------------
441 // CRUD tests : DELETE tests
442 // ---------------------------------------------------------------
445 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
448 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
449 dependsOnMethods = {"read"})
450 public void delete(String testName) throws Exception {
452 if (logger.isDebugEnabled()) {
453 logger.debug(testBanner(testName, CLASS_NAME));
458 // Submit the request to the service and store the response.
459 PermissionRoleClient client = new PermissionRoleClient();
460 ClientResponse<Response> res = null;
463 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId(), "123");
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(permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
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 permRolerole instance
527 * @param pv permissionvalue
528 * @param rvs rolevalue array
531 * @return PermissionRole
533 public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
534 Collection<RoleValue> rvs,
538 List<RoleValue> rvls = new ArrayList<RoleValue>();
540 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
541 pv, rvls, usePermId, useRoleId);
542 if (logger.isDebugEnabled()) {
543 logger.debug("to be created, permRole");
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 ...");
567 PermissionRoleClient client = new PermissionRoleClient();
568 for (String resourceId : allResourceIdsCreated) {
570 ClientResponse<Response> res = client.delete(resourceId, "123");
571 int statusCode = res.getStatus();
573 if (logger.isDebugEnabled()) {
574 logger.debug("cleanup: delete relationships for permission id="
575 + resourceId + " status=" + statusCode);
577 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
578 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
579 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
581 res.releaseConnection();
585 for (PermissionValue pv : permValues.values()) {
586 deletePermission(pv.getPermissionId());
588 for (RoleValue rv : roleValues.values()) {
589 deleteRole(rv.getRoleId());
594 * Creates the permission.
596 * @param resName the res name
597 * @param effect the effect
600 private String createPermission(String resName, EffectType effect) {
601 if (logger.isDebugEnabled()) {
602 logger.debug(testBanner("createPermission"));
605 PermissionClient permClient = new PermissionClient();
606 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
607 Permission permission = PermissionFactory.createPermissionInstance(resName,
608 "default permissions for " + resName,
609 actions, effect, true, true, true);
611 ClientResponse<Response> res = null;
613 res = permClient.create(permission);
615 int statusCode = res.getStatus();
616 if (logger.isDebugEnabled()) {
617 logger.debug("createPermission: resName=" + resName
618 + " status = " + statusCode);
620 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
621 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
622 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
626 res.releaseConnection();
635 * @param permId the perm id
637 private void deletePermission(String permId) {
638 if (logger.isDebugEnabled()) {
639 logger.debug(testBanner("deletePermission"));
642 PermissionClient permClient = new PermissionClient();
643 ClientResponse<Response> res = null;
645 res = permClient.delete(permId);
646 int statusCode = res.getStatus();
647 if (logger.isDebugEnabled()) {
648 logger.debug("deletePermission: delete permission id="
649 + permId + " status=" + statusCode);
651 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
652 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
653 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
655 res.releaseConnection();
663 * @param roleName the role name
666 private String createRole(String roleName) {
667 if (logger.isDebugEnabled()) {
668 logger.debug(testBanner("createRole"));
671 RoleClient roleClient = new RoleClient();
673 Role role = RoleFactory.createRoleInstance(roleName,
674 "role for " + roleName, true);
675 ClientResponse<Response> res = null;
678 res = roleClient.create(role);
679 int statusCode = res.getStatus();
680 if (logger.isDebugEnabled()) {
681 logger.debug("createRole: name=" + roleName
682 + " status = " + statusCode);
684 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
685 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
686 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
690 res.releaseConnection();
698 * @param roleId the role id
700 private void deleteRole(String roleId) {
701 if (logger.isDebugEnabled()) {
702 logger.debug(testBanner("deleteRole"));
705 RoleClient roleClient = new RoleClient();
706 ClientResponse<Response> res = null;
708 res = roleClient.delete(roleId);
709 int statusCode = res.getStatus();
710 if (logger.isDebugEnabled()) {
711 logger.debug("deleteRole: delete role id=" + roleId
712 + " status=" + statusCode);
714 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
715 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
716 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
718 res.releaseConnection();