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.PayloadOutputPart;
58 import org.collectionspace.services.client.PermissionClient;
59 import org.collectionspace.services.client.PermissionFactory;
60 import org.collectionspace.services.client.PermissionRoleClient;
61 import org.collectionspace.services.client.PermissionRoleFactory;
62 import org.collectionspace.services.client.PoxPayloadIn;
63 import org.collectionspace.services.client.PoxPayloadOut;
64 import org.collectionspace.services.client.RoleClient;
65 import org.collectionspace.services.client.RoleFactory;
66 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
67 import org.collectionspace.services.dimension.DimensionsCommon;
68 import org.collectionspace.services.intake.IntakesCommon;
69 import org.collectionspace.services.jaxb.AbstractCommonList;
70 import org.jboss.resteasy.client.ClientResponse;
71 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
72 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
73 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
75 import org.testng.Assert;
76 import org.testng.annotations.Test;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
80 import org.testng.annotations.AfterClass;
81 import org.testng.annotations.BeforeClass;
84 * AuthorizationServiceTest, carries out tests against a
85 * deployed and running Permission, Role, AccountRole, PermissionRole and
86 * CollectionObject Services.
88 * Pre-requisite : authorization-mgt/client tests seed some permissions used
91 * $LastChangedRevision: 917 $
92 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
94 public class AuthorizationServiceTest extends AbstractServiceTestImpl {
96 private final String CLASS_NAME = AuthorizationServiceTest.class.getName();
97 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
98 // Instance variables specific to this test.
99 private String knownResourceId = null;
100 private List<String> allResourceIdsCreated = new ArrayList();
101 //key for accValues is userId
102 private Hashtable<String, AccountValue> accValues = new Hashtable<String, AccountValue>();
103 //key for permValues is id as there could be several perms for the same resource
104 private Hashtable<String, PermissionValue> permValues = new Hashtable<String, PermissionValue>();
105 //key for roleValues is roleName
106 private Hashtable<String, RoleValue> roleValues = new Hashtable<String, RoleValue>();
107 private String bigbirdPermId;
108 private String elmoPermId;
109 private final static String TEST_SERVICE_NAME = "dimensions";
110 private boolean accountRolesFlipped = false;
112 * This method is called only by the parent class, AbstractServiceTestImpl
116 protected String getServiceName() {
117 // no need to return anything but null since no auth resources are
119 throw new UnsupportedOperationException();
123 protected String getServicePathComponent() {
124 // no need to return anything but null since no auth resources are
126 throw new UnsupportedOperationException();
129 @BeforeClass(alwaysRun = true)
130 public void seedData() {
135 seedPermissionRoles();
138 private void seedPermissions() {
139 String res = TEST_SERVICE_NAME;
141 PermissionAction pac = new PermissionAction();
142 pac.setName(ActionType.CREATE);
143 PermissionAction par = new PermissionAction();
144 par.setName(ActionType.READ);
145 PermissionAction pau = new PermissionAction();
146 pau.setName(ActionType.UPDATE);
149 //bigbird can create, read and update but not delete
150 List<PermissionAction> bbactions = new ArrayList<PermissionAction>();
154 bigbirdPermId = createPermission(res, bbactions, EffectType.PERMIT);
155 PermissionValue bbpv = new PermissionValue();
156 bbpv.setResourceName(res);
157 bbpv.setPermissionId(bigbirdPermId);
158 permValues.put(bbpv.getPermissionId(), bbpv);
161 List<PermissionAction> eactions = new ArrayList<PermissionAction>();
163 elmoPermId = createPermission(res, eactions, EffectType.PERMIT);
164 PermissionValue epv = new PermissionValue();
165 epv.setResourceName(res);
166 epv.setPermissionId(elmoPermId);
167 permValues.put(epv.getPermissionId(), epv);
170 private void seedRoles() {
171 String rn1 = "ROLE_TEST_CM";
172 String r1RoleId = createRole(rn1);
173 RoleValue rv1 = new RoleValue();
174 rv1.setRoleId(r1RoleId);
175 rv1.setRoleName(rn1);
176 roleValues.put(rv1.getRoleName(), rv1);
178 String rn2 = "ROLE_TEST_INTERN";
179 String r2RoleId = createRole(rn2);
180 RoleValue rv2 = new RoleValue();
181 rv2.setRoleId(r2RoleId);
182 rv2.setRoleName(rn2);
183 roleValues.put(rv2.getRoleName(), rv2);
186 private void seedAccounts() {
187 String userId1 = "bigbird2010";
188 String accId1 = createAccount(userId1, "bigbird@cspace.org");
189 AccountValue av1 = new AccountValue();
190 av1.setScreenName(userId1);
191 av1.setUserId(userId1);
192 av1.setAccountId(accId1);
193 accValues.put(av1.getUserId(), av1);
195 String userId2 = "elmo2010";
196 String accId2 = createAccount(userId2, "elmo@cspace.org");
197 AccountValue av2 = new AccountValue();
198 av2.setScreenName(userId2);
199 av2.setUserId(userId2);
200 av2.setAccountId(accId2);
201 accValues.put(av2.getUserId(), av2);
203 String userId3 = "lockedOut";
204 String accId3 = createAccount(userId3, "lockedOut@cspace.org");
205 AccountValue av3 = new AccountValue();
206 av3.setScreenName(userId3);
207 av3.setUserId(userId3);
208 av3.setAccountId(accId3);
209 accValues.put(av3.getUserId(), av3);
212 private void seedAccountRoles() {
214 List<RoleValue> bigbirdRoleValues = new ArrayList<RoleValue>();
215 bigbirdRoleValues.add(roleValues.get("ROLE_TEST_CM"));
216 createAccountRole(accValues.get("bigbird2010"), bigbirdRoleValues);
218 List<RoleValue> elmoRoleValues = new ArrayList<RoleValue>();
219 elmoRoleValues.add(roleValues.get("ROLE_TEST_INTERN"));
220 createAccountRole(accValues.get("elmo2010"), elmoRoleValues);
223 private void seedPermissionRoles() {
225 List<RoleValue> bigbirdRoleValues = new ArrayList<RoleValue>();
226 bigbirdRoleValues.add(roleValues.get("ROLE_TEST_CM"));
227 createPermissionRole(permValues.get(bigbirdPermId), bigbirdRoleValues);
229 List<RoleValue> elmoRoleValues = new ArrayList<RoleValue>();
230 elmoRoleValues.add(roleValues.get("ROLE_TEST_INTERN"));
231 createPermissionRole(permValues.get(elmoPermId), elmoRoleValues);
237 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
240 protected CollectionSpaceClient getClientInstance() {
245 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
248 protected AbstractCommonList getAbstractCommonList(
249 ClientResponse<AbstractCommonList> response) {
250 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
251 throw new UnsupportedOperationException();
254 @Test(dataProvider = "testName")
256 public void readPaginatedList(String testName) throws Exception {
257 //FIXME: http://issues.collectionspace.org/browse/CSPACE-1697
259 // ---------------------------------------------------------------
260 // CRUD tests : CREATE tests
261 // ---------------------------------------------------------------
265 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
266 public void create(String testName) throws Exception {
267 if (logger.isDebugEnabled()) {
268 logger.debug(testBanner(testName, CLASS_NAME));
272 // Submit the request to the service and store the response.
273 DimensionClient client = new DimensionClient();
274 //bigbird allowed to create
275 client.setAuth(true, "bigbird2010", true, "bigbird2010", true);
276 String identifier = createIdentifier();
277 DimensionsCommon dimension = new DimensionsCommon();
278 dimension.setDimension("dimensionType");
279 dimension.setValue("value-" + identifier);
280 dimension.setValueDate(new Date().toString());
281 PoxPayloadOut multipart = DimensionFactory.createDimensionInstance(client.getCommonPartName(),
283 ClientResponse<Response> res = client.create(multipart);
285 int statusCode = res.getStatus();
287 if (logger.isDebugEnabled()) {
288 logger.debug(testName + ": status = " + statusCode);
290 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
291 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
292 Assert.assertEquals(statusCode, Response.Status.CREATED.getStatusCode());
293 knownResourceId = extractId(res);
294 if (logger.isDebugEnabled()) {
295 logger.debug(testName + ": knownResourceId=" + knownResourceId);
298 // Now verify that elmo cannot create
299 client = new DimensionClient();
300 client.setAuth(true, "elmo2010", true, "elmo2010", true);
301 res = client.create(multipart);
303 statusCode = res.getStatus();
304 if (logger.isDebugEnabled()) {
305 logger.debug(testName + " (verify not allowed): status = " + statusCode);
307 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
308 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
309 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
311 //Finally, verify that elmo has no access to Intakes
312 // Submit the request to the service and store the response.
313 IntakeClient iclient = new IntakeClient();
314 iclient.setAuth(true, "elmo2010", true, "elmo2010", true);
315 multipart = createIntakeInstance(
316 "entryNumber-" + identifier,
317 "entryDate-" + identifier,
318 "depositor-" + identifier);
319 res = iclient.create(multipart);
320 if (logger.isDebugEnabled()) {
321 logger.debug(testName + " (verify create intake not allowed): status = " + statusCode);
323 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
324 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
325 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
330 * Creates the intake instance.
332 * @param entryNumber the entry number
333 * @param entryDate the entry date
334 * @param depositor the depositor
335 * @return the multipart output
337 private PoxPayloadOut createIntakeInstance(String entryNumber,
340 IntakesCommon intake = new IntakesCommon();
341 intake.setEntryNumber(entryNumber);
342 intake.setEntryDate(entryDate);
343 intake.setDepositor(depositor);
345 PoxPayloadOut multipart = new PoxPayloadOut(IntakeClient.SERVICE_PAYLOAD_NAME);
346 PayloadOutputPart commonPart =
347 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
348 commonPart.setLabel(new IntakeClient().getCommonPartName());
350 if(logger.isDebugEnabled()){
351 logger.debug("to be created, intake common");
352 logger.debug(objectAsXmlString(intake, IntakesCommon.class));
359 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
360 dependsOnMethods = {"delete"})
361 public void verifyCreateWithFlippedRoles(String testName) throws Exception {
362 if (logger.isDebugEnabled()) {
363 logger.debug(testBanner(testName, CLASS_NAME));
367 // Submit the request to the service and store the response.
368 DimensionClient client = new DimensionClient();
369 flipInitialAccountRoles();
371 // Now verify that elmo can create
372 client.setAuth(true, "elmo2010", true, "elmo2010", true);
373 client = new DimensionClient();
375 String identifier = createIdentifier();
376 DimensionsCommon dimension = new DimensionsCommon();
377 dimension.setDimension("dimensionType");
378 dimension.setValue("value-" + identifier);
379 dimension.setValueDate(new Date().toString());
380 PoxPayloadOut multipart = DimensionFactory.createDimensionInstance(client.getCommonPartName(),
382 ClientResponse<Response> res = client.create(multipart);
384 int statusCode = res.getStatus();
386 if (logger.isDebugEnabled()) {
387 logger.debug(testName + ": status = " + statusCode);
389 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
390 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
391 Assert.assertEquals(statusCode, Response.Status.CREATED.getStatusCode());
392 knownResourceId = extractId(res);
393 if (logger.isDebugEnabled()) {
394 logger.debug(testName + ": knownResourceId=" + knownResourceId);
397 //bigbird no longer allowed to create
398 client.setAuth(true, "bigbird2010", true, "bigbird2010", true);
399 res = client.create(multipart);
401 statusCode = res.getStatus();
402 if (logger.isDebugEnabled()) {
403 logger.debug(testName + " (verify not allowed): status = " + statusCode);
405 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
406 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
407 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
408 restoreInitialAccountRoles();
411 //to not cause uniqueness violation for permRole, createList is removed
413 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
414 dependsOnMethods = {"create"})
415 public void createList(String testName) throws Exception {
419 // Placeholders until the three tests below can be uncommented.
420 // See Issue CSPACE-401.
422 public void createWithEmptyEntityBody(String testName) throws Exception {
426 public void createWithMalformedXml(String testName) throws Exception {
430 public void createWithWrongXmlSchema(String testName) throws Exception {
433 // ---------------------------------------------------------------
434 // CRUD tests : READ tests
435 // ---------------------------------------------------------------
438 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
439 dependsOnMethods = {"create"})
440 public void read(String testName) throws Exception {
442 if (logger.isDebugEnabled()) {
443 logger.debug(testBanner(testName, CLASS_NAME));
448 // Submit the request to the service and store the response.
449 DimensionClient client = new DimensionClient();
450 //elmo allowed to read
451 client.setAuth(true, "elmo2010", true, "elmo2010", true);
452 ClientResponse<String> res = client.read(knownResourceId);
453 int statusCode = res.getStatus();
455 // Check the status code of the response: does it match
456 // the expected response(s)?
457 if (logger.isDebugEnabled()) {
458 logger.debug(testName + ": status = " + statusCode);
460 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
461 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
462 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
464 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
465 DimensionsCommon dimension = (DimensionsCommon) extractPart(input,
466 client.getCommonPartName(), DimensionsCommon.class);
467 Assert.assertNotNull(dimension);
470 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
471 dependsOnMethods = {"read"})
472 public void readLockedOut(String testName) throws Exception {
474 if (logger.isDebugEnabled()) {
475 logger.debug(testBanner(testName, CLASS_NAME));
480 // Submit the request to the service and store the response.
481 DimensionClient client = new DimensionClient();
482 //lockedOut allowed to read
483 client.setAuth(true, "lockedOut", true, "lockedOut", true);
484 ClientResponse<String> res = client.read(knownResourceId);
485 int statusCode = res.getStatus();
487 // Check the status code of the response: does it match
488 // the expected response(s)?
489 if (logger.isDebugEnabled()) {
490 logger.debug(testName + " (test lockedOut): status = " + statusCode);
492 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
493 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
494 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
499 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
500 public void readNonExistent(String testName) throws Exception {
503 setupReadNonExistent();
506 // ---------------------------------------------------------------
507 // CRUD tests : READ_LIST tests
508 // ---------------------------------------------------------------
511 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
512 dependsOnMethods = {"createList", "read"})
513 public void readList(String testName) throws Exception {
519 // ---------------------------------------------------------------
520 // CRUD tests : UPDATE tests
521 // ---------------------------------------------------------------
524 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
525 dependsOnMethods = {"read", "readList", "readNonExistent"})
526 public void update(String testName) throws Exception {
531 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
532 dependsOnMethods = {"read", "readList", "readNonExistent"})
533 public void updateNotAllowed(String testName) throws Exception {
535 if (logger.isDebugEnabled()) {
536 logger.debug(testBanner(testName, CLASS_NAME));
540 DimensionClient client = new DimensionClient();
542 //elmo not allowed to update
543 client.setAuth(true, "elmo2010", true, "elmo2010", true);
545 DimensionsCommon dimension = new DimensionsCommon();
546 dimension.setDimension("dimensionType");
547 // Update the content of this resource.
548 dimension.setValue("updated-" + dimension.getValue());
549 dimension.setValueDate("updated-" + dimension.getValueDate());
550 // Submit the request to the service and store the response.
551 PoxPayloadOut output = new PoxPayloadOut(DimensionClient.SERVICE_PAYLOAD_NAME);
552 PayloadOutputPart commonPart = output.addPart(dimension, MediaType.APPLICATION_XML_TYPE);
553 commonPart.setLabel(client.getCommonPartName());
555 ClientResponse<String> res = client.update(knownResourceId, output);
556 int statusCode = res.getStatus();
557 // Check the status code of the response: does it match the expected response(s)?
558 if (logger.isDebugEnabled()) {
559 logger.debug(testName + ": status = " + statusCode);
561 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
562 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
563 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
565 client = new DimensionClient();
567 //lockedOut not allowed to update
568 client.setAuth(true, "lockedOut", true, "lockedOut", true);
569 res = client.update(knownResourceId, output);
570 statusCode = res.getStatus();
571 // Check the status code of the response: does it match the expected response(s)?
572 if (logger.isDebugEnabled()) {
573 logger.debug(testName + ": (lockedOut) status = " + statusCode);
575 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
576 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
577 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
582 // Placeholders until the three tests below can be uncommented.
583 // See Issue CSPACE-401.
585 public void updateWithEmptyEntityBody(String testName) throws Exception {
589 public void updateWithMalformedXml(String testName) throws Exception {
593 public void updateWithWrongXmlSchema(String testName) throws Exception {
597 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
598 dependsOnMethods = {"readNonExistent", "testSubmitRequest"})
599 public void updateNonExistent(String testName) throws Exception {
602 // ---------------------------------------------------------------
603 // CRUD tests : DELETE tests
604 // ---------------------------------------------------------------
606 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
607 dependsOnMethods = {"updateNotAllowed"})
608 public void deleteNotAllowed(String testName) throws Exception {
610 if (logger.isDebugEnabled()) {
611 logger.debug(testBanner(testName, CLASS_NAME));
616 // Submit the request to the service and store the response.
617 DimensionClient client = new DimensionClient();
618 //bigbird can not delete
619 client.setAuth(true, "bigbird2010", true, "bigbird2010", true);
620 ClientResponse<Response> res = client.delete(knownResourceId);
621 int statusCode = res.getStatus();
623 // Check the status code of the response: does it match
624 // the expected response(s)?
625 if (logger.isDebugEnabled()) {
626 logger.debug(testName + ": status = " + statusCode);
628 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
629 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
630 Assert.assertEquals(statusCode, Response.Status.FORBIDDEN.getStatusCode());
635 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
636 dependsOnMethods = {"deleteNotAllowed"})
637 public void delete(String testName) throws Exception {
639 if (logger.isDebugEnabled()) {
640 logger.debug(testBanner(testName, CLASS_NAME));
645 // Submit the request to the service and store the response.
646 DimensionClient client = new DimensionClient();
648 ClientResponse<Response> res = client.delete(knownResourceId);
649 int statusCode = res.getStatus();
651 // Check the status code of the response: does it match
652 // the expected response(s)?
653 if (logger.isDebugEnabled()) {
654 logger.debug(testName + ": status = " + statusCode);
656 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
657 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
658 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
664 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
665 public void deleteNonExistent(String testName) throws Exception {
666 //ignoring this test as the service side returns 200 now even if it does
667 //not find a record in the db
670 // ---------------------------------------------------------------
671 // Utility tests : tests of code used in tests above
672 // ---------------------------------------------------------------
674 * Tests the code for manually submitting data that is used by several
675 * of the methods above.
677 @Test(dependsOnMethods = {"create"})
678 public void testSubmitRequest() throws Exception {
681 // ---------------------------------------------------------------
682 // Utility methods used by tests above
683 // ---------------------------------------------------------------
684 @AfterClass(alwaysRun = true)
685 public void cleanUp() {
687 String noTest = System.getProperty("noTestCleanup");
688 if (Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
689 if (logger.isDebugEnabled()) {
690 logger.debug("Skipping Cleanup phase ...");
694 if (logger.isDebugEnabled()) {
695 logger.debug("Cleaning up temporary resources created for testing ...");
698 deletePermissionRoles();
699 deleteAccountRoles();
700 //FIXME delete on permission deletes all associations with roles
701 //this would delete association with ROLE_ADMINISTRATOR too
702 //deletePermissions();
707 private void deletePermissionRoles() {
709 List<RoleValue> bigbirdRoleValues = new ArrayList<RoleValue>();
710 bigbirdRoleValues.add(roleValues.get("ROLE_TEST_CM"));
711 deletePermissionRole(permValues.get(bigbirdPermId), bigbirdRoleValues);
713 List<RoleValue> elmoRoleValues = new ArrayList<RoleValue>();
714 elmoRoleValues.add(roleValues.get("ROLE_TEST_INTERN"));
715 deletePermissionRole(permValues.get(elmoPermId), elmoRoleValues);
719 private void deleteAccountRoles() {
720 List<RoleValue> bigbirdRoleValues = new ArrayList<RoleValue>();
721 bigbirdRoleValues.add(roleValues.get("ROLE_TEST_CM"));
723 List<RoleValue> elmoRoleValues = new ArrayList<RoleValue>();
724 elmoRoleValues.add(roleValues.get("ROLE_TEST_INTERN"));
725 if(!accountRolesFlipped) {
726 deleteAccountRole(accValues.get("bigbird2010"), bigbirdRoleValues);
727 deleteAccountRole(accValues.get("elmo2010"), elmoRoleValues);
729 deleteAccountRole(accValues.get("bigbird2010"), elmoRoleValues);
730 deleteAccountRole(accValues.get("elmo2010"),bigbirdRoleValues );
734 private void flipInitialAccountRoles() {
735 if(!accountRolesFlipped) {
736 List<RoleValue> cmRoleValues = new ArrayList<RoleValue>();
737 List<RoleValue> internRoleValues = new ArrayList<RoleValue>();
738 cmRoleValues.add(roleValues.get("ROLE_TEST_CM"));
739 internRoleValues.add(roleValues.get("ROLE_TEST_INTERN"));
741 deleteAccountRole(accValues.get("bigbird2010"), cmRoleValues);
742 deleteAccountRole(accValues.get("elmo2010"), internRoleValues);
744 createAccountRole(accValues.get("bigbird2010"), internRoleValues);
745 createAccountRole(accValues.get("elmo2010"), cmRoleValues);
747 accountRolesFlipped = true;
751 private void restoreInitialAccountRoles() {
752 if(accountRolesFlipped) {
753 List<RoleValue> cmRoleValues = new ArrayList<RoleValue>();
754 List<RoleValue> internRoleValues = new ArrayList<RoleValue>();
755 cmRoleValues.add(roleValues.get("ROLE_TEST_CM"));
756 internRoleValues.add(roleValues.get("ROLE_TEST_INTERN"));
758 deleteAccountRole(accValues.get("bigbird2010"), internRoleValues);
759 deleteAccountRole(accValues.get("elmo2010"), cmRoleValues);
761 createAccountRole(accValues.get("bigbird2010"), internRoleValues);
762 createAccountRole(accValues.get("elmo2010"), cmRoleValues);
763 accountRolesFlipped = false;
769 private void deletePermissions() {
771 for (PermissionValue pv : permValues.values()) {
772 deletePermission(pv.getPermissionId());
776 private void deleteRoles() {
777 for (RoleValue rv : roleValues.values()) {
778 deleteRole(rv.getRoleId());
782 private void deleteAccounts() {
784 for (AccountValue av1 : accValues.values()) {
785 deleteAccount(av1.getAccountId());
789 private String createPermission(String resName, EffectType effect) {
790 List<PermissionAction> actions = PermissionFactory.createDefaultActions();
791 return createPermission(resName, actions, effect);
794 private String createPermission(String resName,
795 List<PermissionAction> actions, EffectType effect) {
797 PermissionClient permClient = new PermissionClient();
798 Permission permission = PermissionFactory.createPermissionInstance(resName,
799 "default permissions for " + resName,
800 actions, effect, true, true, true);
801 ClientResponse<Response> res = permClient.create(permission);
802 int statusCode = res.getStatus();
803 if (logger.isDebugEnabled()) {
804 logger.debug("createPermission: resName=" + resName
805 + " status = " + statusCode);
807 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
808 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
809 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
810 res.releaseConnection();
811 return extractId(res);
814 private void deletePermission(String permId) {
816 PermissionClient permClient = new PermissionClient();
817 ClientResponse<Response> res = permClient.delete(permId);
818 int statusCode = res.getStatus();
819 if (logger.isDebugEnabled()) {
820 logger.debug("deletePermission: delete permission id="
821 + permId + " status=" + statusCode);
823 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
824 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
825 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
826 res.releaseConnection();
829 private String createRole(String roleName) {
831 RoleClient roleClient = new RoleClient();
833 Role role = RoleFactory.createRoleInstance(roleName,
834 roleName, //the display name
835 "role for " + roleName, true);
836 ClientResponse<Response> res = roleClient.create(role);
837 int statusCode = res.getStatus();
838 if (logger.isDebugEnabled()) {
839 logger.debug("createRole: name=" + roleName
840 + " status = " + statusCode);
842 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
843 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
844 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
845 res.releaseConnection();
846 return extractId(res);
849 private void deleteRole(String roleId) {
851 RoleClient roleClient = new RoleClient();
852 ClientResponse<Response> res = roleClient.delete(roleId);
853 int statusCode = res.getStatus();
854 if (logger.isDebugEnabled()) {
855 logger.debug("deleteRole: delete role id=" + roleId
856 + " status=" + statusCode);
858 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
859 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
860 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
861 res.releaseConnection();
864 private String createAccount(String userName, String email) {
866 AccountClient accountClient = new AccountClient();
867 AccountsCommon account = AccountFactory.createAccountInstance(
868 userName, userName, userName, email, accountClient.getTenantId(),
869 true, false, true, true);
870 ClientResponse<Response> res = accountClient.create(account);
871 int statusCode = res.getStatus();
872 if (logger.isDebugEnabled()) {
873 logger.debug("createAccount: userName=" + userName
874 + " status = " + statusCode);
876 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
877 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
878 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
879 res.releaseConnection();
880 return extractId(res);
883 private void deleteAccount(String accId) {
885 AccountClient accClient = new AccountClient();
886 ClientResponse<Response> res = accClient.delete(accId);
887 int statusCode = res.getStatus();
888 if (logger.isDebugEnabled()) {
889 logger.debug("deleteAccount: delete account id="
890 + accId + " status=" + statusCode);
892 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
893 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
894 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
895 res.releaseConnection();
898 private String createAccountRole(AccountValue av,
899 Collection<RoleValue> rvs) {
902 // Submit the request to the service and store the response.
903 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
904 av, rvs, true, true);
905 AccountRoleClient client = new AccountRoleClient();
906 ClientResponse<Response> res = client.create(av.getAccountId(), accRole);
907 int statusCode = res.getStatus();
909 if (logger.isDebugEnabled()) {
910 logger.debug("createAccountRole: status = " + statusCode);
912 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
913 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
914 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
915 res.releaseConnection();
916 return extractId(res);
919 private void deleteAccountRole(AccountValue av,
920 Collection<RoleValue> rvs) {
924 // Submit the request to the service and store the response.
925 AccountRoleClient client = new AccountRoleClient();
926 AccountRole accRole = AccountRoleFactory.createAccountRoleInstance(
927 av, rvs, true, true);
928 ClientResponse<Response> res = client.delete(
930 int statusCode = res.getStatus();
932 // Check the status code of the response: does it match
933 // the expected response(s)?
934 if (logger.isDebugEnabled()) {
935 logger.debug("deleteAccountRole: status = " + statusCode);
937 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
938 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
939 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
940 res.releaseConnection();
943 private String createPermissionRole(PermissionValue pv,
944 Collection<RoleValue> rvs) {
946 List<RoleValue> rvls = new ArrayList<RoleValue>();
948 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
949 pv, rvls, true, true);
950 PermissionRoleClient client = new PermissionRoleClient();
951 ClientResponse<Response> res = client.create(pv.getPermissionId(), permRole);
952 int statusCode = res.getStatus();
954 if (logger.isDebugEnabled()) {
955 logger.debug("createPermissionRole: status = " + statusCode);
957 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
958 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
959 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
960 res.releaseConnection();
961 return extractId(res);
964 private void deletePermissionRole(PermissionValue pv,
965 Collection<RoleValue> rvs) {
966 List<RoleValue> rvls = new ArrayList<RoleValue>();
972 // Submit the request to the service and store the response.
973 PermissionRoleClient client = new PermissionRoleClient();
974 PermissionRole permRole = PermissionRoleFactory.createPermissionRoleInstance(
975 pv, rvls, true, true);
976 ClientResponse<Response> res = client.delete(pv.getPermissionId());
977 int statusCode = res.getStatus();
979 // Check the status code of the response: does it match
980 // the expected response(s)?
981 if (logger.isDebugEnabled()) {
982 logger.debug("deletePermissionRole : status = " + statusCode);
984 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
985 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
986 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
987 res.releaseConnection();