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 @@ -71,6 +76,10 @@ public void shouldRequestToPublishCourtListWithCaseRestriction() throws Exceptio
withJsonPath("$.caseIds", hasSize(1)),
withJsonPath("$.restrictCourtList", is(true)))));

// Wait for the restriction projection to land before publishing
courtListRestrictionSteps.waitForRestrictionProjection(courtCentreId, eventTime.toLocalDate(),
withJsonPath("$.court.courtSites[0].courtRooms[0].cases.casesDetails", hasSize(0)));

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

final PublishCourtListSteps publishCourtListSteps = new PublishCourtListSteps();
Expand All @@ -96,11 +105,20 @@ public void shouldRequestToPublishCourtListWithCaseRestriction() throws Exceptio
withJsonPath("$.caseIds", hasSize(1)),
withJsonPath("$.restrictCourtList", is(false)))));

// Wait for the un-restriction projection to land before publishing
courtListRestrictionSteps.waitForRestrictionProjection(courtCentreId, eventTime.toLocalDate(),
withJsonPath("$.court.courtSites[0].courtRooms[0].cases.casesDetails", hasSize(1)));

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 All @@ -127,6 +145,10 @@ public void shouldRequestToPublishCourtListWithDefendantRestrictionOnOff() throw
withJsonPath("$.defendantIds", hasSize(1)),
withJsonPath("$.restrictCourtList", is(true)))));

// Wait for defendant restriction to land in the projection before publishing
courtListRestrictionSteps.waitForRestrictionProjection(courtCentreId, eventTime.toLocalDate(),
withJsonPath("$.court.courtSites[0].courtRooms[0].cases.casesDetails[0].defendants", hasSize(0)));

final JsonObject publishCourtListJsonObject = buildPublishCourtListJsonString(courtCentreId, "26");
final PublishCourtListSteps publishCourtListSteps = new PublishCourtListSteps();

Expand Down Expand Up @@ -155,8 +177,17 @@ public void shouldRequestToPublishCourtListWithDefendantRestrictionOnOff() throw
withJsonPath("$.defendantIds", hasSize(1)),
withJsonPath("$.restrictCourtList", is(false)))));

// Wait for defendant un-restriction to land in the projection before publishing
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 @@ -191,6 +222,10 @@ public void shouldRequestToPublishCourtListForApplicationRestrictionOnOff() thro
withJsonPath("$.courtApplicationIds", hasSize(1)),
withJsonPath("$.restrictCourtList", is(true)))));

// Wait for application restriction to land in the projection before publishing
courtListRestrictionSteps.waitForRestrictionProjection(courtCentreId, eventTime.toLocalDate(),
withJsonPath("$.court.courtSites[0].courtRooms[0].cases.casesDetails", hasSize(0)));

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

final PublishCourtListSteps publishCourtListSteps = new PublishCourtListSteps();
Expand All @@ -216,11 +251,20 @@ public void shouldRequestToPublishCourtListForApplicationRestrictionOnOff() thro
withJsonPath("$.courtApplicationIds", hasSize(1)),
withJsonPath("$.restrictCourtList", is(false)))));

// Wait for application un-restriction to land in the projection before publishing
courtListRestrictionSteps.waitForRestrictionProjection(courtCentreId, eventTime.toLocalDate(),
withJsonPath("$.court.courtSites[0].courtRooms[0].cases.casesDetails", hasSize(1)));

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 @@ -249,6 +293,10 @@ public void shouldRequestToPublishCourtListForApplicationApplicantRestrictionOnO
withJsonPath("$.courtApplicationApplicantIds", hasSize(1)),
withJsonPath("$.restrictCourtList", is(true)))));

// Wait for applicant restriction to land in the projection before publishing
courtListRestrictionSteps.waitForRestrictionProjection(courtCentreId, eventTime.toLocalDate(),
hasNoJsonPath("$.court.courtSites[0].courtRooms[0].cases.casesDetails[0].defendants[0].firstName"));

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

