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;
30 //import org.collectionspace.services.authorization.ActionType;
31 import org.collectionspace.services.authorization.perms.EffectType;
32 import org.collectionspace.services.client.CollectionSpaceClient;
33 import org.collectionspace.services.client.PermissionClient;
34 import org.collectionspace.services.authorization.perms.Permission;
35 import org.collectionspace.services.authorization.perms.PermissionAction;
36 import org.collectionspace.services.authorization.perms.PermissionsList;
37 import org.collectionspace.services.client.PermissionFactory;
38 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
40 import org.testng.Assert;
41 import org.testng.annotations.Test;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * PermissionServiceTest, carries out tests against a
47 * deployed and running Permission Service.
49 * $LastChangedRevision: 917 $
50 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
52 public class PermissionServiceTest extends AbstractServiceTestImpl<PermissionsList, Permission,
53 Permission, Permission> {
55 /** The Constant logger. */
56 private final static String CLASS_NAME = PermissionServiceTest.class.getName();
57 private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
59 // Instance variables specific to this test.
60 private String knownResource = "accounts-test";
63 public String getServiceName() {
64 return PermissionClient.SERVICE_NAME;
68 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
71 protected String getServicePathComponent() {
72 return PermissionClient.SERVICE_PATH_COMPONENT;
76 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
79 protected CollectionSpaceClient getClientInstance() {
80 return new PermissionClient();
84 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
85 return new PermissionClient(clientPropertiesFilename);
89 * The entity type expected from the JAX-RS Response object
91 public Class<Permission> getEntityResponseType() {
92 return Permission.class;
96 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
98 // @Test(dataProvider = "testName")
100 public void readPaginatedList(String testName) throws Exception {
101 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
105 protected String getKnowResourceIdentifier() {
106 return knownResource;
110 * Creates the without resource name.
112 * @param testName the test name
113 * @throws Exception the exception
115 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
116 dependsOnMethods = {"CRUDTests"})
117 public void createWithoutResourceName(String testName) throws Exception {
120 // Submit the request to the service and store the response.
121 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
122 Permission permission = createPermissionInstance(null,
123 "default permissions for account",
129 PermissionClient client = new PermissionClient();
130 Response res = client.create(permission);
132 int statusCode = res.getStatus();
133 // Does it exactly match the expected status code?
134 if (logger.isDebugEnabled()) {
135 logger.debug(testName + ": status = " + statusCode);
137 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
138 invalidStatusCodeMessage(testRequestType, statusCode));
139 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
146 * Search resource name.
148 * @param testName the test name
149 * @throws Exception the exception
151 @Test(dataProvider = "testName",
152 dependsOnMethods = {"CRUDTests"})
153 public void searchResourceName(String testName) throws Exception {
157 // Submit the request to the service and store the response.
158 PermissionClient client = new PermissionClient();
159 Response res = client.readSearchList("acquisition");
161 assertStatusCode(res, testName);
162 PermissionsList list = res.readEntity(PermissionsList.class);
163 int EXPECTED_ITEMS = 2 + 4; //2 seeded base resource permissions and 4 workflow-related permissions
164 int actual = list.getPermission().size();
165 if (logger.isDebugEnabled()) {
166 logger.debug(testName + ": received = " + actual
167 + " expected=" + EXPECTED_ITEMS);
169 // Optionally output additional data about list members for debugging.
170 boolean iterateThroughList = true;
171 if ((iterateThroughList || (EXPECTED_ITEMS != list.getPermission().size()))
172 && logger.isDebugEnabled()) {
173 printList(testName, list);
175 Assert.assertEquals(list.getPermission().size(), EXPECTED_ITEMS);
184 public void delete(String testName) throws Exception {
185 //This method does nothing because we want to postpone the "delete" test until after
186 //the "updateNotAllowed" test gets run. Our "localDelete" test will call the real "delete" test later.
189 @Test(dataProvider = "testName",
190 dependsOnMethods = {"updateNotAllowed", "updateActions"})
191 public void localDelete(String testName) throws Exception {
192 super.delete(testName);
195 @Test(dataProvider = "testName",
196 dependsOnMethods = {"CRUDTests"})
197 public void updateNotAllowed(String testName) throws Exception {
202 Permission permToUpdate = new Permission();
203 permToUpdate.setCsid(knownResourceId);
204 // Update the content of this resource.
205 permToUpdate.setResourceName("updated-resource");
206 if (logger.isDebugEnabled()) {
207 logger.debug("updated object");
208 logger.debug(objectAsXmlString(permToUpdate,
211 PermissionClient client = new PermissionClient();
212 // Submit the request to the service and store the response.
213 Response res = client.update(knownResourceId, permToUpdate);
214 int statusCode = res.getStatus();
215 // Check the status code of the response: does it match the expected response(s)?
216 if (logger.isDebugEnabled()) {
217 logger.debug(testName + ": status = " + statusCode);
219 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
220 invalidStatusCodeMessage(testRequestType, statusCode));
221 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
228 * @param testName the test name
229 * @throws Exception the exception
231 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
232 dependsOnMethods = {"updateNotAllowed"})
233 public void updateActions(String testName) throws Exception {
237 Permission permToUpdate = new Permission();
238 permToUpdate.setCsid(knownResourceId);
239 permToUpdate.setResourceName(knownResource);
240 // Update the content of this resource.
241 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
242 int default_actions = actions.size();
245 int toUpdate_actions = actions.size();
246 if (logger.isDebugEnabled()) {
247 logger.debug(testName + " no. of actions default=" + default_actions
248 + " to update =" + toUpdate_actions);
250 permToUpdate.setAction(actions);
251 if (logger.isDebugEnabled()) {
252 logger.debug(testName + " updated object\n"
253 + objectAsXmlString(permToUpdate, Permission.class));
255 PermissionClient client = new PermissionClient();
256 // Submit the request to the service and store the response.
257 Response res = client.update(knownResourceId, permToUpdate);
258 int statusCode = res.getStatus();
259 // Check the status code of the response: does it match the expected response(s)?
260 if (logger.isDebugEnabled()) {
261 logger.debug(testName + ": status = " + statusCode);
263 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
264 invalidStatusCodeMessage(testRequestType, statusCode));
265 Assert.assertEquals(statusCode, testExpectedStatusCode);
267 Permission permUpdated = res.readEntity(Permission.class);
268 Assert.assertNotNull(permUpdated);
269 int updated_actions = permToUpdate.getAction().size();
270 if (logger.isDebugEnabled()) {
271 logger.debug(testName + " no. of actions to update=" + toUpdate_actions
272 + " updated =" + updated_actions);
274 Assert.assertEquals(toUpdate_actions,
276 "Data in updated object did not match submitted data.");
280 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
283 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
284 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
285 public void updateNonExistent(String testName) throws Exception {
287 setupUpdateNonExistent();
289 // Submit the request to the service and store the response.
291 // Note: The ID used in this 'create' call may be arbitrary.
292 // The only relevant ID may be the one used in updatePermission(), below.
293 PermissionClient client = new PermissionClient();
294 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
295 Permission permission = createPermissionInstance("test-acquisitions",
296 "default permissions for test-acquisitions",
302 Response res = client.update(NON_EXISTENT_ID, permission);
304 int statusCode = res.getStatus();
306 // Check the status code of the response: does it match
307 // the expected response(s)?
308 if (logger.isDebugEnabled()) {
309 logger.debug(testName + ": status = " + statusCode);
311 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
312 invalidStatusCodeMessage(testRequestType, statusCode));
313 Assert.assertEquals(statusCode, testExpectedStatusCode);
319 // ---------------------------------------------------------------
321 // ---------------------------------------------------------------
324 public void searchWorkflowDeleted(String testName) throws Exception {
325 // Fixme: null test for now, overriding test in base class
328 // ---------------------------------------------------------------
329 // Utility methods used by tests above
330 // ---------------------------------------------------------------
332 * create permission instance
333 * @param resourceName
335 * @param actionList list of actions for this permission
336 * @param effect effect of the permission
337 * @param useResourceName
342 public static Permission createPermissionInstance(String resourceName,
344 List<PermissionAction> actionList,
346 boolean useResourceName,
350 Permission permission = PermissionFactory.createPermissionInstance(resourceName,
351 description, actionList, effect,
352 useResourceName, useAction, useEffect);
354 if (logger.isDebugEnabled()) {
355 logger.debug("to be created, permission");
356 logger.debug(objectAsXmlString(permission, Permission.class));
365 * @param testName the test name
366 * @param list the list
370 protected void printList(String testName, PermissionsList list) {
371 for (Permission permission : list.getPermission()) {
372 logger.debug(testName + " permission csid=" + permission.getCsid()
373 + " name=" + permission.getResourceName()
374 + " desc=" + permission.getDescription());
379 protected Permission createInstance(String commonPartName, String identifier) {
380 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
381 Permission permission = createPermissionInstance(identifier,
382 "default permissions for " + identifier,
392 protected Permission updateInstance(Permission original) {
393 Permission result = new Permission();
395 result.setCsid(original.getCsid());
396 result.setResourceName(original.getResourceName());
397 // Update the content of this resource.
398 result.setDescription("updated-" + original.getDescription());
404 protected void compareUpdatedInstances(Permission original,
405 Permission updated) throws Exception {
406 Assert.assertEquals(updated.getCsid(),
408 "CSID in updated object did not match submitted data.");
410 Assert.assertEquals(updated.getResourceName(),
411 original.getResourceName(),
412 "Resource name in updated object did not match submitted data.");
414 Assert.assertEquals(updated.getDescription(),
415 original.getDescription(),
416 "Description in updated object did not match submitted data.");
420 protected Class<PermissionsList> getCommonListType() {
421 return PermissionsList.class;
425 @Test(dataProvider = "testName",
427 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
428 public void CRUDTests(String testName) {
429 // Do nothing. Simply here to for a TestNG execution order for our tests
433 public void updateWithEmptyEntityBody(String testName) throws Exception {
434 // TODO Auto-generated method stub
439 public void updateWithMalformedXml(String testName) throws Exception {
440 // TODO Auto-generated method stub
445 public void updateWithWrongXmlSchema(String testName) throws Exception {
446 // TODO Auto-generated method stub
451 protected long getSizeOfList(PermissionsList list) {
452 // TODO Auto-generated method stub
453 return list.getPermission().size();