generated from hmcts/spring-boot-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dmp 3375 implement admin endpoint get case details by (#2625)
- Loading branch information
1 parent
475e6e8
commit 304c6e9
Showing
18 changed files
with
884 additions
and
69 deletions.
There are no files selected for viewing
239 changes: 239 additions & 0 deletions
239
...nTest/java/uk/gov/hmcts/darts/cases/controller/CaseControllerAdminGetCaseByIdIntTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...urces/tests/cases/CaseControllerAdminGetCaseByIdTest/testCaseClosed/expectedResponse.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.