import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hubspot.jinjava.Jinjava;
+import com.hubspot.jinjava.JinjavaConfig;
+import com.hubspot.jinjava.interpret.FatalTemplateErrorsException;
/**
* <p>
private final Logger logger = LoggerFactory.getLogger(JinjaTransformationService.class);
- private Jinjava jinjava = new Jinjava();
+ private final JinjavaConfig config = JinjavaConfig.newBuilder().withFailOnUnknownTokens(true).build();
+ private final Jinjava jinjava = new Jinjava(config);
/**
* Transforms the input <code>value</code> by Jinja template.
*/
@Override
public @Nullable String transform(String template, String value) throws TransformationException {
+ String transformationResult;
+ Map<String, @Nullable Object> bindings = new HashMap<>();
+
logger.debug("about to transform '{}' by the function '{}'", value, template);
- Map<String, @Nullable Object> bindings = new HashMap<>();
bindings.put("value", value);
try {
// ok, then value_json is null...
}
- String transformationResult = jinjava.render(template, bindings);
+ try {
+ transformationResult = jinjava.render(template, bindings);
+ } catch (FatalTemplateErrorsException e) {
+ throw new TransformationException("An error occurred while transformation. " + e.getMessage(), e);
+ }
logger.debug("transformation resulted in '{}'", transformationResult);
// Asserts
assertEquals("Hello world!", transformedResponse);
}
+
+ @Test
+ public void testJsonParsingError() throws TransformationException {
+ // when JSON binding parsing failed
+ String transformedResponse = processor.transform("Hello {{ value }}!", "{\"string\"{: \"world\"}");
+
+ // then template should be rendered
+ assertEquals("Hello {\"string\"{: \"world\"}!", transformedResponse);
+ }
+
+ @Test
+ public void testTemplateError() {
+ assertThrows(TransformationException.class,
+ () -> processor.transform("Hello {{{ value_json.string }}!", "{\"string\": \"world\"}"));
+ }
+
+ @Test
+ public void testMissingVariableError() {
+ assertThrows(TransformationException.class,
+ () -> processor.transform("Hello {{ missing }}!", "{\"string\": \"world\"}"));
+ }
+
+ @Test
+ public void testMissingMapKeyError() {
+ assertThrows(TransformationException.class,
+ () -> processor.transform("Hello {{ value_json.missing }}!", "{\"string\": \"world\"}"));
+ }
+
+ @Test
+ public void testMissingVariableIsDefined() throws TransformationException {
+ // when checking missing variable
+ String transformedResponse = processor.transform("{{ missing is defined }}", "{\"string\": \"world\"}");
+
+ // then missing variable is not defined
+ assertEquals("false", transformedResponse);
+ }
+
+ @Test
+ public void testMissingMapKeyIsDefined() throws TransformationException {
+ // when checking missing map key
+ String transformedResponse = processor.transform("{{ value_json.missing is defined }}",
+ "{\"string\": \"world\"}");
+
+ // then missing map key is not defined
+ assertEquals("false", transformedResponse);
+ }
+
+ @Test
+ public void testIsDefined() throws TransformationException {
+ // when checking map key
+ String transformedResponse = processor.transform("{{ value_json.string is defined }}",
+ "{\"string\": \"world\"}");
+
+ // then map key is defined
+ assertEquals("true", transformedResponse);
+ }
}