From e924a736bd186a91baa7fa7712fedef8892bb187 Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Wed, 5 Oct 2011 22:17:03 +0000 Subject: [PATCH] CSPACE-3442: Added /roles/{csid}/accountroles/ resource to the "authorization" service. Use this method to get a list of accounts that are associated to a specified role. --- .../client/test/AccountRoleServiceTest.java | 66 +++++++++++++++++++ .../services/client/RoleClient.java | 5 ++ .../services/client/RoleProxy.java | 6 ++ .../services/authorization/RoleResource.java | 22 +++++++ 4 files changed, 99 insertions(+) diff --git a/services/account/client/src/test/java/org/collectionspace/services/account/client/test/AccountRoleServiceTest.java b/services/account/client/src/test/java/org/collectionspace/services/account/client/test/AccountRoleServiceTest.java index 159e0f404..0a1485124 100644 --- a/services/account/client/src/test/java/org/collectionspace/services/account/client/test/AccountRoleServiceTest.java +++ b/services/account/client/src/test/java/org/collectionspace/services/account/client/test/AccountRoleServiceTest.java @@ -348,6 +348,72 @@ public class AccountRoleServiceTest extends AbstractServiceTestImpl { res.releaseConnection(); } } + + /* + * In this test, for setup, we associate both test roles ("ROLE_CO1", "ROLE_CO2") with the test account "acc-role-user2". + * After we've performed this setup, our call to "/role/{csid}/accountroles" should contain an AccountRole that has + * a list of 1 account -the test user account we associated during setup. + */ + @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, + dependsOnMethods = {"delete"}) + public void readRoleAccounts(String testName) throws Exception { + + if (logger.isDebugEnabled()) { + testBanner(testName, CLASS_NAME); + } + + /* + * Setup a temp local scope for local variables that we need to create the AccountRole for + * the setup of the read tests. + */ + { + setupCreate(); + + // Associate "acc-role-user2" with all the roles. + AccountValue av = accValues.get("acc-role-user2"); + AccountRole accRole = createAccountRoleInstance(av, + roleValues.values(), true, true); + AccountRoleClient client = new AccountRoleClient(); + ClientResponse res = client.create(av.getAccountId(), accRole); + int statusCode = res.getStatus(); + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + } + + // + // Now read the list of accounts associated with the role "ROLE_CO1". + // There should be just the "acc-role-user2" account. + // + setupRead(); + RoleClient roleClient = new RoleClient(); + + // Submit the request to the service and store the response. + ClientResponse res = roleClient.readRoleAccounts( + roleValues.get("ROLE_CO1").getRoleId()); + int statusCode = res.getStatus(); + try { + // Check the status code of the response: does it match + // the expected response(s)? + if (logger.isDebugEnabled()) { + logger.debug(testName + ": status = " + statusCode); + } + Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode), + invalidStatusCodeMessage(REQUEST_TYPE, statusCode)); + Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE); + AccountRole output = res.getEntity(); + + // Now verify that the role has 2 accounts associate to it. + Assert.assertEquals(output.getAccounts().size(), 1); + + String sOutput = objectAsXmlString(output, AccountRole.class); + if(logger.isDebugEnabled()) { + logger.debug(testName + " received " + sOutput); + } + } finally { + res.releaseConnection(); + } + } // --------------------------------------------------------------- // CRUD tests : READ_LIST tests diff --git a/services/authorization-mgt/client/src/main/java/org/collectionspace/services/client/RoleClient.java b/services/authorization-mgt/client/src/main/java/org/collectionspace/services/client/RoleClient.java index 1d888e918..78d1f48de 100644 --- a/services/authorization-mgt/client/src/main/java/org/collectionspace/services/client/RoleClient.java +++ b/services/authorization-mgt/client/src/main/java/org/collectionspace/services/client/RoleClient.java @@ -29,6 +29,7 @@ package org.collectionspace.services.client; import javax.ws.rs.core.Response; +import org.collectionspace.services.authorization.AccountRole; import org.collectionspace.services.authorization.Role; import org.collectionspace.services.authorization.RolesList; import org.jboss.resteasy.client.ClientResponse; @@ -71,6 +72,10 @@ public class RoleClient extends AbstractServiceClientImpl public ClientResponse read(String csid) { return getProxy().read(csid); } + + public ClientResponse readRoleAccounts(String csid) { + return getProxy().readRoleAccounts(csid); + } /** * Creates the. diff --git a/services/authorization-mgt/client/src/main/java/org/collectionspace/services/client/RoleProxy.java b/services/authorization-mgt/client/src/main/java/org/collectionspace/services/client/RoleProxy.java index 8360bee95..176b764f4 100644 --- a/services/authorization-mgt/client/src/main/java/org/collectionspace/services/client/RoleProxy.java +++ b/services/authorization-mgt/client/src/main/java/org/collectionspace/services/client/RoleProxy.java @@ -37,6 +37,7 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; +import org.collectionspace.services.authorization.AccountRole; import org.collectionspace.services.authorization.Role; import org.collectionspace.services.authorization.RolesList; import org.jboss.resteasy.client.ClientResponse; @@ -64,6 +65,11 @@ public interface RoleProxy extends CollectionSpaceProxy { @GET @Path("/{csid}") ClientResponse read(@PathParam("csid") String csid); + + //(R)ead accounts associate with this role + @GET + @Path("{csid}/accountroles") + ClientResponse readRoleAccounts(@PathParam("csid") String csid); //(U)pdate @PUT diff --git a/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/RoleResource.java b/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/RoleResource.java index 7a8d04bc9..8bbd027a9 100644 --- a/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/RoleResource.java +++ b/services/authorization-mgt/service/src/main/java/org/collectionspace/services/authorization/RoleResource.java @@ -95,6 +95,28 @@ public class RoleResource extends SecurityResourceBase { public Role getRole(@PathParam("csid") String csid) { return (Role)get(csid, Role.class); } + + /* + * Get a list of accounts associated with this role. + */ + @GET + @Path("{csid}/accountroles") + public AccountRole getRoleAccounts( + @PathParam("csid") String accCsid) { + logger.debug("getAccountRole with accCsid=" + accCsid); + ensureCSID(accCsid, ServiceMessages.GET_FAILED+ "accountroles role "); + AccountRole result = null; + try { + AccountRoleSubResource subResource = + new AccountRoleSubResource(AccountRoleSubResource.ACCOUNT_ACCOUNTROLE_SERVICE); + //get relationships for a role + result = subResource.getAccountRole(accCsid, SubjectType.ACCOUNT); + } catch (Exception e) { + throw bigReThrow(e, ServiceMessages.GET_FAILED, accCsid); + } + checkResult(result, accCsid, ServiceMessages.GET_FAILED); + return result; + } @GET @Produces("application/xml") -- 2.47.3