Skip to content

Commit

Permalink
DFR-2943 d11 served blank (#1754)
Browse files Browse the repository at this point in the history
* WIP: Flatten pdf document before sending to bulk print

* WIP: Unit test flattening

* Unit test

* Lint

* Streamline

* Add exception to message

* Add exception test

* Updating Terraform Formatting

* Account for PDFs without form layer

---------

Co-authored-by: 58060 <62423932+hmcts-jenkins-d-to-i[bot]@users.noreply.github.com>
Co-authored-by: Paul Trelease <[email protected]>
  • Loading branch information
3 people committed Jul 29, 2024
1 parent 3a6a0a1 commit 07735f7
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 12 deletions.
14 changes: 7 additions & 7 deletions infrastructure/state.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ terraform {
backend "azurerm" {}

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.109.0"
}
random = {
source = "hashicorp/random"
}
azurerm = {
source = "hashicorp/azurerm"
version = "3.109.0"
}
random = {
source = "hashicorp/random"
}
}
}
2 changes: 1 addition & 1 deletion infrastructure/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ variable "product" {}
variable "env" {}

variable "common_tags" {
type = map(string)
type = map(string)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ public List<byte[]> downloadDocuments(BulkPrintRequest bulkPrintRequest, String
log.info("Downloading document for bulk print for Case ID: {}", caseId);

List<byte[]> documents = bulkPrintRequest.getBulkPrintDocuments().stream()
.map(bulkPrintDocument -> service.download(bulkPrintDocument.getBinaryFileUrl(), auth))
.map(bulkPrintDocument -> documentConversionService.flattenPdfDocument(service.download(bulkPrintDocument.getBinaryFileUrl(), auth)))
.toList();
log.info("Download document count for bulk print {} for Case ID: {} ", documents.size(),
caseId);

return documents;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.tika.Tika;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
Expand All @@ -21,6 +24,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -57,6 +61,25 @@ public String getConvertedFilename(String filename) {
return FilenameUtils.getBaseName(filename) + ".pdf";
}

public byte[] flattenPdfDocument(byte[] document) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();

try {
PDDocument doc = PDDocument.load(document);
Optional<PDAcroForm> acroForm = Optional.ofNullable(doc.getDocumentCatalog().getAcroForm());

if (acroForm.isPresent()) {
acroForm.get().flatten();
doc.save(bos);
doc.close();
return bos.toByteArray();
}
} catch (IOException e) {
log.error("Unable to flatten document", e);
}
return document;
}

private byte[] convert(Document sourceDocument, String auth) {
try {
String filename = getConvertedFilename(sourceDocument.getFileName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class BulkPrintDocumentServiceTest {
private static final String DOC_FILE_NAME = "abc.docx";
public static final String AUTH = "auth";
private final byte[] someBytes = "ainhsdcnoih".getBytes();
private final byte[] someFlattenedBytes = "ainhsdcnoih_flattened".getBytes();
@InjectMocks
private BulkPrintDocumentService service;

Expand All @@ -48,6 +49,7 @@ class BulkPrintDocumentServiceTest {
@Test
void downloadDocuments() {
when(evidenceManagementService.download(FILE_URL, AUTH)).thenReturn(someBytes);
when(documentConversionService.flattenPdfDocument(someBytes)).thenReturn(someFlattenedBytes);

BulkPrintRequest bulkPrintRequest = BulkPrintRequest.builder()
.bulkPrintDocuments(singletonList(BulkPrintDocument.builder()
Expand All @@ -57,7 +59,7 @@ void downloadDocuments() {
.build();

List<byte[]> result = service.downloadDocuments(bulkPrintRequest, AUTH);
assertThat(result.get(0), is(equalTo(someBytes)));
assertThat(result.get(0), is(equalTo(someFlattenedBytes)));
}

@Test
Expand All @@ -77,6 +79,7 @@ void validateWordDocumentOnUploadedDocument() {
@Test
void validateEncryptionOnUploadedDocumentWhenInvalidByteSupplied() {
when(evidenceManagementService.download(FILE_BINARY_URL, AUTH)).thenReturn(someBytes);
when(documentConversionService.flattenPdfDocument(someBytes)).thenReturn(someFlattenedBytes);
CaseDocument caseDocument = TestSetUpUtils.caseDocument(FILE_URL, FILE_NAME, FILE_BINARY_URL);
BulkPrintRequest bulkPrintRequest = BulkPrintRequest.builder()
.bulkPrintDocuments(singletonList(BulkPrintDocument.builder()
Expand All @@ -86,7 +89,7 @@ void validateEncryptionOnUploadedDocumentWhenInvalidByteSupplied() {
.build();

List<byte[]> result = service.downloadDocuments(bulkPrintRequest, AUTH);
assertThat(result.get(0), is(equalTo(someBytes)));
assertThat(result.get(0), is(equalTo(someFlattenedBytes)));

List<String> errors = new ArrayList<>();
service.validateEncryptionOnUploadedDocument(caseDocument, "1234", errors, AUTH);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.gov.hmcts.reform.finrem.caseorchestration.service.documentgenerator;


import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -19,6 +18,9 @@
import uk.gov.hmcts.reform.finrem.caseorchestration.service.DocumentConversionService;
import uk.gov.hmcts.reform.finrem.caseorchestration.service.evidencemanagement.EvidenceManagementDownloadService;

import java.io.IOException;
import java.io.InputStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -57,6 +59,42 @@ public void setUp() {
documentToConvert.setBinaryUrl("binaryurl.com");
}

@Test
public void flattenPdfDocument() throws IOException {

// Note: Already flat PDFs are unchanged by flattening (see flattenFlattenedD11.pdf)
String editedPdfFixture = "/fixtures/D11Edited.pdf";
byte[] editedPdfBytes = loadResource(editedPdfFixture);

String flatPdfFixture = "/fixtures/D11Edited-flattened.pdf";
byte[] expectedFlatPdfBytes = loadResource(flatPdfFixture);

byte[] result = documentConversionService.flattenPdfDocument(editedPdfBytes);

assertThat(expectedFlatPdfBytes, is(result));
}

@Test
public void doNotFlattenPdfDocumentWithNoFromLayer() throws IOException {

String editedPdfFixture = "/fixtures/D81_consent_order.pdf";
byte[] pdfBytes = loadResource(editedPdfFixture);
byte[] result = documentConversionService.flattenPdfDocument(pdfBytes);

assertThat(pdfBytes, is(result));
}

@Test
public void flattenNonPdfDocumentHandleException() throws IOException {

String toBeFlattenedFile = "/fixtures/MockD11Word.docx";
byte[] toBeFlattenedbytes = loadResource(toBeFlattenedFile);

byte[] result = documentConversionService.flattenPdfDocument(toBeFlattenedbytes);

assertThat(toBeFlattenedbytes, is(result));
}

@Test
public void convertWordToPdf() {
mockServer.expect(requestTo(PDF_SERVICE_URI))
Expand Down Expand Up @@ -91,4 +129,12 @@ public void getConvertedFilename() {
assertThat(documentConversionService.getConvertedFilename("nodot"), is("nodot.pdf"));
assertThat(documentConversionService.getConvertedFilename("word.docx"), is("word.pdf"));
}

private byte[] loadResource(String testPdf) throws IOException {

try (InputStream resourceAsStream = getClass().getResourceAsStream(testPdf)) {
assert resourceAsStream != null;
return resourceAsStream.readAllBytes();
}
}
}
Binary file not shown.
Binary file added src/test/resources/fixtures/D11Edited.pdf
Binary file not shown.
Binary file added src/test/resources/fixtures/D81_consent_order.pdf
Binary file not shown.
Binary file added src/test/resources/fixtures/MockD11Word.docx
Binary file not shown.
Binary file not shown.

0 comments on commit 07735f7

Please sign in to comment.