Skip to content
Open
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 @@ -72,7 +72,7 @@
<usersgroups.version>17.104.48</usersgroups.version>
<defence.version>17.0.85</defence.version>
<referencedata.offence.version>17.103.42</referencedata.offence.version>
<coredomain.version>17.103.14-SNAPSHOT</coredomain.version>
<coredomain.version>17.103.13.1977-SNAPSHOT</coredomain.version>
<directionsmanagement.version>8.0.30</directionsmanagement.version>
<elasticsearch-maven-plugin.version>6.13</elasticsearch-maven-plugin.version>
<results.version>17.103.105</results.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import uk.gov.justice.core.courts.CourtApplicationPartyListingNeeds;
import uk.gov.justice.core.courts.CreateHearingApplicationRequest;
import uk.gov.justice.core.courts.SummonsApprovedOutcome;
import uk.gov.justice.services.core.aggregate.AggregateService;
import uk.gov.justice.services.core.annotation.Component;
import uk.gov.justice.services.core.annotation.Handles;
Expand All @@ -17,9 +16,6 @@
import uk.gov.moj.cpp.progression.aggregate.HearingAggregate;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;

import javax.inject.Inject;
Expand Down Expand Up @@ -48,35 +44,6 @@ public void handle(final Envelope<CreateHearingApplicationRequest> createHearing
final HearingAggregate hearingAggregate = aggregateService.get(eventStream, HearingAggregate.class);
final Stream<Object> events = hearingAggregate.createHearingApplicationRequest(applicationRequests);
appendEventsToStream(createHearingApplicationRequest, eventStream, events);

linkBoxworkHearingToFirstHearing(createHearingApplicationRequest);
}

private void linkBoxworkHearingToFirstHearing(final Envelope<CreateHearingApplicationRequest> requestEnvelope) throws EventStreamException {
final Optional<UUID> boxworkHearingId = getBoxworkHearingId(requestEnvelope);

if (boxworkHearingId.isPresent()) {
final UUID bxwHearingId = boxworkHearingId.get();
final UUID firstHearingId = requestEnvelope.payload().getHearingId();
final EventStream boxworkHearingEventStream = eventSource.getStreamById(bxwHearingId);
final HearingAggregate boxworkHearingAggregate = aggregateService.get(boxworkHearingEventStream, HearingAggregate.class);

if (boxworkHearingAggregate.isLinkedToFirstHearing()) {
LOGGER.info("Boxwork hearing {} already linked to the first hearing {}", bxwHearingId, firstHearingId);
return;
}

final Stream<Object> linkEvents = boxworkHearingAggregate.linkBoxworkHearing(bxwHearingId, firstHearingId);
appendEventsToStream(requestEnvelope, boxworkHearingEventStream, linkEvents);
}
}

private static Optional<UUID> getBoxworkHearingId(final Envelope<CreateHearingApplicationRequest> requestEnvelope) {
return requestEnvelope.payload().getApplicationRequests().stream()
.map(CourtApplicationPartyListingNeeds::getSummonsApprovedOutcome)
.filter(Objects::nonNull)
.map(SummonsApprovedOutcome::getHearingId)
.findFirst();
}

private void appendEventsToStream(final Envelope<?> envelope, final EventStream eventStream, final Stream<Object> events) throws EventStreamException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.slf4j.LoggerFactory;
import uk.gov.justice.core.courts.CreateHearingDefendantRequest;
import uk.gov.justice.core.courts.ListDefendantRequest;
import uk.gov.justice.core.courts.SummonsApprovedOutcome;
import uk.gov.justice.services.core.aggregate.AggregateService;
import uk.gov.justice.services.core.annotation.Component;
import uk.gov.justice.services.core.annotation.Handles;
Expand All @@ -19,9 +18,6 @@
import javax.inject.Inject;
import javax.json.JsonValue;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Stream;

