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