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
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ && isNull(def.getIndividual().getPersonalInformation().getAddress()))) {

final InitiateProsecution initiateProsecution = envelope.payload();
final Prosecution ccProsecution = Prosecution.prosecution()
.withCaseDetails(caseDetailsEnrichmentService.enrichCaseDetails(initiateProsecution.getCaseDetails(), initiateProsecution.getCaseDetails().getProsecutor()))
.withCaseDetails(caseDetailsEnrichmentService.enrichCaseDetails(initiateProsecution.getCaseDetails(), initiateProsecution.getCaseDetails().getProsecutor(), channel))
.withChannel(initiateProsecution.getChannel())
.withMigrationSourceSystem(initiateProsecution.getMigrationSourceSystem())
.withListNewHearing(initiateProsecution.getListNewHearing())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void initiateGroupProsecution(final Envelope<InitiateGroupProsecution> en
.stream()
.map(groupProsecution -> {
final List<Defendant> defendants = enrichDefendants(envelope, groupProsecution, prosecutionAuthority, offenceReferenceData);
return new GroupProsecution(this.caseDetailsEnrichmentService.enrichCaseDetails(groupProsecution.getCaseDetails(), groupProsecution.getCaseDetails().getProsecutor()),
return new GroupProsecution(this.caseDetailsEnrichmentService.enrichCaseDetails(groupProsecution.getCaseDetails(), groupProsecution.getCaseDetails().getProsecutor(), initiateGroupProsecution.getChannel()),
defendants,
groupProsecution.getGroupId(),
groupProsecution.getIsCivil(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void initiateSjpProsecution(final Envelope<InitiateProsecution> envelope)
final Prosecution sjpProsecution = prosecution()
.withChannel(initiateProsecution.getChannel())
.withDefendants(defendantsWithReferenceData.getDefendants())
.withCaseDetails(caseDetailsEnrichmentService.enrichCaseDetails(initiateProsecution.getCaseDetails(), prosecutorWithReferenceData))
.withCaseDetails(caseDetailsEnrichmentService.enrichCaseDetails(initiateProsecution.getCaseDetails(), prosecutorWithReferenceData, initiateProsecution.getChannel()))
.withExternalId(initiateProsecution.getExternalId())
.withIsCivil(initiateProsecution.getIsCivil())
.withIsGroupMaster(initiateProsecution.getIsGroupMaster())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.moj.cpp.prosecution.casefile.command.api.service;

import uk.gov.moj.cpp.prosecution.casefile.json.schemas.CaseDetails;
import uk.gov.moj.cpp.prosecution.casefile.json.schemas.Channel;
import uk.gov.moj.cpp.prosecution.casefile.json.schemas.Prosecutor;

import java.util.Optional;
Expand All @@ -11,11 +12,11 @@ public class CaseDetailsEnrichmentService {
@Inject
private IdGenerationService idGenerationService;

public CaseDetails enrichCaseDetails(final CaseDetails caseDetails, Prosecutor prosecutorWithReferenceData) {
public CaseDetails enrichCaseDetails(final CaseDetails caseDetails, final Prosecutor prosecutorWithReferenceData, final Channel channel) {
final String prosecutorCaseReference = Optional.ofNullable(caseDetails.getProsecutorCaseReference())
.orElseGet(() -> idGenerationService.generateCaseReference());
final UUID caseId = Optional.ofNullable(caseDetails.getCaseId())
.orElseGet(() -> idGenerationService.generateCaseId(prosecutorCaseReference));
.orElseGet(() -> idGenerationService.generateCaseId(prosecutorCaseReference, prosecutorWithReferenceData, channel));
return enrichCaseDetailsWithCaseIdAndProsecutorCaseReference(caseId, prosecutorCaseReference, caseDetails, prosecutorWithReferenceData);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import static java.lang.String.format;
import static java.util.UUID.randomUUID;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.apache.commons.text.CharacterPredicates.DIGITS;
import static org.apache.commons.text.CharacterPredicates.LETTERS;
import static uk.gov.moj.cpp.prosecution.casefile.json.schemas.Channel.MCC;

import uk.gov.justice.services.core.dispatcher.SystemUserProvider;
import uk.gov.moj.cpp.prosecution.casefile.json.schemas.Channel;
import uk.gov.moj.cpp.prosecution.casefile.json.schemas.Prosecutor;
import uk.gov.moj.cpp.prosecution.casefile.json.schemas.ProsecutorsReferenceData;
import uk.gov.moj.cpp.systemidmapper.client.AdditionResponse;
import uk.gov.moj.cpp.systemidmapper.client.ResultCode;
import uk.gov.moj.cpp.systemidmapper.client.SystemIdMap;
Expand Down Expand Up @@ -38,21 +41,35 @@ public class IdGenerationService {
@Inject
private SystemIdMapperClient systemIdMapperClient;

public UUID generateCaseId(final String caseReference) {
public UUID generateCaseId(final String caseReference, final Prosecutor prosecutorWithReferenceData, final Channel channel) {
final String effectiveKey = buildEffectiveKey(caseReference, prosecutorWithReferenceData, channel);
final UUID newCaseId = randomUUID();
final Optional<SystemIdMapping> systemIdMapping = fetchSystemIdMappingFor(caseReference);
final Optional<SystemIdMapping> systemIdMapping = fetchSystemIdMappingFor(effectiveKey);
if (systemIdMapping.isPresent()) {
return systemIdMapping.map(SystemIdMapping::getTargetId).orElseThrow(()
-> new IllegalStateException(format("Invalid mapping found against case reference %s", caseReference)));
} else if(addMappingForProsecutorCaseReference(caseReference, newCaseId).isSuccess()) {
} else if (addMappingForProsecutorCaseReference(effectiveKey, newCaseId).isSuccess()) {
return newCaseId;
} else {
throw new IllegalStateException(format("Unable to generate case id for reference %s", caseReference));
}
}

private AdditionResponse addMappingForProsecutorCaseReference(final String caseReference, final UUID caseId) {
final SystemIdMap systemIdMap = new SystemIdMap(caseReference, SOURCE_TYPE, caseId, TARGET_TYPE_CPI_MCC);
private String buildEffectiveKey(final String caseReference, final Prosecutor prosecutorWithReferenceData, final Channel channel) {
if (MCC.equals(channel) && prosecutorWithReferenceData != null) {
final ProsecutorsReferenceData refData = prosecutorWithReferenceData.getReferenceData();
if (refData != null) {
if (Boolean.TRUE.equals(refData.getPoliceFlag())) {
return caseReference;
}
return refData.getOucode() + ":" + caseReference;
}
}
return caseReference;
}

private AdditionResponse addMappingForProsecutorCaseReference(final String effectiveCaseReference, final UUID caseId) {
final SystemIdMap systemIdMap = new SystemIdMap(effectiveCaseReference, SOURCE_TYPE, caseId, TARGET_TYPE_CPI_MCC);
final Optional<UUID> contextSystemUserId = systemUserProvider.getContextSystemUserId();

if (contextSystemUserId.isPresent()) {
Expand All @@ -61,9 +78,9 @@ private AdditionResponse addMappingForProsecutorCaseReference(final String caseR
return new AdditionResponse(caseId, ResultCode.CONFLICT, Optional.of("Failed to add system id mapping"));
}

public Optional<SystemIdMapping> fetchSystemIdMappingFor(final String caseReference) {
public Optional<SystemIdMapping> fetchSystemIdMappingFor(final String effectiveCaseReference) {
return systemIdMapperClient.findBy(systemUserProvider.getContextSystemUserId().orElseThrow(() -> new IllegalStateException(INVALID_CONTEXT_SYSTEM_USER_ID)),
caseReference, TARGET_TYPE_CPI_MCC,TARGET_TYPE_SPI,TARGET_TYPE_SJP);
effectiveCaseReference, TARGET_TYPE_CPI_MCC, TARGET_TYPE_SPI, TARGET_TYPE_SJP);
}

public String generateCaseReference() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void createValidPayloadToAssert(final Channel channel, final boolean isM
when(caseDetails.getProsecutor()).thenReturn(prosecutor);
when(caseDetails.getPoliceSystemId()).thenReturn(POLICE_SYSTEM_ID);
when(prosecutor.getProsecutingAuthority()).thenReturn("OWTW");
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any())).thenReturn(caseDetails);
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any(), any())).thenReturn(caseDetails);

final ProsecutorsReferenceData prosecutorsReferenceData = new ProsecutorsReferenceData.Builder()
.withShortName("OWTW")
Expand Down Expand Up @@ -295,7 +295,7 @@ void shouldSendReceiveCCProsecutionWithReferenceDataCommandWithLocationSetPayloa
when(initiateProsecution.getIsCivil()).thenReturn(isCivil);
when(caseDetails.getProsecutor()).thenReturn(prosecutor);
when(prosecutor.getProsecutingAuthority()).thenReturn("OWTW");
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any())).thenReturn(caseDetails);
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any(), any())).thenReturn(caseDetails);

final ProsecutorsReferenceData prosecutorsReferenceData = new ProsecutorsReferenceData.Builder()
.withShortName("OWTW")
Expand All @@ -318,7 +318,7 @@ void shouldSetDefaultOffenceLocationWhenOffenceHasNullLocationAndProsecutingAuth
final Offence offence = offence().withOffenceLocation(null).build();
final Defendant defendant = defendant().withOffences(ImmutableList.of(offence)).withAddress(Address.address().build()).build();
final Envelope<InitiateProsecution> envelope = envelope(caseProsecution(defendant, DVLA_PROSECUTOR, null, null));
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any())).thenReturn(caseDetails);
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any(), any())).thenReturn(caseDetails);

final ProsecutorsReferenceData prosecutorsReferenceData = new ProsecutorsReferenceData.Builder()
.withShortName("OWTW")
Expand Down Expand Up @@ -350,7 +350,7 @@ void shouldGetProsecutorByIdWhenOUCodeIsNull() {
when(initiateProsecution.getChannel()).thenReturn(Channel.SPI);
when(caseDetails.getProsecutor()).thenReturn(prosecutor);
when(caseDetails.getPoliceSystemId()).thenReturn(POLICE_SYSTEM_ID);
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any())).thenReturn(caseDetails);
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any(), any())).thenReturn(caseDetails);

when(referenceDataQueryService.retrieveOffenceData(any(), any())).thenReturn(Collections.emptyList());

Expand All @@ -373,7 +373,7 @@ void shouldSetOffenceLocationProvidedWhenOffenceHasLocationAndProsecutingAuthori
final Offence offence = offence().withOffenceLocation("My Location").build();
final Defendant defendant = defendant().withOffences(ImmutableList.of(offence)).withAddress(Address.address().build()).build();
final Envelope<InitiateProsecution> envelope = envelope(caseProsecution(defendant, DVLA_PROSECUTOR, null, null));
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any())).thenReturn(caseDetails);
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any(), any())).thenReturn(caseDetails);

