]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5b4a18cecea782931bf85db236efae9190ee4a0e
[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@collectionspace.org";
97     private final static String TENANT_2_ADMIN_USER = "admin@hearstmuseum.berkeley.edu";
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.setValue("value-" + 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.setValue("updated-" + dimension.getValue());
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     // Utility tests : tests of code used in tests above
605     // ---------------------------------------------------------------
606     /**
607      * Tests the code for manually submitting data that is used by several
608      * of the methods above.
609      */
610     @Test(dependsOnMethods = {"create"})
611     public void testSubmitRequest() throws Exception {
612     }
613
614     // ---------------------------------------------------------------
615     // Utility methods used by tests above
616     // ---------------------------------------------------------------
617     @AfterClass(alwaysRun = true)
618     public void cleanUp() {
619         if (logger.isDebugEnabled()) {
620             testBanner("cleanUp", CLASS_NAME);
621         }
622         setupDelete();
623         String noTest = System.getProperty("noTestCleanup");
624         if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
625             if (logger.isDebugEnabled()) {
626                 logger.debug("Skipping Cleanup phase ...");
627             }
628             return;
629         }
630         if (logger.isDebugEnabled()) {
631             logger.debug("Cleaning up temporary resources created for testing ...");
632         }
633
634         //tenant admin users are used to create accounts, roles and permissions and relationships
635         //assumption : two tenant admin users exist before running this test
636
637         deletePermissionRoles();
638         deleteAccountRoles();
639         //deletePermissions would delete association with ROLE_XXX_ADMINISTRTOR too
640         //deletePermissions();
641         deleteRoles();
642         deleteAccounts();
643     }
644
645     private void deletePermissionRoles() {
646
647         for (String tenantId : tenantPermissions.keySet()) {
648             List<RoleValue> tenantRoleValues = new ArrayList<RoleValue>();
649             tenantRoleValues.add(tenantRoles.get(tenantId));
650             PermissionValue pv = tenantPermissions.get(tenantId);
651             deletePermissionRole(tenantId, pv, tenantRoleValues);
652         }
653     }
654
655     private void deleteAccountRoles() {
656         for (String tenantId : tenantAccounts.keySet()) {
657             List<RoleValue> tenantRoleValues = new ArrayList<RoleValue>();
658             tenantRoleValues.add(tenantRoles.get(tenantId));
659             AccountValue av = tenantAccounts.get(tenantId);
660             deleteAccountRole(tenantId, av, tenantRoleValues);
661         }
662     }
663
664     private void deletePermissions() {
665         for (String tenantId : tenantPermissions.keySet()) {
666             PermissionValue pv = tenantPermissions.get(tenantId);
667             deletePermission(tenantId, pv.getPermissionId());
668         }
669     }
670
671     private void deleteRoles() {
672         for (String tenantId : tenantRoles.keySet()) {
673             RoleValue rv = tenantRoles.get(tenantId);
674             deleteRole(tenantId, rv.getRoleId());
675         }
676     }
677
678     private void deleteAccounts() {
679
680         for (String tenantId : tenantAccounts.keySet()) {
681             AccountValue av = tenantAccounts.get(tenantId);
682             deleteAccount(tenantId, av.getAccountId());
683         }
684     }
685
686     private String createPermission(String tenantId, String resName,
687             List<PermissionAction> actions, EffectType effect) {
688         setupCreate();
689         PermissionClient permClient = new PermissionClient();
690         UserInfo ui = tenantAdminUsers.get(tenantId);
691         permClient.setAuth(true, ui.userName, true, ui.password, true);
692         Permission permission = PermissionFactory.createPermissionInstance(resName,
693                 "default permissions for " + resName,
694                 actions, effect, true, true, true);
695         permission.setTenantId(tenantId);
696         ClientResponse<Response> res = permClient.create(permission);
697         int statusCode = res.getStatus();
698         if (logger.isDebugEnabled()) {
699             logger.debug("createPermission: resName=" + resName
700                     + " status = " + statusCode);
701         }
702         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
703                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
704         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
705         res.releaseConnection();
706         return extractId(res);
707     }
708
709     private void deletePermission(String tenantId, String permId) {
710         setupDelete();
711         PermissionClient permClient = new PermissionClient();
712         UserInfo ui = tenantAdminUsers.get(tenantId);
713         permClient.setAuth(true, ui.userName, true, ui.password, true);
714         ClientResponse<Response> res = permClient.delete(permId);
715         int statusCode = res.getStatus();
716         if (logger.isDebugEnabled()) {
717             logger.debug("deletePermission: delete permission id="
718                     + permId + " status=" + statusCode);
719         }
720         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
721                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
722         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
723         res.releaseConnection();
724     }
725
726     private String createRole(String tenantId, String roleName) {
727         setupCreate();
728         RoleClient roleClient = new RoleClient();
729         UserInfo ui = tenantAdminUsers.get(tenantId);
730         roleClient.setAuth(true, ui.userName, true, ui.password, true);
731         Role role = RoleFactory.createRoleInstance(roleName,
732                         roleName, //the display name
733                 "role for " + roleName, true);
734         role.setTenantId(tenantId);
735         ClientResponse<Response> res = roleClient.create(role);
736         int statusCode = res.getStatus();
737         if (logger.isDebugEnabled()) {
738             logger.debug("createRole: name=" + roleName
739                     + " status = " + statusCode);
740         }
741         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
742                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
743         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
744         res.releaseConnection();
745         return extractId(res);
746     }
747
748     private void deleteRole(String tenantId, String roleId) {
749         setupDelete();
750         RoleClient roleClient = new RoleClient();
751         UserInfo ui = tenantAdminUsers.get(tenantId);
752         roleClient.setAuth(true, ui.userName, true, ui.password, true);
753         ClientResponse<Response> res = roleClient.delete(roleId);
754         int statusCode = res.getStatus();
755         if (logger.isDebugEnabled()) {
756             logger.debug("deleteRole: delete role id=" + roleId
757                     + " status=" + statusCode);
758         }
759         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
760                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
761         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
762         res.releaseConnection();
763     }
764
765     private String createAccount(String tenantId, String userName, String email) {
766         setupCreate();
767         AccountClient accountClient = new AccountClient();
768         UserInfo ui = tenantAdminUsers.get(tenantId);
769         accountClient.setAuth(true, ui.userName, true, ui.password, true);
770         AccountsCommon account = AccountFactory.createAccountInstance(
771                 userName, userName, userName, email, tenantId,
772                 true, false, true, true);
773         ClientResponse<Response> res = accountClient.create(account);
774         int statusCode = res.getStatus();
775         if (logger.isDebugEnabled()) {
776             logger.debug("createAccount: tenantId=" + tenantId + " userName=" + userName
777                     + " status = " + statusCode);
778         }
779         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
780                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
781         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
782         res.releaseConnection();
783         return extractId(res);
784     }
785
786     private void deleteAccount(String tenantId, String accId) {
787         setupDelete();
788         AccountClient accClient = new AccountClient();
789         UserInfo ui = tenantAdminUsers.get(tenantId);
790         accClient.setAuth(true, ui.userName, true, ui.password, true);
791         ClientResponse<Response> res = accClient.delete(accId);
792         int statusCode = res.getStatus();
793         if (logger.isDebugEnabled()) {
794             logger.debug("deleteAccount: delete account id="
795                     + accId + " status=" + statusCode);
796         }
797         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
798                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
799         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
800         res.releaseConnection();
801     }
802
803     private String createAccountRole(String tenantId, AccountValue av,
804             Collection<RoleValue> rvs) {
805         setupCreate();
806
807         // Submit the request to the service and store the response.
808         AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
809                 av, rvs, true, true);
810         AccountRoleClient client = new AccountRoleClient();
811         UserInfo ui = tenantAdminUsers.get(tenantId);
812         client.setAuth(true, ui.userName, true, ui.password, true);
813         ClientResponse<Response> res = client.create(av.getAccountId(), accRole);
814         int statusCode = res.getStatus();
815
816         if (logger.isDebugEnabled()) {
817             logger.debug("createAccountRole: status = " + statusCode);
818         }
819         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
820                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
821         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
822         res.releaseConnection();
823         return extractId(res);
824     }
825
826     private void deleteAccountRole(String tenantId, AccountValue av,
827             List<RoleValue> rvs) {
828         // Perform setup.
829         setupDelete();
830
831         // Submit the request to the service and store the response.
832         AccountRoleClient client = new AccountRoleClient();
833         UserInfo ui = tenantAdminUsers.get(tenantId);
834         client.setAuth(true, ui.userName, true, ui.password, true);
835         AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
836                 av, rvs, true, true);
837         ClientResponse<Response> res = client.delete(
838                 av.getAccountId());
839         int statusCode = res.getStatus();
840
841         // Check the status code of the response: does it match
842         // the expected response(s)?
843         if (logger.isDebugEnabled()) {
844             logger.debug("deleteAccountRole: status = " + statusCode);
845         }
846         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
847                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
848         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
849         res.releaseConnection();
850     }
851
852     private String createPermissionRole(String tenantId, PermissionValue pv,
853             Collection<RoleValue> rvs) {
854         setupCreate();
855         List<RoleValue> rvls = new ArrayList<RoleValue>();
856         rvls.addAll(rvs);
857         PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
858                 pv, rvls, true, true);
859         PermissionRoleClient client = new PermissionRoleClient();
860         UserInfo ui = tenantAdminUsers.get(tenantId);
861         client.setAuth(true, ui.userName, true, ui.password, true);
862         ClientResponse<Response> res = client.create(pv.getPermissionId(), permRole);
863         int statusCode = res.getStatus();
864
865         if (logger.isDebugEnabled()) {
866             logger.debug("createPermissionRole: status = " + statusCode);
867         }
868         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
869                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
870         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
871         res.releaseConnection();
872         return extractId(res);
873     }
874
875     private void deletePermissionRole(String tenantId, PermissionValue pv, List<RoleValue> rvls) {
876
877         // Perform setup.
878         setupDelete();
879
880         // Submit the request to the service and store the response.
881         PermissionRoleClient client = new PermissionRoleClient();
882         UserInfo ui = tenantAdminUsers.get(tenantId);
883         client.setAuth(true, ui.userName, true, ui.password, true);
884         PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
885                 pv, rvls, true, true);
886         ClientResponse<Response> res = client.delete(pv.getPermissionId());
887         int statusCode = res.getStatus();
888
889         // Check the status code of the response: does it match
890         // the expected response(s)?
891         if (logger.isDebugEnabled()) {
892             logger.debug("deletePermissionRole : status = " + statusCode);
893         }
894         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
895                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
896         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
897         res.releaseConnection();
898     }
899
900         @Override
901         protected String getServiceName() {
902                 // TODO Auto-generated method stub
903         throw new UnsupportedOperationException();
904         }
905 }