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