]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3fae6b3cd0bca2e46b79853c3df2d6f45fd632a7
[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     // Failure outcomes
163     // Placeholders until the three tests below can be uncommented.
164     // See Issue CSPACE-401.
165     @Override
166     public void createWithEmptyEntityBody(String testName) throws Exception {
167     }
168
169     @Override
170     public void createWithMalformedXml(String testName) throws Exception {
171     }
172
173     @Override
174     public void createWithWrongXmlSchema(String testName) throws Exception {
175     }
176
177     // ---------------------------------------------------------------
178     // CRUD tests : READ tests
179     // ---------------------------------------------------------------
180     // Success outcomes
181     @Override
182     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
183     dependsOnMethods = {"create"})
184     public void read(String testName) throws Exception {
185
186         // Perform setup.
187         setupRead(testName);
188
189         // Submit the request to the service and store the response.
190         PermissionRoleClient client = new PermissionRoleClient();
191         ClientResponse<PermissionRole> res = client.read(
192                 permValues.get("accounts").getPermissionId(), "123");
193         int statusCode = res.getStatus();
194
195         // Check the status code of the response: does it match
196         // the expected response(s)?
197         if (logger.isDebugEnabled()) {
198             logger.debug(testName + ": status = " + statusCode);
199         }
200         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
201                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
202         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
203
204         PermissionRole output = (PermissionRole) res.getEntity();
205         Assert.assertNotNull(output);
206     }
207
208     // Failure outcomes
209     @Override
210     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
211     public void readNonExistent(String testName) throws Exception {
212
213         // Perform setup.
214         setupReadNonExistent(testName);
215
216         // Submit the request to the service and store the response.
217         PermissionRoleClient client = new PermissionRoleClient();
218         ClientResponse<PermissionRole> res = client.read(NON_EXISTENT_ID, "123");
219         int statusCode = res.getStatus();
220
221         // Check the status code of the response: does it match
222         // the expected response(s)?
223         if (logger.isDebugEnabled()) {
224             logger.debug(testName + ": status = " + statusCode);
225         }
226         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
227                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
228         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
229     }
230
231     // ---------------------------------------------------------------
232     // CRUD tests : READ_LIST tests
233     // ---------------------------------------------------------------
234     // Success outcomes
235     @Override
236     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
237     dependsOnMethods = {"createList", "read"})
238     public void readList(String testName) throws Exception {
239     }
240
241     // Failure outcomes
242     // None at present.
243     // ---------------------------------------------------------------
244     // CRUD tests : UPDATE tests
245     // ---------------------------------------------------------------
246     // Success outcomes
247     @Override
248     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
249     dependsOnMethods = {"read", "readList", "readNonExistent"})
250     public void update(String testName) throws Exception {
251     }
252
253     // Failure outcomes
254     // Placeholders until the three tests below can be uncommented.
255     // See Issue CSPACE-401.
256     @Override
257     public void updateWithEmptyEntityBody(String testName) throws Exception {
258     }
259
260     @Override
261     public void updateWithMalformedXml(String testName) throws Exception {
262     }
263
264     @Override
265     public void updateWithWrongXmlSchema(String testName) throws Exception {
266     }
267
268     @Override
269     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
270     dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
271     public void updateNonExistent(String testName) throws Exception {
272     }
273
274     // ---------------------------------------------------------------
275     // CRUD tests : DELETE tests
276     // ---------------------------------------------------------------
277     // Success outcomes
278     @Override
279     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
280     dependsOnMethods = {"read"})
281     public void delete(String testName) throws Exception {
282
283         // Perform setup.
284         setupDelete(testName);
285
286         // Submit the request to the service and store the response.
287         PermissionRoleClient client = new PermissionRoleClient();
288         ClientResponse<Response> res = client.delete(
289                 permValues.get("accounts").getPermissionId(), "123");
290         int statusCode = res.getStatus();
291
292         // Check the status code of the response: does it match
293         // the expected response(s)?
294         if (logger.isDebugEnabled()) {
295             logger.debug(testName + ": status = " + statusCode);
296         }
297         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
298                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
299         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
300     }
301
302     // Failure outcomes
303     @Override
304     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
305     public void deleteNonExistent(String testName) throws Exception {
306         //ignoring this test as the service side returns 200 now even if it does
307         //not find a record in the db
308     }
309
310     // ---------------------------------------------------------------
311     // Utility tests : tests of code used in tests above
312     // ---------------------------------------------------------------
313     /**
314      * Tests the code for manually submitting data that is used by several
315      * of the methods above.
316      */
317     @Test(dependsOnMethods = {"create"})
318     public void testSubmitRequest() throws Exception {
319
320         // Expected status code: 200 OK
321         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
322
323         // Submit the request to the service and store the response.
324         String method = ServiceRequestType.READ.httpMethodName();
325         String url = getResourceURL(permValues.get("accounts").getPermissionId());
326         int statusCode = submitRequest(method, url);
327
328         // Check the status code of the response: does it match
329         // the expected response(s)?
330         if (logger.isDebugEnabled()) {
331             logger.debug("testSubmitRequest: url=" + url
332                     + " status=" + statusCode);
333         }
334         Assert.assertEquals(statusCode, EXPECTED_STATUS);
335
336     }
337
338     // ---------------------------------------------------------------
339     // Utility methods used by tests above
340     // ---------------------------------------------------------------
341     /**
342      * create permRolerole instance
343      * @param permId
344      * @param roleValues array of role ids
345      * @param userPermId
346      * @param useRoleId
347      * @return
348      */
349     public static PermissionRole createPermissionRoleInstance(PermissionValue pv,
350             Collection<RoleValue> rvs,
351             boolean usePermId,
352             boolean useRoleId) {
353
354         PermissionRole permRole = new PermissionRole();
355         //service consume is not required to provide subject as it is determined
356         //from URI used
357 //        permRole.setSubject(SubjectType.ROLE);
358         if (usePermId) {
359             ArrayList<PermissionValue> pvs = new ArrayList<PermissionValue>();
360             pvs.add(pv);
361             permRole.setPermissions(pvs);
362         }
363         if (useRoleId) {
364             //FIXME is there a better way?
365             ArrayList<RoleValue> rvas = new ArrayList<RoleValue>();
366             for (RoleValue rv : rvs) {
367                 rvas.add(rv);
368             }
369             permRole.setRoles(rvas);
370         }
371
372         if (logger.isDebugEnabled()) {
373             logger.debug("to be created, permRole common");
374             logger.debug(objectAsXmlString(permRole, PermissionRole.class));
375         }
376         return permRole;
377     }
378
379     @AfterClass(alwaysRun = true)
380     public void cleanUp() {
381         setupDelete("delete");
382         String noTest = System.getProperty("noTestCleanup");
383         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
384             if (logger.isDebugEnabled()) {
385                 logger.debug("Skipping Cleanup phase ...");
386             }
387             return;
388         }
389         if (logger.isDebugEnabled()) {
390             logger.debug("Cleaning up temporary resources created for testing ...");
391         }
392         PermissionRoleClient client = new PermissionRoleClient();
393         for (String resourceId : allResourceIdsCreated) {
394
395             ClientResponse<Response> res = client.delete(resourceId, "123");
396             int statusCode = res.getStatus();
397             if (logger.isDebugEnabled()) {
398                 logger.debug("cleanup: delete relationships for permission id="
399                         + resourceId + " status=" + statusCode);
400             }
401             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
402                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
403             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
404         }
405
406         for (PermissionValue pv : permValues.values()) {
407             deletePermission(pv.getPermissionId());
408         }
409
410         for (RoleValue rv : roleValues.values()) {
411             deleteRole(rv.getRoleId());
412         }
413     }
414
415     private String createPermission(String resName, EffectType effect) {
416         setupCreate();
417         PermissionClient permClient = new PermissionClient();
418         List<PermissionAction> actions = PermissionFactory.createDefaultActions();
419         Permission permission = PermissionServiceTest.createPermissionInstance(resName,
420                 "default permissions for " + resName,
421                 actions, effect, true, true, true);
422         ClientResponse<Response> res = permClient.create(permission);
423         int statusCode = res.getStatus();
424         if (logger.isDebugEnabled()) {
425             logger.debug("createPermission: resName=" + resName
426                     + " status = " + statusCode);
427         }
428         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
429                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
430         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
431         return extractId(res);
432     }
433
434     private void deletePermission(String permId) {
435         setupDelete();
436         PermissionClient permClient = new PermissionClient();
437         ClientResponse<Response> res = permClient.delete(permId);
438         int statusCode = res.getStatus();
439         if (logger.isDebugEnabled()) {
440             logger.debug("deletePermission: delete permission id="
441                     + permId + " status=" + statusCode);
442         }
443         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
444                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
445         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
446     }
447
448     private String createRole(String roleName) {
449         setupCreate();
450         RoleClient roleClient = new RoleClient();
451
452         Role role = RoleFactory.createRoleInstance(roleName,
453                 "role for " + roleName, true);
454         ClientResponse<Response> res = roleClient.create(role);
455         int statusCode = res.getStatus();
456         if (logger.isDebugEnabled()) {
457             logger.debug("createRole: name=" + roleName
458                     + " status = " + statusCode);
459         }
460         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
461                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
462         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
463         return extractId(res);
464     }
465
466     private void deleteRole(String roleId) {
467         setupDelete();
468         RoleClient roleClient = new RoleClient();
469         ClientResponse<Response> res = roleClient.delete(roleId);
470         int statusCode = res.getStatus();
471         if (logger.isDebugEnabled()) {
472             logger.debug("deleteRole: delete role id=" + roleId
473                     + " status=" + statusCode);
474         }
475         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
476                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
477         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
478     }
479 }