Skip to content

Commit

Permalink
NIFI-5268: Fix JostTransformJSON spec validation with EL
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Villard <[email protected]>

This closes apache#2762.
  • Loading branch information
ijokarumawak authored and pvillard31 committed Jun 6, 2018
1 parent 504152e commit 71cf3fd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,15 @@ protected Collection<ValidationResult> customValidate(ValidationContext validati
}

final String specValue = validationContext.getProperty(JOLT_SPEC).getValue();
final String invalidExpressionMsg = validationContext.newExpressionLanguageCompiler().validateExpression(specValue,true);

if (validationContext.isExpressionLanguagePresent(specValue) && invalidExpressionMsg != null) {
final String customMessage = "The expression language used withing this specification is invalid";
results.add(new ValidationResult.Builder().valid(false)
.explanation(customMessage)
.build());
if (validationContext.isExpressionLanguagePresent(specValue)) {
final String invalidExpressionMsg = validationContext.newExpressionLanguageCompiler().validateExpression(specValue,true);
if (!StringUtils.isEmpty(invalidExpressionMsg)) {
results.add(new ValidationResult.Builder().valid(false)
.subject(JOLT_SPEC.getDisplayName())
.explanation("Invalid Expression Language: " + invalidExpressionMsg)
.build());
}
} else {
//for validation we want to be able to ensure the spec is syntactically correct and not try to resolve variables since they may not exist yet
Object specJson = SORTR.getValue().equals(transform) ? null : JsonUtils.jsonToObject(specValue.replaceAll("\\$\\{","\\\\\\\\\\$\\{"), DEFAULT_CHARSET);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

import org.apache.nifi.flowfile.attributes.CoreAttributes;
Expand Down Expand Up @@ -417,5 +419,32 @@ public void testTransformInputCustomTransformationIgnored() throws IOException {
assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}

@Test
public void testJoltSpecEL() throws IOException {
final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
final String spec = "${joltSpec}";
runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
runner.setProperty(JoltTransformJSON.JOLT_TRANSFORM,JoltTransformJSON.DEFAULTR);
final Map<String, String> attributes = Collections.singletonMap("joltSpec",
"{\"RatingRange\":5,\"rating\":{\"*\":{\"MaxLabel\":\"High\",\"MinLabel\":\"Low\",\"DisplayType\":\"NORMAL\"}}}");
runner.enqueue(JSON_INPUT, attributes);
runner.run();
runner.assertAllFlowFilesTransferred(JoltTransformJSON.REL_SUCCESS);
final MockFlowFile transformed = runner.getFlowFilesForRelationship(JoltTransformJSON.REL_SUCCESS).get(0);
transformed.assertAttributeExists(CoreAttributes.MIME_TYPE.key());
transformed.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(),"application/json");
Object transformedJson = JsonUtils.jsonToObject(new ByteArrayInputStream(transformed.toByteArray()));
Object compareJson = JsonUtils.jsonToObject(Files.newInputStream(Paths.get("src/test/resources/TestJoltTransformJson/defaultrOutput.json")));
assertTrue(DIFFY.diff(compareJson, transformedJson).isEmpty());
}

@Test
public void testJoltSpecInvalidEL() throws IOException {
final TestRunner runner = TestRunners.newTestRunner(new JoltTransformJSON());
final String spec = "${joltSpec:nonExistingFunction()}";
runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
runner.enqueue(JSON_INPUT);
runner.assertNotValid();
}

}

0 comments on commit 71cf3fd

Please sign in to comment.