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.jboss.resteasy.client.ClientResponse;
41 import org.testng.Assert;
42 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() {
82 return new PermissionClient();
86 * The entity type expected from the JAX-RS Response object
88 public Class<Permission> getEntityResponseType() {
89 return Permission.class;
93 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
95 // @Test(dataProvider = "testName")
97 public void readPaginatedList(String testName) throws Exception {
98 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
102 protected String getKnowResourceIdentifier() {
103 return knownResource;
107 * Creates the without resource name.
109 * @param testName the test name
110 * @throws Exception the exception
112 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
113 dependsOnMethods = {"CRUDTests"})
114 public void createWithoutResourceName(String testName) throws Exception {
117 // Submit the request to the service and store the response.
118 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
119 Permission permission = createPermissionInstance(null,
120 "default permissions for account",
126 PermissionClient client = new PermissionClient();
127 ClientResponse<Response> res = client.create(permission);
128 int statusCode = res.getStatus();
129 // Does it exactly match the expected status code?
130 if (logger.isDebugEnabled()) {
131 logger.debug(testName + ": status = " + statusCode);
133 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
134 invalidStatusCodeMessage(testRequestType, statusCode));
135 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
139 * Search resource name.
141 * @param testName the test name
142 * @throws Exception the exception
144 @Test(dataProvider = "testName",
145 dependsOnMethods = {"CRUDTests"})
146 public void searchResourceName(String testName) throws Exception {
150 // Submit the request to the service and store the response.
151 PermissionClient client = new PermissionClient();
152 ClientResponse<PermissionsList> res = client.readSearchList("acquisition");
154 assertStatusCode(res, testName);
155 PermissionsList list = res.getEntity(PermissionsList.class);
156 int EXPECTED_ITEMS = 2 + 4; //2 seeded base resource permissions and 4 workflow-related permissions
157 int actual = list.getPermission().size();
158 if (logger.isDebugEnabled()) {
159 logger.debug(testName + ": received = " + actual
160 + " expected=" + EXPECTED_ITEMS);
162 // Optionally output additional data about list members for debugging.
163 boolean iterateThroughList = true;
164 if ((iterateThroughList || (EXPECTED_ITEMS != list.getPermission().size()))
165 && logger.isDebugEnabled()) {
166 printList(testName, list);
168 Assert.assertEquals(list.getPermission().size(), EXPECTED_ITEMS);
171 res.releaseConnection();
177 public void delete(String testName) throws Exception {
178 //This method does nothing because we want to postpone the "delete" test until after
179 //the "updateNotAllowed" test gets run. Our "localDelete" test will call the real "delete" test later.
182 @Test(dataProvider = "testName",
183 dependsOnMethods = {"updateNotAllowed", "updateActions"})
184 public void localDelete(String testName) throws Exception {
185 super.delete(testName);
188 @Test(dataProvider = "testName",
189 dependsOnMethods = {"CRUDTests"})
190 public void updateNotAllowed(String testName) throws Exception {
195 Permission permToUpdate = new Permission();
196 permToUpdate.setCsid(knownResourceId);
197 // Update the content of this resource.
198 permToUpdate.setResourceName("updated-resource");
199 if (logger.isDebugEnabled()) {
200 logger.debug("updated object");
201 logger.debug(objectAsXmlString(permToUpdate,
204 PermissionClient client = new PermissionClient();
205 // Submit the request to the service and store the response.
206 ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
207 int statusCode = res.getStatus();
208 // Check the status code of the response: does it match the expected response(s)?
209 if (logger.isDebugEnabled()) {
210 logger.debug(testName + ": status = " + statusCode);
212 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
213 invalidStatusCodeMessage(testRequestType, statusCode));
214 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
221 * @param testName the test name
222 * @throws Exception the exception
224 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
225 dependsOnMethods = {"updateNotAllowed"})
226 public void updateActions(String testName) throws Exception {
230 Permission permToUpdate = new Permission();
231 permToUpdate.setCsid(knownResourceId);
232 permToUpdate.setResourceName(knownResource);
233 // Update the content of this resource.
234 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
235 int default_actions = actions.size();
238 int toUpdate_actions = actions.size();
239 if (logger.isDebugEnabled()) {
240 logger.debug(testName + " no. of actions default=" + default_actions
241 + " to update =" + toUpdate_actions);
243 permToUpdate.setAction(actions);
244 if (logger.isDebugEnabled()) {
245 logger.debug(testName + " updated object\n"
246 + objectAsXmlString(permToUpdate, Permission.class));
248 PermissionClient client = new PermissionClient();
249 // Submit the request to the service and store the response.
250 ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
251 int statusCode = res.getStatus();
252 // Check the status code of the response: does it match the expected response(s)?
253 if (logger.isDebugEnabled()) {
254 logger.debug(testName + ": status = " + statusCode);
256 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
257 invalidStatusCodeMessage(testRequestType, statusCode));
258 Assert.assertEquals(statusCode, testExpectedStatusCode);
260 Permission permUpdated = (Permission) res.getEntity();
261 Assert.assertNotNull(permUpdated);
262 int updated_actions = permToUpdate.getAction().size();
263 if (logger.isDebugEnabled()) {
264 logger.debug(testName + " no. of actions to update=" + toUpdate_actions
265 + " updated =" + updated_actions);
267 Assert.assertEquals(toUpdate_actions,
269 "Data in updated object did not match submitted data.");
273 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
276 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
277 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
278 public void updateNonExistent(String testName) throws Exception {
280 setupUpdateNonExistent();
282 // Submit the request to the service and store the response.
284 // Note: The ID used in this 'create' call may be arbitrary.
285 // The only relevant ID may be the one used in updatePermission(), below.
286 PermissionClient client = new PermissionClient();
287 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
288 Permission permission = createPermissionInstance("test-acquisitions",
289 "default permissions for test-acquisitions",
295 ClientResponse<Permission> res =
296 client.update(NON_EXISTENT_ID, permission);
297 int statusCode = res.getStatus();
299 // Check the status code of the response: does it match
300 // the expected response(s)?
301 if (logger.isDebugEnabled()) {
302 logger.debug(testName + ": status = " + statusCode);
304 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
305 invalidStatusCodeMessage(testRequestType, statusCode));
306 Assert.assertEquals(statusCode, testExpectedStatusCode);
309 // ---------------------------------------------------------------
311 // ---------------------------------------------------------------
314 public void searchWorkflowDeleted(String testName) throws Exception {
315 // Fixme: null test for now, overriding test in base class
318 // ---------------------------------------------------------------
319 // Utility methods used by tests above
320 // ---------------------------------------------------------------
322 * create permission instance
323 * @param resourceName
325 * @param actionList list of actions for this permission
326 * @param effect effect of the permission
327 * @param useResourceName
332 public static Permission createPermissionInstance(String resourceName,
334 List<PermissionAction> actionList,
336 boolean useResourceName,
340 Permission permission = PermissionFactory.createPermissionInstance(resourceName,
341 description, actionList, effect,
342 useResourceName, useAction, useEffect);
344 if (logger.isDebugEnabled()) {
345 logger.debug("to be created, permission");
346 logger.debug(objectAsXmlString(permission, Permission.class));
354 * @param testName the test name
355 * @param list the list
359 protected void printList(String testName, PermissionsList list) {
360 for (Permission permission : list.getPermission()) {
361 logger.debug(testName + " permission csid=" + permission.getCsid()
362 + " name=" + permission.getResourceName()
363 + " desc=" + permission.getDescription());
368 protected Permission createInstance(String commonPartName, String identifier) {
369 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
370 Permission permission = createPermissionInstance(identifier,
371 "default permissions for " + identifier,
381 protected Permission updateInstance(Permission original) {
382 Permission result = new Permission();
384 result.setCsid(original.getCsid());
385 result.setResourceName(original.getResourceName());
386 // Update the content of this resource.
387 result.setDescription("updated-" + original.getDescription());
393 protected void compareUpdatedInstances(Permission original,
394 Permission updated) throws Exception {
395 Assert.assertEquals(updated.getCsid(),
397 "CSID in updated object did not match submitted data.");
399 Assert.assertEquals(updated.getResourceName(),
400 original.getResourceName(),
401 "Resource name in updated object did not match submitted data.");
403 Assert.assertEquals(updated.getDescription(),
404 original.getDescription(),
405 "Description in updated object did not match submitted data.");
409 protected Class<PermissionsList> getCommonListType() {
410 return PermissionsList.class;
414 @Test(dataProvider = "testName",
416 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
417 public void CRUDTests(String testName) {
418 // Do nothing. Simply here to for a TestNG execution order for our tests
422 public void updateWithEmptyEntityBody(String testName) throws Exception {
423 // TODO Auto-generated method stub
428 public void updateWithMalformedXml(String testName) throws Exception {
429 // TODO Auto-generated method stub
434 public void updateWithWrongXmlSchema(String testName) throws Exception {
435 // TODO Auto-generated method stub
440 protected long getSizeOfList(PermissionsList list) {
441 // TODO Auto-generated method stub
442 return list.getPermission().size();