diff --git a/build.gradle b/build.gradle index d2d5cf4..e9927e1 100644 --- a/build.gradle +++ b/build.gradle @@ -247,6 +247,10 @@ ext { lombokVersion = "1.18.38" } +tasks.named('processPactVerificationTestResources') { + duplicatesStrategy = DuplicatesStrategy.EXCLUDE +} + dependencies { implementation "uk.gov.hmcts.cp:api-cp-crime-schedulingandlisting-courtschedule:$apiCourtScheduleVersion" implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.9' @@ -272,6 +276,9 @@ dependencies { implementation 'org.hibernate.validator:hibernate-validator:9.0.1.Final' implementation 'org.apache.commons:commons-text:1.13.1' + implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2' + implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.2' + compileOnly group: 'org.projectlombok', name: 'lombok', version: lombokVersion annotationProcessor group: 'org.projectlombok', name: 'lombok', version: lombokVersion diff --git a/src/pactVerificationTest/java/uk/gov/hmcts/cp/pact/helper/JsonFileToObject.java b/src/pactVerificationTest/java/uk/gov/hmcts/cp/pact/helper/JsonFileToObject.java new file mode 100644 index 0000000..6b5c4f0 --- /dev/null +++ b/src/pactVerificationTest/java/uk/gov/hmcts/cp/pact/helper/JsonFileToObject.java @@ -0,0 +1,17 @@ +package uk.gov.hmcts.cp.pact.helper; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; + +import java.io.File; + +public class JsonFileToObject { + + private static final ObjectMapper mapper = new ObjectMapper() + .registerModule(new JavaTimeModule()); + + public static T readJsonFromResources(String fileName, Class clazz) throws Exception { + File file = new File(JsonFileToObject.class.getClassLoader().getResource(fileName).toURI()); + return mapper.readValue(file, clazz); + } +} diff --git a/src/pactVerificationTest/java/pact.provider/CourtScheduleProviderPactTest.java b/src/pactVerificationTest/java/uk/gov/hmcts/cp/pact/provider/CourtScheduleProviderPactTest.java similarity index 63% rename from src/pactVerificationTest/java/pact.provider/CourtScheduleProviderPactTest.java rename to src/pactVerificationTest/java/uk/gov/hmcts/cp/pact/provider/CourtScheduleProviderPactTest.java index 06e2e6c..361a71a 100644 --- a/src/pactVerificationTest/java/pact.provider/CourtScheduleProviderPactTest.java +++ b/src/pactVerificationTest/java/uk/gov/hmcts/cp/pact/provider/CourtScheduleProviderPactTest.java @@ -16,15 +16,9 @@ import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.test.context.junit.jupiter.SpringExtension; import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse; -import uk.gov.hmcts.cp.openapi.model.CourtSchedule; -import uk.gov.hmcts.cp.openapi.model.Hearing; -import uk.gov.hmcts.cp.openapi.model.CourtSitting; +import uk.gov.hmcts.cp.pact.helper.JsonFileToObject; import uk.gov.hmcts.cp.repositories.CourtScheduleRepository; -import java.time.OffsetDateTime; -import java.util.List; -import java.util.UUID; - @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ExtendWith({SpringExtension.class, PactVerificationInvocationContextProvider.class}) @Provider("CPCourtScheduleProvider") @@ -50,31 +44,10 @@ void setupTarget(PactVerificationContext context) { } @State("court schedule for case 456789 exists") - public void setupCourtSchedule() { + public void setupCourtSchedule() throws Exception{ courtScheduleRepository.clearAll(); - var courtSitting = CourtSitting.builder() - .courtHouse("Central Criminal Court") - .sittingStart(OffsetDateTime.now()) - .sittingEnd(OffsetDateTime.now().plusMinutes(60)) - .judiciaryId(UUID.randomUUID().toString()) - .build(); - var hearing = Hearing.builder() - .hearingId(UUID.randomUUID().toString()) - .listNote("Requires interpreter") - .hearingDescription("Sentencing for theft case") - .hearingType("Trial") - .courtSittings(List.of(courtSitting)) - .build(); - - var schedule = CourtSchedule.builder() - .hearings(List.of(hearing)) - .build(); - - var response = CourtScheduleResponse.builder() - .courtSchedule(List.of(schedule)) - .build(); - - courtScheduleRepository.saveCourtSchedule("456789", response); + CourtScheduleResponse courtScheduleResponse = JsonFileToObject.readJsonFromResources("courtSchedule.json", CourtScheduleResponse.class); + courtScheduleRepository.saveCourtSchedule("456789", courtScheduleResponse); } @TestTemplate diff --git a/src/pactVerificationTest/resources/courtSchedule.json b/src/pactVerificationTest/resources/courtSchedule.json new file mode 100644 index 0000000..edb43f2 --- /dev/null +++ b/src/pactVerificationTest/resources/courtSchedule.json @@ -0,0 +1,22 @@ +{ + "courtSchedule": [ + { + "hearings": [ + { + "hearingId": "HRG-123456", + "hearingType": "Preliminary", + "hearingDescription": "Initial appearance for case 456789", + "listNote": "Judge prefers afternoon start", + "courtSittings": [ + { + "sittingStart": "2025-03-25T09:00:00Z", + "sittingEnd": "2025-03-25T12:00:00Z", + "judiciaryId": "123e4567-e89b-12d3-a456-426614174000", + "courtHouse": "223e4567-e89b-12d3-a456-426614174111" + } + ] + } + ] + } + ] +}