]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f2f02f90a7fbdd18457b89a409063a3fe7c58b89
[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 2010 University of California at Berkeley
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
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 /*
25  * To change this template, choose Tools | Templates
26  * and open the template in the editor.
27  */
28 package org.collectionspace.services.security.client.test;
29
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Date;
33 import java.util.Hashtable;
34 import java.util.List;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37 import org.collectionspace.services.account.AccountsCommon;
38 import org.collectionspace.services.authorization.AccountRole;
39 import org.collectionspace.services.authorization.AccountValue;
40 import org.collectionspace.services.authorization.ActionType;
41 import org.collectionspace.services.authorization.EffectType;
42
43 import org.collectionspace.services.authorization.Permission;
44 import org.collectionspace.services.authorization.PermissionAction;
45 import org.collectionspace.services.authorization.PermissionRole;
46 import org.collectionspace.services.authorization.PermissionValue;
47 import org.collectionspace.services.authorization.Role;
48 import org.collectionspace.services.authorization.RoleValue;
49 import org.collectionspace.services.client.AccountClient;
50 import org.collectionspace.services.client.AccountFactory;
51 import org.collectionspace.services.client.AccountRoleClient;
52 import org.collectionspace.services.client.AccountRoleFactory;
53 import org.collectionspace.services.client.CollectionSpaceClient;
54 import org.collectionspace.services.client.DimensionClient;
55 import org.collectionspace.services.client.DimensionFactory;
56 import org.collectionspace.services.client.PayloadOutputPart;
57 import org.collectionspace.services.client.PermissionClient;
58 import org.collectionspace.services.client.PermissionFactory;
59 import org.collectionspace.services.client.PermissionRoleClient;
60 import org.collectionspace.services.client.PermissionRoleFactory;
61 import org.collectionspace.services.client.PoxPayloadIn;
62 import org.collectionspace.services.client.PoxPayloadOut;
63 import org.collectionspace.services.client.RoleClient;
64 import org.collectionspace.services.client.RoleFactory;
65 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
66 import org.collectionspace.services.dimension.DimensionsCommon;
67 import org.collectionspace.services.jaxb.AbstractCommonList;
68 import org.jboss.resteasy.client.ClientResponse;
69 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
70 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
71 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
72
73 import org.testng.Assert;
74 import org.testng.annotations.Test;
75
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78 import org.testng.annotations.AfterClass;
79 import org.testng.annotations.BeforeClass;
80
81 /**
82  * AuthorizationServiceTest, carries out tests against a
83  * deployed and running Permission, Role, AccountRole, PermissionRole and
84  * CollectionObject Services.
85  *
86  * Pre-requisite : authorization-mgt/client tests seed some permissions used
87  * by this test
88  *
89  * $LastChangedRevision: 917 $
90  * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
91  */
92 public class MultiTenancyTest extends AbstractServiceTestImpl {
93
94     private final String CLASS_NAME = MultiTenancyTest.class.getName();
95     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
96     private final static String TENANT_1_ADMIN_USER = "admin@core.collectionspace.org";
97     private final static String TENANT_2_ADMIN_USER = "admin@lifesci.collectionspace.org";
98     private final static String TENANT_ADMIN_PASS = "Administrator";
99     private final static String TENANT_1_USER = "user1@museum1.org";
100     private final static String TENANT_2_USER = "user2@museum2.org";
101     private final static String TENANT_1 = "1";
102     private final static String TENANT_2 = "2";
103     private final static String TEST_ROLE_NAME = "ROLE_TEST_REGISTRAR";
104     private final static String TEST_SERVICE_A = "dimensions";
105
106     private static class UserInfo {
107
108         String userName;
109         String password;
110
111         UserInfo(String u, String p) {
112             userName = u;
113             password = p;
114         }
115     }
116     // Instance variables specific to this test.
117     private String TENANT_RESOURCE_1 = null;
118     private String TENANT_RESOURCE_2 = null;
119     private List<String> allResourceIdsCreated = new ArrayList();
120     //key for userAccounts is userId
121     private Hashtable<String, AccountValue> userAccounts = new Hashtable<String, AccountValue>();
122     //key for permValues is id as there could be several perms for the same resource
123     private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
124     //key for all tenantXXX tables is tenant id, expecting only one entity per tenant for this test
125     private Hashtable<String, UserInfo> tenantAdminUsers = new Hashtable<String, UserInfo>();
126     private Hashtable<String, AccountValue> tenantAccounts = new Hashtable<String, AccountValue>();
127     private Hashtable<String, RoleValue> tenantRoles = new Hashtable<String, RoleValue>();
128     private Hashtable<String, Role> tenantAdminRoles = new Hashtable<String, Role>();
129     private Hashtable<String, PermissionValue> tenantPermissions = new Hashtable<String, PermissionValue>();
130
131 //    private String permId1;
132 //    private String permId2;
133
134     /*
135      * This method is called only by the parent class, AbstractServiceTestImpl
136      */
137     @Override
138     protected String getServicePathComponent() {
139         return null;
140     }
141
142     @BeforeClass(alwaysRun = true)
143     public void seedData() {
144         if (logger.isDebugEnabled()) {
145             testBanner("seedData", CLASS_NAME);
146         }
147
148         //tenant admin users are used to create accounts, roles and permissions and relationships
149         //assumption : two tenant admin users exist before running this test
150         tenantAdminUsers.put(TENANT_1, new UserInfo(TENANT_1_ADMIN_USER, TENANT_ADMIN_PASS));
151         tenantAdminUsers.put(TENANT_2, new UserInfo(TENANT_2_ADMIN_USER, TENANT_ADMIN_PASS));
152
153         seedAccounts();
154         seedPermissions();
155         seedRoles();
156         seedAccountRoles();
157         seedPermissionRoles();
158     }
159
160     private void seedAccounts() {
161         seedAccount(TENANT_1, TENANT_1_USER);
162         seedAccount(TENANT_2, TENANT_2_USER);
163     }
164
165     private void seedAccount(String tenantId, String userId) {
166         //create account using default user in admin role but assign tenant id
167         //create username, email and password same for simplicity
168         String accId = createAccount(tenantId, userId, userId);
169         AccountValue ava = new AccountValue();
170         ava.setScreenName(userId);
171         ava.setUserId(userId);
172         ava.setAccountId(accId);
173         userAccounts.put(ava.getUserId(), ava);
174         tenantAccounts.put(tenantId, ava);
175         if (logger.isDebugEnabled()) {
176             logger.debug("seedAccount tenantId=" + tenantId + " userId=" + userId);
177         }
178     }
179
180     private void seedPermissions() {
181         String resource = TEST_SERVICE_A;
182
183         PermissionAction pac = new PermissionAction();
184         pac.setName(ActionType.CREATE);
185         PermissionAction par = new PermissionAction();
186         par.setName(ActionType.READ);
187         PermissionAction pau = new PermissionAction();
188         pau.setName(ActionType.UPDATE);
189         PermissionAction pad = new PermissionAction();
190         pad.setName(ActionType.DELETE);
191
192         //both users can create, read and update and delete
193         List<PermissionAction> testActions = new ArrayList<PermissionAction>();
194         testActions.add(pac);
195         testActions.add(par);
196         testActions.add(pau);
197         testActions.add(pad);
198
199         seedPermission(TENANT_1, resource, testActions, EffectType.PERMIT);
200         seedPermission(TENANT_2, resource, testActions, EffectType.PERMIT);
201     }
202
203     private void seedPermission(String tenantId,
204             String resource, List<PermissionAction> testActions, EffectType effect) {
205         //create permission using default user in admin role but assign tenant id
206         String id = createPermission(tenantId, resource, testActions, effect);
207         PermissionValue pv = new PermissionValue();
208         pv.setResourceName(resource);
209         pv.setPermissionId(id);
210         permValues.put(pv.getPermissionId(), pv);
211         tenantPermissions.put(tenantId, pv);
212         if (logger.isDebugEnabled()) {
213             logger.debug("seedPermission tenantId=" + tenantId
214                     + " permId=" + id + " resource=" + resource);
215         }
216     }
217
218     private void seedRoles() {
219         //create role using default user in admin role but assign tenant id
220         //use the same role name to check constraints
221         seedRole(TENANT_1, TEST_ROLE_NAME);
222         seedRole(TENANT_2, TEST_ROLE_NAME);
223     }
224
225     private void seedRole(String tenantId, String roleName) {
226         String rid = createRole(tenantId, roleName);
227         RoleValue rv = new RoleValue();
228         rv.setRoleId(rid);
229         rv.setRoleName(roleName);
230         tenantRoles.put(tenantId, rv);
231         if (logger.isDebugEnabled()) {
232             logger.debug("seedRole tenantId=" + tenantId + " roleName=" + roleName);
233         }
234     }
235
236     private void seedAccountRoles() {
237
238         for (String tenantId : tenantAccounts.keySet()) {
239             AccountValue av = (AccountValue) tenantAccounts.get(tenantId);
240             seedAccountRole(tenantId, av.getUserId());
241         }
242     }
243
244     private void seedAccountRole(String tenantId, String userId) {
245         List<RoleValue> tenantRoleValues = new ArrayList<RoleValue>();
246         tenantRoleValues.add(tenantRoles.get(tenantId));
247         createAccountRole(tenantId, userAccounts.get(userId), tenantRoleValues);
248         if (logger.isDebugEnabled()) {
249             logger.debug("seedAccountRole tenantId=" + tenantId + " userId=" + userId);
250         }
251     }
252
253     private void seedPermissionRoles() {
254
255         for (String tenantId : tenantPermissions.keySet()) {
256             PermissionValue pv = tenantPermissions.get(tenantId);
257             seedPermissionRole(tenantId, pv.getPermissionId());
258         }
259     }
260
261     private void seedPermissionRole(String tenantId, String permId) {
262         List<RoleValue> tenantRoleValues = new ArrayList<RoleValue>();
263         tenantRoleValues.add(tenantRoles.get(tenantId));
264         PermissionValue pv = permValues.get(permId);
265         createPermissionRole(tenantId, permValues.get(permId), tenantRoleValues);
266         if (logger.isDebugEnabled()) {
267             logger.debug("seedPermissionRole tenantId=" + tenantId
268                     + " permId=" + permId + " resource=" + pv.getResourceName());
269         }
270     }
271
272
273     /* (non-Javadoc)
274      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
275      */
276     @Override
277     protected CollectionSpaceClient getClientInstance() {
278         return null;
279     }
280
281     /* (non-Javadoc)
282      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
283      */
284     @Override
285     protected AbstractCommonList getAbstractCommonList(
286             ClientResponse<AbstractCommonList> response) {
287         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
288         throw new UnsupportedOperationException();
289     }
290
291     @Test(dataProvider = "testName")
292     @Override
293     public void readPaginatedList(String testName) throws Exception {
294         //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
295     }
296     // ---------------------------------------------------------------
297     // CRUD tests : CREATE tests
298     // ---------------------------------------------------------------
299     // Success outcomes
300
301     @Override
302     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
303     public void create(String testName) throws Exception {
304         if (logger.isDebugEnabled()) {
305             logger.debug(testBanner(testName, CLASS_NAME));
306         }
307
308         ClientResponse<Response> res = create(testName, TENANT_1_USER, TENANT_1);
309
310         TENANT_RESOURCE_1 = extractId(res);
311
312         if (logger.isDebugEnabled()) {
313             logger.debug(testName + ": tenantId= " + TENANT_1
314                     + " userId=" + TENANT_1_USER
315                     + " TENANT_RESOURCE_1 id=" + TENANT_RESOURCE_1);
316         }
317     }
318
319     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
320     public void create2(String testName) throws Exception {
321         if (logger.isDebugEnabled()) {
322             logger.debug(testBanner(testName, CLASS_NAME));
323         }
324
325         ClientResponse<Response> res = create(testName, TENANT_2_USER, TENANT_2);
326
327         TENANT_RESOURCE_2 = extractId(res);
328         if (logger.isDebugEnabled()) {
329             logger.debug(testName + ": tenantId= " + TENANT_2
330                     + " userId=" + TENANT_2_USER
331                     + " TENANT_RESOURCE_2 id=" + TENANT_RESOURCE_2);
332         }
333     }
334
335     private ClientResponse<Response> create(String testName, String userName, String tenatnId) {
336         setupCreate();
337         // Submit the request to the service and store the response.
338         DimensionClient client = new DimensionClient();
339         client.setAuth(true, userName, true, userName, true);
340         String identifier = createIdentifier();
341         DimensionsCommon dimension = new DimensionsCommon();
342         dimension.setDimension("dimensionType");
343         dimension.setMeasurementUnit("measurementUnit-" + identifier);
344         dimension.setValueDate(new Date().toString());
345         PoxPayloadOut multipart = DimensionFactory.createDimensionInstance(client.getCommonPartName(),
346                 dimension);
347         ClientResponse<Response> res = client.create(multipart);
348         int statusCode = res.getStatus();
349
350         if (logger.isDebugEnabled()) {
351             logger.debug(testName + ": status = " + statusCode);
352         }
353         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
354                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
355         Assert.assertEquals(statusCode, Response.Status.CREATED.getStatusCode());
356         return res;
357
358     }
359
360     //to not cause uniqueness violation for permRole, createList is removed
361     @Override
362     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
363     dependsOnMethods = {"create"})
364     public void createList(String testName) throws Exception {
365     }
366
367     // Failure outcomes
368     // Placeholders until the three tests below can be uncommented.
369     // See Issue CSPACE-401.
370     @Override
371     public void createWithEmptyEntityBody(String testName) throws Exception {
372     }
373
374     @Override
375     public void createWithMalformedXml(String testName) throws Exception {
376     }
377
378     @Override
379     public void createWithWrongXmlSchema(String testName) throws Exception {
380     }
381
382     // ---------------------------------------------------------------
383     // CRUD tests : READ tests
384     // ---------------------------------------------------------------
385     // Success outcomes
386     @Override
387     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
388     dependsOnMethods = {"create"})
389     public void read(String testName) throws Exception {
390
391         if (logger.isDebugEnabled()) {
392             logger.debug(testBanner(testName, CLASS_NAME));
393         }
394         DimensionsCommon dimension = read(testName, TENANT_RESOURCE_1, TENANT_1_USER);
395         Assert.assertNotNull(dimension);
396         if (logger.isDebugEnabled()) {
397             logger.debug(testName + ": tenantId= " + TENANT_1
398                     + " userId=" + TENANT_1_USER
399                     + " TENANT_RESOURCE_1 retrieved id=" + dimension.getCsid());
400         }
401     }
402
403     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
404     dependsOnMethods = {"create2"})
405     public void read2(String testName) throws Exception {
406
407         if (logger.isDebugEnabled()) {
408             logger.debug(testBanner(testName, CLASS_NAME));
409         }
410         DimensionsCommon dimension = read(testName, TENANT_RESOURCE_2, TENANT_2_USER);
411         Assert.assertNotNull(dimension);
412         if (logger.isDebugEnabled()) {
413             logger.debug(testName + ": tenantId= " + TENANT_2
414                     + " userId=" + TENANT_2_USER
415                     + " TENANT_RESOURCE_1 retrieved id=" + dimension.getCsid());
416         }
417     }
418
419     private DimensionsCommon read(String testName, String id, String userName) throws Exception {
420         setupRead();
421         // Submit the request to the service and store the response.
422         DimensionClient client = new DimensionClient();
423         client.setAuth(true, userName, true, userName, true);
424         ClientResponse<String> res = client.read(id);
425         int statusCode = res.getStatus();
426
427         // Check the status code of the response: does it match
428         // the expected response(s)?
429         if (logger.isDebugEnabled()) {
430             logger.debug(testName + ": status = " + statusCode);
431         }
432         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
433                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
434         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
435
436         PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
437         return (DimensionsCommon) extractPart(input,
438                 client.getCommonPartName(), DimensionsCommon.class);
439     }
440     // Failure outcomes
441
442     @Override
443     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
444     public void readNonExistent(String testName) throws Exception {
445
446         // Perform setup.
447         setupReadNonExistent();
448     }
449
450     // ---------------------------------------------------------------
451     // CRUD tests : READ_LIST tests
452     // ---------------------------------------------------------------
453     // Success outcomes
454     @Override
455     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
456     dependsOnMethods = {"createList", "read"})
457     public void readList(String testName) throws Exception {
458         setupReadList();
459     }
460
461     // Failure outcomes
462     // None at present.
463     // ---------------------------------------------------------------
464     // CRUD tests : UPDATE tests
465     // ---------------------------------------------------------------
466     // Success outcomes
467     @Override
468     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
469     dependsOnMethods = {"read", "readList", "readNonExistent"})
470     public void update(String testName) throws Exception {
471         setupUpdate();
472
473     }
474
475     // Failure outcomes
476     // Placeholders until the three tests below can be uncommented.
477     // See Issue CSPACE-401.
478     @Override
479     public void updateWithEmptyEntityBody(String testName) throws Exception {
480     }
481
482     @Override
483     public void updateWithMalformedXml(String testName) throws Exception {
484     }
485
486     @Override
487     public void updateWithWrongXmlSchema(String testName) throws Exception {
488     }
489
490     @Override
491     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
492     dependsOnMethods = {"read2", "readNonExistent", "testSubmitRequest"})
493     public void updateNonExistent(String testName) throws Exception {
494
495         DimensionClient client = new DimensionClient();
496         //TENANT_1_USER is not allowed to update the resource of TENANT_2
497         client.setAuth(true, TENANT_1_USER, true, TENANT_1_USER, true);
498
499         DimensionsCommon dimension = new DimensionsCommon();
500         dimension.setDimension("dimensionType");
501         // Update the content of this resource.
502         dimension.setMeasurementUnit("updated-" + dimension.getMeasurementUnit());
503         dimension.setValueDate("updated-" + dimension.getValueDate());
504         // Submit the request to the service and store the response.
505         PoxPayloadOut output = new PoxPayloadOut(DimensionClient.SERVICE_PAYLOAD_NAME);
506         PayloadOutputPart commonPart = output.addPart(dimension, MediaType.APPLICATION_XML_TYPE);
507         commonPart.setLabel(client.getCommonPartName());
508
509         ClientResponse<String> res = client.update(TENANT_RESOURCE_2, output);
510         int statusCode = res.getStatus();
511         // Check the status code of the response: does it match the expected response(s)?
512         if (logger.isDebugEnabled()) {
513             logger.debug(testName + " resource = " + TENANT_RESOURCE_2
514                     + " status = " + statusCode);
515         }
516         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
517                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
518         //going to incorrect Nuxeo domain would give DocumentNotFoundException
519         //instead of giving FORBIDDEN
520         Assert.assertEquals(statusCode, Response.Status.NOT_FOUND.getStatusCode());
521     }
522
523     // ---------------------------------------------------------------
524     // CRUD tests : DELETE tests
525     // ---------------------------------------------------------------
526     // Success outcomes
527     @Override
528     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
529     dependsOnMethods = {"deleteNonExistent"})
530     public void delete(String testName) throws Exception {
531
532         if (logger.isDebugEnabled()) {
533             logger.debug(testBanner(testName, CLASS_NAME));
534         }
535
536         int statusCode = delete(testName, TENANT_RESOURCE_1, TENANT_1_USER);
537     }
538
539     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
540     dependsOnMethods = {"updateNonExistent"})
541     public void delete2(String testName) throws Exception {
542
543         if (logger.isDebugEnabled()) {
544             logger.debug(testBanner(testName, CLASS_NAME));
545         }
546         int statusCode = delete(testName, TENANT_RESOURCE_2, TENANT_2_USER);
547
548     }
549
550     private int delete(String testName, String id, String userName) throws Exception {
551         // Perform setup.
552         setupDelete();
553         // Submit the request to the service and store the response.
554         DimensionClient client = new DimensionClient();
555         client.setAuth(true, userName, true, userName, true);
556         ClientResponse<Response> res = client.delete(id);
557         int statusCode = res.getStatus();
558
559         // Check the status code of the response: does it match
560         // the expected response(s)?
561         if (logger.isDebugEnabled()) {
562             logger.debug(testName + ": status = " + statusCode);
563         }
564         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
565                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
566         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
567         return statusCode;
568     }
569
570     // Failure outcomes
571     @Override
572     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
573     dependsOnMethods = {"read"})
574     public void deleteNonExistent(String testName) throws Exception {
575         //ignoring this test as the service side returns 200 now even if it does
576         //not find a record in the db
577
578         if (logger.isDebugEnabled()) {
579             logger.debug(testBanner(testName, CLASS_NAME));
580         }
581         // Perform setup.
582         setupDelete();
583
584         // Submit the request to the service and store the response.
585         DimensionClient client = new DimensionClient();
586         //TENANT_2_USER of TENANT_2 is not allowed to delete the resource of TENANT_1
587         client.setAuth(true, TENANT_2_USER, true, TENANT_2_USER, true);
588         ClientResponse<Response> res = client.delete(TENANT_RESOURCE_1);
589         int statusCode = res.getStatus();
590
591         // Check the status code of the response: does it match
592         // the expected response(s)?
593         if (logger.isDebugEnabled()) {
594             logger.debug(testName + ": status = " + statusCode);
595         }
596         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
597                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
598         //going to incorrect Nuxeo domain would give DocumentNotFoundException
599         //instead of giving FORBIDDEN
600         Assert.assertEquals(statusCode, Response.Status.NOT_FOUND.getStatusCode());
601     }
602     
603     // ---------------------------------------------------------------
604     // Search tests
605     // ---------------------------------------------------------------
606     
607     @Override
608     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
609     public void searchWorkflowDeleted(String testName) throws Exception {
610         // Fixme: null test for now, overriding test in base class
611     } 
612
613     // ---------------------------------------------------------------
614     // Utility tests : tests of code used in tests above
615     // ---------------------------------------------------------------
616     /**
617      * Tests the code for manually submitting data that is used by several
618      * of the methods above.
619      */
620     @Test(dependsOnMethods = {"create"})
621     public void testSubmitRequest() throws Exception {
622     }
623
624     // ---------------------------------------------------------------
625     // Utility methods used by tests above
626     // ---------------------------------------------------------------
627     @AfterClass(alwaysRun = true)
628     public void cleanUp() {
629         if (logger.isDebugEnabled()) {
630             testBanner("cleanUp", CLASS_NAME);
631         }
632         setupDelete();
633         String noTest = System.getProperty("noTestCleanup");
634         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
635             if (logger.isDebugEnabled()) {
636                 logger.debug("Skipping Cleanup phase ...");
637             }
638             return;
639         }
640         if (logger.isDebugEnabled()) {
641             logger.debug("Cleaning up temporary resources created for testing ...");
642         }
643
644         //tenant admin users are used to create accounts, roles and permissions and relationships
645         //assumption : two tenant admin users exist before running this test
646
647         deletePermissionRoles();
648         deleteAccountRoles();
649         //deletePermissions would delete association with ROLE_XXX_ADMINISTRTOR too
650         //deletePermissions();
651         deleteRoles();
652         deleteAccounts();
653     }
654
655     private void deletePermissionRoles() {
656
657         for (String tenantId : tenantPermissions.keySet()) {
658             List<RoleValue> tenantRoleValues = new ArrayList<RoleValue>();
659             tenantRoleValues.add(tenantRoles.get(tenantId));
660             PermissionValue pv = tenantPermissions.get(tenantId);
661             deletePermissionRole(tenantId, pv, tenantRoleValues);
662         }
663     }
664
665     private void deleteAccountRoles() {
666         for (String tenantId : tenantAccounts.keySet()) {
667             List<RoleValue> tenantRoleValues = new ArrayList<RoleValue>();
668             tenantRoleValues.add(tenantRoles.get(tenantId));
669             AccountValue av = tenantAccounts.get(tenantId);
670             deleteAccountRole(tenantId, av, tenantRoleValues);
671         }
672     }
673
674     private void deletePermissions() {
675         for (String tenantId : tenantPermissions.keySet()) {
676             PermissionValue pv = tenantPermissions.get(tenantId);
677             deletePermission(tenantId, pv.getPermissionId());
678         }
679     }
680
681     private void deleteRoles() {
682         for (String tenantId : tenantRoles.keySet()) {
683             RoleValue rv = tenantRoles.get(tenantId);
684             deleteRole(tenantId, rv.getRoleId());
685         }
686     }
687
688     private void deleteAccounts() {
689
690         for (String tenantId : tenantAccounts.keySet()) {
691             AccountValue av = tenantAccounts.get(tenantId);
692             deleteAccount(tenantId, av.getAccountId());
693         }
694     }
695
696     private String createPermission(String tenantId, String resName,
697             List<PermissionAction> actions, EffectType effect) {
698         setupCreate();
699         PermissionClient permClient = new PermissionClient();
700         UserInfo ui = tenantAdminUsers.get(tenantId);
701         permClient.setAuth(true, ui.userName, true, ui.password, true);
702         Permission permission = PermissionFactory.createPermissionInstance(resName,
703                 "default permissions for " + resName,
704                 actions, effect, true, true, true);
705         permission.setTenantId(tenantId);
706         ClientResponse<Response> res = permClient.create(permission);
707         int statusCode = res.getStatus();
708         if (logger.isDebugEnabled()) {
709             logger.debug("createPermission: resName=" + resName
710                     + " status = " + statusCode);
711         }
712         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
713                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
714         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
715         res.releaseConnection();
716         return extractId(res);
717     }
718
719     private void deletePermission(String tenantId, String permId) {
720         setupDelete();
721         PermissionClient permClient = new PermissionClient();
722         UserInfo ui = tenantAdminUsers.get(tenantId);
723         permClient.setAuth(true, ui.userName, true, ui.password, true);
724         ClientResponse<Response> res = permClient.delete(permId);
725         int statusCode = res.getStatus();
726         if (logger.isDebugEnabled()) {
727             logger.debug("deletePermission: delete permission id="
728                     + permId + " status=" + statusCode);
729         }
730         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
731                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
732         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
733         res.releaseConnection();
734     }
735
736     private String createRole(String tenantId, String roleName) {
737         setupCreate();
738         RoleClient roleClient = new RoleClient();
739         UserInfo ui = tenantAdminUsers.get(tenantId);
740         roleClient.setAuth(true, ui.userName, true, ui.password, true);
741         Role role = RoleFactory.createRoleInstance(roleName,
742                         roleName, //the display name
743                 "role for " + roleName, true);
744         role.setTenantId(tenantId);
745         ClientResponse<Response> res = roleClient.create(role);
746         int statusCode = res.getStatus();
747         if (logger.isDebugEnabled()) {
748             logger.debug("createRole: name=" + roleName
749                     + " status = " + statusCode);
750         }
751         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
752                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
753         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
754         res.releaseConnection();
755         return extractId(res);
756     }
757
758     private void deleteRole(String tenantId, String roleId) {
759         setupDelete();
760         RoleClient roleClient = new RoleClient();
761         UserInfo ui = tenantAdminUsers.get(tenantId);
762         roleClient.setAuth(true, ui.userName, true, ui.password, true);
763         ClientResponse<Response> res = roleClient.delete(roleId);
764         int statusCode = res.getStatus();
765         if (logger.isDebugEnabled()) {
766             logger.debug("deleteRole: delete role id=" + roleId
767                     + " status=" + statusCode);
768         }
769         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
770                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
771         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
772         res.releaseConnection();
773     }
774
775     private String createAccount(String tenantId, String userName, String email) {
776         setupCreate();
777         AccountClient accountClient = new AccountClient();
778         UserInfo ui = tenantAdminUsers.get(tenantId);
779         accountClient.setAuth(true, ui.userName, true, ui.password, true);
780         AccountsCommon account = AccountFactory.createAccountInstance(
781                 userName, userName, userName, email, tenantId,
782                 true, false, true, true);
783         ClientResponse<Response> res = accountClient.create(account);
784         int statusCode = res.getStatus();
785         if (logger.isDebugEnabled()) {
786             logger.debug("createAccount: tenantId=" + tenantId + " userName=" + userName
787                     + " status = " + statusCode);
788         }
789         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
790                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
791         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
792         res.releaseConnection();
793         return extractId(res);
794     }
795
796     private void deleteAccount(String tenantId, String accId) {
797         setupDelete();
798         AccountClient accClient = new AccountClient();
799         UserInfo ui = tenantAdminUsers.get(tenantId);
800         accClient.setAuth(true, ui.userName, true, ui.password, true);
801         ClientResponse<Response> res = accClient.delete(accId);
802         int statusCode = res.getStatus();
803         if (logger.isDebugEnabled()) {
804             logger.debug("deleteAccount: delete account id="
805                     + accId + " status=" + statusCode);
806         }
807         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
808                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
809         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
810         res.releaseConnection();
811     }
812
813     private String createAccountRole(String tenantId, AccountValue av,
814             Collection<RoleValue> rvs) {
815         setupCreate();
816
817         // Submit the request to the service and store the response.
818         AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
819                 av, rvs, true, true);
820         AccountRoleClient client = new AccountRoleClient();
821         UserInfo ui = tenantAdminUsers.get(tenantId);
822         client.setAuth(true, ui.userName, true, ui.password, true);
823         ClientResponse<Response> res = client.create(av.getAccountId(), accRole);
824         int statusCode = res.getStatus();
825
826         if (logger.isDebugEnabled()) {
827             logger.debug("createAccountRole: status = " + statusCode);
828         }
829         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
830                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
831         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
832         res.releaseConnection();
833         return extractId(res);
834     }
835
836     private void deleteAccountRole(String tenantId, AccountValue av,
837             List<RoleValue> rvs) {
838         // Perform setup.
839         setupDelete();
840
841         // Submit the request to the service and store the response.
842         AccountRoleClient client = new AccountRoleClient();
843         UserInfo ui = tenantAdminUsers.get(tenantId);
844         client.setAuth(true, ui.userName, true, ui.password, true);
845         AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
846                 av, rvs, true, true);
847         ClientResponse<Response> res = client.delete(
848                 av.getAccountId());
849         int statusCode = res.getStatus();
850
851         // Check the status code of the response: does it match
852         // the expected response(s)?
853         if (logger.isDebugEnabled()) {
854             logger.debug("deleteAccountRole: status = " + statusCode);
855         }
856         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
857                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
858         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
859         res.releaseConnection();
860     }
861
862     private String createPermissionRole(String tenantId, PermissionValue pv,
863             Collection<RoleValue> rvs) {
864         setupCreate();
865         List<RoleValue> rvls = new ArrayList<RoleValue>();
866         rvls.addAll(rvs);
867         PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
868                 pv, rvls, true, true);
869         PermissionRoleClient client = new PermissionRoleClient();
870         UserInfo ui = tenantAdminUsers.get(tenantId);
871         client.setAuth(true, ui.userName, true, ui.password, true);
872         ClientResponse<Response> res = client.create(pv.getPermissionId(), permRole);
873         int statusCode = res.getStatus();
874
875         if (logger.isDebugEnabled()) {
876             logger.debug("createPermissionRole: status = " + statusCode);
877         }
878         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
879                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
880         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
881         res.releaseConnection();
882         return extractId(res);
883     }
884
885     private void deletePermissionRole(String tenantId, PermissionValue pv, List<RoleValue> rvls) {
886
887         // Perform setup.
888         setupDelete();
889
890         // Submit the request to the service and store the response.
891         PermissionRoleClient client = new PermissionRoleClient();
892         UserInfo ui = tenantAdminUsers.get(tenantId);
893         client.setAuth(true, ui.userName, true, ui.password, true);
894         PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
895                 pv, rvls, true, true);
896         ClientResponse<Response> res = client.delete(pv.getPermissionId());
897         int statusCode = res.getStatus();
898
899         // Check the status code of the response: does it match
900         // the expected response(s)?
901         if (logger.isDebugEnabled()) {
902             logger.debug("deletePermissionRole : status = " + statusCode);
903         }
904         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
905                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
906         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
907         res.releaseConnection();
908     }
909
910         @Override
911         protected String getServiceName() {
912                 // TODO Auto-generated method stub
913         throw new UnsupportedOperationException();
914         }
915 }