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.RoleClient;
43 import org.collectionspace.services.client.RoleFactory;
44 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
45 import org.collectionspace.services.client.test.ServiceRequestType;
46 import org.collectionspace.services.jaxb.AbstractCommonList;
47 import org.jboss.resteasy.client.ClientResponse;
49 import org.testng.Assert;
50 import org.testng.annotations.Test;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.testng.annotations.AfterClass;
55 import org.testng.annotations.BeforeClass;
58 * PermissionServiceTest, carries out tests against a
59 * deployed and running Permission, Role and PermissionRole Services.
61 * $LastChangedRevision: 917 $
62 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
64 public class PermissionRoleServiceTest extends AbstractServiceTestImpl {
66 static private final Logger logger =
67 LoggerFactory.getLogger(PermissionRoleServiceTest.class);
68 // Instance variables specific to this test.
69 private String knownResourceId = null;
70 private List<String> allResourceIdsCreated = new ArrayList();
71 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
72 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
74 * This method is called only by the parent class, AbstractServiceTestImpl
78 protected String getServicePathComponent() {
79 return new PermissionRoleClient().getServicePathComponent();
82 @BeforeClass(alwaysRun = true)
83 public void seedData() {
84 String ra = "accounts";
85 String accPermId = createPermission(ra, EffectType.PERMIT);
86 PermissionValue pva = new PermissionValue();
87 pva.setResourceName(ra);
88 pva.setPermissionId(accPermId);
89 permValues.put(pva.getResourceName(), pva);
91 String rc = "collectionobjects";
92 String coPermId = createPermission(rc, EffectType.DENY);
93 PermissionValue pvc = new PermissionValue();
94 pvc.setResourceName(rc);
95 pvc.setPermissionId(coPermId);
96 permValues.put(pvc.getResourceName(), pvc);
98 String ri = "intakes";
99 String iPermId = createPermission(ri, EffectType.DENY);
100 PermissionValue pvi = new PermissionValue();
101 pvi.setResourceName(ri);
102 pvi.setPermissionId(iPermId);
103 permValues.put(pvi.getResourceName(), pvi);
105 String rn1 = "ROLE_CO1";
106 String r1RoleId = createRole(rn1);
107 RoleValue rv1 = new RoleValue();
108 rv1.setRoleId(r1RoleId);
109 rv1.setRoleName(rn1);
110 roleValues.put(rv1.getRoleName(), rv1);
112 String rn2 = "ROLE_CO2";
113 String r2RoleId = createRole(rn2);
114 RoleValue rv2 = new RoleValue();
115 rv2.setRoleId(r2RoleId);
116 rv2.setRoleName(rn2);
117 roleValues.put(rv2.getRoleName(), rv2);
121 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
124 protected CollectionSpaceClient getClientInstance() {
125 return new PermissionRoleClient();
129 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
132 protected AbstractCommonList getAbstractCommonList(
133 ClientResponse<AbstractCommonList> response) {
134 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
135 throw new UnsupportedOperationException();
138 @Test(dataProvider = "testName")
140 public void readPaginatedList(String testName) throws Exception {
141 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
143 // ---------------------------------------------------------------
144 // CRUD tests : CREATE tests
145 // ---------------------------------------------------------------
148 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
149 public void create(String testName) throws Exception {
151 // Perform setup, such as initializing the type of service request
152 // (e.g. CREATE, DELETE), its valid and expected status codes, and
153 // its associated HTTP method name (e.g. POST, DELETE).
154 setupCreate(testName);
156 // Submit the request to the service and store the response.
157 PermissionValue pv = permValues.get("accounts");
158 PermissionRole permRole = createPermissionRoleInstance(pv,
159 roleValues.values(), true, true);
160 PermissionRoleClient client = new PermissionRoleClient();
161 ClientResponse<Response> res = client.create(pv.getPermissionId(), permRole);
162 int statusCode = res.getStatus();
164 if (logger.isDebugEnabled()) {
165 logger.debug(testName + ": status = " + statusCode);
167 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
168 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
169 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
171 // Store the ID returned from this create operation
172 // for additional tests below.
173 //this is is not important in case of this relationship
174 knownResourceId = extractId(res);
175 if (logger.isDebugEnabled()) {
176 logger.debug(testName + ": knownResourceId=" + knownResourceId);
180 //to not cause uniqueness violation for permRole, createList is removed
182 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
183 dependsOnMethods = {"create"})
184 public void createList(String testName) throws Exception {
188 // Placeholders until the three tests below can be uncommented.
189 // See Issue CSPACE-401.
191 public void createWithEmptyEntityBody(String testName) throws Exception {
195 public void createWithMalformedXml(String testName) throws Exception {
199 public void createWithWrongXmlSchema(String testName) throws Exception {
202 // ---------------------------------------------------------------
203 // CRUD tests : READ tests
204 // ---------------------------------------------------------------
207 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
208 dependsOnMethods = {"create"})
209 public void read(String testName) throws Exception {
214 // Submit the request to the service and store the response.
215 PermissionRoleClient client = new PermissionRoleClient();
216 ClientResponse<PermissionRole> res = client.read(
217 permValues.get("accounts").getPermissionId(), "123");
218 int statusCode = res.getStatus();
220 // Check the status code of the response: does it match
221 // the expected response(s)?
222 if (logger.isDebugEnabled()) {
223 logger.debug(testName + ": status = " + statusCode);
225 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
226 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
227 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
229 PermissionRole output = (PermissionRole) res.getEntity();
230 Assert.assertNotNull(output);
235 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
236 public void readNonExistent(String testName) throws Exception {
239 setupReadNonExistent(testName);
241 // Submit the request to the service and store the response.
242 PermissionRoleClient client = new PermissionRoleClient();
243 ClientResponse<PermissionRole> res = client.read(NON_EXISTENT_ID, "123");
244 int statusCode = res.getStatus();
246 // Check the status code of the response: does it match
247 // the expected response(s)?
248 if (logger.isDebugEnabled()) {
249 logger.debug(testName + ": status = " + statusCode);
251 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
252 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
253 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
256 // ---------------------------------------------------------------
257 // CRUD tests : READ_LIST tests
258 // ---------------------------------------------------------------
261 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
262 dependsOnMethods = {"createList", "read"})
263 public void readList(String testName) throws Exception {
268 // ---------------------------------------------------------------
269 // CRUD tests : UPDATE tests
270 // ---------------------------------------------------------------
273 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
274 dependsOnMethods = {"read", "readList", "readNonExistent"})
275 public void update(String testName) throws Exception {
279 // Placeholders until the three tests below can be uncommented.
280 // See Issue CSPACE-401.
282 public void updateWithEmptyEntityBody(String testName) throws Exception {
286 public void updateWithMalformedXml(String testName) throws Exception {
290 public void updateWithWrongXmlSchema(String testName) throws Exception {
294 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
295 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
296 public void updateNonExistent(String testName) throws Exception {
299 // ---------------------------------------------------------------
300 // CRUD tests : DELETE tests
301 // ---------------------------------------------------------------
304 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
305 dependsOnMethods = {"read"})
306 public void delete(String testName) throws Exception {
309 setupDelete(testName);
311 // Submit the request to the service and store the response.
312 PermissionRoleClient client = new PermissionRoleClient();
313 ClientResponse<Response> res = client.delete(
314 permValues.get("accounts").getPermissionId(), "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);
329 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
330 public void deleteNonExistent(String testName) throws Exception {
331 //ignoring this test as the service side returns 200 now even if it does
332 //not find a record in the db
335 // ---------------------------------------------------------------
336 // Utility tests : tests of code used in tests above
337 // ---------------------------------------------------------------
339 * Tests the code for manually submitting data that is used by several
340 * of the methods above.
342 @Test(dependsOnMethods = {"create"})
343 public void testSubmitRequest() throws Exception {
345 // Expected status code: 200 OK
346 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
348 // Submit the request to the service and store the response.
349 String method = ServiceRequestType.READ.httpMethodName();
350 String url = getResourceURL(permValues.get("accounts").getPermissionId());
351 int statusCode = submitRequest(method, url);
353 // Check the status code of the response: does it match
354 // the expected response(s)?
355 if (logger.isDebugEnabled()) {
356 logger.debug("testSubmitRequest: url=" + url
357 + " status=" + statusCode);
359 Assert.assertEquals(statusCode, EXPECTED_STATUS);
363 // ---------------------------------------------------------------
364 // Utility methods used by tests above
365 // ---------------------------------------------------------------
367 * create permRolerole instance
369 * @param roleValues array of role ids
374 public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
375 Collection<RoleValue> rvs,
379 PermissionRole permRole = new PermissionRole();
380 //service consume is not required to provide subject as it is determined
382 // permRole.setSubject(SubjectType.ROLE);
384 ArrayList<PermissionValue> pvs = new ArrayList<PermissionValue>();
386 permRole.setPermissions(pvs);
389 //FIXME is there a better way?
390 ArrayList<RoleValue> rvas = new ArrayList<RoleValue>();
391 for (RoleValue rv : rvs) {
394 permRole.setRoles(rvas);
397 if (logger.isDebugEnabled()) {
398 logger.debug("to be created, permRole common");
399 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
404 @AfterClass(alwaysRun = true)
405 public void cleanUp() {
406 setupDelete("delete");
407 String noTest = System.getProperty("noTestCleanup");
408 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
409 if (logger.isDebugEnabled()) {
410 logger.debug("Skipping Cleanup phase ...");
414 if (logger.isDebugEnabled()) {
415 logger.debug("Cleaning up temporary resources created for testing ...");
417 PermissionRoleClient client = new PermissionRoleClient();
418 for (String resourceId : allResourceIdsCreated) {
420 ClientResponse<Response> res = client.delete(resourceId, "123");
421 int statusCode = res.getStatus();
422 if (logger.isDebugEnabled()) {
423 logger.debug("cleanup: delete relationships for permission id="
424 + resourceId + " status=" + statusCode);
426 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
427 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
428 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
431 for (PermissionValue pv : permValues.values()) {
432 deletePermission(pv.getPermissionId());
435 for (RoleValue rv : roleValues.values()) {
436 deleteRole(rv.getRoleId());
440 private String createPermission(String resName, EffectType effect) {
442 PermissionClient permClient = new PermissionClient();
443 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
444 Permission permission = PermissionServiceTest.createPermissionInstance(resName,
445 "default permissions for " + resName,
446 actions, effect, true, true, true);
447 ClientResponse<Response> res = permClient.create(permission);
448 int statusCode = res.getStatus();
449 if (logger.isDebugEnabled()) {
450 logger.debug("createPermission: resName=" + resName
451 + " status = " + statusCode);
453 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
454 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
455 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
456 return extractId(res);
459 private void deletePermission(String permId) {
461 PermissionClient permClient = new PermissionClient();
462 ClientResponse<Response> res = permClient.delete(permId);
463 int statusCode = res.getStatus();
464 if (logger.isDebugEnabled()) {
465 logger.debug("deletePermission: delete permission id="
466 + permId + " status=" + statusCode);
468 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
469 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
470 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
473 private String createRole(String roleName) {
475 RoleClient roleClient = new RoleClient();
477 Role role = RoleFactory.createRoleInstance(roleName,
478 "role for " + roleName, true);
479 ClientResponse<Response> res = roleClient.create(role);
480 int statusCode = res.getStatus();
481 if (logger.isDebugEnabled()) {
482 logger.debug("createRole: name=" + roleName
483 + " status = " + statusCode);
485 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
486 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
487 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
488 return extractId(res);
491 private void deleteRole(String roleId) {
493 RoleClient roleClient = new RoleClient();
494 ClientResponse<Response> res = roleClient.delete(roleId);
495 int statusCode = res.getStatus();
496 if (logger.isDebugEnabled()) {
497 logger.debug("deleteRole: delete role id=" + roleId
498 + " status=" + statusCode);
500 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
501 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
502 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);