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