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