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 hearing-command/hearing-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.hearing</groupId>
<artifactId>hearing-command</artifactId>
<version>17.104.180-SNAPSHOT</version>
<version>17.104.180-SNI-9204-SNAPSHOT</version>
</parent>
<artifactId>hearing-command-api</artifactId>
<packaging>war</packaging>
Expand Down
2 changes: 1 addition & 1 deletion hearing-command/hearing-command-handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>uk.gov.moj.cpp.hearing</groupId>
<artifactId>hearing-command</artifactId>
<version>17.104.180-SNAPSHOT</version>
<version>17.104.180-SNI-9204-SNAPSHOT</version>
</parent>
<artifactId>hearing-command-handler</artifactId>
<packaging>war</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ public void updateInheritPlea(final JsonEnvelope envelope) throws EventStreamExc
LOGGER.debug("hearing.command.enrich-update-plea-with-associated-hearings event received {}", envelope.toObfuscatedDebugString());
}

final Set<String> guiltyPleaTypes = referenceDataService.retrieveGuiltyPleaTypes();
final UpdateInheritedPleaCommand command = convertToObject(envelope, UpdateInheritedPleaCommand.class);

for (final UUID hearingId : command.getHearingIds()) {
aggregate(HearingAggregate.class, hearingId, envelope,
hearingAggregate -> hearingAggregate.inheritPlea(hearingId, command.getPlea()));
hearingAggregate -> hearingAggregate.inheritPlea(hearingId, command.getPlea(), guiltyPleaTypes));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@
import uk.gov.moj.cpp.hearing.domain.event.ConvictionDateRemoved;
import uk.gov.moj.cpp.hearing.domain.event.HearingInitiated;
import uk.gov.moj.cpp.hearing.domain.event.IndicatedPleaUpdated;
import uk.gov.moj.cpp.hearing.domain.event.InheritedPlea;
import uk.gov.moj.cpp.hearing.domain.event.OffencePleaUpdated;
import uk.gov.moj.cpp.hearing.domain.event.PleaUpsert;
import uk.gov.moj.cpp.hearing.domain.updatepleas.UpdateAssociatedHearingsWithIndicatedPlea;
import uk.gov.moj.cpp.hearing.domain.updatepleas.UpdateInheritedPleaCommand;
import uk.gov.moj.cpp.hearing.domain.updatepleas.UpdateOffencePleaCommand;
import uk.gov.moj.cpp.hearing.domain.updatepleas.UpdatePleaCommand;

Expand Down Expand Up @@ -84,7 +86,8 @@ public class UpdatePleaCommandHandlerTest {
OffencePleaUpdated.class,
ConvictionDateAdded.class,
ConvictionDateRemoved.class,
IndicatedPleaUpdated.class);
IndicatedPleaUpdated.class,
InheritedPlea.class);
private ObjectMapper objectMapper = new ObjectMapperProducer().objectMapper();
@InjectMocks
private UpdatePleaCommandHandler hearingCommandHandler;
Expand Down Expand Up @@ -596,4 +599,166 @@ private Set<String> createGuiltyPleaTypes() {
guiltyPleaTypes.add(GUILTY);
return guiltyPleaTypes;
}

