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