final ProsecutorsReferenceData prosecutorsReferenceData = new ProsecutorsReferenceData.Builder()
.withShortName("OWTW")
Expand All @@ -393,7 +393,7 @@ void shouldLeaveOffenceLocationAsIsWhenProsecutingAuthorityIsNonDVLA() {
final Offence offence = offence().withOffenceLocation("Canada").build();
final Defendant defendant = defendant().withOffences(ImmutableList.of(offence)).withAddress(Address.address().build()).build();
final Envelope<InitiateProsecution> envelope = envelope(caseProsecution(defendant, NON_DVLA_PROSECUTOR, null, null));
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any())).thenReturn(caseDetails);
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any(), any())).thenReturn(caseDetails);

final ProsecutorsReferenceData prosecutorsReferenceData = new ProsecutorsReferenceData.Builder()
.withShortName("OWTW")
Expand All @@ -414,7 +414,7 @@ void shouldLeaveOffenceLocationAsIsWhenProsecutingAuthorityIsUnknown() {
final Offence offence = offence().withOffenceLocation("Canada").build();
final Defendant defendant = defendant().withOffences(ImmutableList.of(offence)).withAddress(Address.address().build()).build();
final Envelope<InitiateProsecution> envelope = envelope(caseProsecution(defendant, NON_DVLA_PROSECUTOR, null, null));
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any())).thenReturn(caseDetails);
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any(), any())).thenReturn(caseDetails);

