Skip to content

Commit

Permalink
Dmp 3375 implement admin endpoint get case details by (#2625)
Browse files Browse the repository at this point in the history
  • Loading branch information
karen-hedges authored Mar 3, 2025
1 parent 475e6e8 commit 304c6e9
Show file tree
Hide file tree
Showing 18 changed files with 884 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package uk.gov.hmcts.darts.cases.controller;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import uk.gov.hmcts.darts.authorisation.component.UserIdentity;
import uk.gov.hmcts.darts.common.entity.CourtCaseEntity;
import uk.gov.hmcts.darts.common.entity.CourthouseEntity;
import uk.gov.hmcts.darts.common.entity.CourtroomEntity;
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.UserAccountEntity;
import uk.gov.hmcts.darts.common.util.DateConverterUtil;
import uk.gov.hmcts.darts.test.common.data.PersistableFactory;
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.time.format.DateTimeFormatter;
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.status;
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;
import static uk.gov.hmcts.darts.test.common.data.EventTestData.createEventWith;
import static uk.gov.hmcts.darts.test.common.data.JudgeTestData.createJudgeWithName;
import static uk.gov.hmcts.darts.test.common.data.ProsecutorTestData.createProsecutorForCase;

@AutoConfigureMockMvc
class CaseControllerAdminGetCaseByIdIntTest extends IntegrationBase {

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

private static final OffsetDateTime SOME_DATE_TIME = OffsetDateTime.parse("2023-01-01T12:00Z");
private static final String SOME_COURTHOUSE = "SOME-COURTHOUSE";
private static final String SOME_COURTROOM = "some-courtroom";
private static final String SOME_CASE_NUMBER = "1";
private static final OffsetDateTime DATE_TIME = OffsetDateTime.parse("2023-06-26T13:00:00Z");

@Autowired
private MockMvc mockMvc;

@Autowired
private SuperAdminUserStub superAdminUserStub;
@Autowired
private UserAccountStub accountStub;
@MockitoBean
private UserIdentity mockUserIdentity;

@BeforeEach
void setupData() {

CourthouseEntity swanseaCourthouse = someMinimalCourthouse();
swanseaCourthouse.setCourthouseName("SWANSEA");
swanseaCourthouse.setDisplayName("SWANSEA");

CourtCaseEntity case1 = PersistableFactory.getCourtCaseTestData().createCaseAt(swanseaCourthouse);
case1.setCaseNumber("Case1");

JudgeEntity judge = createJudgeWithName("aJudge");
CourtroomEntity courtroom1 = createCourtRoomWithNameAtCourthouse(swanseaCourthouse, "courtroom1");
HearingEntity hearing1a = PersistableFactory.getHearingTestData().createHearingWithDefaults(case1, courtroom1, LocalDate.of(2023, 5, 20), judge);
HearingEntity hearing1b = PersistableFactory.getHearingTestData().createHearingWithDefaults(case1, courtroom1, LocalDate.of(2023, 5, 21), judge);
HearingEntity hearing1c = PersistableFactory.getHearingTestData().createHearingWithDefaults(case1, courtroom1, LocalDate.of(2023, 5, 22), judge);

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

@Test
void adminGetCaseById_ShouldReturnForbiddenError() throws Exception {
// given
UserAccountEntity accountEntity = accountStub.createJudgeUser();
when(mockUserIdentity.getUserAccount()).thenReturn(accountEntity);

// when
MockHttpServletRequestBuilder requestBuilder = get(ENDPOINT_URL, getCaseId(SOME_CASE_NUMBER, SOME_COURTHOUSE));

// then
mockMvc.perform(requestBuilder).andExpect(status().isForbidden());
}

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

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

List<OffsetDateTime> eventDateTimes = new ArrayList<>();
eventDateTimes.add(DATE_TIME);
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));
courtCase.addDefence("aDefence");
courtCase = dartsDatabase.save(courtCase);

MockHttpServletRequestBuilder requestBuilder = get(ENDPOINT_URL, courtCase.getId());

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

// then
String actualResponse = mvcResult.getResponse().getContentAsString();
String expectedResponse = getContentsFromFile(
"tests/cases/CaseControllerAdminGetCaseByIdTest/testCaseOpen/expectedResponse.json");
expectedResponse = expectedResponse.replace("<CREATED_AT>", courtCase.getCreatedDateTime().format(DateTimeFormatter.ISO_DATE_TIME));
expectedResponse = expectedResponse.replace("<LAST_MODIFIED_AT>", courtCase.getLastModifiedDateTime().format(DateTimeFormatter.ISO_DATE_TIME));
JSONAssert.assertEquals(expectedResponse, actualResponse, JSONCompareMode.NON_EXTENSIBLE);

}

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

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

