diff --git a/src/main/java/org/folio/template/util/TemplateContextPreProcessor.java b/src/main/java/org/folio/template/util/TemplateContextPreProcessor.java index 8f0cb63..a562493 100644 --- a/src/main/java/org/folio/template/util/TemplateContextPreProcessor.java +++ b/src/main/java/org/folio/template/util/TemplateContextPreProcessor.java @@ -36,6 +36,7 @@ public class TemplateContextPreProcessor { private static final String SUFFIX_DATE = "Date"; private static final String SUFFIX_TIME = "Time"; private static final String SUFFIX_BARCODE = ".barcode"; + private static final String SUFFIX_HRID = "Hrid"; private static final String SUFFIX_IMAGE = "Image"; private final LocalizedTemplatesProperty template; @@ -62,7 +63,7 @@ public List getAttachments() { } public void process() { - LOG.debug("process:: Started processsing"); + LOG.debug("process:: Started processing"); enrichContextWithDateTimes(); formatDatesInContext(context, config.getLanguageTag(), config.getTimeZoneId()); handleBarcodeImageTokens(); @@ -84,7 +85,7 @@ void handleBarcodeImageTokens() { Set newTokens = new HashSet<>(); contextMap.entrySet().stream() - .filter(e -> e.getKey().endsWith(SUFFIX_BARCODE)) + .filter(e -> isSuitableImageSource(e.getKey())) .filter(e -> objectIsNonBlankString(e.getValue())) .map(e -> new Token(e.getKey(), (String) e.getValue())) .filter(token -> templateTokens.contains(token.shortPath() + SUFFIX_IMAGE)) @@ -103,6 +104,10 @@ void handleBarcodeImageTokens() { fixTokensWithHtmlValue(newTokens); } + private boolean isSuitableImageSource(String tokenKey) { + return tokenKey.endsWith(SUFFIX_BARCODE) || tokenKey.endsWith(SUFFIX_HRID); + } + private boolean objectIsNonBlankString(Object obj) { return obj instanceof String && StringUtils.isNoneBlank((String) obj); diff --git a/src/test/java/org/folio/template/util/TemplateContextPreProcessorTest.java b/src/test/java/org/folio/template/util/TemplateContextPreProcessorTest.java index 68b6eef..1ac0647 100644 --- a/src/test/java/org/folio/template/util/TemplateContextPreProcessorTest.java +++ b/src/test/java/org/folio/template/util/TemplateContextPreProcessorTest.java @@ -105,6 +105,55 @@ void barcodeImageTokensAndAttachmentsAreCreated() { Base64.getDecoder().decode(attachment.getData()); } + @Test + void hridBarcodeImageTokensAndAttachmentsAreCreated() { + LocalizedTemplatesProperty template = new LocalizedTemplatesProperty() + .withHeader("Instance HRID: {{item.instanceHrid}}") + .withBody("HRID barcode image: {{item.instanceHridImage}}"); + + JsonObject inputJson = new JsonObject() + .put("barcode", "00000") + .put("item", new JsonObject() + .put("name", "tester") + .put("instanceHrid", "987654321") // valid, HRID barcodeImage is present in template + .put("foobarcode", "22222") + .put("barcoder", "33333")) + .put("user", new JsonObject() + .put("barcode", "22222")); // valid, but barcodeImage is NOT present in template + + JsonObject expectedJson = new JsonObject() + .put("barcode", "00000") + .put("item", new JsonObject() + .put("name", "tester") + .put("instanceHrid", "987654321") + .put("foobarcode", "22222") + .put("barcoder", "33333") + .put("instanceHridImage", "barcode_987654321")) + .put("user", new JsonObject() + .put("barcode", "22222")); + + String expectedAttachmentContentId = ""; + String expectedAttachmentName = "barcode_987654321"; + String expectedAttachmentDisposition = "inline"; + String expectedAttachmentContentType = "image/png"; + + TemplateContextPreProcessor processor = new TemplateContextPreProcessor(template, inputJson, null); + processor.handleBarcodeImageTokens(); + + assertEquals(expectedJson, inputJson); + + List attachments = processor.getAttachments(); + assertEquals(1, attachments.size()); + + Attachment attachment = attachments.get(0); + assertEquals(attachment.getContentId(), expectedAttachmentContentId); + assertEquals(attachment.getContentType(), expectedAttachmentContentType); + assertEquals(attachment.getDisposition(), expectedAttachmentDisposition); + assertEquals(attachment.getName(), expectedAttachmentName); + assertTrue(StringUtils.isNotBlank(attachment.getData())); + Base64.getDecoder().decode(attachment.getData()); + } + @Test void duplicateTokensDoNotProduceDuplicateAttachments() { LocalizedTemplatesProperty template = new LocalizedTemplatesProperty()