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 String knownResourceId = null;
59 private List<String> allResourceIdsCreated = new ArrayList();
60 boolean addTenant = true;
62 * This method is called only by the parent class, AbstractServiceTestImpl
66 protected String getServicePathComponent() {
67 return new PermissionClient().getServicePathComponent();
70 // ---------------------------------------------------------------
71 // CRUD tests : CREATE tests
72 // ---------------------------------------------------------------
75 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
76 public void create(String testName) throws Exception {
78 // Perform setup, such as initializing the type of service request
79 // (e.g. CREATE, DELETE), its valid and expected status codes, and
80 // its associated HTTP method name (e.g. POST, DELETE).
81 setupCreate(testName);
83 // Submit the request to the service and store the response.
84 List<PermissionAction> actions = getDefaultActions();
85 Permission permission = createPermissionInstance("accounts",
86 "default permissions for account",
92 PermissionClient client = new PermissionClient();
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 PermissionClient client = new PermissionClient();
133 ClientResponse<Response> res = client.create(permission);
134 int statusCode = res.getStatus();
135 // Does it exactly match the expected status code?
136 if (logger.isDebugEnabled()) {
137 logger.debug(testName + ": status = " + statusCode);
139 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
140 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
141 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
144 //to not cause uniqueness violation for permission, createList is removed
146 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
147 dependsOnMethods = {"create"})
148 public void createList(String testName) throws Exception {
150 setupCreate(testName);
151 // Submit the request to the service and store the response.
152 List<PermissionAction> actions = getDefaultActions();
153 Permission permission1 = createPermissionInstance("collectionobjects",
154 "default permissions for collectionobjects",
160 PermissionClient client = new PermissionClient();
161 ClientResponse<Response> res = client.create(permission1);
162 int statusCode = res.getStatus();
163 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
164 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
165 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
166 allResourceIdsCreated.add(extractId(res));
168 Permission permission2 = createPermissionInstance("acquisitions",
169 "default permissions for acquisitions",
175 res = client.create(permission2);
176 statusCode = res.getStatus();
177 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
178 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
179 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
180 allResourceIdsCreated.add(extractId(res));
182 Permission permission3 = createPermissionInstance("ids",
183 "default permissions for id service",
189 res = client.create(permission3);
190 statusCode = res.getStatus();
191 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
192 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
193 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
194 allResourceIdsCreated.add(extractId(res));
198 // Placeholders until the three tests below can be uncommented.
199 // See Issue CSPACE-401.
201 public void createWithEmptyEntityBody(String testName) throws Exception {
205 public void createWithMalformedXml(String testName) throws Exception {
209 public void createWithWrongXmlSchema(String testName) throws Exception {
212 // ---------------------------------------------------------------
213 // CRUD tests : READ tests
214 // ---------------------------------------------------------------
217 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
218 dependsOnMethods = {"create"})
219 public void read(String testName) throws Exception {
224 // Submit the request to the service and store the response.
225 PermissionClient client = new PermissionClient();
226 ClientResponse<Permission> res = client.read(knownResourceId);
227 int statusCode = res.getStatus();
229 // Check the status code of the response: does it match
230 // the expected response(s)?
231 if (logger.isDebugEnabled()) {
232 logger.debug(testName + ": status = " + statusCode);
234 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
235 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
236 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
238 Permission output = (Permission) res.getEntity();
239 Assert.assertNotNull(output);
244 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
245 dependsOnMethods = {"read"})
246 public void readNonExistent(String testName) throws Exception {
249 setupReadNonExistent(testName);
251 // Submit the request to the service and store the response.
252 PermissionClient client = new PermissionClient();
253 ClientResponse<Permission> res = client.read(NON_EXISTENT_ID);
254 int statusCode = res.getStatus();
256 // Check the status code of the response: does it match
257 // the expected response(s)?
258 if (logger.isDebugEnabled()) {
259 logger.debug(testName + ": status = " + statusCode);
261 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
262 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
263 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
266 // ---------------------------------------------------------------
267 // CRUD tests : READ_LIST tests
268 // ---------------------------------------------------------------
271 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
272 dependsOnMethods = {"createList", "read"})
273 public void readList(String testName) throws Exception {
276 setupReadList(testName);
278 // Submit the request to the service and store the response.
279 PermissionClient client = new PermissionClient();
280 ClientResponse<PermissionsList> res = client.readList();
281 PermissionsList list = res.getEntity();
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);
293 // Optionally output additional data about list members for debugging.
294 boolean iterateThroughList = true;
295 if (iterateThroughList && logger.isDebugEnabled()) {
296 printList(testName, list);
300 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
301 dependsOnMethods = {"createList", "read"})
302 public void searchResourceName(String testName) throws Exception {
305 setupReadList(testName);
307 // Submit the request to the service and store the response.
308 PermissionClient client = new PermissionClient();
309 ClientResponse<PermissionsList> res = client.readSearchList("acquisition");
310 PermissionsList list = res.getEntity();
311 int statusCode = res.getStatus();
312 // Check the status code of the response: does it match
313 // the expected response(s)?
314 if (logger.isDebugEnabled()) {
315 logger.debug(testName + ": status = " + statusCode);
317 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
318 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
319 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
320 int EXPECTED_ITEMS = 1;
321 if (logger.isDebugEnabled()) {
322 logger.debug(testName + ": received = " + list.getPermissions().size()
323 + " expected=" + EXPECTED_ITEMS);
325 Assert.assertEquals(EXPECTED_ITEMS, list.getPermissions().size());
326 // Optionally output additional data about list members for debugging.
327 boolean iterateThroughList = true;
328 if (iterateThroughList && logger.isDebugEnabled()) {
329 printList(testName, list);
335 // ---------------------------------------------------------------
336 // CRUD tests : UPDATE tests
337 // ---------------------------------------------------------------
340 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
341 dependsOnMethods = {"read", "readList", "readNonExistent"})
342 public void update(String testName) throws Exception {
345 setupUpdate(testName);
347 // Retrieve the contents of a resource to update.
348 PermissionClient client = new PermissionClient();
349 ClientResponse<Permission> res =
350 client.read(knownResourceId);
351 if (logger.isDebugEnabled()) {
352 logger.debug(testName + ": read status = " + res.getStatus());
354 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
356 if (logger.isDebugEnabled()) {
357 logger.debug("got object to update with ID: " + knownResourceId);
359 Permission toUpdatePermission =
360 (Permission) res.getEntity();
361 Assert.assertNotNull(toUpdatePermission);
363 // Update the content of this resource.
364 toUpdatePermission.setResourceName("updated-" + toUpdatePermission.getResourceName());
365 if (logger.isDebugEnabled()) {
366 logger.debug("updated object");
367 logger.debug(objectAsXmlString(toUpdatePermission,
371 // Submit the request to the service and store the response.
372 res = client.update(knownResourceId, toUpdatePermission);
373 int statusCode = res.getStatus();
374 // Check the status code of the response: does it match the expected response(s)?
375 if (logger.isDebugEnabled()) {
376 logger.debug(testName + ": status = " + statusCode);
378 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
379 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
380 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
383 Permission updatedPermission = (Permission) res.getEntity();
384 Assert.assertNotNull(updatedPermission);
386 Assert.assertEquals(updatedPermission.getResourceName(),
387 toUpdatePermission.getResourceName(),
388 "Data in updated object did not match submitted data.");
392 // Placeholders until the three tests below can be uncommented.
393 // See Issue CSPACE-401.
395 public void updateWithEmptyEntityBody(String testName) throws Exception {
399 public void updateWithMalformedXml(String testName) throws Exception {
403 public void updateWithWrongXmlSchema(String testName) throws Exception {
407 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
408 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
409 public void updateNonExistent(String testName) throws Exception {
412 setupUpdateNonExistent(testName);
414 // Submit the request to the service and store the response.
416 // Note: The ID used in this 'create' call may be arbitrary.
417 // The only relevant ID may be the one used in updatePermission(), below.
418 PermissionClient client = new PermissionClient();
419 List<PermissionAction> actions = getDefaultActions();
420 Permission permission = createPermissionInstance("acquisitions",
421 "default permissions for acquisitions",
427 ClientResponse<Permission> res =
428 client.update(NON_EXISTENT_ID, permission);
429 int statusCode = res.getStatus();
431 // Check the status code of the response: does it match
432 // the expected response(s)?
433 if (logger.isDebugEnabled()) {
434 logger.debug(testName + ": status = " + statusCode);
436 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
437 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
438 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
441 // ---------------------------------------------------------------
442 // CRUD tests : DELETE tests
443 // ---------------------------------------------------------------
446 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
447 dependsOnMethods = {"update"})
448 public void delete(String testName) throws Exception {
451 setupDelete(testName);
453 // Submit the request to the service and store the response.
454 PermissionClient client = new PermissionClient();
455 ClientResponse<Response> res = client.delete(knownResourceId);
456 int statusCode = res.getStatus();
458 // Check the status code of the response: does it match
459 // the expected response(s)?
460 if (logger.isDebugEnabled()) {
461 logger.debug(testName + ": status = " + statusCode);
463 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
464 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
465 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
470 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
471 dependsOnMethods = {"delete"})
472 public void deleteNonExistent(String testName) throws Exception {
475 setupDeleteNonExistent(testName);
477 // Submit the request to the service and store the response.
478 PermissionClient client = new PermissionClient();
479 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
480 int statusCode = res.getStatus();
482 // Check the status code of the response: does it match
483 // the expected response(s)?
484 if (logger.isDebugEnabled()) {
485 logger.debug(testName + ": status = " + statusCode);
487 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
488 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
489 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
492 // ---------------------------------------------------------------
493 // Utility tests : tests of code used in tests above
494 // ---------------------------------------------------------------
496 * Tests the code for manually submitting data that is used by several
497 * of the methods above.
499 @Test(dependsOnMethods = {"create", "read"})
500 public void testSubmitRequest() throws Exception {
502 // Expected status code: 200 OK
503 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
505 // Submit the request to the service and store the response.
506 String method = ServiceRequestType.READ.httpMethodName();
507 String url = getResourceURL(knownResourceId);
508 int statusCode = submitRequest(method, url);
510 // Check the status code of the response: does it match
511 // the expected response(s)?
512 if (logger.isDebugEnabled()) {
513 logger.debug("testSubmitRequest: url=" + url
514 + " status=" + statusCode);
516 Assert.assertEquals(statusCode, EXPECTED_STATUS);
520 // ---------------------------------------------------------------
521 // Utility methods used by tests above
522 // ---------------------------------------------------------------
524 * create permission instance
525 * @param resourceName
527 * @param actionList list of actions for this permission
528 * @param effect effect of the permission
529 * @param useResourceName
534 static Permission createPermissionInstance(String resourceName,
536 List<PermissionAction> actionList,
538 boolean useResourceName,
542 Permission permission = new Permission();
543 if (useResourceName) {
544 permission.setResourceName(resourceName);
547 permission.setActions(actionList);
550 permission.setEffect(effect);
554 if (logger.isDebugEnabled()) {
555 logger.debug("to be created, permission common");
556 logger.debug(objectAsXmlString(permission, Permission.class));
561 static List<PermissionAction> getDefaultActions() {
562 List<PermissionAction> actions = new ArrayList<PermissionAction>();
563 PermissionAction create = new PermissionAction();
564 create.setName(ActionType.CREATE);
567 PermissionAction read = new PermissionAction();
568 read.setName(ActionType.READ);
571 PermissionAction update = new PermissionAction();
572 update.setName(ActionType.UPDATE);
575 PermissionAction delete = new PermissionAction();
576 delete.setName(ActionType.DELETE);
579 PermissionAction search = new PermissionAction();
580 search.setName(ActionType.SEARCH);
586 @AfterClass(alwaysRun = true)
587 public void cleanUp() {
588 setupDelete("delete");
589 if (logger.isDebugEnabled()) {
590 logger.debug("Cleaning up temporary resources created for testing ...");
592 PermissionClient client = new PermissionClient();
593 for (String resourceId : allResourceIdsCreated) {
594 // Note: Any non-success responses are ignored and not reported.
595 ClientResponse<Response> res = client.delete(resourceId);
596 int statusCode = res.getStatus();
597 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
598 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
599 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
603 private int printList(String testName, PermissionsList list) {
607 for (Permission permission : list.getPermissions()) {
608 logger.debug(testName + " permission csid=" + permission.getCsid()
609 + " name=" + permission.getResourceName()
610 + " desc=" + permission.getDescription());