diff --git a/pom.xml b/pom.xml index 0a68f39d3..87c1b64b5 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,7 @@ 17.0.75 1.0.6 6.4.1 - 17.104.136 + 17.104.137 17.104.3 17.104.180 17.104.50 diff --git a/progression-domain/progression-domain-message/src/raml/json/schema/progression.related-reference.json b/progression-domain/progression-domain-message/src/raml/json/schema/progression.related-reference.json index 50477097d..d6dcf922e 100644 --- a/progression-domain/progression-domain-message/src/raml/json/schema/progression.related-reference.json +++ b/progression-domain/progression-domain-message/src/raml/json/schema/progression.related-reference.json @@ -11,10 +11,6 @@ }, "prosecutionCaseId": { "$ref": "http://justice.gov.uk/domain/core/common/definitions.json#/definitions/uuid" - }, - "isCivil": { - "description": "True if the related case is a civil case, false if criminal, absent if the related case cannot be resolved", - "type": "boolean" } }, "required": [ diff --git a/progression-integration-test/src/test/java/uk/gov/moj/cpp/progression/LinkCasesIT.java b/progression-integration-test/src/test/java/uk/gov/moj/cpp/progression/LinkCasesIT.java index 44fd26a97..9524518cf 100644 --- a/progression-integration-test/src/test/java/uk/gov/moj/cpp/progression/LinkCasesIT.java +++ b/progression-integration-test/src/test/java/uk/gov/moj/cpp/progression/LinkCasesIT.java @@ -1,13 +1,11 @@ package uk.gov.moj.cpp.progression; -import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasNoJsonPath; import static com.jayway.jsonpath.matchers.JsonPathMatchers.withJsonPath; import static java.util.Collections.emptyList; import static java.util.UUID.randomUUID; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; -import static uk.gov.moj.cpp.progression.helper.PreAndPostConditionHelper.civilCaseInitiateCourtProceedings; import static uk.gov.moj.cpp.progression.helper.PreAndPostConditionHelper.deleteRelatedReference; import static uk.gov.moj.cpp.progression.helper.PreAndPostConditionHelper.generateUrn; import static uk.gov.moj.cpp.progression.helper.PreAndPostConditionHelper.getCaseLsmInfoFor; @@ -19,7 +17,6 @@ import static uk.gov.moj.cpp.progression.helper.PreAndPostConditionHelper.splitCase; import static uk.gov.moj.cpp.progression.helper.PreAndPostConditionHelper.unlinkCases; import static uk.gov.moj.cpp.progression.util.LSMCasesHelper.getLsmQueryMatchers; -import static uk.gov.moj.cpp.progression.util.ReferProsecutionCaseToCrownCourtHelper.getCivilProsecutionCaseMatchers; import static uk.gov.moj.cpp.progression.util.ReferProsecutionCaseToCrownCourtHelper.getProsecutionCaseMatchers; import uk.gov.justice.services.common.converter.StringToJsonObjectConverter; @@ -149,11 +146,8 @@ void shouldRelatedReferenceUrnOnCaseCreation() { initiateCourtProceedings(PROGRESSION_COMMAND_INITIATE_COURT_PROCEEDINGS_WITH_RELATED_URN_JSON, prosecutionCaseId_2, defendantId_2, randomUUID().toString(), materialIdActive, materialIdDeleted, referralReasonId, caseUrn_2, listedStartDateTime, earliestStartDateTime, defendantDOB, caseUrn_1); pollProsecutionCasesProgressionFor(prosecutionCaseId_2, getProsecutionCaseMatchers(prosecutionCaseId_2, defendantId_2, emptyList())); - // caseUrn_1 is never ingested as a real case here, so its case type cannot be determined List matchers = List.of( - withJsonPath("$.isCivil", is(false)), - withJsonPath("$.relatedReferenceList[0].prosecutionCaseId", is(prosecutionCaseId_2)), - hasNoJsonPath("$.relatedReferenceList[0].isCivil") + withJsonPath("$.relatedReferenceList[0].prosecutionCaseId", is(prosecutionCaseId_2)) ); Matcher[] relatedReferenceMatchers = matchers.toArray(new Matcher[0]); @@ -166,52 +160,4 @@ void shouldRelatedReferenceUrnOnCaseCreation() { deleteRelatedReference(prosecutionCaseId_2, relatedReferenceId); } - - @Test - void shouldReturnIsCivilFalseForBothPrimaryAndRelatedCaseWhenBothAreCriminal() throws IOException { - - initiateCourtProceedings(INITIAL_COURT_PROCEEDINGS_WITH_MULTIPLE_DEFENDANTS, prosecutionCaseId_1, defendantId_1, randomUUID().toString(), materialIdActive, materialIdDeleted, referralReasonId, caseUrn_1, listedStartDateTime, earliestStartDateTime, defendantDOB); - pollProsecutionCasesProgressionFor(prosecutionCaseId_1, getProsecutionCaseMatchers(prosecutionCaseId_1, defendantId_1, emptyList())); - - initiateCourtProceedings(PROGRESSION_COMMAND_INITIATE_COURT_PROCEEDINGS_WITH_RELATED_URN_JSON, prosecutionCaseId_2, defendantId_2, randomUUID().toString(), materialIdActive, materialIdDeleted, referralReasonId, caseUrn_2, listedStartDateTime, earliestStartDateTime, defendantDOB, caseUrn_1); - pollProsecutionCasesProgressionFor(prosecutionCaseId_2, getProsecutionCaseMatchers(prosecutionCaseId_2, defendantId_2, emptyList())); - - List matchers = List.of( - withJsonPath("$.isCivil", is(false)), - withJsonPath("$.relatedReferenceList[0].prosecutionCaseId", is(prosecutionCaseId_2)), - withJsonPath("$.relatedReferenceList[0].isCivil", is(false)) - ); - - JsonObject responseAsJson = new StringToJsonObjectConverter().convert(getRelatedReference(prosecutionCaseId_2, matchers.toArray(new Matcher[0]))); - final String relatedReferenceId = responseAsJson.getJsonArray("relatedReferenceList").getJsonObject(0).getString("relatedReferenceId"); - - assertThat(responseAsJson.getBoolean("isCivil"), is(false)); - assertThat(responseAsJson.getJsonArray("relatedReferenceList").getJsonObject(0).getBoolean("isCivil"), is(false)); - - deleteRelatedReference(prosecutionCaseId_2, relatedReferenceId); - } - - @Test - void shouldReturnIsCivilTrueForRelatedCaseWhenPrimaryIsCriminalAndRelatedIsCivil() { - - civilCaseInitiateCourtProceedings(prosecutionCaseId_1, defendantId_1, materialIdActive, materialIdDeleted, referralReasonId, caseUrn_1, listedStartDateTime, earliestStartDateTime, defendantDOB, randomUUID().toString()); - pollProsecutionCasesProgressionFor(prosecutionCaseId_1, getCivilProsecutionCaseMatchers(prosecutionCaseId_1, defendantId_1, emptyList())); - - initiateCourtProceedings(PROGRESSION_COMMAND_INITIATE_COURT_PROCEEDINGS_WITH_RELATED_URN_JSON, prosecutionCaseId_2, defendantId_2, randomUUID().toString(), materialIdActive, materialIdDeleted, referralReasonId, caseUrn_2, listedStartDateTime, earliestStartDateTime, defendantDOB, caseUrn_1); - pollProsecutionCasesProgressionFor(prosecutionCaseId_2, getProsecutionCaseMatchers(prosecutionCaseId_2, defendantId_2, emptyList())); - - List matchers = List.of( - withJsonPath("$.isCivil", is(false)), - withJsonPath("$.relatedReferenceList[0].prosecutionCaseId", is(prosecutionCaseId_2)), - withJsonPath("$.relatedReferenceList[0].isCivil", is(true)) - ); - - JsonObject responseAsJson = new StringToJsonObjectConverter().convert(getRelatedReference(prosecutionCaseId_2, matchers.toArray(new Matcher[0]))); - final String relatedReferenceId = responseAsJson.getJsonArray("relatedReferenceList").getJsonObject(0).getString("relatedReferenceId"); - - assertThat(responseAsJson.getBoolean("isCivil"), is(false)); - assertThat(responseAsJson.getJsonArray("relatedReferenceList").getJsonObject(0).getBoolean("isCivil"), is(true)); - - deleteRelatedReference(prosecutionCaseId_2, relatedReferenceId); - } } diff --git a/progression-integration-test/src/test/java/uk/gov/moj/cpp/progression/ProsecutionCaseRelatedCasesIT.java b/progression-integration-test/src/test/java/uk/gov/moj/cpp/progression/ProsecutionCaseRelatedCasesIT.java index de2a68501..071ef8e2b 100644 --- a/progression-integration-test/src/test/java/uk/gov/moj/cpp/progression/ProsecutionCaseRelatedCasesIT.java +++ b/progression-integration-test/src/test/java/uk/gov/moj/cpp/progression/ProsecutionCaseRelatedCasesIT.java @@ -105,6 +105,7 @@ public void shouldVerifyRelatedCasesWhenAllCasesInActive() throws Exception { withJsonPath("$.relatedCases[0].masterDefendantId", equalTo(masterDefendantId_1)), withJsonPath("$.relatedCases[0].cases[0].caseId", equalTo(prosecutionCaseId_2)), withJsonPath("$.relatedCases[0].cases[0].caseStatus", equalTo("ACTIVE")), + withJsonPath("$.relatedCases[0].cases[0].isCivil", equalTo(false)), withJsonPath("$.relatedCases[0].cases[0].offences[0].offenceTitle", equalTo("ROBBERY")), withJsonPath("$.relatedCases[0].cases[0].offences[0].maxPenalty", equalTo("Max Penalty")) ); @@ -114,6 +115,7 @@ public void shouldVerifyRelatedCasesWhenAllCasesInActive() throws Exception { withJsonPath("$.relatedCases[0].masterDefendantId", equalTo(masterDefendantId_1)), withJsonPath("$.relatedCases[0].cases[0].caseId", equalTo(prosecutionCaseId_1)), withJsonPath("$.relatedCases[0].cases[0].caseStatus", equalTo("ACTIVE")), + withJsonPath("$.relatedCases[0].cases[0].isCivil", equalTo(false)), withJsonPath("$.relatedCases[0].cases[0].offences[0].offenceTitle", equalTo("ROBBERY")) ); @@ -125,6 +127,7 @@ public void shouldVerifyRelatedCasesWhenAllCasesInActive() throws Exception { withJsonPath("$.relatedCases[0].masterDefendantId", equalTo(masterDefendantId_1)), withJsonPath("$.relatedCases[0].cases[0].caseId", equalTo(prosecutionCaseId_2)), withJsonPath("$.relatedCases[0].cases[0].caseStatus", equalTo("INACTIVE")), + withJsonPath("$.relatedCases[0].cases[0].isCivil", equalTo(false)), withJsonPath("$.relatedCases[0].cases[0].offences[0].offenceTitle", equalTo("ROBBERY")) ); @@ -133,6 +136,7 @@ public void shouldVerifyRelatedCasesWhenAllCasesInActive() throws Exception { withJsonPath("$.relatedCases[0].masterDefendantId", equalTo(masterDefendantId_1)), withJsonPath("$.relatedCases[0].cases[0].caseId", equalTo(prosecutionCaseId_1)), withJsonPath("$.relatedCases[0].cases[0].caseStatus", equalTo("INACTIVE")), + withJsonPath("$.relatedCases[0].cases[0].isCivil", equalTo(false)), withJsonPath("$.relatedCases[0].cases[0].offences[0].offenceTitle", equalTo("ROBBERY")) ); } @@ -162,6 +166,44 @@ public void shouldVerifyRelatedCasesWhenCasesAreMix() throws IOException { ); } + @Test + public void shouldReturnIsCivilTrueForBothPrimaryAndRelatedCaseWhenBothAreCivil() throws IOException { + verifyIsCivilOnMatchedDefendantCases(true, true); + } + + @Test + public void shouldReturnIsCivilFalseForRelatedCaseWhenPrimaryIsCivilAndRelatedIsCriminal() throws IOException { + verifyIsCivilOnMatchedDefendantCases(true, false); + } + + @Test + public void shouldReturnIsCivilTrueForRelatedCaseWhenPrimaryIsCriminalAndRelatedIsCivil() throws IOException { + verifyIsCivilOnMatchedDefendantCases(false, true); + } + + private void verifyIsCivilOnMatchedDefendantCases(final boolean isCivilCase1, final boolean isCivilCase2) throws IOException { + initiateCourtProceedingsForDefendantMatching(prosecutionCaseId_1, defendantId_1_forMasterDefendantId_1, masterDefendantId_1, materialIdActive, materialIdDeleted, referralReasonId, listedStartDateTime, earliestStartDateTime, defendantDOB, isCivilCase1); + pollCaseAndGetHearingForDefendant(prosecutionCaseId_1, defendantId_1_forMasterDefendantId_1); + + initiateCourtProceedingsForDefendantMatching(prosecutionCaseId_2, defendantId_2_forMasterDefendantId_1, defendantId_2_forMasterDefendantId_1, materialIdActive, materialIdDeleted, referralReasonId, listedStartDateTime, earliestStartDateTime, defendantDOB, isCivilCase2); + final Matcher[] prosecutionCaseMatchers = getProsecutionCaseMatchers(prosecutionCaseId_2, defendantId_2_forMasterDefendantId_1, emptyList()); + pollProsecutionCasesProgressionFor(prosecutionCaseId_2, prosecutionCaseMatchers); + // match defendantId_2_forMasterDefendantId_1 associated to case 2 + matchDefendant(prosecutionCaseId_2, defendantId_2_forMasterDefendantId_1, prosecutionCaseId_1, defendantId_1_forMasterDefendantId_1, masterDefendantId_1); + + pollProsecutionCasesProgressionFor(prosecutionCaseId_1, + withJsonPath("$.prosecutionCase.isCivil", equalTo(isCivilCase1)), + withJsonPath("$.relatedCases[0].cases[0].caseId", equalTo(prosecutionCaseId_2)), + withJsonPath("$.relatedCases[0].cases[0].isCivil", equalTo(isCivilCase2)) + ); + + pollProsecutionCasesProgressionFor(prosecutionCaseId_2, + withJsonPath("$.prosecutionCase.isCivil", equalTo(isCivilCase2)), + withJsonPath("$.relatedCases[0].cases[0].caseId", equalTo(prosecutionCaseId_1)), + withJsonPath("$.relatedCases[0].cases[0].isCivil", equalTo(isCivilCase1)) + ); + } + private void closeTheCase(final String caseId, final String defendantId, final String hearingId) { final JsonEnvelope publicEventEnvelope = envelopeFrom(buildMetadata(PUBLIC_HEARING_RESULTED, randomUUID()), getHearingWithSingleCaseJsonObject(PUBLIC_HEARING_RESULTED_CASE_UPDATED + ".json", caseId, diff --git a/progression-integration-test/src/test/java/uk/gov/moj/cpp/progression/helper/PreAndPostConditionHelper.java b/progression-integration-test/src/test/java/uk/gov/moj/cpp/progression/helper/PreAndPostConditionHelper.java index e06f7affc..b1a333a08 100644 --- a/progression-integration-test/src/test/java/uk/gov/moj/cpp/progression/helper/PreAndPostConditionHelper.java +++ b/progression-integration-test/src/test/java/uk/gov/moj/cpp/progression/helper/PreAndPostConditionHelper.java @@ -313,6 +313,20 @@ public static Response initiateCourtProceedingsForDefendantMatching(final String } + public static Response initiateCourtProceedingsForDefendantMatching(final String caseId, + final String defendantId, + final String masterDefendantId, + final String materialIdOne, + final String materialIdTwo, + final String referralId, + final String listedStartDateTime, final String earliestStartDateTime, final String dob, + final boolean isCivil) { + return postCommand(getWriteUrl("/initiatecourtproceedings"), + "application/vnd.progression.initiate-court-proceedings+json", + getInitiateCourtProceedingsJsonBodyForDefendantMatching(caseId, defendantId, masterDefendantId, materialIdOne, materialIdTwo, referralId, generateUrn(), listedStartDateTime, earliestStartDateTime, dob, isCivil)); + + } + public static Response initiateCourtProceedingsForLegalEntityDefendantMatching(final String caseId, final String defendantId, final String masterDefendantId, @@ -375,17 +389,6 @@ public static Response civilCaseInitiateCourtProceedings(final String caseId, fi } - public static Response civilCaseInitiateCourtProceedings(final String caseId, final String defendantId, final String materialIdOne, - final String materialIdTwo, - final String referralId, final String caseUrn, - final String listedStartDateTime, final String earliestStartDateTime, final String dob, final String feesId) { - return postCommand(getWriteUrl("/initiatecourtproceedings"), - "application/vnd.progression.initiate-court-proceedings+json", - getCivilCaseInitiateCourtProceedingsJsonFromResource("progression.command.civil-case-initiate-court-proceedings.json", caseId, - defendantId, materialIdOne, materialIdTwo, referralId, caseUrn, listedStartDateTime, earliestStartDateTime, dob, feesId, "")); - - } - public static Response initiateCourtProceedingsForGroupCases(final UUID masterCaseId, final Map> caseDefendantOffence, final String listedStartDateTime, final String earliestStartDateTime, final String groupId, final String courtCenterId, final String courtCenterName) throws JSONException { final String payload = getInitiateCourtProceedingsForGroupCasesJsonBody(masterCaseId, caseDefendantOffence, listedStartDateTime, earliestStartDateTime, groupId, courtCenterId, courtCenterName); return postCommand(getWriteUrl("/initiatecourtproceedings"), @@ -860,7 +863,7 @@ private static String getInitiateCourtProceedingsJsonFromResourceForDefendantMat final String materialIdOne, final String materialIdTwo, final String referralId, final String caseUrn, final String listedStartDateTime, final String earliestStartDateTime, - final String dob) { + final String dob, final boolean isCivil) { String payload = getPayload(resourceLocation) .replace("RANDOM_CASE_ID", caseId) .replace("RANDOM_REFERENCE", caseUrn) @@ -871,7 +874,8 @@ private static String getInitiateCourtProceedingsJsonFromResourceForDefendantMat .replace("RANDOM_MATERIAL_ID_TWO", materialIdTwo) .replace("RANDOM_REFERRAL_ID", referralId) .replace("LISTED_START_DATE_TIME", listedStartDateTime) - .replace("EARLIEST_START_DATE_TIME", earliestStartDateTime); + .replace("EARLIEST_START_DATE_TIME", earliestStartDateTime) + .replace("IS_CIVIL_FLAG", String.valueOf(isCivil)); if (Objects.nonNull(dob)) { payload = payload.replace("DOB", dob); @@ -1050,8 +1054,16 @@ private static String getInitiateCourtProceedingsJsonBodyForDefendantMatching(fi final String referralId, final String caseUrn, final String listedStartDateTime, final String earliestStartDateTime, final String dob) { + return getInitiateCourtProceedingsJsonBodyForDefendantMatching(caseId, defendantId, masterDefendantId, materialIdOne, materialIdTwo, referralId, caseUrn, listedStartDateTime, earliestStartDateTime, dob, false); + } + + private static String getInitiateCourtProceedingsJsonBodyForDefendantMatching(final String caseId, final String defendantId, final String masterDefendantId, final String materialIdOne, + final String materialIdTwo, + final String referralId, final String caseUrn, + final String listedStartDateTime, final String earliestStartDateTime, + final String dob, final boolean isCivil) { return getInitiateCourtProceedingsJsonFromResourceForDefendantMatching("progression.command.initiate-court-proceedings-for-defendant-matching.json", caseId, - defendantId, masterDefendantId, materialIdOne, materialIdTwo, referralId, caseUrn, listedStartDateTime, earliestStartDateTime, dob); + defendantId, masterDefendantId, materialIdOne, materialIdTwo, referralId, caseUrn, listedStartDateTime, earliestStartDateTime, dob, isCivil); } @@ -1060,7 +1072,7 @@ private static String getInitiateCourtProceedingsJsonBodyForLegalEntityDefendant final String referralId, final String caseUrn, final String listedStartDateTime, final String earliestStartDateTime) { return getInitiateCourtProceedingsJsonFromResourceForDefendantMatching("progression.command.initiate-court-proceedings-for-defendant-matching-legal-entity.json", caseId, - defendantId, masterDefendantId, materialIdOne, materialIdTwo, referralId, caseUrn, listedStartDateTime, earliestStartDateTime, null); + defendantId, masterDefendantId, materialIdOne, materialIdTwo, referralId, caseUrn, listedStartDateTime, earliestStartDateTime, null, false); } diff --git a/progression-integration-test/src/test/resources/progression.command.initiate-court-proceedings-for-defendant-matching.json b/progression-integration-test/src/test/resources/progression.command.initiate-court-proceedings-for-defendant-matching.json index 4ac22a20c..d403b3393 100644 --- a/progression-integration-test/src/test/resources/progression.command.initiate-court-proceedings-for-defendant-matching.json +++ b/progression-integration-test/src/test/resources/progression.command.initiate-court-proceedings-for-defendant-matching.json @@ -18,6 +18,7 @@ }, "originatingOrganisation": "G01FT01AB", "initiationCode": "J", + "isCivil": IS_CIVIL_FLAG, "statementOfFacts": "You did it", "statementOfFactsWelsh": "You did it in Welsh", "relatedUrn": "TTH105HY", diff --git a/progression-query/progression-query-api/src/raml/json/schema/progression.query.prosecutioncase.json b/progression-query/progression-query-api/src/raml/json/schema/progression.query.prosecutioncase.json index c6c389f42..0240f2947 100644 --- a/progression-query/progression-query-api/src/raml/json/schema/progression.query.prosecutioncase.json +++ b/progression-query/progression-query-api/src/raml/json/schema/progression.query.prosecutioncase.json @@ -563,6 +563,10 @@ "caseStatus": { "type": "string" }, + "isCivil": { + "description": "True if this related case is a civil case, false if criminal", + "type": "boolean" + }, "offences": { "type": "array", "minItems": 1, @@ -573,6 +577,7 @@ }, "required": [ "caseId", + "isCivil", "offences" ], "additionalProperties": false diff --git a/progression-query/progression-query-api/src/raml/json/schema/progression.query.related-references.json b/progression-query/progression-query-api/src/raml/json/schema/progression.query.related-references.json index f147e66b9..4369b32b7 100644 --- a/progression-query/progression-query-api/src/raml/json/schema/progression.query.related-references.json +++ b/progression-query/progression-query-api/src/raml/json/schema/progression.query.related-references.json @@ -3,10 +3,6 @@ "id": "http://justice.gov.uk/progression/query/progression.query.related-references.json", "type": "object", "properties": { - "isCivil": { - "description": "True if the primary case (identified by caseId) is a civil case, false if criminal, absent if the primary case cannot be resolved", - "type": "boolean" - }, "relatedReferenceList": { "type": "array", "items": { diff --git a/progression-query/progression-query-view/src/main/java/uk/gov/moj/cpp/progression/query/ProsecutionCaseQuery.java b/progression-query/progression-query-view/src/main/java/uk/gov/moj/cpp/progression/query/ProsecutionCaseQuery.java index 4d16210a4..fc502aa1b 100644 --- a/progression-query/progression-query-view/src/main/java/uk/gov/moj/cpp/progression/query/ProsecutionCaseQuery.java +++ b/progression-query/progression-query-view/src/main/java/uk/gov/moj/cpp/progression/query/ProsecutionCaseQuery.java @@ -126,6 +126,7 @@ public class ProsecutionCaseQuery { public static final String ALL_CASE_HEARINGS = "allCaseHearings"; public static final String LINKED_APPLICATIONS_SUMMARY = "linkedApplicationsSummary"; public static final String CIVIL_FEES = "civilFees"; + public static final String IS_CIVIL = "isCivil"; public static final String PROSECUTION_CASES = "prosecutionCases"; public static final String OFFENCE_IDS = "offenceIds"; public static final String APPEALS_LODGED_INFO = "appealsLodgedInfo"; @@ -913,6 +914,7 @@ private void buildCases(final MatchDefendantCaseHearingEntity matchDefendantCase final JsonObjectBuilder caseObjectBuilder = Json.createObjectBuilder(); caseObjectBuilder.add(CASE_ID, prosecutionCase.getId().toString()); caseObjectBuilder.add(CASE_STATUS, prosecutionCaseStatus); + caseObjectBuilder.add(IS_CIVIL, nonNull(prosecutionCase.getIsCivil()) && prosecutionCase.getIsCivil()); if (nonNull(prosecutionCase.getProsecutionCaseIdentifier())) { final ProsecutionCaseIdentifier prosecutionCaseIdentifier = prosecutionCase.getProsecutionCaseIdentifier(); final JsonObjectBuilder pciJsonBuilder = Json.createObjectBuilder(); diff --git a/progression-query/progression-query-view/src/main/java/uk/gov/moj/cpp/progression/query/view/RelatedReferenceQueryView.java b/progression-query/progression-query-view/src/main/java/uk/gov/moj/cpp/progression/query/view/RelatedReferenceQueryView.java index ce3579581..07bdd68e5 100644 --- a/progression-query/progression-query-view/src/main/java/uk/gov/moj/cpp/progression/query/view/RelatedReferenceQueryView.java +++ b/progression-query/progression-query-view/src/main/java/uk/gov/moj/cpp/progression/query/view/RelatedReferenceQueryView.java @@ -1,56 +1,32 @@ package uk.gov.moj.cpp.progression.query.view; -import static java.util.Objects.nonNull; import static uk.gov.moj.cpp.progression.query.CaseLsmInfoQuery.PARAM_CASE_ID; -import uk.gov.justice.core.courts.ProsecutionCase; import uk.gov.justice.progression.query.RelatedReference; -import uk.gov.justice.services.common.converter.JsonObjectToObjectConverter; import uk.gov.justice.services.common.converter.ObjectToJsonObjectConverter; -import uk.gov.justice.services.common.converter.StringToJsonObjectConverter; import uk.gov.justice.services.core.annotation.Component; import uk.gov.justice.services.core.annotation.Handles; import uk.gov.justice.services.core.annotation.ServiceComponent; import uk.gov.justice.services.messaging.JsonEnvelope; import uk.gov.justice.services.messaging.JsonObjects; -import uk.gov.moj.cpp.prosecutioncase.persistence.entity.ProsecutionCaseEntity; -import uk.gov.moj.cpp.prosecutioncase.persistence.entity.SearchProsecutionCaseEntity; -import uk.gov.moj.cpp.prosecutioncase.persistence.repository.ProsecutionCaseRepository; import uk.gov.moj.cpp.prosecutioncase.persistence.repository.RelatedReferenceRepository; -import uk.gov.moj.cpp.prosecutioncase.persistence.repository.SearchProsecutionCaseRepository; -import java.util.Optional; import java.util.UUID; import javax.inject.Inject; import javax.json.Json; import javax.json.JsonArrayBuilder; import javax.json.JsonObjectBuilder; -import javax.persistence.NoResultException; @ServiceComponent(Component.QUERY_VIEW) public class RelatedReferenceQueryView { public static final String RELATED_REFERENCE_LIST = "relatedReferenceList"; - public static final String IS_CIVIL = "isCivil"; - @Inject private ObjectToJsonObjectConverter objectToJsonObjectConverter; - @Inject - private StringToJsonObjectConverter stringToJsonObjectConverter; - - @Inject - private JsonObjectToObjectConverter jsonObjectToObjectConverter; - @Inject private RelatedReferenceRepository relatedReferenceRepository; - @Inject - private ProsecutionCaseRepository prosecutionCaseRepository; - - @Inject - private SearchProsecutionCaseRepository searchProsecutionCaseRepository; - @Handles("progression.query.related-references") public JsonEnvelope getProsecutionCaseWithRelatedUrn(final JsonEnvelope envelope) { final UUID caseId = JsonObjects.getUUID(envelope.payloadAsJsonObject(), PARAM_CASE_ID) @@ -61,57 +37,19 @@ public JsonEnvelope getProsecutionCaseWithRelatedUrn(final JsonEnvelope envelope private JsonObjectBuilder getRelatedReferences(final UUID caseId) { final JsonObjectBuilder responseBuilder = Json.createObjectBuilder(); final JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder(); - - resolveIsCivil(caseId).ifPresent(isCivil -> responseBuilder.add(IS_CIVIL, isCivil)); - relatedReferenceRepository .findByProsecutionCaseId(caseId) .stream() - .map(e -> { - final RelatedReference.Builder builder = RelatedReference.relatedReference() - .withRelatedReference(e.getReference()) - .withRelatedReferenceId(e.getId()) - .withProsecutionCaseId(e.getProsecutionCaseId()); - resolveRelatedCaseIsCivil(e.getReference()).ifPresent(builder::withIsCivil); - return builder.build(); - }) - .map(objectToJsonObjectConverter::convert) - .forEach(jsonArrayBuilder::add); + .map(e -> RelatedReference.relatedReference() + .withRelatedReference(e.getReference()) + .withRelatedReferenceId(e.getId()) + .withProsecutionCaseId(e.getProsecutionCaseId()) + .build()) + .map(e -> objectToJsonObjectConverter.convert(e)) + .forEach(e -> jsonArrayBuilder.add(objectToJsonObjectConverter.convert(e))); responseBuilder.add(RELATED_REFERENCE_LIST, jsonArrayBuilder); return responseBuilder; } - private Optional resolveRelatedCaseIsCivil(final String relatedReferenceUrn) { - if (relatedReferenceUrn == null) { - return Optional.empty(); - } - - return searchProsecutionCaseRepository.findByCaseUrn(relatedReferenceUrn.toUpperCase()) - .stream() - .findFirst() - .map(SearchProsecutionCaseEntity::getCaseId) - .flatMap(this::toUUID) - .flatMap(this::resolveIsCivil); - } - - private Optional resolveIsCivil(final UUID caseId) { - try { - final ProsecutionCaseEntity prosecutionCaseEntity = prosecutionCaseRepository.findByCaseId(caseId); - final ProsecutionCase prosecutionCase = jsonObjectToObjectConverter.convert( - stringToJsonObjectConverter.convert(prosecutionCaseEntity.getPayload()), ProsecutionCase.class); - return Optional.of(nonNull(prosecutionCase.getIsCivil()) && prosecutionCase.getIsCivil()); - } catch (final NoResultException e) { - return Optional.empty(); - } - } - - private Optional toUUID(final String value) { - try { - return Optional.of(UUID.fromString(value)); - } catch (final IllegalArgumentException e) { - return Optional.empty(); - } - } - } diff --git a/progression-query/progression-query-view/src/raml/json/schema/progression.query.prosecutioncase.json b/progression-query/progression-query-view/src/raml/json/schema/progression.query.prosecutioncase.json index 9c9831e53..cfab4a84d 100644 --- a/progression-query/progression-query-view/src/raml/json/schema/progression.query.prosecutioncase.json +++ b/progression-query/progression-query-view/src/raml/json/schema/progression.query.prosecutioncase.json @@ -562,6 +562,10 @@ "caseStatus": { "type": "string" }, + "isCivil": { + "description": "True if this related case is a civil case, false if criminal", + "type": "boolean" + }, "offences": { "type": "array", "minItems": 1, @@ -572,6 +576,7 @@ }, "required": [ "caseId", + "isCivil", "offences" ], "additionalProperties": false diff --git a/progression-query/progression-query-view/src/raml/json/schema/progression.query.related-references.json b/progression-query/progression-query-view/src/raml/json/schema/progression.query.related-references.json index f147e66b9..4369b32b7 100644 --- a/progression-query/progression-query-view/src/raml/json/schema/progression.query.related-references.json +++ b/progression-query/progression-query-view/src/raml/json/schema/progression.query.related-references.json @@ -3,10 +3,6 @@ "id": "http://justice.gov.uk/progression/query/progression.query.related-references.json", "type": "object", "properties": { - "isCivil": { - "description": "True if the primary case (identified by caseId) is a civil case, false if criminal, absent if the primary case cannot be resolved", - "type": "boolean" - }, "relatedReferenceList": { "type": "array", "items": { diff --git a/progression-query/progression-query-view/src/test/java/uk/gov/moj/cpp/progression/query/view/ProsecutionCaseQueryViewTest.java b/progression-query/progression-query-view/src/test/java/uk/gov/moj/cpp/progression/query/view/ProsecutionCaseQueryViewTest.java index 99b4abf65..af5310a0c 100644 --- a/progression-query/progression-query-view/src/test/java/uk/gov/moj/cpp/progression/query/view/ProsecutionCaseQueryViewTest.java +++ b/progression-query/progression-query-view/src/test/java/uk/gov/moj/cpp/progression/query/view/ProsecutionCaseQueryViewTest.java @@ -437,7 +437,69 @@ public void shouldFindProsecutionCaseAndRelatedCasesByIdWithMultipleDefendants() .assertThat("$.[*].cases[*].caseStatus", hasItems("ACTIVE2", "ACTIVE3")); with(relatedCases.toString()) .assertThat("$.[*].cases[*].offences[*].id", hasItems(OFFENCE_ID2.toString(), OFFENCE_ID4.toString())); + with(relatedCases.toString()) + .assertThat("$.[*].cases[*].isCivil", hasItems(false, false)); + + } + + @Test + public void shouldReturnIsCivilTrueForRelatedCaseWhenMatchedCaseIsCivil() { + final JsonObject jsonObject = Json.createObjectBuilder() + .add("caseId", CASE_ID1.toString()).build(); + + final JsonEnvelope jsonEnvelope = JsonEnvelope.envelopeFrom( + JsonEnvelope.metadataBuilder().withId(randomUUID()).withName("progression.query.prosecutioncase").build(), + jsonObject); + + final GetHearingsAtAGlance getCaseAtAGlance = GetHearingsAtAGlance.getHearingsAtAGlance() + .withHearings(asList(Hearings.hearings().build())) + .withDefendantHearings(asList(DefendantHearings.defendantHearings().build())) + .withId(randomUUID()) + .build(); + + final ProsecutionCase primaryCase = ProsecutionCase.prosecutionCase() + .withDefendants(List.of(Defendant.defendant() + .withMasterDefendantId(MASTER_DEFENDANT_ID1) + .withOffences(buildOffences(OFFENCE_ID1)) + .build())) + .withCaseStatus("ACTIVE") + .withId(CASE_ID1) + .withProsecutionCaseIdentifier(ProsecutionCaseIdentifier.prosecutionCaseIdentifier() + .withProsecutionAuthorityId(randomUUID()) + .withProsecutionAuthorityCode("Code1") + .withCaseURN("CaseURN1") + .build()) + .build(); + final ProsecutionCase matchedCivilCase = ProsecutionCase.prosecutionCase() + .withDefendants(List.of(Defendant.defendant() + .withMasterDefendantId(MASTER_DEFENDANT_ID1) + .withOffences(buildOffences(OFFENCE_ID2)) + .build())) + .withCaseStatus("ACTIVE") + .withIsCivil(true) + .withId(CASE_ID2) + .withProsecutionCaseIdentifier(ProsecutionCaseIdentifier.prosecutionCaseIdentifier() + .withProsecutionAuthorityId(randomUUID()) + .withProsecutionAuthorityCode("Code2") + .withCaseURN("CaseURN2") + .build()) + .build(); + + final List matchDefendantCaseHearingEntities = List.of( + buildMatchDefendantCaseHearingEntity(MASTER_DEFENDANT_ID1, CASE_ID1, primaryCase), + buildMatchDefendantCaseHearingEntity(MASTER_DEFENDANT_ID1, CASE_ID2, matchedCivilCase) + ); + + when(prosecutionCaseRepository.findByCaseId(CASE_ID1)).thenReturn(matchDefendantCaseHearingEntities.get(0).getProsecutionCase()); + when(matchDefendantCaseHearingRepository.findByMasterDefendantId(anyList())).thenReturn(matchDefendantCaseHearingEntities); + when(hearingAtAGlanceService.getHearingAtAGlance(CASE_ID1)).thenReturn(getCaseAtAGlance); + + final JsonEnvelope response = prosecutionCaseQuery.getProsecutionCase(jsonEnvelope); + + final JsonArray relatedCases = response.payloadAsJsonObject().getJsonArray("relatedCases"); + assertThat(relatedCases.size(), is(1)); + assertThat(relatedCases.getJsonObject(0).getJsonArray("cases").getJsonObject(0).getBoolean("isCivil"), is(true)); } @Test diff --git a/progression-query/progression-query-view/src/test/java/uk/gov/moj/cpp/progression/query/view/RelatedReferenceQueryViewTest.java b/progression-query/progression-query-view/src/test/java/uk/gov/moj/cpp/progression/query/view/RelatedReferenceQueryViewTest.java deleted file mode 100644 index 1e2f98c69..000000000 --- a/progression-query/progression-query-view/src/test/java/uk/gov/moj/cpp/progression/query/view/RelatedReferenceQueryViewTest.java +++ /dev/null @@ -1,222 +0,0 @@ -package uk.gov.moj.cpp.progression.query.view; - -import static java.util.Collections.emptyList; -import static java.util.UUID.randomUUID; -import static javax.json.Json.createObjectBuilder; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static uk.gov.justice.services.messaging.JsonEnvelope.envelopeFrom; -import static uk.gov.justice.services.messaging.JsonEnvelope.metadataBuilder; - -import uk.gov.justice.core.courts.ProsecutionCase; -import uk.gov.justice.services.common.converter.JsonObjectToObjectConverter; -import uk.gov.justice.services.common.converter.ObjectToJsonObjectConverter; -import uk.gov.justice.services.common.converter.StringToJsonObjectConverter; -import uk.gov.justice.services.messaging.JsonEnvelope; -import uk.gov.justice.services.test.utils.framework.api.JsonObjectConvertersFactory; -import uk.gov.moj.cpp.prosecutioncase.persistence.entity.ProsecutionCaseEntity; -import uk.gov.moj.cpp.prosecutioncase.persistence.entity.RelatedReference; -import uk.gov.moj.cpp.prosecutioncase.persistence.entity.SearchProsecutionCaseEntity; -import uk.gov.moj.cpp.prosecutioncase.persistence.repository.ProsecutionCaseRepository; -import uk.gov.moj.cpp.prosecutioncase.persistence.repository.RelatedReferenceRepository; -import uk.gov.moj.cpp.prosecutioncase.persistence.repository.SearchProsecutionCaseRepository; - -import java.util.List; -import java.util.UUID; -import java.util.stream.Stream; - -import javax.json.JsonArray; -import javax.json.JsonObject; -import javax.persistence.NoResultException; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; -import org.junit.jupiter.params.provider.ValueSource; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Spy; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -class RelatedReferenceQueryViewTest { - - @Spy - private ObjectToJsonObjectConverter objectToJsonObjectConverter = new JsonObjectConvertersFactory().objectToJsonObjectConverter(); - - @Mock - private StringToJsonObjectConverter stringToJsonObjectConverter; - - @Mock - private JsonObjectToObjectConverter jsonObjectToObjectConverter; - - @Mock - private RelatedReferenceRepository relatedReferenceRepository; - - @Mock - private ProsecutionCaseRepository prosecutionCaseRepository; - - @Mock - private SearchProsecutionCaseRepository searchProsecutionCaseRepository; - - @InjectMocks - private RelatedReferenceQueryView relatedReferenceQueryView; - - private final UUID primaryCaseId = randomUUID(); - private final UUID relatedCaseId = randomUUID(); - private final UUID relatedReferenceId = randomUUID(); - private final String relatedReferenceUrn = "cn12345"; - - @ParameterizedTest - @ValueSource(booleans = {true, false}) - void shouldReturnRootIsCivilMatchingPrimaryCase(final boolean isCivil) { - givenRelatedReference(); - givenPrimaryCase(isCivil); - givenRelatedCaseUrnNotFound(); - - assertThat(invoke().getBoolean("isCivil"), is(isCivil)); - } - - @Test - void shouldOmitRootIsCivilWhenPrimaryCaseNotFound() { - givenRelatedReference(); - givenPrimaryCaseNotFound(); - givenRelatedCaseUrnNotFound(); - - assertThat(invoke().containsKey("isCivil"), is(false)); - } - - private static Stream relatedCaseIsCivilScenarios() { - return Stream.of( - Arguments.of(true, true), - Arguments.of(false, false), - Arguments.of(null, false) - ); - } - - @ParameterizedTest - @MethodSource("relatedCaseIsCivilScenarios") - void shouldResolveRelatedCaseIsCivilOnEntry(final Boolean isCivil, final boolean expected) { - givenRelatedReference(); - givenPrimaryCaseNotFound(); - givenRelatedCaseResolvable(isCivil); - - assertThat(firstRelatedReference().getBoolean("isCivil"), is(expected)); - } - - @Test - void shouldOmitEntryIsCivilWhenRelatedCaseUrnNotFound() { - givenRelatedReference(); - givenPrimaryCaseNotFound(); - givenRelatedCaseUrnNotFound(); - - assertThat(firstRelatedReference().containsKey("isCivil"), is(false)); - } - - @Test - void shouldOmitEntryIsCivilWhenRelatedCaseMissingFromProsecutionCase() { - givenRelatedReference(); - givenPrimaryCaseNotFound(); - givenRelatedCaseMissingFromProsecutionCase(); - - assertThat(firstRelatedReference().containsKey("isCivil"), is(false)); - } - - @Test - void shouldResolveRelatedCaseIsCivilIndependentlyOfPrimaryCase() { - givenRelatedReference(); - givenPrimaryCaseNotFound(); - givenRelatedCaseResolvable(true); - - final JsonObject response = invoke(); - - assertThat(response.containsKey("isCivil"), is(false)); - assertThat(response.getJsonArray("relatedReferenceList").getJsonObject(0).getBoolean("isCivil"), is(true)); - } - - @Test - void shouldUppercaseRelatedReferenceBeforeLookup() { - givenRelatedReference(); - givenPrimaryCaseNotFound(); - givenRelatedCaseResolvable(false); - - invoke(); - - verify(searchProsecutionCaseRepository).findByCaseUrn(relatedReferenceUrn.toUpperCase()); - } - - @Test - void shouldReturnEmptyListWhenNoRelatedReferences() { - when(relatedReferenceRepository.findByProsecutionCaseId(primaryCaseId)).thenReturn(emptyList()); - givenPrimaryCase(false); - - final JsonArray list = invoke().getJsonArray("relatedReferenceList"); - - assertThat(list.isEmpty(), is(true)); - } - - private JsonObject invoke() { - final JsonObject payload = createObjectBuilder().add("caseId", primaryCaseId.toString()).build(); - final JsonEnvelope envelope = envelopeFrom(metadataBuilder().withId(randomUUID()).withName("progression.query.related-references"), payload); - return relatedReferenceQueryView.getProsecutionCaseWithRelatedUrn(envelope).payloadAsJsonObject(); - } - - private JsonObject firstRelatedReference() { - return invoke().getJsonArray("relatedReferenceList").getJsonObject(0); - } - - private void givenRelatedReference() { - final RelatedReference relatedReference = new RelatedReference(); - relatedReference.setId(relatedReferenceId); - relatedReference.setReference(relatedReferenceUrn); - relatedReference.setProsecutionCaseId(primaryCaseId); - when(relatedReferenceRepository.findByProsecutionCaseId(primaryCaseId)).thenReturn(List.of(relatedReference)); - } - - private void givenPrimaryCase(final boolean isCivil) { - final ProsecutionCaseEntity entity = new ProsecutionCaseEntity(); - entity.setCaseId(primaryCaseId); - entity.setPayload("primaryPayload"); - when(prosecutionCaseRepository.findByCaseId(primaryCaseId)).thenReturn(entity); - - final JsonObject primaryJson = createObjectBuilder().add("marker", "primary").build(); - when(stringToJsonObjectConverter.convert("primaryPayload")).thenReturn(primaryJson); - when(jsonObjectToObjectConverter.convert(primaryJson, ProsecutionCase.class)) - .thenReturn(ProsecutionCase.prosecutionCase().withIsCivil(isCivil).build()); - } - - private void givenPrimaryCaseNotFound() { - when(prosecutionCaseRepository.findByCaseId(primaryCaseId)).thenThrow(new NoResultException()); - } - - private void givenRelatedCaseResolvable(final Boolean isCivil) { - final SearchProsecutionCaseEntity searchEntity = new SearchProsecutionCaseEntity(); - searchEntity.setCaseId(relatedCaseId.toString()); - when(searchProsecutionCaseRepository.findByCaseUrn(relatedReferenceUrn.toUpperCase())).thenReturn(List.of(searchEntity)); - - final ProsecutionCaseEntity entity = new ProsecutionCaseEntity(); - entity.setCaseId(relatedCaseId); - entity.setPayload("relatedPayload"); - when(prosecutionCaseRepository.findByCaseId(relatedCaseId)).thenReturn(entity); - - final JsonObject relatedJson = createObjectBuilder().add("marker", "related").build(); - when(stringToJsonObjectConverter.convert("relatedPayload")).thenReturn(relatedJson); - when(jsonObjectToObjectConverter.convert(relatedJson, ProsecutionCase.class)) - .thenReturn(ProsecutionCase.prosecutionCase().withIsCivil(isCivil).build()); - } - - private void givenRelatedCaseUrnNotFound() { - when(searchProsecutionCaseRepository.findByCaseUrn(relatedReferenceUrn.toUpperCase())).thenReturn(emptyList()); - } - - private void givenRelatedCaseMissingFromProsecutionCase() { - final SearchProsecutionCaseEntity searchEntity = new SearchProsecutionCaseEntity(); - searchEntity.setCaseId(relatedCaseId.toString()); - when(searchProsecutionCaseRepository.findByCaseUrn(relatedReferenceUrn.toUpperCase())).thenReturn(List.of(searchEntity)); - when(prosecutionCaseRepository.findByCaseId(relatedCaseId)).thenThrow(new NoResultException()); - } -}