]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a8d4a224a1cf8f8efe4f519cb4cae0b6d3d9385e
[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         // Retrieve the contents of a resource to update.
349         PermissionClient client = new PermissionClient();
350         ClientResponse<Permission> res =
351                 client.read(knownResourceId);
352         if (logger.isDebugEnabled()) {
353             logger.debug(testName + ": read status = " + res.getStatus());
354         }
355         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
356
357         if (logger.isDebugEnabled()) {
358             logger.debug("got object to update with ID: " + knownResourceId);
359         }
360         Permission toUpdatePermission =
361                 (Permission) res.getEntity();
362         Assert.assertNotNull(toUpdatePermission);
363
364         // Update the content of this resource.
365         toUpdatePermission.setResourceName("updated-" + toUpdatePermission.getResourceName());
366         if (logger.isDebugEnabled()) {
367             logger.debug("updated object");
368             logger.debug(objectAsXmlString(toUpdatePermission,
369                     Permission.class));
370         }
371
372         // Submit the request to the service and store the response.
373         res = client.update(knownResourceId, toUpdatePermission);
374         int statusCode = res.getStatus();
375         // Check the status code of the response: does it match the expected response(s)?
376         if (logger.isDebugEnabled()) {
377             logger.debug(testName + ": status = " + statusCode);
378         }
379         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
380                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
381         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
382
383
384         Permission updatedPermission = (Permission) res.getEntity();
385         Assert.assertNotNull(updatedPermission);
386
387         Assert.assertEquals(updatedPermission.getResourceName(),
388                 toUpdatePermission.getResourceName(),
389                 "Data in updated object did not match submitted data.");
390     }
391
392     // Failure outcomes
393     // Placeholders until the three tests below can be uncommented.
394     // See Issue CSPACE-401.
395     @Override
396     public void updateWithEmptyEntityBody(String testName) throws Exception {
397     }
398
399     @Override
400     public void updateWithMalformedXml(String testName) throws Exception {
401     }
402
403     @Override
404     public void updateWithWrongXmlSchema(String testName) throws Exception {
405     }
406
407     @Override
408     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
409     dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
410     public void updateNonExistent(String testName) throws Exception {
411
412         // Perform setup.
413         setupUpdateNonExistent(testName);
414
415         // Submit the request to the service and store the response.
416         //
417         // Note: The ID used in this 'create' call may be arbitrary.
418         // The only relevant ID may be the one used in updatePermission(), below.
419         PermissionClient client = new PermissionClient();
420         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
421         Permission permission = createPermissionInstance("acquisitions",
422                 "default permissions for acquisitions",
423                 actions,
424                 EffectType.PERMIT,
425                 true,
426                 true,
427                 true);
428         ClientResponse<Permission> res =
429                 client.update(NON_EXISTENT_ID, permission);
430         int statusCode = res.getStatus();
431
432         // Check the status code of the response: does it match
433         // the expected response(s)?
434         if (logger.isDebugEnabled()) {
435             logger.debug(testName + ": status = " + statusCode);
436         }
437         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
438                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
439         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
440     }
441
442     // ---------------------------------------------------------------
443     // CRUD tests : DELETE tests
444     // ---------------------------------------------------------------
445     // Success outcomes
446     @Override
447     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
448     dependsOnMethods = {"update"})
449     public void delete(String testName) throws Exception {
450
451         // Perform setup.
452         setupDelete(testName);
453
454         // Submit the request to the service and store the response.
455         PermissionClient client = new PermissionClient();
456         ClientResponse<Response> res = client.delete(knownResourceId);
457         int statusCode = res.getStatus();
458
459         // Check the status code of the response: does it match
460         // the expected response(s)?
461         if (logger.isDebugEnabled()) {
462             logger.debug(testName + ": status = " + statusCode);
463         }
464         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
465                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
466         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
467     }
468
469     // Failure outcomes
470     @Override
471     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
472     dependsOnMethods = {"delete"})
473     public void deleteNonExistent(String testName) throws Exception {
474
475         // Perform setup.
476         setupDeleteNonExistent(testName);
477
478         // Submit the request to the service and store the response.
479         PermissionClient client = new PermissionClient();
480         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
481         int statusCode = res.getStatus();
482
483         // Check the status code of the response: does it match
484         // the expected response(s)?
485         if (logger.isDebugEnabled()) {
486             logger.debug(testName + ": status = " + statusCode);
487         }
488         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
489                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
490         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
491     }
492
493     // ---------------------------------------------------------------
494     // Utility tests : tests of code used in tests above
495     // ---------------------------------------------------------------
496     /**
497      * Tests the code for manually submitting data that is used by several
498      * of the methods above.
499      */
500     @Test(dependsOnMethods = {"create", "read"})
501     public void testSubmitRequest() throws Exception {
502
503         // Expected status code: 200 OK
504         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
505
506         // Submit the request to the service and store the response.
507         String method = ServiceRequestType.READ.httpMethodName();
508         String url = getResourceURL(knownResourceId);
509         int statusCode = submitRequest(method, url);
510
511         // Check the status code of the response: does it match
512         // the expected response(s)?
513         if (logger.isDebugEnabled()) {
514             logger.debug("testSubmitRequest: url=" + url
515                     + " status=" + statusCode);
516         }
517         Assert.assertEquals(statusCode, EXPECTED_STATUS);
518
519     }
520
521     // ---------------------------------------------------------------
522     // Utility methods used by tests above
523     // ---------------------------------------------------------------
524     /**
525      * create permission instance
526      * @param resourceName
527      * @param description
528      * @param actionList list of actions for this permission
529      * @param effect effect of the permission
530      * @param useResourceName
531      * @param useAction
532      * @param useEffect
533      * @return
534      */
535     public static Permission createPermissionInstance(String resourceName,
536             String description,
537             List<PermissionAction> actionList,
538             EffectType effect,
539             boolean useResourceName,
540             boolean useAction,
541             boolean useEffect) {
542
543         Permission permission = PermissionFactory.createPermissionInstance(resourceName,
544                 description, actionList, effect,
545                 useResourceName, useAction, useEffect);
546
547         if (logger.isDebugEnabled()) {
548             logger.debug("to be created, permission common");
549             logger.debug(objectAsXmlString(permission, Permission.class));
550         }
551         return permission;
552     }
553
554
555     @AfterClass(alwaysRun = true)
556     public void cleanUp() {
557         setupDelete("delete");
558         String noTest = System.getProperty("noTestCleanup");
559         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
560             if (logger.isDebugEnabled()) {
561                 logger.debug("Skipping Cleanup phase ...");
562             }
563             return;
564         }
565         if (logger.isDebugEnabled()) {
566             logger.debug("Cleaning up temporary resources created for testing ...");
567         }
568         PermissionClient client = new PermissionClient();
569         for (String resourceId : allResourceIdsCreated) {
570             ClientResponse<Response> res = client.delete(resourceId);
571             int statusCode = res.getStatus();
572             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
573                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
574             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
575         }
576     }
577
578     private int printList(String testName, PermissionsList list) {
579
580         int i = 0;
581
582         for (Permission permission : list.getPermissions()) {
583             logger.debug(testName + " permission csid=" + permission.getCsid()
584                     + " name=" + permission.getResourceName()
585                     + " desc=" + permission.getDescription());
586             i++;
587         }
588         return i;
589     }
590 }