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