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 26, 2025
1 parent 328a3dd commit 07d2c21
Showing 1 changed file with 88 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import uk.gov.hmcts.darts.cases.mapper.CasesAnnotationMapper;
import uk.gov.hmcts.darts.cases.mapper.CasesMapper;
import uk.gov.hmcts.darts.cases.model.AddCaseRequest;
import uk.gov.hmcts.darts.cases.model.AdminSingleCaseResponseItem;
import uk.gov.hmcts.darts.cases.model.Event;
import uk.gov.hmcts.darts.cases.model.GetCasesRequest;
import uk.gov.hmcts.darts.cases.model.Hearing;
Expand Down Expand Up @@ -75,41 +76,41 @@
@SuppressWarnings({"PMD.VariableDeclarationUsageDistance", "PMD.ExcessiveImports", "PMD.AvoidDuplicateLiterals"})
class CaseServiceImplTest {

public static final String SWANSEA = "SWANSEA";
public static final String TEST_COURT_CASE = "CASE_COURTHOUSE";
public static final OffsetDateTime FIXED_DATETIME = OffsetDateTime.of(2024, 3, 25, 10, 0, 0, 0, ZoneOffset.UTC);
private static final String SWANSEA = "SWANSEA";
private static final String TEST_COURT_CASE = "CASE_COURTHOUSE";
private static final OffsetDateTime FIXED_DATETIME = OffsetDateTime.of(2024, 3, 25, 10, 0, 0, 0, ZoneOffset.UTC);

CaseServiceImpl service;
private CaseServiceImpl caseService;

CasesMapper mapper;
CasesAnnotationMapper annotationMapper;
private CasesMapper casesMapper;
private CasesAnnotationMapper annotationMapper;

@Mock
CaseRepository caseRepository;
private CaseRepository caseRepository;

@Mock
HearingRepository hearingRepository;
private HearingRepository hearingRepository;

@Mock
EventRepository eventRepository;
private EventRepository eventRepository;

@Mock
HearingReportingRestrictionsRepository hearingReportingRestrictionsRepository;
private HearingReportingRestrictionsRepository hearingReportingRestrictionsRepository;

@Mock
RetrieveCoreObjectService retrieveCoreObjectService;
private RetrieveCoreObjectService retrieveCoreObjectService;

@Mock
AdvancedSearchRequestHelper advancedSearchRequestHelper;
private AdvancedSearchRequestHelper advancedSearchRequestHelper;
@Mock
AdminCasesSearchRequestHelper adminCasesSearchRequestHelper;
private AdminCasesSearchRequestHelper adminCasesSearchRequestHelper;

@Mock
TranscriptionRepository transcriptionRepository;
private TranscriptionRepository transcriptionRepository;
@Mock
TranscriptionDocumentRepository transcriptionDocumentRepository;
private TranscriptionDocumentRepository transcriptionDocumentRepository;
@Captor
ArgumentCaptor<CourtCaseEntity> caseEntityArgumentCaptor;
private ArgumentCaptor<CourtCaseEntity> caseEntityArgumentCaptor;

@Mock
private CaseRetentionRepository caseRetentionRepository;
Expand All @@ -127,11 +128,11 @@ class CaseServiceImplTest {
@BeforeEach
void setUp() {
Pattern unallocatedCaseRegex = Pattern.compile(".*\\d{8}-\\d{6}.*");
mapper = new CasesMapper(retrieveCoreObjectService, hearingReportingRestrictionsRepository, caseRetentionRepository, authorisationApi, logApi,
unallocatedCaseRegex);
casesMapper = new CasesMapper(retrieveCoreObjectService, hearingReportingRestrictionsRepository, caseRetentionRepository, authorisationApi, logApi,
unallocatedCaseRegex);

service = new CaseServiceImpl(
mapper,
caseService = new CaseServiceImpl(
casesMapper,
annotationMapper,
hearingRepository,
eventRepository,
Expand All @@ -156,7 +157,7 @@ void testGetCasesById() throws Exception {
courtCaseEntity.setHearings(hearings);
when(caseRepository.findById(any())).thenReturn(Optional.of(courtCaseEntity));

SingleCase result = service.getCasesById(101);
SingleCase result = caseService.getCasesById(101);

String actualResponse = objectMapper.writeValueAsString(result);

Expand All @@ -172,7 +173,7 @@ void testGetCasesByIdHearingsNotActual() {

when(caseRepository.findById(any())).thenReturn(Optional.of(courtCaseEntity));

DartsApiException exception = assertThrows(DartsApiException.class, () -> service.getCasesById(101));
DartsApiException exception = assertThrows(DartsApiException.class, () -> caseService.getCasesById(101));

assertEquals("CASE_107", exception.getError().getErrorTypeNumeric());
}
Expand All @@ -192,7 +193,7 @@ void testGetCasesWithMultipleHearing() throws IOException {
request.setCourtroom("1");
request.setDate(LocalDate.of(2023, 6, 20));

List<ScheduledCase> resultList = service.getHearings(request);
List<ScheduledCase> resultList = caseService.getHearings(request);
String actualResponse = objectMapper.writeValueAsString(resultList);
String expectedResponse = getContentsFromFile(
"Tests/cases/CaseServiceTest/testGetCasesWithMultipleHearing/expectedResponse.json");
Expand All @@ -215,7 +216,7 @@ void testGetCasesWithSingleHearingAndDifferentCourtroom() throws IOException {
request.setCourtroom("2");
request.setDate(LocalDate.of(2023, 6, 20));

List<ScheduledCase> resultList = service.getHearings(request);
List<ScheduledCase> resultList = caseService.getHearings(request);
String actualResponse = objectMapper.writeValueAsString(resultList);
String expectedResponse = getContentsFromFile(
"Tests/cases/CaseServiceTest/testGetCasesWithSingleHearingAndDifferentCourtroom/expectedResponse.json");
Expand Down Expand Up @@ -243,7 +244,7 @@ void testGetCasesCreateCourtroom() {
userAccount.setId(10);
when(authorisationApi.getCurrentUser()).thenReturn(userAccount);

service.getHearings(request);
caseService.getHearings(request);
verify(retrieveCoreObjectService).retrieveOrCreateCourtroom(eq(SWANSEA), eq("99"), any(UserAccountEntity.class));
verify(logApi, times(1)).casesRequestedByDarPc(request);
}
Expand All @@ -261,7 +262,7 @@ void testAddCase() throws IOException {

AddCaseRequest request = CommonTestDataUtil.createAddCaseRequest();

PostCaseResponse result = service.addCaseOrUpdate(request);
PostCaseResponse result = caseService.addCaseOrUpdate(request);

String actualResponse = TestUtils.removeTags(List.of("case_id"), objectMapper.writeValueAsString(result));
String expectedResponse = getContentsFromFile(
Expand Down Expand Up @@ -297,7 +298,7 @@ void testAddCaseNonExistingCourthouse() {

DartsApiException thrownException = assertThrows(
DartsApiException.class,
() -> service.addCaseOrUpdate(request)
() -> caseService.addCaseOrUpdate(request)
);

assertEquals("Provided courthouse does not exist", thrownException.getMessage());
Expand All @@ -319,7 +320,7 @@ void testGetCaseHearingsWhenHearingIsActualTrue() {

when(hearingRepository.findByCaseIds(List.of(existingCaseEntity.getId()))).thenReturn(existingHearings);

List<Hearing> caseHearings = service.getCaseHearings(existingCaseEntity.getId());
List<Hearing> caseHearings = caseService.getCaseHearings(existingCaseEntity.getId());

assertEquals(existingHearings.get(0).getId(), caseHearings.get(0).getId());
assertEquals(existingHearings.get(0).getCourtroom().getName(), caseHearings.get(0).getCourtroom());
Expand All @@ -343,7 +344,7 @@ void testGetCaseHearingsWhenHearingIsActualFalse() {

when(hearingRepository.findByCaseIds(List.of(existingCaseEntity.getId()))).thenReturn(existingHearings);

List<Hearing> caseHearings = service.getCaseHearings(existingCaseEntity.getId());
List<Hearing> caseHearings = caseService.getCaseHearings(existingCaseEntity.getId());

assertEquals(0, caseHearings.size());
}
Expand All @@ -353,7 +354,7 @@ void testGetCaseHearingsWhenCaseDoesNotExistThrowsException() {
when(hearingRepository.findByCaseIds(any())).thenReturn(Collections.emptyList());

var exception = assertThrows(DartsApiException.class, () ->
service.getCaseHearings(1));
caseService.getCaseHearings(1));

assertEquals("The requested case cannot be found", exception.getMessage());
}
Expand All @@ -367,7 +368,7 @@ void testGetCaseHearingsWhenCaseIsExpiredThrowsException() {

when(caseRepository.findById(existingCaseEntity.getId())).thenReturn(Optional.of(existingCaseEntity));
var exception = assertThrows(DartsApiException.class, () ->
service.getCaseHearings(1));
caseService.getCaseHearings(1));

assertThat(exception.getMessage()).isEqualTo("Case has expired.");
assertThat(exception.getError()).isEqualTo(CaseApiError.CASE_EXPIRED);
Expand All @@ -393,7 +394,7 @@ void testGetEventsByCaseId() throws Exception {

when(eventRepository.findAllByCaseId(courtCaseEntity.getId())).thenReturn(events);
when(caseRepository.findById(courtCaseEntity.getId())).thenReturn(Optional.of(courtCaseEntity));
List<Event> result = service.getEventsByCaseId(courtCaseEntity.getId());
List<Event> result = caseService.getEventsByCaseId(courtCaseEntity.getId());

String actualResponse = objectMapper.writeValueAsString(result);

Expand All @@ -408,13 +409,13 @@ void testGetEventsByCaseIdIsAnonymous() {
courtCaseEntity.setDataAnonymised(true);
when(caseRepository.findById(courtCaseEntity.getId())).thenReturn(Optional.of(courtCaseEntity));

DartsApiException exception = assertThrows(DartsApiException.class, () -> service.getEventsByCaseId(courtCaseEntity.getId()));
DartsApiException exception = assertThrows(DartsApiException.class, () -> caseService.getEventsByCaseId(courtCaseEntity.getId()));
assertThat(exception.getMessage()).isEqualTo("Case has expired.");
assertThat(exception.getError()).isEqualTo(CaseApiError.CASE_EXPIRED);
}

@Test
void testUpdateCaseWithNonExistingCourtroomAndMatchingHearingDate() {
void updateCase_WithNonExistingCourtroomAndMatchingHearingDate() {
CourthouseEntity courthouseEntity = CommonTestDataUtil.createCourthouse(SWANSEA);
CourtCaseEntity existingCaseEntity = CommonTestDataUtil.createCase("case1", courthouseEntity);
existingCaseEntity.setId(1);
Expand All @@ -429,7 +430,7 @@ void testUpdateCaseWithNonExistingCourtroomAndMatchingHearingDate() {
});

AddCaseRequest request = CommonTestDataUtil.createUpdateCaseRequest();
service.addCaseOrUpdate(request);
caseService.addCaseOrUpdate(request);

verify(caseRepository).saveAndFlush(caseEntityArgumentCaptor.capture());

Expand All @@ -445,7 +446,7 @@ void testUpdateCaseWithNonExistingCourtroomAndMatchingHearingDate() {
}

@Test
void testUpdateCaseWithMultipleHearingsWithOldHearingDateWithCourtroomInRequest() {
void updateCase_WithMultipleHearingsWithOldHearingDateWithCourtroomInRequest() {
CourthouseEntity courthouseEntity = CommonTestDataUtil.createCourthouse(SWANSEA);
CourtCaseEntity existingCaseEntity = CommonTestDataUtil.createCase("case1", courthouseEntity);
existingCaseEntity.setId(1);
Expand All @@ -461,7 +462,7 @@ void testUpdateCaseWithMultipleHearingsWithOldHearingDateWithCourtroomInRequest(
});

AddCaseRequest request = CommonTestDataUtil.createUpdateCaseRequest();
service.addCaseOrUpdate(request);
caseService.addCaseOrUpdate(request);

verify(caseRepository).saveAndFlush(caseEntityArgumentCaptor.capture());

Expand All @@ -475,7 +476,7 @@ void testUpdateCaseWithMultipleHearingsWithOldHearingDateWithCourtroomInRequest(
}

@Test
void testUpdateCaseWithMultipleHearingsWithCourtroomInRequest() {
void updateCase_WithMultipleHearingsWithCourtroomInRequest() {
CourthouseEntity courthouseEntity = CommonTestDataUtil.createCourthouse(SWANSEA);
CourtCaseEntity existingCaseEntity = CommonTestDataUtil.createCase("case1", courthouseEntity);
existingCaseEntity.setId(1);
Expand All @@ -491,7 +492,7 @@ void testUpdateCaseWithMultipleHearingsWithCourtroomInRequest() {
});

AddCaseRequest request = CommonTestDataUtil.createUpdateCaseRequest();
service.addCaseOrUpdate(request);
caseService.addCaseOrUpdate(request);

verify(caseRepository).saveAndFlush(caseEntityArgumentCaptor.capture());

Expand All @@ -505,7 +506,8 @@ void testUpdateCaseWithMultipleHearingsWithCourtroomInRequest() {
}

@Test
void testUpdateCaseWithMultipleHearingsWithoutCourtroomInRequest() {
void updateCase_WithMultipleHearingsWithoutCourtroomInRequest() {
// given
CourthouseEntity courthouseEntity = CommonTestDataUtil.createCourthouse(SWANSEA);
CourtCaseEntity existingCaseEntity = CommonTestDataUtil.createCase("case1", courthouseEntity);

Expand All @@ -519,8 +521,11 @@ void testUpdateCaseWithMultipleHearingsWithoutCourtroomInRequest() {
});

AddCaseRequest request = CommonTestDataUtil.createUpdateCaseRequest();
service.addCaseOrUpdate(request);

// when
caseService.addCaseOrUpdate(request);

// then
verify(caseRepository).saveAndFlush(caseEntityArgumentCaptor.capture());
verifyNoInteractions(hearingRepository);

Expand All @@ -534,6 +539,48 @@ void testUpdateCaseWithMultipleHearingsWithoutCourtroomInRequest() {

}

@Test
void adminGetCaseById_ShouldReturnCase_WhenCaseExists() {
// given
List<HearingEntity> hearings = CommonTestDataUtil.createHearings(1);
CourtCaseEntity courtCaseEntity = hearings.getFirst().getCourtCase();
courtCaseEntity.setHearings(hearings);

when(caseRepository.findById(1)).thenReturn(Optional.of(courtCaseEntity));

// when
AdminSingleCaseResponseItem result = caseService.adminGetCaseById(1);

// then
assertNotNull(result);
assertEquals(courtCaseEntity.getId(), result.getId());
assertEquals(courtCaseEntity.getCourthouse().getId(), result.getCourthouse().getId());
assertEquals(courtCaseEntity.getCourthouse().getDisplayName(), result.getCourthouse().getDisplayName());
assertEquals(courtCaseEntity.getCaseNumber(), result.getCaseNumber());
assertEquals(2, result.getDefendants().size());
assertEquals(2, result.getJudges().size());
assertEquals(2, result.getProsecutors().size());
assertEquals(2, result.getDefenders().size());
assertEquals(0, result.getReportingRestrictions().size());
assertEquals(Boolean.FALSE, result.getIsDataAnonymised());

verify(caseRepository, times(1)).findById(1);
}

@Test
void adminGetCaseById_ShouldThrowException_WhenCaseDoesNotExist() {
// given
when(caseRepository.findById(1)).thenReturn(Optional.empty());

// when
DartsApiException exception = assertThrows(DartsApiException.class, () -> caseService.adminGetCaseById(1));

// then
assertEquals("CASE_104", exception.getError().getErrorTypeNumeric());
assertEquals("The requested case cannot be found", exception.getMessage());
verify(caseRepository, times(1)).findById(1);
}

private HearingEntity createHearingEntity() {
CourthouseEntity courthouseEntity = CommonTestDataUtil.createCourthouse(SWANSEA);
CourtroomEntity courtroomEntity = CommonTestDataUtil.createCourtroom(courthouseEntity, "2");
Expand Down

0 comments on commit 07d2c21

Please sign in to comment.