Skip to content

Commit

Permalink
Added assign roles post api call to role assignment api and service c…
Browse files Browse the repository at this point in the history
…lasses
  • Loading branch information
Gareth40342 committed Jun 24, 2024
1 parent 80f3b74 commit 4fd0b7e
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package uk.gov.hmcts.reform.civil.ras.model;

public enum GrantType {
BASIC,
SPECIFIC,
STANDARD,
CHALLENGED,
EXCLUDED
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> attributes;

private List<String> authorisations;

private List<String> 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;

}
Original file line number Diff line number Diff line change
@@ -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<RoleAssignment> requestedRoles;
private RoleRequest roleRequest;

}
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions src/main/java/uk/gov/hmcts/reform/civil/ras/model/RoleRequest.java
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package uk.gov.hmcts.reform.civil.ras.model;

public enum RoleType {
CASE,
ORGANISATION
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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));
}

}

0 comments on commit 4fd0b7e

Please sign in to comment.