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;
28 import javax.ws.rs.core.Response;
32 //import org.collectionspace.services.authorization.ActionType;
33 import org.collectionspace.services.authorization.perms.EffectType;
34 import org.collectionspace.services.client.CollectionSpaceClient;
35 import org.collectionspace.services.client.PermissionClient;
36 import org.collectionspace.services.authorization.perms.Permission;
37 import org.collectionspace.services.authorization.perms.PermissionAction;
38 import org.collectionspace.services.authorization.perms.PermissionsList;
39 import org.collectionspace.services.client.PermissionFactory;
40 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
41 import org.collectionspace.services.client.test.ServiceRequestType;
42 import org.testng.Assert;
43 import org.testng.annotations.Test;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
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<PermissionsList, Permission,
55 Permission, Permission> {
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 private String knownResource = "accounts-test";
65 public String getServiceName() {
66 return PermissionClient.SERVICE_NAME;
70 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
73 protected String getServicePathComponent() {
74 return PermissionClient.SERVICE_PATH_COMPONENT;
78 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
81 protected CollectionSpaceClient getClientInstance() throws Exception {
82 return new PermissionClient();
86 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
87 return new PermissionClient(clientPropertiesFilename);
91 * The entity type expected from the JAX-RS Response object
94 public Class<Permission> getEntityResponseType() {
95 return Permission.class;
99 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
101 // @Test(dataProvider = "testName")
103 public void readPaginatedList(String testName) throws Exception {
104 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
108 protected String getKnowResourceIdentifier() {
109 return knownResource;
113 * Sets up create tests with empty entity body.
116 protected void setupCreateWithEmptyEntityBody() {
117 testExpectedStatusCode = STATUS_INTERNAL_SERVER_ERROR; // Empty payload never gets past RESTEasy filter
118 testRequestType = ServiceRequestType.CREATE;
119 testSetup(testExpectedStatusCode, testRequestType);
123 * Sets up create tests with malformed xml.
126 protected void setupCreateWithMalformedXml() {
127 testExpectedStatusCode = STATUS_INTERNAL_SERVER_ERROR; // Malformed payload never gets past RESTEasy filter
128 testRequestType = ServiceRequestType.CREATE;
129 testSetup(testExpectedStatusCode, testRequestType);
133 * Sets up create tests with wrong xml schema.
136 protected void setupCreateWithWrongXmlSchema() {
137 testExpectedStatusCode = STATUS_INTERNAL_SERVER_ERROR; // Empty payload never gets past RESTEasy filter
138 testRequestType = ServiceRequestType.CREATE;
139 testSetup(testExpectedStatusCode, testRequestType);
143 * Creates the without resource name.
145 * @param testName the test name
146 * @throws Exception the exception
148 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
149 dependsOnMethods = {"CRUDTests"})
150 public void createWithoutResourceName(String testName) throws Exception {
153 // Submit the request to the service and store the response.
154 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
155 Permission permission = createPermissionInstance(null,
156 "default permissions for account",
162 PermissionClient client = new PermissionClient();
163 Response res = client.create(permission);
165 int statusCode = res.getStatus();
166 // Does it exactly match the expected status code?
167 if (logger.isDebugEnabled()) {
168 logger.debug(testName + ": status = " + statusCode);
170 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
171 invalidStatusCodeMessage(testRequestType, statusCode));
172 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
179 * Search resource name.
181 * @param testName the test name
182 * @throws Exception the exception
184 @Test(dataProvider = "testName",
185 dependsOnMethods = {"CRUDTests"})
186 public void searchResourceName(String testName) throws Exception {
190 // Submit the request to the service and store the response.
191 PermissionClient client = new PermissionClient();
192 Response res = client.readSearchList("acquisition");
194 assertStatusCode(res, testName);
195 PermissionsList list = res.readEntity(PermissionsList.class);
196 int EXPECTED_ITEMS = 2 + 4; //2 seeded base resource permissions and 4 workflow-related permissions
197 int actual = list.getPermission().size();
198 if (logger.isDebugEnabled()) {
199 logger.debug(testName + ": received = " + actual
200 + " expected=" + EXPECTED_ITEMS);
202 // Optionally output additional data about list members for debugging.
203 boolean iterateThroughList = true;
204 if ((iterateThroughList || (EXPECTED_ITEMS != list.getPermission().size()))
205 && logger.isDebugEnabled()) {
206 printList(testName, list);
208 Assert.assertEquals(list.getPermission().size(), EXPECTED_ITEMS);
217 public void delete(String testName) throws Exception {
218 //This method does nothing because we want to postpone the "delete" test until after
219 //the "updateNotAllowed" test gets run. Our "localDelete" test will call the real "delete" test later.
222 @Test(dataProvider = "testName",
223 dependsOnMethods = {"updateNotAllowed", "updateActions"})
224 public void localDelete(String testName) throws Exception {
225 super.delete(testName);
228 @Test(dataProvider = "testName",
229 dependsOnMethods = {"CRUDTests"})
230 public void updateNotAllowed(String testName) throws Exception {
235 Permission permToUpdate = new Permission();
236 permToUpdate.setCsid(knownResourceId);
237 // Update the content of this resource.
238 permToUpdate.setResourceName("updated-resource");
239 if (logger.isDebugEnabled()) {
240 logger.debug("updated object");
241 logger.debug(objectAsXmlString(permToUpdate,
244 PermissionClient client = new PermissionClient();
245 // Submit the request to the service and store the response.
246 Response res = client.update(knownResourceId, permToUpdate);
247 int statusCode = res.getStatus();
248 // Check the status code of the response: does it match the expected response(s)?
249 if (logger.isDebugEnabled()) {
250 logger.debug(testName + ": status = " + statusCode);
252 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
253 invalidStatusCodeMessage(testRequestType, statusCode));
254 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
261 * @param testName the test name
262 * @throws Exception the exception
264 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
265 dependsOnMethods = {"updateNotAllowed"})
266 public void updateActions(String testName) throws Exception {
270 Permission permToUpdate = new Permission();
271 permToUpdate.setCsid(knownResourceId);
272 permToUpdate.setResourceName(knownResource);
273 // Update the content of this resource.
274 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
275 int default_actions = actions.size();
278 int toUpdate_actions = actions.size();
279 if (logger.isDebugEnabled()) {
280 logger.debug(testName + " no. of actions default=" + default_actions
281 + " to update =" + toUpdate_actions);
283 permToUpdate.setAction(actions);
284 if (logger.isDebugEnabled()) {
285 logger.debug(testName + " updated object\n"
286 + objectAsXmlString(permToUpdate, Permission.class));
288 PermissionClient client = new PermissionClient();
289 // Submit the request to the service and store the response.
290 Response res = client.update(knownResourceId, permToUpdate);
291 int statusCode = res.getStatus();
292 // Check the status code of the response: does it match the expected response(s)?
293 if (logger.isDebugEnabled()) {
294 logger.debug(testName + ": status = " + statusCode);
296 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
297 invalidStatusCodeMessage(testRequestType, statusCode));
298 Assert.assertEquals(statusCode, testExpectedStatusCode);
300 Permission permUpdated = res.readEntity(Permission.class);
301 Assert.assertNotNull(permUpdated);
302 int updated_actions = permToUpdate.getAction().size();
303 if (logger.isDebugEnabled()) {
304 logger.debug(testName + " no. of actions to update=" + toUpdate_actions
305 + " updated =" + updated_actions);
307 Assert.assertEquals(toUpdate_actions,
309 "Data in updated object did not match submitted data.");
313 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
316 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
317 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
318 public void updateNonExistent(String testName) throws Exception {
320 setupUpdateNonExistent();
322 // Submit the request to the service and store the response.
324 // Note: The ID used in this 'create' call may be arbitrary.
325 // The only relevant ID may be the one used in updatePermission(), below.
326 PermissionClient client = new PermissionClient();
327 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
328 Permission permission = createPermissionInstance("test-acquisitions",
329 "default permissions for test-acquisitions",
335 Response res = client.update(NON_EXISTENT_ID, permission);
337 int statusCode = res.getStatus();
339 // Check the status code of the response: does it match
340 // the expected response(s)?
341 if (logger.isDebugEnabled()) {
342 logger.debug(testName + ": status = " + statusCode);
344 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
345 invalidStatusCodeMessage(testRequestType, statusCode));
346 Assert.assertEquals(statusCode, testExpectedStatusCode);
352 // ---------------------------------------------------------------
354 // ---------------------------------------------------------------
357 public void searchWorkflowDeleted(String testName) throws Exception {
358 // Fixme: null test for now, overriding test in base class
361 // ---------------------------------------------------------------
362 // Utility methods used by tests above
363 // ---------------------------------------------------------------
365 * create permission instance
366 * @param resourceName
368 * @param actionList list of actions for this permission
369 * @param effect effect of the permission
370 * @param useResourceName
375 public static Permission createPermissionInstance(String resourceName,
377 List<PermissionAction> actionList,
379 boolean useResourceName,
383 Permission permission = PermissionFactory.createPermissionInstance(resourceName,
384 description, actionList, effect,
385 useResourceName, useAction, useEffect);
387 if (logger.isDebugEnabled()) {
388 logger.debug("to be created, permission");
389 logger.debug(objectAsXmlString(permission, Permission.class));
398 * @param testName the test name
399 * @param list the list
403 protected void printList(String testName, PermissionsList list) {
404 for (Permission permission : list.getPermission()) {
405 logger.debug(testName + " permission csid=" + permission.getCsid()
406 + " name=" + permission.getResourceName()
407 + " desc=" + permission.getDescription());
412 protected Permission createInstance(String commonPartName, String identifier) {
413 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
414 Permission permission = createPermissionInstance(identifier,
415 "default permissions for " + identifier,
425 protected Permission updateInstance(Permission original) {
426 Permission result = new Permission();
428 result.setCsid(original.getCsid());
429 result.setResourceName(original.getResourceName());
430 // Update the content of this resource.
431 result.setDescription("updated-" + original.getDescription());
437 protected void compareUpdatedInstances(Permission original,
438 Permission updated) throws Exception {
439 Assert.assertEquals(updated.getCsid(),
441 "CSID in updated object did not match submitted data.");
443 Assert.assertEquals(updated.getResourceName(),
444 original.getResourceName(),
445 "Resource name in updated object did not match submitted data.");
447 Assert.assertEquals(updated.getDescription(),
448 original.getDescription(),
449 "Description in updated object did not match submitted data.");
453 protected Class<PermissionsList> getCommonListType() {
454 return PermissionsList.class;
458 @Test(dataProvider = "testName",
460 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
461 public void CRUDTests(String testName) {
462 // Do nothing. Simply here to for a TestNG execution order for our tests
466 // public void updateWithEmptyEntityBody(String testName) throws Exception {
467 // // TODO Auto-generated method stub
472 // public void updateWithMalformedXml(String testName) throws Exception {
473 // // TODO Auto-generated method stub
478 // public void updateWithWrongXmlSchema(String testName) throws Exception {
479 // // TODO Auto-generated method stub
484 protected long getSizeOfList(PermissionsList list) {
485 // TODO Auto-generated method stub
486 return list.getPermission().size();