final ProsecutorsReferenceData prosecutorsReferenceData = new ProsecutorsReferenceData.Builder()
.withShortName("OWTW")
Expand Down Expand Up @@ -463,7 +463,7 @@ void shouldPopulatePleaAndVerdict() {
.withLibraReferenceNumber(libraReferenceNumber)
.build();
final Envelope<InitiateProsecution> envelope = envelope(caseProsecution(defendant, NON_DVLA_PROSECUTOR, null, null));
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any())).thenReturn(caseDetails);
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any(), any())).thenReturn(caseDetails);


final ProsecutorsReferenceData prosecutorsReferenceData = new ProsecutorsReferenceData.Builder()
Expand Down Expand Up @@ -527,7 +527,7 @@ void shouldCheckConvictingCourtCodeConditionalMandatory() {
.build())
.build();
final Envelope<InitiateProsecution> envelope = envelope(caseProsecution(defendant, NON_DVLA_PROSECUTOR, migrationSourceSystem, null));
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any())).thenReturn(caseDetails);
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any(), any())).thenReturn(caseDetails);

final ProsecutorsReferenceData prosecutorsReferenceData = new ProsecutorsReferenceData.Builder()
.withShortName("OWTW")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void shouldSendReceiveGroupProsecutionWithReferenceDataCommandWithCorrect
.withInitiationCode("C")
.withPoliceSystemId(POLICE_SYSTEM_ID)
.build();
when(this.caseDetailsEnrichmentService.enrichCaseDetails(any(), any())).thenReturn(caseDetails);
when(this.caseDetailsEnrichmentService.enrichCaseDetails(any(), any(), any())).thenReturn(caseDetails);
when(this.referenceDataQueryService.retrieveOffenceData(any(), any())).thenReturn(singletonList(OffenceReferenceData.offenceReferenceData()
.withLocationRequired("N")
.build()));
Expand Down Expand Up @@ -99,7 +99,7 @@ public void shouldGetProsecutorByIdWhenOUCodeIsNull() throws Exception {
.withCaseId(UUID.fromString("51cac7fb-387c-4d19-9c80-8963fa8cf222"))
.withPoliceSystemId(POLICE_SYSTEM_ID)
.build();
when(this.caseDetailsEnrichmentService.enrichCaseDetails(any(), any())).thenReturn(caseDetails);
when(this.caseDetailsEnrichmentService.enrichCaseDetails(any(), any(), any())).thenReturn(caseDetails);

when(this.referenceDataQueryService.retrieveOffenceData(any(), any())).thenReturn(singletonList(OffenceReferenceData.offenceReferenceData()
.withLocationRequired("N")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private void shouldInitiateSjpProsecution(final String inputPayloadPath, final S

final List<ReferenceDataCountryNationality> referenceDataCountryNationalities = referenceDataCountryNationalities();

when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any())).thenReturn(CaseDetails.caseDetails().build());
when(caseDetailsEnrichmentService.enrichCaseDetails(any(), any(), any())).thenReturn(CaseDetails.caseDetails().build());
when(idGenerator.generateId()).thenReturn(UUID.fromString(UUID_IN_TEST));

initiateSjpProsecutionApi.initiateSjpProsecution(receivedEnvelope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import static java.util.UUID.randomUUID;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.moj.cpp.prosecution.casefile.json.schemas.Channel.SPI;

import uk.gov.moj.cpp.prosecution.casefile.json.schemas.CaseDetails;
import uk.gov.moj.cpp.prosecution.casefile.json.schemas.Channel;
import uk.gov.moj.cpp.prosecution.casefile.json.schemas.Prosecutor;

import java.time.LocalDate;
Expand Down Expand Up @@ -44,7 +47,7 @@ public void shouldReturnOriginalCaseIdAndProsecutorReference() {
.build())
.build();

final CaseDetails response = caseDetailsEnrichmentService.enrichCaseDetails(caseDetails, prosecutor);
final CaseDetails response = caseDetailsEnrichmentService.enrichCaseDetails(caseDetails, prosecutor, SPI);

assertThat(originalCaseId, is(response.getCaseId()));
assertThat(originalProsecutorCaseReference, is(response.getProsecutorCaseReference()));
Expand All @@ -62,8 +65,8 @@ public void shouldReturnGeneratedCaseIdAndProsecutorReference() {
.build();

when(idGenerationService.generateCaseReference()).thenReturn(generatedProsecutorCaseReference);
when(idGenerationService.generateCaseId(anyString())).thenReturn(generatedCaseId);
final CaseDetails response = caseDetailsEnrichmentService.enrichCaseDetails(caseDetails, prosecutor);
when(idGenerationService.generateCaseId(anyString(), any(), any(Channel.class))).thenReturn(generatedCaseId);
final CaseDetails response = caseDetailsEnrichmentService.enrichCaseDetails(caseDetails, prosecutor, SPI);

assertThat(generatedCaseId, is(response.getCaseId()));
assertThat(generatedProsecutorCaseReference, is(response.getProsecutorCaseReference()));
Expand All @@ -85,8 +88,8 @@ public void shouldReturnGeneratedCaseIdAndProsecutorReferenceAndDateOfCommittalA
.build();

when(idGenerationService.generateCaseReference()).thenReturn(generatedProsecutorCaseReference);
when(idGenerationService.generateCaseId(anyString())).thenReturn(generatedCaseId);
final CaseDetails response = caseDetailsEnrichmentService.enrichCaseDetails(caseDetails, prosecutor);
when(idGenerationService.generateCaseId(anyString(), any(), any(Channel.class))).thenReturn(generatedCaseId);
final CaseDetails response = caseDetailsEnrichmentService.enrichCaseDetails(caseDetails, prosecutor, SPI);

assertThat(generatedCaseId, is(response.getCaseId()));
assertThat(generatedProsecutorCaseReference, is(response.getProsecutorCaseReference()));
Expand All @@ -106,8 +109,8 @@ public void shouldNotInvokeIdGenerationServiceForCaseReferenceAndCaseId() {
.build())
.build();

final CaseDetails response = caseDetailsEnrichmentService.enrichCaseDetails(caseDetails, prosecutor);
verify(idGenerationService, times(0)).generateCaseId(originalProsecutorCaseReference);
final CaseDetails response = caseDetailsEnrichmentService.enrichCaseDetails(caseDetails, prosecutor, SPI);
verify(idGenerationService, times(0)).generateCaseId(anyString(), any(), any(Channel.class));
verify(idGenerationService, times(0)).generateCaseReference();

assertThat(originalCaseId, is(response.getCaseId()));
Expand Down
Loading
Loading