]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4d2e26365e93255c5bfbfe1e46ea0913fc7af79a
[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     /* (non-Javadoc)
86      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readPaginatedList(java.lang.String)
87      */
88 //    @Test(dataProvider = "testName")
89     @Override
90     public void readPaginatedList(String testName) throws Exception {
91         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
92     }
93
94     @Override
95     protected String getKnowResourceIdentifier() {
96         return knownResource;
97     }
98     
99     /**
100      * Creates the without resource name.
101      *
102      * @param testName the test name
103      * @throws Exception the exception
104      */
105     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
106                 dependsOnMethods = {"CRUDTests"})
107     public void createWithoutResourceName(String testName) throws Exception {
108         setupCreate();
109
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",
114                 actions,
115                 EffectType.PERMIT,
116                 false,
117                 true,
118                 true);
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);
125         }
126         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
127                 invalidStatusCodeMessage(testRequestType, statusCode));
128         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
129     }
130
131     /**
132      * Search resource name.
133      *
134      * @param testName the test name
135      * @throws Exception the exception
136      */
137     @Test(dataProvider = "testName",
138                 dependsOnMethods = {"CRUDTests"})
139     public void searchResourceName(String testName) throws Exception {
140         // Perform setup.
141         setupReadList();
142
143         // Submit the request to the service and store the response.
144         PermissionClient client = new PermissionClient();
145         ClientResponse<PermissionsList> res = client.readSearchList("acquisition");
146         try {
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);
154                 }
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);
160                 }
161                 Assert.assertEquals(list.getPermission().size(), EXPECTED_ITEMS);
162         } finally {
163                 if (res != null) {
164                 res.releaseConnection();
165             }
166         }
167     }
168     
169     @Override
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.
173     }
174     
175     @Test(dataProvider = "testName",
176                 dependsOnMethods = {"updateNotAllowed", "updateActions"})
177     public void localDelete(String testName) throws Exception {
178         super.delete(testName);
179     }
180
181     @Test(dataProvider = "testName",
182                 dependsOnMethods = {"CRUDTests"})
183     public void updateNotAllowed(String testName) throws Exception {
184
185         // Perform setup.
186         setupUpdate();
187
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,
195                     Permission.class));
196         }
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);
204         }
205         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
206                 invalidStatusCodeMessage(testRequestType, statusCode));
207         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
208
209     }
210
211     /**
212      * Update actions.
213      *
214      * @param testName the test name
215      * @throws Exception the exception
216      */
217     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
218                 dependsOnMethods = {"updateNotAllowed"})
219     public void updateActions(String testName) throws Exception {
220         // Perform setup.
221         setupUpdate();
222
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();
229         actions.remove(0);
230         actions.remove(0);
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);
235         }
236         permToUpdate.setAction(actions);
237         if (logger.isDebugEnabled()) {
238             logger.debug(testName + " updated object\n"
239                     + objectAsXmlString(permToUpdate, Permission.class));
240         }
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);
248         }
249         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
250                 invalidStatusCodeMessage(testRequestType, statusCode));
251         Assert.assertEquals(statusCode, testExpectedStatusCode);
252
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);
259         }
260         Assert.assertEquals(toUpdate_actions,
261                 updated_actions,
262                 "Data in updated object did not match submitted data.");
263     }
264     
265     /* (non-Javadoc)
266      * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
267      */
268     @Override
269 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
270 //      dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
271     public void updateNonExistent(String testName) throws Exception {
272         // Perform setup.
273         setupUpdateNonExistent();
274
275         // Submit the request to the service and store the response.
276         //
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",
283                 actions,
284                 EffectType.PERMIT,
285                 true,
286                 true,
287                 true);
288         ClientResponse<Permission> res =
289                 client.update(NON_EXISTENT_ID, permission);
290         int statusCode = res.getStatus();
291
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);
296         }
297         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
298                 invalidStatusCodeMessage(testRequestType, statusCode));
299         Assert.assertEquals(statusCode, testExpectedStatusCode);
300     }
301     
302     // ---------------------------------------------------------------
303     // Search tests
304     // ---------------------------------------------------------------
305     
306     @Override
307     public void searchWorkflowDeleted(String testName) throws Exception {
308         // Fixme: null test for now, overriding test in base class
309     }
310
311     // ---------------------------------------------------------------
312     // Utility methods used by tests above
313     // ---------------------------------------------------------------
314     /**
315      * create permission instance
316      * @param resourceName
317      * @param description
318      * @param actionList list of actions for this permission
319      * @param effect effect of the permission
320      * @param useResourceName
321      * @param useAction
322      * @param useEffect
323      * @return permission
324      */
325     public static Permission createPermissionInstance(String resourceName,
326             String description,
327             List<PermissionAction> actionList,
328             EffectType effect,
329             boolean useResourceName,
330             boolean useAction,
331             boolean useEffect) {
332
333         Permission permission = PermissionFactory.createPermissionInstance(resourceName,
334                 description, actionList, effect,
335                 useResourceName, useAction, useEffect);
336
337         if (logger.isDebugEnabled()) {
338             logger.debug("to be created, permission");
339             logger.debug(objectAsXmlString(permission, Permission.class));
340         }
341         return permission;
342     }
343
344     /**
345      * Prints the list.
346      *
347      * @param testName the test name
348      * @param list the list
349      * @return the int
350      */
351     @Override
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());
357         }
358     }
359
360         @Override
361         protected Permission createInstance(String commonPartName, String identifier) {
362         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
363         Permission permission = createPermissionInstance(identifier,
364                 "default permissions for " + identifier,
365                 actions,
366                 EffectType.PERMIT,
367                 true,
368                 true,
369                 true);
370         return permission;
371         }
372
373         @Override
374         protected Permission updateInstance(Permission original) {
375                 Permission result = new Permission();
376                 
377                 result.setCsid(original.getCsid());
378                 result.setResourceName(original.getResourceName());
379         // Update the content of this resource.
380                 result.setDescription("updated-" + original.getDescription());
381                 
382                 return result;
383         }
384
385         @Override
386         protected void compareUpdatedInstances(Permission original,
387                         Permission updated) throws Exception {
388         Assert.assertEquals(updated.getCsid(),
389                         original.getCsid(),
390                 "CSID in updated object did not match submitted data.");
391
392         Assert.assertEquals(updated.getResourceName(),
393                         original.getResourceName(),
394                 "Resource name in updated object did not match submitted data.");
395
396         Assert.assertEquals(updated.getDescription(),
397                         original.getDescription(),
398                 "Description in updated object did not match submitted data.");
399     }
400
401         @Override
402         protected Class<PermissionsList> getCommonListType() {
403                 return PermissionsList.class;
404         }
405
406     @Override
407     @Test(dataProvider = "testName",
408                 dependsOnMethods = {
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
412     }
413
414         @Override
415         public void updateWithEmptyEntityBody(String testName) throws Exception {
416                 // TODO Auto-generated method stub
417                 
418         }
419
420         @Override
421         public void updateWithMalformedXml(String testName) throws Exception {
422                 // TODO Auto-generated method stub
423                 
424         }
425
426         @Override
427         public void updateWithWrongXmlSchema(String testName) throws Exception {
428                 // TODO Auto-generated method stub
429                 
430         }
431 }