diff --git a/azure-pipelines.yaml b/azure-pipelines.yaml index e8c28fa2e4..0e8258f927 100644 --- a/azure-pipelines.yaml +++ b/azure-pipelines.yaml @@ -21,7 +21,7 @@ resources: type: github name: hmcts/cpp-azure-devops-templates endpoint: 'hmcts' - ref: 'main' + ref: 'pr-debug' pool: name: "MDV-ADO-AGENT-AKS-01" diff --git a/progression-event/progression-event-listener/src/main/java/uk/gov/moj/cpp/application/event/listener/CourtApplicationEventListener.java b/progression-event/progression-event-listener/src/main/java/uk/gov/moj/cpp/application/event/listener/CourtApplicationEventListener.java index 7d0fd4a963..612d1f5278 100644 --- a/progression-event/progression-event-listener/src/main/java/uk/gov/moj/cpp/application/event/listener/CourtApplicationEventListener.java +++ b/progression-event/progression-event-listener/src/main/java/uk/gov/moj/cpp/application/event/listener/CourtApplicationEventListener.java @@ -413,12 +413,15 @@ private InitiateCourtApplicationEntity getInitiateCourtApplication(final CourtAp private void addCourtApplicationToCase(final CourtApplication courtApplication, final UUID prosecutionCaseId, final String caseReference) { final CourtApplicationEntity courtApplicationEntity = courtApplicationRepository.findBy(courtApplication.getId()); if (nonNull(courtApplicationEntity)) { - final CourtApplicationCaseEntity courtApplicationCaseEntity = new CourtApplicationCaseEntity(); - final CourtApplicationCaseKey courtApplicationCaseKey = new CourtApplicationCaseKey(randomUUID(), courtApplication.getId(), prosecutionCaseId); - courtApplicationCaseEntity.setId(courtApplicationCaseKey); - courtApplicationCaseEntity.setCourtApplication(courtApplicationEntity); - courtApplicationCaseEntity.setCaseReference(caseReference); - courtApplicationCaseRepository.save(courtApplicationCaseEntity); + CourtApplicationCaseEntity courtApplicationCase = courtApplicationCaseRepository.findByApplicationIdAndCaseId(courtApplication.getId(), prosecutionCaseId); + if(courtApplicationCase == null) { + final CourtApplicationCaseEntity courtApplicationCaseEntity = new CourtApplicationCaseEntity(); + final CourtApplicationCaseKey courtApplicationCaseKey = new CourtApplicationCaseKey(randomUUID(), courtApplication.getId(), prosecutionCaseId); + courtApplicationCaseEntity.setId(courtApplicationCaseKey); + courtApplicationCaseEntity.setCourtApplication(courtApplicationEntity); + courtApplicationCaseEntity.setCaseReference(caseReference); + courtApplicationCaseRepository.save(courtApplicationCaseEntity); + } } } diff --git a/progression-event/progression-event-listener/src/test/java/uk/gov/moj/cpp/application/event/listener/CourtApplicationEventListenerTest.java b/progression-event/progression-event-listener/src/test/java/uk/gov/moj/cpp/application/event/listener/CourtApplicationEventListenerTest.java index 05ba7651f5..756d88bbd9 100644 --- a/progression-event/progression-event-listener/src/test/java/uk/gov/moj/cpp/application/event/listener/CourtApplicationEventListenerTest.java +++ b/progression-event/progression-event-listener/src/test/java/uk/gov/moj/cpp/application/event/listener/CourtApplicationEventListenerTest.java @@ -792,6 +792,49 @@ public void shouldUpdateRespondentsAddressOnApplication_LegalEntityDefendant(){ verify(initiateCourtApplicationRepository).save(initiateCourtApplicationEntityArgumentCaptor.capture()); } + @Test + public void shouldHandleCourtApplicationAddedWillBeSkippedIfWeHaveApplicationAndCaseInsideTable() { + final UUID applicationId = randomUUID(); + final CourtApplicationEntity persistedEntity = new CourtApplicationEntity(); + persistedEntity.setApplicationId(applicationId); + persistedEntity.setPayload(payload.toString()); + final UUID caseId = randomUUID(); + + final CourtApplication courtApplication = CourtApplication.courtApplication() + .withId(applicationId) + .withCourtApplicationCases( + singletonList( + CourtApplicationCase.courtApplicationCase() + .withProsecutionCaseId(caseId) + .withProsecutionCaseIdentifier(ProsecutionCaseIdentifier.prosecutionCaseIdentifier() + .withCaseURN("CaseURN") + .build()) + .build() + ) + ) + .withCourtOrder(CourtOrder.courtOrder().withId(randomUUID()) + .withOrderingCourt(CourtCentre.courtCentre().withId(randomUUID()).build()) + .withCourtOrderOffences(singletonList(courtOrderOffence().withOffence(offence() + .withJudicialResults(null) + + .build()) + .withProsecutionCaseId(caseId) + .withProsecutionCaseIdentifier(ProsecutionCaseIdentifier.prosecutionCaseIdentifier().withCaseURN("CaseURN").build()) + .build())) + .build()) + .build(); + CourtApplicationCaseEntity courtApplicationCase = new CourtApplicationCaseEntity(); + courtApplicationCase.setCaseReference(caseId.toString()); + courtApplicationCase.setCourtApplication(persistedEntity); + when(envelope.payloadAsJsonObject()).thenReturn(payload); + when(jsonObjectToObjectConverter.convert(payload, CourtApplicationAddedToCase.class)) + .thenReturn(courtApplicationAddedToCase); + when(courtApplicationAddedToCase.getCourtApplication()).thenReturn(courtApplication); + when(courtApplicationCaseRepository.findByApplicationIdAndCaseId(courtApplication.getId(),caseId)).thenReturn(courtApplicationCase); + when(repository.findBy(applicationId)).thenReturn(persistedEntity); + eventListener.processCourtApplicationAddedToCase(envelope); + } + private static CourtApplicationParty buildOriginalDefendant(boolean isPersonDefendant, final UUID masterDefendantId) { final Address originalAddress = Address.address().withAddress1("Old Address 1").withAddress2("Old Address 2").withPostcode("RG2 1WE").build(); if(isPersonDefendant) { diff --git a/progression-viewstore/progression-viewstore-persistence/src/main/java/uk/gov/moj/cpp/prosecutioncase/persistence/repository/CourtApplicationCaseRepository.java b/progression-viewstore/progression-viewstore-persistence/src/main/java/uk/gov/moj/cpp/prosecutioncase/persistence/repository/CourtApplicationCaseRepository.java index 00c817eba9..3b4622a2f7 100644 --- a/progression-viewstore/progression-viewstore-persistence/src/main/java/uk/gov/moj/cpp/prosecutioncase/persistence/repository/CourtApplicationCaseRepository.java +++ b/progression-viewstore/progression-viewstore-persistence/src/main/java/uk/gov/moj/cpp/prosecutioncase/persistence/repository/CourtApplicationCaseRepository.java @@ -11,6 +11,7 @@ import org.apache.deltaspike.data.api.Query; import org.apache.deltaspike.data.api.QueryParam; import org.apache.deltaspike.data.api.Repository; +import org.apache.deltaspike.data.api.SingleResultType; @Repository public interface CourtApplicationCaseRepository extends EntityRepository { @@ -27,4 +28,7 @@ public interface CourtApplicationCaseRepository extends EntityRepository