Skip to content

Commit

Permalink
DMP-3375 Implement admin endpoint to get case details by id
Browse files Browse the repository at this point in the history
Added admin endpoint to get case details
  • Loading branch information
karen-hedges committed Feb 21, 2025
1 parent ccdb3b6 commit 03933c0
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package uk.gov.hmcts.darts.cases.controller;

import lombok.extern.slf4j.Slf4j;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
Expand All @@ -19,25 +18,23 @@
import uk.gov.hmcts.darts.common.entity.EventEntity;
import uk.gov.hmcts.darts.common.entity.HearingEntity;
import uk.gov.hmcts.darts.common.entity.JudgeEntity;
import uk.gov.hmcts.darts.common.entity.SecurityGroupEntity;
import uk.gov.hmcts.darts.common.entity.UserAccountEntity;
import uk.gov.hmcts.darts.common.repository.SecurityGroupRepository;
import uk.gov.hmcts.darts.common.repository.UserAccountRepository;
import uk.gov.hmcts.darts.common.util.DateConverterUtil;
import uk.gov.hmcts.darts.test.common.data.PersistableFactory;
import uk.gov.hmcts.darts.test.common.data.SecurityGroupTestData;
import uk.gov.hmcts.darts.testutils.IntegrationBase;
import uk.gov.hmcts.darts.testutils.stubs.SuperAdminUserStub;
import uk.gov.hmcts.darts.testutils.stubs.UserAccountStub;

import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;

import static java.util.stream.IntStream.rangeClosed;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static uk.gov.hmcts.darts.common.enums.SecurityRoleEnum.SUPER_ADMIN;
import static uk.gov.hmcts.darts.test.common.TestUtils.getContentsFromFile;
import static uk.gov.hmcts.darts.test.common.data.CourthouseTestData.someMinimalCourthouse;
import static uk.gov.hmcts.darts.test.common.data.CourtroomTestData.createCourtRoomWithNameAtCourthouse;
import static uk.gov.hmcts.darts.test.common.data.DefendantTestData.createDefendantForCase;
Expand All @@ -47,7 +44,7 @@

