diff --git a/hearing-integration-test/src/test/java/uk/gov/moj/cpp/hearing/steps/CourtListRestrictionSteps.java b/hearing-integration-test/src/test/java/uk/gov/moj/cpp/hearing/steps/CourtListRestrictionSteps.java index 849e1b598..6fb6f8999 100644 --- a/hearing-integration-test/src/test/java/uk/gov/moj/cpp/hearing/steps/CourtListRestrictionSteps.java +++ b/hearing-integration-test/src/test/java/uk/gov/moj/cpp/hearing/steps/CourtListRestrictionSteps.java @@ -130,6 +130,33 @@ public void waitForRestrictionProjection(final String courtCentreId, expectedPayload))); } + /** + * Polls the publish-side query {@code hearing.latest-hearings-by-court-centres} until the + * just-created hearing is observable. MUST be called after {@code createHearingEvent*} and + * BEFORE any {@code hide*FromXhibit} call. + *
+ * Without this wait, the {@code public.listing.court-list-restricted} → ... →
+ * {@code hearing.event.court-list-restricted} chain can reach
+ * {@link uk.gov.moj.cpp.hearing.event.listener.CourtListRestrictionEventListener} before the
+ * hearing-creation projection has committed to {@code ha_hearing}. The listener does
+ * {@code hearingRepository.findOptionalBy(hearingId)} and, if the row is missing, silently
+ * returns (the message is consumed and never replayed). The restriction is then lost, the
+ * subsequent publish reads the un-restricted hearing, and the assertion on the redacted XML
+ * fails. This flake reproduced ~2/3 of CI runs on team/rv-2616.
+ */
+ public void waitForHearingVisible(final String courtCentreId, final LocalDate hearingDate) {
+ 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(
+ withJsonPath("$.court.courtSites[0].courtRooms[0].courtRoomId", notNullValue())));
+ }
+
private void sendListingPublicEvent(final JsonObject restrictCourtListDataObject) {
sendMessage(
getPublicTopicInstance().createProducer(),
@@ -142,11 +169,12 @@ public CommandHelpers.InitiateHearingCommandHelper createHearingEvent(final UUID
final UUID eventDefinitionId, final ZonedDateTime eventTime, final Optional