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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>uk.gov.moj.cpp.results</groupId>
<artifactId>results-parent</artifactId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>

<packaging>pom</packaging>
<name>Results Service - Parent Module</name>
Expand Down
2 changes: 1 addition & 1 deletion results-command/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>uk.gov.moj.cpp.results</groupId>
<artifactId>results-parent</artifactId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>
<artifactId>results-command</artifactId>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion results-command/results-command-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>uk.gov.moj.cpp.results</groupId>
<artifactId>results-command</artifactId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>
<artifactId>results-command-api</artifactId>
<packaging>war</packaging>
Expand Down
2 changes: 1 addition & 1 deletion results-command/results-command-handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>uk.gov.moj.cpp.results</groupId>
<artifactId>results-command</artifactId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>
<artifactId>results-command-handler</artifactId>
<packaging>war</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public class ResultsCommandHandler extends AbstractCommandHandler {
private static final String A_4 = "A4";
private static final String ZERO_FOUR = "04";
private static final String CONTACT_EMAIL_ADDRESS = "contactEmailAddress";
private static final String CPS_FLAG = "cpsFlag";
private static final String CPS_CC_EMAIL_ADDRESS = "cpsCcEmailAddress";

@Inject
private ObjectToJsonObjectConverter objectToJsonObjectConverter;
Expand Down Expand Up @@ -352,7 +354,7 @@ private void createAppealUpdateEmail(final JsonEnvelope commandEnvelope, final O
final String[] emailAddress = new String[1];

refDataProsecutorJson.ifPresent(prosecutorJson ->
emailAddress[0] = getEmailAddress(prosecutorJson, jurisdictionType)
emailAddress[0] = getAppealUpdateEmailAddress(prosecutorJson, jurisdictionType)
);

if (emailAddress[0] != null && !emailAddress[0].isBlank()) {
Expand Down Expand Up @@ -638,6 +640,21 @@ private String getEmailAddress(final JsonObject prosecutorJson, final Optional<J
return "";
}

/**
* Resolves the email address for appeal-update notifications.
* For CPS prosecutors (cpsFlag == true) the CPS Crown Court email (cpsCcEmailAddress) is used when present;
* otherwise it falls back to the standard {@link #getEmailAddress} resolution.
*/
private String getAppealUpdateEmailAddress(final JsonObject prosecutorJson, final Optional<JurisdictionType> jurisdictionType) {
if (getFlagValue(CPS_FLAG, prosecutorJson)) {
final String cpsCcEmailAddress = prosecutorJson.getString(CPS_CC_EMAIL_ADDRESS, null);
if (cpsCcEmailAddress != null && !cpsCcEmailAddress.isBlank()) {
return cpsCcEmailAddress;
}
}
return getEmailAddress(prosecutorJson, jurisdictionType);
}

private void appendEventsToStream(final Envelope<?> envelope, final EventStream eventStream, final Stream<Object> events) throws EventStreamException {
final JsonEnvelope jsonEnvelope = envelopeFrom(envelope.metadata(), NULL);
eventStream.append(events.map(toEnvelopeWithMetadataFrom(jsonEnvelope)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Named.named;
import static org.junit.jupiter.params.provider.Arguments.arguments;
Expand Down Expand Up @@ -1519,6 +1520,106 @@ public void shouldCreateAppealUpdateEvent() throws EventStreamException {
));
}

@Test
public void shouldUseCpsCcEmailAddressForCpsProsecutorOnAppealUpdate() throws EventStreamException {
final String cpsCcEmail = "crowncourt@cps.gov.uk";
final ResultsAggregate resultsAggregate = new ResultsAggregate();

when(eventSource.getStreamById(any())).thenReturn(this.eventStream);
when(aggregateService.get(any(EventStream.class), any())).thenReturn(resultsAggregate);

// CPS prosecutor: cpsCcEmailAddress should win over contactEmailAddress
final JsonObjectBuilder cpsProsecutorBuilder = createObjectBuilder();
cpsProsecutorBuilder
.add("spiOutFlag", true)
.add("cpsFlag", true)
.add("contactEmailAddress", EMAIL)
.add("cpsCcEmailAddress", cpsCcEmail);

// Non-CPS prosecutor: existing fallback behaviour is unchanged
final JsonObjectBuilder nonCpsProsecutorBuilder = createObjectBuilder();
nonCpsProsecutorBuilder
.add("spiOutFlag", true)
.add("policeFlag", true)
.add("informantEmailAddress", EMAIL2);

when(referenceDataService.getSpiOutFlagForProsecutionAuthorityCode(any()))
.thenReturn(of(cpsProsecutorBuilder.build()))
.thenReturn(of(nonCpsProsecutorBuilder.build()));

final JsonObject payload = getPayload(TEMPLATE_PAYLOAD_APPLICATION_RESULTED);
final JsonEnvelope envelope = envelopeFrom(metadataOf(metadataId, "results.command.create-results-for-day"), payload);
this.resultsCommandHandler.createResultsForDay(envelope);

verify(eventStream, Mockito.times(3)).append(streamArgumentCaptor.capture());
final List<JsonEnvelope> allValues = convertStreamToEventList(streamArgumentCaptor.getAllValues());

assertThat(allValues, containsInAnyOrder(
jsonEnvelope(
withMetadataEnvelopedFrom(envelope).withName("results.event.session-added-event"),
payloadIsJson(allOf(
withJsonPath("$.id", notNullValue()),
withJsonPath("$.courtCentreWithLJA", notNullValue()),
withJsonPath("$.sessionDays", notNullValue())
)
)),
jsonEnvelope(
withMetadataEnvelopedFrom(envelope)
.withName("results.event.appeal-update-notification-requested"),
payloadIsJson(allOf(
withJsonPath("$.applicationId", notNullValue()),
withJsonPath("$.subject", is("Appeal Update")),
withJsonPath("$.emailAddress", is(cpsCcEmail))
)
)),
jsonEnvelope(
withMetadataEnvelopedFrom(envelope)
.withName("results.event.appeal-update-notification-requested"),
payloadIsJson(allOf(
withJsonPath("$.applicationId", notNullValue()),
withJsonPath("$.subject", is("Appeal Update")),
withJsonPath("$.emailAddress", is(EMAIL2))
)
))
));
}

@Test
public void shouldFallBackToContactEmailWhenCpsProsecutorHasNoCpsCcEmailOnAppealUpdate() throws EventStreamException {
final ResultsAggregate resultsAggregate = new ResultsAggregate();

when(eventSource.getStreamById(any())).thenReturn(this.eventStream);
when(aggregateService.get(any(EventStream.class), any())).thenReturn(resultsAggregate);

// CPS prosecutor but no cpsCcEmailAddress -> falls back to contactEmailAddress (Crown Court)
final JsonObjectBuilder cpsProsecutorBuilder = createObjectBuilder();
cpsProsecutorBuilder
.add("spiOutFlag", true)
.add("cpsFlag", true)
.add("contactEmailAddress", EMAIL);

when(referenceDataService.getSpiOutFlagForProsecutionAuthorityCode(any()))
.thenReturn(of(cpsProsecutorBuilder.build()));

final JsonObject payload = getPayload(TEMPLATE_PAYLOAD_APPLICATION_RESULTED);
final JsonEnvelope envelope = envelopeFrom(metadataOf(metadataId, "results.command.create-results-for-day"), payload);
this.resultsCommandHandler.createResultsForDay(envelope);

verify(eventStream, Mockito.atLeastOnce()).append(streamArgumentCaptor.capture());
final List<JsonEnvelope> allValues = convertStreamToEventList(streamArgumentCaptor.getAllValues());

assertThat(allValues, hasItem(
jsonEnvelope(
withMetadataEnvelopedFrom(envelope)
.withName("results.event.appeal-update-notification-requested"),
payloadIsJson(allOf(
withJsonPath("$.subject", is("Appeal Update")),
withJsonPath("$.emailAddress", is(EMAIL))
)
))
));
}

private List<JsonEnvelope> convertStreamToEventList(final List<Stream<JsonEnvelope>> listOfStreams) {
return listOfStreams.stream()
.flatMap(jsonEnvelopeStream -> jsonEnvelopeStream).collect(toList());
Expand Down
2 changes: 1 addition & 1 deletion results-domain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>uk.gov.moj.cpp.results</groupId>
<artifactId>results-parent</artifactId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>
<artifactId>results-domain</artifactId>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion results-domain/results-domain-aggregate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>uk.gov.moj.cpp.results</groupId>
<artifactId>results-domain</artifactId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>

<artifactId>results-domain-aggregate</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion results-domain/results-domain-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>results-domain</artifactId>
<groupId>uk.gov.moj.cpp.results</groupId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>

<artifactId>results-domain-common</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion results-domain/results-domain-event/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>results-domain</artifactId>
<groupId>uk.gov.moj.cpp.results</groupId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>

<artifactId>results-domain-event</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion results-event-sources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>results-parent</artifactId>
<groupId>uk.gov.moj.cpp.results</groupId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion results-event/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>uk.gov.moj.cpp.results</groupId>
<artifactId>results-parent</artifactId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>
<artifactId>results-event</artifactId>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion results-event/results-event-listener/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>results-event</artifactId>
<groupId>uk.gov.moj.cpp.results</groupId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>

<artifactId>results-event-listener</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion results-event/results-event-processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>uk.gov.moj.cpp.results</groupId>
<artifactId>results-event</artifactId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>

<artifactId>results-event-processor</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion results-healthchecks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>results-parent</artifactId>
<groupId>uk.gov.moj.cpp.results</groupId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion results-integration-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<artifactId>results-parent</artifactId>
<groupId>uk.gov.moj.cpp.results</groupId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>

<artifactId>results-integration-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import static uk.gov.moj.cpp.results.it.steps.ResultsStepDefinitions.verifyAppealUpdateEmail;
import static uk.gov.moj.cpp.results.it.stub.NotificationNotifyServiceStub.NOTIFICATION_NOTIFY_ENDPOINT;
import static uk.gov.moj.cpp.results.it.stub.NotificationNotifyServiceStub.setupNotificationNotifyStubs;
import static uk.gov.moj.cpp.results.it.stub.NotificationNotifyServiceStub.verifyEmailNotificationIsRaised;
import static uk.gov.moj.cpp.results.it.utils.ReferenceDataServiceStub.stubCpsProsecutorForAppealUpdate;
import static uk.gov.moj.cpp.results.it.utils.ReferenceDataServiceStub.stubGetOrganisationUnit;
import static uk.gov.moj.cpp.results.it.utils.ReferenceDataServiceStub.stubPoliceFlag;
import static uk.gov.moj.cpp.results.it.utils.ReferenceDataServiceStub.stubSpiOutFlag;
Expand All @@ -41,6 +43,7 @@
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -125,6 +128,32 @@ public void testAppealApplicationEmailNotificationSuccess() {
verifyAppealUpdateEmail(NOTIFICATION_NOTIFY_ENDPOINT);
}

@Test
public void testAppealApplicationEmailNotificationUsesCpsCrownCourtEmailForCpsProsecutor() {
final String cpsCrownCourtEmail = "appeals.cc@cps.gov.uk";
final String contactEmail = "contact@cps.gov.uk";

stubGetOrganisationUnit();
createMessageConsumers();
setupNotificationNotifyStubs();
ReferenceDataServiceStub.stubCountryNationalities();

final PublicHearingResulted resultsMessage = basicShareResultsTemplateWithAppealFlag(JurisdictionType.CROWN, true);
resultsMessage.setIsReshare(Optional.of(Boolean.FALSE));
resultsMessage.setSharedTime(ZonedDateTime.now(ZoneId.of("UTC")));
resultsMessage.setHearingDay(Optional.of(LocalDate.now()));

stubSpiOutFlag(true, true, "CCSU@derbyshire.pnn.police.uk");
stubPoliceFlag("DERPF", "DERPF");
// The appeal-update notification looks the prosecutor up by code; a CPS prosecutor must be emailed at its
// CPS Crown Court address (cpsCcEmailAddress), not its contactEmailAddress.
stubCpsProsecutorForAppealUpdate(contactEmail, cpsCrownCourtEmail);

hearingResultsHaveBeenSharedV2WithoutPoliceResultGenerated(resultsMessage);

verifyEmailNotificationIsRaised(List.of("Appeal Update", cpsCrownCourtEmail));
}

private void validateProsecutorResults(final String ouCode, final String startDate, final String endDate, final javax.ws.rs.core.Response.Status status, final ResponsePayloadMatcher responsePayloadMatcher) {
final String url = BASE_URI + format("/results-query-api/query/api/rest/results/prosecutor/%s?startDate=%s%s", ouCode, startDate, Objects.nonNull(endDate) ? "&endDate=" + endDate : "");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
Expand Down Expand Up @@ -115,6 +116,36 @@ public static void stubSpiOutFlag(final boolean spiOutFlag, final boolean police
.withBody(prosecutorCodeResponse.toString())));
}

/**
* Stubs the prosecutor reference-data query (by prosecutorCode) to return a CPS prosecutor for ANY prosecutorCode.
* Used by the appeal-update notification flow, which looks the prosecutor up by code. The CPS Crown Court email
* (cpsCcEmailAddress) is expected to be used for CPS prosecutors; contactEmailAddress is included to prove it is
* NOT used when the CPS Crown Court email is present.
*/
public static void stubCpsProsecutorForAppealUpdate(final String contactEmailAddress, final String cpsCcEmailAddress) {
final String urlPath = "/referencedata-service/query/api/rest/referencedata/prosecutors";

final JsonObject cpsProsecutor = createObjectBuilder()
.add("spiOutFlag", true)
.add("cpsFlag", true)
.add("contactEmailAddress", contactEmailAddress)
.add("cpsCcEmailAddress", cpsCcEmailAddress)
.build();

final JsonObject prosecutorCodeResponse = createObjectBuilder()
.add("prosecutors", createArrayBuilder()
.add(cpsProsecutor)
.build())
.build();

stubFor(get(urlPathEqualTo(urlPath))
.withQueryParam("prosecutorCode", matching(".+"))
.willReturn(aResponse().withStatus(SC_OK)
.withHeader(ID, randomUUID().toString())
.withHeader(CONTENT_TYPE, APPLICATION_JSON)
.withBody(prosecutorCodeResponse.toString())));
}

public static void stubPoliceFlag(final String originatingOrganisation, final String prosecutionAuthority) {
final String urlPath = "/referencedata-service/query/api/rest/referencedata/prosecutors";

Expand Down
2 changes: 1 addition & 1 deletion results-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>results-parent</artifactId>
<groupId>uk.gov.moj.cpp.results</groupId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion results-performance-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>results-parent</artifactId>
<groupId>uk.gov.moj.cpp.results</groupId>
<version>17.104.111-SNAPSHOT</version>
<version>17.104.111-CIMD-3868-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Loading
Loading