From 4fd0b7e6e0dcf99634db88600e0b3eaca2c9a2ff Mon Sep 17 00:00:00 2001 From: GarethLancaster <31533575+Gareth40342@users.noreply.github.com> Date: Mon, 24 Jun 2024 12:35:35 +0100 Subject: [PATCH] Added assign roles post api call to role assignment api and service classes --- .../civil/ras/client/RoleAssignmentsApi.java | 14 ++++++ .../reform/civil/ras/model/GrantType.java | 9 ++++ .../civil/ras/model/RoleAssignment.java | 50 +++++++++++++++++++ .../ras/model/RoleAssignmentRequest.java | 21 ++++++++ .../reform/civil/ras/model/RoleCategory.java | 12 +++++ .../reform/civil/ras/model/RoleRequest.java | 19 +++++++ .../reform/civil/ras/model/RoleType.java | 6 +++ .../civil/service/RoleAssignmentsService.java | 9 ++++ .../service/RoleAssignmentsServiceTest.java | 44 +++++++++++++++- 9 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 src/main/java/uk/gov/hmcts/reform/civil/ras/model/GrantType.java create mode 100644 src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleAssignment.java create mode 100644 src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleAssignmentRequest.java create mode 100644 src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleCategory.java create mode 100644 src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleRequest.java create mode 100644 src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleType.java diff --git a/src/main/java/uk/gov/hmcts/reform/civil/ras/client/RoleAssignmentsApi.java b/src/main/java/uk/gov/hmcts/reform/civil/ras/client/RoleAssignmentsApi.java index 04a10631..9ba6ad12 100644 --- a/src/main/java/uk/gov/hmcts/reform/civil/ras/client/RoleAssignmentsApi.java +++ b/src/main/java/uk/gov/hmcts/reform/civil/ras/client/RoleAssignmentsApi.java @@ -3,7 +3,10 @@ import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; +import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentRequest; import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentServiceResponse; import static org.springframework.http.HttpHeaders.AUTHORIZATION; @@ -26,4 +29,15 @@ RoleAssignmentServiceResponse getRoleAssignments( @RequestHeader(SERVICE_AUTHORIZATION) String serviceAuthorization, @PathVariable(ACTOR_ID) String actorId); + @PostMapping( + value = "/am/role-assignments", + consumes = APPLICATION_JSON_VALUE, + headers = CONTENT_TYPE + "=" + APPLICATION_JSON_VALUE + ) + RoleAssignmentServiceResponse createRoleAssignment( + @RequestHeader(AUTHORIZATION) String authorisation, + @RequestHeader(SERVICE_AUTHORIZATION) String serviceAuthorization, + @RequestBody RoleAssignmentRequest request + ); + } diff --git a/src/main/java/uk/gov/hmcts/reform/civil/ras/model/GrantType.java b/src/main/java/uk/gov/hmcts/reform/civil/ras/model/GrantType.java new file mode 100644 index 00000000..c6f3e0c7 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/civil/ras/model/GrantType.java @@ -0,0 +1,9 @@ +package uk.gov.hmcts.reform.civil.ras.model; + +public enum GrantType { + BASIC, + SPECIFIC, + STANDARD, + CHALLENGED, + EXCLUDED +} diff --git a/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleAssignment.java b/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleAssignment.java new file mode 100644 index 00000000..1cdc04d9 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleAssignment.java @@ -0,0 +1,50 @@ +package uk.gov.hmcts.reform.civil.ras.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +import java.time.ZonedDateTime; +import java.util.List; +import java.util.Map; + +@Data +@Builder +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +@AllArgsConstructor +public class RoleAssignment { + + private String id; + + @Builder.Default + private String actorIdType = "IDAM"; + + private Map attributes; + + private List authorisations; + + private List notes; + + private ZonedDateTime beginTime; + + private ZonedDateTime endTime; + + private ZonedDateTime created; + + @Builder.Default + private String status = "CREATE_REQUESTED"; + + @Builder.Default + private String classification = "PUBLIC"; + + private String actorId; + private GrantType grantType; + private RoleCategory roleCategory; + private String roleName; + private RoleType roleType; + private boolean readOnly; + +} diff --git a/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleAssignmentRequest.java b/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleAssignmentRequest.java new file mode 100644 index 00000000..b91bbe40 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleAssignmentRequest.java @@ -0,0 +1,21 @@ +package uk.gov.hmcts.reform.civil.ras.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.extern.jackson.Jacksonized; + +import java.util.List; + +@Data +@Builder +@NoArgsConstructor +@Jacksonized +@AllArgsConstructor +public class RoleAssignmentRequest { + + private List requestedRoles; + private RoleRequest roleRequest; + +} diff --git a/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleCategory.java b/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleCategory.java new file mode 100644 index 00000000..7e789121 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleCategory.java @@ -0,0 +1,12 @@ +package uk.gov.hmcts.reform.civil.ras.model; + +public enum RoleCategory { + JUDICIAL, + LEGAL_OPERATIONS, + ADMIN, + PROFESSIONAL, + CITIZEN, + SYSTEM, + OTHER_GOV_DEPT, + CTSC +} diff --git a/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleRequest.java b/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleRequest.java new file mode 100644 index 00000000..62751b75 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleRequest.java @@ -0,0 +1,19 @@ +package uk.gov.hmcts.reform.civil.ras.model; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class RoleRequest { + + private String assignerId; + + @Builder.Default + private String process = "fpl-case-service"; + private String reference; + + @Builder.Default + private boolean replaceExisting = false; + +} diff --git a/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleType.java b/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleType.java new file mode 100644 index 00000000..07cc876b --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleType.java @@ -0,0 +1,6 @@ +package uk.gov.hmcts.reform.civil.ras.model; + +public enum RoleType { + CASE, + ORGANISATION +} diff --git a/src/main/java/uk/gov/hmcts/reform/civil/service/RoleAssignmentsService.java b/src/main/java/uk/gov/hmcts/reform/civil/service/RoleAssignmentsService.java index 3d9a2a45..580c3f3e 100644 --- a/src/main/java/uk/gov/hmcts/reform/civil/service/RoleAssignmentsService.java +++ b/src/main/java/uk/gov/hmcts/reform/civil/service/RoleAssignmentsService.java @@ -5,6 +5,7 @@ import org.springframework.stereotype.Service; import uk.gov.hmcts.reform.authorisation.generators.AuthTokenGenerator; import uk.gov.hmcts.reform.civil.ras.client.RoleAssignmentsApi; +import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentRequest; import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentServiceResponse; @Service @@ -28,4 +29,12 @@ public RoleAssignmentServiceResponse getRoleAssignments(String actorId, actorId ); } + + public void assignUserRoles(String actorId, String authorization, RoleAssignmentRequest roleAssignmentRequest) { + if (log.isDebugEnabled()) { + log.debug(actorId, "Assigning roles to actorId {0}"); + } + + roleAssignmentApi.createRoleAssignment(authorization, authTokenGenerator.generate(), roleAssignmentRequest); + } } diff --git a/src/test/java/uk/gov/hmcts/reform/civil/service/RoleAssignmentsServiceTest.java b/src/test/java/uk/gov/hmcts/reform/civil/service/RoleAssignmentsServiceTest.java index 188750eb..a4b4116e 100644 --- a/src/test/java/uk/gov/hmcts/reform/civil/service/RoleAssignmentsServiceTest.java +++ b/src/test/java/uk/gov/hmcts/reform/civil/service/RoleAssignmentsServiceTest.java @@ -9,14 +9,24 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; import uk.gov.hmcts.reform.authorisation.generators.AuthTokenGenerator; import uk.gov.hmcts.reform.civil.ras.client.RoleAssignmentsApi; +import uk.gov.hmcts.reform.civil.ras.model.GrantType; +import uk.gov.hmcts.reform.civil.ras.model.RoleAssignment; +import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentRequest; import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentResponse; import uk.gov.hmcts.reform.civil.ras.model.RoleAssignmentServiceResponse; +import uk.gov.hmcts.reform.civil.ras.model.RoleCategory; +import uk.gov.hmcts.reform.civil.ras.model.RoleRequest; +import uk.gov.hmcts.reform.civil.ras.model.RoleType; import java.util.List; +import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.clearInvocations; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @ExtendWith(SpringExtension.class) @@ -26,6 +36,7 @@ class RoleAssignmentsServiceTest { private static final String USER_AUTH_TOKEN = "Bearer caa-user-xyz"; + private static final String SERVICE_TOKEN = "Bearer service-token"; private static final String ACTORID = "1111111"; private static RoleAssignmentServiceResponse RAS_RESPONSE = RoleAssignmentServiceResponse .builder() @@ -50,14 +61,43 @@ class RoleAssignmentsServiceTest { @BeforeEach void init() { clearInvocations(authTokenGenerator); - when(authTokenGenerator.generate()).thenReturn(USER_AUTH_TOKEN); + when(authTokenGenerator.generate()).thenReturn(SERVICE_TOKEN); when(roleAssignmentApi.getRoleAssignments(anyString(), anyString(), anyString())).thenReturn(RAS_RESPONSE); } @Test void shouldReturn() { - var roleAssignmentsExpected = roleAssignmentsService.getRoleAssignments(ACTORID, authTokenGenerator.generate()); + var roleAssignmentsExpected = roleAssignmentsService.getRoleAssignments(ACTORID, USER_AUTH_TOKEN); assertEquals(roleAssignmentsExpected, RAS_RESPONSE); } + @Test + void shouldPostExpectedPayload() { + RoleAssignmentRequest request = RoleAssignmentRequest.builder() + .roleRequest(RoleRequest.builder() + .assignerId(ACTORID) + .reference("civil-hearings-system-user") + .process("civil-system-user") + .replaceExisting(true) + .build()) + .requestedRoles(List.of( + RoleAssignment.builder() + .actorId(ACTORID) + .actorIdType("IDAM") + .roleType(RoleType.ORGANISATION) + .classification("PUBLIC") + .grantType(GrantType.STANDARD) + .roleCategory(RoleCategory.SYSTEM) + .roleName("some-role") + .attributes(Map.of("jurisdiction", "CIVIL", "caseType", "CIVIL")) + .readOnly(false) + .build() + )).build(); + + roleAssignmentsService.assignUserRoles(ACTORID, USER_AUTH_TOKEN, request); + + verify(roleAssignmentApi, times(1)) + .createRoleAssignment(eq(USER_AUTH_TOKEN), eq(SERVICE_TOKEN), eq(request)); + } + }