@Test
public void updateInheritPleaAndSetConvictionDateWhenGuilty() throws Throwable {
final InitiateHearingCommandHelper hearingHelper = h(standardInitiateHearingTemplate());
final UUID hearingId = hearingHelper.getHearingId();
final UUID offenceId = hearingHelper.getFirstOffenceForFirstDefendantForFirstCase().getId();
final LocalDate pleaDate = PAST_LOCAL_DATE.next();

final UpdateInheritedPleaCommand command = new UpdateInheritedPleaCommand();
command.setHearingIds(asList(hearingId));
command.setPlea(Plea.plea()
.withOffenceId(offenceId)
.withPleaValue(GUILTY)
.withPleaDate(pleaDate)
.withOriginatingHearingId(randomUUID())
.build());

final HearingAggregate hearingAggregate = new HearingAggregate() {{
apply(new HearingInitiated(hearingHelper.getHearing()));
}};

when(this.eventSource.getStreamById(hearingId)).thenReturn(this.hearingAggregateEventStream);
when(this.aggregateService.get(this.hearingAggregateEventStream, HearingAggregate.class)).thenReturn(hearingAggregate);
when(this.referenceDataService.retrieveGuiltyPleaTypes()).thenReturn(guiltyPleaTypes);

final JsonEnvelope envelope = envelopeFrom(metadataWithRandomUUID("hearing.command.enrich-update-plea-with-associated-hearings"),
objectToJsonObjectConverter.convert(command));

this.hearingCommandHandler.updateInheritPlea(envelope);

final List<JsonEnvelope> events = verifyAppendAndGetArgumentFrom(this.hearingAggregateEventStream).collect(Collectors.toList());

assertThat(events.size(), is(2));
assertThat(events.get(0).metadata().name(), is("hearing.events.inherited-plea"));
assertThat(events.get(1).metadata().name(), is("hearing.conviction-date-added"));
assertThat(asPojo(events.get(1), ConvictionDateAdded.class), isBean(ConvictionDateAdded.class)
.with(ConvictionDateAdded::getOffenceId, is(offenceId))
.with(ConvictionDateAdded::getHearingId, is(hearingId))
.with(ConvictionDateAdded::getConvictionDate, is(pleaDate)));
}

@Test
public void updateInheritPleaAndRemoveConvictionDateWhenNotGuilty() throws Throwable {
final InitiateHearingCommandHelper hearingHelper = h(standardInitiateHearingTemplate());
final UUID hearingId = hearingHelper.getHearingId();
final UUID offenceId = hearingHelper.getFirstOffenceForFirstDefendantForFirstCase().getId();
hearingHelper.getFirstOffenceForFirstDefendantForFirstCase().setConvictionDate(PAST_LOCAL_DATE.next());

final UpdateInheritedPleaCommand command = new UpdateInheritedPleaCommand();
command.setHearingIds(asList(hearingId));
command.setPlea(Plea.plea()
.withOffenceId(offenceId)
.withPleaValue(NOT_GUILTY)
.withPleaDate(PAST_LOCAL_DATE.next())
.withOriginatingHearingId(randomUUID())
.build());

final HearingAggregate hearingAggregate = new HearingAggregate() {{
apply(new HearingInitiated(hearingHelper.getHearing()));
}};

when(this.eventSource.getStreamById(hearingId)).thenReturn(this.hearingAggregateEventStream);
when(this.aggregateService.get(this.hearingAggregateEventStream, HearingAggregate.class)).thenReturn(hearingAggregate);
when(this.referenceDataService.retrieveGuiltyPleaTypes()).thenReturn(guiltyPleaTypes);

final JsonEnvelope envelope = envelopeFrom(metadataWithRandomUUID("hearing.command.enrich-update-plea-with-associated-hearings"),
objectToJsonObjectConverter.convert(command));

this.hearingCommandHandler.updateInheritPlea(envelope);

final List<JsonEnvelope> events = verifyAppendAndGetArgumentFrom(this.hearingAggregateEventStream).collect(Collectors.toList());

assertThat(events.size(), is(2));
assertThat(events.get(0).metadata().name(), is("hearing.events.inherited-plea"));
assertThat(events.get(1).metadata().name(), is("hearing.conviction-date-removed"));
assertThat(asPojo(events.get(1), ConvictionDateRemoved.class), isBean(ConvictionDateRemoved.class)
.with(ConvictionDateRemoved::getOffenceId, is(offenceId))
.with(ConvictionDateRemoved::getHearingId, is(hearingId)));
}

