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.testng.Assert;
42 import org.testng.annotations.Test;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
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<PermissionsList, Permission,
54 Permission, Permission> {
56 /** The Constant logger. */
57 private final static String CLASS_NAME = PermissionServiceTest.class.getName();
58 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
60 // Instance variables specific to this test.
61 private String knownResource = "accounts-test";
64 public String getServiceName() {
65 return PermissionClient.SERVICE_NAME;
69 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
72 protected String getServicePathComponent() {
73 return PermissionClient.SERVICE_PATH_COMPONENT;
77 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
80 protected CollectionSpaceClient getClientInstance() throws Exception {
81 return new PermissionClient();
85 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
86 return new PermissionClient(clientPropertiesFilename);
90 * The entity type expected from the JAX-RS Response object
92 public Class<Permission> getEntityResponseType() {
93 return Permission.class;
97 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
99 // @Test(dataProvider = "testName")
101 public void readPaginatedList(String testName) throws Exception {
102 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
106 protected String getKnowResourceIdentifier() {
107 return knownResource;
111 * Creates the without resource name.
113 * @param testName the test name
114 * @throws Exception the exception
116 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
117 dependsOnMethods = {"CRUDTests"})
118 public void createWithoutResourceName(String testName) throws Exception {
121 // Submit the request to the service and store the response.
122 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
123 Permission permission = createPermissionInstance(null,
124 "default permissions for account",
130 PermissionClient client = new PermissionClient();
131 Response res = client.create(permission);
133 int statusCode = res.getStatus();
134 // Does it exactly match the expected status code?
135 if (logger.isDebugEnabled()) {
136 logger.debug(testName + ": status = " + statusCode);
138 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
139 invalidStatusCodeMessage(testRequestType, statusCode));
140 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
147 * Search resource name.
149 * @param testName the test name
150 * @throws Exception the exception
152 @Test(dataProvider = "testName",
153 dependsOnMethods = {"CRUDTests"})
154 public void searchResourceName(String testName) throws Exception {
158 // Submit the request to the service and store the response.
159 PermissionClient client = new PermissionClient();
160 Response res = client.readSearchList("acquisition");
162 assertStatusCode(res, testName);
163 PermissionsList list = res.readEntity(PermissionsList.class);
164 int EXPECTED_ITEMS = 2 + 4; //2 seeded base resource permissions and 4 workflow-related permissions
165 int actual = list.getPermission().size();
166 if (logger.isDebugEnabled()) {
167 logger.debug(testName + ": received = " + actual
168 + " expected=" + EXPECTED_ITEMS);
170 // Optionally output additional data about list members for debugging.
171 boolean iterateThroughList = true;
172 if ((iterateThroughList || (EXPECTED_ITEMS != list.getPermission().size()))
173 && logger.isDebugEnabled()) {
174 printList(testName, list);
176 Assert.assertEquals(list.getPermission().size(), EXPECTED_ITEMS);
185 public void delete(String testName) throws Exception {
186 //This method does nothing because we want to postpone the "delete" test until after
187 //the "updateNotAllowed" test gets run. Our "localDelete" test will call the real "delete" test later.
190 @Test(dataProvider = "testName",
191 dependsOnMethods = {"updateNotAllowed", "updateActions"})
192 public void localDelete(String testName) throws Exception {
193 super.delete(testName);
196 @Test(dataProvider = "testName",
197 dependsOnMethods = {"CRUDTests"})
198 public void updateNotAllowed(String testName) throws Exception {
203 Permission permToUpdate = new Permission();
204 permToUpdate.setCsid(knownResourceId);
205 // Update the content of this resource.
206 permToUpdate.setResourceName("updated-resource");
207 if (logger.isDebugEnabled()) {
208 logger.debug("updated object");
209 logger.debug(objectAsXmlString(permToUpdate,
212 PermissionClient client = new PermissionClient();
213 // Submit the request to the service and store the response.
214 Response res = client.update(knownResourceId, permToUpdate);
215 int statusCode = res.getStatus();
216 // Check the status code of the response: does it match the expected response(s)?
217 if (logger.isDebugEnabled()) {
218 logger.debug(testName + ": status = " + statusCode);
220 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
221 invalidStatusCodeMessage(testRequestType, statusCode));
222 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
229 * @param testName the test name
230 * @throws Exception the exception
232 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
233 dependsOnMethods = {"updateNotAllowed"})
234 public void updateActions(String testName) throws Exception {
238 Permission permToUpdate = new Permission();
239 permToUpdate.setCsid(knownResourceId);
240 permToUpdate.setResourceName(knownResource);
241 // Update the content of this resource.
242 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
243 int default_actions = actions.size();
246 int toUpdate_actions = actions.size();
247 if (logger.isDebugEnabled()) {
248 logger.debug(testName + " no. of actions default=" + default_actions
249 + " to update =" + toUpdate_actions);
251 permToUpdate.setAction(actions);
252 if (logger.isDebugEnabled()) {
253 logger.debug(testName + " updated object\n"
254 + objectAsXmlString(permToUpdate, Permission.class));
256 PermissionClient client = new PermissionClient();
257 // Submit the request to the service and store the response.
258 Response res = client.update(knownResourceId, permToUpdate);
259 int statusCode = res.getStatus();
260 // Check the status code of the response: does it match the expected response(s)?
261 if (logger.isDebugEnabled()) {
262 logger.debug(testName + ": status = " + statusCode);
264 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
265 invalidStatusCodeMessage(testRequestType, statusCode));
266 Assert.assertEquals(statusCode, testExpectedStatusCode);
268 Permission permUpdated = res.readEntity(Permission.class);
269 Assert.assertNotNull(permUpdated);
270 int updated_actions = permToUpdate.getAction().size();
271 if (logger.isDebugEnabled()) {
272 logger.debug(testName + " no. of actions to update=" + toUpdate_actions
273 + " updated =" + updated_actions);
275 Assert.assertEquals(toUpdate_actions,
277 "Data in updated object did not match submitted data.");
281 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
284 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
285 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
286 public void updateNonExistent(String testName) throws Exception {
288 setupUpdateNonExistent();
290 // Submit the request to the service and store the response.
292 // Note: The ID used in this 'create' call may be arbitrary.
293 // The only relevant ID may be the one used in updatePermission(), below.
294 PermissionClient client = new PermissionClient();
295 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
296 Permission permission = createPermissionInstance("test-acquisitions",
297 "default permissions for test-acquisitions",
303 Response res = client.update(NON_EXISTENT_ID, permission);
305 int statusCode = res.getStatus();
307 // Check the status code of the response: does it match
308 // the expected response(s)?
309 if (logger.isDebugEnabled()) {
310 logger.debug(testName + ": status = " + statusCode);
312 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
313 invalidStatusCodeMessage(testRequestType, statusCode));
314 Assert.assertEquals(statusCode, testExpectedStatusCode);
320 // ---------------------------------------------------------------
322 // ---------------------------------------------------------------
325 public void searchWorkflowDeleted(String testName) throws Exception {
326 // Fixme: null test for now, overriding test in base class
329 // ---------------------------------------------------------------
330 // Utility methods used by tests above
331 // ---------------------------------------------------------------
333 * create permission instance
334 * @param resourceName
336 * @param actionList list of actions for this permission
337 * @param effect effect of the permission
338 * @param useResourceName
343 public static Permission createPermissionInstance(String resourceName,
345 List<PermissionAction> actionList,
347 boolean useResourceName,
351 Permission permission = PermissionFactory.createPermissionInstance(resourceName,
352 description, actionList, effect,
353 useResourceName, useAction, useEffect);
355 if (logger.isDebugEnabled()) {
356 logger.debug("to be created, permission");
357 logger.debug(objectAsXmlString(permission, Permission.class));
366 * @param testName the test name
367 * @param list the list
371 protected void printList(String testName, PermissionsList list) {
372 for (Permission permission : list.getPermission()) {
373 logger.debug(testName + " permission csid=" + permission.getCsid()
374 + " name=" + permission.getResourceName()
375 + " desc=" + permission.getDescription());
380 protected Permission createInstance(String commonPartName, String identifier) {
381 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
382 Permission permission = createPermissionInstance(identifier,
383 "default permissions for " + identifier,
393 protected Permission updateInstance(Permission original) {
394 Permission result = new Permission();
396 result.setCsid(original.getCsid());
397 result.setResourceName(original.getResourceName());
398 // Update the content of this resource.
399 result.setDescription("updated-" + original.getDescription());
405 protected void compareUpdatedInstances(Permission original,
406 Permission updated) throws Exception {
407 Assert.assertEquals(updated.getCsid(),
409 "CSID in updated object did not match submitted data.");
411 Assert.assertEquals(updated.getResourceName(),
412 original.getResourceName(),
413 "Resource name in updated object did not match submitted data.");
415 Assert.assertEquals(updated.getDescription(),
416 original.getDescription(),
417 "Description in updated object did not match submitted data.");
421 protected Class<PermissionsList> getCommonListType() {
422 return PermissionsList.class;
426 @Test(dataProvider = "testName",
428 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
429 public void CRUDTests(String testName) {
430 // Do nothing. Simply here to for a TestNG execution order for our tests
434 public void updateWithEmptyEntityBody(String testName) throws Exception {
435 // TODO Auto-generated method stub
440 public void updateWithMalformedXml(String testName) throws Exception {
441 // TODO Auto-generated method stub
446 public void updateWithWrongXmlSchema(String testName) throws Exception {
447 // TODO Auto-generated method stub
452 protected long getSizeOfList(PermissionsList list) {
453 // TODO Auto-generated method stub
454 return list.getPermission().size();