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