Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -62,7 +63,7 @@ public List<Attachment> getAttachments() {
}

public void process() {
LOG.debug("process:: Started processsing");
LOG.debug("process:: Started processing");
enrichContextWithDateTimes();
formatDatesInContext(context, config.getLanguageTag(), config.getTimeZoneId());
handleBarcodeImageTokens();
Expand All @@ -84,7 +85,7 @@ void handleBarcodeImageTokens() {
Set<String> 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))
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<img src='cid:barcode_987654321' alt='barcode_987654321'>"))
.put("user", new JsonObject()
.put("barcode", "22222"));

String expectedAttachmentContentId = "<barcode_987654321>";
String expectedAttachmentName = "barcode_987654321";
String expectedAttachmentDisposition = "inline";
String expectedAttachmentContentType = "image/png";

TemplateContextPreProcessor processor = new TemplateContextPreProcessor(template, inputJson, null);
processor.handleBarcodeImageTokens();

assertEquals(expectedJson, inputJson);

List<Attachment> 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()
Expand Down