]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f89259cf7287951c944477de0d4e8765d23253e8
[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
28 import javax.ws.rs.core.Response;
29
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;
39
40 import org.testng.Assert;
41 import org.testng.annotations.Test;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * PermissionServiceTest, carries out tests against a
47  * deployed and running Permission Service.
48  * 
49  * $LastChangedRevision: 917 $
50  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
51  */
52 public class PermissionServiceTest extends AbstractServiceTestImpl<PermissionsList, Permission,
53                 Permission, Permission> {
54
55     /** The Constant logger. */
56     private final static String CLASS_NAME = PermissionServiceTest.class.getName();
57     private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
58     
59     // Instance variables specific to this test.
60     private String knownResource = "accounts-test";
61
62     @Override
63     public String getServiceName() { 
64         return PermissionClient.SERVICE_NAME;
65     }
66     
67     /* (non-Javadoc)
68      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
69      */
70     @Override
71     protected String getServicePathComponent() {
72         return PermissionClient.SERVICE_PATH_COMPONENT;
73     }
74
75     /* (non-Javadoc)
76      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
77      */
78     @Override
79     protected CollectionSpaceClient getClientInstance() {
80         return new PermissionClient();
81     }
82
83         @Override
84         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
85         return new PermissionClient(clientPropertiesFilename);
86         }
87
88     /**
89      * The entity type expected from the JAX-RS Response object
90      */
91     public Class<Permission> getEntityResponseType() {
92         return Permission.class;
93     }
94     
95     /* (non-Javadoc)
96      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
97      */
98 //    @Test(dataProvider = "testName")
99     @Override
100     public void readPaginatedList(String testName) throws Exception {
101         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
102     }
103
104     @Override
105     protected String getKnowResourceIdentifier() {
106         return knownResource;
107     }
108     
109     /**
110      * Creates the without resource name.
111      *
112      * @param testName the test name
113      * @throws Exception the exception
114      */
115     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
116                 dependsOnMethods = {"CRUDTests"})
117     public void createWithoutResourceName(String testName) throws Exception {
118         setupCreate();
119
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",
124                 actions,
125                 EffectType.PERMIT,
126                 false,
127                 true,
128                 true);
129         PermissionClient client = new PermissionClient();
130         Response res = client.create(permission);
131         try {
132                 int statusCode = res.getStatus();
133                 // Does it exactly match the expected status code?
134                 if (logger.isDebugEnabled()) {
135                     logger.debug(testName + ": status = " + statusCode);
136                 }
137                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
138                         invalidStatusCodeMessage(testRequestType, statusCode));
139                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
140         } finally {
141                 res.close();
142         }
143     }
144
145     /**
146      * Search resource name.
147      *
148      * @param testName the test name
149      * @throws Exception the exception
150      */
151     @Test(dataProvider = "testName",
152                 dependsOnMethods = {"CRUDTests"})
153     public void searchResourceName(String testName) throws Exception {
154         // Perform setup.
155         setupReadList();
156
157         // Submit the request to the service and store the response.
158         PermissionClient client = new PermissionClient();
159         Response res = client.readSearchList("acquisition");
160         try {
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);
168                 }
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);
174                 }
175                 Assert.assertEquals(list.getPermission().size(), EXPECTED_ITEMS);
176         } finally {
177                 if (res != null) {
178                 res.close();
179             }
180         }
181     }
182     
183     @Override
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.
187     }
188     
189     @Test(dataProvider = "testName",
190                 dependsOnMethods = {"updateNotAllowed", "updateActions"})
191     public void localDelete(String testName) throws Exception {
192         super.delete(testName);
193     }
194
195     @Test(dataProvider = "testName",
196                 dependsOnMethods = {"CRUDTests"})
197     public void updateNotAllowed(String testName) throws Exception {
198
199         // Perform setup.
200         setupUpdate();
201
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,
209                     Permission.class));
210         }
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);
218         }
219         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
220                 invalidStatusCodeMessage(testRequestType, statusCode));
221         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
222
223     }
224
225     /**
226      * Update actions.
227      *
228      * @param testName the test name
229      * @throws Exception the exception
230      */
231     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
232                 dependsOnMethods = {"updateNotAllowed"})
233     public void updateActions(String testName) throws Exception {
234         // Perform setup.
235         setupUpdate();
236
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();
243         actions.remove(0);
244         actions.remove(0);
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);
249         }
250         permToUpdate.setAction(actions);
251         if (logger.isDebugEnabled()) {
252             logger.debug(testName + " updated object\n"
253                     + objectAsXmlString(permToUpdate, Permission.class));
254         }
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);
262         }
263         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
264                 invalidStatusCodeMessage(testRequestType, statusCode));
265         Assert.assertEquals(statusCode, testExpectedStatusCode);
266
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);
273         }
274         Assert.assertEquals(toUpdate_actions,
275                 updated_actions,
276                 "Data in updated object did not match submitted data.");
277     }
278     
279     /* (non-Javadoc)
280      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
281      */
282     @Override
283 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
284 //      dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
285     public void updateNonExistent(String testName) throws Exception {
286         // Perform setup.
287         setupUpdateNonExistent();
288
289         // Submit the request to the service and store the response.
290         //
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",
297                 actions,
298                 EffectType.PERMIT,
299                 true,
300                 true,
301                 true);
302         Response res = client.update(NON_EXISTENT_ID, permission);
303         try {
304                 int statusCode = res.getStatus();
305         
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);
310                 }
311                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
312                         invalidStatusCodeMessage(testRequestType, statusCode));
313                 Assert.assertEquals(statusCode, testExpectedStatusCode);
314         } finally {
315                 res.close();
316         }
317     }
318     
319     // ---------------------------------------------------------------
320     // Search tests
321     // ---------------------------------------------------------------
322     
323     @Override
324     public void searchWorkflowDeleted(String testName) throws Exception {
325         // Fixme: null test for now, overriding test in base class
326     }
327
328     // ---------------------------------------------------------------
329     // Utility methods used by tests above
330     // ---------------------------------------------------------------
331     /**
332      * create permission instance
333      * @param resourceName
334      * @param description
335      * @param actionList list of actions for this permission
336      * @param effect effect of the permission
337      * @param useResourceName
338      * @param useAction
339      * @param useEffect
340      * @return permission
341      */
342     public static Permission createPermissionInstance(String resourceName,
343             String description,
344             List<PermissionAction> actionList,
345             EffectType effect,
346             boolean useResourceName,
347             boolean useAction,
348             boolean useEffect) {
349
350         Permission permission = PermissionFactory.createPermissionInstance(resourceName,
351                 description, actionList, effect,
352                 useResourceName, useAction, useEffect);
353
354         if (logger.isDebugEnabled()) {
355             logger.debug("to be created, permission");
356             logger.debug(objectAsXmlString(permission, Permission.class));
357         }
358         
359         return permission;
360     }
361
362     /**
363      * Prints the list.
364      *
365      * @param testName the test name
366      * @param list the list
367      * @return the int
368      */
369     @Override
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());
375         }
376     }
377
378         @Override
379         protected Permission createInstance(String commonPartName, String identifier) {
380         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
381         Permission permission = createPermissionInstance(identifier,
382                 "default permissions for " + identifier,
383                 actions,
384                 EffectType.PERMIT,
385                 true,
386                 true,
387                 true);
388         return permission;
389         }
390
391         @Override
392         protected Permission updateInstance(Permission original) {
393                 Permission result = new Permission();
394                 
395                 result.setCsid(original.getCsid());
396                 result.setResourceName(original.getResourceName());
397         // Update the content of this resource.
398                 result.setDescription("updated-" + original.getDescription());
399                 
400                 return result;
401         }
402
403         @Override
404         protected void compareUpdatedInstances(Permission original,
405                         Permission updated) throws Exception {
406         Assert.assertEquals(updated.getCsid(),
407                         original.getCsid(),
408                 "CSID in updated object did not match submitted data.");
409
410         Assert.assertEquals(updated.getResourceName(),
411                         original.getResourceName(),
412                 "Resource name in updated object did not match submitted data.");
413
414         Assert.assertEquals(updated.getDescription(),
415                         original.getDescription(),
416                 "Description in updated object did not match submitted data.");
417     }
418
419         @Override
420         protected Class<PermissionsList> getCommonListType() {
421                 return PermissionsList.class;
422         }
423
424     @Override
425     @Test(dataProvider = "testName",
426                 dependsOnMethods = {
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
430     }
431
432         @Override
433         public void updateWithEmptyEntityBody(String testName) throws Exception {
434                 // TODO Auto-generated method stub
435                 
436         }
437
438         @Override
439         public void updateWithMalformedXml(String testName) throws Exception {
440                 // TODO Auto-generated method stub
441                 
442         }
443
444         @Override
445         public void updateWithWrongXmlSchema(String testName) throws Exception {
446                 // TODO Auto-generated method stub
447                 
448         }
449
450         @Override
451         protected long getSizeOfList(PermissionsList list) {
452                 // TODO Auto-generated method stub
453                 return list.getPermission().size();
454         }
455 }