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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2010 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
25 * To change this template, choose Tools | Templates
26 * and open the template in the editor.
28 package org.collectionspace.services.security.client.test;
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;
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.IntakeClient;
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.RoleClient;
62 import org.collectionspace.services.client.RoleFactory;
63 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
64 import org.collectionspace.services.dimension.DimensionsCommon;
65 import org.collectionspace.services.intake.IntakesCommon;
66 import org.collectionspace.services.jaxb.AbstractCommonList;
67 import org.jboss.resteasy.client.ClientResponse;
68 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
69 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
70 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
72 import org.testng.Assert;
73 import org.testng.annotations.Test;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77 import org.testng.annotations.AfterClass;
78 import org.testng.annotations.BeforeClass;
81 * AuthorizationServiceTest, carries out tests against a
82 * deployed and running Permission, Role, AccountRole, PermissionRole and
83 * CollectionObject Services.
85 * Pre-requisite : authorization-mgt/client tests seed some permissions used
88 * $LastChangedRevision: 917 $
89 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
91 public class AuthorizationServiceTest extends AbstractServiceTestImpl {
93 private final String CLASS_NAME = AuthorizationServiceTest.class.getName();
94 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
95 // Instance variables specific to this test.
96 private String knownResourceId = null;
97 private List<String> allResourceIdsCreated = new ArrayList();
98 //key for accValues is userId
99 private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
100 //key for permValues is id as there could be several perms for the same resource
101 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
102 //key for roleValues is roleName
103 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
104 private String bigbirdPermId;
105 private String elmoPermId;
106 private final static String TEST_SERVICE_NAME = "dimensions";
107 private boolean accountRolesFlipped = false;
109 * This method is called only by the parent class, AbstractServiceTestImpl
113 protected String getServicePathComponent() {
117 @BeforeClass(alwaysRun = true)
118 public void seedData() {
123 seedPermissionRoles();
126 private void seedPermissions() {
127 String res = TEST_SERVICE_NAME;
129 PermissionAction pac = new PermissionAction();
130 pac.setName(ActionType.CREATE);
131 PermissionAction par = new PermissionAction();
132 par.setName(ActionType.READ);
133 PermissionAction pau = new PermissionAction();
134 pau.setName(ActionType.UPDATE);
137 //bigbird can create, read and update but not delete
138 List<PermissionAction> bbactions = new ArrayList<PermissionAction>();
142 bigbirdPermId = createPermission(res, bbactions, EffectType.PERMIT);
143 PermissionValue bbpv = new PermissionValue();
144 bbpv.setResourceName(res);
145 bbpv.setPermissionId(bigbirdPermId);
146 permValues.put(bbpv.getPermissionId(), bbpv);
149 List<PermissionAction> eactions = new ArrayList<PermissionAction>();
151 elmoPermId = createPermission(res, eactions, EffectType.PERMIT);
152 PermissionValue epv = new PermissionValue();
153 epv.setResourceName(res);
154 epv.setPermissionId(elmoPermId);
155 permValues.put(epv.getPermissionId(), epv);
158 private void seedRoles() {
159 String rn1 = "ROLE_TEST_CM";
160 String r1RoleId = createRole(rn1);
161 RoleValue rv1 = new RoleValue();
162 rv1.setRoleId(r1RoleId);
163 rv1.setRoleName(rn1);
164 roleValues.put(rv1.getRoleName(), rv1);
166 String rn2 = "ROLE_TEST_INTERN";
167 String r2RoleId = createRole(rn2);
168 RoleValue rv2 = new RoleValue();
169 rv2.setRoleId(r2RoleId);
170 rv2.setRoleName(rn2);
171 roleValues.put(rv2.getRoleName(), rv2);
174 private void seedAccounts() {
175 String userId1 = "bigbird2010";
176 String accId1 = createAccount(userId1, "bigbird@cspace.org");
177 AccountValue av1 = new AccountValue();
178 av1.setScreenName(userId1);
179 av1.setUserId(userId1);
180 av1.setAccountId(accId1);
181 accValues.put(av1.getUserId(), av1);
183 String userId2 = "elmo2010";
184 String accId2 = createAccount(userId2, "elmo@cspace.org");
185 AccountValue av2 = new AccountValue();
186 av2.setScreenName(userId2);
187 av2.setUserId(userId2);
188 av2.setAccountId(accId2);
189 accValues.put(av2.getUserId(), av2);
191 String userId3 = "lockedOut";
192 String accId3 = createAccount(userId3, "lockedOut@cspace.org");
193 AccountValue av3 = new AccountValue();
194 av3.setScreenName(userId3);
195 av3.setUserId(userId3);
196 av3.setAccountId(accId3);
197 accValues.put(av3.getUserId(), av3);
200 private void seedAccountRoles() {
202 List<RoleValue> bigbirdRoleValues = new ArrayList<RoleValue>();
203 bigbirdRoleValues.add(roleValues.get("ROLE_TEST_CM"));
204 createAccountRole(accValues.get("bigbird2010"), bigbirdRoleValues);
206 List<RoleValue> elmoRoleValues = new ArrayList<RoleValue>();
207 elmoRoleValues.add(roleValues.get("ROLE_TEST_INTERN"));
208 createAccountRole(accValues.get("elmo2010"), elmoRoleValues);
211 private void seedPermissionRoles() {
213 List<RoleValue> bigbirdRoleValues = new ArrayList<RoleValue>();
214 bigbirdRoleValues.add(roleValues.get("ROLE_TEST_CM"));
215 createPermissionRole(permValues.get(bigbirdPermId), bigbirdRoleValues);
217 List<RoleValue> elmoRoleValues = new ArrayList<RoleValue>();
218 elmoRoleValues.add(roleValues.get("ROLE_TEST_INTERN"));
219 createPermissionRole(permValues.get(elmoPermId), elmoRoleValues);
225 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
228 protected CollectionSpaceClient getClientInstance() {
233 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
236 protected AbstractCommonList getAbstractCommonList(
237 ClientResponse<AbstractCommonList> response) {
238 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
239 throw new UnsupportedOperationException();
242 @Test(dataProvider = "testName")
244 public void readPaginatedList(String testName) throws Exception {
245 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
247 // ---------------------------------------------------------------
248 // CRUD tests : CREATE tests
249 // ---------------------------------------------------------------
253 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
254 public void create(String testName) throws Exception {
255 if (logger.isDebugEnabled()) {
256 logger.debug(testBanner(testName, CLASS_NAME));
260 // Submit the request to the service and store the response.
261 DimensionClient client = new DimensionClient();
262 //bigbird allowed to create
263 client.setAuth(true, "bigbird2010", true, "bigbird2010", true);
264 String identifier = createIdentifier();
265 DimensionsCommon dimension = new DimensionsCommon();
266 dimension.setDimension("dimensionType");
267 dimension.setValue("value-" + identifier);
268 dimension.setValueDate(new Date().toString());
269 MultipartOutput multipart = DimensionFactory.createDimensionInstance(client.getCommonPartName(),
271 ClientResponse<Response> res = client.create(multipart);
273 int statusCode = res.getStatus();
275 if (logger.isDebugEnabled()) {
276 logger.debug(testName + ": status = " + statusCode);
278 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
279 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
280 Assert.assertEquals(statusCode, Response.Status.CREATED.getStatusCode());
281 knownResourceId = extractId(res);
282 if (logger.isDebugEnabled()) {
283 logger.debug(testName + ": knownResourceId=" + knownResourceId);
286 // Now verify that elmo cannot create
287 client = new DimensionClient();
288 client.setAuth(true, "elmo2010", true, "elmo2010", true);
289 res = client.create(multipart);
291 statusCode = res.getStatus();
292 if (logger.isDebugEnabled()) {
293 logger.debug(testName + " (verify not allowed): status = " + statusCode);
295 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
296 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
297 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
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(
304 "entryNumber-" + identifier,
305 "entryDate-" + identifier,
306 "depositor-" + identifier);
307 res = iclient.create(multipart);
308 if (logger.isDebugEnabled()) {
309 logger.debug(testName + " (verify create intake not allowed): status = " + statusCode);
311 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
312 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
313 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
318 * Creates the intake instance.
320 * @param entryNumber the entry number
321 * @param entryDate the entry date
322 * @param depositor the depositor
323 * @return the multipart output
325 private MultipartOutput createIntakeInstance(String entryNumber,
328 IntakesCommon intake = new IntakesCommon();
329 intake.setEntryNumber(entryNumber);
330 intake.setEntryDate(entryDate);
331 intake.setDepositor(depositor);
333 MultipartOutput multipart = new MultipartOutput();
334 OutputPart commonPart =
335 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
336 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
338 if(logger.isDebugEnabled()){
339 logger.debug("to be created, intake common");
340 logger.debug(objectAsXmlString(intake, IntakesCommon.class));
347 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
348 dependsOnMethods = {"delete"})
349 public void verifyCreateWithFlippedRoles(String testName) throws Exception {
350 if (logger.isDebugEnabled()) {
351 logger.debug(testBanner(testName, CLASS_NAME));
355 // Submit the request to the service and store the response.
356 DimensionClient client = new DimensionClient();
357 flipInitialAccountRoles();
359 // Now verify that elmo can create
360 client.setAuth(true, "elmo2010", true, "elmo2010", true);
361 client = new DimensionClient();
363 String identifier = createIdentifier();
364 DimensionsCommon dimension = new DimensionsCommon();
365 dimension.setDimension("dimensionType");
366 dimension.setValue("value-" + identifier);
367 dimension.setValueDate(new Date().toString());
368 MultipartOutput multipart = DimensionFactory.createDimensionInstance(client.getCommonPartName(),
370 ClientResponse<Response> res = client.create(multipart);
372 int statusCode = res.getStatus();
374 if (logger.isDebugEnabled()) {
375 logger.debug(testName + ": status = " + statusCode);
377 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
378 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
379 Assert.assertEquals(statusCode, Response.Status.CREATED.getStatusCode());
380 knownResourceId = extractId(res);
381 if (logger.isDebugEnabled()) {
382 logger.debug(testName + ": knownResourceId=" + knownResourceId);
385 //bigbird no longer allowed to create
386 client.setAuth(true, "bigbird2010", true, "bigbird2010", true);
387 res = client.create(multipart);
389 statusCode = res.getStatus();
390 if (logger.isDebugEnabled()) {
391 logger.debug(testName + " (verify not allowed): status = " + statusCode);
393 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
394 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
395 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
396 restoreInitialAccountRoles();
399 //to not cause uniqueness violation for permRole, createList is removed
401 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
402 dependsOnMethods = {"create"})
403 public void createList(String testName) throws Exception {
407 // Placeholders until the three tests below can be uncommented.
408 // See Issue CSPACE-401.
410 public void createWithEmptyEntityBody(String testName) throws Exception {
414 public void createWithMalformedXml(String testName) throws Exception {
418 public void createWithWrongXmlSchema(String testName) throws Exception {
421 // ---------------------------------------------------------------
422 // CRUD tests : READ tests
423 // ---------------------------------------------------------------
426 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
427 dependsOnMethods = {"create"})
428 public void read(String testName) throws Exception {
430 if (logger.isDebugEnabled()) {
431 logger.debug(testBanner(testName, CLASS_NAME));
436 // Submit the request to the service and store the response.
437 DimensionClient client = new DimensionClient();
438 //elmo allowed to read
439 client.setAuth(true, "elmo2010", true, "elmo2010", true);
440 ClientResponse<MultipartInput> res = client.read(knownResourceId);
441 int statusCode = res.getStatus();
443 // Check the status code of the response: does it match
444 // the expected response(s)?
445 if (logger.isDebugEnabled()) {
446 logger.debug(testName + ": status = " + statusCode);
448 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
449 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
450 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
452 MultipartInput input = (MultipartInput) res.getEntity();
453 DimensionsCommon dimension = (DimensionsCommon) extractPart(input,
454 client.getCommonPartName(), DimensionsCommon.class);
455 Assert.assertNotNull(dimension);
458 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
459 dependsOnMethods = {"read"})
460 public void readLockedOut(String testName) throws Exception {
462 if (logger.isDebugEnabled()) {
463 logger.debug(testBanner(testName, CLASS_NAME));
468 // Submit the request to the service and store the response.
469 DimensionClient client = new DimensionClient();
470 //lockedOut allowed to read
471 client.setAuth(true, "lockedOut", true, "lockedOut", true);
472 ClientResponse<MultipartInput> res = client.read(knownResourceId);
473 int statusCode = res.getStatus();
475 // Check the status code of the response: does it match
476 // the expected response(s)?
477 if (logger.isDebugEnabled()) {
478 logger.debug(testName + " (test lockedOut): status = " + statusCode);
480 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
481 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
482 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
487 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
488 public void readNonExistent(String testName) throws Exception {
491 setupReadNonExistent();
494 // ---------------------------------------------------------------
495 // CRUD tests : READ_LIST tests
496 // ---------------------------------------------------------------
499 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
500 dependsOnMethods = {"createList", "read"})
501 public void readList(String testName) throws Exception {
507 // ---------------------------------------------------------------
508 // CRUD tests : UPDATE tests
509 // ---------------------------------------------------------------
512 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
513 dependsOnMethods = {"read", "readList", "readNonExistent"})
514 public void update(String testName) throws Exception {
519 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
520 dependsOnMethods = {"read", "readList", "readNonExistent"})
521 public void updateNotAllowed(String testName) throws Exception {
523 if (logger.isDebugEnabled()) {
524 logger.debug(testBanner(testName, CLASS_NAME));
528 DimensionClient client = new DimensionClient();
530 //elmo not allowed to update
531 client.setAuth(true, "elmo2010", true, "elmo2010", true);
533 DimensionsCommon dimension = new DimensionsCommon();
534 dimension.setDimension("dimensionType");
535 // Update the content of this resource.
536 dimension.setValue("updated-" + dimension.getValue());
537 dimension.setValueDate("updated-" + dimension.getValueDate());
538 // Submit the request to the service and store the response.
539 MultipartOutput output = new MultipartOutput();
540 OutputPart commonPart = output.addPart(dimension, MediaType.APPLICATION_XML_TYPE);
541 commonPart.getHeaders().add("label", client.getCommonPartName());
543 ClientResponse<MultipartInput> res = client.update(knownResourceId, output);
544 int statusCode = res.getStatus();
545 // Check the status code of the response: does it match the expected response(s)?
546 if (logger.isDebugEnabled()) {
547 logger.debug(testName + ": status = " + statusCode);
549 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
550 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
551 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
553 client = new DimensionClient();
555 //lockedOut not allowed to update
556 client.setAuth(true, "lockedOut", true, "lockedOut", true);
557 res = client.update(knownResourceId, output);
558 statusCode = res.getStatus();
559 // Check the status code of the response: does it match the expected response(s)?
560 if (logger.isDebugEnabled()) {
561 logger.debug(testName + ": (lockedOut) status = " + statusCode);
563 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
564 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
565 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
570 // Placeholders until the three tests below can be uncommented.
571 // See Issue CSPACE-401.
573 public void updateWithEmptyEntityBody(String testName) throws Exception {
577 public void updateWithMalformedXml(String testName) throws Exception {
581 public void updateWithWrongXmlSchema(String testName) throws Exception {
585 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
586 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
587 public void updateNonExistent(String testName) throws Exception {
590 // ---------------------------------------------------------------
591 // CRUD tests : DELETE tests
592 // ---------------------------------------------------------------
594 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
595 dependsOnMethods = {"updateNotAllowed"})
596 public void deleteNotAllowed(String testName) throws Exception {
598 if (logger.isDebugEnabled()) {
599 logger.debug(testBanner(testName, CLASS_NAME));
604 // Submit the request to the service and store the response.
605 DimensionClient client = new DimensionClient();
606 //bigbird can not delete
607 client.setAuth(true, "bigbird2010", true, "bigbird2010", true);
608 ClientResponse<Response> res = client.delete(knownResourceId);
609 int statusCode = res.getStatus();
611 // Check the status code of the response: does it match
612 // the expected response(s)?
613 if (logger.isDebugEnabled()) {
614 logger.debug(testName + ": status = " + statusCode);
616 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
617 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
618 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
623 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
624 dependsOnMethods = {"deleteNotAllowed"})
625 public void delete(String testName) throws Exception {
627 if (logger.isDebugEnabled()) {
628 logger.debug(testBanner(testName, CLASS_NAME));
633 // Submit the request to the service and store the response.
634 DimensionClient client = new DimensionClient();
636 ClientResponse<Response> res = client.delete(knownResourceId);
637 int statusCode = res.getStatus();
639 // Check the status code of the response: does it match
640 // the expected response(s)?
641 if (logger.isDebugEnabled()) {
642 logger.debug(testName + ": status = " + statusCode);
644 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
645 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
646 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
652 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
653 public void deleteNonExistent(String testName) throws Exception {
654 //ignoring this test as the service side returns 200 now even if it does
655 //not find a record in the db
658 // ---------------------------------------------------------------
659 // Utility tests : tests of code used in tests above
660 // ---------------------------------------------------------------
662 * Tests the code for manually submitting data that is used by several
663 * of the methods above.
665 @Test(dependsOnMethods = {"create"})
666 public void testSubmitRequest() throws Exception {
669 // ---------------------------------------------------------------
670 // Utility methods used by tests above
671 // ---------------------------------------------------------------
672 @AfterClass(alwaysRun = true)
673 public void cleanUp() {
675 String noTest = System.getProperty("noTestCleanup");
676 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
677 if (logger.isDebugEnabled()) {
678 logger.debug("Skipping Cleanup phase ...");
682 if (logger.isDebugEnabled()) {
683 logger.debug("Cleaning up temporary resources created for testing ...");
686 deletePermissionRoles();
687 deleteAccountRoles();
688 //FIXME delete on permission deletes all associations with roles
689 //this would delete association with ROLE_ADMINISTRATOR too
690 //deletePermissions();
695 private void deletePermissionRoles() {
697 List<RoleValue> bigbirdRoleValues = new ArrayList<RoleValue>();
698 bigbirdRoleValues.add(roleValues.get("ROLE_TEST_CM"));
699 deletePermissionRole(permValues.get(bigbirdPermId), bigbirdRoleValues);
701 List<RoleValue> elmoRoleValues = new ArrayList<RoleValue>();
702 elmoRoleValues.add(roleValues.get("ROLE_TEST_INTERN"));
703 deletePermissionRole(permValues.get(elmoPermId), elmoRoleValues);
707 private void deleteAccountRoles() {
708 List<RoleValue> bigbirdRoleValues = new ArrayList<RoleValue>();
709 bigbirdRoleValues.add(roleValues.get("ROLE_TEST_CM"));
711 List<RoleValue> elmoRoleValues = new ArrayList<RoleValue>();
712 elmoRoleValues.add(roleValues.get("ROLE_TEST_INTERN"));
713 if(!accountRolesFlipped) {
714 deleteAccountRole(accValues.get("bigbird2010"), bigbirdRoleValues);
715 deleteAccountRole(accValues.get("elmo2010"), elmoRoleValues);
717 deleteAccountRole(accValues.get("bigbird2010"), elmoRoleValues);
718 deleteAccountRole(accValues.get("elmo2010"),bigbirdRoleValues );
722 private void flipInitialAccountRoles() {
723 if(!accountRolesFlipped) {
724 List<RoleValue> cmRoleValues = new ArrayList<RoleValue>();
725 List<RoleValue> internRoleValues = new ArrayList<RoleValue>();
726 cmRoleValues.add(roleValues.get("ROLE_TEST_CM"));
727 internRoleValues.add(roleValues.get("ROLE_TEST_INTERN"));
729 deleteAccountRole(accValues.get("bigbird2010"), cmRoleValues);
730 deleteAccountRole(accValues.get("elmo2010"), internRoleValues);
732 createAccountRole(accValues.get("bigbird2010"), internRoleValues);
733 createAccountRole(accValues.get("elmo2010"), cmRoleValues);
735 accountRolesFlipped = true;
739 private void restoreInitialAccountRoles() {
740 if(accountRolesFlipped) {
741 List<RoleValue> cmRoleValues = new ArrayList<RoleValue>();
742 List<RoleValue> internRoleValues = new ArrayList<RoleValue>();
743 cmRoleValues.add(roleValues.get("ROLE_TEST_CM"));
744 internRoleValues.add(roleValues.get("ROLE_TEST_INTERN"));
746 deleteAccountRole(accValues.get("bigbird2010"), internRoleValues);
747 deleteAccountRole(accValues.get("elmo2010"), cmRoleValues);
749 createAccountRole(accValues.get("bigbird2010"), internRoleValues);
750 createAccountRole(accValues.get("elmo2010"), cmRoleValues);
751 accountRolesFlipped = false;
757 private void deletePermissions() {
759 for (PermissionValue pv : permValues.values()) {
760 deletePermission(pv.getPermissionId());
764 private void deleteRoles() {
765 for (RoleValue rv : roleValues.values()) {
766 deleteRole(rv.getRoleId());
770 private void deleteAccounts() {
772 for (AccountValue av1 : accValues.values()) {
773 deleteAccount(av1.getAccountId());
777 private String createPermission(String resName, EffectType effect) {
778 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
779 return createPermission(resName, actions, effect);
782 private String createPermission(String resName,
783 List<PermissionAction> actions, EffectType effect) {
785 PermissionClient permClient = new PermissionClient();
786 Permission permission = PermissionFactory.createPermissionInstance(resName,
787 "default permissions for " + resName,
788 actions, effect, true, true, true);
789 ClientResponse<Response> res = permClient.create(permission);
790 int statusCode = res.getStatus();
791 if (logger.isDebugEnabled()) {
792 logger.debug("createPermission: resName=" + resName
793 + " status = " + statusCode);
795 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
796 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
797 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
798 res.releaseConnection();
799 return extractId(res);
802 private void deletePermission(String permId) {
804 PermissionClient permClient = new PermissionClient();
805 ClientResponse<Response> res = permClient.delete(permId);
806 int statusCode = res.getStatus();
807 if (logger.isDebugEnabled()) {
808 logger.debug("deletePermission: delete permission id="
809 + permId + " status=" + statusCode);
811 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
812 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
813 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
814 res.releaseConnection();
817 private String createRole(String roleName) {
819 RoleClient roleClient = new RoleClient();
821 Role role = RoleFactory.createRoleInstance(roleName,
822 roleName, //the display name
823 "role for " + roleName, true);
824 ClientResponse<Response> res = roleClient.create(role);
825 int statusCode = res.getStatus();
826 if (logger.isDebugEnabled()) {
827 logger.debug("createRole: name=" + roleName
828 + " status = " + statusCode);
830 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
831 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
832 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
833 res.releaseConnection();
834 return extractId(res);
837 private void deleteRole(String roleId) {
839 RoleClient roleClient = new RoleClient();
840 ClientResponse<Response> res = roleClient.delete(roleId);
841 int statusCode = res.getStatus();
842 if (logger.isDebugEnabled()) {
843 logger.debug("deleteRole: delete role id=" + roleId
844 + " status=" + statusCode);
846 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
847 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
848 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
849 res.releaseConnection();
852 private String createAccount(String userName, String email) {
854 AccountClient accountClient = new AccountClient();
855 AccountsCommon account = AccountFactory.createAccountInstance(
856 userName, userName, userName, email, accountClient.getTenantId(),
857 true, false, true, true);
858 ClientResponse<Response> res = accountClient.create(account);
859 int statusCode = res.getStatus();
860 if (logger.isDebugEnabled()) {
861 logger.debug("createAccount: userName=" + userName
862 + " status = " + statusCode);
864 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
865 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
866 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
867 res.releaseConnection();
868 return extractId(res);
871 private void deleteAccount(String accId) {
873 AccountClient accClient = new AccountClient();
874 ClientResponse<Response> res = accClient.delete(accId);
875 int statusCode = res.getStatus();
876 if (logger.isDebugEnabled()) {
877 logger.debug("deleteAccount: delete account id="
878 + accId + " status=" + statusCode);
880 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
881 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
882 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
883 res.releaseConnection();
886 private String createAccountRole(AccountValue av,
887 Collection<RoleValue> rvs) {
890 // Submit the request to the service and store the response.
891 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
892 av, rvs, true, true);
893 AccountRoleClient client = new AccountRoleClient();
894 ClientResponse<Response> res = client.create(av.getAccountId(), accRole);
895 int statusCode = res.getStatus();
897 if (logger.isDebugEnabled()) {
898 logger.debug("createAccountRole: status = " + statusCode);
900 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
901 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
902 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
903 res.releaseConnection();
904 return extractId(res);
907 private void deleteAccountRole(AccountValue av,
908 Collection<RoleValue> rvs) {
912 // Submit the request to the service and store the response.
913 AccountRoleClient client = new AccountRoleClient();
914 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
915 av, rvs, true, true);
916 ClientResponse<Response> res = client.delete(
918 int statusCode = res.getStatus();
920 // Check the status code of the response: does it match
921 // the expected response(s)?
922 if (logger.isDebugEnabled()) {
923 logger.debug("deleteAccountRole: status = " + statusCode);
925 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
926 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
927 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
928 res.releaseConnection();
931 private String createPermissionRole(PermissionValue pv,
932 Collection<RoleValue> rvs) {
934 List<RoleValue> rvls = new ArrayList<RoleValue>();
936 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
937 pv, rvls, true, true);
938 PermissionRoleClient client = new PermissionRoleClient();
939 ClientResponse<Response> res = client.create(pv.getPermissionId(), permRole);
940 int statusCode = res.getStatus();
942 if (logger.isDebugEnabled()) {
943 logger.debug("createPermissionRole: status = " + statusCode);
945 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
946 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
947 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
948 res.releaseConnection();
949 return extractId(res);
952 private void deletePermissionRole(PermissionValue pv,
953 Collection<RoleValue> rvs) {
954 List<RoleValue> rvls = new ArrayList<RoleValue>();
960 // Submit the request to the service and store the response.
961 PermissionRoleClient client = new PermissionRoleClient();
962 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
963 pv, rvls, true, true);
964 ClientResponse<Response> res = client.delete(pv.getPermissionId());
965 int statusCode = res.getStatus();
967 // Check the status code of the response: does it match
968 // the expected response(s)?
969 if (logger.isDebugEnabled()) {
970 logger.debug("deletePermissionRole : status = " + statusCode);
972 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
973 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
974 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
975 res.releaseConnection();