Skip to content

Commit

Permalink
Merge branch 'main' into VAS-889-feat-add-qrcode-datamatrix-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-cialini authored Apr 12, 2024
2 parents ff69523 + ec8c32a commit 1f204f7
Show file tree
Hide file tree
Showing 20 changed files with 171 additions and 125 deletions.
4 changes: 2 additions & 2 deletions helm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: v2
name: pagopapdfengine
description: Microservice description
type: application
version: 0.102.0
appVersion: 2.6.5
version: 0.105.0
appVersion: 2.8.1
dependencies:
- name: microservice-chart
version: 2.4.0
Expand Down
2 changes: 1 addition & 1 deletion helm/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: ""
image:
repository: ghcr.io/pagopa/pagopa-pdf-engine
tag: "2.6.5"
tag: "2.8.1"
pullPolicy: Always
# https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script.WebHost/Controllers/HostController.cs
livenessProbe:
Expand Down
2 changes: 1 addition & 1 deletion helm/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: ""
image:
repository: ghcr.io/pagopa/pagopa-pdf-engine
tag: "2.6.5"
tag: "2.8.1"
pullPolicy: Always
# https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script.WebHost/Controllers/HostController.cs
livenessProbe:
Expand Down
2 changes: 1 addition & 1 deletion helm/values-uat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ microservice-chart:
fullnameOverride: ""
image:
repository: ghcr.io/pagopa/pagopa-pdf-engine
tag: "2.6.5"
tag: "2.8.1"
pullPolicy: Always
# https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script.WebHost/Controllers/HostController.cs
livenessProbe:
Expand Down
212 changes: 106 additions & 106 deletions node/package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
"axios": "^1.5.0",
"express": "^4.18.2",
"fs-extra": "^11.1.1",
"handlebars": "^4.7.7",
"handlebars-helpers": "^0.10.0",
"hbs-cli": "^1.4.1",
"handlebars": "^4.7.8",
"puppeteer": "^22.4.0",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.0.1",
"puppeteer": "^18.0.2",
Expand Down
1 change: 1 addition & 0 deletions node/pdf-generate/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("generatePdf", () => {

it("should return a pdf when passing valid data", async () => {
const formData = new FormData();
formData.append('title', 'Test Title');
formData.append('data', '{\n' +
'\t\t"transaction": {\n' +
'\t\t\t"id": "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890123456789",\n' +
Expand Down
8 changes: 7 additions & 1 deletion node/pdf-generate/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ const generatePdf = async function (req, res, next) {
page = await browser.newPage();

let data = req.body.data;
let title = req.body.title;

if (title == undefined) {
title = "Documento PDF PagoPA";
}

if (data == undefined) {
res.status(400);
Expand Down Expand Up @@ -101,6 +106,7 @@ const generatePdf = async function (req, res, next) {
await waitForRender(page);
await page.pdf({
path: path.join(workingDir, "pagopa-receipt.pdf"),
title: title,
format: 'A4',
landscape: false,
printBackground: true,
Expand Down Expand Up @@ -155,7 +161,7 @@ const waitForRender = async (page, timeout = 30000) => {
}

lastSize = currentSize;
await page.waitForTimeout(checkInterval);
await new Promise(r => setTimeout(r, checkInterval))
}
};

Expand Down
2 changes: 1 addition & 1 deletion node/pdf-generate/helpers/eq.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var eq = function (a, b, options) {
return a === b ? options.fn(this) : null;
return a === b ? options.fn(this) : options.inverse(this);
};

module.exports = eq;
1 change: 0 additions & 1 deletion node/pdf-generate/utils/browserManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const puppeteer = require('puppeteer');
const registerHelpers = require("handlebars-helpers");
let handlebars = require("handlebars");
let splitAndSpace = require('../helpers/splitAndSpace');
let not = require('../helpers/not')
Expand Down
2 changes: 1 addition & 1 deletion openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition - PDF Engine",
"version": "2.6.5"
"version": "2.8.1"
},
"servers": [
{
Expand Down
2 changes: 1 addition & 1 deletion openapi/openapi_node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition - PDF Engine Node",
"version": "2.6.5"
"version": "2.8.1"
},
"servers": [
{
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>it.gov.pagopa</groupId>
<artifactId>pdf-engine</artifactId>
<version>2.6.5</version>
<version>2.8.1</version>
<packaging>jar</packaging>

<name>pagopa-pdf-engine</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public enum AppErrorCodeEnum {

PDFE_714("PDFE_708", "I/O error when reading generate type from request, and writing it to the output stream"),

PDFE_715("PDFE_715", "I/O error when reading title from request and writing it to the output stream"),

PDFE_896("PDFE_896", "Unexpected field in request body"),
PDFE_897("PDFE_897", "Invalid HTML template, template not provided"),
PDFE_898("PDFE_898", "Invalid document data"),
Expand All @@ -41,8 +43,6 @@ public enum AppErrorCodeEnum {
PDFE_907("PDFE_907", "I/O error on handling the PDF generation result"),
PDFE_908("PDFE_908", "I/O error on creating the working directory");



private final String errorCode;
private final String errorMessage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class GeneratePDFInput {
private boolean applySignature;
private boolean generateZipped;
private ZipFile templateZip;
private String title;

private GeneratorType generatorType = GeneratorType.ITEXT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ public class PdfEngineRequest {

String data;

String workingDirPath;
ZipFile template;
String workingDirPath;

String title;

ZipFile template;


}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public BufferedInputStream generatePDF(GeneratePDFInput generatePDFInput, Path w

PdfEngineClientImpl pdfEngineClient = PdfEngineClientImpl.getInstance();
PdfEngineRequest pdfEngineRequest = new PdfEngineRequest();
pdfEngineRequest.setTitle(generatePDFInput.getTitle() != null ?
generatePDFInput.getTitle() : "Documento PDF PagoPA");
pdfEngineRequest.setWorkingDirPath(workingDirPath.toFile().getAbsolutePath());
pdfEngineRequest.setData(ObjectMapperUtils.writeValueAsString(generatePDFInput.getData()));
pdfEngineRequest.setTemplate(generatePDFInput.getTemplateZip());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public GeneratePDFInput retrieveInputData(byte[] requestBody, Map<String, String
case "data":
generatePDFInput.setData(getDocumentInputData(multipartStream));
break;
case "title":
generatePDFInput.setTitle(getStringField(multipartStream, PDFE_715));
break;
case "applySignature":
generatePDFInput.setApplySignature(getBooleanField(multipartStream, PDFE_708));
break;
Expand Down Expand Up @@ -87,6 +90,17 @@ private boolean getBooleanField(MultipartStream multipartStream, AppErrorCodeEnu
return Boolean.parseBoolean(outputStream.toString());
}

private String getStringField(MultipartStream multipartStream, AppErrorCodeEnum errorCode) throws RequestBodyParseException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
multipartStream.readBodyData(outputStream);
} catch (IOException e) {
throw new RequestBodyParseException(errorCode, errorCode.getErrorMessage(), e);
}
return outputStream.toString();
}



private Map<String,Object> getDocumentInputData(MultipartStream multipartStream) throws RequestBodyParseException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void generatePDFZippedWithSuccess() {
pdfInput.setData(Collections.singletonMap("a", "b"));
pdfInput.setApplySignature(false);
pdfInput.setGenerateZipped(true);
pdfInput.setTitle("title");

PdfEngineResponse pdfEngineResponse = new PdfEngineResponse();
pdfEngineResponse.setStatusCode(200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ void retrieveInputDataSuccess() {
doReturn(
String.format(HEADER_TEMPLATE, "applySignature"),
String.format(HEADER_TEMPLATE, "generateZipped"),
String.format(HEADER_TEMPLATE, "title"),
String.format(HEADER_TEMPLATE, "data")
).when(multipartStreamMock).readHeaders();
doReturn(Collections.singletonMap("ke1", "value1")).when(objectMapperMock).readValue(anyString(), any(TypeReference.class));
doReturn(true, true, false).when(multipartStreamMock).readBoundary();
doReturn(true, true, true, false).when(multipartStreamMock).readBoundary();

GeneratePDFInput result = sut.retrieveInputData(new byte[2], Collections.singletonMap(CONTENT_TYPE_HEADER, CONTENT_TYPE_HEADER_VALUE), workingPath);

Expand Down Expand Up @@ -306,4 +307,23 @@ void retrieveInputDataFailReadBoundaryThrowsMalformedStreamException() {

Assertions.assertEquals(AppErrorCodeEnum.PDFE_710, e.getErrorCode());
}

@Test
@SneakyThrows
void retrieveInputDataFailReadTitleThrowsIOException() {
MultipartStream multipartStreamMock = mock(MultipartStream.class);

doReturn(multipartStreamMock).when(sut).getMultipartStream(any(), anyString());
doReturn(true).when(multipartStreamMock).skipPreamble();
doReturn(String.format(HEADER_TEMPLATE, "title")).when(multipartStreamMock).readHeaders();
doThrow(IOException.class).when(multipartStreamMock).readBodyData(any());

RequestBodyParseException e = assertThrows(
RequestBodyParseException.class,
() -> sut.retrieveInputData(new byte[2], Collections.singletonMap(CONTENT_TYPE_HEADER, CONTENT_TYPE_HEADER_VALUE), workingPath)
);

Assertions.assertEquals(AppErrorCodeEnum.PDFE_715, e.getErrorCode());
}

}

0 comments on commit 1f204f7

Please sign in to comment.