Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ret 2783 Welsh translation content - Generate Welsh pdf claim form #191

Merged
merged 7 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void setUpBeforeEach() {
@DisplayName("Should get single case details")
@Test
void caseDetailsEndpoint() throws Exception {
when(ccdApiClient.getCase(any(),any(),any())).thenReturn(caseDetailsResponse);
when(ccdApiClient.getCase(any(), any(), any())).thenReturn(caseDetailsResponse);
CaseRequest caseRequest = CaseRequest.builder()
.caseId("1646").build();

Expand All @@ -100,7 +100,7 @@ void caseDetailsEndpoint() throws Exception {
@DisplayName("Should get all case details list by user")
@Test
void returnCasesByUserEndpoint() throws Exception {
when(ccdApiClient.searchForCitizen(any(),any(),any(),any(),any(),any()))
when(ccdApiClient.searchForCitizen(any(), any(), any(), any(), any(), any()))
.thenReturn(Collections.singletonList(caseDetailsResponse));

mockMvc.perform(get("/cases/user-cases").header(HttpHeaders.AUTHORIZATION, AUTH_TOKEN))
Expand All @@ -114,17 +114,17 @@ void createCaseEndpoint() throws Exception {
CaseRequest caseRequest = CaseRequest.builder()
.build();

when(ccdApiClient.startForCitizen(any(),any(),any(),any(),any(),any()))
when(ccdApiClient.startForCitizen(any(), any(), any(), any(), any(), any()))
.thenReturn(startEventResponse);

when(ccdApiClient.submitForCitizen(any(),any(),any(),any(),any(),anyBoolean(),any()))
when(ccdApiClient.submitForCitizen(any(), any(), any(), any(), any(), anyBoolean(), any()))
.thenReturn(caseDetailsResponse);

mockMvc.perform(
post("/cases/initiate-case")
.header(HttpHeaders.AUTHORIZATION, AUTH_TOKEN)
.contentType(MediaType.APPLICATION_JSON)
.content(resourceLoader.toJson(caseRequest)))
post("/cases/initiate-case")
.header(HttpHeaders.AUTHORIZATION, AUTH_TOKEN)
.contentType(MediaType.APPLICATION_JSON)
.content(resourceLoader.toJson(caseRequest)))
.andExpect(status().isOk())
.andExpect(content().json(getSerialisedMessage(CASE_DETAILS_JSON)));
}
Expand All @@ -137,9 +137,9 @@ void updateDraftCaseEndpoint() throws Exception {
.caseId("12")
.build();

when(ccdApiClient.startEventForCitizen(any(),any(),any(),any(),any(),any(),any()))
when(ccdApiClient.startEventForCitizen(any(), any(), any(), any(), any(), any(), any()))
.thenReturn(startEventResponse);
when(ccdApiClient.submitEventForCitizen(any(),any(),any(),any(),any(),any(),anyBoolean(),any()))
when(ccdApiClient.submitEventForCitizen(any(), any(), any(), any(), any(), any(), anyBoolean(), any()))
.thenReturn(caseDetailsResponse);

mockMvc.perform(
Expand All @@ -159,9 +159,9 @@ void submitCaseEndpoint() throws Exception {
.caseId("12")
.build();

when(ccdApiClient.startEventForCitizen(any(),any(),any(),any(),any(),any(),any()))
when(ccdApiClient.startEventForCitizen(any(), any(), any(), any(), any(), any(), any()))
.thenReturn(startEventResponse);
when(ccdApiClient.submitEventForCitizen(any(),any(),any(),any(),any(),any(),anyBoolean(),any()))
when(ccdApiClient.submitEventForCitizen(any(), any(), any(), any(), any(), any(), anyBoolean(), any()))
.thenReturn(caseDetailsResponse);

mockMvc.perform(
Expand All @@ -181,9 +181,9 @@ void updateSubmittedCaseEndpoint() throws Exception {
.caseId("12")
.build();

when(ccdApiClient.startEventForCitizen(any(),any(),any(),any(),any(),any(),any()))
when(ccdApiClient.startEventForCitizen(any(), any(), any(), any(), any(), any(), any()))
.thenReturn(startEventResponse);
when(ccdApiClient.submitEventForCitizen(any(),any(),any(),any(),any(),any(),anyBoolean(),any()))
when(ccdApiClient.submitEventForCitizen(any(), any(), any(), any(), any(), any(), anyBoolean(), any()))
.thenReturn(caseDetailsResponse);

mockMvc.perform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,19 @@ public Boolean isEmpty() {

public List<DocumentTypeItem> uploadAllDocuments(String authToken,
String caseType,
PdfDecodedMultipartFile pdfDecodedMultipartFile,
List<PdfDecodedMultipartFile> pdfDecodedMultipartFiles,
List<PdfDecodedMultipartFile> acasCertificates)
throws CaseDocumentException {
List<DocumentTypeItem> documentTypeItems = new ArrayList<>();
if (pdfDecodedMultipartFile != null) {
documentTypeItems.add(createDocumentTypeItem(
authToken,
caseType,
TYPE_OF_DOCUMENT_ET1_CASE_PDF,
pdfDecodedMultipartFile
));
if (pdfDecodedMultipartFiles != null) {
for (PdfDecodedMultipartFile casePdf : pdfDecodedMultipartFiles) {
documentTypeItems.add(createDocumentTypeItem(
authToken,
caseType,
TYPE_OF_DOCUMENT_ET1_CASE_PDF,
casePdf
));
}
}
if (acasCertificates != null) {
for (PdfDecodedMultipartFile acasCertificate : acasCertificates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ public CaseDetails submitCase(String authorization, CaseRequest caseRequest)
log.error("Invalid ACAS numbers", e);
}

PdfDecodedMultipartFile casePdfFile =
List<PdfDecodedMultipartFile> casePdfFiles =
pdfService.convertCaseDataToPdfDecodedMultipartFile(caseData);
caseDetails.getData().put("ClaimantPcqId", caseData.getClaimantPcqId());
caseDetails.getData().put("documentCollection",
caseDocumentService
.uploadAllDocuments(authorization,
caseRequest.getCaseTypeId(),
casePdfFile,
casePdfFiles,
acasCertificates));
String emailTemplateId = WELSH_LANGUAGE.equals(caseData.getClaimantType().getClaimantContactLanguage())
? notificationsProperties.getCySubmitCaseEmailTemplateId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
Expand All @@ -31,9 +30,14 @@
@RequiredArgsConstructor()
public class PdfService {

private static final String ENGLISH = "English";
private static final String WELSH = "Welsh";

private final PdfMapperService pdfMapperService;
@Value("${pdf.source}")
public String pdfTemplateSource;
@Value("${pdf.english}")
public String englishPdfTemplateSource;
@Value("${pdf.welsh}")
public String welshPdfTemplateSource;

private static final String PDF_FILE_TIKA_CONTENT_TYPE = "application/pdf";
private static final String NOT_FOUND = "not found";
Expand All @@ -42,23 +46,24 @@ public class PdfService {
* Converts a {@link CaseData} class object into a pdf document
* using template (ver. ET1_0922)
* @param caseData The data that is to be converted into pdf
* @param pdfSource The source location of the PDF file to be used as the template
* @return a byte array that contains the pdf document.
*/
public byte[] convertCaseToPdf(CaseData caseData) throws PdfServiceException {
public byte[] convertCaseToPdf(CaseData caseData, String pdfSource) throws PdfServiceException {
byte[] pdfDocumentBytes;
try {
pdfDocumentBytes = createPdf(caseData);
pdfDocumentBytes = createPdf(caseData, pdfSource);
} catch (IOException ex) {
throw new PdfServiceException("Failed to convert to PDF", ex);
}
return pdfDocumentBytes;
}

protected byte[] createPdf(CaseData caseData) throws IOException {
protected byte[] createPdf(CaseData caseData, String pdfSource) throws IOException {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try (InputStream is = cl.getResourceAsStream(this.pdfTemplateSource);
PDDocument pdfDocument = Loader.loadPDF(
Objects.requireNonNull(is))) {

try (PDDocument pdfDocument = Loader.loadPDF(
Objects.requireNonNull(cl.getResourceAsStream(pdfSource)))) {
PDDocumentCatalog pdDocumentCatalog = pdfDocument.getDocumentCatalog();
PDAcroForm pdfForm = pdDocumentCatalog.getAcroForm();
for (Map.Entry<String, Optional<String>> entry : this.pdfMapperService.mapHeadersToPdf(caseData)
Expand All @@ -80,11 +85,12 @@ protected byte[] createPdf(CaseData caseData) throws IOException {
}
}

private static String createPdfDocumentNameFromCaseData(CaseData caseData) {
private static String createPdfDocumentNameFromCaseData(CaseData caseData, String documentLanguage) {
return "ET1_CASE_DOCUMENT_"
+ caseData.getClaimantIndType().getClaimantFirstNames().replace(" ", "_")
+ "_"
+ caseData.getClaimantIndType().getClaimantLastName().replace(" ", "_")
+ (ENGLISH.equals(documentLanguage) ? "" : "_" + documentLanguage)
+ ".pdf";
}

Expand Down Expand Up @@ -116,13 +122,22 @@ private static String createPdfDocumentDescriptionFromCaseDataAndAcasCertificate
+ acasCertificate.getCertificateNumber();
}

public PdfDecodedMultipartFile convertCaseDataToPdfDecodedMultipartFile(CaseData caseData)
public List<PdfDecodedMultipartFile> convertCaseDataToPdfDecodedMultipartFile(CaseData caseData)
throws PdfServiceException {
byte[] pdfData = convertCaseToPdf(caseData);
return new PdfDecodedMultipartFile(pdfData,
createPdfDocumentNameFromCaseData(caseData),
PDF_FILE_TIKA_CONTENT_TYPE,
createPdfDocumentDescriptionFromCaseData(caseData));
List<PdfDecodedMultipartFile> files = new ArrayList<>();
files.add(new PdfDecodedMultipartFile(convertCaseToPdf(caseData, this.englishPdfTemplateSource),
createPdfDocumentNameFromCaseData(caseData, ENGLISH),
PDF_FILE_TIKA_CONTENT_TYPE,
createPdfDocumentDescriptionFromCaseData(caseData)));

if (WELSH.equals(caseData.getClaimantType().getClaimantContactLanguage())) {
files.add(new PdfDecodedMultipartFile(convertCaseToPdf(caseData, this.welshPdfTemplateSource),
createPdfDocumentNameFromCaseData(caseData, WELSH),
PDF_FILE_TIKA_CONTENT_TYPE,
createPdfDocumentDescriptionFromCaseData(caseData)));
}

return files;
}

public List<PdfDecodedMultipartFile> convertAcasCertificatesToPdfDecodedMultipartFiles(
Expand Down
Binary file added src/main/resources/CY_ET1_0922.pdf
Binary file not shown.
3 changes: 2 additions & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ notifications:


pdf:
source: "ET1_0922.pdf"
english: "ET1_0922.pdf"
welsh: "CY_ET1_0922.pdf"
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@ class CaseDocumentServiceTest {
MOCK_FILE_BODY.getBytes()
);
private static final PdfDecodedMultipartFile MOCK_PDF_DECODED_MULTIPART_FILE = new PdfDecodedMultipartFile(
MOCK_FILE_BODY.getBytes(), "Test OriginalFileName.pdf", "text/plain",
MOCK_FILE_BODY.getBytes(), "Test OriginalFileName.pdf", "text/plain",
"Test Document Description"
);

private static final List<PdfDecodedMultipartFile> MOCK_PDF_DECODED_MULTIPART_FILE_LIST =
new ArrayList<PdfDecodedMultipartFile>() {{
add(MOCK_PDF_DECODED_MULTIPART_FILE);
}
};

private static final MockMultipartFile MOCK_FILE_INVALID_NAME = new MockMultipartFile(
"mock_file_with_invalid_name",
Expand Down Expand Up @@ -131,7 +135,8 @@ void setup() {
AuthTokenGenerator authTokenGenerator = () -> SERVICE_AUTH;
caseDocumentService = new CaseDocumentService(restTemplate,
authTokenGenerator,
DOCUMENT_SERVICE_API_URL, 3);
DOCUMENT_SERVICE_API_URL, 3
);
mockServer = MockRestServiceServer.createServer(restTemplate);
}

Expand Down Expand Up @@ -198,10 +203,12 @@ void theUploadDocWhenRestTemplateFailsProducesDocException() {
void theUploadDocWhenIoExceptionProducesDocException() throws IOException {
IOException ioException = new IOException("Test throw");

MockMultipartFile mockMultipartFileSpy = Mockito.spy(new MockMultipartFile("mock_file_spy",
DOCUMENT_NAME,
MediaType.TEXT_PLAIN_VALUE,
"Hello, World!".getBytes()));
MockMultipartFile mockMultipartFileSpy = Mockito.spy(new MockMultipartFile(
"mock_file_spy",
DOCUMENT_NAME,
MediaType.TEXT_PLAIN_VALUE,
"Hello, World!".getBytes()
));

doThrow(ioException).when(mockMultipartFileSpy).getBytes();

Expand Down Expand Up @@ -422,8 +429,9 @@ void shouldUploadAllDocuments() throws CaseDocumentException {
.body(MOCK_RESPONSE_WITH_DOCUMENT));

List<PdfDecodedMultipartFile> acasCertificates = new ArrayList<>();
caseDocumentService.uploadAllDocuments(MOCK_TOKEN, CASE_TYPE, MOCK_PDF_DECODED_MULTIPART_FILE,
acasCertificates);
caseDocumentService.uploadAllDocuments(MOCK_TOKEN, CASE_TYPE, MOCK_PDF_DECODED_MULTIPART_FILE_LIST,
acasCertificates
);
}

@Test
Expand All @@ -437,6 +445,7 @@ void shouldUploadAllDocumentsWhenAcasCertificatesExists() throws CaseDocumentExc
List<PdfDecodedMultipartFile> acasCertificates = new ArrayList<>();
acasCertificates.add(MOCK_PDF_DECODED_MULTIPART_FILE);
caseDocumentService.uploadAllDocuments(MOCK_TOKEN, CASE_TYPE, null,
acasCertificates);
acasCertificates
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import uk.gov.hmcts.reform.ccd.client.model.CaseDetails;
import uk.gov.hmcts.reform.et.syaapi.constants.EtSyaConstants;
import uk.gov.hmcts.reform.et.syaapi.model.TestData;
import uk.gov.hmcts.reform.et.syaapi.service.pdf.PdfDecodedMultipartFile;
import uk.gov.hmcts.reform.et.syaapi.service.pdf.PdfServiceException;
import uk.gov.hmcts.reform.idam.client.IdamApi;
import uk.gov.hmcts.reform.idam.client.IdamClient;
Expand Down Expand Up @@ -95,7 +94,7 @@ void beforeEach() throws CaseDocumentException {
when(caseDocumentService.uploadAllDocuments(
eq(TEST_SERVICE_AUTH_TOKEN),
eq(testData.getCaseRequest().getCaseTypeId()),
any(PdfDecodedMultipartFile.class),
anyList(),
anyList()
)).thenReturn(testData.getUploadDocumentResponse());
when(notificationService.sendSubmitCaseConfirmationEmail(
Expand Down
Loading