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