diff --git a/src/contractTest/resources/application.yaml b/src/contractTest/resources/application.yaml index 9569e5b8369..3a8c97e6904 100644 --- a/src/contractTest/resources/application.yaml +++ b/src/contractTest/resources/application.yaml @@ -544,6 +544,8 @@ document: doc_send_reply_message_template: FL-PRL-LET-ENG-SEND-REPLY-MESSAGE.docx doc_send_reply_message_welsh_template: FL-PRL-LET-WEL-SEND-REPLY-MESSAGE.docx citizen: + prl_citizen_witness_statement_welsh_template: FL-PRL-GOR-WEL-Citizen-Witness-Statement.docx + prl_citizen_witness_statement_template: FL-PRL-GOR-ENG-Citizen-Witness-Statement.docx prl_citizen_upload_template: FL-PRL-GOR-ENG-Citizen-Uploaded-Statement.docx prl_citizen_upload_filename: Citizen_Uploaded_Statement.pdf fl401listonnotice: diff --git a/src/functionalTest/resources/application.yaml b/src/functionalTest/resources/application.yaml index 1075db64363..595452e30e6 100644 --- a/src/functionalTest/resources/application.yaml +++ b/src/functionalTest/resources/application.yaml @@ -577,6 +577,8 @@ document: doc_send_reply_message_template: FL-PRL-LET-ENG-SEND-REPLY-MESSAGE.docx doc_send_reply_message_welsh_template: FL-PRL-LET-WEL-SEND-REPLY-MESSAGE.docx citizen: + prl_citizen_witness_statement_welsh_template: FL-PRL-GOR-WEL-Citizen-Witness-Statement.docx + prl_citizen_witness_statement_template: FL-PRL-GOR-ENG-Citizen-Witness-Statement.docx prl_citizen_upload_template: FL-PRL-GOR-ENG-Citizen-Uploaded-Statement.docx prl_citizen_upload_filename: Citizen_Uploaded_Statement.pdf fl401listonnotice: diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/prl/controllers/citizen/CaseDocumentControllerIntegrationTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/prl/controllers/citizen/CaseDocumentControllerIntegrationTest.java index d20bf8a9f78..001bbbaeb0f 100644 --- a/src/integrationTest/java/uk/gov/hmcts/reform/prl/controllers/citizen/CaseDocumentControllerIntegrationTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/prl/controllers/citizen/CaseDocumentControllerIntegrationTest.java @@ -9,12 +9,12 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; @@ -69,34 +69,35 @@ public class CaseDocumentControllerIntegrationTest { @Autowired private WebApplicationContext webApplicationContext; - @MockBean - DocumentGenService documentGenService; + @MockitoBean + private DocumentGenService documentGenService; - @MockBean - UploadDocumentService uploadService; + @MockitoBean + private UploadDocumentService uploadService; - @MockBean - AuthorisationService authorisationService; + @MockitoBean + private AuthorisationService authorisationService; - @MockBean - CoreCaseDataApi coreCaseDataApi; - @MockBean - IdamClient idamClient; + @MockitoBean + private CoreCaseDataApi coreCaseDataApi; - @MockBean - CaseService caseService; + @MockitoBean + private IdamClient idamClient; - @MockBean - EmailService emailService; + @MockitoBean + private CaseService caseService; - @MockBean - CitizenDocumentService citizenDocumentService; + @MockitoBean + private EmailService emailService; - @MockBean - UserInfo userInfo; + @MockitoBean + private CitizenDocumentService citizenDocumentService; + + @MockitoBean + private UserInfo userInfo; @Autowired - ObjectMapper objectMapper; + private ObjectMapper objectMapper; @Before public void setUp() { @@ -139,10 +140,6 @@ public void testDeleteCitizenStatementDocument() throws Exception { public void testGenerateCitizenStatementDocument() throws Exception { when(authorisationService.isAuthorized(anyString(), anyString())).thenReturn(true); - Map map = new HashMap<>(); - map.put("caseId", "123"); - map.put("citizenUploadedDocumentList", List.of(element(UploadedDocuments.builder().build()))); - map.put("partyId", "7663081e-778d-4317-b278-7642b740d317"); String caseDetails = ResourceLoader.loadJson("requests/c100-respondent-solicitor-c1adraft-generate.json"); @@ -243,15 +240,6 @@ public void testUploadCitizenStatementDocument() throws Exception { "test content".getBytes() ); - String jsonRequest = "{" - + "\"caseId\": \"123\"," - + "\"documentType\": \"statement\"," - + "\"partyName\": \"John Doe\"," - + "\"partyId\": \"7663081e-778d-4317-b278-7642b740d317\"," - + "\"isApplicant\": \"true\"," - + "\"documentRequestedByCourt\": \"YES\"" - + "}"; - mockMvc.perform( multipart(url) .file(file) @@ -363,7 +351,7 @@ public void testCitizenGenerateDocument() throws Exception { .document(Document.builder().documentFileName("test.pdf").build()) .build(); - when(documentGenService.generateAndUploadDocument(anyString(), any())).thenReturn(documentResponse); + when(documentGenService.generateAndUploadDocument(anyString(), any())).thenReturn(List.of(documentResponse)); String url = "/citizen-generate-document"; String jsonRequest = "{" diff --git a/src/integrationTest/resources/application.yaml b/src/integrationTest/resources/application.yaml index af81e03e6f2..c95622d50b0 100644 --- a/src/integrationTest/resources/application.yaml +++ b/src/integrationTest/resources/application.yaml @@ -722,6 +722,8 @@ document: doc_send_reply_message_template: FL-PRL-LET-ENG-SEND-REPLY-MESSAGE.docx doc_send_reply_message_welsh_template: FL-PRL-LET-WEL-SEND-REPLY-MESSAGE.docx citizen: + prl_citizen_witness_statement_welsh_template: FL-PRL-GOR-WEL-Citizen-Witness-Statement.docx + prl_citizen_witness_statement_template: FL-PRL-GOR-ENG-Citizen-Witness-Statement.docx prl_citizen_upload_template: FL-PRL-GOR-ENG-Citizen-Uploaded-Statement.docx prl_citizen_upload_filename: Citizen_Uploaded_Statement.pdf fl401listonnotice: diff --git a/src/main/java/uk/gov/hmcts/reform/prl/controllers/citizen/CaseDocumentController.java b/src/main/java/uk/gov/hmcts/reform/prl/controllers/citizen/CaseDocumentController.java index a9a93d3d413..bce43eb8839 100644 --- a/src/main/java/uk/gov/hmcts/reform/prl/controllers/citizen/CaseDocumentController.java +++ b/src/main/java/uk/gov/hmcts/reform/prl/controllers/citizen/CaseDocumentController.java @@ -206,12 +206,12 @@ private void sendEmailToCitizen(CaseData tempCaseData, String name, String email emailService.send( email, EmailTemplateNames.DOCUMENT_UPLOADED, - buildUploadDocuemntEmail(tempCaseData, name, dashboardUrl), + buildUploadDocumentEmail(tempCaseData, name, dashboardUrl), LanguagePreference.english ); } - private EmailTemplateVars buildUploadDocuemntEmail(CaseData caseData, String name, String link) { + private EmailTemplateVars buildUploadDocumentEmail(CaseData caseData, String name, String link) { return UploadDocumentEmail.builder() .caseReference(String.valueOf(caseData.getId())) .caseName(caseData.getApplicantCaseName()) @@ -225,7 +225,7 @@ private EmailTemplateVars buildUploadDocuemntEmail(CaseData caseData, String nam @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Uploaded Successfully"), @ApiResponse(responseCode = "400", description = "Bad Request while uploading the document"), - @ApiResponse(responseCode = "401", description = "Provided Authroization token is missing or invalid"), + @ApiResponse(responseCode = "401", description = "Provided authorization token is missing or invalid"), @ApiResponse(responseCode = "500", description = "Internal Server Error") }) public ResponseEntity uploadCitizenStatementDocument(@RequestHeader(HttpHeaders.AUTHORIZATION) String authorisation, @@ -317,9 +317,9 @@ public String deleteCitizenStatementDocument(@RequestBody DeleteDocumentRequest CaseData tempCaseData = CaseUtils.getCaseData(caseDetails, objectMapper); if (deleteDocumentRequest.getValues() != null && deleteDocumentRequest.getValues().containsKey("documentId")) { - final String documenIdToBeDeleted = deleteDocumentRequest.getValues().get("documentId"); + final String documentIdToBeDeleted = deleteDocumentRequest.getValues().get("documentId"); tempUploadedDocumentsList = tempCaseData.getCitizenUploadedDocumentList(); - uploadedDocumentsList = tempUploadedDocumentsList.stream().filter(element -> !documenIdToBeDeleted.equalsIgnoreCase( + uploadedDocumentsList = tempUploadedDocumentsList.stream().filter(element -> !documentIdToBeDeleted.equalsIgnoreCase( element.getId().toString())) .toList(); } @@ -344,7 +344,7 @@ public String deleteCitizenStatementDocument(@RequestBody DeleteDocumentRequest @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Uploaded Successfully"), @ApiResponse(responseCode = "400", description = "Bad Request while uploading the document"), - @ApiResponse(responseCode = "401", description = "Provided Authroization token is missing or invalid"), + @ApiResponse(responseCode = "401", description = "Provided Authorization token is missing or invalid"), @ApiResponse(responseCode = "500", description = "Internal Server Error") }) public ResponseEntity uploadCitizenDocument(@RequestHeader(HttpHeaders.AUTHORIZATION) String authorisation, @@ -419,16 +419,16 @@ public ResponseEntity citizenGenerateDocument(@RequestHeader(HttpHeaders throw (new RuntimeException(INVALID_CLIENT)); } - DocumentResponse documentResponse = null; + List documentResponses; try { - documentResponse = documentGenService.generateAndUploadDocument(authorisation, documentRequest); + documentResponses = documentGenService.generateAndUploadDocument(authorisation, documentRequest); } catch (DocumentGenerationException dge) { log.error("Exception in generating a document {}", dge.getMessage()); return ResponseEntity.internalServerError().body("Error in generating a document"); } - if (isNotEmpty(documentResponse)) { - return ResponseEntity.ok(documentResponse); + if (isNotEmpty(documentResponses)) { + return ResponseEntity.ok(documentResponses); } else { return ResponseEntity.internalServerError().body("Error in generating citizen document"); } diff --git a/src/main/java/uk/gov/hmcts/reform/prl/models/dto/citizen/DocumentCategory.java b/src/main/java/uk/gov/hmcts/reform/prl/models/dto/citizen/DocumentCategory.java index 1b669561fac..786d7082bf9 100644 --- a/src/main/java/uk/gov/hmcts/reform/prl/models/dto/citizen/DocumentCategory.java +++ b/src/main/java/uk/gov/hmcts/reform/prl/models/dto/citizen/DocumentCategory.java @@ -57,6 +57,18 @@ public static DocumentCategory getValue(String id) { return DocumentCategory.valueOf(id); } + public boolean isWitnessStatement() { + return WITNESS_STATEMENTS_APPLICANT.equals(this) || WITNESS_STATEMENTS_RESPONDENT.equals(this); + } + + public boolean isApplicantWitnessStatement() { + return WITNESS_STATEMENTS_APPLICANT.equals(this); + } + + public boolean isRespondentWitnessStatement() { + return WITNESS_STATEMENTS_RESPONDENT.equals(this); + } + private static class Constants { public static final String ANY_OTHER_DOC = "anyOtherDoc"; public static final String ANY_OTHER_DOCUMENTS = "Any Other Documents"; diff --git a/src/main/java/uk/gov/hmcts/reform/prl/services/DgsService.java b/src/main/java/uk/gov/hmcts/reform/prl/services/DgsService.java index 5c41515e818..508a68a15e1 100644 --- a/src/main/java/uk/gov/hmcts/reform/prl/services/DgsService.java +++ b/src/main/java/uk/gov/hmcts/reform/prl/services/DgsService.java @@ -1,5 +1,6 @@ package uk.gov.hmcts.reform.prl.services; +import com.fasterxml.jackson.databind.ObjectMapper; import feign.FeignException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -12,17 +13,33 @@ import uk.gov.hmcts.reform.prl.framework.exceptions.DocumentGenerationException; import uk.gov.hmcts.reform.prl.mapper.AppObjectMapper; import uk.gov.hmcts.reform.prl.mapper.welshlang.WelshLangMapper; +import uk.gov.hmcts.reform.prl.models.Element; +import uk.gov.hmcts.reform.prl.models.complextypes.PartyDetails; import uk.gov.hmcts.reform.prl.models.dto.GenerateDocumentRequest; import uk.gov.hmcts.reform.prl.models.dto.GeneratedDocumentInfo; import uk.gov.hmcts.reform.prl.models.dto.ccd.CaseData; import uk.gov.hmcts.reform.prl.models.dto.ccd.CaseDetails; +import uk.gov.hmcts.reform.prl.models.dto.citizen.DocumentCategory; import uk.gov.hmcts.reform.prl.models.dto.citizen.DocumentRequest; import uk.gov.hmcts.reform.prl.models.dto.citizen.GenerateAndUploadDocumentRequest; +import uk.gov.hmcts.reform.prl.services.citizen.CaseService; +import uk.gov.hmcts.reform.prl.utils.CaseUtils; +import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.function.Function; +import static java.util.Objects.nonNull; +import static org.apache.commons.collections4.ListUtils.emptyIfNull; import static uk.gov.hmcts.reform.prl.constants.PrlAppsConstants.C100_CASE_TYPE; +import static uk.gov.hmcts.reform.prl.constants.PrlAppsConstants.CASE_DATA_ID; +import static uk.gov.hmcts.reform.prl.constants.PrlAppsConstants.CASE_ID; +import static uk.gov.hmcts.reform.prl.constants.PrlAppsConstants.COURT_NAME_FIELD; +import static uk.gov.hmcts.reform.prl.constants.PrlAppsConstants.FL401_CASE_TYPE; +import static uk.gov.hmcts.reform.prl.constants.PrlAppsConstants.ISSUE_DATE_FIELD; @Slf4j @Service @@ -30,10 +47,19 @@ @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class DgsService { + private static final String SPACE = " "; + private static final String FAMILYMAN_CASE_NUMBER = "familymanCaseNumber"; + private static final String APPLICANT_NAME = "applicantName"; + private static final String RESPONDENT_NAME = "respondentName"; + private static final String CITIZEN_UPLOADED_STATEMENT = "citizenUploadedStatement"; + private static final String SIGNED_BY = "signedBy"; + private static final String SIGNED_DATE = "signedDate"; private final DgsApiClient dgsApiClient; private final AllegationOfHarmRevisedService allegationOfHarmService; private final HearingDataService hearingDataService; private final UserRoleService userRoleService; + private final CaseService caseService; + private final ObjectMapper objectMapper; private static final String CASE_DETAILS_STRING = "caseDetails"; private static final String ERROR_MESSAGE = "Error generating and storing document for case {}"; @@ -47,9 +73,6 @@ public GeneratedDocumentInfo generateDocument(String authorisation, String caseI .builder().template(templateName).values(dataMap).build() ); - } catch (FeignException ex) { - log.error(ERROR_MESSAGE, caseId); - throw new DocumentGenerationException(ex.getMessage(), ex); } catch (Exception ex) { log.error(ERROR_MESSAGE, caseId); throw new DocumentGenerationException(ex.getMessage(), ex); @@ -93,8 +116,7 @@ public GeneratedDocumentInfo generateDocument(String authorisation, CaseDetails public GeneratedDocumentInfo generateWelshDocument(String authorisation, String caseId, String caseTypeOfApplication, String templateName, Map dataMap) { - Map welshDataMap = new HashMap<>(); - welshDataMap.putAll(dataMap); + Map welshDataMap = new HashMap<>(dataMap); welshDataMap.forEach((k, v) -> { if (v != null) { Object updatedWelshObj = WelshLangMapper.applyWelshTranslation(k, v, @@ -156,7 +178,7 @@ public GeneratedDocumentInfo generateWelshDocument(String authorisation, CaseDet public GeneratedDocumentInfo generateCitizenDocument(String authorisation, GenerateAndUploadDocumentRequest generateAndUploadDocumentRequest, - String templateName) throws Exception { + String templateName) { Map tempCaseDetails = new HashMap<>(); String freeTextUploadStatements = null; @@ -166,7 +188,7 @@ public GeneratedDocumentInfo generateCitizenDocument(String authorisation, } String caseId = generateAndUploadDocumentRequest.getValues().get("caseId"); CaseDetails caseDetails = CaseDetails.builder().caseId(caseId).state("ISSUE") - .caseData(CaseData.builder().id(Long.valueOf(caseId)) + .caseData(CaseData.builder().id(Long.parseLong(caseId)) .citizenUploadedStatement(freeTextUploadStatements).build()).build(); tempCaseDetails.put( CASE_DETAILS_STRING, @@ -174,7 +196,7 @@ public GeneratedDocumentInfo generateCitizenDocument(String authorisation, ); - GeneratedDocumentInfo generatedDocumentInfo = null; + GeneratedDocumentInfo generatedDocumentInfo; try { generatedDocumentInfo = dgsApiClient.generateDocument(authorisation, GenerateDocumentRequest @@ -188,44 +210,77 @@ public GeneratedDocumentInfo generateCitizenDocument(String authorisation, return generatedDocumentInfo; } - public GeneratedDocumentInfo generateCitizenDocument(String authorisation, + public List generateCitizenDocument(String authorisation, DocumentRequest documentRequest, - String prlCitizenUploadTemplate) throws DocumentGenerationException { - Map tempCaseDetails = new HashMap<>(); - String caseId = documentRequest.getCaseId(); + List prlCitizenUploadTemplates, + DocumentCategory documentCategory) throws DocumentGenerationException { - CaseDetails caseDetails = CaseDetails.builder() - .caseId(caseId) - .caseData(CaseData.builder() - .id(Long.parseLong(caseId)) - .citizenUploadedStatement(documentRequest.getFreeTextStatements()) - .build()) - .build(); - tempCaseDetails.put( - CASE_DETAILS_STRING, - AppObjectMapper.getObjectMapper().convertValue(caseDetails, Map.class) - ); + String caseId = documentRequest.getCaseId(); + uk.gov.hmcts.reform.ccd.client.model.CaseDetails caseDetailsFromCcd = caseService.getCase(authorisation, caseId); + CaseData caseDataFromCcd = nonNull(caseDetailsFromCcd) ? CaseUtils.getCaseData(caseDetailsFromCcd, objectMapper) : null; + boolean applicantWitnessStatement = false; + boolean respondentWitnessStatement = false; + LocalDate issueDate = null; + String familymanCaseNumber = null; + String courtName = null; - GeneratedDocumentInfo generatedDocumentInfo = null; - try { - generatedDocumentInfo = - dgsApiClient.generateDocument( - authorisation, - GenerateDocumentRequest.builder() - .template(prlCitizenUploadTemplate) - .values(tempCaseDetails).build() - ); + if (nonNull(documentCategory)) { + applicantWitnessStatement = documentCategory.isApplicantWitnessStatement(); + respondentWitnessStatement = documentCategory.isRespondentWitnessStatement(); + } - } catch (Exception ex) { - log.error(ERROR_MESSAGE, caseId); - throw new DocumentGenerationException(ex.getMessage(), ex); + if (nonNull(caseDataFromCcd)) { + issueDate = caseDataFromCcd.getIssueDate(); + familymanCaseNumber = caseDataFromCcd.getFamilymanCaseNumber(); + courtName = caseDataFromCcd.getCourtName(); } - return generatedDocumentInfo; + + Map caseDetails = new HashMap<>(); + caseDetails.put(CASE_ID, caseId); + caseDetails.put(COURT_NAME_FIELD, courtName); + caseDetails.put(CASE_DATA_ID, Long.parseLong(caseId)); + caseDetails.put(ISSUE_DATE_FIELD, issueDate); + caseDetails.put(FAMILYMAN_CASE_NUMBER, familymanCaseNumber); + caseDetails.put( + APPLICANT_NAME, + getApplicantName(applicantWitnessStatement, respondentWitnessStatement, documentRequest, caseDataFromCcd)); + caseDetails.put( + RESPONDENT_NAME, + getRespondentName(respondentWitnessStatement, applicantWitnessStatement, documentRequest, caseDataFromCcd)); + caseDetails.put(CITIZEN_UPLOADED_STATEMENT, documentRequest.getFreeTextStatements()); + caseDetails.put(SIGNED_BY, documentRequest.getPartyName()); + caseDetails.put(SIGNED_DATE, LocalDateTime.now()); + + + return emptyIfNull(prlCitizenUploadTemplates) + .stream() + .map(getGeneratedDocumentInfo(authorisation, caseDetails, caseId)) + .toList(); + } + + private Function getGeneratedDocumentInfo(String authorisation, Map caseDetails, String caseId) { + return prlCitizenUploadTemplate -> { + GeneratedDocumentInfo generatedDocumentInfo; + try { + generatedDocumentInfo = + dgsApiClient.generateDocument( + authorisation, + GenerateDocumentRequest.builder() + .template(prlCitizenUploadTemplate) + .values(caseDetails).build() + ); + + } catch (Exception ex) { + log.error(ERROR_MESSAGE, caseId, ex); + throw new DocumentGenerationException(ex.getMessage(), ex); + } + return generatedDocumentInfo; + }; } public GeneratedDocumentInfo generateCoverLetterDocument(String authorisation, Map requestPayload, String templateName, String caseId) throws Exception { - GeneratedDocumentInfo generatedDocumentInfo = null; + GeneratedDocumentInfo generatedDocumentInfo; try { generatedDocumentInfo = dgsApiClient.generateDocument(authorisation, GenerateDocumentRequest @@ -238,4 +293,56 @@ public GeneratedDocumentInfo generateCoverLetterDocument(String authorisation, M } return generatedDocumentInfo; } + + private String getApplicantName(boolean applicantWitnessStatement, boolean respondentWitnessStatement, + DocumentRequest documentRequest,CaseData caseDataFromCcd) { + String applicantName = null; + if (applicantWitnessStatement) { + applicantName = documentRequest.getPartyName(); + } else { + if (respondentWitnessStatement) { + String caseTypeOfApplication = caseDataFromCcd.getCaseTypeOfApplication(); + if (C100_CASE_TYPE.equalsIgnoreCase(caseTypeOfApplication)) { + List> applicantElements = emptyIfNull(caseDataFromCcd.getApplicants()); + List applicants = emptyIfNull(applicantElements.stream().map(Element::getValue).toList()); + applicantName = String.join( + ", ", applicants.stream() + .map(partyDetails -> partyDetails.getFirstName() + SPACE + partyDetails.getLastName()).toList() + ); + } else if (FL401_CASE_TYPE.equalsIgnoreCase(caseTypeOfApplication)) { + PartyDetails applicantsFL401 = caseDataFromCcd.getApplicantsFL401(); + applicantName = nonNull(applicantsFL401) ? applicantsFL401.getFirstName() + SPACE + applicantsFL401.getLastName() : ""; + } + } else { + applicantName = ""; + } + } + return applicantName; + } + + private String getRespondentName(boolean respondentWitnessStatement, boolean applicantWitnessStatement, + DocumentRequest documentRequest,CaseData caseDataFromCcd) { + String respondentName = null; + if (respondentWitnessStatement) { + respondentName = documentRequest.getPartyName(); + } else { + if (applicantWitnessStatement) { + String caseTypeOfApplication = caseDataFromCcd.getCaseTypeOfApplication(); + if (C100_CASE_TYPE.equalsIgnoreCase(caseTypeOfApplication)) { + List> respondentElements = emptyIfNull(caseDataFromCcd.getRespondents()); + List respondents = emptyIfNull(respondentElements.stream().map(Element::getValue).toList()); + respondentName = String.join( + ", ", respondents.stream() + .map(partyDetails -> partyDetails.getFirstName() + SPACE + partyDetails.getLastName()).toList() + ); + } else if (FL401_CASE_TYPE.equalsIgnoreCase(caseTypeOfApplication)) { + PartyDetails respondentFL401 = caseDataFromCcd.getRespondentsFL401(); + respondentName = nonNull(respondentFL401) ? respondentFL401.getFirstName() + SPACE + respondentFL401.getLastName() : ""; + } + } else { + respondentName = ""; + } + } + return respondentName; + } } diff --git a/src/main/java/uk/gov/hmcts/reform/prl/services/document/DocumentGenService.java b/src/main/java/uk/gov/hmcts/reform/prl/services/document/DocumentGenService.java index f53a7c2a4ff..3c51d39aa98 100644 --- a/src/main/java/uk/gov/hmcts/reform/prl/services/document/DocumentGenService.java +++ b/src/main/java/uk/gov/hmcts/reform/prl/services/document/DocumentGenService.java @@ -3,6 +3,7 @@ import feign.FeignException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.io.FilenameUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -45,14 +46,17 @@ import java.io.IOException; import java.time.LocalDate; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Objects; import java.util.Optional; +import java.util.function.IntFunction; +import java.util.stream.IntStream; +import static java.util.Objects.nonNull; import static java.util.Optional.ofNullable; import static uk.gov.hmcts.reform.prl.constants.PrlAppsConstants.C100_CASE_TYPE; import static uk.gov.hmcts.reform.prl.constants.PrlAppsConstants.C1A_DRAFT_HINT; @@ -282,6 +286,10 @@ private record DocumentUpdateContext( protected String privacyNoticeFilename; @Value("${document.templates.citizen.prl_citizen_upload_template}") protected String prlCitizenUploadTemplate; + @Value("${document.templates.citizen.prl_citizen_witness_statement_template}") + protected String prlCitizenWitnessStatementTemplate; + @Value("${document.templates.citizen.prl_citizen_witness_statement_welsh_template}") + protected String prlCitizenWitnessStatementWelshTemplate; @Value("${document.templates.citizen.prl_citizen_upload_filename}") protected String prlCitizenUploadFileName; @Value("${document.templates.fl401listonnotice.prl_fl404b_for_da_list_on_notice_template}") @@ -735,17 +743,19 @@ public Map generateC7DraftDocuments(String authorisation, CaseDa return updatedCaseData; } - private String getCitizenUploadedStatementFileName(DocumentRequest documentRequest) { + private String getCitizenUploadedStatementFileName(DocumentRequest documentRequest, DocumentCategory documentCategory, String language) { StringBuilder fileNameBuilder = new StringBuilder(); if (null != documentRequest.getPartyName()) { fileNameBuilder.append(documentRequest.getPartyName().replace(EMPTY_SPACE_STRING, UNDERSCORE)); fileNameBuilder.append(UNDERSCORE); } - if (null != documentRequest.getCategoryId()) { - fileNameBuilder.append(DocumentCategory.getValue(documentRequest.getCategoryId()).getFileNamePrefix()); + if (nonNull(documentCategory)) { + fileNameBuilder.append(documentCategory.getFileNamePrefix()); fileNameBuilder.append(UNDERSCORE); } + fileNameBuilder.append(language); + fileNameBuilder.append(UNDERSCORE); fileNameBuilder.append(dateTime.now().format(DateTimeFormatter.ofPattern("dd-MMM-yyyy-hh-mm-ss-a", Locale.UK))); fileNameBuilder.append(UNDERSCORE); fileNameBuilder.append(SUBMITTED_PDF); @@ -1224,7 +1234,7 @@ private boolean isChildrenDetailsConfidentiality(CaseData caseData, Optional typeOfApplicationOrders) { if (typeOfApplicationOrders.isPresent() && typeOfApplicationOrders.get().getOrderType().contains( FL401OrderTypeEnum.occupationOrder) - && Objects.nonNull(caseData.getHome()) + && nonNull(caseData.getHome()) && YesOrNo.Yes.equals(caseData.getHome().getDoAnyChildrenLiveAtAddress())) { List childrenLiveAtAddresses = caseData.getHome().getChildren().stream().map(Element::getValue).toList(); @@ -1570,26 +1580,45 @@ public Document convertToPdf(String authorisation, Document document) { return document; } - public DocumentResponse generateAndUploadDocument(String authorisation, + public List generateAndUploadDocument(String authorisation, DocumentRequest documentRequest) throws DocumentGenerationException { - //generate file name - String fileName = getCitizenUploadedStatementFileName(documentRequest); - log.info("fileName {}", fileName); + String categoryId = documentRequest.getCategoryId(); + DocumentCategory documentCategory = nonNull(categoryId) ? DocumentCategory.getValue(categoryId) : null; + + + List citizenUploadTemplates = nonNull(documentCategory) && documentCategory.isWitnessStatement() + ? List.of(prlCitizenWitnessStatementTemplate, prlCitizenWitnessStatementWelshTemplate) : List.of(prlCitizenUploadTemplate); - GeneratedDocumentInfo generatedDocumentInfo = dgsService.generateCitizenDocument( + + List generatedDocumentInfos = dgsService.generateCitizenDocument( authorisation, documentRequest, - prlCitizenUploadTemplate + citizenUploadTemplates, + documentCategory ); - log.info("generatedDocumentInfo {}", generatedDocumentInfo); - if (null != generatedDocumentInfo) { + + log.info("generatedDocumentInfo {}", generatedDocumentInfos); + + List languages = List.of("ENG", "WELSH"); + List documentResponses = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(generatedDocumentInfos)) { + documentResponses = IntStream.range(0, generatedDocumentInfos.size()) + .mapToObj(buildDocumentResponse(documentRequest, documentCategory, languages, generatedDocumentInfos)) + .toList(); + } + return documentResponses; + } + + private IntFunction buildDocumentResponse(DocumentRequest documentRequest, DocumentCategory documentCategory, + List languages, List generatedDocumentInfos) { + return i -> { + String fileName = getCitizenUploadedStatementFileName(documentRequest, documentCategory, languages.get(i)); + log.info("fileName {}", fileName); return DocumentResponse.builder() .status(SUCCESS) - .document(generateDocumentField(fileName, generatedDocumentInfo)) + .document(generateDocumentField(fileName, generatedDocumentInfos.get(i))) .build(); - } - - return null; + }; } private boolean isCaseNotLocked(DocumentUpdateContext documentUpdateContext) { diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 809271cc2a9..cd09d33e823 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -732,6 +732,8 @@ document: doc_send_reply_message_template: FL-PRL-LET-ENG-SEND-REPLY-MESSAGE.docx doc_send_reply_message_welsh_template: FL-PRL-LET-WEL-SEND-REPLY-MESSAGE.docx citizen: + prl_citizen_witness_statement_welsh_template: FL-PRL-GOR-WEL-Citizen-Witness-Statement.docx + prl_citizen_witness_statement_template: FL-PRL-GOR-ENG-Citizen-Witness-Statement.docx prl_citizen_upload_template: FL-PRL-GOR-ENG-Citizen-Uploaded-Statement.docx prl_citizen_upload_filename: Citizen_Uploaded_Statement.pdf fl401listonnotice: diff --git a/src/smokeTest/resources/application.yaml b/src/smokeTest/resources/application.yaml index 879190c7376..1a462d20fda 100644 --- a/src/smokeTest/resources/application.yaml +++ b/src/smokeTest/resources/application.yaml @@ -567,6 +567,8 @@ document: doc_send_reply_message_template: FL-PRL-LET-ENG-SEND-REPLY-MESSAGE.docx doc_send_reply_message_welsh_template: FL-PRL-LET-WEL-SEND-REPLY-MESSAGE.docx citizen: + prl_citizen_witness_statement_welsh_template: FL-PRL-GOR-WEL-Citizen-Witness-Statement.docx + prl_citizen_witness_statement_template: FL-PRL-GOR-ENG-Citizen-Witness-Statement.docx prl_citizen_upload_template: PRL-CA-GOR-ENG-Draft-An-Order.docx prl_citizen_upload_filename: Self_created.pdf fl401listonnotice: diff --git a/src/test/java/uk/gov/hmcts/reform/prl/controllers/citizen/CaseDocumentControllerTest.java b/src/test/java/uk/gov/hmcts/reform/prl/controllers/citizen/CaseDocumentControllerTest.java index 34aa01fc337..d9e94564d81 100644 --- a/src/test/java/uk/gov/hmcts/reform/prl/controllers/citizen/CaseDocumentControllerTest.java +++ b/src/test/java/uk/gov/hmcts/reform/prl/controllers/citizen/CaseDocumentControllerTest.java @@ -325,12 +325,6 @@ void testGenerateCitizenStatementDocumentt() throws Exception { @Test void testUploadCitizenStatementDocument() { - HashMap map = new HashMap<>(); - map.put("caseId", "1656350492135029"); - map.put("state", "AWAITING_SUBMISSION_TO_HMCTS"); - map.put("documentType", "test"); - map.put("partyName", "test"); - map.put("partyId", "test"); Document document = Document.builder().documentUrl("") .documentFileName("test") .build(); @@ -564,7 +558,7 @@ void testGenerateDocument() throws DocumentGenerationException { when(documentGenService.generateAndUploadDocument( AUTH_TOKEN, documentRequest - )).thenReturn(mockDocumentResponse); + )).thenReturn(List.of(mockDocumentResponse)); //Action ResponseEntity response = caseDocumentController.citizenGenerateDocument( @@ -575,7 +569,6 @@ void testGenerateDocument() throws DocumentGenerationException { //Then assertEquals(OK, response.getStatusCode()); - assertEquals(DocumentResponse.class, response.getBody().getClass()); assertNotNull(response.getBody()); } diff --git a/src/test/java/uk/gov/hmcts/reform/prl/services/DgsServiceTest.java b/src/test/java/uk/gov/hmcts/reform/prl/services/DgsServiceTest.java index c1e6ab47076..2e45c4c9052 100644 --- a/src/test/java/uk/gov/hmcts/reform/prl/services/DgsServiceTest.java +++ b/src/test/java/uk/gov/hmcts/reform/prl/services/DgsServiceTest.java @@ -1,25 +1,34 @@ package uk.gov.hmcts.reform.prl.services; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; import feign.FeignException; +import org.jspecify.annotations.NonNull; import org.junit.Before; import org.junit.Test; import org.junit.function.ThrowingRunnable; import org.junit.runner.RunWith; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.test.util.ReflectionTestUtils; import uk.gov.hmcts.reform.prl.clients.DgsApiClient; import uk.gov.hmcts.reform.prl.framework.exceptions.DocumentGenerationException; import uk.gov.hmcts.reform.prl.models.Element; +import uk.gov.hmcts.reform.prl.models.complextypes.PartyDetails; import uk.gov.hmcts.reform.prl.models.dto.GenerateDocumentRequest; import uk.gov.hmcts.reform.prl.models.dto.GeneratedDocumentInfo; import uk.gov.hmcts.reform.prl.models.dto.ccd.CaseData; import uk.gov.hmcts.reform.prl.models.dto.ccd.CaseDetails; import uk.gov.hmcts.reform.prl.models.dto.ccd.HearingData; import uk.gov.hmcts.reform.prl.models.dto.ccd.ManageOrders; +import uk.gov.hmcts.reform.prl.models.dto.citizen.DocumentCategory; import uk.gov.hmcts.reform.prl.models.dto.citizen.DocumentRequest; import uk.gov.hmcts.reform.prl.models.dto.citizen.GenerateAndUploadDocumentRequest; +import uk.gov.hmcts.reform.prl.services.citizen.CaseService; import java.util.HashMap; import java.util.List; @@ -30,17 +39,39 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThrows; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when; +import static uk.gov.hmcts.reform.prl.constants.PrlAppsConstants.C100_CASE_TYPE; +import static uk.gov.hmcts.reform.prl.constants.PrlAppsConstants.FL401_CASE_TYPE; @RunWith(MockitoJUnitRunner.class) public class DgsServiceTest { - @InjectMocks + private static final String AUTHORISATION = " "; + private static final String TEMPLATE = "template"; + private static final String TEST_URL = "TestUrl"; + private static final String CASE_ID = "123"; + private static final String WITNESS_STATEMENTS_RESPONDENT = "WITNESS_STATEMENTS_RESPONDENT"; + private static final String RESPONDENT = "respondent"; + private static final String DOCUMENT_DETAILS = "test details"; + private static final String FREE_TEXT_STATEMENTS = "free text to generate document"; + private static final String PARTY_NAME = "appf appl"; + private static final String FIRST_NAME = "firstNameValue"; + private static final String LAST_NAME = "lastName"; + private static final String WITNESS_STATEMENTS_APPLICANT = "WITNESS_STATEMENTS_APPLICANT"; + private static final String PARTY_TYPE = "applicant"; private DgsService dgsService; @Mock private DgsApiClient dgsApiClient; + + @Mock + private CaseService caseService; + + @Mock + private ObjectMapper objectMapper; + @Mock private UserRoleService userRoleService; @@ -63,6 +94,8 @@ public class DgsServiceTest { @Before public void setUp() { + dgsService = new DgsService(dgsApiClient, allegationOfHarmRevisedService, hearingDataService, + userRoleService, caseService, objectMapper); caseData = CaseData.builder() .manageOrders(ManageOrders.builder() @@ -75,16 +108,16 @@ public void setUp() { .build(); caseDetails = CaseDetails.builder() - .caseId("123") + .caseId(CASE_ID) .caseData(caseData) .build(); generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); Map values = new HashMap<>(); - values.put("caseId","123"); + values.put("caseId", CASE_ID); values.put("freeTextUploadStatements","test"); generateAndUploadDocumentRequest = GenerateAndUploadDocumentRequest .builder() @@ -94,19 +127,20 @@ public void setUp() { .thenReturn(generatedDocumentInfo); documentRequest = DocumentRequest.builder() - .caseId("123") + .caseId(CASE_ID) .categoryId("POSITION_STATEMENTS") - .partyName("appf appl") - .partyType("applicant") - .restrictDocumentDetails("test details") - .freeTextStatements("free text to generate document") + .partyName(PARTY_NAME) + .partyType(PARTY_TYPE) + .restrictDocumentDetails(DOCUMENT_DETAILS) + .freeTextStatements(FREE_TEXT_STATEMENTS) .build(); + when(objectMapper.convertValue(any(), eq(CaseData.class))).thenReturn(caseData); } @Test - public void testToGenerateDocument() throws Exception { + public void testToGenerateDocument() { generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); @@ -118,10 +152,10 @@ public void testToGenerateDocument() throws Exception { } @Test - public void testToGenerateDocumentWithCaseData() throws Exception { + public void testToGenerateDocumentWithCaseData() { Map respondentDetails = new HashMap<>(); generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); @@ -134,16 +168,17 @@ public void testToGenerateCoverLetterDocument() throws Exception { Map dataMap = new HashMap<>(); dataMap.put("coverLetter", "test.pdf"); generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); assertNotNull(dgsService.generateCoverLetterDocument(authToken, dataMap, PRL_DRAFT_TEMPLATE, - "123")); + CASE_ID + )); } @Test - public void testToGenerateDocumentWithCaseDataNoDataExpectedException() throws Exception { + public void testToGenerateDocumentWithCaseDataNoDataExpectedException() { dgsService.generateDocument(authToken,null, PRL_DRAFT_TEMPLATE, null); Throwable exception = assertThrows(Exception.class, () -> { throw new Exception("Error generating and storing document for case"); @@ -152,7 +187,7 @@ public void testToGenerateDocumentWithCaseDataNoDataExpectedException() throws E } @Test - public void testToGenerateDocumentWithNoDataExpectedException() throws Exception { + public void testToGenerateDocumentWithNoDataExpectedException() { Map dataMap = new HashMap<>(); Mockito.doNothing().when(hearingDataService).populatePartiesAndSolicitorsNames(caseData, dataMap); dgsService.generateDocument(authToken, caseDetails, PRL_DRAFT_TEMPLATE); @@ -163,9 +198,9 @@ public void testToGenerateDocumentWithNoDataExpectedException() throws Exception } @Test - public void testToGenerateWelshDocument() throws Exception { + public void testToGenerateWelshDocument() { generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); @@ -179,7 +214,7 @@ public void testToGenerateWelshDocumentWithCaseData() { Map respondentDetails = new HashMap<>(); respondentDetails.put("fullName", "test"); generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); @@ -189,16 +224,31 @@ public void testToGenerateWelshDocumentWithCaseData() { } @Test - public void testgenerateCitizenDocument() throws Exception { - dgsService.generateCitizenDocument(" ", generateAndUploadDocumentRequest, " "); - assertEquals("test", generateAndUploadDocumentRequest.getValues().get("freeTextUploadStatements")); + public void testGenerateCitizenDocumentWithFreeTextUploadStatements() { + Map values = new HashMap<>(); + values.put("caseId", CASE_ID); + GenerateAndUploadDocumentRequest request = GenerateAndUploadDocumentRequest + .builder() + .values(values) + .build(); + + // when + GeneratedDocumentInfo result = dgsService.generateCitizenDocument( + AUTHORISATION, + request, + TEMPLATE + ); + + // then + assertNotNull(result); + assertEquals(TEST_URL, result.getUrl()); } @Test - public void testToGenerateDocumentWithEmptyHearingsData() throws Exception { + public void testToGenerateDocumentWithEmptyHearingsData() { CaseData caseData1 = CaseData.builder().manageOrders(ManageOrders.builder().build()).build(); CaseDetails caseDetails1 = CaseDetails.builder() - .caseId("123") + .caseId(CASE_ID) .caseData(caseData1) .build(); @@ -206,22 +256,199 @@ public void testToGenerateDocumentWithEmptyHearingsData() throws Exception { } @Test - public void testGenerateCitizenDocument() throws Exception { - //Given - generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") - .binaryUrl("binaryUrl") - .hashToken("testHashToken") + public void testGenerateRespondentWitnessStatementForC100() { + // Given + ObjectMapper objectMapper = getObjectMapper(); + setUpGenerateCitizenDocument(); + documentRequest = DocumentRequest.builder() + .caseId(CASE_ID) + .categoryId(WITNESS_STATEMENTS_RESPONDENT) + .partyName(PARTY_NAME) + .partyType(RESPONDENT) + .restrictDocumentDetails(DOCUMENT_DETAILS) + .freeTextStatements(FREE_TEXT_STATEMENTS) .build(); - //When - doReturn(generatedDocumentInfo).when(dgsApiClient).generateDocument( - Mockito.anyString(), - Mockito.any(GenerateDocumentRequest.class) - ); + PartyDetails partyDetail = PartyDetails.builder() + .caseTypeOfApplication(C100_CASE_TYPE) + .firstName(FIRST_NAME) + .lastName(LAST_NAME) + .build(); + Element element = Element.builder().id(UUID.randomUUID()) + .value(partyDetail).build(); + CaseData data = CaseData.builder() + .caseTypeOfApplication(C100_CASE_TYPE) + .applicants(List.of(element)) + .build(); + uk.gov.hmcts.reform.ccd.client.model.CaseDetails caseDetailsFromCcd = uk.gov.hmcts.reform.ccd.client.model.CaseDetails.builder() + .id(Long.parseLong(documentRequest.getCaseId())) + .data(objectMapper.convertValue(data, Map.class)) + .build(); + when(caseService.getCase(AUTHORISATION, documentRequest.getCaseId())).thenReturn(caseDetailsFromCcd); + + // When + GeneratedDocumentInfo response = dgsService.generateCitizenDocument( + AUTHORISATION, documentRequest, + List.of(TEMPLATE), DocumentCategory.WITNESS_STATEMENTS_RESPONDENT + ).getFirst(); + + // Then + assertNotNull(response); + assertNotNull(response.getBinaryUrl()); + assertNotNull(response.getHashToken()); + } + + + + @Test + public void testGenerateRespondentWitnessStatementForFl401() { + // Given + ObjectMapper objectMapper = getObjectMapper(); + setUpGenerateCitizenDocument(); + documentRequest = DocumentRequest.builder() + .caseId(CASE_ID) + .categoryId(WITNESS_STATEMENTS_RESPONDENT) + .partyName(PARTY_NAME) + .partyType(RESPONDENT) + .restrictDocumentDetails(DOCUMENT_DETAILS) + .freeTextStatements(FREE_TEXT_STATEMENTS) + .build(); + + PartyDetails partyDetail = PartyDetails.builder() + .caseTypeOfApplication(FL401_CASE_TYPE) + .firstName(FIRST_NAME) + .lastName(LAST_NAME) + .build(); + CaseData data = CaseData.builder() + .caseTypeOfApplication(FL401_CASE_TYPE) + .applicantsFL401(partyDetail) + .build(); + uk.gov.hmcts.reform.ccd.client.model.CaseDetails caseDetailsFromCcd = uk.gov.hmcts.reform.ccd.client.model.CaseDetails.builder() + .id(Long.parseLong(documentRequest.getCaseId())) + .data(objectMapper.convertValue(data, Map.class)) + .build(); + when(caseService.getCase(AUTHORISATION, documentRequest.getCaseId())).thenReturn(caseDetailsFromCcd); + + // When + GeneratedDocumentInfo response = dgsService.generateCitizenDocument( + AUTHORISATION, documentRequest, + List.of(TEMPLATE), DocumentCategory.WITNESS_STATEMENTS_RESPONDENT + ).get(0); + + // Then + assertNotNull(response); + assertNotNull(response.getBinaryUrl()); + assertNotNull(response.getHashToken()); + } + + + + + @Test + public void testGenerateApplicantWitnessStatementForC100() { + // Given + ObjectMapper objectMapper = getObjectMapper(); + setUpGenerateCitizenDocument(); + documentRequest = DocumentRequest.builder() + .caseId(CASE_ID) + .categoryId(WITNESS_STATEMENTS_APPLICANT) + .partyName(PARTY_NAME) + .partyType(PARTY_TYPE) + .restrictDocumentDetails(DOCUMENT_DETAILS) + .freeTextStatements(FREE_TEXT_STATEMENTS) + .build(); + + PartyDetails partyDetail = PartyDetails.builder() + .caseTypeOfApplication(C100_CASE_TYPE) + .firstName(FIRST_NAME) + .lastName(LAST_NAME) + .build(); + Element element = Element.builder().id(UUID.randomUUID()) + .value(partyDetail).build(); + CaseData data = CaseData.builder() + .caseTypeOfApplication(C100_CASE_TYPE) + .respondents(List.of(element)) + .build(); + uk.gov.hmcts.reform.ccd.client.model.CaseDetails caseDetailsFromCcd = uk.gov.hmcts.reform.ccd.client.model.CaseDetails.builder() + .id(Long.parseLong(documentRequest.getCaseId())) + .data(objectMapper.convertValue(data, Map.class)) + .build(); + when(caseService.getCase(AUTHORISATION, documentRequest.getCaseId())).thenReturn(caseDetailsFromCcd); + + // When + GeneratedDocumentInfo response = dgsService.generateCitizenDocument( + AUTHORISATION, documentRequest, + List.of(TEMPLATE), DocumentCategory.WITNESS_STATEMENTS_APPLICANT + ).getFirst(); + + // Then + assertNotNull(response); + assertNotNull(response.getBinaryUrl()); + assertNotNull(response.getHashToken()); + } + + @Test + public void testGenerateApplicantWitnessStatementForFl401() { + // Given + ObjectMapper objectMapper = getObjectMapper(); + setUpGenerateCitizenDocument(); + documentRequest = DocumentRequest.builder() + .caseId(CASE_ID) + .categoryId(WITNESS_STATEMENTS_APPLICANT) + .partyName(PARTY_NAME) + .partyType(PARTY_TYPE) + .restrictDocumentDetails(DOCUMENT_DETAILS) + .freeTextStatements(FREE_TEXT_STATEMENTS) + .build(); + + PartyDetails partyDetail = PartyDetails.builder() + .caseTypeOfApplication(FL401_CASE_TYPE) + .firstName(FIRST_NAME) + .lastName(LAST_NAME) + .build(); + + CaseData data = CaseData.builder() + .caseTypeOfApplication(FL401_CASE_TYPE) + .respondentsFL401(partyDetail) + .build(); + uk.gov.hmcts.reform.ccd.client.model.CaseDetails caseDetailsFromCcd = uk.gov.hmcts.reform.ccd.client.model.CaseDetails.builder() + .id(Long.parseLong(documentRequest.getCaseId())) + .data(objectMapper.convertValue(data, Map.class)) + .build(); + when(caseService.getCase(AUTHORISATION, documentRequest.getCaseId())).thenReturn(caseDetailsFromCcd); + + // When + GeneratedDocumentInfo response = dgsService.generateCitizenDocument( + AUTHORISATION, documentRequest, + List.of(TEMPLATE), DocumentCategory.WITNESS_STATEMENTS_APPLICANT + ).getFirst(); + + // Then + assertNotNull(response); + assertNotNull(response.getBinaryUrl()); + assertNotNull(response.getHashToken()); + } + + - //Action - GeneratedDocumentInfo response = dgsService.generateCitizenDocument(" ", documentRequest, " "); + + @Test + public void testGenerateCitizenDocumentWithCaseDetailsRetrievedFromCcd() { + // Given + setUpGenerateCitizenDocument(); + uk.gov.hmcts.reform.ccd.client.model.CaseDetails caseDetailsFromCcd = uk.gov.hmcts.reform.ccd.client.model.CaseDetails.builder() + .id(Long.parseLong(documentRequest.getCaseId())) + .build(); + + + when(caseService.getCase(AUTHORISATION, documentRequest.getCaseId())).thenReturn(caseDetailsFromCcd); + + + // When + GeneratedDocumentInfo response = dgsService.generateCitizenDocument( + AUTHORISATION, documentRequest, + List.of(TEMPLATE), DocumentCategory.WITNESS_STATEMENTS_RESPONDENT + ).getFirst(); //Then assertNotNull(response); @@ -232,29 +459,27 @@ public void testGenerateCitizenDocument() throws Exception { @Test public void testGenerateCitizenDocumentThrowsFeignException() { generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); when(dgsApiClient.generateDocument(any(),any())).thenThrow(FeignException.class); - assertExpectedException(() -> { - dgsService.generateCitizenDocument(" ", generateAndUploadDocumentRequest, " "); - }, DocumentGenerationException.class, null); + assertExpectedException(() -> dgsService.generateCitizenDocument(AUTHORISATION, generateAndUploadDocumentRequest, TEMPLATE), + DocumentGenerationException.class, null); } @Test public void testGenerateCitizenDocumentCitizenUploadThrowsException() { generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); when(dgsApiClient.generateDocument(any(),any())).thenThrow(FeignException.class); - assertExpectedException(() -> { - dgsService.generateCitizenDocument(" ", documentRequest, " "); - }, DocumentGenerationException.class, null); + assertExpectedException(() -> dgsService.generateCitizenDocument(AUTHORISATION, documentRequest, List.of(TEMPLATE), null), + DocumentGenerationException.class, null); } @@ -263,37 +488,35 @@ public void testToGenerateCoverLetterDocumentThrowsException() { Map dataMap = new HashMap<>(); dataMap.put("coverLetter", "test.pdf"); generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); when(dgsApiClient.generateDocument(any(),any())).thenThrow(FeignException.class); - assertExpectedException(() -> { - dgsService.generateCoverLetterDocument(authToken, dataMap, PRL_DRAFT_TEMPLATE, - "123"); - }, DocumentGenerationException.class, null); + assertExpectedException(() -> dgsService.generateCoverLetterDocument(authToken, dataMap, PRL_DRAFT_TEMPLATE, + CASE_ID + ), DocumentGenerationException.class, null); } @Test public void testToGenerateWelshDocumentThrowsException() { generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); when(dgsApiClient.generateDocument(any(),any())).thenThrow(FeignException.class); - assertExpectedException(() -> { - dgsService.generateWelshDocument(authToken, caseDetails, PRL_DRAFT_TEMPLATE); - }, DocumentGenerationException.class, null); + assertExpectedException(() -> dgsService.generateWelshDocument(authToken, caseDetails, PRL_DRAFT_TEMPLATE), + DocumentGenerationException.class, null); } @Test public void testToGenerateDocumentThrowsException() { generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); @@ -301,24 +524,22 @@ public void testToGenerateDocumentThrowsException() { Mockito.doNothing().when(hearingDataService).populatePartiesAndSolicitorsNames(caseData, dataMap); when(dgsApiClient.generateDocument(any(),any())).thenThrow(FeignException.class); - assertExpectedException(() -> { - dgsService.generateDocument(authToken, caseDetails, PRL_DRAFT_TEMPLATE); - }, DocumentGenerationException.class, null); + assertExpectedException(() -> dgsService.generateDocument(authToken, caseDetails, PRL_DRAFT_TEMPLATE), + DocumentGenerationException.class, null); } @Test - public void testToGenerateDocumentWithCaseDataThrowsRuntimeExcetion() { + public void testToGenerateDocumentWithCaseDataThrowsRuntimeException() { Map respondentDetails = new HashMap<>(); generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); when(dgsApiClient.generateDocument(any(),any())).thenThrow(RuntimeException.class); - assertExpectedException(() -> { - dgsService.generateDocument(authToken, null, PRL_DRAFT_TEMPLATE,respondentDetails); - }, DocumentGenerationException.class, null); + assertExpectedException(() -> dgsService.generateDocument(authToken, null, PRL_DRAFT_TEMPLATE, respondentDetails), + DocumentGenerationException.class, null); } @@ -326,15 +547,14 @@ public void testToGenerateDocumentWithCaseDataThrowsRuntimeExcetion() { public void testToGenerateDocumentWithCaseDataThrowsExcetion() { Map respondentDetails = new HashMap<>(); generatedDocumentInfo = GeneratedDocumentInfo.builder() - .url("TestUrl") + .url(TEST_URL) .binaryUrl("binaryUrl") .hashToken("testHashToken") .build(); when(dgsApiClient.generateDocument(any(),any())).thenThrow(FeignException.class); - assertExpectedException(() -> { - dgsService.generateDocument(authToken, null, PRL_DRAFT_TEMPLATE,respondentDetails); - }, DocumentGenerationException.class, null); + assertExpectedException(() -> dgsService.generateDocument(authToken, null, PRL_DRAFT_TEMPLATE, respondentDetails), + DocumentGenerationException.class, null); } @@ -344,5 +564,30 @@ protected void assertExpectedException(ThrowingRunnable me assertEquals(expectedMessage, exception.getMessage()); } + private void setUpGenerateCitizenDocument() { + generatedDocumentInfo = GeneratedDocumentInfo.builder() + .url(TEST_URL) + .binaryUrl("binaryUrl") + .hashToken("testHashToken") + .build(); + + + doReturn(generatedDocumentInfo).when(dgsApiClient).generateDocument( + Mockito.anyString(), + Mockito.any(GenerateDocumentRequest.class) + ); + } + + private @NonNull ObjectMapper getObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.registerModule(new JavaTimeModule()); + objectMapper.registerModule(new ParameterNamesModule()); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + objectMapper.enable(SerializationFeature.INDENT_OUTPUT); + + ReflectionTestUtils.setField(dgsService, "objectMapper", objectMapper); + return objectMapper; + } } diff --git a/src/test/java/uk/gov/hmcts/reform/prl/services/document/DocumentGenServiceTest.java b/src/test/java/uk/gov/hmcts/reform/prl/services/document/DocumentGenServiceTest.java index dd4bb2ef9af..8349ce67dc1 100644 --- a/src/test/java/uk/gov/hmcts/reform/prl/services/document/DocumentGenServiceTest.java +++ b/src/test/java/uk/gov/hmcts/reform/prl/services/document/DocumentGenServiceTest.java @@ -52,6 +52,7 @@ import uk.gov.hmcts.reform.prl.models.dto.ccd.AllegationOfHarmRevised; import uk.gov.hmcts.reform.prl.models.dto.ccd.CaseData; import uk.gov.hmcts.reform.prl.models.dto.ccd.CaseDetails; +import uk.gov.hmcts.reform.prl.models.dto.citizen.DocumentCategory; import uk.gov.hmcts.reform.prl.models.dto.citizen.DocumentRequest; import uk.gov.hmcts.reform.prl.models.dto.citizen.GenerateAndUploadDocumentRequest; import uk.gov.hmcts.reform.prl.models.language.DocumentLanguage; @@ -432,7 +433,7 @@ public void generateDocsForC100TestFinalDoc() throws Exception { when(organisationService.getRespondentOrganisationDetails(any(CaseData.class))).thenReturn(c100CaseDataFinal); when(allegationOfHarmRevisedService.updateChildAbusesForDocmosis(Mockito.any(CaseData.class))).thenReturn( c100CaseDataFinal); - + c100CaseDataFinal.setState(State.JUDICIAL_REVIEW); Map stringObjectMap = documentGenService.createUpdatedCaseDataWithDocuments(AUTH_TOKEN, c100CaseDataFinal); // Verify that if a case is issued then only C8 documents are updated and not C100 or C1A @@ -471,6 +472,7 @@ public void generateDocsForFL401TestWithOrganisation() throws Exception { when(organisationService.getRespondentOrganisationDetailsForFL401(Mockito.any(CaseData.class))).thenReturn( fl401CaseData); + fl401CaseData.setState(State.SUBMITTED_PAID); Map stringObjectMap = documentGenService.createUpdatedCaseDataWithDocuments(AUTH_TOKEN, fl401CaseData); verifyDocumentsUpdated(stringObjectMap, DOCUMENT_FIELD_C8_WELSH, DOCUMENT_FIELD_FINAL_WELSH, DOCUMENT_FIELD_C8, @@ -1097,8 +1099,12 @@ public void testDocsNullValueWhenWelshNotenglish() throws Exception { .thenReturn(caseData); when(organisationService.getRespondentOrganisationDetailsForFL401(Mockito.any(CaseData.class))) .thenReturn(caseData); + fl401CaseData.setState(State.PREPARE_FOR_HEARING_CONDUCT_HEARING); + // when documentGenService.createUpdatedCaseDataWithDocuments(AUTH_TOKEN, fl401CaseData); + + // then verify(dgsService).generateWelshDocument( Mockito.anyString(), any(CaseDetails.class), @@ -2683,6 +2689,7 @@ protected void assertExpectedException(ThrowingRunnable me @Test public void testGenerateAndUploadDocument() { //Given + ReflectionTestUtils.setField(documentGenService, "prlCitizenUploadTemplate", "FL-PRL-GOR-ENG-Citizen-Uploaded-Statement.docx"); generatedDocumentInfo = GeneratedDocumentInfo.builder() .url("TestUrl") .binaryUrl("binaryUrl") @@ -2700,20 +2707,21 @@ public void testGenerateAndUploadDocument() { .build(); //When - doReturn(generatedDocumentInfo).when(dgsService).generateCitizenDocument( + doReturn(List.of(generatedDocumentInfo)).when(dgsService).generateCitizenDocument( Mockito.anyString(), Mockito.any(DocumentRequest.class), - Mockito.any() + Mockito.any(), + any(DocumentCategory.class) ); when(dateTime.now()).thenReturn(LocalDateTime.now()); //Action - DocumentResponse documentResponse = documentGenService.generateAndUploadDocument(AUTH_TOKEN, documentRequest); + List documentResponses = documentGenService.generateAndUploadDocument(AUTH_TOKEN, documentRequest); //Then - assertNotNull(documentResponse); - assertNotNull(documentResponse.getDocument()); - assertEquals(SUCCESS, documentResponse.getStatus()); + assertNotNull(documentResponses); + assertNotNull(documentResponses.get(0).getDocument()); + assertEquals(SUCCESS, documentResponses.get(0).getStatus()); } @Test