]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e9c6b4c1f43f4b74aa156f9146d67849f8b4ee0b
[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
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.Response;
38
39 import org.collectionspace.services.account.AccountsCommon;
40 import org.collectionspace.services.authorization.AccountRole;
41 import org.collectionspace.services.authorization.AccountValue;
42 import org.collectionspace.services.authorization.perms.ActionType;
43 import org.collectionspace.services.authorization.perms.EffectType;
44 import org.collectionspace.services.authorization.perms.Permission;
45 import org.collectionspace.services.authorization.perms.PermissionAction;
46 import org.collectionspace.services.authorization.PermissionRole;
47 import org.collectionspace.services.authorization.PermissionValue;
48 import org.collectionspace.services.authorization.Role;
49 import org.collectionspace.services.authorization.RoleValue;
50 import org.collectionspace.services.client.AccountClient;
51 import org.collectionspace.services.client.AccountFactory;
52 import org.collectionspace.services.client.AccountRoleClient;
53 import org.collectionspace.services.client.AccountRoleFactory;
54 import org.collectionspace.services.client.CollectionSpaceClient;
55 import org.collectionspace.services.client.DimensionClient;
56 import org.collectionspace.services.client.DimensionFactory;
57 import org.collectionspace.services.client.IntakeClient;
58 import org.collectionspace.services.client.PayloadOutputPart;
59 import org.collectionspace.services.client.PermissionClient;
60 import org.collectionspace.services.client.PermissionFactory;
61 import org.collectionspace.services.client.PermissionRoleClient;
62 import org.collectionspace.services.client.PermissionRoleFactory;
63 import org.collectionspace.services.client.PoxPayloadIn;
64 import org.collectionspace.services.client.PoxPayloadOut;
65 import org.collectionspace.services.client.RoleClient;
66 import org.collectionspace.services.client.RoleFactory;
67 import org.collectionspace.services.client.test.BaseServiceTest;
68 import org.collectionspace.services.dimension.DimensionsCommon;
69 import org.collectionspace.services.intake.IntakesCommon;
70 import org.collectionspace.services.jaxb.AbstractCommonList;
71 import org.testng.Assert;
72 import org.testng.annotations.Test;
73 import org.testng.annotations.AfterClass;
74 import org.testng.annotations.BeforeClass;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77
78 /**
79  * AuthorizationServiceTest, carries out tests against a deployed and running
80  * Permission, Role, AccountRole, PermissionRole and CollectionObject Services.
81  *
82  * Pre-requisite : authorization-mgt/client tests seed some permissions used by
83  * this test
84  *
85  * $LastChangedRevision: 917 $ $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri,
86  * 06 Nov 2009) $
87  */
88 public class AuthorizationServiceTest extends BaseServiceTest<AbstractCommonList> {
89
90         private final String CLASS_NAME = AuthorizationServiceTest.class.getName();
91         private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
92         // Instance variables specific to this test.
93         private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
94         // key for permValues is id as there could be several perms for the same
95         // resource
96         private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
97         // key for roleValues is roleName
98         private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
99         private String bigbirdPermId;
100         private String elmoPermId;
101         private final static String TEST_SERVICE_NAME = "dimensions";
102         private boolean accountRolesFlipped = false;
103
104         /*
105          * This method is called only by the parent class, AbstractServiceTestImpl
106          */
107
108         @Override
109         protected String getServiceName() {
110                 // no need to return anything but null since no auth resources are
111                 // accessed
112                 throw new UnsupportedOperationException();
113         }
114
115         @Override
116         protected String getServicePathComponent() {
117                 // no need to return anything but null since no auth resources are
118                 // accessed
119                 throw new UnsupportedOperationException();
120         }
121
122         @BeforeClass(alwaysRun = true)
123         public void seedData() throws Exception {
124                 seedPermissions();
125                 seedRoles();
126                 seedAccounts();
127                 seedAccountRoles();
128                 seedPermissionRoles();
129         }
130
131         private void seedPermissions() throws Exception {
132                 String res = TEST_SERVICE_NAME;
133
134                 PermissionAction pac = new PermissionAction();
135                 pac.setName(ActionType.CREATE);
136                 PermissionAction par = new PermissionAction();
137                 par.setName(ActionType.READ);
138                 PermissionAction pau = new PermissionAction();
139                 pau.setName(ActionType.UPDATE);
140
141                 // bigbird can create, read and update but not delete
142                 List<PermissionAction> bbactions = new ArrayList<PermissionAction>();
143                 bbactions.add(pac);
144                 bbactions.add(par);
145                 bbactions.add(pau);
146                 bigbirdPermId = createPermission(res, bbactions, EffectType.PERMIT);
147                 PermissionValue bbpv = new PermissionValue();
148                 bbpv.setResourceName(res);
149                 bbpv.setPermissionId(bigbirdPermId);
150                 permValues.put(bbpv.getPermissionId(), bbpv);
151
152                 // elmo can only read
153                 List<PermissionAction> eactions = new ArrayList<PermissionAction>();
154                 eactions.add(par);
155                 elmoPermId = createPermission(res, eactions, EffectType.PERMIT);
156                 PermissionValue epv = new PermissionValue();
157                 epv.setResourceName(res);
158                 epv.setPermissionId(elmoPermId);
159                 permValues.put(epv.getPermissionId(), epv);
160         }
161
162         private void seedRoles() throws Exception {
163                 String rn1 = "xROLE_TEST_CM";
164                 String r1RoleId = createRole(rn1);
165                 RoleValue rv1 = new RoleValue();
166                 rv1.setRoleId(r1RoleId);
167                 rv1.setRoleName(rn1);
168                 roleValues.put(rv1.getRoleName(), rv1);
169
170                 String rn2 = "xROLE_TEST_INTERN";
171                 String r2RoleId = createRole(rn2);
172                 RoleValue rv2 = new RoleValue();
173                 rv2.setRoleId(r2RoleId);
174                 rv2.setRoleName(rn2);
175                 roleValues.put(rv2.getRoleName(), rv2);
176         }
177
178         private void seedAccounts() throws Exception {
179                 String userId1 = "bigbird2010";
180                 String accId1 = createAccount(userId1, "bigbird@cspace.org");
181                 AccountValue av1 = new AccountValue();
182                 av1.setScreenName(userId1);
183                 av1.setUserId(userId1);
184                 av1.setAccountId(accId1);
185                 accValues.put(av1.getUserId(), av1);
186
187                 String userId2 = "elmo2010";
188                 String accId2 = createAccount(userId2, "elmo@cspace.org");
189                 AccountValue av2 = new AccountValue();
190                 av2.setScreenName(userId2);
191                 av2.setUserId(userId2);
192                 av2.setAccountId(accId2);
193                 accValues.put(av2.getUserId(), av2);
194
195                 String userId3 = "lockedOut";
196                 String accId3 = createAccount(userId3, "lockedOut@cspace.org");
197                 AccountValue av3 = new AccountValue();
198                 av3.setScreenName(userId3);
199                 av3.setUserId(userId3);
200                 av3.setAccountId(accId3);
201                 accValues.put(av3.getUserId(), av3);
202         }
203
204         private void seedAccountRoles() throws Exception {
205
206                 List<RoleValue> bigbirdRoleValues = new ArrayList<RoleValue>();
207                 bigbirdRoleValues.add(roleValues.get("xROLE_TEST_CM"));
208                 createAccountRole(accValues.get("bigbird2010"), bigbirdRoleValues);
209
210                 List<RoleValue> elmoRoleValues = new ArrayList<RoleValue>();
211                 elmoRoleValues.add(roleValues.get("xROLE_TEST_INTERN"));
212                 createAccountRole(accValues.get("elmo2010"), elmoRoleValues);
213         }
214
215         private void seedPermissionRoles() throws Exception {
216
217                 List<RoleValue> bigbirdRoleValues = new ArrayList<RoleValue>();
218                 bigbirdRoleValues.add(roleValues.get("xROLE_TEST_CM"));
219                 createPermissionRole(permValues.get(bigbirdPermId), bigbirdRoleValues);
220
221                 List<RoleValue> elmoRoleValues = new ArrayList<RoleValue>();
222                 elmoRoleValues.add(roleValues.get("xROLE_TEST_INTERN"));
223                 createPermissionRole(permValues.get(elmoPermId), elmoRoleValues);
224
225         }
226
227         /*
228          * (non-Javadoc)
229          * 
230          * @see
231          * org.collectionspace.services.client.test.BaseServiceTest#getClientInstance
232          * ()
233          */
234         @Override
235         protected CollectionSpaceClient getClientInstance() {
236                 // This method is meaningless to this test.
237                 return null;
238         }
239
240         @Override
241         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
242                 // TODO Auto-generated method stub
243                 return null;
244         }
245
246         // ---------------------------------------------------------------
247         // CRUD tests : CREATE tests
248         // ---------------------------------------------------------------
249         // Success outcomes
250
251         @Test(dataProvider = "testName")
252         public void create(String testName) throws Exception {
253                 int statusCode;
254                 setupCreate();
255
256                 // Submit the request to the service and store the response.
257                 DimensionClient client = new DimensionClient();
258                 // bigbird allowed to create
259                 client.setAuth(true, "bigbird2010", true, "bigbird2010", true);
260                 String identifier = createIdentifier();
261                 DimensionsCommon dimension = new DimensionsCommon();
262                 dimension.setDimension("dimensionType");
263                 dimension.setMeasurementUnit("measurementUnit-" + identifier);
264                 dimension.setValueDate(new Date().toString());
265                 PoxPayloadOut multipart = DimensionFactory.createDimensionInstance(client.getCommonPartName(), dimension);
266                 Response res = client.create(multipart);
267                 try {
268                         statusCode = res.getStatus();
269                         if (logger.isDebugEnabled()) {
270                                 logger.debug(testName + ": status = " + statusCode);
271                         }
272                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
273                                         invalidStatusCodeMessage(testRequestType, statusCode));
274                         Assert.assertEquals(statusCode, Response.Status.CREATED.getStatusCode());
275                         knownResourceId = extractId(res);
276                         if (logger.isDebugEnabled()) {
277                                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
278                         }
279                 } finally {
280                         res.close();
281                 }
282
283                 // Now verify that elmo cannot create
284                 client = new DimensionClient();
285                 client.setAuth(true, "elmo2010", true, "elmo2010", true);
286                 res = client.create(multipart);
287                 try {
288                         statusCode = res.getStatus();
289                         if (logger.isDebugEnabled()) {
290                                 logger.debug(testName + " (verify not allowed): status = " + statusCode);
291                         }
292                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
293                                         invalidStatusCodeMessage(testRequestType, statusCode));
294                         Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
295                 } finally {
296                         res.close();
297                 }
298
299                 // Finally, verify that elmo has no access to Intakes
300                 // Submit the request to the service and store the response.
301                 IntakeClient iclient = new IntakeClient();
302                 iclient.setAuth(true, "elmo2010", true, "elmo2010", true);
303                 multipart = createIntakeInstance("entryNumber-" + identifier, "entryDate-" + identifier, "depositor-"
304                                 + identifier);
305                 res = iclient.create(multipart);
306                 try {
307                         if (logger.isDebugEnabled()) {
308                                 logger.debug(testName + " (verify create intake not allowed): status = " + statusCode);
309                         }
310                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
311                                         invalidStatusCodeMessage(testRequestType, statusCode));
312                         Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
313                 } finally {
314                         res.close();
315                 }
316         }
317
318         /**
319          * Creates the intake instance.
320          *
321          * @param entryNumber
322          *            the entry number
323          * @param entryDate
324          *            the entry date
325          * @param depositor
326          *            the depositor
327          * @return the multipart output
328          * @throws Exception 
329          */
330         private PoxPayloadOut createIntakeInstance(String entryNumber, String entryDate, String depositor) throws Exception {
331                 IntakesCommon intake = new IntakesCommon();
332                 intake.setEntryNumber(entryNumber);
333                 intake.setEntryDate(entryDate);
334                 intake.setDepositor(depositor);
335
336                 PoxPayloadOut multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
337                 PayloadOutputPart commonPart = multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
338                 commonPart.setLabel(new IntakeClient().getCommonPartName());
339
340                 if (logger.isDebugEnabled()) {
341                         logger.debug("to be created, intake common");
342                         logger.debug(objectAsXmlString(intake, IntakesCommon.class));
343                 }
344
345                 return multipart;
346         }
347
348         @Test(dataProvider = "testName", dependsOnMethods = { "delete" })
349         public void verifyCreateWithFlippedRoles(String testName) throws Exception {
350                 int statusCode;
351                 setupCreate();
352
353                 // Submit the request to the service and store the response.
354                 DimensionClient client = new DimensionClient();
355                 flipInitialAccountRoles();
356
357                 // Now verify that elmo can create
358                 client.setAuth(true, "elmo2010", true, "elmo2010", true);
359                 client = new DimensionClient();
360
361                 String identifier = createIdentifier();
362                 DimensionsCommon dimension = new DimensionsCommon();
363                 dimension.setDimension("dimensionType");
364                 dimension.setMeasurementUnit("measurementUnit-" + identifier);
365                 dimension.setValueDate(new Date().toString());
366                 PoxPayloadOut multipart = DimensionFactory.createDimensionInstance(client.getCommonPartName(), dimension);
367                 Response res = client.create(multipart);
368                 try {
369                         statusCode = res.getStatus();
370
371                         if (logger.isDebugEnabled()) {
372                                 logger.debug(testName + ": status = " + statusCode);
373                         }
374                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
375                                         invalidStatusCodeMessage(testRequestType, statusCode));
376                         Assert.assertEquals(statusCode, Response.Status.CREATED.getStatusCode());
377                         knownResourceId = extractId(res);
378                         if (logger.isDebugEnabled()) {
379                                 logger.debug(testName + ": knownResourceId=" + knownResourceId);
380                         }
381                 } finally {
382                         res.close();
383                 }
384
385                 // bigbird no longer allowed to create
386                 client.setAuth(true, "bigbird2010", true, "bigbird2010", true);
387                 res = client.create(multipart);
388                 try {
389                         statusCode = res.getStatus();
390                         if (logger.isDebugEnabled()) {
391                                 logger.debug(testName + " (verify not allowed): status = " + statusCode);
392                         }
393                         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
394                                         invalidStatusCodeMessage(testRequestType, statusCode));
395                         Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
396                 } finally {
397                         res.close();
398                 }
399
400                 restoreInitialAccountRoles();
401         }
402
403         // ---------------------------------------------------------------
404         // CRUD tests : READ tests
405         // ---------------------------------------------------------------
406         // Success outcomes
407         @Test(dataProvider = "testName", dependsOnMethods = { "create" })
408         public void read(String testName) throws Exception {
409                 // Perform setup.
410                 setupRead();
411
412                 // Submit the request to the service and store the response.
413                 DimensionClient client = new DimensionClient();
414                 // elmo allowed to read
415                 client.setAuth(true, "elmo2010", true, "elmo2010", true);
416                 Response res = client.read(knownResourceId);
417                 try {
418                         assertStatusCode(res, testName);
419                         PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
420                         DimensionsCommon dimension = (DimensionsCommon) extractPart(input, client.getCommonPartName(),
421                                         DimensionsCommon.class);
422                         Assert.assertNotNull(dimension);
423                 } finally {
424                         if (res != null) {
425                                 res.close();
426                         }
427                 }
428         }
429
430         @Test(dataProvider = "testName", dependsOnMethods = { "read" })
431         public void readLockedOut(String testName) throws Exception {
432                 // Perform setup.
433                 setupForbidden();
434
435                 // Submit the request to the service and store the response.
436                 DimensionClient client = new DimensionClient();
437                 // lockedOut allowed to read
438                 client.setAuth(true, "lockedOut", true, "lockedOut", true);
439                 Response res = client.read(knownResourceId);
440                 try {
441                         assertStatusCode(res, testName);
442                 } finally {
443                         if (res != null) {
444                                 res.close();
445                         }
446                 }
447         }
448
449         @Test(dataProvider = "testName", dependsOnMethods = { "read" })
450         public void updateNotAllowed(String testName) throws Exception {
451                 setupForbidden();
452
453                 // Create a new client and change its AuthN credentials
454                 DimensionClient client = new DimensionClient();
455                 // elmo not allowed to update
456                 client.setAuth(true, "elmo2010", true, "elmo2010", true);
457                 //
458                 // Create a new dimension object
459                 //
460                 DimensionsCommon dimension = new DimensionsCommon();
461                 dimension.setDimension("dimensionType");
462                 // Update the content of this resource.
463                 dimension.setMeasurementUnit("updated-" + dimension.getMeasurementUnit());
464                 dimension.setValueDate("updated-" + dimension.getValueDate());
465                 //
466                 // Create and submit the request to the service and store the response.
467                 //
468                 PoxPayloadOut output = new PoxPayloadOut(DimensionClient.SERVICE_PAYLOAD_NAME);
469                 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), dimension);
470                 Response res = client.update(knownResourceId, output);
471                 try {
472                         assertStatusCode(res, testName);
473                 } finally {
474                         if (res != null) {
475                                 res.close();
476                         }
477                 }
478                 //
479                 // Create another new client with new credentials
480                 //
481                 client = new DimensionClient();
482                 // lockedOut not allowed to update
483                 client.setAuth(true, "lockedOut", true, "lockedOut", true);
484                 //
485                 // Try the update again.
486                 //
487                 res = client.update(knownResourceId, output);
488                 try {
489                         assertStatusCode(res, testName);
490                 } finally {
491                         if (res != null) {
492                                 res.close();
493                         }
494                 }
495
496         }
497
498         // ---------------------------------------------------------------
499         // CRUD tests : DELETE tests
500         // ---------------------------------------------------------------
501         // Success outcomes
502         @Test(dataProvider = "testName", dependsOnMethods = { "updateNotAllowed" })
503         public void deleteNotAllowed(String testName) throws Exception {
504                 // Perform setup.
505                 setupForbidden();
506                 //
507                 // Create a new client and change the AuthN credentials
508                 //
509                 DimensionClient client = new DimensionClient();
510                 // bigbird can not delete
511                 client.setAuth(true, "bigbird2010", true, "bigbird2010", true);
512                 //
513                 // Try to make a DELETE request
514                 //
515                 Response res = client.delete(knownResourceId);
516                 try {
517                         assertStatusCode(res, testName);
518                 } finally {
519                         if (res != null) {
520                                 res.close();
521                         }
522                 }
523         }
524
525         @Test(dataProvider = "testName", dependsOnMethods = { "deleteNotAllowed" })
526         public void delete(String testName) throws Exception {
527                 // Perform setup.
528                 setupDelete();
529
530                 // Submit the request to the service and store the response.
531                 DimensionClient client = new DimensionClient();
532
533                 Response res = client.delete(knownResourceId);
534                 try {
535                         assertStatusCode(res, testName);
536                 } finally {
537                         if (res != null) {
538                                 res.close();
539                         }
540                 }
541         }
542
543         // ---------------------------------------------------------------
544         // Utility methods used by tests above
545         // ---------------------------------------------------------------
546         @AfterClass(alwaysRun = true)
547         public void cleanUp() throws Exception {
548                 setupDelete();
549                 String noTest = System.getProperty("noTestCleanup");
550                 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
551                         if (logger.isDebugEnabled()) {
552                                 logger.debug("Skipping Cleanup phase ...");
553                         }
554                         return;
555                 }
556                 if (logger.isDebugEnabled()) {
557                         logger.debug("Cleaning up temporary resources created for testing ...");
558                 }
559
560                 deletePermissionRoles();
561                 deleteAccountRoles();
562                 // FIXME delete on permission deletes all associations with roles
563                 // this would delete association with ROLE_ADMINISTRATOR too
564                 // deletePermissions();
565                 deleteRoles();
566                 deleteAccounts();
567         }
568
569         private void deletePermissionRoles() throws Exception {
570                 List<RoleValue> bigbirdRoleValues = new ArrayList<RoleValue>();
571                 bigbirdRoleValues.add(roleValues.get("xROLE_TEST_CM"));
572                 deletePermissionRole(permValues.get(bigbirdPermId), bigbirdRoleValues);
573
574                 List<RoleValue> elmoRoleValues = new ArrayList<RoleValue>();
575                 elmoRoleValues.add(roleValues.get("xROLE_TEST_INTERN"));
576                 deletePermissionRole(permValues.get(elmoPermId), elmoRoleValues);
577         }
578
579         private void deleteAccountRoles() throws Exception {
580                 List<RoleValue> bigbirdRoleValues = new ArrayList<RoleValue>();
581                 bigbirdRoleValues.add(roleValues.get("xROLE_TEST_CM"));
582
583                 List<RoleValue> elmoRoleValues = new ArrayList<RoleValue>();
584                 elmoRoleValues.add(roleValues.get("xROLE_TEST_INTERN"));
585                 if (!accountRolesFlipped) {
586                         deleteAccountRole(accValues.get("bigbird2010"), bigbirdRoleValues);
587                         deleteAccountRole(accValues.get("elmo2010"), elmoRoleValues);
588                 } else {
589                         deleteAccountRole(accValues.get("bigbird2010"), elmoRoleValues);
590                         deleteAccountRole(accValues.get("elmo2010"), bigbirdRoleValues);
591                 }
592         }
593
594         private void flipInitialAccountRoles() throws Exception {
595                 if (!accountRolesFlipped) {
596                         List<RoleValue> cmRoleValues = new ArrayList<RoleValue>();
597                         List<RoleValue> internRoleValues = new ArrayList<RoleValue>();
598                         cmRoleValues.add(roleValues.get("xROLE_TEST_CM"));
599                         internRoleValues.add(roleValues.get("xROLE_TEST_INTERN"));
600
601                         deleteAccountRole(accValues.get("bigbird2010"), cmRoleValues);
602                         deleteAccountRole(accValues.get("elmo2010"), internRoleValues);
603
604                         createAccountRole(accValues.get("bigbird2010"), internRoleValues);
605                         createAccountRole(accValues.get("elmo2010"), cmRoleValues);
606
607                         accountRolesFlipped = true;
608                 }
609         }
610
611         private void restoreInitialAccountRoles() throws Exception {
612                 if (accountRolesFlipped) {
613                         List<RoleValue> cmRoleValues = new ArrayList<RoleValue>();
614                         List<RoleValue> internRoleValues = new ArrayList<RoleValue>();
615                         cmRoleValues.add(roleValues.get("xROLE_TEST_CM"));
616                         internRoleValues.add(roleValues.get("xROLE_TEST_INTERN"));
617
618                         deleteAccountRole(accValues.get("bigbird2010"), internRoleValues);
619                         deleteAccountRole(accValues.get("elmo2010"), cmRoleValues);
620
621                         createAccountRole(accValues.get("bigbird2010"), internRoleValues);
622                         createAccountRole(accValues.get("elmo2010"), cmRoleValues);
623                         accountRolesFlipped = false;
624                 }
625         }
626
627         private void deletePermissions() throws Exception {
628                 // delete entities
629                 for (PermissionValue pv : permValues.values()) {
630                         deletePermission(pv.getPermissionId());
631                 }
632         }
633
634         private void deleteRoles() throws Exception {
635                 for (RoleValue rv : roleValues.values()) {
636                         deleteRole(rv.getRoleId());
637                 }
638         }
639
640         private void deleteAccounts() throws Exception {
641
642                 for (AccountValue av1 : accValues.values()) {
643                         deleteAccount(av1.getAccountId());
644                 }
645         }
646
647         private String createPermission(String resName, EffectType effect) throws Exception {
648                 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
649                 return createPermission(resName, actions, effect);
650         }
651
652         private String createPermission(String resName, List<PermissionAction> actions, EffectType effect) throws Exception {
653                 String result = null;
654
655                 setupCreate();
656                 PermissionClient permClient = new PermissionClient();
657                 Permission permission = PermissionFactory.createPermissionInstance(resName, "default permissions for "
658                                 + resName, actions, effect, true, true, true);
659                 Response res = permClient.create(permission);
660                 try {
661                         assertStatusCode(res, "CreatePermission");
662                         result = extractId(res);
663                 } finally {
664                         if (res != null) {
665                                 res.close();
666                         }
667                 }
668
669                 return result;
670         }
671
672         private void deletePermission(String permId) throws Exception {
673                 setupDelete();
674                 PermissionClient permClient = new PermissionClient();
675                 Response res = permClient.delete(permId);
676                 try {
677                         assertStatusCode(res, "DeletePermission");
678                 } finally {
679                         if (res != null) {
680                                 res.close();
681                         }
682                 }
683         }
684
685         private String createRole(String roleName) throws Exception {
686                 String result = null;
687
688                 setupCreate();
689                 RoleClient roleClient = new RoleClient();
690                 Role role = RoleFactory.createRoleInstance(roleName, roleName, // the
691                                                                                                                                                 // display
692                                                                                                                                                 // name
693                                 "role for " + roleName, true, RoleFactory.EMPTY_PERMVALUE_LIST);
694                 Response res = roleClient.create(role);
695                 try {
696                         assertStatusCode(res, "CreateRole");
697                         result = extractId(res);
698                 } finally {
699                         if (res != null) {
700                                 res.close();
701                         }
702                 }
703
704                 return result;
705         }
706
707         private void deleteRole(String roleId) throws Exception {
708                 setupDelete();
709                 RoleClient roleClient = new RoleClient();
710                 Response res = roleClient.delete(roleId);
711                 try {
712                         assertStatusCode(res, "DeleteRole");
713                 } finally {
714                         if (res != null) {
715                                 res.close();
716                         }
717                 }
718         }
719
720         private String createAccount(String userName, String email) throws Exception {
721                 String result = null;
722
723                 setupCreate();
724                 AccountClient accountClient = new AccountClient();
725                 AccountsCommon account = AccountFactory.createAccountInstance(userName, userName, userName, email,
726                                 accountClient.getTenantId(), true, false, true, true);
727                 Response res = accountClient.create(account);
728                 try {
729                         assertStatusCode(res, "CreateAccount");
730                         result = extractId(res);
731                 } finally {
732                         if (res != null) {
733                                 res.close();
734                         }
735                 }
736
737                 return result;
738         }
739
740         private void deleteAccount(String accId) throws Exception {
741                 setupDelete();
742                 AccountClient accClient = new AccountClient();
743                 Response res = accClient.delete(accId);
744                 try {
745                         assertStatusCode(res, "DeleteAccount");
746                 } finally {
747                         if (res != null) {
748                                 res.close();
749                         }
750                 }
751         }
752
753         private String createAccountRole(AccountValue av, Collection<RoleValue> rvs) throws Exception {
754                 String result = null;
755
756                 setupCreate();
757                 // Submit the request to the service and store the response.
758                 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(av, rvs, true, true);
759                 AccountRoleClient client = new AccountRoleClient();
760                 Response res = client.create(av.getAccountId(), accRole);
761                 try {
762                         assertStatusCode(res, "CreateAccountRole");
763                         result = extractId(res);
764                 } finally {
765                         if (res != null) {
766                                 res.close();
767                         }
768                 }
769
770                 return result;
771         }
772
773         private void deleteAccountRole(AccountValue av, Collection<RoleValue> rvs) throws Exception {
774                 // Perform setup.
775                 setupDelete();
776
777                 // Submit the request to the service and store the response.
778                 AccountRoleClient client = new AccountRoleClient();
779                 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(av, rvs, true, true);
780                 Response res = client.delete(av.getAccountId());
781                 try {
782                         assertStatusCode(res, "DeleteAccountRole");
783                 } finally {
784                         if (res != null) {
785                                 res.close();
786                         }
787                 }
788         }
789
790         private String createPermissionRole(PermissionValue pv, Collection<RoleValue> rvs) throws Exception {
791                 String result = null;
792
793                 setupCreate();
794                 List<RoleValue> rvls = new ArrayList<RoleValue>();
795                 rvls.addAll(rvs);
796                 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(pv, rvls, true, true);
797                 PermissionRoleClient client = new PermissionRoleClient();
798                 Response res = client.create(pv.getPermissionId(), permRole);
799                 try {
800                         assertStatusCode(res, "CreatePermissionRole");
801                         result = extractId(res);
802                 } finally {
803                         if (res != null) {
804                                 res.close();
805                         }
806                 }
807
808                 return result;
809         }
810
811         private void deletePermissionRole(PermissionValue pv, Collection<RoleValue> rvs) throws Exception {
812                 List<RoleValue> rvls = new ArrayList<RoleValue>();
813                 rvls.addAll(rvs);
814
815                 // Perform setup.
816                 setupDelete();
817
818                 // Submit the request to the service and store the response.
819                 PermissionRoleClient client = new PermissionRoleClient();
820                 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(pv, rvls, true, true);
821                 Response res = client.delete(pv.getPermissionId());
822                 try {
823                         assertStatusCode(res, "DeletePermissionRole");
824                 } finally {
825                         if (res != null) {
826                                 res.close();
827                         }
828                 }
829         }
830
831         @Override
832         protected Class<AbstractCommonList> getCommonListType() {
833                 throw new UnsupportedOperationException();
834         }
835 }