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 {
164 // Placeholders until the three tests below can be uncommented.
165 // See Issue CSPACE-401.
167 public void createWithEmptyEntityBody(String testName) throws Exception {
171 public void createWithMalformedXml(String testName) throws Exception {
175 public void createWithWrongXmlSchema(String testName) throws Exception {
178 // ---------------------------------------------------------------
179 // CRUD tests : READ tests
180 // ---------------------------------------------------------------
183 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
184 dependsOnMethods = {"create"})
185 public void read(String testName) throws Exception {
190 // Submit the request to the service and store the response.
191 PermissionRoleClient client = new PermissionRoleClient();
192 ClientResponse<PermissionRole> res = client.read(
193 permValues.get("accounts").getPermissionId(), "123");
194 int statusCode = res.getStatus();
196 // Check the status code of the response: does it match
197 // the expected response(s)?
198 if (logger.isDebugEnabled()) {
199 logger.debug(testName + ": status = " + statusCode);
201 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
202 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
203 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
205 PermissionRole output = (PermissionRole) res.getEntity();
206 Assert.assertNotNull(output);
211 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
212 public void readNonExistent(String testName) throws Exception {
215 setupReadNonExistent(testName);
217 // Submit the request to the service and store the response.
218 PermissionRoleClient client = new PermissionRoleClient();
219 ClientResponse<PermissionRole> res = client.read(NON_EXISTENT_ID, "123");
220 int statusCode = res.getStatus();
222 // Check the status code of the response: does it match
223 // the expected response(s)?
224 if (logger.isDebugEnabled()) {
225 logger.debug(testName + ": status = " + statusCode);
227 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
228 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
229 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
232 // ---------------------------------------------------------------
233 // CRUD tests : READ_LIST tests
234 // ---------------------------------------------------------------
237 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
238 dependsOnMethods = {"createList", "read"})
239 public void readList(String testName) throws Exception {
244 // ---------------------------------------------------------------
245 // CRUD tests : UPDATE tests
246 // ---------------------------------------------------------------
249 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
250 dependsOnMethods = {"read", "readList", "readNonExistent"})
251 public void update(String testName) throws Exception {
255 // Placeholders until the three tests below can be uncommented.
256 // See Issue CSPACE-401.
258 public void updateWithEmptyEntityBody(String testName) throws Exception {
262 public void updateWithMalformedXml(String testName) throws Exception {
266 public void updateWithWrongXmlSchema(String testName) throws Exception {
270 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
271 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
272 public void updateNonExistent(String testName) throws Exception {
275 // ---------------------------------------------------------------
276 // CRUD tests : DELETE tests
277 // ---------------------------------------------------------------
280 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
281 dependsOnMethods = {"read"})
282 public void delete(String testName) throws Exception {
285 setupDelete(testName);
287 // Submit the request to the service and store the response.
288 PermissionRoleClient client = new PermissionRoleClient();
289 ClientResponse<Response> res = client.delete(
290 permValues.get("accounts").getPermissionId(), "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);
305 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
306 public void deleteNonExistent(String testName) throws Exception {
309 setupDeleteNonExistent(testName);
311 // Submit the request to the service and store the response.
312 PermissionRoleClient client = new PermissionRoleClient();
313 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID, "123");
314 int statusCode = res.getStatus();
316 // Check the status code of the response: does it match
317 // the expected response(s)?
318 if (logger.isDebugEnabled()) {
319 logger.debug(testName + ": status = " + statusCode);
321 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
322 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
323 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
326 // ---------------------------------------------------------------
327 // Utility tests : tests of code used in tests above
328 // ---------------------------------------------------------------
330 * Tests the code for manually submitting data that is used by several
331 * of the methods above.
333 @Test(dependsOnMethods = {"create"})
334 public void testSubmitRequest() throws Exception {
336 // Expected status code: 200 OK
337 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
339 // Submit the request to the service and store the response.
340 String method = ServiceRequestType.READ.httpMethodName();
341 String url = getResourceURL(permValues.get("accounts").getPermissionId());
342 int statusCode = submitRequest(method, url);
344 // Check the status code of the response: does it match
345 // the expected response(s)?
346 if (logger.isDebugEnabled()) {
347 logger.debug("testSubmitRequest: url=" + url
348 + " status=" + statusCode);
350 Assert.assertEquals(statusCode, EXPECTED_STATUS);
354 // ---------------------------------------------------------------
355 // Utility methods used by tests above
356 // ---------------------------------------------------------------
358 * create permRolerole instance
360 * @param roleValues array of role ids
365 public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
366 Collection<RoleValue> rvs,
370 PermissionRole permRole = new PermissionRole();
371 //service consume is not required to provide subject as it is determined
373 // permRole.setSubject(SubjectType.ROLE);
375 ArrayList<PermissionValue> pvs = new ArrayList<PermissionValue>();
377 permRole.setPermissions(pvs);
380 //FIXME is there a better way?
381 ArrayList<RoleValue> rvas = new ArrayList<RoleValue>();
382 for (RoleValue rv : rvs) {
385 permRole.setRoles(rvas);
388 if (logger.isDebugEnabled()) {
389 logger.debug("to be created, permRole common");
390 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
395 @AfterClass(alwaysRun = true)
396 public void cleanUp() {
397 setupDelete("delete");
398 if (logger.isDebugEnabled()) {
399 logger.debug("clenaup: Cleaning up temporary resources created for testing ...");
401 PermissionRoleClient client = new PermissionRoleClient();
402 for (String resourceId : allResourceIdsCreated) {
404 // Note: Any non-success responses are ignored and not reported.
405 ClientResponse<Response> res = client.delete(resourceId, "123");
406 int statusCode = res.getStatus();
407 if (logger.isDebugEnabled()) {
408 logger.debug("clenaup: delete relationships for permission id="
409 + resourceId + " status=" + statusCode);
411 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
412 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
413 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
416 for (PermissionValue pv : permValues.values()) {
417 deletePermission(pv.getPermissionId());
420 for (RoleValue rv : roleValues.values()) {
421 deleteRole(rv.getRoleId());
425 private String createPermission(String resName, EffectType effect) {
427 PermissionClient permClient = new PermissionClient();
428 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
429 Permission permission = PermissionServiceTest.createPermissionInstance(resName,
430 "default permissions for " + resName,
431 actions, effect, true, true, true);
432 ClientResponse<Response> res = permClient.create(permission);
433 int statusCode = res.getStatus();
434 if (logger.isDebugEnabled()) {
435 logger.debug("createPermission: resName=" + resName +
436 " status = " + statusCode);
438 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
439 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
440 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
441 return extractId(res);
444 private void deletePermission(String permId) {
446 PermissionClient permClient = new PermissionClient();
447 ClientResponse<Response> res = permClient.delete(permId);
448 int statusCode = res.getStatus();
449 if (logger.isDebugEnabled()) {
450 logger.debug("deletePermission: delete permission id="
451 + permId + " status=" + statusCode);
453 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
454 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
455 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
458 private String createRole(String roleName) {
460 RoleClient roleClient = new RoleClient();
462 Role role = RoleFactory.createRoleInstance(roleName,
463 "role for " + roleName, true);
464 ClientResponse<Response> res = roleClient.create(role);
465 int statusCode = res.getStatus();
466 if (logger.isDebugEnabled()) {
467 logger.debug("createRole: name=" + roleName +
468 " status = " + statusCode);
470 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
471 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
472 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
473 return extractId(res);
476 private void deleteRole(String roleId) {
478 RoleClient roleClient = new RoleClient();
479 ClientResponse<Response> res = roleClient.delete(roleId);
480 int statusCode = res.getStatus();
481 if (logger.isDebugEnabled()) {
482 logger.debug("deleteRole: delete role id=" + roleId +
483 " status=" + statusCode);
485 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
486 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
487 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);