diff --git a/build.gradle b/build.gradle index 4ebc6b9de7..1f06570404 100644 --- a/build.gradle +++ b/build.gradle @@ -541,6 +541,7 @@ tasks.withType(CftlibExec).configureEach { environment 'SPRING_PROFILES_ACTIVE', 'dev' environment 'XUI_JURISDICTIONS', 'PCS' environment 'XUI_DOCUMENTS_API_V2', 'http://ccd-case-document-am-api-aat.service.core-compute-aat.internal' + environment 'CASE_DOCUMENT_S2S_AUTHORISED_SERVICES', 'ccd_case_document_am_api,ccd_gw,xui_webapp,ccd_data,pcs_frontend,pcs_api' environment 'DATA_STORE_S2S_AUTHORISED_SERVICES', 'ccd_data,ccd_gw,pcs_frontend,pcs_api' environment 'CCD_S2S-AUTHORISED_SERVICES_CASE_USER_ROLES', 'aac_manage_case_assignment,pcs_api' } diff --git a/charts/pcs-api/values.ccd.preview.template.yaml b/charts/pcs-api/values.ccd.preview.template.yaml index 1ee4c2fa35..08f0d27540 100644 --- a/charts/pcs-api/values.ccd.preview.template.yaml +++ b/charts/pcs-api/values.ccd.preview.template.yaml @@ -236,6 +236,7 @@ ccd-case-document-am-api: S2S_URL: http://rpe-service-auth-provider-aat.service.core-compute-aat.internal DM_STORE_BASE_URL: http://dm-store-aat.service.core-compute-aat.internal CCD_DATA_STORE_API_BASE_URL: http://${SERVICE_NAME}-ccd-data-store-api + CASE_DOCUMENT_S2S_AUTHORISED_SERVICES: ccd_case_document_am_api,ccd_gw,xui_webapp,ccd_data,bulk_scan_processor,sscs,probate_backend,iac,em_npa_app,fprl_dgs_api,dg_docassembly_api,em_stitching_api,em_ccd_orchestrator,cmc_claim_store,civil_service,bulk_scan_orchestrator,ethos_repl_service,divorce_document_generator,finrem_document_generator,finrem_case_orchestration,fpl_case_service,et_cos,prl_cos_api,prl_dgs_api,et_sya_api,adoption_cos_api,adoption_web,nfdiv_case_api,divorce_frontend,sptribs_case_api,sptribs_dss_backend,civil_general_applications,pcs_api,pcs_frontend keyVaults: ccd: secrets: diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/document/DocumentNameService.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/document/DocumentNameService.java new file mode 100644 index 0000000000..88deb5a18b --- /dev/null +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/document/DocumentNameService.java @@ -0,0 +1,61 @@ +package uk.gov.hmcts.reform.pcs.ccd.service.document; + +import org.apache.commons.io.FilenameUtils; +import org.springframework.stereotype.Service; +import uk.gov.hmcts.reform.pcs.ccd.entity.ClaimEntity; +import uk.gov.hmcts.reform.pcs.ccd.entity.GenAppEntity; +import uk.gov.hmcts.reform.pcs.ccd.entity.party.ClaimPartyEntity; +import uk.gov.hmcts.reform.pcs.ccd.entity.party.PartyRole; +import uk.gov.hmcts.reform.pcs.exception.PartyNotFoundException; + +import java.util.UUID; + +@Service +public class DocumentNameService { + + public String appendGenAppPostfix(String originalFilename, + GenAppEntity genAppEntity, + ClaimEntity mainClaim, + UUID applicantPartyId) { + + if (originalFilename == null) { + return null; + } + + String baseName = FilenameUtils.getBaseName(originalFilename); + String extension = FilenameUtils.getExtension(originalFilename); + + // Example label: General Application (GA2) - Defendant 1.pdf + String partyLabel = getPartyLabel(mainClaim, applicantPartyId); + String filename = "%s GA%d".formatted(baseName, genAppEntity.getRank()); + if (partyLabel != null) { + filename += " - " + partyLabel; + } + + if (!extension.isBlank()) { + filename += "." + extension; + } + + return filename; + } + + private static String getPartyLabel(ClaimEntity mainClaim, UUID partyId) { + ClaimPartyEntity applicantClaimParty = getClaimParty(mainClaim, partyId); + + if (applicantClaimParty.getRole() == PartyRole.CLAIMANT) { + return "Claimant %d".formatted(applicantClaimParty.getRank()); + } else if (applicantClaimParty.getRole() == PartyRole.DEFENDANT) { + return "Defendant %d".formatted(applicantClaimParty.getRank()); + } else { + return null; + } + } + + private static ClaimPartyEntity getClaimParty(ClaimEntity claim, UUID partyId) { + return claim.getClaimParties().stream() + .filter(claimPartyEntity -> partyId.equals(claimPartyEntity.getParty().getId())) + .findFirst() + .orElseThrow(() -> new PartyNotFoundException("Party not found")); + } + +} diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppDocumentGenerator.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppDocumentGenerator.java index 91c8f5fc75..fbc941a6e3 100644 --- a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppDocumentGenerator.java +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppDocumentGenerator.java @@ -10,6 +10,7 @@ import uk.gov.hmcts.reform.pcs.ccd.domain.genapp.CitizenGenAppRequest; import uk.gov.hmcts.reform.pcs.ccd.entity.AddressEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.ClaimEntity; +import uk.gov.hmcts.reform.pcs.ccd.entity.DocumentEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.GenAppEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.PcsCaseEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.party.ClaimPartyEntity; @@ -21,6 +22,7 @@ import uk.gov.hmcts.reform.pcs.ccd.service.party.PartyService; import uk.gov.hmcts.reform.pcs.ccd.util.AddressFormatter; import uk.gov.hmcts.reform.pcs.ccd.util.AddressMapper; +import uk.gov.hmcts.reform.pcs.document.model.Document; import uk.gov.hmcts.reform.pcs.document.model.StatementOfTruth; import uk.gov.hmcts.reform.pcs.document.model.genapp.GenAppFormPayload; import uk.gov.hmcts.reform.pcs.document.service.DocAssemblyService; @@ -82,8 +84,12 @@ public String generateSubmissionDocument(long caseReference, UUID applicantUserId = securityContextService.getCurrentUserId(); String outputFilename = getDocumentFilename(mainClaim, genAppEntity, applicantUserId); - GenAppFormPayload genAppFormPayload - = createGenAppFormPayload(caseReference, pcsCaseEntity, mainClaim, citizenGenAppRequest, applicantUserId); + GenAppFormPayload genAppFormPayload = createGenAppFormPayload(caseReference, + pcsCaseEntity, + mainClaim, + citizenGenAppRequest, + genAppEntity, + applicantUserId); return docAssemblyService .generateDocument(genAppFormPayload, TEMPLATE_ID, OutputType.PDF, outputFilename); @@ -93,6 +99,7 @@ private GenAppFormPayload createGenAppFormPayload(long caseReference, PcsCaseEntity pcsCaseEntity, ClaimEntity mainClaim, CitizenGenAppRequest citizenGenAppRequest, + GenAppEntity genAppEntity, UUID applicantUserId) { LocalDate currentUkDate = LocalDate.now(ukClock); @@ -122,6 +129,8 @@ private GenAppFormPayload createGenAppFormPayload(long caseReference, .otherPartiesAgreed(citizenGenAppRequest.getOtherPartiesAgreed()) .withoutNotice(citizenGenAppRequest.getWithoutNotice()) .withoutNoticeReason(citizenGenAppRequest.getWithoutNoticeReason()) + .documentUploadWanted(citizenGenAppRequest.getHasSupportingDocuments()) + .uploadedDocuments(getDocumentList(genAppEntity)) .statementOfTruth(StatementOfTruth.builder() .fullName(citizenGenAppRequest.getSotFullName()) .submittedOn(currentUkDate) @@ -130,6 +139,14 @@ private GenAppFormPayload createGenAppFormPayload(long caseReference, .build(); } + private List getDocumentList(GenAppEntity genAppEntity) { + List uploadedDocuments = genAppEntity.getDocuments(); + + return uploadedDocuments.stream() + .map(documentEntity -> Document.builder().filename(documentEntity.getFileName()).build()) + .toList(); + } + private String buildCaseName(ClaimEntity mainClaim) { Map> partyMap = getPartyMap(mainClaim); diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppService.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppService.java index 56cc41fd83..49e9b804bf 100644 --- a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppService.java +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppService.java @@ -3,10 +3,12 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import uk.gov.hmcts.ccd.sdk.type.ListValue; +import uk.gov.hmcts.reform.pcs.ccd.domain.CaseFileCategory; import uk.gov.hmcts.reform.pcs.ccd.domain.UploadedDocument; import uk.gov.hmcts.reform.pcs.ccd.domain.VerticalYesNo; import uk.gov.hmcts.reform.pcs.ccd.domain.genapp.CitizenGenAppRequest; import uk.gov.hmcts.reform.pcs.ccd.domain.genapp.GenAppState; +import uk.gov.hmcts.reform.pcs.ccd.entity.ClaimEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.DocumentEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.GenAppEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.HelpWithFeesEntity; @@ -15,10 +17,12 @@ import uk.gov.hmcts.reform.pcs.ccd.entity.party.PartyEntity; import uk.gov.hmcts.reform.pcs.ccd.repository.DocumentRepository; import uk.gov.hmcts.reform.pcs.ccd.repository.GenAppRepository; +import uk.gov.hmcts.reform.pcs.ccd.service.document.DocumentNameService; import java.time.Clock; import java.time.LocalDateTime; import java.util.List; +import java.util.UUID; import static uk.gov.hmcts.reform.pcs.ccd.util.YesOrNoConverter.toYesOrNo; @@ -26,13 +30,16 @@ public class GenAppService { private final GenAppRepository genAppRepository; + private final DocumentNameService documentNameService; private final DocumentRepository documentRepository; private final Clock utcClock; public GenAppService(GenAppRepository genAppRepository, + DocumentNameService documentNameService, DocumentRepository documentRepository, @Qualifier("utcClock") Clock utcClock) { this.genAppRepository = genAppRepository; + this.documentNameService = documentNameService; this.documentRepository = documentRepository; this.utcClock = utcClock; } @@ -51,6 +58,10 @@ public GenAppEntity createGenAppEntity(CitizenGenAppRequest citizenCreateGenApp, .appliedForHwf(citizenCreateGenApp.getAppliedForHwf()) .build(); + // Adding the Gen App to the PcsCaseEntity allocates it a rank, + // which we rely on later on in this method to rename the supporting documents + pcsCaseEntity.addGenApp(genAppEntity); + if (citizenCreateGenApp.getAppliedForHwf() == VerticalYesNo.YES && citizenCreateGenApp.getHwfReference() != null) { HelpWithFeesEntity helpWithFeesEntity = new HelpWithFeesEntity(); @@ -71,7 +82,10 @@ public GenAppEntity createGenAppEntity(CitizenGenAppRequest citizenCreateGenApp, genAppEntity.setDocumentsUploaded(citizenCreateGenApp.getHasSupportingDocuments()); if (citizenCreateGenApp.getHasSupportingDocuments() == VerticalYesNo.YES) { List documentEntities - = createDocumentEntities(citizenCreateGenApp.getUploadedDocuments(), pcsCaseEntity, genAppEntity); + = createDocumentEntities(citizenCreateGenApp.getUploadedDocuments(), + pcsCaseEntity, + genAppEntity, + applicantParty.getId()); genAppEntity.setDocuments(documentEntities); } @@ -88,31 +102,38 @@ public GenAppEntity createGenAppEntity(CitizenGenAppRequest citizenCreateGenApp, genAppEntity.setStatementOfTruth(statementOfTruthEntity); } - pcsCaseEntity.addGenApp(genAppEntity); - return genAppRepository.save(genAppEntity); } public List createDocumentEntities(List> uploadedDocuments, PcsCaseEntity pcsCaseEntity, - GenAppEntity genAppEntity) { + GenAppEntity genAppEntity, + UUID applicantPartyId) { if (uploadedDocuments == null) { return List.of(); } + ClaimEntity mainClaimEntity = pcsCaseEntity.getClaims().getFirst(); + List documentEntities = uploadedDocuments.stream() .map(ListValue::getValue) - .map(defDoc -> DocumentEntity.builder() - .pcsCase(pcsCaseEntity) - .generalApplication(genAppEntity) - .url(defDoc.getDocument().getUrl()) - .fileName(defDoc.getDocument().getFilename()) - .binaryUrl(defDoc.getDocument().getBinaryUrl()) - .categoryId(null) - .contentType(defDoc.getContentType()) - .size(defDoc.getSizeInBytes()) - .build()) + .map(uploadedDocument -> { + String originalFilename = uploadedDocument.getDocument().getFilename(); + String updatedFilename = documentNameService + .appendGenAppPostfix(originalFilename, genAppEntity, mainClaimEntity, applicantPartyId); + + return DocumentEntity.builder() + .pcsCase(pcsCaseEntity) + .generalApplication(genAppEntity) + .url(uploadedDocument.getDocument().getUrl()) + .fileName(updatedFilename) + .binaryUrl(uploadedDocument.getDocument().getBinaryUrl()) + .categoryId(CaseFileCategory.APPLICATIONS.getId()) + .contentType(uploadedDocument.getContentType()) + .size(uploadedDocument.getSizeInBytes()) + .build(); + }) .toList(); return documentRepository.saveAll(documentEntities); diff --git a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/view/GenAppsView.java b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/view/GenAppsView.java index ab2b8b6d9e..a4f794d6c5 100644 --- a/src/main/java/uk/gov/hmcts/reform/pcs/ccd/view/GenAppsView.java +++ b/src/main/java/uk/gov/hmcts/reform/pcs/ccd/view/GenAppsView.java @@ -46,6 +46,7 @@ private ListValue createListValue(GenAppEntity genAppEntity) .party(party) .submittedOn(genAppEntity.getApplicationSubmittedDate()) .submissionDocument(getSubmissionDocument(genAppEntity)) + .supportingDocuments(createSupportingDocumentList(genAppEntity)) .build(); return new ListValue<>(genAppEntity.getId().toString(), generalApplication); @@ -76,6 +77,18 @@ private DocumentWithId getSubmissionDocument(GenAppEntity genAppEntity) { .orElse(null); } + private List> createSupportingDocumentList(GenAppEntity genAppEntity) { + return genAppEntity.getDocuments().stream() + .map(documentEntity -> { + Document document = modelMapper.map(documentEntity, Document.class); + return ListValue.builder() + .id(documentEntity.getId().toString()) + .value(document) + .build(); + }) + .toList(); + } + private boolean isVisibleToUser(GenAppEntity genAppEntity, UUID userId) { return genAppEntity.getWithoutNotice() != VerticalYesNo.YES || userId.equals(genAppEntity.getParty().getIdamId()); diff --git a/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/document/DocumentNameServiceTest.java b/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/document/DocumentNameServiceTest.java new file mode 100644 index 0000000000..b2e0b70e28 --- /dev/null +++ b/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/document/DocumentNameServiceTest.java @@ -0,0 +1,82 @@ +package uk.gov.hmcts.reform.pcs.ccd.service.document; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import uk.gov.hmcts.reform.pcs.ccd.entity.ClaimEntity; +import uk.gov.hmcts.reform.pcs.ccd.entity.GenAppEntity; +import uk.gov.hmcts.reform.pcs.ccd.entity.party.ClaimPartyEntity; +import uk.gov.hmcts.reform.pcs.ccd.entity.party.PartyEntity; +import uk.gov.hmcts.reform.pcs.ccd.entity.party.PartyRole; + +import java.util.List; +import java.util.UUID; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.params.provider.Arguments.argumentSet; + +class DocumentNameServiceTest { + + private DocumentNameService underTest; + + @BeforeEach + void setUp() { + underTest = new DocumentNameService(); + } + + @ParameterizedTest + @MethodSource("genAppNamingScenarios") + void shouldAddGenAppNumberAndPartyLabelToGenAppDocument(PartyRole partyRole, + String originalFilename, + String expectedFilename) { + // Given + UUID applicantPartyId = UUID.randomUUID(); + GenAppEntity genAppEntity = GenAppEntity.builder() + .rank(5) + .build(); + + PartyEntity party1 = PartyEntity.builder() + .id(applicantPartyId) + .build(); + + ClaimPartyEntity claimParty1 = ClaimPartyEntity.builder() + .party(party1) + .rank(2) + .role(partyRole) + .build(); + + ClaimEntity mainClaim = ClaimEntity.builder() + .claimParties(List.of(claimParty1)) + .build(); + + // When + String updatedFilename + = underTest.appendGenAppPostfix(originalFilename, genAppEntity, mainClaim, applicantPartyId); + + // Then + assertThat(updatedFilename).isEqualTo(expectedFilename); + } + + private static Stream genAppNamingScenarios() { + return Stream.of( + // Party role, original filename, expected updated filename + argumentSet("null filename", + PartyRole.DEFENDANT, null, null), + argumentSet("no extension, defendant", + PartyRole.DEFENDANT, "sample", "sample GA5 - Defendant 2"), + argumentSet("with extension, defendant", + PartyRole.DEFENDANT, "sample.pdf", "sample GA5 - Defendant 2.pdf"), + argumentSet("no extension, claimant", + PartyRole.CLAIMANT, "sample", "sample GA5 - Claimant 2"), + argumentSet("with extension, claimant", + PartyRole.CLAIMANT, "sample.pdf", "sample GA5 - Claimant 2.pdf"), + argumentSet("no extension, other party type", + PartyRole.UNDERLESSEE_OR_MORTGAGEE, "sample", "sample GA5"), + argumentSet("with extension, other party type", + PartyRole.UNDERLESSEE_OR_MORTGAGEE, "sample.pdf", "sample GA5.pdf") + ); + } + +} diff --git a/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppDocumentGeneratorTest.java b/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppDocumentGeneratorTest.java index 951a878b10..34c7da2d4b 100644 --- a/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppDocumentGeneratorTest.java +++ b/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppDocumentGeneratorTest.java @@ -18,6 +18,7 @@ import uk.gov.hmcts.reform.pcs.ccd.domain.genapp.GenAppType; import uk.gov.hmcts.reform.pcs.ccd.entity.AddressEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.ClaimEntity; +import uk.gov.hmcts.reform.pcs.ccd.entity.DocumentEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.GenAppEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.PcsCaseEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.party.ClaimPartyEntity; @@ -29,6 +30,7 @@ import uk.gov.hmcts.reform.pcs.ccd.service.party.PartyService; import uk.gov.hmcts.reform.pcs.ccd.util.AddressFormatter; import uk.gov.hmcts.reform.pcs.ccd.util.AddressMapper; +import uk.gov.hmcts.reform.pcs.document.model.Document; import uk.gov.hmcts.reform.pcs.document.model.Party; import uk.gov.hmcts.reform.pcs.document.model.StatementOfTruth; import uk.gov.hmcts.reform.pcs.document.model.genapp.GenAppFormPayload; @@ -357,6 +359,28 @@ void shouldSetWithoutNoticeReason() { assertThat(formPayload.getWithoutNoticeReason()).isEqualTo(expectedWithoutNoticeReason); } + @Test + void shouldSetSupportingDocumentsFromGenAppEntity() { + // Given + when(citizenGenAppRequest.getHasSupportingDocuments()).thenReturn(VerticalYesNo.YES); + + String filename1 = "filename1"; + String filename2 = "filename2"; + DocumentEntity documentEntity1 = createDocumentEntity(filename1); + DocumentEntity documentEntity2 = createDocumentEntity(filename2); + when(genAppEntity.getDocuments()).thenReturn(List.of(documentEntity1, documentEntity2)); + + // When + underTest.generateSubmissionDocument(CASE_REFERENCE, citizenGenAppRequest, genAppEntity); + + // Then + GenAppFormPayload formPayload = getFormPayload(); + assertThat(formPayload.getDocumentUploadWanted()).isEqualTo(VerticalYesNo.YES); + assertThat(formPayload.getUploadedDocuments()) + .extracting(Document::getFilename) + .containsExactly(filename1, filename2); + } + @Test void shouldSetStatementOfTruth() { // Given @@ -391,6 +415,12 @@ private void stubFormattedApplicantAddress(String formattedApplicantAddress) { .thenReturn(formattedApplicantAddress); } + private static DocumentEntity createDocumentEntity(String filename) { + DocumentEntity documentEntity1 = mock(DocumentEntity.class); + when(documentEntity1.getFileName()).thenReturn(filename); + return documentEntity1; + } + private GenAppFormPayload getFormPayload() { verify(docAssemblyService) .generateDocument(formPayloadArgumentCaptor.capture(), anyString(), any(OutputType.class), anyString()); diff --git a/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppServiceTest.java b/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppServiceTest.java index 4190d2722c..dd66a09b35 100644 --- a/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppServiceTest.java +++ b/src/test/java/uk/gov/hmcts/reform/pcs/ccd/service/genapp/GenAppServiceTest.java @@ -9,17 +9,20 @@ import org.junit.jupiter.params.provider.MethodSource; import org.mockito.ArgumentCaptor; import org.mockito.Captor; +import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import uk.gov.hmcts.ccd.sdk.type.Document; import uk.gov.hmcts.ccd.sdk.type.ListValue; import uk.gov.hmcts.ccd.sdk.type.YesOrNo; +import uk.gov.hmcts.reform.pcs.ccd.domain.CaseFileCategory; import uk.gov.hmcts.reform.pcs.ccd.domain.LanguageUsed; import uk.gov.hmcts.reform.pcs.ccd.domain.UploadedDocument; import uk.gov.hmcts.reform.pcs.ccd.domain.VerticalYesNo; import uk.gov.hmcts.reform.pcs.ccd.domain.genapp.CitizenGenAppRequest; import uk.gov.hmcts.reform.pcs.ccd.domain.genapp.GenAppState; import uk.gov.hmcts.reform.pcs.ccd.domain.genapp.GenAppType; +import uk.gov.hmcts.reform.pcs.ccd.entity.ClaimEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.DocumentEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.GenAppEntity; import uk.gov.hmcts.reform.pcs.ccd.entity.PcsCaseEntity; @@ -27,20 +30,25 @@ import uk.gov.hmcts.reform.pcs.ccd.entity.party.PartyEntity; import uk.gov.hmcts.reform.pcs.ccd.repository.DocumentRepository; import uk.gov.hmcts.reform.pcs.ccd.repository.GenAppRepository; +import uk.gov.hmcts.reform.pcs.ccd.service.document.DocumentNameService; import java.time.Clock; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.List; +import java.util.UUID; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.params.provider.Arguments.argumentSet; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.Mock.Strictness.LENIENT; +import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -53,13 +61,17 @@ class GenAppServiceTest { @Mock private GenAppRepository genAppRepository; + @Mock + private DocumentNameService documentNameService; @Mock(strictness = LENIENT) private DocumentRepository documentRepository; @Mock private Clock utcClock; - @Mock + @Mock(strictness = LENIENT) private PcsCaseEntity pcsCaseEntity; @Mock + private ClaimEntity mainClaim; + @Mock private PartyEntity applicantParty; @Captor private ArgumentCaptor genAppEntityCaptor; @@ -71,8 +83,9 @@ class GenAppServiceTest { @BeforeEach void setUp() { stubUtcClock(TEST_UTC_DATE_TIME); + when(pcsCaseEntity.getClaims()).thenReturn(List.of(mainClaim)); - underTest = new GenAppService(genAppRepository, documentRepository, utcClock); + underTest = new GenAppService(genAppRepository, documentNameService, documentRepository, utcClock); } @Test @@ -146,7 +159,7 @@ void shouldSetInitialState() { void shouldSetApplicantParty() { // Given CitizenGenAppRequest genAppRequest = CitizenGenAppRequest.builder() - .applicationType(GenAppType.SUSPEND) + .applicationType(GenAppType.ADJOURN) .build(); // When @@ -271,12 +284,21 @@ void shouldSetSupportingDocumentsFlag(VerticalYesNo hasSupportingDocuments) { @Test void shouldSaveUploadedDocuments() { // Given + String originalFilename = "original filename"; + String modifiedFilename = "modified filename"; + Document document = Document.builder() - .filename("test filename") + .filename(originalFilename) .url("test url") .binaryUrl("test binary url") .build(); + UUID applicantPartyId = UUID.randomUUID(); + when(applicantParty.getId()).thenReturn(applicantPartyId); + when(documentNameService.appendGenAppPostfix(eq(originalFilename), isA(GenAppEntity.class), + eq(mainClaim), eq(applicantPartyId))) + .thenReturn(modifiedFilename); + UploadedDocument uploadedDocument = UploadedDocument.builder() .document(document) .contentType("test content type") @@ -303,10 +325,10 @@ void shouldSaveUploadedDocuments() { assertThat(documentEntityListCaptor.getValue()).hasSize(1); DocumentEntity documentEntity = documentEntityListCaptor.getValue().getFirst(); - assertThat(documentEntity.getFileName()).isEqualTo("test filename"); + assertThat(documentEntity.getFileName()).isEqualTo(modifiedFilename); assertThat(documentEntity.getUrl()).isEqualTo("test url"); assertThat(documentEntity.getBinaryUrl()).isEqualTo("test binary url"); - assertThat(documentEntity.getCategoryId()).isNull(); + assertThat(documentEntity.getCategoryId()).isEqualTo(CaseFileCategory.APPLICATIONS.getId()); assertThat(documentEntity.getContentType()).isEqualTo("test content type"); assertThat(documentEntity.getSize()).isEqualTo(1234L); } @@ -343,6 +365,37 @@ void shouldNotSaveUploadedDocumentsIfSupportingDocumentsFlagIsNo() { assertThat(genAppEntity.getDocuments()).isEmpty(); } + @Test + void shouldAddGenAppToCaseEntityBeforeRenamingDocuments() { + // Given + Document document = Document.builder() + .filename("test filename") + .url("test url") + .binaryUrl("test binary url") + .build(); + + UUID applicantPartyId = UUID.randomUUID(); + when(applicantParty.getId()).thenReturn(applicantPartyId); + + UploadedDocument uploadedDocument = UploadedDocument.builder() + .document(document) + .build(); + + CitizenGenAppRequest genAppRequest = CitizenGenAppRequest.builder() + .hasSupportingDocuments(VerticalYesNo.YES) + .uploadedDocuments(List.of(ListValue.builder().value(uploadedDocument).build())) + .build(); + + // When + underTest.createGenAppEntity(genAppRequest, pcsCaseEntity, applicantParty); + + // Then + InOrder inOrder = inOrder(pcsCaseEntity, documentNameService); + inOrder.verify(pcsCaseEntity).addGenApp(isA(GenAppEntity.class)); + inOrder.verify(documentNameService) + .appendGenAppPostfix(anyString(), isA(GenAppEntity.class), eq(mainClaim), eq(applicantPartyId)); + } + @ParameterizedTest @EnumSource(value = LanguageUsed.class) void shouldSetLanguageUsed(LanguageUsed languageUsed) { diff --git a/src/test/java/uk/gov/hmcts/reform/pcs/ccd/view/GenAppsViewTest.java b/src/test/java/uk/gov/hmcts/reform/pcs/ccd/view/GenAppsViewTest.java index f9094323f9..53af0b363b 100644 --- a/src/test/java/uk/gov/hmcts/reform/pcs/ccd/view/GenAppsViewTest.java +++ b/src/test/java/uk/gov/hmcts/reform/pcs/ccd/view/GenAppsViewTest.java @@ -188,10 +188,9 @@ void shouldSetSubmissionDocument() { GenAppEntity genAppEntity1 = createGenAppEntity(UUID.randomUUID(), genApp1SubmittedDate); DocumentEntity submissionDocumentEntity = mock(DocumentEntity.class); genAppEntity1.setSubmissionDocument(submissionDocumentEntity); - when(submissionDocumentEntity.getId()).thenReturn(pcsDocumentId); - Document expectedSubmissionDocument = mock(Document.class); - when(modelMapper.map(submissionDocumentEntity, Document.class)).thenReturn(expectedSubmissionDocument); + final Document expectedSubmissionDocument = stubDocument(submissionDocumentEntity, pcsDocumentId); + when(pcsCaseEntity.getGenApps()).thenReturn(Set.of(genAppEntity1)); // When @@ -202,10 +201,54 @@ void shouldSetSubmissionDocument() { assertThat(genApps).hasSize(1); - DocumentWithId actualSubmission = genApps.getFirst().getValue().getSubmissionDocument(); + DocumentWithId actualSubmissionDocument = genApps.getFirst().getValue().getSubmissionDocument(); + + assertThat(actualSubmissionDocument.getId()).isEqualTo(pcsDocumentId.toString()); + assertThat(actualSubmissionDocument.getDocument()).isEqualTo(expectedSubmissionDocument); + } + + @Test + void shouldSetSupporingDocsDocument() { + // Given + UUID pcsDocumentId1 = UUID.randomUUID(); + UUID pcsDocumentId2 = UUID.randomUUID(); + + LocalDateTime genAppSubmittedDate = LocalDateTime.parse("2026-05-02T15:00:00"); + GenAppEntity genAppEntity = createGenAppEntity(UUID.randomUUID(), genAppSubmittedDate); + when(pcsCaseEntity.getGenApps()).thenReturn(Set.of(genAppEntity)); + DocumentEntity documentEntity1 = mock(DocumentEntity.class); + DocumentEntity documentEntity2 = mock(DocumentEntity.class); + + final Document expectedSupportingDocument1 = stubDocument(documentEntity1, pcsDocumentId1); + final Document expectedSupportingDocument2 = stubDocument(documentEntity2, pcsDocumentId2); + + genAppEntity.setDocuments(List.of(documentEntity1, documentEntity2)); + + // When + underTest.setCaseFields(pcsCase, pcsCaseEntity); + + // Then + List> genApps = pcsCase.getGenApps(); + + assertThat(genApps).hasSize(1); + + List> actualSupportingDocuments = genApps.getFirst().getValue().getSupportingDocuments(); + + assertThat(actualSupportingDocuments).hasSize(2); + assertThat(actualSupportingDocuments.get(0).getId()).isEqualTo(pcsDocumentId1.toString()); + assertThat(actualSupportingDocuments.get(0).getValue()).isEqualTo(expectedSupportingDocument1); + + assertThat(actualSupportingDocuments.get(1).getId()).isEqualTo(pcsDocumentId2.toString()); + assertThat(actualSupportingDocuments.get(1).getValue()).isEqualTo(expectedSupportingDocument2); + + } + + private Document stubDocument(DocumentEntity documentEntity, UUID pcsDocumentId) { + when(documentEntity.getId()).thenReturn(pcsDocumentId); - assertThat(actualSubmission.getId()).isEqualTo(pcsDocumentId.toString()); - assertThat(actualSubmission.getDocument()).isEqualTo(expectedSubmissionDocument); + Document document = mock(Document.class); + when(modelMapper.map(documentEntity, Document.class)).thenReturn(document); + return document; } private static PartyEntity createPartyEntityWithIdamId(UUID currentUserIdamId) {