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.Hashtable;
27 import java.util.List;
28 import javax.ws.rs.core.Response;
29 import org.collectionspace.services.authorization.EffectType;
31 import org.collectionspace.services.authorization.Permission;
32 import org.collectionspace.services.authorization.PermissionAction;
33 import org.collectionspace.services.authorization.PermissionRole;
34 import org.collectionspace.services.authorization.Role;
35 import org.collectionspace.services.client.PermissionClient;
36 import org.collectionspace.services.client.PermissionRoleClient;
37 import org.collectionspace.services.client.RoleClient;
38 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
39 import org.collectionspace.services.client.test.ServiceRequestType;
40 import org.jboss.resteasy.client.ClientResponse;
42 import org.testng.Assert;
43 import org.testng.annotations.Test;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.testng.annotations.AfterClass;
48 import org.testng.annotations.BeforeClass;
51 * PermissionServiceTest, carries out tests against a
52 * deployed and running Permission, Role and PermissionRole Services.
54 * $LastChangedRevision: 917 $
55 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
57 public class PermissionRoleServiceTest extends AbstractServiceTestImpl {
59 private final Logger logger =
60 LoggerFactory.getLogger(PermissionRoleServiceTest.class);
61 // Instance variables specific to this test.
62 private PermissionRoleClient client = new PermissionRoleClient();
63 private String knownResourceId = null;
64 private List<String> allResourceIdsCreated = new ArrayList();
65 private Hashtable<String, String> permIds = new Hashtable<String, String>();
66 private Hashtable<String, String> roleIds = new Hashtable<String, String>();
68 * This method is called only by the parent class, AbstractServiceTestImpl
72 protected String getServicePathComponent() {
73 return client.getServicePathComponent();
76 @BeforeClass(alwaysRun = true)
77 public void seedData() {
78 String accPermId = createPermission("accounts", EffectType.PERMIT);
79 permIds.put("accounts", accPermId);
81 String coPermId = createPermission("collectionobjects", EffectType.DENY);
82 permIds.put("collectionobjects", coPermId);
84 String iPermId = createPermission("intakes", EffectType.DENY);
85 permIds.put("intakes", iPermId);
87 String r1RoleId = createRole("ROLE_CO1");
88 roleIds.put("ROLE_1", r1RoleId);
90 String r2RoleId = createRole("ROLE_CO2");
91 roleIds.put("ROLE_2", r2RoleId);
94 // ---------------------------------------------------------------
95 // CRUD tests : CREATE tests
96 // ---------------------------------------------------------------
99 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
100 public void create(String testName) throws Exception {
102 // Perform setup, such as initializing the type of service request
103 // (e.g. CREATE, DELETE), its valid and expected status codes, and
104 // its associated HTTP method name (e.g. POST, DELETE).
105 setupCreate(testName);
107 // Submit the request to the service and store the response.
108 PermissionRole permRole = createPermissionRoleInstance(permIds.get("accounts"),
109 roleIds.values().toArray(new String[0]), true, true);
110 ClientResponse<Response> res = client.create(permIds.get("accounts"), permRole);
111 int statusCode = res.getStatus();
113 if (logger.isDebugEnabled()) {
114 logger.debug(testName + ": status = " + statusCode);
116 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
117 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
118 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
120 // Store the ID returned from this create operation
121 // for additional tests below.
122 //this is is not important in case of this relationship
123 knownResourceId = extractId(res);
124 if (logger.isDebugEnabled()) {
125 logger.debug(testName + ": knownResourceId=" + knownResourceId);
129 //to not cause uniqueness violation for permRole, createList is removed
131 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
132 dependsOnMethods = {"create"})
133 public void createList(String testName) throws Exception {
135 setupCreate(testName);
136 // Submit the request to the service and store the response.
137 PermissionRole permRole = createPermissionRoleInstance(permIds.get("collectionobjects"),
138 roleIds.values().toArray(new String[0]), true, true);
139 ClientResponse<Response> res = client.create(permIds.get("collectionobjects"), permRole);
140 int statusCode = res.getStatus();
141 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
142 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
143 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
144 //id of relationship is not important
145 allResourceIdsCreated.add(permIds.get("collectionobjects"));
147 PermissionRole permRole2 = createPermissionRoleInstance(permIds.get("intakes"),
148 roleIds.values().toArray(new String[0]), true, true);
149 res = client.create(permIds.get("intakes"), permRole2);
150 statusCode = res.getStatus();
151 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
152 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
153 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
154 //id of relationship is not important
155 allResourceIdsCreated.add(permIds.get("intakes"));
160 // Placeholders until the three tests below can be uncommented.
161 // See Issue CSPACE-401.
163 public void createWithEmptyEntityBody(String testName) throws Exception {
167 public void createWithMalformedXml(String testName) throws Exception {
171 public void createWithWrongXmlSchema(String testName) throws Exception {
174 // ---------------------------------------------------------------
175 // CRUD tests : READ tests
176 // ---------------------------------------------------------------
179 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
180 dependsOnMethods = {"create"})
181 public void read(String testName) throws Exception {
186 // Submit the request to the service and store the response.
187 ClientResponse<PermissionRole> res = client.read(permIds.get("accounts"), "123");
188 int statusCode = res.getStatus();
190 // Check the status code of the response: does it match
191 // the expected response(s)?
192 if (logger.isDebugEnabled()) {
193 logger.debug(testName + ": status = " + statusCode);
195 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
199 PermissionRole output = (PermissionRole) res.getEntity();
200 Assert.assertNotNull(output);
205 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
206 public void readNonExistent(String testName) throws Exception {
209 setupReadNonExistent(testName);
211 // Submit the request to the service and store the response.
212 ClientResponse<PermissionRole> res = client.read(NON_EXISTENT_ID, "123");
213 int statusCode = res.getStatus();
215 // Check the status code of the response: does it match
216 // the expected response(s)?
217 if (logger.isDebugEnabled()) {
218 logger.debug(testName + ": status = " + statusCode);
220 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
221 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
222 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
225 // ---------------------------------------------------------------
226 // CRUD tests : READ_LIST tests
227 // ---------------------------------------------------------------
230 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
231 dependsOnMethods = {"createList", "read"})
232 public void readList(String testName) throws Exception {
237 // ---------------------------------------------------------------
238 // CRUD tests : UPDATE tests
239 // ---------------------------------------------------------------
242 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
243 dependsOnMethods = {"read", "readList", "readNonExistent"})
244 public void update(String testName) throws Exception {
248 // Placeholders until the three tests below can be uncommented.
249 // See Issue CSPACE-401.
251 public void updateWithEmptyEntityBody(String testName) throws Exception {
255 public void updateWithMalformedXml(String testName) throws Exception {
259 public void updateWithWrongXmlSchema(String testName) throws Exception {
263 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
264 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
265 public void updateNonExistent(String testName) throws Exception {
268 // ---------------------------------------------------------------
269 // CRUD tests : DELETE tests
270 // ---------------------------------------------------------------
273 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
274 dependsOnMethods = {"read"})
275 public void delete(String testName) throws Exception {
278 setupDelete(testName);
280 // Submit the request to the service and store the response.
281 ClientResponse<Response> res = client.delete(permIds.get("accounts"), "123");
282 int statusCode = res.getStatus();
284 // Check the status code of the response: does it match
285 // the expected response(s)?
286 if (logger.isDebugEnabled()) {
287 logger.debug(testName + ": status = " + statusCode);
289 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
290 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
291 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
297 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
298 public void deleteNonExistent(String testName) throws Exception {
301 setupDeleteNonExistent(testName);
303 // Submit the request to the service and store the response.
304 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID, "123");
305 int statusCode = res.getStatus();
307 // Check the status code of the response: does it match
308 // the expected response(s)?
309 if (logger.isDebugEnabled()) {
310 logger.debug(testName + ": status = " + statusCode);
312 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
313 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
314 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
317 // ---------------------------------------------------------------
318 // Utility tests : tests of code used in tests above
319 // ---------------------------------------------------------------
321 * Tests the code for manually submitting data that is used by several
322 * of the methods above.
324 @Test(dependsOnMethods = {"create"})
325 public void testSubmitRequest() throws Exception {
327 // Expected status code: 200 OK
328 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
330 // Submit the request to the service and store the response.
331 String method = ServiceRequestType.READ.httpMethodName();
332 String url = getResourceURL(permIds.get("accounts"));
333 int statusCode = submitRequest(method, url);
335 // Check the status code of the response: does it match
336 // the expected response(s)?
337 if (logger.isDebugEnabled()) {
338 logger.debug("testSubmitRequest: url=" + url
339 + " status=" + statusCode);
341 Assert.assertEquals(statusCode, EXPECTED_STATUS);
345 // ---------------------------------------------------------------
346 // Utility methods used by tests above
347 // ---------------------------------------------------------------
349 * create permRolerole instance
351 * @param roleIds array of role ids
356 private PermissionRole createPermissionRoleInstance(String permId,
361 PermissionRole permRole = new PermissionRole();
362 //service consume is not required to provide subject as it is determined
364 // permRole.setSubject(SubjectType.ROLE);
366 ArrayList<String> pl = new ArrayList<String>();
368 permRole.setPermissionIds(pl);
371 ArrayList<String> rl = new ArrayList<String>();
372 for (String roleId : roleIds) {
375 permRole.setRoleIds(rl);
378 if (logger.isDebugEnabled()) {
379 logger.debug("to be created, permRole common");
380 logger.debug(objectAsXmlString(permRole, PermissionRole.class));
385 @AfterClass(alwaysRun = true)
386 public void cleanUp() {
387 setupDelete("delete");
388 if (logger.isDebugEnabled()) {
389 logger.debug("Cleaning up temporary resources created for testing ...");
391 for (String resourceId : allResourceIdsCreated) {
392 // Note: Any non-success responses are ignored and not reported.
393 ClientResponse<Response> res = client.delete(resourceId, "123");
394 int statusCode = res.getStatus();
395 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
396 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
397 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
400 for (String permId : permIds.values()) {
401 deletePermission(permId);
404 for (String roleId : roleIds.values()) {
409 private String createPermission(String resName, EffectType effect) {
411 PermissionClient permClient = new PermissionClient();
412 List<PermissionAction> actions = PermissionServiceTest.getDefaultActions();
413 Permission permission = PermissionServiceTest.createPermissionInstance(resName,
414 "default permissions for " + resName,
415 actions, EffectType.PERMIT, true, true, true);
416 ClientResponse<Response> res = permClient.create(permission);
417 int statusCode = res.getStatus();
418 if (logger.isDebugEnabled()) {
419 logger.debug("createPermission" + ": status = " + statusCode);
421 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
422 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
423 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
424 return extractId(res);
427 private void deletePermission(String permId) {
429 PermissionClient permClient = new PermissionClient();
430 ClientResponse<Response> res = permClient.delete(permId);
431 int statusCode = res.getStatus();
432 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
433 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
434 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
437 private String createRole(String roleName) {
439 RoleClient roleClient = new RoleClient();
441 Role role = RoleServiceTest.createRoleInstance(roleName,
442 "role for " + roleName, true);
443 ClientResponse<Response> res = roleClient.create(role);
444 int statusCode = res.getStatus();
445 if (logger.isDebugEnabled()) {
446 logger.debug("createRole" + ": status = " + statusCode);
448 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
449 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
450 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
451 return extractId(res);
454 private void deleteRole(String roleId) {
456 RoleClient roleClient = new RoleClient();
457 ClientResponse<Response> res = roleClient.delete(roleId);
458 int statusCode = res.getStatus();
459 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
460 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
461 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);