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.PermissionRoleClient;
40 import org.collectionspace.services.client.RoleClient;
41 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
42 import org.collectionspace.services.client.test.ServiceRequestType;
43 import org.jboss.resteasy.client.ClientResponse;
45 import org.testng.Assert;
46 import org.testng.annotations.Test;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.BeforeClass;
54 * PermissionServiceTest, carries out tests against a
55 * deployed and running Permission, Role and PermissionRole Services.
57 * $LastChangedRevision: 917 $
58 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
60 public class PermissionRoleServiceTest extends AbstractServiceTestImpl {
62 private final Logger logger =
63 LoggerFactory.getLogger(PermissionRoleServiceTest.class);
64 // Instance variables specific to this test.
65 private String knownResourceId = null;
66 private List<String> allResourceIdsCreated = new ArrayList();
67 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
68 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
70 * This method is called only by the parent class, AbstractServiceTestImpl
74 protected String getServicePathComponent() {
75 return new PermissionRoleClient().getServicePathComponent();
78 @BeforeClass(alwaysRun = true)
79 public void seedData() {
80 String ra = "accounts";
81 String accPermId = createPermission(ra, EffectType.PERMIT);
82 PermissionValue pva = new PermissionValue();
83 pva.setResourceName(ra);
84 pva.setPermissionId(accPermId);
85 permValues.put(pva.getResourceName(), pva);
87 String rc = "collectionobjects";
88 String coPermId = createPermission(rc, EffectType.DENY);
89 PermissionValue pvc = new PermissionValue();
90 pvc.setResourceName(rc);
91 pvc.setPermissionId(coPermId);
92 permValues.put(pvc.getResourceName(), pvc);
94 String ri = "intakes";
95 String iPermId = createPermission(ri, EffectType.DENY);
96 PermissionValue pvi = new PermissionValue();
97 pvi.setResourceName(ri);
98 pvi.setPermissionId(iPermId);
99 permValues.put(pvi.getResourceName(), pvi);
101 String rn1 = "ROLE_CO1";
102 String r1RoleId = createRole(rn1);
103 RoleValue rv1 = new RoleValue();
104 rv1.setRoleId(r1RoleId);
105 rv1.setRoleName(rn1);
106 roleValues.put(rv1.getRoleName(), rv1);
108 String rn2 = "ROLE_CO2";
109 String r2RoleId = createRole(rn2);
110 RoleValue rv2 = new RoleValue();
111 rv2.setRoleId(r2RoleId);
112 rv2.setRoleName(rn2);
113 roleValues.put(rv2.getRoleName(), rv2);
116 // ---------------------------------------------------------------
117 // CRUD tests : CREATE tests
118 // ---------------------------------------------------------------
121 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
122 public void create(String testName) throws Exception {
124 // Perform setup, such as initializing the type of service request
125 // (e.g. CREATE, DELETE), its valid and expected status codes, and
126 // its associated HTTP method name (e.g. POST, DELETE).
127 setupCreate(testName);
129 // Submit the request to the service and store the response.
130 PermissionValue pv = permValues.get("accounts");
131 PermissionRole permRole = createPermissionRoleInstance(pv,
132 roleValues.values(), true, true);
133 PermissionRoleClient client = new PermissionRoleClient();
134 ClientResponse<Response> res = client.create(pv.getPermissionId(), permRole);
135 int statusCode = res.getStatus();
137 if (logger.isDebugEnabled()) {
138 logger.debug(testName + ": status = " + statusCode);
140 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
141 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
142 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
144 // Store the ID returned from this create operation
145 // for additional tests below.
146 //this is is not important in case of this relationship
147 knownResourceId = extractId(res);
148 if (logger.isDebugEnabled()) {
149 logger.debug(testName + ": knownResourceId=" + knownResourceId);
153 //to not cause uniqueness violation for permRole, createList is removed
155 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
156 dependsOnMethods = {"create"})
157 public void createList(String testName) throws Exception {
159 setupCreate(testName);
160 // Submit the request to the service and store the response.
161 PermissionRoleClient client = new PermissionRoleClient();
162 PermissionValue pv = permValues.get("collectionobjects");
163 PermissionRole permRole = createPermissionRoleInstance(pv,
164 roleValues.values(), true, true);
165 ClientResponse<Response> res = client.create(pv.getPermissionId(), permRole);
166 int statusCode = res.getStatus();
167 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
168 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
169 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
170 //id of relationship is not important
171 allResourceIdsCreated.add(pv.getPermissionId());
173 PermissionValue pv2 = permValues.get("intakes");
174 PermissionRole permRole2 = createPermissionRoleInstance(pv2,
175 roleValues.values(), true, true);
176 res = client.create(pv2.getPermissionId(), permRole2);
177 statusCode = res.getStatus();
178 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
181 //id of relationship is not important
182 allResourceIdsCreated.add(pv2.getPermissionId());
187 // Placeholders until the three tests below can be uncommented.
188 // See Issue CSPACE-401.
190 public void createWithEmptyEntityBody(String testName) throws Exception {
194 public void createWithMalformedXml(String testName) throws Exception {
198 public void createWithWrongXmlSchema(String testName) throws Exception {
201 // ---------------------------------------------------------------
202 // CRUD tests : READ tests
203 // ---------------------------------------------------------------
206 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
207 dependsOnMethods = {"create"})
208 public void read(String testName) throws Exception {
213 // Submit the request to the service and store the response.
214 PermissionRoleClient client = new PermissionRoleClient();
215 ClientResponse<PermissionRole> res = client.read(
216 permValues.get("accounts").getPermissionId(), "123");
217 int statusCode = res.getStatus();
219 // Check the status code of the response: does it match
220 // the expected response(s)?
221 if (logger.isDebugEnabled()) {
222 logger.debug(testName + ": status = " + statusCode);
224 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
225 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
226 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
228 PermissionRole output = (PermissionRole) res.getEntity();
229 Assert.assertNotNull(output);
234 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
235 public void readNonExistent(String testName) throws Exception {
238 setupReadNonExistent(testName);
240 // Submit the request to the service and store the response.
241 PermissionRoleClient client = new PermissionRoleClient();
242 ClientResponse<PermissionRole> res = client.read(NON_EXISTENT_ID, "123");
243 int statusCode = res.getStatus();
245 // Check the status code of the response: does it match
246 // the expected response(s)?
247 if (logger.isDebugEnabled()) {
248 logger.debug(testName + ": status = " + statusCode);
250 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
251 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
252 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
255 // ---------------------------------------------------------------
256 // CRUD tests : READ_LIST tests
257 // ---------------------------------------------------------------
260 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
261 dependsOnMethods = {"createList", "read"})
262 public void readList(String testName) throws Exception {
267 // ---------------------------------------------------------------
268 // CRUD tests : UPDATE tests
269 // ---------------------------------------------------------------
272 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
273 dependsOnMethods = {"read", "readList", "readNonExistent"})
274 public void update(String testName) throws Exception {
278 // Placeholders until the three tests below can be uncommented.
279 // See Issue CSPACE-401.
281 public void updateWithEmptyEntityBody(String testName) throws Exception {
285 public void updateWithMalformedXml(String testName) throws Exception {
289 public void updateWithWrongXmlSchema(String testName) throws Exception {
293 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
294 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
295 public void updateNonExistent(String testName) throws Exception {
298 // ---------------------------------------------------------------
299 // CRUD tests : DELETE tests
300 // ---------------------------------------------------------------
303 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
304 dependsOnMethods = {"read"})
305 public void delete(String testName) throws Exception {
308 setupDelete(testName);
310 // Submit the request to the service and store the response.
311 PermissionRoleClient client = new PermissionRoleClient();
312 ClientResponse<Response> res = client.delete(
313 permValues.get("accounts").getPermissionId(), "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);
328 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
329 public void deleteNonExistent(String testName) throws Exception {
332 setupDeleteNonExistent(testName);
334 // Submit the request to the service and store the response.
335 PermissionRoleClient client = new PermissionRoleClient();
336 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID, "123");
337 int statusCode = res.getStatus();
339 // Check the status code of the response: does it match
340 // the expected response(s)?
341 if (logger.isDebugEnabled()) {
342 logger.debug(testName + ": status = " + statusCode);
344 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
345 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
346 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
349 // ---------------------------------------------------------------
350 // Utility tests : tests of code used in tests above
351 // ---------------------------------------------------------------
353 * Tests the code for manually submitting data that is used by several
354 * of the methods above.
356 @Test(dependsOnMethods = {"create"})
357 public void testSubmitRequest() throws Exception {
359 // Expected status code: 200 OK
360 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
362 // Submit the request to the service and store the response.
363 String method = ServiceRequestType.READ.httpMethodName();
364 String url = getResourceURL(permValues.get("accounts").getPermissionId());
365 int statusCode = submitRequest(method, url);
367 // Check the status code of the response: does it match
368 // the expected response(s)?
369 if (logger.isDebugEnabled()) {
370 logger.debug("testSubmitRequest: url=" + url
371 + " status=" + statusCode);
373 Assert.assertEquals(statusCode, EXPECTED_STATUS);
377 // ---------------------------------------------------------------
378 // Utility methods used by tests above
379 // ---------------------------------------------------------------
381 * create permRolerole instance
383 * @param roleValues array of role ids
388 private PermissionRole createPermissionRoleInstance(PermissionValue pv,
389 Collection<RoleValue> rvs,
393 PermissionRole permRole = new PermissionRole();
394 //service consume is not required to provide subject as it is determined
396 // permRole.setSubject(SubjectType.ROLE);
398 ArrayList<PermissionValue> pvs = new ArrayList<PermissionValue>();
400 permRole.setPermissions(pvs);
403 //FIXME is there a better way?
404 ArrayList<RoleValue> rvas = new ArrayList<RoleValue>();
405 for (RoleValue rv : rvs) {
408 permRole.setRoles(rvas);
411 if (logger.isDebugEnabled()) {
412 logger.debug("to be created, permRole common");
413 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
418 @AfterClass(alwaysRun = true)
419 public void cleanUp() {
420 setupDelete("delete");
421 if (logger.isDebugEnabled()) {
422 logger.debug("clenaup: Cleaning up temporary resources created for testing ...");
424 PermissionRoleClient client = new PermissionRoleClient();
425 for (String resourceId : allResourceIdsCreated) {
427 // Note: Any non-success responses are ignored and not reported.
428 ClientResponse<Response> res = client.delete(resourceId, "123");
429 int statusCode = res.getStatus();
430 if (logger.isDebugEnabled()) {
431 logger.debug("clenaup: delete relationships for permission id="
432 + resourceId + " status=" + statusCode);
434 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
435 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
436 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
439 for (PermissionValue pv : permValues.values()) {
440 deletePermission(pv.getPermissionId());
443 for (RoleValue rv : roleValues.values()) {
444 deleteRole(rv.getRoleId());
448 private String createPermission(String resName, EffectType effect) {
450 PermissionClient permClient = new PermissionClient();
451 List<PermissionAction> actions = PermissionServiceTest.getDefaultActions();
452 Permission permission = PermissionServiceTest.createPermissionInstance(resName,
453 "default permissions for " + resName,
454 actions, EffectType.PERMIT, true, true, true);
455 ClientResponse<Response> res = permClient.create(permission);
456 int statusCode = res.getStatus();
457 if (logger.isDebugEnabled()) {
458 logger.debug("createPermission: resName=" + resName +
459 " status = " + statusCode);
461 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
462 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
463 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
464 return extractId(res);
467 private void deletePermission(String permId) {
469 PermissionClient permClient = new PermissionClient();
470 ClientResponse<Response> res = permClient.delete(permId);
471 int statusCode = res.getStatus();
472 if (logger.isDebugEnabled()) {
473 logger.debug("deletePermission: delete permission id="
474 + permId + " status=" + statusCode);
476 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
477 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
478 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
481 private String createRole(String roleName) {
483 RoleClient roleClient = new RoleClient();
485 Role role = RoleServiceTest.createRoleInstance(roleName,
486 "role for " + roleName, true);
487 ClientResponse<Response> res = roleClient.create(role);
488 int statusCode = res.getStatus();
489 if (logger.isDebugEnabled()) {
490 logger.debug("createRole: name=" + roleName +
491 " status = " + statusCode);
493 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
494 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
495 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
496 return extractId(res);
499 private void deleteRole(String roleId) {
501 RoleClient roleClient = new RoleClient();
502 ClientResponse<Response> res = roleClient.delete(roleId);
503 int statusCode = res.getStatus();
504 if (logger.isDebugEnabled()) {
505 logger.debug("deleteRole: delete role id=" + roleId +
506 " status=" + statusCode);
508 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
509 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
510 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);