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.perms.EffectType;
31 import org.collectionspace.services.client.CollectionSpaceClient;
32 import org.collectionspace.services.client.PermissionClient;
33 import org.collectionspace.services.authorization.perms.Permission;
34 import org.collectionspace.services.authorization.perms.PermissionAction;
35 import org.collectionspace.services.authorization.perms.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;
49 * PermissionServiceTest, carries out tests against a
50 * deployed and running Permission Service.
52 * $LastChangedRevision: 917 $
53 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
55 public class PermissionServiceTest extends AbstractServiceTestImpl {
57 /** The Constant logger. */
58 private final static String CLASS_NAME = PermissionServiceTest.class.getName();
59 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
61 // Instance variables specific to this test.
62 /** The known resource id. */
63 private String knownResourceId = null;
64 private String knownResource = "accounts-test";
67 public String getServiceName() {
68 return PermissionClient.SERVICE_NAME;
72 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
75 protected String getServicePathComponent() {
76 return PermissionClient.SERVICE_PATH_COMPONENT;
80 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
83 protected CollectionSpaceClient getClientInstance() {
84 return new PermissionClient();
88 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
91 protected AbstractCommonList getAbstractCommonList(
92 ClientResponse<AbstractCommonList> response) {
93 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
94 throw new UnsupportedOperationException();
98 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
100 @Test(dataProvider = "testName")
102 public void readPaginatedList(String testName) throws Exception {
103 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
106 // ---------------------------------------------------------------
107 // CRUD tests : CREATE tests
108 // ---------------------------------------------------------------
111 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
114 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
115 public void create(String testName) throws Exception {
117 if (logger.isDebugEnabled()) {
118 logger.debug(testBanner(testName, CLASS_NAME));
120 // Perform setup, such as initializing the type of service request
121 // (e.g. CREATE, DELETE), its valid and expected status codes, and
122 // its associated HTTP method name (e.g. POST, DELETE).
125 // Submit the request to the service and store the response.
126 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
127 Permission permission = createPermissionInstance(knownResource,
128 "default permissions for account",
134 PermissionClient client = new PermissionClient();
135 ClientResponse<Response> res = client.create(permission);
136 int statusCode = res.getStatus();
138 // Check the status code of the response: does it match
139 // the expected response(s)?
142 // Does it fall within the set of valid status codes?
143 // Does it exactly match the expected status code?
144 if (logger.isDebugEnabled()) {
145 logger.debug(testName + ": status = " + statusCode);
147 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
148 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
149 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
151 // Store the ID returned from this create operation
152 // for additional tests below.
153 knownResourceId = extractId(res);
154 if (logger.isDebugEnabled()) {
155 logger.debug(testName + ": knownResourceId=" + knownResourceId);
160 * Creates the without resource name.
162 * @param testName the test name
163 * @throws Exception the exception
165 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
166 dependsOnMethods = {"create"})
167 public void createWithoutResourceName(String testName) throws Exception {
169 if (logger.isDebugEnabled()) {
170 logger.debug(testBanner(testName, CLASS_NAME));
174 // Submit the request to the service and store the response.
175 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
176 Permission permission = createPermissionInstance(null,
177 "default permissions for account",
183 PermissionClient client = new PermissionClient();
184 ClientResponse<Response> res = client.create(permission);
185 int statusCode = res.getStatus();
186 // Does it exactly match the expected status code?
187 if (logger.isDebugEnabled()) {
188 logger.debug(testName + ": status = " + statusCode);
190 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
191 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
192 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
195 //to not cause uniqueness violation for permission, createList is removed
197 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
200 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
201 dependsOnMethods = {"create"})
202 public void createList(String testName) throws Exception {
204 if (logger.isDebugEnabled()) {
205 logger.debug(testBanner(testName, CLASS_NAME));
208 // Submit the request to the service and store the response.
209 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
210 Permission permission1 = createPermissionInstance("test-objects",
211 "default permissions for test-objects",
217 PermissionClient client = new PermissionClient();
218 ClientResponse<Response> res = client.create(permission1);
219 int statusCode = res.getStatus();
220 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
221 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
222 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
223 allResourceIdsCreated.add(extractId(res));
225 Permission permission2 = createPermissionInstance("test-acquisitions",
226 "default permissions for test-acquisitions",
232 res = client.create(permission2);
233 statusCode = res.getStatus();
234 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
235 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
236 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
237 allResourceIdsCreated.add(extractId(res));
239 Permission permission3 = createPermissionInstance("test-ids",
240 "default permissions for id service",
246 res = client.create(permission3);
247 statusCode = res.getStatus();
248 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
249 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
250 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
251 allResourceIdsCreated.add(extractId(res));
255 // Placeholders until the three tests below can be uncommented.
256 // See Issue CSPACE-401.
258 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
261 public void createWithEmptyEntityBody(String testName) throws Exception {
262 //FIXME: Should this test really be empty? If so, please comment accordingly.
266 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
269 public void createWithMalformedXml(String testName) throws Exception {
270 //FIXME: Should this test really be empty? If so, please comment accordingly.
274 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
277 public void createWithWrongXmlSchema(String testName) throws Exception {
278 //FIXME: Should this test really be empty? If so, please comment accordingly.
281 // ---------------------------------------------------------------
282 // CRUD tests : READ tests
283 // ---------------------------------------------------------------
286 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
289 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
290 dependsOnMethods = {"create"})
291 public void read(String testName) throws Exception {
293 if (logger.isDebugEnabled()) {
294 logger.debug(testBanner(testName, CLASS_NAME));
299 // Submit the request to the service and store the response.
300 PermissionClient client = new PermissionClient();
301 ClientResponse<Permission> res = client.read(knownResourceId);
302 assertStatusCode(res, testName);
303 Permission output = (Permission) res.getEntity();
304 Assert.assertNotNull(output);
309 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
312 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
313 dependsOnMethods = {"read"})
314 public void readNonExistent(String testName) throws Exception {
316 if (logger.isDebugEnabled()) {
317 logger.debug(testBanner(testName, CLASS_NAME));
320 setupReadNonExistent();
322 // Submit the request to the service and store the response.
323 PermissionClient client = new PermissionClient();
324 ClientResponse<Permission> res = client.read(NON_EXISTENT_ID);
325 int statusCode = res.getStatus();
327 // Check the status code of the response: does it match
328 // the expected response(s)?
329 if (logger.isDebugEnabled()) {
330 logger.debug(testName + ": status = " + statusCode);
332 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
333 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
334 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
337 // ---------------------------------------------------------------
338 // CRUD tests : READ_LIST tests
339 // ---------------------------------------------------------------
342 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
345 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
346 dependsOnMethods = {"createList", "read"})
347 public void readList(String testName) throws Exception {
349 if (logger.isDebugEnabled()) {
350 logger.debug(testBanner(testName, CLASS_NAME));
355 // Submit the request to the service and store the response.
356 PermissionClient client = new PermissionClient();
357 ClientResponse<PermissionsList> res = client.readList();
358 assertStatusCode(res, testName);
359 PermissionsList list = res.getEntity(PermissionsList.class);
361 // Optionally output additional data about list members for debugging.
362 boolean iterateThroughList = true;
363 if (iterateThroughList && logger.isDebugEnabled()) {
364 printList(testName, list);
369 * Search resource name.
371 * @param testName the test name
372 * @throws Exception the exception
374 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
375 dependsOnMethods = {"createList", "read"})
376 public void searchResourceName(String testName) throws Exception {
378 if (logger.isDebugEnabled()) {
379 logger.debug(testBanner(testName, CLASS_NAME));
384 // Submit the request to the service and store the response.
385 PermissionClient client = new PermissionClient();
386 ClientResponse<PermissionsList> res = client.readSearchList("acquisition");
387 assertStatusCode(res, testName);
388 PermissionsList list = res.getEntity(PermissionsList.class);
389 int EXPECTED_ITEMS = 5; //seeded permissions
390 int actual = list.getPermission().size();
391 if (logger.isDebugEnabled()) {
392 logger.debug(testName + ": received = " + actual
393 + " expected=" + EXPECTED_ITEMS);
395 // Optionally output additional data about list members for debugging.
396 boolean iterateThroughList = true;
397 if ((iterateThroughList || (EXPECTED_ITEMS != list.getPermission().size()))
398 && logger.isDebugEnabled()) {
399 printList(testName, list);
401 Assert.assertEquals(list.getPermission().size(), EXPECTED_ITEMS);
407 // ---------------------------------------------------------------
408 // CRUD tests : UPDATE tests
409 // ---------------------------------------------------------------
412 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
415 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
416 dependsOnMethods = {"read", "readList", "readNonExistent"})
417 public void update(String testName) throws Exception {
419 if (logger.isDebugEnabled()) {
420 logger.debug(testBanner(testName, CLASS_NAME));
425 Permission permToUpdate = new Permission();
426 permToUpdate.setCsid(knownResourceId);
427 permToUpdate.setResourceName(knownResource);
428 // Update the content of this resource.
429 permToUpdate.setDescription("updated description");
430 if (logger.isDebugEnabled()) {
431 logger.debug("updated object");
432 logger.debug(objectAsXmlString(permToUpdate,
435 PermissionClient client = new PermissionClient();
436 // Submit the request to the service and store the response.
437 ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
438 assertStatusCode(res, testName);
439 Permission permUpdated = (Permission) res.getEntity();
440 Assert.assertNotNull(permUpdated);
442 Assert.assertEquals(permUpdated.getDescription(),
443 permToUpdate.getDescription(),
444 "Data in updated object did not match submitted data.");
447 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
448 dependsOnMethods = {"read", "readList", "readNonExistent"})
449 public void updateNotAllowed(String testName) throws Exception {
454 Permission permToUpdate = new Permission();
455 permToUpdate.setCsid(knownResourceId);
456 // Update the content of this resource.
457 permToUpdate.setResourceName("updated-resource");
458 if (logger.isDebugEnabled()) {
459 logger.debug("updated object");
460 logger.debug(objectAsXmlString(permToUpdate,
463 PermissionClient client = new PermissionClient();
464 // Submit the request to the service and store the response.
465 ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
466 int statusCode = res.getStatus();
467 // Check the status code of the response: does it match the expected response(s)?
468 if (logger.isDebugEnabled()) {
469 logger.debug(testName + ": status = " + statusCode);
471 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
472 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
473 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
480 * @param testName the test name
481 * @throws Exception the exception
483 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
484 dependsOnMethods = {"updateNotAllowed"})
485 public void updateActions(String testName) throws Exception {
487 if (logger.isDebugEnabled()) {
488 logger.debug(testBanner(testName, CLASS_NAME));
493 Permission permToUpdate = new Permission();
494 permToUpdate.setCsid(knownResourceId);
495 permToUpdate.setResourceName(knownResource);
496 // Update the content of this resource.
497 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
498 int default_actions = actions.size();
501 int toUpdate_actions = actions.size();
502 if (logger.isDebugEnabled()) {
503 logger.debug(testName + " no. of actions default=" + default_actions
504 + " to update =" + toUpdate_actions);
506 permToUpdate.setAction(actions);
507 if (logger.isDebugEnabled()) {
508 logger.debug(testName + " updated object\n"
509 + objectAsXmlString(permToUpdate, Permission.class));
511 PermissionClient client = new PermissionClient();
512 // Submit the request to the service and store the response.
513 ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
514 assertStatusCode(res, testName);
515 Permission permUpdated = (Permission) res.getEntity();
516 Assert.assertNotNull(permUpdated);
517 int updated_actions = permToUpdate.getAction().size();
518 if (logger.isDebugEnabled()) {
519 logger.debug(testName + " no. of actions to update=" + toUpdate_actions
520 + " updated =" + updated_actions);
522 Assert.assertEquals(toUpdate_actions,
524 "Data in updated object did not match submitted data.");
527 // Placeholders until the three tests below can be uncommented.
528 // See Issue CSPACE-401.
531 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
534 public void updateWithEmptyEntityBody(String testName) throws Exception {
535 //FIXME: Should this test really be empty? If so, please comment accordingly.
539 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
542 public void updateWithMalformedXml(String testName) throws Exception {
543 //FIXME: Should this test really be empty? If so, please comment accordingly.
547 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
550 public void updateWithWrongXmlSchema(String testName) throws Exception {
551 //FIXME: Should this test really be empty? If so, please comment accordingly.
555 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
558 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
559 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
560 public void updateNonExistent(String testName) throws Exception {
562 if (logger.isDebugEnabled()) {
563 logger.debug(testBanner(testName, CLASS_NAME));
566 setupUpdateNonExistent();
568 // Submit the request to the service and store the response.
570 // Note: The ID used in this 'create' call may be arbitrary.
571 // The only relevant ID may be the one used in updatePermission(), below.
572 PermissionClient client = new PermissionClient();
573 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
574 Permission permission = createPermissionInstance("test-acquisitions",
575 "default permissions for test-acquisitions",
581 ClientResponse<Permission> res =
582 client.update(NON_EXISTENT_ID, permission);
583 int statusCode = res.getStatus();
585 // Check the status code of the response: does it match
586 // the expected response(s)?
587 if (logger.isDebugEnabled()) {
588 logger.debug(testName + ": status = " + statusCode);
590 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
591 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
592 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
595 // ---------------------------------------------------------------
596 // CRUD tests : DELETE tests
597 // ---------------------------------------------------------------
600 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
603 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
604 dependsOnMethods = {"update", "updateActions", "testSubmitRequest"})
605 public void delete(String testName) throws Exception {
607 if (logger.isDebugEnabled()) {
608 logger.debug(testBanner(testName, CLASS_NAME));
613 // Submit the request to the service and store the response.
614 PermissionClient client = new PermissionClient();
615 ClientResponse<Response> res = client.delete(knownResourceId);
616 int statusCode = res.getStatus();
618 // Check the status code of the response: does it match
619 // the expected response(s)?
620 if (logger.isDebugEnabled()) {
621 logger.debug(testName + ": status = " + statusCode);
623 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
624 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
625 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
630 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
633 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
634 dependsOnMethods = {"delete"})
635 public void deleteNonExistent(String testName) throws Exception {
637 if (logger.isDebugEnabled()) {
638 logger.debug(testBanner(testName, CLASS_NAME));
641 setupDeleteNonExistent();
643 // Submit the request to the service and store the response.
644 PermissionClient client = new PermissionClient();
645 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
646 int statusCode = res.getStatus();
648 // Check the status code of the response: does it match
649 // the expected response(s)?
650 if (logger.isDebugEnabled()) {
651 logger.debug(testName + ": status = " + statusCode);
653 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
654 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
655 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
658 // ---------------------------------------------------------------
660 // ---------------------------------------------------------------
663 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
664 public void searchWorkflowDeleted(String testName) throws Exception {
665 // Fixme: null test for now, overriding test in base class
668 // ---------------------------------------------------------------
669 // Utility tests : tests of code used in tests above
670 // ---------------------------------------------------------------
672 * Tests the code for manually submitting data that is used by several
673 * of the methods above.
676 @Test(dependsOnMethods = {"create"})
677 public void testSubmitRequest() throws Exception {
679 // Expected status code: 200 OK
680 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
682 // Submit the request to the service and store the response.
683 String method = ServiceRequestType.READ.httpMethodName();
684 String url = getResourceURL(knownResourceId);
685 int statusCode = submitRequest(method, url);
687 // Check the status code of the response: does it match
688 // the expected response(s)?
689 if (logger.isDebugEnabled()) {
690 logger.debug("testSubmitRequest: url=" + url
691 + " status=" + statusCode);
693 Assert.assertEquals(statusCode, EXPECTED_STATUS);
697 // ---------------------------------------------------------------
698 // Utility methods used by tests above
699 // ---------------------------------------------------------------
701 * create permission instance
702 * @param resourceName
704 * @param actionList list of actions for this permission
705 * @param effect effect of the permission
706 * @param useResourceName
711 public static Permission createPermissionInstance(String resourceName,
713 List<PermissionAction> actionList,
715 boolean useResourceName,
719 Permission permission = PermissionFactory.createPermissionInstance(resourceName,
720 description, actionList, effect,
721 useResourceName, useAction, useEffect);
723 if (logger.isDebugEnabled()) {
724 logger.debug("to be created, permission");
725 logger.debug(objectAsXmlString(permission, Permission.class));
733 * @param testName the test name
734 * @param list the list
737 private int printList(String testName, PermissionsList list) {
741 for (Permission permission : list.getPermission()) {
742 logger.debug(testName + " permission csid=" + permission.getCsid()
743 + " name=" + permission.getResourceName()
744 + " desc=" + permission.getDescription());