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);
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 = "_PermissionRoleServiceTest";
77 final private static String TEST_SERVICE_NAME = "fakeservice";
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 = "collectionobjects";
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 // ---------------------------------------------------------------
333 // CRUD tests : READ_LIST tests
334 // ---------------------------------------------------------------
337 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
340 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
341 dependsOnMethods = {"createList", "read"})
342 public void readList(String testName) throws Exception {
343 //Should this really be empty?
348 // ---------------------------------------------------------------
349 // CRUD tests : UPDATE tests
350 // ---------------------------------------------------------------
353 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
356 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
357 dependsOnMethods = {"read", "readList", "readNonExistent"})
358 public void update(String testName) throws Exception {
359 //Should this really be empty?
363 // Placeholders until the three tests below can be uncommented.
364 // See Issue CSPACE-401.
366 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
369 public void updateWithEmptyEntityBody(String testName) throws Exception {
370 //Should this really be empty?
374 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
377 public void updateWithMalformedXml(String testName) throws Exception {
378 //Should this really be empty?
382 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
385 public void updateWithWrongXmlSchema(String testName) throws Exception {
386 //Should this really be empty?
390 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
393 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
394 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
395 public void updateNonExistent(String testName) throws Exception {
396 //Should this really be empty?
399 // ---------------------------------------------------------------
400 // CRUD tests : DELETE tests
401 // ---------------------------------------------------------------
404 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
407 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
408 dependsOnMethods = {"read"})
409 public void delete(String testName) throws Exception {
411 if (logger.isDebugEnabled()) {
412 logger.debug(testBanner(testName, CLASS_NAME));
417 // Submit the request to the service and store the response.
418 PermissionRoleClient client = new PermissionRoleClient();
419 ClientResponse<Response> res = null;
422 permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId(), "123");
423 int statusCode = res.getStatus();
425 // Check the status code of the response: does it match
426 // the expected response(s)?
427 if (logger.isDebugEnabled()) {
428 logger.debug(testName + ": status = " + statusCode);
430 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
431 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
432 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
435 res.releaseConnection();
442 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
445 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
446 public void deleteNonExistent(String testName) throws Exception {
447 //ignoring this test as the service side returns 200 now even if it does
448 //not find a record in the db
451 // ---------------------------------------------------------------
452 // Utility tests : tests of code used in tests above
453 // ---------------------------------------------------------------
455 * Tests the code for manually submitting data that is used by several
456 * of the methods above.
459 @Test(dependsOnMethods = {"create"})
460 public void testSubmitRequest() throws Exception {
462 // Expected status code: 200 OK
463 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
465 // Submit the request to the service and store the response.
466 String method = ServiceRequestType.READ.httpMethodName();
467 String url = getResourceURL(permValues.get(TEST_SERVICE_NAME + TEST_MARKER).getPermissionId());
468 int statusCode = submitRequest(method, url);
470 // Check the status code of the response: does it match
471 // the expected response(s)?
472 if (logger.isDebugEnabled()) {
473 logger.debug("testSubmitRequest: url=" + url
474 + " status=" + statusCode);
476 Assert.assertEquals(statusCode, EXPECTED_STATUS);
481 // ---------------------------------------------------------------
482 // Utility methods used by tests above
483 // ---------------------------------------------------------------
485 * create permRolerole instance
486 * @param pv permissionvalue
487 * @param rvs rolevalue array
490 * @return PermissionRole
492 public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
493 Collection<RoleValue> rvs,
497 List<RoleValue> rvls = new ArrayList<RoleValue>();
499 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
500 pv, rvls, usePermId, useRoleId);
501 if (logger.isDebugEnabled()) {
502 logger.debug("to be created, permRole");
503 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
511 @AfterClass(alwaysRun = true)
513 public void cleanUp() {
515 String noTest = System.getProperty("noTestCleanup");
516 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
517 if (logger.isDebugEnabled()) {
518 logger.debug("Skipping Cleanup phase ...");
522 if (logger.isDebugEnabled()) {
523 logger.debug("Cleaning up temporary resources created for testing ...");
526 PermissionRoleClient client = new PermissionRoleClient();
527 for (String resourceId : allResourceIdsCreated) {
529 ClientResponse<Response> res = client.delete(resourceId, "123");
530 int statusCode = res.getStatus();
532 if (logger.isDebugEnabled()) {
533 logger.debug("cleanup: delete relationships for permission id="
534 + resourceId + " status=" + statusCode);
536 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
537 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
538 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
540 res.releaseConnection();
544 for (PermissionValue pv : permValues.values()) {
545 deletePermission(pv.getPermissionId());
547 for (RoleValue rv : roleValues.values()) {
548 deleteRole(rv.getRoleId());
553 * Creates the permission.
555 * @param resName the res name
556 * @param effect the effect
559 private String createPermission(String resName, EffectType effect) {
560 if (logger.isDebugEnabled()) {
561 logger.debug(testBanner("createPermission"));
564 PermissionClient permClient = new PermissionClient();
565 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
566 Permission permission = PermissionFactory.createPermissionInstance(resName,
567 "default permissions for " + resName,
568 actions, effect, true, true, true);
570 ClientResponse<Response> res = null;
572 res = permClient.create(permission);
574 int statusCode = res.getStatus();
575 if (logger.isDebugEnabled()) {
576 logger.debug("createPermission: resName=" + resName
577 + " status = " + statusCode);
579 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
580 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
581 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
585 res.releaseConnection();
594 * @param permId the perm id
596 private void deletePermission(String permId) {
597 if (logger.isDebugEnabled()) {
598 logger.debug(testBanner("deletePermission"));
601 PermissionClient permClient = new PermissionClient();
602 ClientResponse<Response> res = null;
604 res = permClient.delete(permId);
605 int statusCode = res.getStatus();
606 if (logger.isDebugEnabled()) {
607 logger.debug("deletePermission: delete permission id="
608 + permId + " status=" + statusCode);
610 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
611 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
612 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
614 res.releaseConnection();
622 * @param roleName the role name
625 private String createRole(String roleName) {
626 if (logger.isDebugEnabled()) {
627 logger.debug(testBanner("createRole"));
630 RoleClient roleClient = new RoleClient();
632 Role role = RoleFactory.createRoleInstance(roleName,
633 "role for " + roleName, true);
634 ClientResponse<Response> res = null;
637 res = roleClient.create(role);
638 int statusCode = res.getStatus();
639 if (logger.isDebugEnabled()) {
640 logger.debug("createRole: name=" + roleName
641 + " 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 roleId the role id
659 private void deleteRole(String roleId) {
660 if (logger.isDebugEnabled()) {
661 logger.debug(testBanner("deleteRole"));
664 RoleClient roleClient = new RoleClient();
665 ClientResponse<Response> res = null;
667 res = roleClient.delete(roleId);
668 int statusCode = res.getStatus();
669 if (logger.isDebugEnabled()) {
670 logger.debug("deleteRole: delete role id=" + roleId
671 + " status=" + statusCode);
673 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
674 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
675 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
677 res.releaseConnection();