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.CollectionSpaceClient;
32 import org.collectionspace.services.client.PermissionClient;
33 import org.collectionspace.services.authorization.Permission;
34 import org.collectionspace.services.authorization.PermissionAction;
35 import org.collectionspace.services.authorization.PermissionsList;
36 import org.collectionspace.services.client.PermissionFactory;
37 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
38 import org.collectionspace.services.client.test.ServiceRequestType;
39 import org.collectionspace.services.jaxb.AbstractCommonList;
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;
50 * PermissionServiceTest, carries out tests against a
51 * deployed and running Permission Service.
53 * $LastChangedRevision: 917 $
54 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
56 public class PermissionServiceTest extends AbstractServiceTestImpl {
58 static private final Logger logger =
59 LoggerFactory.getLogger(PermissionServiceTest.class);
60 // Instance variables specific to this test.
61 private String knownResourceId = null;
62 private List<String> allResourceIdsCreated = new ArrayList<String>();
63 boolean addTenant = true;
65 * This method is called only by the parent class, AbstractServiceTestImpl
69 protected String getServicePathComponent() {
70 return new PermissionClient().getServicePathComponent();
74 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
77 protected CollectionSpaceClient getClientInstance() {
78 return new PermissionClient();
82 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
85 protected AbstractCommonList getAbstractCommonList(
86 ClientResponse<AbstractCommonList> response) {
87 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
88 throw new UnsupportedOperationException();
91 @Test(dataProvider = "testName")
93 public void readPaginatedList(String testName) throws Exception {
94 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
97 // ---------------------------------------------------------------
98 // CRUD tests : CREATE tests
99 // ---------------------------------------------------------------
102 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
103 public void create(String testName) throws Exception {
105 // Perform setup, such as initializing the type of service request
106 // (e.g. CREATE, DELETE), its valid and expected status codes, and
107 // its associated HTTP method name (e.g. POST, DELETE).
108 setupCreate(testName);
110 // Submit the request to the service and store the response.
111 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
112 Permission permission = createPermissionInstance("accounts",
113 "default permissions for account",
119 PermissionClient client = new PermissionClient();
120 ClientResponse<Response> res = client.create(permission);
121 int statusCode = res.getStatus();
123 // Check the status code of the response: does it match
124 // the expected response(s)?
127 // Does it fall within the set of valid status codes?
128 // Does it exactly match the expected status code?
129 if (logger.isDebugEnabled()) {
130 logger.debug(testName + ": status = " + statusCode);
132 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
133 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
134 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
136 // Store the ID returned from this create operation
137 // for additional tests below.
138 knownResourceId = extractId(res);
139 if (logger.isDebugEnabled()) {
140 logger.debug(testName + ": knownResourceId=" + knownResourceId);
144 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
145 dependsOnMethods = {"create"})
146 public void createWithoutResourceName(String testName) throws Exception {
148 setupCreate(testName);
150 // Submit the request to the service and store the response.
151 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
152 Permission permission = createPermissionInstance(null,
153 "default permissions for account",
159 PermissionClient client = new PermissionClient();
160 ClientResponse<Response> res = client.create(permission);
161 int statusCode = res.getStatus();
162 // Does it exactly match the expected status code?
163 if (logger.isDebugEnabled()) {
164 logger.debug(testName + ": status = " + statusCode);
166 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
167 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
168 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
171 //to not cause uniqueness violation for permission, createList is removed
173 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
174 dependsOnMethods = {"create"})
175 public void createList(String testName) throws Exception {
177 setupCreate(testName);
178 // Submit the request to the service and store the response.
179 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
180 Permission permission1 = createPermissionInstance("collectionobjects",
181 "default permissions for collectionobjects",
187 PermissionClient client = new PermissionClient();
188 ClientResponse<Response> res = client.create(permission1);
189 int statusCode = res.getStatus();
190 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
191 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
192 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
193 allResourceIdsCreated.add(extractId(res));
195 Permission permission2 = createPermissionInstance("acquisitions",
196 "default permissions for acquisitions",
202 res = client.create(permission2);
203 statusCode = res.getStatus();
204 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
205 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
206 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
207 allResourceIdsCreated.add(extractId(res));
209 Permission permission3 = createPermissionInstance("ids",
210 "default permissions for id service",
216 res = client.create(permission3);
217 statusCode = res.getStatus();
218 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
220 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
221 allResourceIdsCreated.add(extractId(res));
225 // Placeholders until the three tests below can be uncommented.
226 // See Issue CSPACE-401.
228 public void createWithEmptyEntityBody(String testName) throws Exception {
229 //FIXME: Should this test really be empty? If so, please comment accordingly.
233 public void createWithMalformedXml(String testName) throws Exception {
234 //FIXME: Should this test really be empty? If so, please comment accordingly.
238 public void createWithWrongXmlSchema(String testName) throws Exception {
239 //FIXME: Should this test really be empty? If so, please comment accordingly.
242 // ---------------------------------------------------------------
243 // CRUD tests : READ tests
244 // ---------------------------------------------------------------
247 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
248 dependsOnMethods = {"create"})
249 public void read(String testName) throws Exception {
254 // Submit the request to the service and store the response.
255 PermissionClient client = new PermissionClient();
256 ClientResponse<Permission> res = client.read(knownResourceId);
257 int statusCode = res.getStatus();
259 // Check the status code of the response: does it match
260 // the expected response(s)?
261 if (logger.isDebugEnabled()) {
262 logger.debug(testName + ": status = " + statusCode);
264 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
265 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
266 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
268 Permission output = (Permission) res.getEntity();
269 Assert.assertNotNull(output);
274 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
275 dependsOnMethods = {"read"})
276 public void readNonExistent(String testName) throws Exception {
279 setupReadNonExistent(testName);
281 // Submit the request to the service and store the response.
282 PermissionClient client = new PermissionClient();
283 ClientResponse<Permission> res = client.read(NON_EXISTENT_ID);
284 int statusCode = res.getStatus();
286 // Check the status code of the response: does it match
287 // the expected response(s)?
288 if (logger.isDebugEnabled()) {
289 logger.debug(testName + ": status = " + statusCode);
291 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
292 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
293 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
296 // ---------------------------------------------------------------
297 // CRUD tests : READ_LIST tests
298 // ---------------------------------------------------------------
301 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
302 dependsOnMethods = {"createList", "read"})
303 public void readList(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.readList();
311 PermissionsList list = res.getEntity();
312 int statusCode = res.getStatus();
314 // Check the status code of the response: does it match
315 // the expected response(s)?
316 if (logger.isDebugEnabled()) {
317 logger.debug(testName + ": status = " + statusCode);
319 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
320 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
321 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
323 // Optionally output additional data about list members for debugging.
324 boolean iterateThroughList = true;
325 if (iterateThroughList && logger.isDebugEnabled()) {
326 printList(testName, list);
330 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
331 dependsOnMethods = {"createList", "read"})
332 public void searchResourceName(String testName) throws Exception {
335 setupReadList(testName);
337 // Submit the request to the service and store the response.
338 PermissionClient client = new PermissionClient();
339 ClientResponse<PermissionsList> res = client.readSearchList("acquisition");
340 PermissionsList list = res.getEntity();
341 int statusCode = res.getStatus();
342 // Check the status code of the response: does it match
343 // the expected response(s)?
344 if (logger.isDebugEnabled()) {
345 logger.debug(testName + ": status = " + statusCode);
347 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
348 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
349 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
350 int EXPECTED_ITEMS = 1;
351 if (logger.isDebugEnabled()) {
352 logger.debug(testName + ": received = " + list.getPermissions().size()
353 + " expected=" + EXPECTED_ITEMS);
355 Assert.assertEquals(EXPECTED_ITEMS, list.getPermissions().size());
356 // Optionally output additional data about list members for debugging.
357 boolean iterateThroughList = true;
358 if (iterateThroughList && logger.isDebugEnabled()) {
359 printList(testName, list);
365 // ---------------------------------------------------------------
366 // CRUD tests : UPDATE tests
367 // ---------------------------------------------------------------
370 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
371 dependsOnMethods = {"read", "readList", "readNonExistent"})
372 public void update(String testName) throws Exception {
375 setupUpdate(testName);
377 Permission permToUpdate = new Permission();
378 permToUpdate.setCsid(knownResourceId);
379 // Update the content of this resource.
380 permToUpdate.setResourceName("updated-resource");
381 if (logger.isDebugEnabled()) {
382 logger.debug("updated object");
383 logger.debug(objectAsXmlString(permToUpdate,
386 PermissionClient client = new PermissionClient();
387 // Submit the request to the service and store the response.
388 ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
389 int statusCode = res.getStatus();
390 // Check the status code of the response: does it match the expected response(s)?
391 if (logger.isDebugEnabled()) {
392 logger.debug(testName + ": status = " + statusCode);
394 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
395 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
396 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
399 Permission permUpdated = (Permission) res.getEntity();
400 Assert.assertNotNull(permUpdated);
402 Assert.assertEquals(permUpdated.getResourceName(),
403 permToUpdate.getResourceName(),
404 "Data in updated object did not match submitted data.");
407 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
408 dependsOnMethods = {"update"})
409 public void updateActions(String testName) throws Exception {
412 setupUpdate(testName);
414 Permission permToUpdate = new Permission();
415 permToUpdate.setCsid(knownResourceId);
416 // Update the content of this resource.
417 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
418 int default_actions = actions.size();
421 int toUpdate_actions = actions.size();
422 if (logger.isDebugEnabled()) {
423 logger.debug(testName + " no. of actions default=" + default_actions
424 + " to update =" + toUpdate_actions);
426 permToUpdate.setActions(actions);
427 if (logger.isDebugEnabled()) {
428 logger.debug(testName + " updated object\n"
429 + objectAsXmlString(permToUpdate, Permission.class));
431 PermissionClient client = new PermissionClient();
432 // Submit the request to the service and store the response.
433 ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
434 int statusCode = res.getStatus();
435 // Check the status code of the response: does it match the expected response(s)?
436 if (logger.isDebugEnabled()) {
437 logger.debug(testName + ": status = " + statusCode);
439 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
440 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
441 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
443 Permission permUpdated = (Permission) res.getEntity();
444 Assert.assertNotNull(permUpdated);
445 int updated_actions = permToUpdate.getActions().size();
446 if (logger.isDebugEnabled()) {
447 logger.debug(testName + " no. of actions to update=" + toUpdate_actions
448 + " updated =" + updated_actions);
450 Assert.assertEquals(toUpdate_actions,
452 "Data in updated object did not match submitted data.");
455 // Placeholders until the three tests below can be uncommented.
456 // See Issue CSPACE-401.
459 public void updateWithEmptyEntityBody(String testName) throws Exception {
460 //FIXME: Should this test really be empty? If so, please comment accordingly.
464 public void updateWithMalformedXml(String testName) throws Exception {
465 //FIXME: Should this test really be empty? If so, please comment accordingly.
469 public void updateWithWrongXmlSchema(String testName) throws Exception {
470 //FIXME: Should this test really be empty? If so, please comment accordingly.
474 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
475 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
476 public void updateNonExistent(String testName) throws Exception {
479 setupUpdateNonExistent(testName);
481 // Submit the request to the service and store the response.
483 // Note: The ID used in this 'create' call may be arbitrary.
484 // The only relevant ID may be the one used in updatePermission(), below.
485 PermissionClient client = new PermissionClient();
486 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
487 Permission permission = createPermissionInstance("acquisitions",
488 "default permissions for acquisitions",
494 ClientResponse<Permission> res =
495 client.update(NON_EXISTENT_ID, permission);
496 int statusCode = res.getStatus();
498 // Check the status code of the response: does it match
499 // the expected response(s)?
500 if (logger.isDebugEnabled()) {
501 logger.debug(testName + ": status = " + statusCode);
503 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
504 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
505 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
508 // ---------------------------------------------------------------
509 // CRUD tests : DELETE tests
510 // ---------------------------------------------------------------
513 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
514 dependsOnMethods = {"updateActions"})
515 public void delete(String testName) throws Exception {
518 setupDelete(testName);
520 // Submit the request to the service and store the response.
521 PermissionClient client = new PermissionClient();
522 ClientResponse<Response> res = client.delete(knownResourceId);
523 int statusCode = res.getStatus();
525 // Check the status code of the response: does it match
526 // the expected response(s)?
527 if (logger.isDebugEnabled()) {
528 logger.debug(testName + ": status = " + statusCode);
530 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
531 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
532 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
537 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
538 dependsOnMethods = {"delete"})
539 public void deleteNonExistent(String testName) throws Exception {
542 setupDeleteNonExistent(testName);
544 // Submit the request to the service and store the response.
545 PermissionClient client = new PermissionClient();
546 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
547 int statusCode = res.getStatus();
549 // Check the status code of the response: does it match
550 // the expected response(s)?
551 if (logger.isDebugEnabled()) {
552 logger.debug(testName + ": status = " + statusCode);
554 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
555 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
556 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
559 // ---------------------------------------------------------------
560 // Utility tests : tests of code used in tests above
561 // ---------------------------------------------------------------
563 * Tests the code for manually submitting data that is used by several
564 * of the methods above.
566 @Test(dependsOnMethods = {"create", "read"})
567 public void testSubmitRequest() throws Exception {
569 // Expected status code: 200 OK
570 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
572 // Submit the request to the service and store the response.
573 String method = ServiceRequestType.READ.httpMethodName();
574 String url = getResourceURL(knownResourceId);
575 int statusCode = submitRequest(method, url);
577 // Check the status code of the response: does it match
578 // the expected response(s)?
579 if (logger.isDebugEnabled()) {
580 logger.debug("testSubmitRequest: url=" + url
581 + " status=" + statusCode);
583 Assert.assertEquals(statusCode, EXPECTED_STATUS);
587 // ---------------------------------------------------------------
588 // Utility methods used by tests above
589 // ---------------------------------------------------------------
591 * create permission instance
592 * @param resourceName
594 * @param actionList list of actions for this permission
595 * @param effect effect of the permission
596 * @param useResourceName
601 public static Permission createPermissionInstance(String resourceName,
603 List<PermissionAction> actionList,
605 boolean useResourceName,
609 Permission permission = PermissionFactory.createPermissionInstance(resourceName,
610 description, actionList, effect,
611 useResourceName, useAction, useEffect);
613 if (logger.isDebugEnabled()) {
614 logger.debug("to be created, permission");
615 logger.debug(objectAsXmlString(permission, Permission.class));
620 @AfterClass(alwaysRun = true)
621 public void cleanUp() {
622 setupDelete("delete");
623 String noTest = System.getProperty("noTestCleanup");
624 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
625 if (logger.isDebugEnabled()) {
626 logger.debug("Skipping Cleanup phase ...");
630 if (logger.isDebugEnabled()) {
631 logger.debug("Cleaning up temporary resources created for testing ...");
633 PermissionClient client = new PermissionClient();
634 for (String resourceId : allResourceIdsCreated) {
635 ClientResponse<Response> res = client.delete(resourceId);
636 int statusCode = res.getStatus();
637 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
638 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
639 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
643 private int printList(String testName, PermissionsList list) {
647 for (Permission permission : list.getPermissions()) {
648 logger.debug(testName + " permission csid=" + permission.getCsid()
649 + " name=" + permission.getResourceName()
650 + " desc=" + permission.getDescription());