From ad1b7e4b4fd6901dfd281d75eb7da67de1efea72 Mon Sep 17 00:00:00 2001 From: Herman Wika Horn Date: Thu, 26 May 2022 21:02:17 +0200 Subject: [PATCH] Adds method for PdfValidationResult to format validation error messages PdfValiationErrors of type UNSUPPORTED_DIMENSIONS depend on the parent PdfValidationResult object to determine the appropriate string formating values. This formating is already performed in the toString()-metoden, however use of this method is not advisable in all contexts. The new implemented method allows for formating the PdfValidationError using the appropriate bleed values --- README.md | 7 ++++++ pom.xml | 6 +++++ .../print/validate/PdfValidationResult.java | 23 +++++++++++-------- .../print/validate/PrintPdfValidatorTest.java | 17 +++++++++++++- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 6dfb181..b7fa5a8 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,13 @@ Krav til PDF dokumenter er tilgjengelig på https://www.digipost.no/plattform/an Biblioteket er også tilgjengelig på [Maven Central](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22printability-validator%22). +Feil ved validering av dimensjoner medfører `PdfValidationError` av typen `UNSUPPORTED_DIMENSIONS`. +Den tilhørende meldingen inneholder variable parametre for margin og bleed. Fra og med versjon 3.3 +tilbyr `PdfValidationResult` en `formattedValidationErrorMessage`-metode for å kunne formatere denne +feilmeldingen med riktig verdier. Denne formateringen skjedde tidligere kun i `PdfValidationResult` +sin `toString`-metode. Den nevnte hjelpemetoden kan benyttes i sammenhenger det er er uønsket å +benytte `toString`. + ## For avsendere som sender til utskrift via offentlig Sikker Digital Post (SDP) meldingsformidlertjeneste. I SDP utskriftstjenesten er det satt opp en felles valideringskonfigurasjon. Den er som følger: diff --git a/pom.xml b/pom.xml index 04bd26a..b9730de 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,12 @@ 4.13.1 test + + org.hamcrest + hamcrest-core + 1.3 + test + org.hamcrest hamcrest-library diff --git a/src/main/java/no/digipost/print/validate/PdfValidationResult.java b/src/main/java/no/digipost/print/validate/PdfValidationResult.java index b81f72d..1b3dd6b 100644 --- a/src/main/java/no/digipost/print/validate/PdfValidationResult.java +++ b/src/main/java/no/digipost/print/validate/PdfValidationResult.java @@ -45,7 +45,19 @@ public boolean hasErrors() { return !errors.isEmpty(); } - + public String formattedValidationErrorMessage(PdfValidationError validationError) { + if (validationError == PdfValidationError.UNSUPPORTED_DIMENSIONS) { + return String.format( + PdfValidationError.UNSUPPORTED_DIMENSIONS.toString(), + PdfValidator.A4_WIDTH_MM - bleed.negativeBleedInMM, + PdfValidator.A4_WIDTH_MM + bleed.positiveBleedInMM, + PdfValidator.A4_HEIGHT_MM - bleed.negativeBleedInMM, + PdfValidator.A4_HEIGHT_MM + bleed.positiveBleedInMM + ); + } else { + return validationError.toString(); + } + } private String toStringValue; @@ -55,14 +67,7 @@ public String toString() { StringBuilder sb = new StringBuilder("["); sb.append(getClass().getSimpleName()); for (PdfValidationError printPdfValideringsFeil : errors) { - final String err; - if(printPdfValideringsFeil == PdfValidationError.UNSUPPORTED_DIMENSIONS) { - err = String.format(PdfValidationError.UNSUPPORTED_DIMENSIONS.toString(), PdfValidator.A4_WIDTH_MM - bleed.negativeBleedInMM, - PdfValidator.A4_WIDTH_MM + bleed.positiveBleedInMM, PdfValidator.A4_HEIGHT_MM - bleed.negativeBleedInMM, PdfValidator.A4_HEIGHT_MM + bleed.positiveBleedInMM); - } else { - err = printPdfValideringsFeil.toString(); - } - + final String err = formattedValidationErrorMessage(printPdfValideringsFeil); sb.append(" "); sb.append(err); } diff --git a/src/test/java/no/digipost/print/validate/PrintPdfValidatorTest.java b/src/test/java/no/digipost/print/validate/PrintPdfValidatorTest.java index 12489d9..6c821fa 100644 --- a/src/test/java/no/digipost/print/validate/PrintPdfValidatorTest.java +++ b/src/test/java/no/digipost/print/validate/PrintPdfValidatorTest.java @@ -33,12 +33,13 @@ import static no.digipost.print.validate.PdfValidationError.TOO_MANY_PAGES_FOR_AUTOMATED_PRINT; import static no.digipost.print.validate.PdfValidationError.UNSUPPORTED_DIMENSIONS; import static no.digipost.print.validate.PdfValidationSettings.CHECK_ALL; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.everyItem; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; public class PrintPdfValidatorTest { @@ -89,6 +90,20 @@ public void failedPdfWithInsufficientMarginForPrintGivesPersonalizedBleedParamet PdfValidator.A4_WIDTH_MM - bleed.negativeBleedInMM, PdfValidator.A4_WIDTH_MM + bleed.positiveBleedInMM, PdfValidator.A4_HEIGHT_MM - bleed.negativeBleedInMM, PdfValidator.A4_HEIGHT_MM + bleed.positiveBleedInMM) + "]")); } + @Test + public void errorMessageMethodReturnsPersonalizedBleedParameters() { + Bleed bleed = new Bleed(2, 3); + PdfValidationResult pdfValidationResult = new PdfValidationResult(validationErrors("/pdf/far-from-a4-free-barcode-area.pdf", new PdfValidationSettings(false, false, false, false, bleed.positiveBleedInMM, bleed.negativeBleedInMM)), 1, bleed); + + PdfValidationError error = pdfValidationResult.errors.stream() + .filter(UNSUPPORTED_DIMENSIONS::equals) + .findAny().orElseThrow(() -> new RuntimeException("Expected unsupported dimensions validation error")); + + String errorMessage = pdfValidationResult.formattedValidationErrorMessage(error); + assertThat(errorMessage, containsString(String.format("%d—%d", PdfValidator.A4_WIDTH_MM - bleed.negativeBleedInMM, PdfValidator.A4_WIDTH_MM + bleed.positiveBleedInMM))); + assertThat(errorMessage, containsString(String.format("%d—%d", PdfValidator.A4_HEIGHT_MM - bleed.negativeBleedInMM, PdfValidator.A4_HEIGHT_MM + bleed.positiveBleedInMM))); + } + @Test public void doesNotFailPdfWithInsufficientMarginForPrintIfCheckDisabled() { PdfValidationSettings innstillinger = new PdfValidationSettings(false, true, true, true);