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

DFR-2943 d11 served blank #1754

Merged
merged 16 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
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"
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a new line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Autogenerated file added this, don't think this needs changing?

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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a new line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Autogenerated file added this, don't think this needs changing?

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();
}
}
ptrelease marked this conversation as resolved.
Show resolved Hide resolved
}
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.