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.PermissionFactory;
36 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
37 import org.collectionspace.services.client.test.ServiceRequestType;
38 import org.jboss.resteasy.client.ClientResponse;
40 import org.testng.Assert;
41 import org.testng.annotations.Test;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.testng.annotations.AfterClass;
48 * PermissionServiceTest, carries out tests against a
49 * deployed and running Permission Service.
51 * $LastChangedRevision: 917 $
52 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
54 public class PermissionServiceTest extends AbstractServiceTestImpl {
56 static private final Logger logger =
57 LoggerFactory.getLogger(PermissionServiceTest.class);
58 // Instance variables specific to this test.
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 new PermissionClient().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 = PermissionFactory.createDefaultActions();
86 Permission permission = createPermissionInstance("accounts",
87 "default permissions for account",
93 PermissionClient client = new PermissionClient();
94 ClientResponse<Response> res = client.create(permission);
95 int statusCode = res.getStatus();
97 // Check the status code of the response: does it match
98 // the expected response(s)?
101 // Does it fall within the set of valid status codes?
102 // Does it exactly match the expected status code?
103 if (logger.isDebugEnabled()) {
104 logger.debug(testName + ": status = " + statusCode);
106 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
107 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
108 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
110 // Store the ID returned from this create operation
111 // for additional tests below.
112 knownResourceId = extractId(res);
113 if (logger.isDebugEnabled()) {
114 logger.debug(testName + ": knownResourceId=" + knownResourceId);
118 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
119 dependsOnMethods = {"create"})
120 public void createWithoutResourceName(String testName) throws Exception {
122 setupCreate(testName);
124 // Submit the request to the service and store the response.
125 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
126 Permission permission = createPermissionInstance(null,
127 "default permissions for account",
133 PermissionClient client = new PermissionClient();
134 ClientResponse<Response> res = client.create(permission);
135 int statusCode = res.getStatus();
136 // Does it exactly match the expected status code?
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, Response.Status.BAD_REQUEST.getStatusCode());
145 //to not cause uniqueness violation for permission, createList is removed
147 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
148 dependsOnMethods = {"create"})
149 public void createList(String testName) throws Exception {
151 setupCreate(testName);
152 // Submit the request to the service and store the response.
153 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
154 Permission permission1 = createPermissionInstance("collectionobjects",
155 "default permissions for collectionobjects",
161 PermissionClient client = new PermissionClient();
162 ClientResponse<Response> res = client.create(permission1);
163 int statusCode = res.getStatus();
164 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
165 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
166 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
167 allResourceIdsCreated.add(extractId(res));
169 Permission permission2 = createPermissionInstance("acquisitions",
170 "default permissions for acquisitions",
176 res = client.create(permission2);
177 statusCode = res.getStatus();
178 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
181 allResourceIdsCreated.add(extractId(res));
183 Permission permission3 = createPermissionInstance("ids",
184 "default permissions for id service",
190 res = client.create(permission3);
191 statusCode = res.getStatus();
192 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
193 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
194 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
195 allResourceIdsCreated.add(extractId(res));
199 // Placeholders until the three tests below can be uncommented.
200 // See Issue CSPACE-401.
202 public void createWithEmptyEntityBody(String testName) throws Exception {
206 public void createWithMalformedXml(String testName) throws Exception {
210 public void createWithWrongXmlSchema(String testName) throws Exception {
213 // ---------------------------------------------------------------
214 // CRUD tests : READ tests
215 // ---------------------------------------------------------------
218 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
219 dependsOnMethods = {"create"})
220 public void read(String testName) throws Exception {
225 // Submit the request to the service and store the response.
226 PermissionClient client = new PermissionClient();
227 ClientResponse<Permission> res = client.read(knownResourceId);
228 int statusCode = res.getStatus();
230 // Check the status code of the response: does it match
231 // the expected response(s)?
232 if (logger.isDebugEnabled()) {
233 logger.debug(testName + ": status = " + statusCode);
235 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
236 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
237 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
239 Permission output = (Permission) res.getEntity();
240 Assert.assertNotNull(output);
245 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
246 dependsOnMethods = {"read"})
247 public void readNonExistent(String testName) throws Exception {
250 setupReadNonExistent(testName);
252 // Submit the request to the service and store the response.
253 PermissionClient client = new PermissionClient();
254 ClientResponse<Permission> res = client.read(NON_EXISTENT_ID);
255 int statusCode = res.getStatus();
257 // Check the status code of the response: does it match
258 // the expected response(s)?
259 if (logger.isDebugEnabled()) {
260 logger.debug(testName + ": status = " + statusCode);
262 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
263 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
264 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
267 // ---------------------------------------------------------------
268 // CRUD tests : READ_LIST tests
269 // ---------------------------------------------------------------
272 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
273 dependsOnMethods = {"createList", "read"})
274 public void readList(String testName) throws Exception {
277 setupReadList(testName);
279 // Submit the request to the service and store the response.
280 PermissionClient client = new PermissionClient();
281 ClientResponse<PermissionsList> res = client.readList();
282 PermissionsList list = res.getEntity();
283 int statusCode = res.getStatus();
285 // Check the status code of the response: does it match
286 // the expected response(s)?
287 if (logger.isDebugEnabled()) {
288 logger.debug(testName + ": status = " + statusCode);
290 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
291 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
292 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
294 // Optionally output additional data about list members for debugging.
295 boolean iterateThroughList = true;
296 if (iterateThroughList && logger.isDebugEnabled()) {
297 printList(testName, list);
301 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
302 dependsOnMethods = {"createList", "read"})
303 public void searchResourceName(String testName) throws Exception {
306 setupReadList(testName);
308 // Submit the request to the service and store the response.
309 PermissionClient client = new PermissionClient();
310 ClientResponse<PermissionsList> res = client.readSearchList("acquisition");
311 PermissionsList list = res.getEntity();
312 int statusCode = res.getStatus();
313 // Check the status code of the response: does it match
314 // the expected response(s)?
315 if (logger.isDebugEnabled()) {
316 logger.debug(testName + ": status = " + statusCode);
318 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
319 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
320 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
321 int EXPECTED_ITEMS = 1;
322 if (logger.isDebugEnabled()) {
323 logger.debug(testName + ": received = " + list.getPermissions().size()
324 + " expected=" + EXPECTED_ITEMS);
326 Assert.assertEquals(EXPECTED_ITEMS, list.getPermissions().size());
327 // Optionally output additional data about list members for debugging.
328 boolean iterateThroughList = true;
329 if (iterateThroughList && logger.isDebugEnabled()) {
330 printList(testName, list);
336 // ---------------------------------------------------------------
337 // CRUD tests : UPDATE tests
338 // ---------------------------------------------------------------
341 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
342 dependsOnMethods = {"read", "readList", "readNonExistent"})
343 public void update(String testName) throws Exception {
346 setupUpdate(testName);
348 // Retrieve the contents of a resource to update.
349 PermissionClient client = new PermissionClient();
350 ClientResponse<Permission> res =
351 client.read(knownResourceId);
352 if (logger.isDebugEnabled()) {
353 logger.debug(testName + ": read status = " + res.getStatus());
355 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
357 if (logger.isDebugEnabled()) {
358 logger.debug("got object to update with ID: " + knownResourceId);
360 Permission toUpdatePermission =
361 (Permission) res.getEntity();
362 Assert.assertNotNull(toUpdatePermission);
364 // Update the content of this resource.
365 toUpdatePermission.setResourceName("updated-" + toUpdatePermission.getResourceName());
366 if (logger.isDebugEnabled()) {
367 logger.debug("updated object");
368 logger.debug(objectAsXmlString(toUpdatePermission,
372 // Submit the request to the service and store the response.
373 res = client.update(knownResourceId, toUpdatePermission);
374 int statusCode = res.getStatus();
375 // Check the status code of the response: does it match the expected response(s)?
376 if (logger.isDebugEnabled()) {
377 logger.debug(testName + ": status = " + statusCode);
379 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
380 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
381 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
384 Permission updatedPermission = (Permission) res.getEntity();
385 Assert.assertNotNull(updatedPermission);
387 Assert.assertEquals(updatedPermission.getResourceName(),
388 toUpdatePermission.getResourceName(),
389 "Data in updated object did not match submitted data.");
393 // Placeholders until the three tests below can be uncommented.
394 // See Issue CSPACE-401.
396 public void updateWithEmptyEntityBody(String testName) throws Exception {
400 public void updateWithMalformedXml(String testName) throws Exception {
404 public void updateWithWrongXmlSchema(String testName) throws Exception {
408 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
409 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
410 public void updateNonExistent(String testName) throws Exception {
413 setupUpdateNonExistent(testName);
415 // Submit the request to the service and store the response.
417 // Note: The ID used in this 'create' call may be arbitrary.
418 // The only relevant ID may be the one used in updatePermission(), below.
419 PermissionClient client = new PermissionClient();
420 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
421 Permission permission = createPermissionInstance("acquisitions",
422 "default permissions for acquisitions",
428 ClientResponse<Permission> res =
429 client.update(NON_EXISTENT_ID, permission);
430 int statusCode = res.getStatus();
432 // Check the status code of the response: does it match
433 // the expected response(s)?
434 if (logger.isDebugEnabled()) {
435 logger.debug(testName + ": status = " + statusCode);
437 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
438 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
439 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
442 // ---------------------------------------------------------------
443 // CRUD tests : DELETE tests
444 // ---------------------------------------------------------------
447 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
448 dependsOnMethods = {"update"})
449 public void delete(String testName) throws Exception {
452 setupDelete(testName);
454 // Submit the request to the service and store the response.
455 PermissionClient client = new PermissionClient();
456 ClientResponse<Response> res = client.delete(knownResourceId);
457 int statusCode = res.getStatus();
459 // Check the status code of the response: does it match
460 // the expected response(s)?
461 if (logger.isDebugEnabled()) {
462 logger.debug(testName + ": status = " + statusCode);
464 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
465 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
466 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
471 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
472 dependsOnMethods = {"delete"})
473 public void deleteNonExistent(String testName) throws Exception {
476 setupDeleteNonExistent(testName);
478 // Submit the request to the service and store the response.
479 PermissionClient client = new PermissionClient();
480 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
481 int statusCode = res.getStatus();
483 // Check the status code of the response: does it match
484 // the expected response(s)?
485 if (logger.isDebugEnabled()) {
486 logger.debug(testName + ": status = " + statusCode);
488 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
489 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
490 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
493 // ---------------------------------------------------------------
494 // Utility tests : tests of code used in tests above
495 // ---------------------------------------------------------------
497 * Tests the code for manually submitting data that is used by several
498 * of the methods above.
500 @Test(dependsOnMethods = {"create", "read"})
501 public void testSubmitRequest() throws Exception {
503 // Expected status code: 200 OK
504 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
506 // Submit the request to the service and store the response.
507 String method = ServiceRequestType.READ.httpMethodName();
508 String url = getResourceURL(knownResourceId);
509 int statusCode = submitRequest(method, url);
511 // Check the status code of the response: does it match
512 // the expected response(s)?
513 if (logger.isDebugEnabled()) {
514 logger.debug("testSubmitRequest: url=" + url
515 + " status=" + statusCode);
517 Assert.assertEquals(statusCode, EXPECTED_STATUS);
521 // ---------------------------------------------------------------
522 // Utility methods used by tests above
523 // ---------------------------------------------------------------
525 * create permission instance
526 * @param resourceName
528 * @param actionList list of actions for this permission
529 * @param effect effect of the permission
530 * @param useResourceName
535 public static Permission createPermissionInstance(String resourceName,
537 List<PermissionAction> actionList,
539 boolean useResourceName,
543 Permission permission = PermissionFactory.createPermissionInstance(resourceName,
544 description, actionList, effect,
545 useResourceName, useAction, useEffect);
547 if (logger.isDebugEnabled()) {
548 logger.debug("to be created, permission common");
549 logger.debug(objectAsXmlString(permission, Permission.class));
555 @AfterClass(alwaysRun = true)
556 public void cleanUp() {
557 setupDelete("delete");
558 String noTest = System.getProperty("noTestCleanup");
559 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
560 if (logger.isDebugEnabled()) {
561 logger.debug("Skipping Cleanup phase ...");
565 if (logger.isDebugEnabled()) {
566 logger.debug("Cleaning up temporary resources created for testing ...");
568 PermissionClient client = new PermissionClient();
569 for (String resourceId : allResourceIdsCreated) {
570 ClientResponse<Response> res = client.delete(resourceId);
571 int statusCode = res.getStatus();
572 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
573 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
574 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
578 private int printList(String testName, PermissionsList list) {
582 for (Permission permission : list.getPermissions()) {
583 logger.debug(testName + " permission csid=" + permission.getCsid()
584 + " name=" + permission.getResourceName()
585 + " desc=" + permission.getDescription());