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