Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.moj.cpp.hearing.it;

import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasNoJsonPath;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJson;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.withJsonPath;
import static java.util.Optional.of;
Expand All @@ -11,6 +12,10 @@
import static org.hamcrest.Matchers.is;
import static uk.gov.moj.cpp.hearing.it.PublishLatestCourtCentreHearingEventsIT.XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26;
import static uk.gov.moj.cpp.hearing.steps.HearingEventStepDefinitions.OPEN_CASE_PROSECUTION_EVENT_DEFINITION_ID;
import static uk.gov.moj.cpp.hearing.utils.WebDavStub.awaitNewFile;
import static uk.gov.moj.cpp.hearing.utils.WebDavStub.awaitNewSentXmlForPubDisplay;
import static uk.gov.moj.cpp.hearing.utils.WebDavStub.countFilesAt;
import static uk.gov.moj.cpp.hearing.utils.WebDavStub.countSentXmlForPubDisplay;
import static uk.gov.moj.cpp.hearing.utils.WebDavStub.getFileForPath;
import static uk.gov.moj.cpp.hearing.utils.WebDavStub.getSentXmlForPubDisplay;

Expand Down Expand Up @@ -106,9 +111,14 @@ public void shouldRequestToPublishCourtListWithCaseRestriction() throws Exceptio

publishCourtListJsonObject = buildPublishCourtListJsonString(courtCentreId, "26");

final int webPageCountBeforeSecondPublish = countFilesAt(XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26);
final int pubDisplayCountBeforeSecondPublish = countSentXmlForPubDisplay();

courtCentreId = sendPublishCourtListCommand(publishCourtListJsonObject, courtCentreId);

publishCourtListSteps.verifyCourtListPublishStatusReturnedWhenQueryingFromAPI(courtCentreId);
awaitNewFile(XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26, webPageCountBeforeSecondPublish);
awaitNewSentXmlForPubDisplay(pubDisplayCountBeforeSecondPublish);

filePayload = getFileForPath(XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26);
filePayloadForPubDisplay = getSentXmlForPubDisplay();
Expand Down Expand Up @@ -171,8 +181,13 @@ public void shouldRequestToPublishCourtListWithDefendantRestrictionOnOff() throw
courtListRestrictionSteps.waitForRestrictionProjection(courtCentreId, eventTime.toLocalDate(),
withJsonPath("$.court.courtSites[0].courtRooms[0].cases.casesDetails[0].defendants", hasSize(1)));

final int webPageCountBeforeSecondPublish = countFilesAt(XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26);
final int pubDisplayCountBeforeSecondPublish = countSentXmlForPubDisplay();

courtCentreId = sendPublishCourtListCommand(publishCourtListJsonObject, courtCentreId);
publishCourtListSteps.verifyCourtListPublishStatusReturnedWhenQueryingFromAPI(courtCentreId);
awaitNewFile(XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26, webPageCountBeforeSecondPublish);
awaitNewSentXmlForPubDisplay(pubDisplayCountBeforeSecondPublish);

filePayload = getFileForPath(XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26);
filePayloadForPubDisplay = getSentXmlForPubDisplay();
Expand Down Expand Up @@ -242,9 +257,14 @@ public void shouldRequestToPublishCourtListForApplicationRestrictionOnOff() thro

publishCourtListJsonObject = buildPublishCourtListJsonString(courtCentreId, "26");

final int webPageCountBeforeSecondPublish = countFilesAt(XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26);
final int pubDisplayCountBeforeSecondPublish = countSentXmlForPubDisplay();

courtCentreId = sendPublishCourtListCommand(publishCourtListJsonObject, courtCentreId);

publishCourtListSteps.verifyCourtListPublishStatusReturnedWhenQueryingFromAPI(courtCentreId);
awaitNewFile(XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26, webPageCountBeforeSecondPublish);
awaitNewSentXmlForPubDisplay(pubDisplayCountBeforeSecondPublish);

filePayload = getFileForPath(XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26);
filePayloadForPubDisplay = getSentXmlForPubDisplay();
Expand Down Expand Up @@ -273,11 +293,9 @@ public void shouldRequestToPublishCourtListForApplicationApplicantRestrictionOnO
withJsonPath("$.courtApplicationApplicantIds", hasSize(1)),
withJsonPath("$.restrictCourtList", is(true)))));

// Wait for applicant restriction to land in the projection before publishing.
// When restricted the applicant is nulled out and the defendant serialises as {} (no keys),
// so we assert on the object being empty rather than firstName being null (absent key ≠ null).
// Wait for applicant restriction to land in the projection before publishing
courtListRestrictionSteps.waitForRestrictionProjection(courtCentreId, eventTime.toLocalDate(),
withJsonPath("$.court.courtSites[0].courtRooms[0].cases.casesDetails[0].defendants[0]", is(java.util.Collections.emptyMap())));
hasNoJsonPath("$.court.courtSites[0].courtRooms[0].cases.casesDetails[0].defendants[0].firstName"));