@ServiceComponent(Component.COMMAND_HANDLER)
Expand All @@ -41,15 +37,12 @@ public class CreateHearingDefendantRequestHandler {

@Handles("progression.command.create-hearing-defendant-request")
public void handle(final Envelope<CreateHearingDefendantRequest> createHearingDefendantRequestEnvelope) throws EventStreamException {
LOGGER.debug("progression.command.create-hearing-defendant-request {}", createHearingDefendantRequestEnvelope);

LOGGER.debug("progression.command.create-hearing-defendant-request {}", createHearingDefendantRequestEnvelope );
final List<ListDefendantRequest> listDefendantRequests = createHearingDefendantRequestEnvelope.payload().getDefendantRequests();
final EventStream eventStream = eventSource.getStreamById(createHearingDefendantRequestEnvelope.payload().getHearingId());
final HearingAggregate hearingAggregate = aggregateService.get(eventStream, HearingAggregate.class);
final Stream<Object> events = hearingAggregate.createHearingDefendantRequest(listDefendantRequests);
appendEventsToStream(createHearingDefendantRequestEnvelope, eventStream, events);

linkBoxworkHearingToFirstHearing(createHearingDefendantRequestEnvelope);
}

private void appendEventsToStream(final Envelope<?> envelope, final EventStream eventStream, final Stream<Object> events) throws EventStreamException {
Expand All @@ -58,33 +51,4 @@ private void appendEventsToStream(final Envelope<?> envelope, final EventStream
events
.map(enveloper.withMetadataFrom(jsonEnvelope)));
}

private void linkBoxworkHearingToFirstHearing (final Envelope<CreateHearingDefendantRequest> requestEnvelope) throws EventStreamException {
final Optional<UUID> boxworkHearingId = getBoxworkHearingId(requestEnvelope);

if (boxworkHearingId.isPresent() ) {
final UUID bxwHearingId = boxworkHearingId.get();
final UUID firstHearingId = requestEnvelope.payload().getHearingId();
final EventStream boxworkHearingEventStream = eventSource.getStreamById(bxwHearingId);
final HearingAggregate boxworkHearingAggregate = aggregateService.get(boxworkHearingEventStream, HearingAggregate.class);

if (boxworkHearingAggregate.isLinkedToFirstHearing()) {
LOGGER.info("Boxwork hearing {} already linked to the first case hearing {}", boxworkHearingId, firstHearingId);
return;
}

final Stream<Object> linkEvents = boxworkHearingAggregate.linkBoxworkHearing(bxwHearingId, firstHearingId);

appendEventsToStream(requestEnvelope, boxworkHearingEventStream, linkEvents);
}
}

private static Optional<UUID> getBoxworkHearingId(final Envelope<CreateHearingDefendantRequest> createHearingDefendantRequestEnvelope) {
return createHearingDefendantRequestEnvelope.payload().getDefendantRequests().stream()
.map(ListDefendantRequest::getSummonsApprovedOutcome)
.filter(Objects::nonNull)
.map(SummonsApprovedOutcome::getHearingId)
.filter(Objects::nonNull)
.findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,15 @@ public void amendSummonsData(final Envelope<AmendSummonsData> amendSummonsDataEn
LOGGER.debug("progression.command.amend-summons-data {}", amendSummonsDataEnvelope);

final AmendSummonsData requestSummons = amendSummonsDataEnvelope.payload();
final UUID boxworkHearingId = requestSummons.getSummonsApprovedOutcome().getHearingId();
final EventStream eventStream = eventSource.getStreamById(boxworkHearingId);
final HearingAggregate bwHearingAggregate = aggregateService.get(eventStream, HearingAggregate.class);
final UUID boxWorkHearingId = requestSummons.getHearingId();
final EventStream eventStream = eventSource.getStreamById(boxWorkHearingId);

//Get linked hearing and invoke amend on that
final UUID firstHearingId = bwHearingAggregate.getBoxworkFirstHearingId();

if (firstHearingId == null) {
LOGGER.warn("Boxwork hearing {} has no linked first hearing, skipping amend", boxworkHearingId);
return;
}

final EventStream fhEventStream = eventSource.getStreamById(firstHearingId);
final HearingAggregate hearingAggregate = aggregateService.get(fhEventStream, HearingAggregate.class);
final HearingAggregate hearingAggregate = aggregateService.get(eventStream, HearingAggregate.class);

final Stream<Object> events = hearingAggregate.amendSummonsData(requestSummons.getSummonsApprovedOutcome());
if ( events != null) {
appendEventsToStream(amendSummonsDataEnvelope, fhEventStream, events);
appendEventsToStream(amendSummonsDataEnvelope, eventStream, events);
}

}

@Handles("progression.command.prepare-summons-data-for-extended-hearing")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"id": "http://justice.gov.uk/core/courts/amend-summons-data.json",
"type": "object",
"properties": {
"hearingId": {
"$ref": "http://justice.gov.uk/domain/core/common/definitions.json#/definitions/uuid"
},
"summonsApprovedOutcome": {
"$ref": "http://justice.gov.uk/core/courts/summonsApprovedOutcome.json"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.justice.services.core.annotation.Component.COMMAND_HANDLER;
import static uk.gov.justice.services.messaging.Envelope.envelopeFrom;
Expand All @@ -21,8 +19,6 @@
import uk.gov.justice.core.courts.CreateHearingDefendantRequest;
import uk.gov.justice.core.courts.HearingDefendantRequestCreated;
import uk.gov.justice.core.courts.ListDefendantRequest;
import uk.gov.justice.core.courts.SummonsApprovedOutcome;
import uk.gov.justice.core.progression.courts.BoxworkHearingLinked;
import uk.gov.justice.services.core.aggregate.AggregateService;
import uk.gov.justice.services.core.enveloper.Enveloper;
import uk.gov.justice.services.eventsourcing.source.core.EventSource;
Expand All @@ -33,7 +29,6 @@
import uk.gov.justice.services.messaging.Metadata;
import uk.gov.justice.services.test.utils.core.enveloper.EnveloperFactory;
import uk.gov.justice.services.test.utils.core.matchers.JsonEnvelopePayloadMatcher;
import uk.gov.justice.services.test.utils.core.reflection.ReflectionUtil;
import uk.gov.moj.cpp.progression.aggregate.HearingAggregate;

import java.util.Arrays;
Expand All @@ -59,16 +54,12 @@ public class CreateHearingDefendantRequestHandlerTest {
@Mock
private EventStream eventStream;

@Mock
private EventStream boxworkEventStream;

@Mock
private AggregateService aggregateService;

@Spy
private final Enveloper enveloper = EnveloperFactory.createEnveloperWithEvents(
HearingDefendantRequestCreated.class,
BoxworkHearingLinked.class);
HearingDefendantRequestCreated.class);

@InjectMocks
private CreateHearingDefendantRequestHandler handler;
Expand Down Expand Up @@ -121,111 +112,4 @@ public void shouldHandleCommand() {
));
}

@Test
public void shouldLinkBoxworkHearingToFirstHearingWhenSummonsApprovedOutcomePresent() throws EventStreamException {
final UUID firstHearingId = randomUUID();
final UUID boxworkHearingId = randomUUID();

final HearingAggregate firstHearingAggregate = new HearingAggregate();
final HearingAggregate boxworkHearingAggregate = new HearingAggregate();

when(eventSource.getStreamById(firstHearingId)).thenReturn(eventStream);
when(eventSource.getStreamById(boxworkHearingId)).thenReturn(boxworkEventStream);
when(aggregateService.get(eventStream, HearingAggregate.class)).thenReturn(firstHearingAggregate);
when(aggregateService.get(boxworkEventStream, HearingAggregate.class)).thenReturn(boxworkHearingAggregate);

final SummonsApprovedOutcome summonsApprovedOutcome = SummonsApprovedOutcome.summonsApprovedOutcome()
.withHearingId(boxworkHearingId)
.build();

final CreateHearingDefendantRequest payload = CreateHearingDefendantRequest.createHearingDefendantRequest()
.withHearingId(firstHearingId)
.withDefendantRequests(Arrays.asList(ListDefendantRequest.listDefendantRequest()
.withDefendantId(DEFENDANT_ID)
.withProsecutionCaseId(CASE_ID)
.withSummonsApprovedOutcome(summonsApprovedOutcome)
.build()))
.build();

final Metadata metadata = Envelope.metadataBuilder()
.withName("progression.command.create-hearing-defendant-request")
.withId(randomUUID())
.build();

handler.handle(envelopeFrom(metadata, payload));

final Stream<JsonEnvelope> boxworkStream = verifyAppendAndGetArgumentFrom(boxworkEventStream);
assertThat(boxworkStream, streamContaining(
jsonEnvelope(
metadata().withName("progression.event.boxwork-hearing-linked"),
JsonEnvelopePayloadMatcher.payload().isJson(allOf(
withJsonPath("$.boxworkHearingId", is(boxworkHearingId.toString())),
withJsonPath("$.firstHearingId", is(firstHearingId.toString()))
)))
));
}

@Test
public void shouldNotLinkBoxworkHearingWhenAlreadyLinked() throws EventStreamException {
final UUID firstHearingId = randomUUID();
final UUID boxworkHearingId = randomUUID();

final HearingAggregate firstHearingAggregate = new HearingAggregate();
final HearingAggregate boxworkHearingAggregate = new HearingAggregate();
ReflectionUtil.setField(boxworkHearingAggregate, "firstHearingId", randomUUID());

when(eventSource.getStreamById(firstHearingId)).thenReturn(eventStream);
when(eventSource.getStreamById(boxworkHearingId)).thenReturn(boxworkEventStream);
when(aggregateService.get(eventStream, HearingAggregate.class)).thenReturn(firstHearingAggregate);
when(aggregateService.get(boxworkEventStream, HearingAggregate.class)).thenReturn(boxworkHearingAggregate);

final SummonsApprovedOutcome summonsApprovedOutcome = SummonsApprovedOutcome.summonsApprovedOutcome()
.withHearingId(boxworkHearingId)
.build();

final CreateHearingDefendantRequest payload = CreateHearingDefendantRequest.createHearingDefendantRequest()
.withHearingId(firstHearingId)
.withDefendantRequests(Arrays.asList(ListDefendantRequest.listDefendantRequest()
.withDefendantId(DEFENDANT_ID)
.withProsecutionCaseId(CASE_ID)
.withSummonsApprovedOutcome(summonsApprovedOutcome)
.build()))
.build();

final Metadata metadata = Envelope.metadataBuilder()
.withName("progression.command.create-hearing-defendant-request")
.withId(randomUUID())
.build();

handler.handle(envelopeFrom(metadata, payload));

verify(boxworkEventStream, never()).append(any());
}

@Test
public void shouldNotAttemptBoxworkLinkingWhenNoSummonsApprovedOutcome() throws EventStreamException {
final UUID firstHearingId = randomUUID();
final HearingAggregate firstHearingAggregate = new HearingAggregate();

when(eventSource.getStreamById(firstHearingId)).thenReturn(eventStream);
when(aggregateService.get(eventStream, HearingAggregate.class)).thenReturn(firstHearingAggregate);

final CreateHearingDefendantRequest payload = CreateHearingDefendantRequest.createHearingDefendantRequest()
.withHearingId(firstHearingId)
.withDefendantRequests(Arrays.asList(ListDefendantRequest.listDefendantRequest()
.withDefendantId(DEFENDANT_ID)
.withProsecutionCaseId(CASE_ID)
.build()))
.build();

final Metadata metadata = Envelope.metadataBuilder()
.withName("progression.command.create-hearing-defendant-request")
.withId(randomUUID())
.build();

handler.handle(envelopeFrom(metadata, payload));

verify(boxworkEventStream, never()).append(any());
}

}
Loading
Loading