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 permissions and
21 * limitations under the License.
23 package org.collectionspace.services.authorization.client.test;
25 import java.util.ArrayList;
26 import java.util.List;
27 import javax.ws.rs.core.Response;
28 import org.collectionspace.services.authorization.ActionType;
29 import org.collectionspace.services.authorization.EffectType;
31 import org.collectionspace.services.client.PermissionClient;
32 import org.collectionspace.services.authorization.Permission;
33 import org.collectionspace.services.authorization.PermissionAction;
34 import org.collectionspace.services.authorization.PermissionsList;
35 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
36 import org.collectionspace.services.client.test.ServiceRequestType;
37 import org.jboss.resteasy.client.ClientResponse;
39 import org.testng.Assert;
40 import org.testng.annotations.Test;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.testng.annotations.AfterClass;
47 * PermissionServiceTest, carries out tests against a
48 * deployed and running Permission Service.
50 * $LastChangedRevision: 917 $
51 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
53 public class PermissionServiceTest extends AbstractServiceTestImpl {
55 static private final Logger logger =
56 LoggerFactory.getLogger(PermissionServiceTest.class);
57 // Instance variables specific to this test.
58 private PermissionClient client = new PermissionClient();
59 private String knownResourceId = null;
60 private List<String> allResourceIdsCreated = new ArrayList();
61 boolean addTenant = true;
63 * This method is called only by the parent class, AbstractServiceTestImpl
67 protected String getServicePathComponent() {
68 return client.getServicePathComponent();
71 // ---------------------------------------------------------------
72 // CRUD tests : CREATE tests
73 // ---------------------------------------------------------------
76 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
77 public void create(String testName) throws Exception {
79 // Perform setup, such as initializing the type of service request
80 // (e.g. CREATE, DELETE), its valid and expected status codes, and
81 // its associated HTTP method name (e.g. POST, DELETE).
82 setupCreate(testName);
84 // Submit the request to the service and store the response.
85 List<PermissionAction> actions = getDefaultActions();
86 Permission permission = createPermissionInstance("accounts",
87 "default permissions for account",
93 ClientResponse<Response> res = client.create(permission);
94 int statusCode = res.getStatus();
96 // Check the status code of the response: does it match
97 // the expected response(s)?
100 // Does it fall within the set of valid status codes?
101 // Does it exactly match the expected status code?
102 if (logger.isDebugEnabled()) {
103 logger.debug(testName + ": status = " + statusCode);
105 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
106 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
107 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
109 // Store the ID returned from this create operation
110 // for additional tests below.
111 knownResourceId = extractId(res);
112 if (logger.isDebugEnabled()) {
113 logger.debug(testName + ": knownResourceId=" + knownResourceId);
117 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
118 dependsOnMethods = {"create"})
119 public void createWithoutResourceName(String testName) throws Exception {
121 setupCreate(testName);
123 // Submit the request to the service and store the response.
124 List<PermissionAction> actions = getDefaultActions();
125 Permission permission = createPermissionInstance(null,
126 "default permissions for account",
132 ClientResponse<Response> res = client.create(permission);
133 int statusCode = res.getStatus();
134 // Does it exactly match the expected status code?
135 if (logger.isDebugEnabled()) {
136 logger.debug(testName + ": status = " + statusCode);
138 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
139 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
140 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
143 //to not cause uniqueness violation for permission, createList is removed
145 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
146 dependsOnMethods = {"create"})
147 public void createList(String testName) throws Exception {
149 setupCreate(testName);
150 // Submit the request to the service and store the response.
151 List<PermissionAction> actions = getDefaultActions();
152 Permission permission1 = createPermissionInstance("collectionobjects",
153 "default permissions for collectionobjects",
159 ClientResponse<Response> res = client.create(permission1);
160 int statusCode = res.getStatus();
161 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
162 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
163 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
164 allResourceIdsCreated.add(extractId(res));
166 Permission permission2 = createPermissionInstance("acquisitions",
167 "default permissions for acquisitions",
173 res = client.create(permission2);
174 statusCode = res.getStatus();
175 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
176 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
177 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
178 allResourceIdsCreated.add(extractId(res));
180 Permission permission3 = createPermissionInstance("ids",
181 "default permissions for id service",
187 res = client.create(permission3);
188 statusCode = res.getStatus();
189 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
190 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
191 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
192 allResourceIdsCreated.add(extractId(res));
196 // Placeholders until the three tests below can be uncommented.
197 // See Issue CSPACE-401.
199 public void createWithEmptyEntityBody(String testName) throws Exception {
203 public void createWithMalformedXml(String testName) throws Exception {
207 public void createWithWrongXmlSchema(String testName) throws Exception {
210 // ---------------------------------------------------------------
211 // CRUD tests : READ tests
212 // ---------------------------------------------------------------
215 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
216 dependsOnMethods = {"create"})
217 public void read(String testName) throws Exception {
222 // Submit the request to the service and store the response.
223 ClientResponse<Permission> res = client.read(knownResourceId);
224 int statusCode = res.getStatus();
226 // Check the status code of the response: does it match
227 // the expected response(s)?
228 if (logger.isDebugEnabled()) {
229 logger.debug(testName + ": status = " + statusCode);
231 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
232 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
233 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
235 Permission output = (Permission) res.getEntity();
236 Assert.assertNotNull(output);
241 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
242 dependsOnMethods = {"read"})
243 public void readNonExistent(String testName) throws Exception {
246 setupReadNonExistent(testName);
248 // Submit the request to the service and store the response.
249 ClientResponse<Permission> res = client.read(NON_EXISTENT_ID);
250 int statusCode = res.getStatus();
252 // Check the status code of the response: does it match
253 // the expected response(s)?
254 if (logger.isDebugEnabled()) {
255 logger.debug(testName + ": status = " + statusCode);
257 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
258 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
259 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
262 // ---------------------------------------------------------------
263 // CRUD tests : READ_LIST tests
264 // ---------------------------------------------------------------
267 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
268 dependsOnMethods = {"createList", "read"})
269 public void readList(String testName) throws Exception {
272 setupReadList(testName);
274 // Submit the request to the service and store the response.
275 ClientResponse<PermissionsList> res = client.readList();
276 PermissionsList list = res.getEntity();
277 int statusCode = res.getStatus();
279 // Check the status code of the response: does it match
280 // the expected response(s)?
281 if (logger.isDebugEnabled()) {
282 logger.debug(testName + ": status = " + statusCode);
284 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
285 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
286 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
288 // Optionally output additional data about list members for debugging.
289 boolean iterateThroughList = true;
290 if (iterateThroughList && logger.isDebugEnabled()) {
291 printList(testName, list);
295 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
296 dependsOnMethods = {"createList", "read"})
297 public void searchResourceName(String testName) throws Exception {
300 setupReadList(testName);
302 // Submit the request to the service and store the response.
303 ClientResponse<PermissionsList> res = client.readSearchList("acquisition");
304 PermissionsList list = res.getEntity();
305 int statusCode = res.getStatus();
306 // Check the status code of the response: does it match
307 // the expected response(s)?
308 if (logger.isDebugEnabled()) {
309 logger.debug(testName + ": status = " + statusCode);
311 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
312 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
313 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
314 int EXPECTED_ITEMS = 1;
315 if (logger.isDebugEnabled()) {
316 logger.debug(testName + ": received = " + list.getPermissions().size()
317 + " expected=" + EXPECTED_ITEMS);
319 Assert.assertEquals(EXPECTED_ITEMS, list.getPermissions().size());
320 // Optionally output additional data about list members for debugging.
321 boolean iterateThroughList = true;
322 if (iterateThroughList && logger.isDebugEnabled()) {
323 printList(testName, list);
329 // ---------------------------------------------------------------
330 // CRUD tests : UPDATE tests
331 // ---------------------------------------------------------------
334 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
335 dependsOnMethods = {"read", "readList", "readNonExistent"})
336 public void update(String testName) throws Exception {
339 setupUpdate(testName);
342 ClientResponse<Permission> res =
343 client.read(knownResourceId);
344 if (logger.isDebugEnabled()) {
345 logger.debug(testName + ": read status = " + res.getStatus());
347 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
349 if (logger.isDebugEnabled()) {
350 logger.debug("got object to update with ID: " + knownResourceId);
352 Permission toUpdatePermission =
353 (Permission) res.getEntity();
354 Assert.assertNotNull(toUpdatePermission);
356 // Update the content of this resource.
357 toUpdatePermission.setResourceName("updated-" + toUpdatePermission.getResourceName());
358 if (logger.isDebugEnabled()) {
359 logger.debug("updated object");
360 logger.debug(objectAsXmlString(toUpdatePermission,
364 // Submit the request to the service and store the response.
365 res = client.update(knownResourceId, toUpdatePermission);
366 int statusCode = res.getStatus();
367 // Check the status code of the response: does it match the expected response(s)?
368 if (logger.isDebugEnabled()) {
369 logger.debug(testName + ": status = " + statusCode);
371 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
372 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
373 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
376 Permission updatedPermission = (Permission) res.getEntity();
377 Assert.assertNotNull(updatedPermission);
379 Assert.assertEquals(updatedPermission.getResourceName(),
380 toUpdatePermission.getResourceName(),
381 "Data in updated object did not match submitted data.");
385 // Placeholders until the three tests below can be uncommented.
386 // See Issue CSPACE-401.
388 public void updateWithEmptyEntityBody(String testName) throws Exception {
392 public void updateWithMalformedXml(String testName) throws Exception {
396 public void updateWithWrongXmlSchema(String testName) throws Exception {
400 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
401 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
402 public void updateNonExistent(String testName) throws Exception {
405 setupUpdateNonExistent(testName);
407 // Submit the request to the service and store the response.
409 // Note: The ID used in this 'create' call may be arbitrary.
410 // The only relevant ID may be the one used in updatePermission(), below.
411 List<PermissionAction> actions = getDefaultActions();
412 Permission permission = createPermissionInstance("acquisitions",
413 "default permissions for acquisitions",
419 ClientResponse<Permission> res =
420 client.update(NON_EXISTENT_ID, permission);
421 int statusCode = res.getStatus();
423 // Check the status code of the response: does it match
424 // the expected response(s)?
425 if (logger.isDebugEnabled()) {
426 logger.debug(testName + ": status = " + statusCode);
428 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
429 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
430 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
433 // ---------------------------------------------------------------
434 // CRUD tests : DELETE tests
435 // ---------------------------------------------------------------
438 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
439 dependsOnMethods = {"update"})
440 public void delete(String testName) throws Exception {
443 setupDelete(testName);
445 // Submit the request to the service and store the response.
446 ClientResponse<Response> res = client.delete(knownResourceId);
447 int statusCode = res.getStatus();
449 // Check the status code of the response: does it match
450 // the expected response(s)?
451 if (logger.isDebugEnabled()) {
452 logger.debug(testName + ": status = " + statusCode);
454 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
455 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
456 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
461 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
462 dependsOnMethods = {"delete"})
463 public void deleteNonExistent(String testName) throws Exception {
466 setupDeleteNonExistent(testName);
468 // Submit the request to the service and store the response.
469 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
470 int statusCode = res.getStatus();
472 // Check the status code of the response: does it match
473 // the expected response(s)?
474 if (logger.isDebugEnabled()) {
475 logger.debug(testName + ": status = " + statusCode);
477 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
478 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
479 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
482 // ---------------------------------------------------------------
483 // Utility tests : tests of code used in tests above
484 // ---------------------------------------------------------------
486 * Tests the code for manually submitting data that is used by several
487 * of the methods above.
489 @Test(dependsOnMethods = {"create", "read"})
490 public void testSubmitRequest() throws Exception {
492 // Expected status code: 200 OK
493 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
495 // Submit the request to the service and store the response.
496 String method = ServiceRequestType.READ.httpMethodName();
497 String url = getResourceURL(knownResourceId);
498 int statusCode = submitRequest(method, url);
500 // Check the status code of the response: does it match
501 // the expected response(s)?
502 if (logger.isDebugEnabled()) {
503 logger.debug("testSubmitRequest: url=" + url
504 + " status=" + statusCode);
506 Assert.assertEquals(statusCode, EXPECTED_STATUS);
510 // ---------------------------------------------------------------
511 // Utility methods used by tests above
512 // ---------------------------------------------------------------
514 * create permission instance
515 * @param resourceName
517 * @param actionList list of actions for this permission
518 * @param effect effect of the permission
519 * @param useResourceName
524 static Permission createPermissionInstance(String resourceName,
526 List<PermissionAction> actionList,
528 boolean useResourceName,
532 Permission permission = new Permission();
533 if (useResourceName) {
534 permission.setResourceName(resourceName);
537 permission.setActions(actionList);
540 permission.setEffect(effect);
544 if (logger.isDebugEnabled()) {
545 logger.debug("to be created, permission common");
546 logger.debug(objectAsXmlString(permission, Permission.class));
551 static List<PermissionAction> getDefaultActions() {
552 List<PermissionAction> actions = new ArrayList<PermissionAction>();
553 PermissionAction create = new PermissionAction();
554 create.setName(ActionType.CREATE);
557 PermissionAction read = new PermissionAction();
558 read.setName(ActionType.READ);
561 PermissionAction update = new PermissionAction();
562 update.setName(ActionType.UPDATE);
565 PermissionAction delete = new PermissionAction();
566 delete.setName(ActionType.DELETE);
569 PermissionAction search = new PermissionAction();
570 search.setName(ActionType.SEARCH);
576 @AfterClass(alwaysRun = true)
577 public void cleanUp() {
578 setupDelete("delete");
579 if (logger.isDebugEnabled()) {
580 logger.debug("Cleaning up temporary resources created for testing ...");
582 for (String resourceId : allResourceIdsCreated) {
583 // Note: Any non-success responses are ignored and not reported.
584 ClientResponse<Response> res = client.delete(resourceId);
585 int statusCode = res.getStatus();
586 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
587 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
588 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
592 private int printList(String testName, PermissionsList list) {
596 for (Permission permission : list.getPermissions()) {
597 logger.debug(testName + " permission csid=" + permission.getCsid()
598 + " name=" + permission.getResourceName()
599 + " desc=" + permission.getDescription());