final PublishCourtListSteps publishCourtListSteps = new PublishCourtListSteps();
Expand All @@ -274,11 +322,20 @@ public void shouldRequestToPublishCourtListForApplicationApplicantRestrictionOnO
withJsonPath("$.courtApplicationApplicantIds", hasSize(1)),
withJsonPath("$.restrictCourtList", is(false)))));

// Wait for applicant un-restriction to land in the projection before publishing
courtListRestrictionSteps.waitForRestrictionProjection(courtCentreId, eventTime.toLocalDate(),
withJsonPath("$.court.courtSites[0].courtRooms[0].cases.casesDetails[0].defendants[0].firstName", org.hamcrest.CoreMatchers.notNullValue()));

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 @@ -308,6 +365,10 @@ public void shouldRequestToPublishCourtListWhenYoungDefendantIsRestrictedOnIniti
caseId, randomUUID(), courtRoom2Id, randomUUID().toString(),
OPEN_CASE_PROSECUTION_EVENT_DEFINITION_ID, eventTime, of(hearingTypeId), courtCentreId, eventTime.toLocalDate());

// Wait for the young-defendant restriction to land in the projection before publishing
courtListRestrictionSteps.waitForRestrictionProjection(courtCentreId, eventTime.toLocalDate(),
withJsonPath("$.court.courtSites[0].courtRooms[0].cases.casesDetails[0].defendants", hasSize(0)));

final JsonObject publishCourtListJsonObject = buildPublishCourtListJsonString(courtCentreId, "26");
final PublishCourtListSteps publishCourtListSteps = new PublishCourtListSteps();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
import static com.google.common.collect.Lists.newArrayList;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJson;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.withJsonPath;
import static java.text.MessageFormat.format;
import static java.util.UUID.fromString;
import static java.util.UUID.randomUUID;
import static java.util.concurrent.TimeUnit.SECONDS;
import static javax.ws.rs.core.Response.Status.OK;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
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;
import static uk.gov.justice.services.test.utils.core.http.RestPoller.poll;
import static uk.gov.justice.services.test.utils.core.matchers.ResponsePayloadMatcher.payload;
import static uk.gov.justice.services.test.utils.core.matchers.ResponseStatusMatcher.status;
import static uk.gov.moj.cpp.hearing.utils.WireMockStubUtils.setupAsAuthorizedAndSystemUser;
import static uk.gov.justice.hearing.courts.CourtListRestricted.courtListRestricted;
import static uk.gov.justice.services.test.utils.core.messaging.MetadataBuilderFactory.metadataWithRandomUUID;
import static uk.gov.moj.cpp.hearing.it.UseCases.asDefault;
Expand Down Expand Up @@ -87,6 +97,30 @@ public JsonPath hearingEventsCourtListRestrictedReceived(final Matcher<?> matche
}
}

/**
* Polls the publish-side query {@code hearing.latest-hearings-by-court-centres} until the
* restriction projection has reached the expected state. The publish flow internally consumes
* the same query — once it reflects the toggle, the next {@code publish-court-list} command is
* guaranteed to see the same state.
* <p>
* 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.
*/
public void waitForRestrictionProjection(final String courtCentreId,
final LocalDate hearingDate,
final Matcher<? super com.jayway.jsonpath.ReadContext> expectedPayload) {
setupAsAuthorizedAndSystemUser(USER_ID_VALUE_AS_ADMIN);
final String queryPart = format(ENDPOINT_PROPERTIES.getProperty("hearing.latest-hearings-by-court-centres"), courtCentreId, hearingDate);
final String searchCourtListUrl = String.format("%s/%s", getBaseUri(), queryPart);

poll(requestParams(searchCourtListUrl, "application/vnd.hearing.latest-hearings-by-court-centres+json")
.withHeader(USER_ID, getLoggedInSystemUserHeader()))
.timeout(60, SECONDS)
.pollInterval(1, SECONDS)
.until(status().is(OK), payload().isJson(expectedPayload));
}

private void sendListingPublicEvent(final JsonObject restrictCourtListDataObject) {
sendMessage(
getPublicTopicInstance().createProducer(),
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