]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
24a1e5e8f00f8ab24858286dbb040d726f3cd4fc
[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.EffectType;
30
31 import org.collectionspace.services.client.PermissionClient;
32 import org.collectionspace.services.authorization.Permission;
33 import org.collectionspace.services.authorization.PermissionAction;
34 import org.collectionspace.services.authorization.PermissionsList;
35 import org.collectionspace.services.client.PermissionFactory;
36 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
37 import org.collectionspace.services.client.test.ServiceRequestType;
38 import org.jboss.resteasy.client.ClientResponse;
39
40 import org.testng.Assert;
41 import org.testng.annotations.Test;
42
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.testng.annotations.AfterClass;
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 {
55
56     static private final Logger logger =
57             LoggerFactory.getLogger(PermissionServiceTest.class);
58     // Instance variables specific to this test.
59     private String knownResourceId = null;
60     private List<String> allResourceIdsCreated = new ArrayList();
61     boolean addTenant = true;
62     /*
63      * This method is called only by the parent class, AbstractServiceTestImpl
64      */
65
66     @Override
67     protected String getServicePathComponent() {
68         return new PermissionClient().getServicePathComponent();
69     }
70
71     // ---------------------------------------------------------------
72     // CRUD tests : CREATE tests
73     // ---------------------------------------------------------------
74     // Success outcomes
75     @Override
76     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
77     public void create(String testName) throws Exception {
78
79         // Perform setup, such as initializing the type of service request
80         // (e.g. CREATE, DELETE), its valid and expected status codes, and
81         // its associated HTTP method name (e.g. POST, DELETE).
82         setupCreate(testName);
83
84         // Submit the request to the service and store the response.
85         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
86         Permission permission = createPermissionInstance("accounts",
87                 "default permissions for account",
88                 actions,
89                 EffectType.PERMIT,
90                 true,
91                 true,
92                 true);
93         PermissionClient client = new PermissionClient();
94         ClientResponse<Response> res = client.create(permission);
95         int statusCode = res.getStatus();
96
97         // Check the status code of the response: does it match
98         // the expected response(s)?
99         //
100         // Specifically:
101         // Does it fall within the set of valid status codes?
102         // Does it exactly match the expected status code?
103         if (logger.isDebugEnabled()) {
104             logger.debug(testName + ": status = " + statusCode);
105         }
106         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
107                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
108         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
109
110         // Store the ID returned from this create operation
111         // for additional tests below.
112         knownResourceId = extractId(res);
113         if (logger.isDebugEnabled()) {
114             logger.debug(testName + ": knownResourceId=" + knownResourceId);
115         }
116     }
117
118     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
119     dependsOnMethods = {"create"})
120     public void createWithoutResourceName(String testName) throws Exception {
121
122         setupCreate(testName);
123
124         // Submit the request to the service and store the response.
125         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
126         Permission permission = createPermissionInstance(null,
127                 "default permissions for account",
128                 actions,
129                 EffectType.PERMIT,
130                 false,
131                 true,
132                 true);
133         PermissionClient client = new PermissionClient();
134         ClientResponse<Response> res = client.create(permission);
135         int statusCode = res.getStatus();
136         // Does it exactly match the expected status code?
137         if (logger.isDebugEnabled()) {
138             logger.debug(testName + ": status = " + statusCode);
139         }
140         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
141                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
142         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
143     }
144
145     //to not cause uniqueness violation for permission, createList is removed
146     @Override
147     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
148     dependsOnMethods = {"create"})
149     public void createList(String testName) throws Exception {
150
151         setupCreate(testName);
152         // Submit the request to the service and store the response.
153         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
154         Permission permission1 = createPermissionInstance("collectionobjects",
155                 "default permissions for collectionobjects",
156                 actions,
157                 EffectType.PERMIT,
158                 true,
159                 true,
160                 true);
161         PermissionClient client = new PermissionClient();
162         ClientResponse<Response> res = client.create(permission1);
163         int statusCode = res.getStatus();
164         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
165                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
166         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
167         allResourceIdsCreated.add(extractId(res));
168
169         Permission permission2 = createPermissionInstance("acquisitions",
170                 "default permissions for acquisitions",
171                 actions,
172                 EffectType.PERMIT,
173                 true,
174                 true,
175                 true);
176         res = client.create(permission2);
177         statusCode = res.getStatus();
178         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
181         allResourceIdsCreated.add(extractId(res));
182
183         Permission permission3 = createPermissionInstance("ids",
184                 "default permissions for id service",
185                 actions,
186                 EffectType.PERMIT,
187                 true,
188                 true,
189                 true);
190         res = client.create(permission3);
191         statusCode = res.getStatus();
192         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
193                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
194         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
195         allResourceIdsCreated.add(extractId(res));
196     }
197
198     // Failure outcomes
199     // Placeholders until the three tests below can be uncommented.
200     // See Issue CSPACE-401.
201     @Override
202     public void createWithEmptyEntityBody(String testName) throws Exception {
203     }
204
205     @Override
206     public void createWithMalformedXml(String testName) throws Exception {
207     }
208
209     @Override
210     public void createWithWrongXmlSchema(String testName) throws Exception {
211     }
212
213     // ---------------------------------------------------------------
214     // CRUD tests : READ tests
215     // ---------------------------------------------------------------
216     // Success outcomes
217     @Override
218     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
219     dependsOnMethods = {"create"})
220     public void read(String testName) throws Exception {
221
222         // Perform setup.
223         setupRead(testName);
224
225         // Submit the request to the service and store the response.
226         PermissionClient client = new PermissionClient();
227         ClientResponse<Permission> res = client.read(knownResourceId);
228         int statusCode = res.getStatus();
229
230         // Check the status code of the response: does it match
231         // the expected response(s)?
232         if (logger.isDebugEnabled()) {
233             logger.debug(testName + ": status = " + statusCode);
234         }
235         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
236                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
237         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
238
239         Permission output = (Permission) res.getEntity();
240         Assert.assertNotNull(output);
241     }
242
243     // Failure outcomes
244     @Override
245     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
246     dependsOnMethods = {"read"})
247     public void readNonExistent(String testName) throws Exception {
248
249         // Perform setup.
250         setupReadNonExistent(testName);
251
252         // Submit the request to the service and store the response.
253         PermissionClient client = new PermissionClient();
254         ClientResponse<Permission> res = client.read(NON_EXISTENT_ID);
255         int statusCode = res.getStatus();
256
257         // Check the status code of the response: does it match
258         // the expected response(s)?
259         if (logger.isDebugEnabled()) {
260             logger.debug(testName + ": status = " + statusCode);
261         }
262         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
263                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
264         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
265     }
266
267     // ---------------------------------------------------------------
268     // CRUD tests : READ_LIST tests
269     // ---------------------------------------------------------------
270     // Success outcomes
271     @Override
272     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
273     dependsOnMethods = {"createList", "read"})
274     public void readList(String testName) throws Exception {
275
276         // Perform setup.
277         setupReadList(testName);
278
279         // Submit the request to the service and store the response.
280         PermissionClient client = new PermissionClient();
281         ClientResponse<PermissionsList> res = client.readList();
282         PermissionsList list = res.getEntity();
283         int statusCode = res.getStatus();
284
285         // Check the status code of the response: does it match
286         // the expected response(s)?
287         if (logger.isDebugEnabled()) {
288             logger.debug(testName + ": status = " + statusCode);
289         }
290         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
291                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
292         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
293
294         // Optionally output additional data about list members for debugging.
295         boolean iterateThroughList = true;
296         if (iterateThroughList && logger.isDebugEnabled()) {
297             printList(testName, list);
298         }
299     }
300
301     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
302     dependsOnMethods = {"createList", "read"})
303     public void searchResourceName(String testName) throws Exception {
304
305         // Perform setup.
306         setupReadList(testName);
307
308         // Submit the request to the service and store the response.
309         PermissionClient client = new PermissionClient();
310         ClientResponse<PermissionsList> res = client.readSearchList("acquisition");
311         PermissionsList list = res.getEntity();
312         int statusCode = res.getStatus();
313         // Check the status code of the response: does it match
314         // the expected response(s)?
315         if (logger.isDebugEnabled()) {
316             logger.debug(testName + ": status = " + statusCode);
317         }
318         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
319                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
320         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
321         int EXPECTED_ITEMS = 1;
322         if (logger.isDebugEnabled()) {
323             logger.debug(testName + ": received = " + list.getPermissions().size()
324                     + " expected=" + EXPECTED_ITEMS);
325         }
326         Assert.assertEquals(EXPECTED_ITEMS, list.getPermissions().size());
327         // Optionally output additional data about list members for debugging.
328         boolean iterateThroughList = true;
329         if (iterateThroughList && logger.isDebugEnabled()) {
330             printList(testName, list);
331         }
332     }
333
334     // Failure outcomes
335     // None at present.
336     // ---------------------------------------------------------------
337     // CRUD tests : UPDATE tests
338     // ---------------------------------------------------------------
339     // Success outcomes
340     @Override
341     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
342     dependsOnMethods = {"read", "readList", "readNonExistent"})
343     public void update(String testName) throws Exception {
344
345         // Perform setup.
346         setupUpdate(testName);
347
348         Permission permToUpdate = new Permission();
349         permToUpdate.setCsid(knownResourceId);
350         // Update the content of this resource.
351         permToUpdate.setResourceName("updated-resource");
352         if (logger.isDebugEnabled()) {
353             logger.debug("updated object");
354             logger.debug(objectAsXmlString(permToUpdate,
355                     Permission.class));
356         }
357         PermissionClient client = new PermissionClient();
358         // Submit the request to the service and store the response.
359         ClientResponse<Permission> res = client.update(knownResourceId, permToUpdate);
360         int statusCode = res.getStatus();
361         // Check the status code of the response: does it match the expected response(s)?
362         if (logger.isDebugEnabled()) {
363             logger.debug(testName + ": status = " + statusCode);
364         }
365         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
366                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
367         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
368
369
370         Permission permUpdated = (Permission) res.getEntity();
371         Assert.assertNotNull(permUpdated);
372
373         Assert.assertEquals(permUpdated.getResourceName(),
374                 permToUpdate.getResourceName(),
375                 "Data in updated object did not match submitted data.");
376     }
377
378     // Failure outcomes
379     // Placeholders until the three tests below can be uncommented.
380     // See Issue CSPACE-401.
381     @Override
382     public void updateWithEmptyEntityBody(String testName) throws Exception {
383     }
384
385     @Override
386     public void updateWithMalformedXml(String testName) throws Exception {
387     }
388
389     @Override
390     public void updateWithWrongXmlSchema(String testName) throws Exception {
391     }
392
393     @Override
394     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
395     dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
396     public void updateNonExistent(String testName) throws Exception {
397
398         // Perform setup.
399         setupUpdateNonExistent(testName);
400
401         // Submit the request to the service and store the response.
402         //
403         // Note: The ID used in this 'create' call may be arbitrary.
404         // The only relevant ID may be the one used in updatePermission(), below.
405         PermissionClient client = new PermissionClient();
406         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
407         Permission permission = createPermissionInstance("acquisitions",
408                 "default permissions for acquisitions",
409                 actions,
410                 EffectType.PERMIT,
411                 true,
412                 true,
413                 true);
414         ClientResponse<Permission> res =
415                 client.update(NON_EXISTENT_ID, permission);
416         int statusCode = res.getStatus();
417
418         // Check the status code of the response: does it match
419         // the expected response(s)?
420         if (logger.isDebugEnabled()) {
421             logger.debug(testName + ": status = " + statusCode);
422         }
423         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
424                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
425         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
426     }
427
428     // ---------------------------------------------------------------
429     // CRUD tests : DELETE tests
430     // ---------------------------------------------------------------
431     // Success outcomes
432     @Override
433     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
434     dependsOnMethods = {"update"})
435     public void delete(String testName) throws Exception {
436
437         // Perform setup.
438         setupDelete(testName);
439
440         // Submit the request to the service and store the response.
441         PermissionClient client = new PermissionClient();
442         ClientResponse<Response> res = client.delete(knownResourceId);
443         int statusCode = res.getStatus();
444
445         // Check the status code of the response: does it match
446         // the expected response(s)?
447         if (logger.isDebugEnabled()) {
448             logger.debug(testName + ": status = " + statusCode);
449         }
450         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
451                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
452         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
453     }
454
455     // Failure outcomes
456     @Override
457     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
458     dependsOnMethods = {"delete"})
459     public void deleteNonExistent(String testName) throws Exception {
460
461         // Perform setup.
462         setupDeleteNonExistent(testName);
463
464         // Submit the request to the service and store the response.
465         PermissionClient client = new PermissionClient();
466         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
467         int statusCode = res.getStatus();
468
469         // Check the status code of the response: does it match
470         // the expected response(s)?
471         if (logger.isDebugEnabled()) {
472             logger.debug(testName + ": status = " + statusCode);
473         }
474         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
475                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
476         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
477     }
478
479     // ---------------------------------------------------------------
480     // Utility tests : tests of code used in tests above
481     // ---------------------------------------------------------------
482     /**
483      * Tests the code for manually submitting data that is used by several
484      * of the methods above.
485      */
486     @Test(dependsOnMethods = {"create", "read"})
487     public void testSubmitRequest() throws Exception {
488
489         // Expected status code: 200 OK
490         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
491
492         // Submit the request to the service and store the response.
493         String method = ServiceRequestType.READ.httpMethodName();
494         String url = getResourceURL(knownResourceId);
495         int statusCode = submitRequest(method, url);
496
497         // Check the status code of the response: does it match
498         // the expected response(s)?
499         if (logger.isDebugEnabled()) {
500             logger.debug("testSubmitRequest: url=" + url
501                     + " status=" + statusCode);
502         }
503         Assert.assertEquals(statusCode, EXPECTED_STATUS);
504
505     }
506
507     // ---------------------------------------------------------------
508     // Utility methods used by tests above
509     // ---------------------------------------------------------------
510     /**
511      * create permission instance
512      * @param resourceName
513      * @param description
514      * @param actionList list of actions for this permission
515      * @param effect effect of the permission
516      * @param useResourceName
517      * @param useAction
518      * @param useEffect
519      * @return
520      */
521     public static Permission createPermissionInstance(String resourceName,
522             String description,
523             List<PermissionAction> actionList,
524             EffectType effect,
525             boolean useResourceName,
526             boolean useAction,
527             boolean useEffect) {
528
529         Permission permission = PermissionFactory.createPermissionInstance(resourceName,
530                 description, actionList, effect,
531                 useResourceName, useAction, useEffect);
532
533         if (logger.isDebugEnabled()) {
534             logger.debug("to be created, permission common");
535             logger.debug(objectAsXmlString(permission, Permission.class));
536         }
537         return permission;
538     }
539
540
541     @AfterClass(alwaysRun = true)
542     public void cleanUp() {
543         setupDelete("delete");
544         String noTest = System.getProperty("noTestCleanup");
545         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
546             if (logger.isDebugEnabled()) {
547                 logger.debug("Skipping Cleanup phase ...");
548             }
549             return;
550         }
551         if (logger.isDebugEnabled()) {
552             logger.debug("Cleaning up temporary resources created for testing ...");
553         }
554         PermissionClient client = new PermissionClient();
555         for (String resourceId : allResourceIdsCreated) {
556             ClientResponse<Response> res = client.delete(resourceId);
557             int statusCode = res.getStatus();
558             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
559                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
560             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
561         }
562     }
563
564     private int printList(String testName, PermissionsList list) {
565
566         int i = 0;
567
568         for (Permission permission : list.getPermissions()) {
569             logger.debug(testName + " permission csid=" + permission.getCsid()
570                     + " name=" + permission.getResourceName()
571                     + " desc=" + permission.getDescription());
572             i++;
573         }
574         return i;
575     }
576 }