Skip to content

Commit f148dd7

Browse files
committed
feat: mocked response moved under InMemoryCourtScheduleRepositoryImpl;
1 parent bdb3b25 commit f148dd7

File tree

7 files changed

+139
-47
lines changed

7 files changed

+139
-47
lines changed

src/main/java/uk/gov/hmcts/cp/controllers/CourtScheduleController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public ResponseEntity<CourtScheduleResponse> getCourtScheduleByCaseUrn(String ca
2727
CourtScheduleResponse courtScheduleResponse;
2828
try {
2929
sanitizedCaseUrn = sanitizeCaseUrn(caseUrn);
30-
courtScheduleResponse = courtScheduleService.getCourtScheduleResponse(sanitizedCaseUrn);
30+
courtScheduleResponse = courtScheduleService.getCourtScheduleByCaseUrn(sanitizedCaseUrn);
3131
} catch (ResponseStatusException e) {
3232
log.error(e.getMessage());
3333
throw e;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package uk.gov.hmcts.cp.repositories;
2+
3+
import org.springframework.stereotype.Repository;
4+
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse;
5+
6+
@Repository
7+
public interface CourtScheduleRepository {
8+
9+
CourtScheduleResponse getCourtScheduleByCaseUrn(String caseUrn);
10+
11+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package uk.gov.hmcts.cp.repositories;
2+
3+
import org.springframework.stereotype.Component;
4+
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse;
5+
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponseCourtScheduleInner;
6+
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponseCourtScheduleInnerHearingsInner;
7+
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponseCourtScheduleInnerHearingsInnerCourtSittingsInner;
8+
9+
import java.time.OffsetDateTime;
10+
import java.util.List;
11+
import java.util.UUID;
12+
13+
@Component
14+
public class InMemoryCourtScheduleRepositoryImpl implements CourtScheduleRepository {
15+
16+
public CourtScheduleResponse getCourtScheduleByCaseUrn(String caseUrn) {
17+
CourtScheduleResponseCourtScheduleInnerHearingsInner courtScheduleHearing = CourtScheduleResponseCourtScheduleInnerHearingsInner.builder()
18+
.hearingId(UUID.randomUUID().toString())
19+
.listNote("Requires interpreter")
20+
.hearingDescription("Sentencing for theft case")
21+
.hearingType("Trial")
22+
.courtSittings(List.of(
23+
CourtScheduleResponseCourtScheduleInnerHearingsInnerCourtSittingsInner.builder()
24+
.courtHouse("Central Criminal Court")
25+
.sittingStart(OffsetDateTime.now())
26+
.sittingEnd(OffsetDateTime.now().plusMinutes(60))
27+
.judiciaryId(UUID.randomUUID().toString())
28+
.build())
29+
).build();
30+
31+
return CourtScheduleResponse.builder()
32+
.courtSchedule(List.of(
33+
CourtScheduleResponseCourtScheduleInner.builder()
34+
.hearings(List.of(courtScheduleHearing)
35+
).build()
36+
)
37+
).build();
38+
}
39+
40+
}
Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,32 @@
11
package uk.gov.hmcts.cp.services;
22

3+
import lombok.RequiredArgsConstructor;
34
import org.apache.commons.lang3.StringUtils;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
67
import org.springframework.http.HttpStatus;
78
import org.springframework.stereotype.Service;
89
import org.springframework.web.server.ResponseStatusException;
910
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse;
10-
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponseCourtScheduleInner;
11-
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponseCourtScheduleInnerHearingsInner;
12-
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponseCourtScheduleInnerHearingsInnerCourtSittingsInner;
13-
14-
import java.time.OffsetDateTime;
15-
import java.util.List;
16-
import java.util.UUID;
11+
import uk.gov.hmcts.cp.repositories.CourtScheduleRepository;
1712

1813
@Service
14+
@RequiredArgsConstructor
1915
public class CourtScheduleService {
2016

2117
private static final Logger log = LoggerFactory.getLogger(CourtScheduleService.class);
2218

23-
public CourtScheduleResponse getCourtScheduleResponse(String caseUrn) throws ResponseStatusException {
19+
private final CourtScheduleRepository courtScheduleRepository;
20+
21+
public CourtScheduleResponse getCourtScheduleByCaseUrn(String caseUrn) throws ResponseStatusException {
2422
if (StringUtils.isEmpty(caseUrn)) {
2523
log.warn("No case urn provided");
2624
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "caseUrn is required");
2725
}
2826
log.warn("NOTE: System configured to return stubbed Court Schedule details. Ignoring provided caseUrn : {}", caseUrn);
29-
CourtScheduleResponse stubbedCourtScheduleResponse = CourtScheduleResponse.builder()
30-
.courtSchedule(List.of(
31-
CourtScheduleResponseCourtScheduleInner.builder()
32-
.hearings(List.of(
33-
CourtScheduleResponseCourtScheduleInnerHearingsInner.builder()
34-
.hearingId(UUID.randomUUID().toString())
35-
.listNote("Requires interpreter")
36-
.hearingDescription("Sentencing for theft case")
37-
.hearingType("Trial")
38-
.courtSittings(List.of(
39-
CourtScheduleResponseCourtScheduleInnerHearingsInnerCourtSittingsInner.builder()
40-
.courtHouse("Central Criminal Court")
41-
.sittingStart(OffsetDateTime.now())
42-
.sittingEnd(OffsetDateTime.now().plusMinutes(60))
43-
.judiciaryId(UUID.randomUUID().toString())
44-
.build())
45-
).build()
46-
)
47-
).build()
48-
)
49-
).build();
27+
CourtScheduleResponse stubbedCourtScheduleResponse = courtScheduleRepository.getCourtScheduleByCaseUrn(caseUrn);
5028
log.debug("Court Schedule response: {}", stubbedCourtScheduleResponse);
5129
return stubbedCourtScheduleResponse;
5230
}
31+
5332
}

src/test/java/uk/gov/hmcts/cp/controllers/CourtScheduleControllerTest.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,35 @@
1111
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponseCourtScheduleInner;
1212
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponseCourtScheduleInnerHearingsInner;
1313
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponseCourtScheduleInnerHearingsInnerCourtSittingsInner;
14+
import uk.gov.hmcts.cp.repositories.CourtScheduleRepository;
15+
import uk.gov.hmcts.cp.repositories.InMemoryCourtScheduleRepositoryImpl;
1416
import uk.gov.hmcts.cp.services.CourtScheduleService;
1517

1618
import java.util.UUID;
1719

18-
import static org.junit.jupiter.api.Assertions.*;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
1925

2026
class CourtScheduleControllerTest {
2127

2228
private static final Logger log = LoggerFactory.getLogger(CourtScheduleControllerTest.class);
2329

30+
private CourtScheduleRepository courtScheduleRepository;
31+
private CourtScheduleService courtScheduleService;
32+
private CourtScheduleController courtScheduleController;
33+
2434
@BeforeEach
2535
void setUp() {
36+
courtScheduleRepository = new InMemoryCourtScheduleRepositoryImpl();
37+
courtScheduleService = new CourtScheduleService(courtScheduleRepository);
38+
courtScheduleController = new CourtScheduleController(courtScheduleService);
2639
}
2740

2841
@Test
2942
void getJudgeById_ShouldReturnJudgesWithOkStatus() {
30-
CourtScheduleController courtScheduleController = new CourtScheduleController(new CourtScheduleService());
3143
UUID caseUrn = UUID.randomUUID();
3244
log.info("Calling courtScheduleController.getCourtScheduleByCaseUrn with caseUrn: {}", caseUrn);
3345
ResponseEntity<?> response = courtScheduleController.getCourtScheduleByCaseUrn(caseUrn.toString());
@@ -63,8 +75,6 @@ void getJudgeById_ShouldReturnJudgesWithOkStatus() {
6375

6476
@Test
6577
void getCourtScheduleByCaseUrn_ShouldSanitizeCaseUrn() {
66-
CourtScheduleController courtScheduleController = new CourtScheduleController(new CourtScheduleService());
67-
6878
String unsanitizedCaseUrn = "<script>alert('xss')</script>";
6979
log.info("Calling courtScheduleController.getCourtScheduleByCaseUrn with unsanitized caseUrn: {}", unsanitizedCaseUrn);
7080

@@ -76,16 +86,12 @@ void getCourtScheduleByCaseUrn_ShouldSanitizeCaseUrn() {
7686

7787
@Test
7888
void getJudgeById_ShouldReturnBadRequestStatus() {
79-
CourtScheduleController courtScheduleController = new CourtScheduleController(new CourtScheduleService());
80-
81-
log.info("Calling courtScheduleController.getCourtScheduleByCaseUrn with null caseUrn");
82-
try {
89+
ResponseStatusException exception = assertThrows(ResponseStatusException.class, () -> {
8390
courtScheduleController.getCourtScheduleByCaseUrn(null);
84-
} catch (ResponseStatusException e) {
85-
log.info(e.getMessage());
86-
assertEquals(HttpStatus.BAD_REQUEST, e.getStatusCode());
87-
assertEquals("caseUrn is required", e.getReason());
88-
}
91+
});
92+
assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
93+
assertThat(exception.getReason()).isEqualTo("caseUrn is required");
94+
assertThat(exception.getMessage()).isEqualTo("400 BAD_REQUEST \"caseUrn is required\"");
8995
}
9096

9197
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package uk.gov.hmcts.cp.repositories;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse;
6+
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponseCourtScheduleInner;
7+
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponseCourtScheduleInnerHearingsInner;
8+
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponseCourtScheduleInnerHearingsInnerCourtSittingsInner;
9+
10+
import java.util.UUID;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertNotNull;
14+
import static org.junit.jupiter.api.Assertions.assertTrue;
15+
16+
class CourtScheduleRepositoryTest {
17+
18+
private CourtScheduleRepository courtScheduleRepository;
19+
20+
@BeforeEach
21+
void setUp() {
22+
courtScheduleRepository = new InMemoryCourtScheduleRepositoryImpl();
23+
}
24+
25+
@Test
26+
void getCourtScheduleByCaseUrn_shouldReturnCourtScheduleResponse() {
27+
UUID caseUrn = UUID.randomUUID();
28+
CourtScheduleResponse response = courtScheduleRepository.getCourtScheduleByCaseUrn(caseUrn.toString());
29+
30+
assertNotNull(response.getCourtSchedule());
31+
assertEquals(1, response.getCourtSchedule().size());
32+
33+
CourtScheduleResponseCourtScheduleInner schedule = response.getCourtSchedule().get(0);
34+
assertNotNull(schedule.getHearings());
35+
assertEquals(1, schedule.getHearings().size());
36+
37+
CourtScheduleResponseCourtScheduleInnerHearingsInner hearing = schedule.getHearings().get(0);
38+
assertNotNull(hearing.getHearingId());
39+
assertEquals("Requires interpreter", hearing.getListNote());
40+
assertEquals("Sentencing for theft case", hearing.getHearingDescription());
41+
assertEquals("Trial", hearing.getHearingType());
42+
assertNotNull(hearing.getCourtSittings());
43+
assertEquals(1, hearing.getCourtSittings().size());
44+
45+
CourtScheduleResponseCourtScheduleInnerHearingsInnerCourtSittingsInner sitting =
46+
hearing.getCourtSittings().get(0);
47+
assertEquals("Central Criminal Court", sitting.getCourtHouse());
48+
assertNotNull(sitting.getSittingStart());
49+
assertTrue(sitting.getSittingEnd().isAfter(sitting.getSittingStart()));
50+
assertNotNull(sitting.getJudiciaryId());
51+
}
52+
53+
}

src/test/java/uk/gov/hmcts/cp/services/CourtScheduleServiceTest.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33
import org.junit.jupiter.api.Test;
44
import org.springframework.web.server.ResponseStatusException;
55
import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse;
6+
import uk.gov.hmcts.cp.repositories.CourtScheduleRepository;
7+
import uk.gov.hmcts.cp.repositories.InMemoryCourtScheduleRepositoryImpl;
68

79
import static org.assertj.core.api.Assertions.assertThat;
810
import static org.assertj.core.api.Assertions.assertThatThrownBy;
911

1012
class CourtScheduleServiceTest {
1113

12-
private final CourtScheduleService courtScheduleService = new CourtScheduleService();
14+
private final CourtScheduleRepository courtScheduleRepository = new InMemoryCourtScheduleRepositoryImpl();
15+
private final CourtScheduleService courtScheduleService = new CourtScheduleService(courtScheduleRepository);
1316

1417
@Test
1518
void shouldReturnStubbedCourtScheduleResponse_whenValidCaseUrnProvided() {
1619
// Arrange
1720
String validCaseUrn = "123-ABC-456";
1821

1922
// Act
20-
CourtScheduleResponse response = courtScheduleService.getCourtScheduleResponse(validCaseUrn);
23+
CourtScheduleResponse response = courtScheduleService.getCourtScheduleByCaseUrn(validCaseUrn);
2124

2225
// Assert
2326
assertThat(response).isNotNull();
@@ -34,7 +37,7 @@ void shouldThrowBadRequestException_whenCaseUrnIsNull() {
3437
String nullCaseUrn = null;
3538

3639
// Act & Assert
37-
assertThatThrownBy(() -> courtScheduleService.getCourtScheduleResponse(nullCaseUrn))
40+
assertThatThrownBy(() -> courtScheduleService.getCourtScheduleByCaseUrn(nullCaseUrn))
3841
.isInstanceOf(ResponseStatusException.class)
3942
.hasMessageContaining("400 BAD_REQUEST")
4043
.hasMessageContaining("caseUrn is required");
@@ -46,7 +49,7 @@ void shouldThrowBadRequestException_whenCaseUrnIsEmpty() {
4649
String emptyCaseUrn = "";
4750

4851
// Act & Assert
49-
assertThatThrownBy(() -> courtScheduleService.getCourtScheduleResponse(emptyCaseUrn))
52+
assertThatThrownBy(() -> courtScheduleService.getCourtScheduleByCaseUrn(emptyCaseUrn))
5053
.isInstanceOf(ResponseStatusException.class)
5154
.hasMessageContaining("400 BAD_REQUEST")
5255
.hasMessageContaining("caseUrn is required");

0 commit comments

Comments
 (0)