]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
eea6502f5d15ea02dc59dc3dba3f6dba49655e7d
[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
31
32 //import org.collectionspace.services.authorization.ActionType;
33 import org.collectionspace.services.authorization.perms.EffectType;
34 import org.collectionspace.services.client.CollectionSpaceClient;
35 import org.collectionspace.services.client.PermissionClient;
36 import org.collectionspace.services.authorization.perms.Permission;
37 import org.collectionspace.services.authorization.perms.PermissionAction;
38 import org.collectionspace.services.authorization.perms.PermissionsList;
39 import org.collectionspace.services.client.PermissionFactory;
40 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
41 import org.testng.Assert;
42 import org.testng.annotations.Test;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * PermissionServiceTest, carries out tests against a
48  * deployed and running Permission Service.
49  * 
50  * $LastChangedRevision: 917 $
51  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
52  */
53 public class PermissionServiceTest extends AbstractServiceTestImpl<PermissionsList, Permission,
54                 Permission, Permission> {
55
56     /** The Constant logger. */
57     private final static String CLASS_NAME = PermissionServiceTest.class.getName();
58     private final static Logger logger = LoggerFactory.getLogger(CLASS_NAME);
59     
60     // Instance variables specific to this test.
61     private String knownResource = "accounts-test";
62
63     @Override
64     public String getServiceName() { 
65         return PermissionClient.SERVICE_NAME;
66     }
67     
68     /* (non-Javadoc)
69      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
70      */
71     @Override
72     protected String getServicePathComponent() {
73         return PermissionClient.SERVICE_PATH_COMPONENT;
74     }
75
76     /* (non-Javadoc)
77      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
78      */
79     @Override
80     protected CollectionSpaceClient getClientInstance() throws Exception {
81         return new PermissionClient();
82     }
83
84         @Override
85         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
86         return new PermissionClient(clientPropertiesFilename);
87         }
88
89     /**
90      * The entity type expected from the JAX-RS Response object
91      */
92     public Class<Permission> getEntityResponseType() {
93         return Permission.class;
94     }
95     
96     /* (non-Javadoc)
97      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
98      */
99 //    @Test(dataProvider = "testName")
100     @Override
101     public void readPaginatedList(String testName) throws Exception {
102         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
103     }
104
105     @Override
106     protected String getKnowResourceIdentifier() {
107         return knownResource;
108     }
109     
110     /**
111      * Creates the without resource name.
112      *
113      * @param testName the test name
114      * @throws Exception the exception
115      */
116     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
117                 dependsOnMethods = {"CRUDTests"})
118     public void createWithoutResourceName(String testName) throws Exception {
119         setupCreate();
120
121         // Submit the request to the service and store the response.
122         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
123         Permission permission = createPermissionInstance(null,
124                 "default permissions for account",
125                 actions,
126                 EffectType.PERMIT,
127                 false,
128                 true,
129                 true);
130         PermissionClient client = new PermissionClient();
131         Response res = client.create(permission);
132         try {
133                 int statusCode = res.getStatus();
134                 // Does it exactly match the expected status code?
135                 if (logger.isDebugEnabled()) {
136                     logger.debug(testName + ": status = " + statusCode);
137                 }
138                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
139                         invalidStatusCodeMessage(testRequestType, statusCode));
140                 Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
141         } finally {
142                 res.close();
143         }
144     }
145
146     /**
147      * Search resource name.
148      *
149      * @param testName the test name
150      * @throws Exception the exception
151      */
152     @Test(dataProvider = "testName",
153                 dependsOnMethods = {"CRUDTests"})
154     public void searchResourceName(String testName) throws Exception {
155         // Perform setup.
156         setupReadList();
157
158         // Submit the request to the service and store the response.
159         PermissionClient client = new PermissionClient();
160         Response res = client.readSearchList("acquisition");
161         try {
162                 assertStatusCode(res, testName);
163                 PermissionsList list = res.readEntity(PermissionsList.class);
164                 int EXPECTED_ITEMS = 2 + 4; //2 seeded base resource permissions and 4 workflow-related permissions
165                 int actual = list.getPermission().size();
166                 if (logger.isDebugEnabled()) {
167                     logger.debug(testName + ": received = " + actual
168                             + " expected=" + EXPECTED_ITEMS);
169                 }
170                 // Optionally output additional data about list members for debugging.
171                 boolean iterateThroughList = true;
172                 if ((iterateThroughList || (EXPECTED_ITEMS != list.getPermission().size()))
173                                 && logger.isDebugEnabled()) {
174                     printList(testName, list);
175                 }
176                 Assert.assertEquals(list.getPermission().size(), EXPECTED_ITEMS);
177         } finally {
178                 if (res != null) {
179                 res.close();
180             }
181         }
182     }
183     
184     @Override
185     public void delete(String testName) throws Exception {
186         //This method does nothing because we want to postpone the "delete" test until after
187         //the "updateNotAllowed" test gets run.  Our "localDelete" test will call the real "delete" test later.
188     }
189     
190     @Test(dataProvider = "testName",
191                 dependsOnMethods = {"updateNotAllowed", "updateActions"})
192     public void localDelete(String testName) throws Exception {
193         super.delete(testName);
194     }
195
196     @Test(dataProvider = "testName",
197                 dependsOnMethods = {"CRUDTests"})
198     public void updateNotAllowed(String testName) throws Exception {
199
200         // Perform setup.
201         setupUpdate();
202
203         Permission permToUpdate = new Permission();
204         permToUpdate.setCsid(knownResourceId);
205         // Update the content of this resource.
206         permToUpdate.setResourceName("updated-resource");
207         if (logger.isDebugEnabled()) {
208             logger.debug("updated object");
209             logger.debug(objectAsXmlString(permToUpdate,
210                     Permission.class));
211         }
212         PermissionClient client = new PermissionClient();
213         // Submit the request to the service and store the response.
214         Response res = client.update(knownResourceId, permToUpdate);
215         int statusCode = res.getStatus();
216         // Check the status code of the response: does it match the expected response(s)?
217         if (logger.isDebugEnabled()) {
218             logger.debug(testName + ": status = " + statusCode);
219         }
220         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
221                 invalidStatusCodeMessage(testRequestType, statusCode));
222         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
223
224     }
225
226     /**
227      * Update actions.
228      *
229      * @param testName the test name
230      * @throws Exception the exception
231      */
232     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
233                 dependsOnMethods = {"updateNotAllowed"})
234     public void updateActions(String testName) throws Exception {
235         // Perform setup.
236         setupUpdate();
237
238         Permission permToUpdate = new Permission();
239         permToUpdate.setCsid(knownResourceId);
240         permToUpdate.setResourceName(knownResource);
241         // Update the content of this resource.
242         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
243         int default_actions = actions.size();
244         actions.remove(0);
245         actions.remove(0);
246         int toUpdate_actions = actions.size();
247         if (logger.isDebugEnabled()) {
248             logger.debug(testName + " no. of actions default=" + default_actions
249                     + " to update =" + toUpdate_actions);
250         }
251         permToUpdate.setAction(actions);
252         if (logger.isDebugEnabled()) {
253             logger.debug(testName + " updated object\n"
254                     + objectAsXmlString(permToUpdate, Permission.class));
255         }
256         PermissionClient client = new PermissionClient();
257         // Submit the request to the service and store the response.
258         Response res = client.update(knownResourceId, permToUpdate);
259         int statusCode = res.getStatus();
260         // Check the status code of the response: does it match the expected response(s)?
261         if (logger.isDebugEnabled()) {
262             logger.debug(testName + ": status = " + statusCode);
263         }
264         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
265                 invalidStatusCodeMessage(testRequestType, statusCode));
266         Assert.assertEquals(statusCode, testExpectedStatusCode);
267
268         Permission permUpdated = res.readEntity(Permission.class);
269         Assert.assertNotNull(permUpdated);
270         int updated_actions = permToUpdate.getAction().size();
271         if (logger.isDebugEnabled()) {
272             logger.debug(testName + " no. of actions to update=" + toUpdate_actions
273                     + " updated =" + updated_actions);
274         }
275         Assert.assertEquals(toUpdate_actions,
276                 updated_actions,
277                 "Data in updated object did not match submitted data.");
278     }
279     
280     /* (non-Javadoc)
281      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
282      */
283     @Override
284 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
285 //      dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
286     public void updateNonExistent(String testName) throws Exception {
287         // Perform setup.
288         setupUpdateNonExistent();
289
290         // Submit the request to the service and store the response.
291         //
292         // Note: The ID used in this 'create' call may be arbitrary.
293         // The only relevant ID may be the one used in updatePermission(), below.
294         PermissionClient client = new PermissionClient();
295         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
296         Permission permission = createPermissionInstance("test-acquisitions",
297                 "default permissions for test-acquisitions",
298                 actions,
299                 EffectType.PERMIT,
300                 true,
301                 true,
302                 true);
303         Response res = client.update(NON_EXISTENT_ID, permission);
304         try {
305                 int statusCode = res.getStatus();
306         
307                 // Check the status code of the response: does it match
308                 // the expected response(s)?
309                 if (logger.isDebugEnabled()) {
310                     logger.debug(testName + ": status = " + statusCode);
311                 }
312                 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
313                         invalidStatusCodeMessage(testRequestType, statusCode));
314                 Assert.assertEquals(statusCode, testExpectedStatusCode);
315         } finally {
316                 res.close();
317         }
318     }
319     
320     // ---------------------------------------------------------------
321     // Search tests
322     // ---------------------------------------------------------------
323     
324     @Override
325     public void searchWorkflowDeleted(String testName) throws Exception {
326         // Fixme: null test for now, overriding test in base class
327     }
328
329     // ---------------------------------------------------------------
330     // Utility methods used by tests above
331     // ---------------------------------------------------------------
332     /**
333      * create permission instance
334      * @param resourceName
335      * @param description
336      * @param actionList list of actions for this permission
337      * @param effect effect of the permission
338      * @param useResourceName
339      * @param useAction
340      * @param useEffect
341      * @return permission
342      */
343     public static Permission createPermissionInstance(String resourceName,
344             String description,
345             List<PermissionAction> actionList,
346             EffectType effect,
347             boolean useResourceName,
348             boolean useAction,
349             boolean useEffect) {
350
351         Permission permission = PermissionFactory.createPermissionInstance(resourceName,
352                 description, actionList, effect,
353                 useResourceName, useAction, useEffect);
354
355         if (logger.isDebugEnabled()) {
356             logger.debug("to be created, permission");
357             logger.debug(objectAsXmlString(permission, Permission.class));
358         }
359         
360         return permission;
361     }
362
363     /**
364      * Prints the list.
365      *
366      * @param testName the test name
367      * @param list the list
368      * @return the int
369      */
370     @Override
371     protected void printList(String testName, PermissionsList list) {
372         for (Permission permission : list.getPermission()) {
373             logger.debug(testName + " permission csid=" + permission.getCsid()
374                     + " name=" + permission.getResourceName()
375                     + " desc=" + permission.getDescription());
376         }
377     }
378
379         @Override
380         protected Permission createInstance(String commonPartName, String identifier) {
381         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
382         Permission permission = createPermissionInstance(identifier,
383                 "default permissions for " + identifier,
384                 actions,
385                 EffectType.PERMIT,
386                 true,
387                 true,
388                 true);
389         return permission;
390         }
391
392         @Override
393         protected Permission updateInstance(Permission original) {
394                 Permission result = new Permission();
395                 
396                 result.setCsid(original.getCsid());
397                 result.setResourceName(original.getResourceName());
398         // Update the content of this resource.
399                 result.setDescription("updated-" + original.getDescription());
400                 
401                 return result;
402         }
403
404         @Override
405         protected void compareUpdatedInstances(Permission original,
406                         Permission updated) throws Exception {
407         Assert.assertEquals(updated.getCsid(),
408                         original.getCsid(),
409                 "CSID in updated object did not match submitted data.");
410
411         Assert.assertEquals(updated.getResourceName(),
412                         original.getResourceName(),
413                 "Resource name in updated object did not match submitted data.");
414
415         Assert.assertEquals(updated.getDescription(),
416                         original.getDescription(),
417                 "Description in updated object did not match submitted data.");
418     }
419
420         @Override
421         protected Class<PermissionsList> getCommonListType() {
422                 return PermissionsList.class;
423         }
424
425     @Override
426     @Test(dataProvider = "testName",
427                 dependsOnMethods = {
428                         "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})    
429     public void CRUDTests(String testName) {
430         // Do nothing.  Simply here to for a TestNG execution order for our tests
431     }
432
433         @Override
434         public void updateWithEmptyEntityBody(String testName) throws Exception {
435                 // TODO Auto-generated method stub
436                 
437         }
438
439         @Override
440         public void updateWithMalformedXml(String testName) throws Exception {
441                 // TODO Auto-generated method stub
442                 
443         }
444
445         @Override
446         public void updateWithWrongXmlSchema(String testName) throws Exception {
447                 // TODO Auto-generated method stub
448                 
449         }
450
451         @Override
452         protected long getSizeOfList(PermissionsList list) {
453                 // TODO Auto-generated method stub
454                 return list.getPermission().size();
455         }
456 }