@Test
public void updateInheritPleaOnlyRaisesInheritedPleaWhenNotGuiltyWithoutConvictionDate() throws Throwable {
final InitiateHearingCommandHelper hearingHelper = h(standardInitiateHearingTemplate());
final UUID hearingId = hearingHelper.getHearingId();
final UUID offenceId = hearingHelper.getFirstOffenceForFirstDefendantForFirstCase().getId();

final UpdateInheritedPleaCommand command = new UpdateInheritedPleaCommand();
command.setHearingIds(asList(hearingId));
command.setPlea(Plea.plea()
.withOffenceId(offenceId)
.withPleaValue(NOT_GUILTY)
.withPleaDate(PAST_LOCAL_DATE.next())
.withOriginatingHearingId(randomUUID())
.build());

final HearingAggregate hearingAggregate = new HearingAggregate() {{
apply(new HearingInitiated(hearingHelper.getHearing()));
}};

when(this.eventSource.getStreamById(hearingId)).thenReturn(this.hearingAggregateEventStream);
when(this.aggregateService.get(this.hearingAggregateEventStream, HearingAggregate.class)).thenReturn(hearingAggregate);
when(this.referenceDataService.retrieveGuiltyPleaTypes()).thenReturn(guiltyPleaTypes);

final JsonEnvelope envelope = envelopeFrom(metadataWithRandomUUID("hearing.command.enrich-update-plea-with-associated-hearings"),
objectToJsonObjectConverter.convert(command));

this.hearingCommandHandler.updateInheritPlea(envelope);

final List<JsonEnvelope> events = verifyAppendAndGetArgumentFrom(this.hearingAggregateEventStream).collect(Collectors.toList());

assertThat(events.size(), is(1));
assertThat(events.get(0).metadata().name(), is("hearing.events.inherited-plea"));
}

@Test
public void updateInheritPleaAppliesConvictionDateToAllAssociatedHearings() throws Throwable {
final InitiateHearingCommandHelper firstHearingHelper = h(standardInitiateHearingTemplate());
final InitiateHearingCommandHelper secondHearingHelper = h(standardInitiateHearingTemplate());
final UUID firstHearingId = firstHearingHelper.getHearingId();
final UUID secondHearingId = secondHearingHelper.getHearingId();
final UUID offenceId = firstHearingHelper.getFirstOffenceForFirstDefendantForFirstCase().getId();
secondHearingHelper.getFirstOffenceForFirstDefendantForFirstCase().setId(offenceId);

final LocalDate pleaDate = PAST_LOCAL_DATE.next();
final UpdateInheritedPleaCommand command = new UpdateInheritedPleaCommand();
command.setHearingIds(Lists.newArrayList(firstHearingId, secondHearingId));
command.setPlea(Plea.plea()
.withOffenceId(offenceId)
.withPleaValue(GUILTY)
.withPleaDate(pleaDate)
.withOriginatingHearingId(randomUUID())
.build());

final HearingAggregate firstHearingAggregate = new HearingAggregate() {{
apply(new HearingInitiated(firstHearingHelper.getHearing()));
}};
final HearingAggregate secondHearingAggregate = new HearingAggregate() {{
apply(new HearingInitiated(secondHearingHelper.getHearing()));
}};

when(this.eventSource.getStreamById(firstHearingId)).thenReturn(this.hearingAggregateEventStream);
when(this.eventSource.getStreamById(secondHearingId)).thenReturn(this.offenceAggregateEventStream);
when(this.aggregateService.get(this.hearingAggregateEventStream, HearingAggregate.class)).thenReturn(firstHearingAggregate);
when(this.aggregateService.get(this.offenceAggregateEventStream, HearingAggregate.class)).thenReturn(secondHearingAggregate);
when(this.referenceDataService.retrieveGuiltyPleaTypes()).thenReturn(guiltyPleaTypes);

final JsonEnvelope envelope = envelopeFrom(metadataWithRandomUUID("hearing.command.enrich-update-plea-with-associated-hearings"),
objectToJsonObjectConverter.convert(command));

this.hearingCommandHandler.updateInheritPlea(envelope);

final List<JsonEnvelope> firstHearingEvents = verifyAppendAndGetArgumentFrom(this.hearingAggregateEventStream).collect(Collectors.toList());
final List<JsonEnvelope> secondHearingEvents = verifyAppendAndGetArgumentFrom(this.offenceAggregateEventStream).collect(Collectors.toList());

assertThat(firstHearingEvents.size(), is(2));
assertThat(firstHearingEvents.get(0).metadata().name(), is("hearing.events.inherited-plea"));
assertThat(firstHearingEvents.get(1).metadata().name(), is("hearing.conviction-date-added"));

assertThat(secondHearingEvents.size(), is(2));
assertThat(secondHearingEvents.get(0).metadata().name(), is("hearing.events.inherited-plea"));
assertThat(secondHearingEvents.get(1).metadata().name(), is("hearing.conviction-date-added"));
}
}
2 changes: 1 addition & 1 deletion hearing-command/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>uk.gov.moj.cpp.hearing</groupId>
<artifactId>hearing-parent</artifactId>
<version>17.104.180-SNAPSHOT</version>
<version>17.104.180-SNI-9204-SNAPSHOT</version>
</parent>
<artifactId>hearing-command</artifactId>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion hearing-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>hearing-parent</artifactId>
<groupId>uk.gov.moj.cpp.hearing</groupId>
<version>17.104.180-SNAPSHOT</version>
<version>17.104.180-SNI-9204-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion hearing-domain/hearing-domain-aggregate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>uk.gov.moj.cpp.hearing</groupId>
<artifactId>hearing-domain</artifactId>
<version>17.104.180-SNAPSHOT</version>
<version>17.104.180-SNI-9204-SNAPSHOT</version>
</parent>
<artifactId>hearing-domain-aggregate</artifactId>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,12 +602,12 @@ public Stream<Object> updatePlea(final UUID hearingId, final PleaModel plea, fin
return apply(pleaDelegate.updatePlea(hearingId, plea, guiltyPleaTypes));
}