List<OffsetDateTime> eventDateTimes = new ArrayList<>();
eventDateTimes.add(DATE_TIME);
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));
courtCase.addDefence("aDefence");
courtCase.setClosed(true);
courtCase.setCaseClosedTimestamp(DATE_TIME);
courtCase = dartsDatabase.save(courtCase);

MockHttpServletRequestBuilder requestBuilder = get(ENDPOINT_URL, courtCase.getId());

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

// then
String actualResponse = mvcResult.getResponse().getContentAsString();
String expectedResponse = getContentsFromFile(
"tests/cases/CaseControllerAdminGetCaseByIdTest/testCaseClosed/expectedResponse.json");
expectedResponse = expectedResponse.replace("<CREATED_AT>", courtCase.getCreatedDateTime().format(DateTimeFormatter.ISO_DATE_TIME));
expectedResponse = expectedResponse.replace("<LAST_MODIFIED_AT>", courtCase.getLastModifiedDateTime().format(DateTimeFormatter.ISO_DATE_TIME));
JSONAssert.assertEquals(expectedResponse, actualResponse, JSONCompareMode.NON_EXTENSIBLE);

}

@Test
void adminGetCaseById_IsAnonymised() throws Exception {
// given
superAdminUserStub.givenUserIsAuthorised(mockUserIdentity);
HearingEntity hearingEntity = dartsDatabase.givenTheDatabaseContainsCourtCaseWithHearingAndCourthouseWithRoom(
"123",
SOME_COURTHOUSE,
SOME_COURTROOM,
DateConverterUtil.toLocalDateTime(SOME_DATE_TIME)
);
CourtCaseEntity courtCase = hearingEntity.getCourtCase();
courtCase.setDataAnonymised(true);
OffsetDateTime dataAnonymisedTs = OffsetDateTime.parse("2023-01-01T12:00:00Z");
courtCase.setDataAnonymisedTs(dataAnonymisedTs);
courtCase.addProsecutor(createProsecutorForCase(courtCase));
courtCase.addDefendant(createDefendantForCase(courtCase));
courtCase.addDefence("aDefence");
courtCase = dartsDatabase.save(courtCase);

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

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

// then
String actualResponse = mvcResult.getResponse().getContentAsString();
String expectedResponse = getContentsFromFile(
"tests/cases/CaseControllerAdminGetCaseByIdTest/testIsAnonymised/expectedResponse.json");
expectedResponse = expectedResponse.replace("<CREATED_AT>", courtCase.getCreatedDateTime().format(DateTimeFormatter.ISO_DATE_TIME));
expectedResponse = expectedResponse.replace("<LAST_MODIFIED_AT>", courtCase.getLastModifiedDateTime().format(DateTimeFormatter.ISO_DATE_TIME));
JSONAssert.assertEquals(expectedResponse, actualResponse, JSONCompareMode.NON_EXTENSIBLE);
}

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

// when
mockMvc.perform(get(ENDPOINT_URL, 25))
.andExpect(status().isNotFound());
}

