]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5fceb11767eefe6dcee9759ee36bf65579698e94
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23 package org.collectionspace.services.authorization.client.test;
24
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;
30
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;
40
41 import org.testng.Assert;
42 import org.testng.annotations.Test;
43
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * PermissionServiceTest, carries out tests against a
49  * deployed and running Permission Service.
50  * 
51  * $LastChangedRevision: 917 $
52  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
53  */
54 public class PermissionServiceTest extends AbstractServiceTestImpl<PermissionsList, Permission,
55                 Permission, Permission> {
56
57     /** The Constant logger. */
58     private final static String CLASS_NAME = PermissionServiceTest.class.getName();
59     private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
60     
61     // Instance variables specific to this test.
62     private String knownResource = "accounts-test";
63
64     @Override
65     public String getServiceName() { 
66         return PermissionClient.SERVICE_NAME;
67     }
68     
69     /* (non-Javadoc)
70      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
71      */
72     @Override
73     protected String getServicePathComponent() {
74         return PermissionClient.SERVICE_PATH_COMPONENT;
75     }
76
77     /* (non-Javadoc)
78      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
79      */
80     @Override
81     protected CollectionSpaceClient getClientInstance() {
82         return new PermissionClient();
83     }
84
85     /**
86      * The entity type expected from the JAX-RS Response object
87      */
88     public Class<Permission> getEntityResponseType() {
89         return Permission.class;
90     }
91     
92     /* (non-Javadoc)
93      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
94      */
95 //    @Test(dataProvider = "testName")
96     @Override
97     public void readPaginatedList(String testName) throws Exception {
98         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
99     }
100
101     @Override
102     protected String getKnowResourceIdentifier() {
103         return knownResource;
104     }
105     
106     /**
107      * Creates the without resource name.
108      *
109      * @param testName the test name
110      * @throws Exception the exception
111      */
112     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
113                 dependsOnMethods = {"CRUDTests"})
114     public void createWithoutResourceName(String testName) throws Exception {
115         setupCreate();
116
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",
121                 actions,
122                 EffectType.PERMIT,
123                 false,
124                 true,
125                 true);
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);
132         }
133         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
134                 invalidStatusCodeMessage(testRequestType, statusCode));
135         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
136     }
137
138     /**
139      * Search resource name.
140      *
141      * @param testName the test name
142      * @throws Exception the exception
143      */
144     @Test(dataProvider = "testName",
145                 dependsOnMethods = {"CRUDTests"})
146     public void searchResourceName(String testName) throws Exception {
147         // Perform setup.
148         setupReadList();
149
150         // Submit the request to the service and store the response.
151         PermissionClient client = new PermissionClient();
152         ClientResponse<PermissionsList> res = client.readSearchList("acquisition");
153         try {
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);
161                 }
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);
167                 }
168                 Assert.assertEquals(list.getPermission().size(), EXPECTED_ITEMS);
169         } finally {
170                 if (res != null) {
171                 res.releaseConnection();
172             }
173         }
174     }
175     
176     @Override
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.
180     }
181     
182     @Test(dataProvider = "testName",
183                 dependsOnMethods = {"updateNotAllowed", "updateActions"})
184     public void localDelete(String testName) throws Exception {
185         super.delete(testName);
186     }
187
188     @Test(dataProvider = "testName",
189                 dependsOnMethods = {"CRUDTests"})
190     public void updateNotAllowed(String testName) throws Exception {
191
192         // Perform setup.
193         setupUpdate();
194
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,
202                     Permission.class));
203         }
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);
211         }
212         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
213                 invalidStatusCodeMessage(testRequestType, statusCode));
214         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
215
216     }
217
218     /**
219      * Update actions.
220      *
221      * @param testName the test name
222      * @throws Exception the exception
223      */
224     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
225                 dependsOnMethods = {"updateNotAllowed"})
226     public void updateActions(String testName) throws Exception {
227         // Perform setup.
228         setupUpdate();
229
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();
236         actions.remove(0);
237         actions.remove(0);
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);
242         }
243         permToUpdate.setAction(actions);
244         if (logger.isDebugEnabled()) {
245             logger.debug(testName + " updated object\n"
246                     + objectAsXmlString(permToUpdate, Permission.class));
247         }
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);
255         }
256         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
257                 invalidStatusCodeMessage(testRequestType, statusCode));
258         Assert.assertEquals(statusCode, testExpectedStatusCode);
259
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);
266         }
267         Assert.assertEquals(toUpdate_actions,
268                 updated_actions,
269                 "Data in updated object did not match submitted data.");
270     }
271     
272     /* (non-Javadoc)
273      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
274      */
275     @Override
276 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
277 //      dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
278     public void updateNonExistent(String testName) throws Exception {
279         // Perform setup.
280         setupUpdateNonExistent();
281
282         // Submit the request to the service and store the response.
283         //
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",
290                 actions,
291                 EffectType.PERMIT,
292                 true,
293                 true,
294                 true);
295         ClientResponse<Permission> res =
296                 client.update(NON_EXISTENT_ID, permission);
297         int statusCode = res.getStatus();
298
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);
303         }
304         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
305                 invalidStatusCodeMessage(testRequestType, statusCode));
306         Assert.assertEquals(statusCode, testExpectedStatusCode);
307     }
308     
309     // ---------------------------------------------------------------
310     // Search tests
311     // ---------------------------------------------------------------
312     
313     @Override
314     public void searchWorkflowDeleted(String testName) throws Exception {
315         // Fixme: null test for now, overriding test in base class
316     }
317
318     // ---------------------------------------------------------------
319     // Utility methods used by tests above
320     // ---------------------------------------------------------------
321     /**
322      * create permission instance
323      * @param resourceName
324      * @param description
325      * @param actionList list of actions for this permission
326      * @param effect effect of the permission
327      * @param useResourceName
328      * @param useAction
329      * @param useEffect
330      * @return permission
331      */
332     public static Permission createPermissionInstance(String resourceName,
333             String description,
334             List<PermissionAction> actionList,
335             EffectType effect,
336             boolean useResourceName,
337             boolean useAction,
338             boolean useEffect) {
339
340         Permission permission = PermissionFactory.createPermissionInstance(resourceName,
341                 description, actionList, effect,
342                 useResourceName, useAction, useEffect);
343
344         if (logger.isDebugEnabled()) {
345             logger.debug("to be created, permission");
346             logger.debug(objectAsXmlString(permission, Permission.class));
347         }
348         return permission;
349     }
350
351     /**
352      * Prints the list.
353      *
354      * @param testName the test name
355      * @param list the list
356      * @return the int
357      */
358     @Override
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());
364         }
365     }
366
367         @Override
368         protected Permission createInstance(String commonPartName, String identifier) {
369         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
370         Permission permission = createPermissionInstance(identifier,
371                 "default permissions for " + identifier,
372                 actions,
373                 EffectType.PERMIT,
374                 true,
375                 true,
376                 true);
377         return permission;
378         }
379
380         @Override
381         protected Permission updateInstance(Permission original) {
382                 Permission result = new Permission();
383                 
384                 result.setCsid(original.getCsid());
385                 result.setResourceName(original.getResourceName());
386         // Update the content of this resource.
387                 result.setDescription("updated-" + original.getDescription());
388                 
389                 return result;
390         }
391
392         @Override
393         protected void compareUpdatedInstances(Permission original,
394                         Permission updated) throws Exception {
395         Assert.assertEquals(updated.getCsid(),
396                         original.getCsid(),
397                 "CSID in updated object did not match submitted data.");
398
399         Assert.assertEquals(updated.getResourceName(),
400                         original.getResourceName(),
401                 "Resource name in updated object did not match submitted data.");
402
403         Assert.assertEquals(updated.getDescription(),
404                         original.getDescription(),
405                 "Description in updated object did not match submitted data.");
406     }
407
408         @Override
409         protected Class<PermissionsList> getCommonListType() {
410                 return PermissionsList.class;
411         }
412
413     @Override
414     @Test(dataProvider = "testName",
415                 dependsOnMethods = {
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
419     }
420
421         @Override
422         public void updateWithEmptyEntityBody(String testName) throws Exception {
423                 // TODO Auto-generated method stub
424                 
425         }
426
427         @Override
428         public void updateWithMalformedXml(String testName) throws Exception {
429                 // TODO Auto-generated method stub
430                 
431         }
432
433         @Override
434         public void updateWithWrongXmlSchema(String testName) throws Exception {
435                 // TODO Auto-generated method stub
436                 
437         }
438
439         @Override
440         protected long getSizeOfList(PermissionsList list) {
441                 // TODO Auto-generated method stub
442                 return list.getPermission().size();
443         }
444 }