]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
acd39752b7d6b446a758176c53c36f19a334f296
[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 permRoles 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.Collection;
27 import java.util.Hashtable;
28 import java.util.List;
29 import javax.ws.rs.core.Response;
30 import org.collectionspace.services.authorization.EffectType;
31
32 import org.collectionspace.services.authorization.Permission;
33 import org.collectionspace.services.authorization.PermissionAction;
34 import org.collectionspace.services.authorization.PermissionRole;
35 import org.collectionspace.services.authorization.PermissionValue;
36 import org.collectionspace.services.authorization.Role;
37 import org.collectionspace.services.authorization.RoleValue;
38 import org.collectionspace.services.client.PermissionClient;
39 import org.collectionspace.services.client.PermissionRoleClient;
40 import org.collectionspace.services.client.RoleClient;
41 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
42 import org.collectionspace.services.client.test.ServiceRequestType;
43 import org.jboss.resteasy.client.ClientResponse;
44
45 import org.testng.Assert;
46 import org.testng.annotations.Test;
47
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.BeforeClass;
52
53 /**
54  * PermissionServiceTest, carries out tests against a
55  * deployed and running Permission, Role and PermissionRole Services.
56  * 
57  * $LastChangedRevision: 917 $
58  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
59  */
60 public class PermissionRoleServiceTest extends AbstractServiceTestImpl {
61
62     private final Logger logger =
63             LoggerFactory.getLogger(PermissionRoleServiceTest.class);
64     // Instance variables specific to this test.
65     private String knownResourceId = null;
66     private List<String> allResourceIdsCreated = new ArrayList();
67     private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
68     private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
69     /*
70      * This method is called only by the parent class, AbstractServiceTestImpl
71      */
72
73     @Override
74     protected String getServicePathComponent() {
75         return new PermissionRoleClient().getServicePathComponent();
76     }
77
78     @BeforeClass(alwaysRun = true)
79     public void seedData() {
80         String ra = "accounts";
81         String accPermId = createPermission(ra, EffectType.PERMIT);
82         PermissionValue pva = new PermissionValue();
83         pva.setResourceName(ra);
84         pva.setPermissionId(accPermId);
85         permValues.put(pva.getResourceName(), pva);
86
87         String rc = "collectionobjects";
88         String coPermId = createPermission(rc, EffectType.DENY);
89         PermissionValue pvc = new PermissionValue();
90         pvc.setResourceName(rc);
91         pvc.setPermissionId(coPermId);
92         permValues.put(pvc.getResourceName(), pvc);
93
94         String ri = "intakes";
95         String iPermId = createPermission(ri, EffectType.DENY);
96         PermissionValue pvi = new PermissionValue();
97         pvi.setResourceName(ri);
98         pvi.setPermissionId(iPermId);
99         permValues.put(pvi.getResourceName(), pvi);
100
101         String rn1 = "ROLE_CO1";
102         String r1RoleId = createRole(rn1);
103         RoleValue rv1 = new RoleValue();
104         rv1.setRoleId(r1RoleId);
105         rv1.setRoleName(rn1);
106         roleValues.put(rv1.getRoleName(), rv1);
107
108         String rn2 = "ROLE_CO2";
109         String r2RoleId = createRole(rn2);
110         RoleValue rv2 = new RoleValue();
111         rv2.setRoleId(r2RoleId);
112         rv2.setRoleName(rn2);
113         roleValues.put(rv2.getRoleName(), rv2);
114     }
115
116     // ---------------------------------------------------------------
117     // CRUD tests : CREATE tests
118     // ---------------------------------------------------------------
119     // Success outcomes
120     @Override
121     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
122     public void create(String testName) throws Exception {
123
124         // Perform setup, such as initializing the type of service request
125         // (e.g. CREATE, DELETE), its valid and expected status codes, and
126         // its associated HTTP method name (e.g. POST, DELETE).
127         setupCreate(testName);
128
129         // Submit the request to the service and store the response.
130         PermissionValue pv = permValues.get("accounts");
131         PermissionRole permRole = createPermissionRoleInstance(pv,
132                 roleValues.values(), true, true);
133         PermissionRoleClient client = new PermissionRoleClient();
134         ClientResponse<Response> res = client.create(pv.getPermissionId(), permRole);
135         int statusCode = res.getStatus();
136
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, EXPECTED_STATUS_CODE);
143
144         // Store the ID returned from this create operation
145         // for additional tests below.
146         //this is is not important in case of this relationship
147         knownResourceId = extractId(res);
148         if (logger.isDebugEnabled()) {
149             logger.debug(testName + ": knownResourceId=" + knownResourceId);
150         }
151     }
152
153     //to not cause uniqueness violation for permRole, createList is removed
154     @Override
155     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
156     dependsOnMethods = {"create"})
157     public void createList(String testName) throws Exception {
158
159         setupCreate(testName);
160         // Submit the request to the service and store the response.
161         PermissionRoleClient client = new PermissionRoleClient();
162         PermissionValue pv = permValues.get("collectionobjects");
163         PermissionRole permRole = createPermissionRoleInstance(pv,
164                 roleValues.values(), true, true);
165         ClientResponse<Response> res = client.create(pv.getPermissionId(), permRole);
166         int statusCode = res.getStatus();
167         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
168                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
169         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
170         //id of relationship is not important
171         allResourceIdsCreated.add(pv.getPermissionId());
172
173         PermissionValue pv2 = permValues.get("intakes");
174         PermissionRole permRole2 = createPermissionRoleInstance(pv2,
175                 roleValues.values(), true, true);
176         res = client.create(pv2.getPermissionId(), permRole2);
177         statusCode = res.getStatus();
178         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
181         //id of relationship is not important
182         allResourceIdsCreated.add(pv2.getPermissionId());
183
184     }
185
186     // Failure outcomes
187     // Placeholders until the three tests below can be uncommented.
188     // See Issue CSPACE-401.
189     @Override
190     public void createWithEmptyEntityBody(String testName) throws Exception {
191     }
192
193     @Override
194     public void createWithMalformedXml(String testName) throws Exception {
195     }
196
197     @Override
198     public void createWithWrongXmlSchema(String testName) throws Exception {
199     }
200
201     // ---------------------------------------------------------------
202     // CRUD tests : READ tests
203     // ---------------------------------------------------------------
204     // Success outcomes
205     @Override
206     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
207     dependsOnMethods = {"create"})
208     public void read(String testName) throws Exception {
209
210         // Perform setup.
211         setupRead(testName);
212
213         // Submit the request to the service and store the response.
214         PermissionRoleClient client = new PermissionRoleClient();
215         ClientResponse<PermissionRole> res = client.read(
216                 permValues.get("accounts").getPermissionId(), "123");
217         int statusCode = res.getStatus();
218
219         // Check the status code of the response: does it match
220         // the expected response(s)?
221         if (logger.isDebugEnabled()) {
222             logger.debug(testName + ": status = " + statusCode);
223         }
224         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
225                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
226         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
227
228         PermissionRole output = (PermissionRole) res.getEntity();
229         Assert.assertNotNull(output);
230     }
231
232     // Failure outcomes
233     @Override
234     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
235     public void readNonExistent(String testName) throws Exception {
236
237         // Perform setup.
238         setupReadNonExistent(testName);
239
240         // Submit the request to the service and store the response.
241         PermissionRoleClient client = new PermissionRoleClient();
242         ClientResponse<PermissionRole> res = client.read(NON_EXISTENT_ID, "123");
243         int statusCode = res.getStatus();
244
245         // Check the status code of the response: does it match
246         // the expected response(s)?
247         if (logger.isDebugEnabled()) {
248             logger.debug(testName + ": status = " + statusCode);
249         }
250         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
251                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
252         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
253     }
254
255     // ---------------------------------------------------------------
256     // CRUD tests : READ_LIST tests
257     // ---------------------------------------------------------------
258     // Success outcomes
259     @Override
260     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
261     dependsOnMethods = {"createList", "read"})
262     public void readList(String testName) throws Exception {
263     }
264
265     // Failure outcomes
266     // None at present.
267     // ---------------------------------------------------------------
268     // CRUD tests : UPDATE tests
269     // ---------------------------------------------------------------
270     // Success outcomes
271     @Override
272     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
273     dependsOnMethods = {"read", "readList", "readNonExistent"})
274     public void update(String testName) throws Exception {
275     }
276
277     // Failure outcomes
278     // Placeholders until the three tests below can be uncommented.
279     // See Issue CSPACE-401.
280     @Override
281     public void updateWithEmptyEntityBody(String testName) throws Exception {
282     }
283
284     @Override
285     public void updateWithMalformedXml(String testName) throws Exception {
286     }
287
288     @Override
289     public void updateWithWrongXmlSchema(String testName) throws Exception {
290     }
291
292     @Override
293     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
294     dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
295     public void updateNonExistent(String testName) throws Exception {
296     }
297
298     // ---------------------------------------------------------------
299     // CRUD tests : DELETE tests
300     // ---------------------------------------------------------------
301     // Success outcomes
302     @Override
303     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
304     dependsOnMethods = {"read"})
305     public void delete(String testName) throws Exception {
306
307         // Perform setup.
308         setupDelete(testName);
309
310         // Submit the request to the service and store the response.
311         PermissionRoleClient client = new PermissionRoleClient();
312         ClientResponse<Response> res = client.delete(
313                 permValues.get("accounts").getPermissionId(), "123");
314         int statusCode = res.getStatus();
315
316         // Check the status code of the response: does it match
317         // the expected response(s)?
318         if (logger.isDebugEnabled()) {
319             logger.debug(testName + ": status = " + statusCode);
320         }
321         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
322                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
323         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
324     }
325
326     // Failure outcomes
327     @Override
328     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
329     public void deleteNonExistent(String testName) throws Exception {
330
331         // Perform setup.
332         setupDeleteNonExistent(testName);
333
334         // Submit the request to the service and store the response.
335         PermissionRoleClient client = new PermissionRoleClient();
336         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID, "123");
337         int statusCode = res.getStatus();
338
339         // Check the status code of the response: does it match
340         // the expected response(s)?
341         if (logger.isDebugEnabled()) {
342             logger.debug(testName + ": status = " + statusCode);
343         }
344         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
345                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
346         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
347     }
348
349     // ---------------------------------------------------------------
350     // Utility tests : tests of code used in tests above
351     // ---------------------------------------------------------------
352     /**
353      * Tests the code for manually submitting data that is used by several
354      * of the methods above.
355      */
356     @Test(dependsOnMethods = {"create"})
357     public void testSubmitRequest() throws Exception {
358
359         // Expected status code: 200 OK
360         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
361
362         // Submit the request to the service and store the response.
363         String method = ServiceRequestType.READ.httpMethodName();
364         String url = getResourceURL(permValues.get("accounts").getPermissionId());
365         int statusCode = submitRequest(method, url);
366
367         // Check the status code of the response: does it match
368         // the expected response(s)?
369         if (logger.isDebugEnabled()) {
370             logger.debug("testSubmitRequest: url=" + url
371                     + " status=" + statusCode);
372         }
373         Assert.assertEquals(statusCode, EXPECTED_STATUS);
374
375     }
376
377     // ---------------------------------------------------------------
378     // Utility methods used by tests above
379     // ---------------------------------------------------------------
380     /**
381      * create permRolerole instance
382      * @param permId
383      * @param roleValues array of role ids
384      * @param userPermId
385      * @param useRoleId
386      * @return
387      */
388     private PermissionRole createPermissionRoleInstance(PermissionValue pv,
389             Collection<RoleValue> rvs,
390             boolean usePermId,
391             boolean useRoleId) {
392
393         PermissionRole permRole = new PermissionRole();
394         //service consume is not required to provide subject as it is determined
395         //from URI used
396 //        permRole.setSubject(SubjectType.ROLE);
397         if (usePermId) {
398             ArrayList<PermissionValue> pvs = new ArrayList<PermissionValue>();
399             pvs.add(pv);
400             permRole.setPermissions(pvs);
401         }
402         if (useRoleId) {
403             //FIXME is there a better way?
404             ArrayList<RoleValue> rvas = new ArrayList<RoleValue>();
405             for (RoleValue rv : rvs) {
406                 rvas.add(rv);
407             }
408             permRole.setRoles(rvas);
409         }
410
411         if (logger.isDebugEnabled()) {
412             logger.debug("to be created, permRole common");
413             logger.debug(objectAsXmlString(permRole, PermissionRole.class));
414         }
415         return permRole;
416     }
417
418     @AfterClass(alwaysRun = true)
419     public void cleanUp() {
420         setupDelete("delete");
421         if (logger.isDebugEnabled()) {
422             logger.debug("clenaup: Cleaning up temporary resources created for testing ...");
423         }
424         PermissionRoleClient client = new PermissionRoleClient();
425         for (String resourceId : allResourceIdsCreated) {
426
427             // Note: Any non-success responses are ignored and not reported.
428             ClientResponse<Response> res = client.delete(resourceId, "123");
429             int statusCode = res.getStatus();
430             if (logger.isDebugEnabled()) {
431                 logger.debug("clenaup: delete relationships for permission id="
432                         + resourceId + " status=" + statusCode);
433             }
434             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
435                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
436             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
437         }
438
439         for (PermissionValue pv : permValues.values()) {
440             deletePermission(pv.getPermissionId());
441         }
442
443         for (RoleValue rv : roleValues.values()) {
444             deleteRole(rv.getRoleId());
445         }
446     }
447
448     private String createPermission(String resName, EffectType effect) {
449         setupCreate();
450         PermissionClient permClient = new PermissionClient();
451         List<PermissionAction> actions = PermissionServiceTest.getDefaultActions();
452         Permission permission = PermissionServiceTest.createPermissionInstance(resName,
453                 "default permissions for " + resName,
454                 actions, EffectType.PERMIT, true, true, true);
455         ClientResponse<Response> res = permClient.create(permission);
456         int statusCode = res.getStatus();
457         if (logger.isDebugEnabled()) {
458             logger.debug("createPermission: resName=" + resName +
459                     " status = " + statusCode);
460         }
461         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
462                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
463         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
464         return extractId(res);
465     }
466
467     private void deletePermission(String permId) {
468         setupDelete();
469         PermissionClient permClient = new PermissionClient();
470         ClientResponse<Response> res = permClient.delete(permId);
471         int statusCode = res.getStatus();
472         if (logger.isDebugEnabled()) {
473             logger.debug("deletePermission: delete permission id="
474                     + permId + " status=" + statusCode);
475         }
476         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
477                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
478         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
479     }
480
481     private String createRole(String roleName) {
482         setupCreate();
483         RoleClient roleClient = new RoleClient();
484
485         Role role = RoleServiceTest.createRoleInstance(roleName,
486                 "role for " + roleName, true);
487         ClientResponse<Response> res = roleClient.create(role);
488         int statusCode = res.getStatus();
489         if (logger.isDebugEnabled()) {
490             logger.debug("createRole: name=" + roleName +
491                     " status = " + statusCode);
492         }
493         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
494                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
495         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
496         return extractId(res);
497     }
498
499     private void deleteRole(String roleId) {
500         setupDelete();
501         RoleClient roleClient = new RoleClient();
502         ClientResponse<Response> res = roleClient.delete(roleId);
503         int statusCode = res.getStatus();
504                 if (logger.isDebugEnabled()) {
505             logger.debug("deleteRole: delete role id=" + roleId +
506                     " status=" + statusCode);
507         }
508         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
509                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
510         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
511     }
512 }