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);
72 // Instance variables specific to this test.
73 /** The known resource id. */
74 private String knownResourceId = null;
75 /** The all resource ids created. */
76 private List<String> allResourceIdsCreated = new ArrayList<String>();
77 final private static String TEST_MARKER = "_RolePermissionServiceTest";
78 final private static String TEST_ROLE_NAME = "ROLE";
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>();
84 private Date now = new Date();
86 * This method is called only by the parent class, AbstractServiceTestImpl
90 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
93 protected String getServicePathComponent() {
94 return new RolePermissionClient().getServicePathComponent();
97 private String getRoleName() {
98 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 ra1 = "fooService" + TEST_MARKER;
114 String permId1 = createPermission(ra1, EffectType.PERMIT);
115 PermissionValue pva1 = new PermissionValue();
116 pva1.setResourceName(ra1);
117 pva1.setPermissionId(permId1);
118 permValues.put(pva1.getResourceName(), pva1);
120 String ra2 = "barService" + TEST_MARKER;
121 String permId2 = createPermission(ra1, EffectType.PERMIT);
122 PermissionValue pva2 = new PermissionValue();
123 pva2.setResourceName(ra2);
124 pva2.setPermissionId(permId2);
125 permValues.put(pva2.getResourceName(), pva2);
129 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
132 protected CollectionSpaceClient getClientInstance() {
133 return new RolePermissionClient();
137 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
140 protected AbstractCommonList getAbstractCommonList(
141 ClientResponse<AbstractCommonList> response) {
142 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
143 throw new UnsupportedOperationException();
147 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
149 @Test(dataProvider = "testName")
151 public void readPaginatedList(String testName) throws Exception {
152 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
154 // ---------------------------------------------------------------
155 // CRUD tests : CREATE tests
156 // ---------------------------------------------------------------
159 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
163 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
164 public void create(String testName) throws Exception {
166 if (logger.isDebugEnabled()) {
167 logger.debug(testBanner(testName, CLASS_NAME));
169 // Perform setup, such as initializing the type of service request
170 // (e.g. CREATE, DELETE), its valid and expected status codes, and
171 // its associated HTTP method name (e.g. POST, DELETE).
174 // Submit the request to the service and store the response.
175 RoleValue rv = roleValues.get(getRoleName());
176 PermissionRole permRole = createPermissionRoleInstance(rv,
177 permValues.values(), true, true);
178 RolePermissionClient client = new RolePermissionClient();
179 ClientResponse<Response> res = null;
181 res = client.create(rv.getRoleId(), permRole);
183 int statusCode = res.getStatus();
185 if (logger.isDebugEnabled()) {
186 logger.debug(testName + ": status = " + statusCode);
188 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
189 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
190 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
191 // Store the ID returned from this create operation
192 // for additional tests below.
193 //this is is not important in case of this relationship
194 knownResourceId = extractId(res);
195 if (logger.isDebugEnabled()) {
196 logger.debug(testName + ": knownResourceId=" + knownResourceId);
200 res.releaseConnection();
205 //to not cause uniqueness violation for permRole, createList is removed
207 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
210 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
211 dependsOnMethods = {"create"})
212 public void createList(String testName) throws Exception {
213 //Should this really be empty?
217 // Placeholders until the three tests below can be uncommented.
218 // See Issue CSPACE-401.
220 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
223 public void createWithEmptyEntityBody(String testName) throws Exception {
224 //Should this really be empty?
228 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
231 public void createWithMalformedXml(String testName) throws Exception {
232 //Should this really be empty?
236 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
239 public void createWithWrongXmlSchema(String testName) throws Exception {
240 //Should this really be empty?
243 // ---------------------------------------------------------------
244 // CRUD tests : READ tests
245 // ---------------------------------------------------------------
248 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
251 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
252 dependsOnMethods = {"create"})
253 public void read(String testName) throws Exception {
255 if (logger.isDebugEnabled()) {
256 logger.debug(testBanner(testName, CLASS_NAME));
261 // Submit the request to the service and store the response.
262 RolePermissionClient client = new RolePermissionClient();
263 ClientResponse<PermissionRole> res = null;
266 roleValues.get(getRoleName()).getRoleId(), "123");
267 int statusCode = res.getStatus();
269 // Check the status code of the response: does it match
270 // the expected response(s)?
271 if (logger.isDebugEnabled()) {
272 logger.debug(testName + ": status = " + statusCode);
274 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
275 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
276 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
278 PermissionRole output = (PermissionRole) res.getEntity();
279 Assert.assertNotNull(output);
282 res.releaseConnection();
289 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
292 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
293 public void readNonExistent(String testName) throws Exception {
295 if (logger.isDebugEnabled()) {
296 logger.debug(testBanner(testName, CLASS_NAME));
299 setupReadNonExistent();
301 // Submit the request to the service and store the response.
302 RolePermissionClient client = new RolePermissionClient();
303 ClientResponse<PermissionRole> res = null;
306 res = client.read(NON_EXISTENT_ID, "123");
307 int statusCode = res.getStatus();
309 // Check the status code of the response: does it match
310 // the expected response(s)?
311 if (logger.isDebugEnabled()) {
312 logger.debug(testName + ": status = " + statusCode);
314 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
315 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
316 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
319 res.releaseConnection();
324 // ---------------------------------------------------------------
325 // CRUD tests : READ_LIST tests
326 // ---------------------------------------------------------------
329 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
332 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
333 dependsOnMethods = {"createList", "read"})
334 public void readList(String testName) throws Exception {
335 //Should this really be empty?
340 // ---------------------------------------------------------------
341 // CRUD tests : UPDATE tests
342 // ---------------------------------------------------------------
345 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
348 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
349 dependsOnMethods = {"read", "readList", "readNonExistent"})
350 public void update(String testName) throws Exception {
351 //Should this really be empty?
355 // Placeholders until the three tests below can be uncommented.
356 // See Issue CSPACE-401.
358 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
361 public void updateWithEmptyEntityBody(String testName) throws Exception {
362 //Should this really be empty?
366 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
369 public void updateWithMalformedXml(String testName) throws Exception {
370 //Should this really be empty?
374 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
377 public void updateWithWrongXmlSchema(String testName) throws Exception {
378 //Should this really be empty?
382 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
385 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
386 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
387 public void updateNonExistent(String testName) throws Exception {
388 //Should this really be empty?
391 // ---------------------------------------------------------------
392 // CRUD tests : DELETE tests
393 // ---------------------------------------------------------------
396 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
399 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
400 dependsOnMethods = {"read"})
401 public void delete(String testName) throws Exception {
403 if (logger.isDebugEnabled()) {
404 logger.debug(testBanner(testName, CLASS_NAME));
409 // Submit the request to the service and store the response.
410 RolePermissionClient client = new RolePermissionClient();
411 ClientResponse<Response> res = null;
414 roleValues.get(getRoleName()).getRoleId(), "123");
415 int statusCode = res.getStatus();
417 // Check the status code of the response: does it match
418 // the expected response(s)?
419 if (logger.isDebugEnabled()) {
420 logger.debug(testName + ": status = " + statusCode);
422 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
423 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
424 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
427 res.releaseConnection();
434 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
437 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
438 public void deleteNonExistent(String testName) throws Exception {
439 //ignoring this test as the service side returns 200 now even if it does
440 //not find a record in the db
443 // ---------------------------------------------------------------
444 // Utility tests : tests of code used in tests above
445 // ---------------------------------------------------------------
447 * Tests the code for manually submitting data that is used by several
448 * of the methods above.
451 @Test(dependsOnMethods = {"create"})
452 public void testSubmitRequest() throws Exception {
454 // Expected status code: 200 OK
455 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
457 // Submit the request to the service and store the response.
458 String method = ServiceRequestType.READ.httpMethodName();
459 String url = getResourceURL(roleValues.get(getRoleName()).getRoleId());
460 int statusCode = submitRequest(method, url);
462 // Check the status code of the response: does it match
463 // the expected response(s)?
464 if (logger.isDebugEnabled()) {
465 logger.debug("testSubmitRequest: url=" + url
466 + " status=" + statusCode);
468 Assert.assertEquals(statusCode, EXPECTED_STATUS);
473 // ---------------------------------------------------------------
474 // Utility methods used by tests above
475 // ---------------------------------------------------------------
477 * create PermissionRole instance
478 * @param rv rolevalue
479 * @param pvs permission value array
482 * @return PermissionRole
484 public static PermissionRole createPermissionRoleInstance(RoleValue rv,
485 Collection<PermissionValue> pvs,
488 List<PermissionValue> pvls = new ArrayList<PermissionValue>();
490 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
491 rv, pvls, usePermId, useRoleId);
492 if (logger.isDebugEnabled()) {
493 logger.debug("to be created, permRole");
494 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
502 @AfterClass(alwaysRun = true)
504 public void cleanUp() {
506 String noTest = System.getProperty("noTestCleanup");
507 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
508 if (logger.isDebugEnabled()) {
509 logger.debug("Skipping Cleanup phase ...");
513 if (logger.isDebugEnabled()) {
514 logger.debug("Cleaning up temporary resources created for testing ...");
517 RolePermissionClient client = new RolePermissionClient();
518 for (String resourceId : allResourceIdsCreated) {
520 ClientResponse<Response> res = client.delete(resourceId, "123");
521 int statusCode = res.getStatus();
523 if (logger.isDebugEnabled()) {
524 logger.debug("cleanup: delete relationships for permission id="
525 + resourceId + " status=" + statusCode);
527 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
528 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
529 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
531 res.releaseConnection();
535 for (PermissionValue pv : permValues.values()) {
536 deletePermission(pv.getPermissionId());
538 for (RoleValue rv : roleValues.values()) {
539 deleteRole(rv.getRoleId());
544 * Creates the permission.
546 * @param resName the res name
547 * @param effect the effect
550 private String createPermission(String resName, EffectType effect) {
552 PermissionClient permClient = new PermissionClient();
553 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
554 Permission permission = PermissionFactory.createPermissionInstance(resName,
555 "default permissions for " + resName,
556 actions, effect, true, true, true);
557 ClientResponse<Response> res = null;
560 res = permClient.create(permission);
561 int statusCode = res.getStatus();
562 if (logger.isDebugEnabled()) {
563 logger.debug("createPermission: resName=" + resName
564 + " status = " + statusCode);
566 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
567 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
568 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
572 res.releaseConnection();
581 * @param permId the perm id
583 private void deletePermission(String permId) {
585 PermissionClient permClient = new PermissionClient();
587 ClientResponse<Response> res = null;
589 res = permClient.delete(permId);
590 int statusCode = res.getStatus();
591 if (logger.isDebugEnabled()) {
592 logger.debug("deletePermission: delete permission id="
593 + permId + " status=" + statusCode);
595 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
596 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
597 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
601 res.releaseConnection();
609 * @param roleName the role name
612 private String createRole(String roleName) {
614 RoleClient roleClient = new RoleClient();
616 Role role = RoleFactory.createRoleInstance(roleName,
617 "role for " + roleName, true);
618 role.setRoleGroup("something");
619 ClientResponse<Response> res = null;
622 res = roleClient.create(role);
623 int statusCode = res.getStatus();
624 if (logger.isDebugEnabled()) {
625 logger.debug("createRole: name=" + roleName
626 + " status = " + statusCode);
628 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
629 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
630 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
634 res.releaseConnection();
643 * @param roleId the role id
645 private void deleteRole(String roleId) {
647 RoleClient roleClient = new RoleClient();
648 ClientResponse<Response> res = null;
650 res = roleClient.delete(roleId);
651 int statusCode = res.getStatus();
652 if (logger.isDebugEnabled()) {
653 logger.debug("deleteRole: delete role id=" + roleId
654 + " status=" + statusCode);
656 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
657 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
658 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
660 res.releaseConnection();