@Slf4j
@AutoConfigureMockMvc
class CaseControllerAdminGetCaseByIdTest extends IntegrationBase {
class CaseControllerAdminGetCaseByIdIntTest extends IntegrationBase {

private static final String endpointUrl = "/admin/cases/{id}";

Expand All @@ -61,27 +58,19 @@ class CaseControllerAdminGetCaseByIdTest extends IntegrationBase {
@Autowired
private transient MockMvc mockMvc;

@Autowired
private SecurityGroupRepository securityGroupRepository;
@Autowired
private UserAccountRepository userAccountRepository;
@Autowired
private SuperAdminUserStub superAdminUserStub;
@Autowired
private UserAccountStub accountStub;
@MockitoBean
private UserIdentity mockUserIdentity;

private UserAccountEntity user;

private CourthouseEntity swanseaCourthouse;


@BeforeEach
void setupData() {

superAdminUserStub.givenUserIsAuthorised(mockUserIdentity);

swanseaCourthouse = someMinimalCourthouse();
swanseaCourthouse.setCourthouseName("SWANSEA");
swanseaCourthouse.setDisplayName("SWANSEA");
Expand All @@ -102,8 +91,6 @@ void setupData() {

EventEntity event1 = createEventWith("eventName", "event1", hearing1a, OffsetDateTime.now());
dartsDatabase.save(event1);

//givenBearerTokenExists(INTEGRATION_TEST_USER_EMAIL);
}

@Test
Expand All @@ -122,12 +109,22 @@ void adminGetCaseById_ShouldReturnForbiddenError() throws Exception {
@Test
void adminGetCaseById_Success() throws Exception {
// given
superAdminUserStub.givenUserIsAuthorised(mockUserIdentity);

hearingEntity = dartsDatabase.givenTheDatabaseContainsCourtCaseWithHearingAndCourthouseWithRoom(
"123",
SOME_COURTHOUSE,
SOME_COURTROOM,
DateConverterUtil.toLocalDateTime(SOME_DATE_TIME)
);

List<OffsetDateTime> eventDateTimes = new ArrayList<>();
eventDateTimes.add(OffsetDateTime.parse("2023-06-26T13:00:00Z"));
var reportingRestrictions = createEventsWithDifferentTimestamps(eventDateTimes).stream()
.map(eve -> dartsDatabase.addHandlerToEvent(eve, 54))
.toList();
hearingEntity = dartsDatabase.saveEventsForHearing(hearingEntity, reportingRestrictions);

CourtCaseEntity courtCase = hearingEntity.getCourtCase();
courtCase.addProsecutor(createProsecutorForCase(courtCase));
courtCase.addDefendant(createDefendantForCase(courtCase));
Expand All @@ -141,45 +138,19 @@ void adminGetCaseById_Success() throws Exception {

// then
String actualResponse = mvcResult.getResponse().getContentAsString();
String expectedResponse = getContentsFromFile(
"tests/cases/CaseControllerAdminGetCaseByIdTest/testOk/expectedResponse.json");
expectedResponse = expectedResponse.replace("<CREATED_AT>", courtCase.getCreatedDateTime().toString());
expectedResponse = expectedResponse.replace("<LAST_MODIFIED_AT>", courtCase.getLastModifiedDateTime().toString());
log.info("actualResponse: {}", actualResponse);
// String expectedResponse = getContentsFromFile(
// "tests/cases/CaseControllerAdminGetCaseByIdTest/testOk/expectedResponse.json");
// expectedResponse = expectedResponse.replace("<CREATED_AT>", courtCase.getCreatedDateTime().toString());
// expectedResponse = expectedResponse.replace("<LAST_MODIFIED_AT>", courtCase.getLastModifiedDateTime().toString());
// assertEquals(expectedResponse, actualResponse, JSONCompareMode.NON_EXTENSIBLE);

mvcResult.getResponse().
/*
"id": 2,
"courthouse": {
"id": 2,
"display_name": "SOME-COURTHOUSE"
},
"case_number": "123",
"defendants": [
"some-defendant"
],
"judges": [
"123JUDGE1"
],
"prosecutors": [
"some-prosecutor"
],
"defenders": [
"aDefence"
],
"reporting_restrictions": [],
"case_status": "OPEN",
"created_by": 0,
"last_modified_by": 0,
"is_deleted": false,
"is_data_anonymised": false,
"is_interpreter_used": false
*/
JSONAssert.assertEquals(expectedResponse, actualResponse, JSONCompareMode.NON_EXTENSIBLE);

}

@Test
void adminGetCaseById_IsAnonymised() throws Exception {
// given
superAdminUserStub.givenUserIsAuthorised(mockUserIdentity);
hearingEntity = dartsDatabase.givenTheDatabaseContainsCourtCaseWithHearingAndCourthouseWithRoom(
"123",
SOME_COURTHOUSE,
Expand All @@ -193,56 +164,30 @@ void adminGetCaseById_IsAnonymised() throws Exception {
courtCase.addProsecutor(createProsecutorForCase(courtCase));
courtCase.addDefendant(createDefendantForCase(courtCase));
courtCase.addDefence("aDefence");
dartsDatabase.save(courtCase);
courtCase = dartsDatabase.save(courtCase);

MockHttpServletRequestBuilder requestBuilder = get(endpointUrl, getCaseId("123", SOME_COURTHOUSE));

// when
MvcResult mvcResult = mockMvc.perform(requestBuilder).andExpect(status().isOk()).andReturn();

String actualJson = mvcResult.getResponse().getContentAsString();
String expectedJson = """
{"case_id":<case-id>,
"courthouse_id":<courthouse-id>,
"courthouse":"SOME-COURTHOUSE",
"case_number":"123",
"defendants":["some-defendant"],
"judges":["123JUDGE1"],
"prosecutors":["some-prosecutor"],
"defenders":["aDefence"],
"reporting_restrictions":[],
"is_data_anonymised":true,
"data_anonymised_at":"2023-01-01T12:00:00Z"
}
""";

expectedJson = expectedJson.replace("<case-id>", hearingEntity.getCourtCase().getId().toString());
expectedJson = expectedJson.replace("<courthouse-id>", hearingEntity.getCourtCase().getCourthouse().getId().toString());
JSONAssert.assertEquals(expectedJson, actualJson, JSONCompareMode.NON_EXTENSIBLE);
}

@Test
void adminGetCaseById_CheckListsAreCorrectSize() throws Exception {
setupData();
final Integer caseId = getCaseId(SOME_CASE_NUMBER, SOME_COURTHOUSE);
MockHttpServletRequestBuilder requestBuilder = get(endpointUrl, caseId);

mockMvc.perform(requestBuilder)
.andExpect(status().isOk())
.andExpect(jsonPath("$.case_id", Matchers.is(caseId)))
.andExpect(jsonPath("$.judges", Matchers.hasSize(1)))
.andExpect(jsonPath("$.judges[0]", Matchers.is("1JUDGE1")))
.andExpect(jsonPath("$.prosecutors", Matchers.hasSize(1)))
.andExpect(jsonPath("$.prosecutors[0]", Matchers.is("some-prosecutor")))
.andExpect(jsonPath("$.defendants", Matchers.hasSize(1)))
.andExpect(jsonPath("$.defendants[0]", Matchers.is("some-defendant")))
.andExpect(jsonPath("$.defenders", Matchers.hasSize(1)))
.andExpect(jsonPath("$.defenders[0]", Matchers.is("aDefence")));

// then
String actualResponse = mvcResult.getResponse().getContentAsString();
String expectedResponse = getContentsFromFile(
"tests/cases/CaseControllerAdminGetCaseByIdTest/testIsAnonymised/expectedResponse.json");
expectedResponse = expectedResponse.replace("<CREATED_AT>", courtCase.getCreatedDateTime().toString());
expectedResponse = expectedResponse.replace("<LAST_MODIFIED_AT>", courtCase.getLastModifiedDateTime().toString());
log.info("actualResponse: {}", actualResponse);
log.info("expectResponse: {}", expectedResponse);
JSONAssert.assertEquals(expectedResponse, actualResponse, JSONCompareMode.NON_EXTENSIBLE);
}

@Test
void adminGetCaseById_CaseNotFound() throws Exception {
setupData();
// given
superAdminUserStub.givenUserIsAuthorised(mockUserIdentity);

// when
mockMvc.perform(get(endpointUrl, 25))
.andExpect(status().isNotFound());
}
Expand All @@ -251,17 +196,14 @@ private Integer getCaseId(String caseNumber, String courthouse) {
return dartsDatabase.createCase(courthouse, caseNumber).getId();
}

private void setupUserAccountAndSecurityGroup() {
var securityGroup = SecurityGroupTestData.createGroupForRole(SUPER_ADMIN);
securityGroup.setGlobalAccess(true);
securityGroup.setUseInterpreter(false);
assignSecurityGroupToUser(user, securityGroup);
}

private void assignSecurityGroupToUser(UserAccountEntity user, SecurityGroupEntity securityGroup) {
securityGroup.getUsers().add(user);
user.getSecurityGroupEntities().add(securityGroup);
securityGroupRepository.save(securityGroup);
userAccountRepository.save(user);
private List<EventEntity> createEventsWithDifferentTimestamps(List<OffsetDateTime> eventDateTimes) {
return rangeClosed(1, eventDateTimes.size())
.mapToObj(index -> {
var event = dartsDatabase.getEventStub().createDefaultEvent();
event.setEventText("some-event-text-" + index);
event.setMessageId("some-message-id-" + index);
event.setTimestamp(eventDateTimes.get(index - 1));
return event;
}).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ private void checkAnnotation(Annotation annotation, HearingEntity hearingEntity,
assertEquals("some text", annotation.getAnnotationText());
assertEquals(hearingEntity.getId(), annotation.getHearingId());
assertEquals(1, annotation.getAnnotationDocuments().size());
assertEquals("a filename", annotation.getAnnotationDocuments().get(0).getFileName());
assertEquals("DOC", annotation.getAnnotationDocuments().get(0).getFileType());
assertEquals(user.getUserFullName(), annotation.getAnnotationDocuments().get(0).getUploadedBy());
assertEquals("a filename", annotation.getAnnotationDocuments().getFirst().getFileName());
assertEquals("DOC", annotation.getAnnotationDocuments().getFirst().getFileType());
assertEquals(user.getUserFullName(), annotation.getAnnotationDocuments().getFirst().getUploadedBy());
}

private AnnotationEntity createAnnotation(UserAccountEntity user, HearingEntity hearingEntity, boolean deleted) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"id": 2,
"courthouse": {
"id": 2,
"display_name": "SOME-COURTHOUSE"
},
"case_number": "123",
"defendants": [
"some-defendant"
],
"judges": [
"123JUDGE1"
],
"prosecutors": [
"some-prosecutor"
],
"defenders": [
"aDefence"
],
"reporting_restrictions": [],
"case_status": "OPEN",
"created_at": "<CREATED_AT>",
"created_by": 0,
"last_modified_at": "<LAST_MODIFIED_AT>",
"last_modified_by": 0,
"is_deleted": false,
"is_data_anonymised": true,
"data_anonymised_at": "2023-01-01T12:00:00Z",
"is_interpreter_used": false
}
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
{"id":2,"courthouse":{"id":2,"display_name":"SOME-COURTHOUSE"},"case_number":"123","defendants":["some-defendant"],"judges":["123JUDGE1"],"prosecutors":["some-prosecutor"],"defenders":["aDefence"],"reporting_restrictions":[],"case_status":"OPEN","created_at":"<CREATED_AT>","created_by":0,"last_modified_at":"<LAST_MODIFIED_AT>","last_modified_by":0,"is_deleted":false,"is_data_anonymised":false,"is_interpreter_used":false}
{
"id": 2,
"courthouse": {
"id": 2,
"display_name": "SOME-COURTHOUSE"
},
"case_number": "123",
"defendants": [
"some-defendant"
],
"judges": [
"123JUDGE1"
],
"prosecutors": [
"some-prosecutor"
],
"defenders": [
"aDefence"
],
"reporting_restrictions": [
{
"hearing_id": 4,
"event_id": 2,
"event_name": "Judge directed on reporting restrictions",
"event_text": "some-event-text-1",
"event_ts": "2023-06-26T13:00:00Z"
}
],
"case_status": "OPEN",
"created_at": "<CREATED_AT>",
"created_by": 0,
"last_modified_at": "<LAST_MODIFIED_AT>",
"last_modified_by": 0,
"is_deleted": false,
"is_data_anonymised": false,
"is_interpreter_used": false
}

0 comments on commit 03933c0

Please sign in to comment.