private Integer getCaseId(String caseNumber, String courthouse) {
return dartsDatabase.createCase(courthouse, caseNumber).getId();
}

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 @@ -53,9 +53,9 @@ class CaseControllerAdminSearchTest extends IntegrationBase {
@Autowired
UserAccountRepository userAccountRepository;
@Autowired
private transient MockMvc mockMvc;
CourthouseEntity swanseaCourthouse;
UserAccountEntity user;
private MockMvc mockMvc;
private CourthouseEntity swanseaCourthouse;
private UserAccountEntity user;

@BeforeEach
void setupData() {
Expand Down Expand Up @@ -89,21 +89,21 @@ void setupData() {

HearingEntity hearing1b = PersistableFactory.getHearingTestData().createHearingWithDefaults(case1, courtroom1, LocalDate.of(2023, 5, 21), judge);

HearingEntity hearing1c = PersistableFactory.getHearingTestData().createHearingWithDefaults(case1, courtroom1, LocalDate.of(2023, 5, 22), judge);
HearingEntity hearing1c = PersistableFactory.getHearingTestData().createHearingWithDefaults(case1, courtroom1, LocalDate.of(2023, 5, 22), judge);

HearingEntity hearing2a = PersistableFactory.getHearingTestData().createHearingWithDefaults(case2, courtroom1, LocalDate.of(2023, 6, 20), judge);
HearingEntity hearing2a = PersistableFactory.getHearingTestData().createHearingWithDefaults(case2, courtroom1, LocalDate.of(2023, 6, 20), judge);

HearingEntity hearing2b = PersistableFactory.getHearingTestData().createHearingWithDefaults(case2, courtroom1, LocalDate.of(2023, 6, 21), judge);
HearingEntity hearing2b = PersistableFactory.getHearingTestData().createHearingWithDefaults(case2, courtroom1, LocalDate.of(2023, 6, 21), judge);

HearingEntity hearing2c = PersistableFactory.getHearingTestData().createHearingWithDefaults(case2, courtroom1, LocalDate.of(2023, 6, 22), judge);
HearingEntity hearing2c = PersistableFactory.getHearingTestData().createHearingWithDefaults(case2, courtroom1, LocalDate.of(2023, 6, 22), judge);

HearingEntity hearing3a = PersistableFactory.getHearingTestData().createHearingWithDefaults(case3, courtroom1, LocalDate.of(2023, 7, 20), judge);
HearingEntity hearing3a = PersistableFactory.getHearingTestData().createHearingWithDefaults(case3, courtroom1, LocalDate.of(2023, 7, 20), judge);
JudgeEntity judge3a = createJudgeWithName("Judge3a");
hearing3a.addJudge(judge3a, false);

HearingEntity hearing3b = PersistableFactory.getHearingTestData().createHearingWithDefaults(case3, courtroom1, LocalDate.of(2023, 7, 21), judge);
HearingEntity hearing3b = PersistableFactory.getHearingTestData().createHearingWithDefaults(case3, courtroom1, LocalDate.of(2023, 7, 21), judge);

HearingEntity hearing3c = PersistableFactory.getHearingTestData().createHearingWithDefaults(case3, courtroom1, LocalDate.of(2023, 7, 22), judge);
HearingEntity hearing3c = PersistableFactory.getHearingTestData().createHearingWithDefaults(case3, courtroom1, LocalDate.of(2023, 7, 22), judge);

CourtroomEntity courtroom2 = createCourtRoomWithNameAtCourthouse(swanseaCourthouse, "courtroom2");

Expand All @@ -118,7 +118,7 @@ void setupData() {
HearingEntity hearing5b = PersistableFactory.getHearingTestData().createHearingWithDefaults(case5, courtroom1, LocalDate.of(2023, 9, 21), judge);

CourtroomEntity courtroom3 = createCourtRoomWithNameAtCourthouse(swanseaCourthouse, "courtroom3");
HearingEntity hearing5c = PersistableFactory.getHearingTestData().createHearingWithDefaults(case5, courtroom3, LocalDate.of(2023, 9, 22), judge);
HearingEntity hearing5c = PersistableFactory.getHearingTestData().createHearingWithDefaults(case5, courtroom3, LocalDate.of(2023, 9, 22), judge);

HearingEntity hearing6a = PersistableFactory.getHearingTestData().createHearingWithDefaults(case6, courtroom2, LocalDate.of(2023, 9, 20), judge);

Expand All @@ -143,8 +143,6 @@ void setupData() {
givenBearerTokenExists(INTEGRATION_TEST_USER_EMAIL);
user = dartsDatabase.getUserAccountStub().getIntegrationTestUserAccountEntity();
setupUserAccountAndSecurityGroup();


}

@Test
Expand Down
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
Expand Up @@ -985,7 +985,7 @@ void givenConfiguredTaskCancelArmRetentionEventDateCalculatorAutomatedTask() {
}

@Test
void givenConfiguredTaskInboundTranscriptionAndAnnotationDeleterAutomatedTask() throws Exception {
void givenConfiguredTaskInboundTranscriptionAndAnnotationDeleterAutomatedTask() {
Set<ScheduledTask> scheduledTasks = scheduledTaskHolder.getScheduledTasks();
displayTasks(scheduledTasks);

Expand All @@ -1000,7 +1000,7 @@ void givenConfiguredTaskInboundTranscriptionAndAnnotationDeleterAutomatedTask()
}

@Test
void givenConfiguredTaskUnstructuredTranscriptionAndAnnotationDeleterAutomatedTask() throws Exception {
void givenConfiguredTaskUnstructuredTranscriptionAndAnnotationDeleterAutomatedTask() {
Set<ScheduledTask> scheduledTasks = scheduledTaskHolder.getScheduledTasks();
displayTasks(scheduledTasks);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"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_closed_date_time": "2023-06-26T13:00:00Z",
"case_status": "CLOSED",
"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
}
Loading

0 comments on commit 304c6e9

Please sign in to comment.