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