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 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
88 // @Test(dataProvider = "testName")
90 public void readPaginatedList(String testName) throws Exception {
91 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
95 protected String getKnowResourceIdentifier() {
100 * Creates the without resource name.
102 * @param testName the test name
103 * @throws Exception the exception
105 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
106 dependsOnMethods = {"CRUDTests"})
107 public void createWithoutResourceName(String testName) throws Exception {
110 // Submit the request to the service and store the response.
111 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
112 Permission permission = createPermissionInstance(null,
113 "default permissions for account",
119 PermissionClient client = new PermissionClient();
120 ClientResponse<Response> res = client.create(permission);
121 int statusCode = res.getStatus();
122 // Does it exactly match the expected status code?
123 if (logger.isDebugEnabled()) {
124 logger.debug(testName + ": status = " + statusCode);
126 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
127 invalidStatusCodeMessage(testRequestType, statusCode));
128 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
132 * Search resource name.
134 * @param testName the test name
135 * @throws Exception the exception
137 @Test(dataProvider = "testName",
138 dependsOnMethods = {"CRUDTests"})
139 public void searchResourceName(String testName) throws Exception {
143 // Submit the request to the service and store the response.
144 PermissionClient client = new PermissionClient();
145 ClientResponse<PermissionsList> res = client.readSearchList("acquisition");
147 assertStatusCode(res, testName);
148 PermissionsList list = res.getEntity(PermissionsList.class);
149 int EXPECTED_ITEMS = 2 + 4; //2 seeded base resource permissions and 4 workflow-related permissions
150 int actual = list.getPermission().size();
151 if (logger.isDebugEnabled()) {
152 logger.debug(testName + ": received = " + actual
153 + " expected=" + EXPECTED_ITEMS);
155 // Optionally output additional data about list members for debugging.
156 boolean iterateThroughList = true;
157 if ((iterateThroughList || (EXPECTED_ITEMS != list.getPermission().size()))
158 && logger.isDebugEnabled()) {
159 printList(testName, list);
161 Assert.assertEquals(list.getPermission().size(), EXPECTED_ITEMS);
164 res.releaseConnection();
170 public void delete(String testName) throws Exception {
171 //This method does nothing because we want to postpone the "delete" test until after
172 //the "updateNotAllowed" test gets run. Our "localDelete" test will call the real "delete" test later.
175 @Test(dataProvider = "testName",
176 dependsOnMethods = {"updateNotAllowed", "updateActions"})
177 public void localDelete(String testName) throws Exception {
178 super.delete(testName);
181 @Test(dataProvider = "testName",
182 dependsOnMethods = {"CRUDTests"})
183 public void updateNotAllowed(String testName) throws Exception {
188 Permission permToUpdate = new Permission();
189 permToUpdate.setCsid(knownResourceId);
190 // Update the content of this resource.
191 permToUpdate.setResourceName("updated-resource");
192 if (logger.isDebugEnabled()) {
193 logger.debug("updated object");
194 logger.debug(objectAsXmlString(permToUpdate,
197 PermissionClient client = new PermissionClient();
198 // Submit the request to the service and store the response.
199 ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
200 int statusCode = res.getStatus();
201 // Check the status code of the response: does it match the expected response(s)?
202 if (logger.isDebugEnabled()) {
203 logger.debug(testName + ": status = " + statusCode);
205 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
206 invalidStatusCodeMessage(testRequestType, statusCode));
207 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
214 * @param testName the test name
215 * @throws Exception the exception
217 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
218 dependsOnMethods = {"updateNotAllowed"})
219 public void updateActions(String testName) throws Exception {
223 Permission permToUpdate = new Permission();
224 permToUpdate.setCsid(knownResourceId);
225 permToUpdate.setResourceName(knownResource);
226 // Update the content of this resource.
227 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
228 int default_actions = actions.size();
231 int toUpdate_actions = actions.size();
232 if (logger.isDebugEnabled()) {
233 logger.debug(testName + " no. of actions default=" + default_actions
234 + " to update =" + toUpdate_actions);
236 permToUpdate.setAction(actions);
237 if (logger.isDebugEnabled()) {
238 logger.debug(testName + " updated object\n"
239 + objectAsXmlString(permToUpdate, Permission.class));
241 PermissionClient client = new PermissionClient();
242 // Submit the request to the service and store the response.
243 ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
244 int statusCode = res.getStatus();
245 // Check the status code of the response: does it match the expected response(s)?
246 if (logger.isDebugEnabled()) {
247 logger.debug(testName + ": status = " + statusCode);
249 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
250 invalidStatusCodeMessage(testRequestType, statusCode));
251 Assert.assertEquals(statusCode, testExpectedStatusCode);
253 Permission permUpdated = (Permission) res.getEntity();
254 Assert.assertNotNull(permUpdated);
255 int updated_actions = permToUpdate.getAction().size();
256 if (logger.isDebugEnabled()) {
257 logger.debug(testName + " no. of actions to update=" + toUpdate_actions
258 + " updated =" + updated_actions);
260 Assert.assertEquals(toUpdate_actions,
262 "Data in updated object did not match submitted data.");
266 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
269 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
270 // dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
271 public void updateNonExistent(String testName) throws Exception {
273 setupUpdateNonExistent();
275 // Submit the request to the service and store the response.
277 // Note: The ID used in this 'create' call may be arbitrary.
278 // The only relevant ID may be the one used in updatePermission(), below.
279 PermissionClient client = new PermissionClient();
280 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
281 Permission permission = createPermissionInstance("test-acquisitions",
282 "default permissions for test-acquisitions",
288 ClientResponse<Permission> res =
289 client.update(NON_EXISTENT_ID, permission);
290 int statusCode = res.getStatus();
292 // Check the status code of the response: does it match
293 // the expected response(s)?
294 if (logger.isDebugEnabled()) {
295 logger.debug(testName + ": status = " + statusCode);
297 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
298 invalidStatusCodeMessage(testRequestType, statusCode));
299 Assert.assertEquals(statusCode, testExpectedStatusCode);
302 // ---------------------------------------------------------------
304 // ---------------------------------------------------------------
307 public void searchWorkflowDeleted(String testName) throws Exception {
308 // Fixme: null test for now, overriding test in base class
311 // ---------------------------------------------------------------
312 // Utility methods used by tests above
313 // ---------------------------------------------------------------
315 * create permission instance
316 * @param resourceName
318 * @param actionList list of actions for this permission
319 * @param effect effect of the permission
320 * @param useResourceName
325 public static Permission createPermissionInstance(String resourceName,
327 List<PermissionAction> actionList,
329 boolean useResourceName,
333 Permission permission = PermissionFactory.createPermissionInstance(resourceName,
334 description, actionList, effect,
335 useResourceName, useAction, useEffect);
337 if (logger.isDebugEnabled()) {
338 logger.debug("to be created, permission");
339 logger.debug(objectAsXmlString(permission, Permission.class));
347 * @param testName the test name
348 * @param list the list
352 protected void printList(String testName, PermissionsList list) {
353 for (Permission permission : list.getPermission()) {
354 logger.debug(testName + " permission csid=" + permission.getCsid()
355 + " name=" + permission.getResourceName()
356 + " desc=" + permission.getDescription());
361 protected Permission createInstance(String commonPartName, String identifier) {
362 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
363 Permission permission = createPermissionInstance(identifier,
364 "default permissions for " + identifier,
374 protected Permission updateInstance(Permission original) {
375 Permission result = new Permission();
377 result.setCsid(original.getCsid());
378 result.setResourceName(original.getResourceName());
379 // Update the content of this resource.
380 result.setDescription("updated-" + original.getDescription());
386 protected void compareUpdatedInstances(Permission original,
387 Permission updated) throws Exception {
388 Assert.assertEquals(updated.getCsid(),
390 "CSID in updated object did not match submitted data.");
392 Assert.assertEquals(updated.getResourceName(),
393 original.getResourceName(),
394 "Resource name in updated object did not match submitted data.");
396 Assert.assertEquals(updated.getDescription(),
397 original.getDescription(),
398 "Description in updated object did not match submitted data.");
402 protected Class<PermissionsList> getCommonListType() {
403 return PermissionsList.class;
407 @Test(dataProvider = "testName",
409 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
410 public void CRUDTests(String testName) {
411 // Do nothing. Simply here to for a TestNG execution order for our tests
415 public void updateWithEmptyEntityBody(String testName) throws Exception {
416 // TODO Auto-generated method stub
421 public void updateWithMalformedXml(String testName) throws Exception {
422 // TODO Auto-generated method stub
427 public void updateWithWrongXmlSchema(String testName) throws Exception {
428 // TODO Auto-generated method stub