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 Permission permToUpdate = new Permission();
349 permToUpdate.setCsid(knownResourceId);
350 // Update the content of this resource.
351 permToUpdate.setResourceName("updated-resource");
352 if (logger.isDebugEnabled()) {
353 logger.debug("updated object");
354 logger.debug(objectAsXmlString(permToUpdate,
357 PermissionClient client = new PermissionClient();
358 // Submit the request to the service and store the response.
359 ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
360 int statusCode = res.getStatus();
361 // Check the status code of the response: does it match the expected response(s)?
362 if (logger.isDebugEnabled()) {
363 logger.debug(testName + ": status = " + statusCode);
365 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
366 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
367 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
370 Permission permUpdated = (Permission) res.getEntity();
371 Assert.assertNotNull(permUpdated);
373 Assert.assertEquals(permUpdated.getResourceName(),
374 permToUpdate.getResourceName(),
375 "Data in updated object did not match submitted data.");
379 // Placeholders until the three tests below can be uncommented.
380 // See Issue CSPACE-401.
382 public void updateWithEmptyEntityBody(String testName) throws Exception {
386 public void updateWithMalformedXml(String testName) throws Exception {
390 public void updateWithWrongXmlSchema(String testName) throws Exception {
394 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
395 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
396 public void updateNonExistent(String testName) throws Exception {
399 setupUpdateNonExistent(testName);
401 // Submit the request to the service and store the response.
403 // Note: The ID used in this 'create' call may be arbitrary.
404 // The only relevant ID may be the one used in updatePermission(), below.
405 PermissionClient client = new PermissionClient();
406 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
407 Permission permission = createPermissionInstance("acquisitions",
408 "default permissions for acquisitions",
414 ClientResponse<Permission> res =
415 client.update(NON_EXISTENT_ID, permission);
416 int statusCode = res.getStatus();
418 // Check the status code of the response: does it match
419 // the expected response(s)?
420 if (logger.isDebugEnabled()) {
421 logger.debug(testName + ": status = " + statusCode);
423 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
424 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
425 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
428 // ---------------------------------------------------------------
429 // CRUD tests : DELETE tests
430 // ---------------------------------------------------------------
433 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
434 dependsOnMethods = {"update"})
435 public void delete(String testName) throws Exception {
438 setupDelete(testName);
440 // Submit the request to the service and store the response.
441 PermissionClient client = new PermissionClient();
442 ClientResponse<Response> res = client.delete(knownResourceId);
443 int statusCode = res.getStatus();
445 // Check the status code of the response: does it match
446 // the expected response(s)?
447 if (logger.isDebugEnabled()) {
448 logger.debug(testName + ": status = " + statusCode);
450 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
451 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
452 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
457 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
458 dependsOnMethods = {"delete"})
459 public void deleteNonExistent(String testName) throws Exception {
462 setupDeleteNonExistent(testName);
464 // Submit the request to the service and store the response.
465 PermissionClient client = new PermissionClient();
466 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
467 int statusCode = res.getStatus();
469 // Check the status code of the response: does it match
470 // the expected response(s)?
471 if (logger.isDebugEnabled()) {
472 logger.debug(testName + ": status = " + statusCode);
474 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
475 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
476 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
479 // ---------------------------------------------------------------
480 // Utility tests : tests of code used in tests above
481 // ---------------------------------------------------------------
483 * Tests the code for manually submitting data that is used by several
484 * of the methods above.
486 @Test(dependsOnMethods = {"create", "read"})
487 public void testSubmitRequest() throws Exception {
489 // Expected status code: 200 OK
490 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
492 // Submit the request to the service and store the response.
493 String method = ServiceRequestType.READ.httpMethodName();
494 String url = getResourceURL(knownResourceId);
495 int statusCode = submitRequest(method, url);
497 // Check the status code of the response: does it match
498 // the expected response(s)?
499 if (logger.isDebugEnabled()) {
500 logger.debug("testSubmitRequest: url=" + url
501 + " status=" + statusCode);
503 Assert.assertEquals(statusCode, EXPECTED_STATUS);
507 // ---------------------------------------------------------------
508 // Utility methods used by tests above
509 // ---------------------------------------------------------------
511 * create permission instance
512 * @param resourceName
514 * @param actionList list of actions for this permission
515 * @param effect effect of the permission
516 * @param useResourceName
521 public static Permission createPermissionInstance(String resourceName,
523 List<PermissionAction> actionList,
525 boolean useResourceName,
529 Permission permission = PermissionFactory.createPermissionInstance(resourceName,
530 description, actionList, effect,
531 useResourceName, useAction, useEffect);
533 if (logger.isDebugEnabled()) {
534 logger.debug("to be created, permission common");
535 logger.debug(objectAsXmlString(permission, Permission.class));
541 @AfterClass(alwaysRun = true)
542 public void cleanUp() {
543 setupDelete("delete");
544 String noTest = System.getProperty("noTestCleanup");
545 if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
546 if (logger.isDebugEnabled()) {
547 logger.debug("Skipping Cleanup phase ...");
551 if (logger.isDebugEnabled()) {
552 logger.debug("Cleaning up temporary resources created for testing ...");
554 PermissionClient client = new PermissionClient();
555 for (String resourceId : allResourceIdsCreated) {
556 ClientResponse<Response> res = client.delete(resourceId);
557 int statusCode = res.getStatus();
558 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
559 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
560 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
564 private int printList(String testName, PermissionsList list) {
568 for (Permission permission : list.getPermissions()) {
569 logger.debug(testName + " permission csid=" + permission.getCsid()
570 + " name=" + permission.getResourceName()
571 + " desc=" + permission.getDescription());