public Stream<Object> inheritPlea(final UUID hearingId, final Plea plea) {
public Stream<Object> inheritPlea(final UUID hearingId, final Plea plea, final Set<String> guiltyPleaTypes) {
if (this.momento.isDeletedOrDuplicated() || SHARED == this.hearingState || checkIfHearingDateHasPassedPleaDate(plea.getPleaDate())) {
return warnEventIgnored(hearingId, "inheritPlea");
}

return apply(this.pleaDelegate.inheritPlea(hearingId, plea));
return apply(this.pleaDelegate.inheritPlea(hearingId, plea, guiltyPleaTypes));
}

public Stream<Object> updateHearingWithIndicatedPlea(final UUID hearingId, final IndicatedPlea indicatedPlea) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,30 @@ public void handlePleaUpsert(final PleaUpsert pleaUpsert) {



public Stream<Object> inheritPlea(final UUID hearingId, final Plea plea) {
return Stream.of(InheritedPlea.inheritedPlea()
public Stream<Object> inheritPlea(final UUID hearingId, final Plea plea, final Set<String> guiltyPleaTypes) {
final List<Object> events = new ArrayList<>();
events.add(InheritedPlea.inheritedPlea()
.setHearingId(hearingId)
.setPlea(plea));

if (!isCivilCase() && nonNull(plea)) {
final UUID offenceId = plea.getOffenceId();
final UUID prosecutionCaseId = ofNullable(this.momento.getHearing().getProsecutionCases()).map(Collection::stream).orElseGet(Stream::empty)
.filter(pc -> pc.getDefendants().stream()
.flatMap(de -> de.getOffences().stream())
.anyMatch(o -> o.getId().equals(offenceId)))
.findFirst()
.map(ProsecutionCase::getId)
.orElse(null);

final UUID courtApplicationId = findCourtApplicationByOffence(offenceId);

if (prosecutionCaseId != null || courtApplicationId != null) {
addConvictionDateEventForPlea(hearingId, guiltyPleaTypes, offenceId, prosecutionCaseId, courtApplicationId, events, plea);
}
}

return events.stream();
}

public Stream<Object> indicatedPlea(final UUID hearingId, final IndicatedPlea indicatedPlea) {
Expand Down
Loading
Loading