Skip to content

Commit

Permalink
Adds method for PdfValidationResult to format validation error messages
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hermanwh committed Jun 7, 2022
1 parent e946801 commit ad1b7e4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/no/digipost/print/validate/PdfValidationResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ad1b7e4

Please sign in to comment.