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.PermissionClient;
39 import org.collectionspace.services.client.PermissionFactory;
40 import org.collectionspace.services.client.PermissionRoleClient;
41 import org.collectionspace.services.client.RoleClient;
42 import org.collectionspace.services.client.RoleFactory;
43 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
44 import org.collectionspace.services.client.test.ServiceRequestType;
45 import org.jboss.resteasy.client.ClientResponse;
47 import org.testng.Assert;
48 import org.testng.annotations.Test;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.testng.annotations.AfterClass;
53 import org.testng.annotations.BeforeClass;
56 * PermissionServiceTest, carries out tests against a
57 * deployed and running Permission, Role and PermissionRole Services.
59 * $LastChangedRevision: 917 $
60 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
62 public class PermissionRoleServiceTest extends AbstractServiceTestImpl {
64 static private final Logger logger =
65 LoggerFactory.getLogger(PermissionRoleServiceTest.class);
66 // Instance variables specific to this test.
67 private String knownResourceId = null;
68 private List<String> allResourceIdsCreated = new ArrayList();
69 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
70 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
72 * This method is called only by the parent class, AbstractServiceTestImpl
76 protected String getServicePathComponent() {
77 return new PermissionRoleClient().getServicePathComponent();
80 @BeforeClass(alwaysRun = true)
81 public void seedData() {
82 String ra = "accounts";
83 String accPermId = createPermission(ra, EffectType.PERMIT);
84 PermissionValue pva = new PermissionValue();
85 pva.setResourceName(ra);
86 pva.setPermissionId(accPermId);
87 permValues.put(pva.getResourceName(), pva);
89 String rc = "collectionobjects";
90 String coPermId = createPermission(rc, EffectType.DENY);
91 PermissionValue pvc = new PermissionValue();
92 pvc.setResourceName(rc);
93 pvc.setPermissionId(coPermId);
94 permValues.put(pvc.getResourceName(), pvc);
96 String ri = "intakes";
97 String iPermId = createPermission(ri, EffectType.DENY);
98 PermissionValue pvi = new PermissionValue();
99 pvi.setResourceName(ri);
100 pvi.setPermissionId(iPermId);
101 permValues.put(pvi.getResourceName(), pvi);
103 String rn1 = "ROLE_CO1";
104 String r1RoleId = createRole(rn1);
105 RoleValue rv1 = new RoleValue();
106 rv1.setRoleId(r1RoleId);
107 rv1.setRoleName(rn1);
108 roleValues.put(rv1.getRoleName(), rv1);
110 String rn2 = "ROLE_CO2";
111 String r2RoleId = createRole(rn2);
112 RoleValue rv2 = new RoleValue();
113 rv2.setRoleId(r2RoleId);
114 rv2.setRoleName(rn2);
115 roleValues.put(rv2.getRoleName(), rv2);
118 // ---------------------------------------------------------------
119 // CRUD tests : CREATE tests
120 // ---------------------------------------------------------------
123 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
124 public void create(String testName) throws Exception {
126 // Perform setup, such as initializing the type of service request
127 // (e.g. CREATE, DELETE), its valid and expected status codes, and
128 // its associated HTTP method name (e.g. POST, DELETE).
129 setupCreate(testName);
131 // Submit the request to the service and store the response.
132 PermissionValue pv = permValues.get("accounts");
133 PermissionRole permRole = createPermissionRoleInstance(pv,
134 roleValues.values(), true, true);
135 PermissionRoleClient client = new PermissionRoleClient();
136 ClientResponse<Response> res = client.create(pv.getPermissionId(), permRole);
137 int statusCode = res.getStatus();
139 if (logger.isDebugEnabled()) {
140 logger.debug(testName + ": status = " + statusCode);
142 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
143 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
144 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
146 // Store the ID returned from this create operation
147 // for additional tests below.
148 //this is is not important in case of this relationship
149 knownResourceId = extractId(res);
150 if (logger.isDebugEnabled()) {
151 logger.debug(testName + ": knownResourceId=" + knownResourceId);
155 //to not cause uniqueness violation for permRole, createList is removed
157 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
158 dependsOnMethods = {"create"})
159 public void createList(String testName) throws Exception {
163 // Placeholders until the three tests below can be uncommented.
164 // See Issue CSPACE-401.
166 public void createWithEmptyEntityBody(String testName) throws Exception {
170 public void createWithMalformedXml(String testName) throws Exception {
174 public void createWithWrongXmlSchema(String testName) throws Exception {
177 // ---------------------------------------------------------------
178 // CRUD tests : READ tests
179 // ---------------------------------------------------------------
182 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
183 dependsOnMethods = {"create"})
184 public void read(String testName) throws Exception {
189 // Submit the request to the service and store the response.
190 PermissionRoleClient client = new PermissionRoleClient();
191 ClientResponse<PermissionRole> res = client.read(
192 permValues.get("accounts").getPermissionId(), "123");
193 int statusCode = res.getStatus();
195 // Check the status code of the response: does it match
196 // the expected response(s)?
197 if (logger.isDebugEnabled()) {
198 logger.debug(testName + ": status = " + statusCode);
200 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
201 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
202 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
204 PermissionRole output = (PermissionRole) res.getEntity();
205 Assert.assertNotNull(output);
210 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
211 public void readNonExistent(String testName) throws Exception {
214 setupReadNonExistent(testName);
216 // Submit the request to the service and store the response.
217 PermissionRoleClient client = new PermissionRoleClient();
218 ClientResponse<PermissionRole> res = client.read(NON_EXISTENT_ID, "123");
219 int statusCode = res.getStatus();
221 // Check the status code of the response: does it match
222 // the expected response(s)?
223 if (logger.isDebugEnabled()) {
224 logger.debug(testName + ": status = " + statusCode);
226 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
227 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
228 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
231 // ---------------------------------------------------------------
232 // CRUD tests : READ_LIST tests
233 // ---------------------------------------------------------------
236 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
237 dependsOnMethods = {"createList", "read"})
238 public void readList(String testName) throws Exception {
243 // ---------------------------------------------------------------
244 // CRUD tests : UPDATE tests
245 // ---------------------------------------------------------------
248 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
249 dependsOnMethods = {"read", "readList", "readNonExistent"})
250 public void update(String testName) throws Exception {
254 // Placeholders until the three tests below can be uncommented.
255 // See Issue CSPACE-401.
257 public void updateWithEmptyEntityBody(String testName) throws Exception {
261 public void updateWithMalformedXml(String testName) throws Exception {
265 public void updateWithWrongXmlSchema(String testName) throws Exception {
269 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
270 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
271 public void updateNonExistent(String testName) throws Exception {
274 // ---------------------------------------------------------------
275 // CRUD tests : DELETE tests
276 // ---------------------------------------------------------------
279 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
280 dependsOnMethods = {"read"})
281 public void delete(String testName) throws Exception {
284 setupDelete(testName);
286 // Submit the request to the service and store the response.
287 PermissionRoleClient client = new PermissionRoleClient();
288 ClientResponse<Response> res = client.delete(
289 permValues.get("accounts").getPermissionId(), "123");
290 int statusCode = res.getStatus();
292 // Check the status code of the response: does it match
293 // the expected response(s)?
294 if (logger.isDebugEnabled()) {
295 logger.debug(testName + ": status = " + statusCode);
297 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
298 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
299 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
304 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
305 public void deleteNonExistent(String testName) throws Exception {
306 //ignoring this test as the service side returns 200 now even if it does
307 //not find a record in the db
310 // ---------------------------------------------------------------
311 // Utility tests : tests of code used in tests above
312 // ---------------------------------------------------------------
314 * Tests the code for manually submitting data that is used by several
315 * of the methods above.
317 @Test(dependsOnMethods = {"create"})
318 public void testSubmitRequest() throws Exception {
320 // Expected status code: 200 OK
321 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
323 // Submit the request to the service and store the response.
324 String method = ServiceRequestType.READ.httpMethodName();
325 String url = getResourceURL(permValues.get("accounts").getPermissionId());
326 int statusCode = submitRequest(method, url);
328 // Check the status code of the response: does it match
329 // the expected response(s)?
330 if (logger.isDebugEnabled()) {
331 logger.debug("testSubmitRequest: url=" + url
332 + " status=" + statusCode);
334 Assert.assertEquals(statusCode, EXPECTED_STATUS);
338 // ---------------------------------------------------------------
339 // Utility methods used by tests above
340 // ---------------------------------------------------------------
342 * create permRolerole instance
344 * @param roleValues array of role ids
349 public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
350 Collection<RoleValue> rvs,
354 PermissionRole permRole = new PermissionRole();
355 //service consume is not required to provide subject as it is determined
357 // permRole.setSubject(SubjectType.ROLE);
359 ArrayList<PermissionValue> pvs = new ArrayList<PermissionValue>();
361 permRole.setPermissions(pvs);
364 //FIXME is there a better way?
365 ArrayList<RoleValue> rvas = new ArrayList<RoleValue>();
366 for (RoleValue rv : rvs) {
369 permRole.setRoles(rvas);
372 if (logger.isDebugEnabled()) {
373 logger.debug("to be created, permRole common");
374 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
379 @AfterClass(alwaysRun = true)
380 public void cleanUp() {
381 setupDelete("delete");
382 String noTest = System.getProperty("noTestCleanup");
383 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
384 if (logger.isDebugEnabled()) {
385 logger.debug("Skipping Cleanup phase ...");
389 if (logger.isDebugEnabled()) {
390 logger.debug("Cleaning up temporary resources created for testing ...");
392 PermissionRoleClient client = new PermissionRoleClient();
393 for (String resourceId : allResourceIdsCreated) {
395 ClientResponse<Response> res = client.delete(resourceId, "123");
396 int statusCode = res.getStatus();
397 if (logger.isDebugEnabled()) {
398 logger.debug("cleanup: delete relationships for permission id="
399 + resourceId + " status=" + statusCode);
401 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
402 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
403 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
406 for (PermissionValue pv : permValues.values()) {
407 deletePermission(pv.getPermissionId());
410 for (RoleValue rv : roleValues.values()) {
411 deleteRole(rv.getRoleId());
415 private String createPermission(String resName, EffectType effect) {
417 PermissionClient permClient = new PermissionClient();
418 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
419 Permission permission = PermissionServiceTest.createPermissionInstance(resName,
420 "default permissions for " + resName,
421 actions, effect, true, true, true);
422 ClientResponse<Response> res = permClient.create(permission);
423 int statusCode = res.getStatus();
424 if (logger.isDebugEnabled()) {
425 logger.debug("createPermission: resName=" + resName
426 + " status = " + statusCode);
428 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
429 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
430 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
431 return extractId(res);
434 private void deletePermission(String permId) {
436 PermissionClient permClient = new PermissionClient();
437 ClientResponse<Response> res = permClient.delete(permId);
438 int statusCode = res.getStatus();
439 if (logger.isDebugEnabled()) {
440 logger.debug("deletePermission: delete permission id="
441 + permId + " status=" + statusCode);
443 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
444 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
445 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
448 private String createRole(String roleName) {
450 RoleClient roleClient = new RoleClient();
452 Role role = RoleFactory.createRoleInstance(roleName,
453 "role for " + roleName, true);
454 ClientResponse<Response> res = roleClient.create(role);
455 int statusCode = res.getStatus();
456 if (logger.isDebugEnabled()) {
457 logger.debug("createRole: name=" + roleName
458 + " status = " + statusCode);
460 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
461 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
462 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
463 return extractId(res);
466 private void deleteRole(String roleId) {
468 RoleClient roleClient = new RoleClient();
469 ClientResponse<Response> res = roleClient.delete(roleId);
470 int statusCode = res.getStatus();
471 if (logger.isDebugEnabled()) {
472 logger.debug("deleteRole: delete role id=" + roleId
473 + " status=" + statusCode);
475 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
476 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
477 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);