|
| 1 | +package uk.gov.hmcts.cp.pact.provider; |
| 2 | + |
| 3 | +import au.com.dius.pact.provider.junit5.HttpTestTarget; |
| 4 | +import au.com.dius.pact.provider.junit5.PactVerificationContext; |
| 5 | +import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider; |
| 6 | +import au.com.dius.pact.provider.junitsupport.Provider; |
| 7 | +import au.com.dius.pact.provider.junitsupport.State; |
| 8 | +import au.com.dius.pact.provider.junitsupport.loader.PactBroker; |
| 9 | +import au.com.dius.pact.provider.junitsupport.loader.PactBrokerAuth; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Tag; |
| 12 | +import org.junit.jupiter.api.TestTemplate; |
| 13 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; |
| 15 | +import org.springframework.boot.test.context.SpringBootTest; |
| 16 | +import org.springframework.boot.test.web.server.LocalServerPort; |
| 17 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 18 | +import uk.gov.hmcts.cp.openapi.model.CourtScheduleResponse; |
| 19 | +import uk.gov.hmcts.cp.openapi.model.CourtSchedule; |
| 20 | +import uk.gov.hmcts.cp.openapi.model.Hearing; |
| 21 | +import uk.gov.hmcts.cp.openapi.model.CourtSitting; |
| 22 | +import uk.gov.hmcts.cp.repositories.CourtScheduleRepository; |
| 23 | + |
| 24 | +import java.time.OffsetDateTime; |
| 25 | +import java.util.List; |
| 26 | +import java.util.UUID; |
| 27 | + |
| 28 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 29 | +@ExtendWith({SpringExtension.class, PactVerificationInvocationContextProvider.class}) |
| 30 | +@Provider("VPCourtScheduleProvider") |
| 31 | +@PactBroker( |
| 32 | + scheme = "https", |
| 33 | + host = "${pact.broker.host}", |
| 34 | + authentication = @PactBrokerAuth(token = "${pact.broker.token}") |
| 35 | +) |
| 36 | +@Tag("pact") |
| 37 | +public class CourtScheduleProviderPactTest { |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private CourtScheduleRepository courtScheduleRepository; |
| 41 | + |
| 42 | + @LocalServerPort |
| 43 | + private int port; |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + void setupTarget(PactVerificationContext context) { |
| 47 | + System.out.println("Running test on port: " + port); |
| 48 | + context.setTarget(new HttpTestTarget("localhost", port)); |
| 49 | + System.out.println("pact.verifier.publishResults: " + System.getProperty("pact.verifier.publishResults")); |
| 50 | + } |
| 51 | + |
| 52 | + @State("court schedule for case 456789 exists") |
| 53 | + public void setupCourtSchedule() { |
| 54 | + courtScheduleRepository.clearAll(); |
| 55 | + var courtSitting = CourtSitting.builder() |
| 56 | + .courtHouse("Central Criminal Court") |
| 57 | + .sittingStart(OffsetDateTime.now()) |
| 58 | + .sittingEnd(OffsetDateTime.now().plusMinutes(60)) |
| 59 | + .judiciaryId(UUID.randomUUID().toString()) |
| 60 | + .build(); |
| 61 | + var hearing = Hearing.builder() |
| 62 | + .hearingId(UUID.randomUUID().toString()) |
| 63 | + .listNote("Requires interpreter") |
| 64 | + .hearingDescription("Sentencing for theft case") |
| 65 | + .hearingType("Trial") |
| 66 | + .courtSittings(List.of(courtSitting)) |
| 67 | + .build(); |
| 68 | + |
| 69 | + var schedule = CourtSchedule.builder() |
| 70 | + .hearings(List.of(hearing)) |
| 71 | + .build(); |
| 72 | + |
| 73 | + var response = CourtScheduleResponse.builder() |
| 74 | + .courtSchedule(List.of(schedule)) |
| 75 | + .build(); |
| 76 | + |
| 77 | + courtScheduleRepository.saveCourtSchedule("456789", response); |
| 78 | + } |
| 79 | + |
| 80 | + @TestTemplate |
| 81 | + void pactVerificationTestTemplate(PactVerificationContext context) { |
| 82 | + context.verifyInteraction(); |
| 83 | + } |
| 84 | +} |
0 commit comments