JsonObject publishCourtListJsonObject = buildPublishCourtListJsonString(courtCentreId, "26");

Expand Down Expand Up @@ -310,9 +328,14 @@ public void shouldRequestToPublishCourtListForApplicationApplicantRestrictionOnO

publishCourtListJsonObject = buildPublishCourtListJsonString(courtCentreId, "26");

final int webPageCountBeforeSecondPublish = countFilesAt(XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26);
final int pubDisplayCountBeforeSecondPublish = countSentXmlForPubDisplay();

courtCentreId = sendPublishCourtListCommand(publishCourtListJsonObject, courtCentreId);

publishCourtListSteps.verifyCourtListPublishStatusReturnedWhenQueryingFromAPI(courtCentreId);
awaitNewFile(XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26, webPageCountBeforeSecondPublish);
awaitNewSentXmlForPubDisplay(pubDisplayCountBeforeSecondPublish);

filePayload = getFileForPath(XHIBIT_GATEWAY_SEND_WEB_PAGE_TO_XHIBIT_FILE_NAME_26);
filePayloadForPubDisplay = getSentXmlForPubDisplay();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static uk.gov.justice.services.common.http.HeaderConstants.USER_ID;
import static uk.gov.justice.services.test.utils.core.http.BaseUriProvider.getBaseUri;
import static uk.gov.justice.services.test.utils.core.http.RequestParamsBuilder.requestParams;
Expand Down Expand Up @@ -106,6 +107,12 @@ public JsonPath hearingEventsCourtListRestrictedReceived(final Matcher<?> matche
* Required because {@link #hearingEventsCourtListRestrictedReceived(Matcher)} only confirms the
* hearing event was emitted; the listener that projects it into the JPA entity runs in a
* separate transaction and may lag behind the publish command if not waited for.
* <p>
* A hearing-visibility precondition ({@code courtRoomId notNullValue}) is prepended to the
* caller's matcher to prevent the poll from short-circuiting on the empty/not-yet-projected
* state — without this, lenient matchers such as {@code hasNoJsonPath(...)} or
* {@code withJsonPath(..., hasSize(0))} would match an empty {@code {}} response and return
* before the restriction event has actually been processed.
*/
public void waitForRestrictionProjection(final String courtCentreId,
final LocalDate hearingDate,
Expand All @@ -118,7 +125,9 @@ public void waitForRestrictionProjection(final String courtCentreId,
.withHeader(USER_ID, getLoggedInSystemUserHeader()))
.timeout(60, SECONDS)
.pollInterval(1, SECONDS)
.until(status().is(OK), payload().isJson(expectedPayload));
.until(status().is(OK), payload().isJson(allOf(
withJsonPath("$.court.courtSites[0].courtRooms[0].courtRoomId", notNullValue()),
expectedPayload)));
}

private void sendListingPublicEvent(final JsonObject restrictCourtListDataObject) {
Expand All @@ -137,7 +146,7 @@ public CommandHelpers.InitiateHearingCommandHelper createHearingEvent(final UUID
}

public CommandHelpers.InitiateHearingCommandHelper createHearingEventWithYoungDefendant(final UUID caseId, final UUID hearingEventId, final String courtRoomId, final String defenceCounselId,
final UUID eventDefinitionId, final ZonedDateTime eventTime, final Optional<UUID> hearingTypeId, final String courtCenter, final LocalDate localDate) throws NoSuchAlgorithmException {
final UUID eventDefinitionId, final ZonedDateTime eventTime, final Optional<UUID> hearingTypeId, final String courtCenter, final LocalDate localDate) throws NoSuchAlgorithmException {
try (final Utilities.EventListener eventListener = listenFor(HEARING_EVENTS_COURT_LIST_RESTRICTED, HEARING_EVENT)
.withFilter(isJson(allOf(
withJsonPath("$.defendantIds", hasSize(1)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
import static javax.ws.rs.core.Response.Status.OK;
import static org.awaitility.Durations.ONE_MINUTE;

import java.util.List;
import java.util.UUID;

import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import org.awaitility.Awaitility;

public class WebDavStub {

Expand All @@ -35,4 +37,22 @@ public static String getFileForPath(final String filePath) {

return loggedRequest.getBodyAsString();
}

public static int countFilesAt(final String filePath) {
return findAll(putRequestedFor(urlPathMatching(filePath))).size();
}

public static int countSentXmlForPubDisplay() {
return countFilesAt(XHIBIT_GATEWAY_SEND_PUB_DISP_TO_XHIBIT_FILE_PATH_REG_EX);
}

public static void awaitNewFile(final String filePath, final int previousCount) {
Awaitility.await()
.atMost(ONE_MINUTE)
.until(() -> countFilesAt(filePath) > previousCount);
}

public static void awaitNewSentXmlForPubDisplay(final int previousCount) {
awaitNewFile(XHIBIT_GATEWAY_SEND_PUB_DISP_TO_XHIBIT_FILE_PATH_REG_EX, previousCount);
}
}
Loading