Skip to content
Open
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 @@ -190,7 +190,15 @@ protected Collection<ValidationResult> customValidate(ValidationContext validati
}
}
} catch (final Exception e) {
String message = String.format("Specification not valid for the selected transformation: %s", e.getMessage());
final String reason;
final Throwable cause = e.getCause();
if (cause == null) {
reason = e.getMessage();
} else {
reason = "%s [%s]".formatted(cause.getMessage(), e.getMessage());
}

final String message = "Specification not valid... %s".formatted(reason);
results.add(new ValidationResult.Builder()
.valid(false)
.subject(JOLT_SPEC.getDisplayName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.joltcommunity.jolt.Diffy;
import io.joltcommunity.jolt.JsonUtils;
import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.flowfile.attributes.CoreAttributes;
import org.apache.nifi.jolt.util.JoltTransformStrategy;
import org.apache.nifi.processor.Processor;
Expand All @@ -40,6 +41,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -109,6 +111,19 @@ void testInvalidJOLTSpec() {
runner.assertValid();
}

@Test
void testNonExistingJOLTSpec() throws IOException {
final Path testResourcesDir = Paths.get("src/test/resources");
final Path nonexistingSpec = testResourcesDir.resolve("nonExistingSpec.json");
runner.setProperty(JoltTransformJSON.JOLT_SPEC, nonexistingSpec.toAbsolutePath().toString());
runner.enqueue(JSON_INPUT);
runner.assertNotValid();

final Collection<ValidationResult> validationResults = runner.validate();
final String explanation = validationResults.iterator().next().getExplanation();
assertTrue(explanation.contains("file"));
}

@Test
void testIncorrectJOLTSpec() {
runner.setProperty(JoltTransformJSON.JOLT_SPEC, chainrSpecContents);
Expand Down Expand Up @@ -211,10 +226,9 @@ void testCustomTransformationWithInvalidClassName() throws IOException {
}

@ParameterizedTest()
@MethodSource("getChainrArguments")
void testTransformInputWithChainr(Path specPath) throws IOException {
final String spec = Files.readString(specPath);
runner.setProperty(JoltTransformJSON.JOLT_SPEC, spec);
@MethodSource("getChainrArgumentContents")
void testTransformInputWithChainrContents(String chainrSpecContents) throws IOException {
runner.setProperty(JoltTransformJSON.JOLT_SPEC, chainrSpecContents);
runner.enqueue(JSON_INPUT);
runner.run();

Expand Down Expand Up @@ -615,9 +629,50 @@ private static Stream<Arguments> unicodeEscaping() {
);
}

private static Stream<Arguments> getChainrArguments() {
private static Stream<Arguments> getChainrArgumentContents() throws IOException {
final String chainrSpecWithLeadingMultLineJavaComment = """
/*
Multiline Java comment
*/
[
{
"operation": "shift",
"spec": {
"rating": {
"primary": {
"value": "Rating",
"max": "RatingRange"
},
"*": {
"max": "SecondaryRatings.&1.Range",
"value": "SecondaryRatings.&1.Value",
"$": "SecondaryRatings.&1.Id"
}
}
}
},
{
"operation": "default",
"spec": {
"Range": 5,
"SecondaryRatings": {
"*": {
"Range": 5
}
}
}
}
]
""";

final Path chainrSpecWithoutCommentsPath = Paths.get(CHAINR_SPEC_PATH);
final String chainrSpecWithoutComments = Files.readString(chainrSpecWithoutCommentsPath);
final Path chainrSpecWithLeadingSingleLineCommentPath = Paths.get("src/test/resources/specs/chainrSpecWithSingleLineComment.json");
final String chainrSpecWithLeadingSingleLineComment = Files.readString(chainrSpecWithLeadingSingleLineCommentPath);

return Stream.of(
Arguments.argumentSet("has no single line comments", Paths.get(CHAINR_SPEC_PATH)),
Arguments.argumentSet("has a single line comment", Paths.get("src/test/resources/specs/chainrSpecWithSingleLineComment.json")));
Arguments.argumentSet("No Java comments", chainrSpecWithoutComments),
Arguments.argumentSet("Leading single line Java comment", chainrSpecWithLeadingSingleLineComment),
Arguments.argumentSet("Leading multi-line Java comment", chainrSpecWithLeadingMultLineJavaComment));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Single line comment
[
// Single line comment
{
"operation": "shift",
"spec": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.nifi.processors.standard;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.nifi.controller.AbstractControllerService;
import org.apache.nifi.json.schema.JsonSchema;
import org.apache.nifi.json.schema.SchemaVersion;
Expand Down Expand Up @@ -252,6 +253,120 @@ void testMultilineJsonWhereSecondLineInvalid(ValidateJson.InputFormat inputForma
runner.clearTransferState();
}

@ParameterizedTest
@MethodSource("schemasWithLeadingComments")
void testSchemaContentWithLeadingComments(String schema) {
runner.setProperty(ValidateJson.SCHEMA_CONTENT, schema);
runner.setProperty(JsonSchemaRegistryComponent.SCHEMA_VERSION, SCHEMA_VERSION);
runner.enqueue(getFileContent("simple-example-with-comments.json"));
runner.assertValid();

final AssertionFailedError assertionFailedError = assertThrows(AssertionFailedError.class, () -> runner.run());
final String stackTrace = ExceptionUtils.getStackTrace(assertionFailedError);
assertTrue(stackTrace.contains("JsonParseException") && !stackTrace.contains("FileNotFoundException"));
}

@ParameterizedTest
@MethodSource("schemasWithEmbeddedComments")
void testSchemaContentWithEmbeddedComments(String schema) {
runner.setProperty(ValidateJson.SCHEMA_CONTENT, schema);
runner.setProperty(JsonSchemaRegistryComponent.SCHEMA_VERSION, SCHEMA_VERSION);
runner.enqueue(getFileContent("simple-example-with-comments.json"));
runner.assertValid();

final AssertionFailedError assertionFailedError = assertThrows(AssertionFailedError.class, () -> runner.run());
final String stackTrace = ExceptionUtils.getStackTrace(assertionFailedError);
assertTrue(stackTrace.contains("JsonParseException"));
}

@ParameterizedTest
@MethodSource("schemasWithTrailingComments")
void testSchemaContentWithTrailingCommentsIgnored(String schema) {
runner.setProperty(ValidateJson.SCHEMA_CONTENT, schema);
runner.setProperty(JsonSchemaRegistryComponent.SCHEMA_VERSION, SCHEMA_VERSION);
runner.enqueue(getFileContent("simple-example-with-comments.json"));
runner.assertValid();

runner.run();

runner.assertAllFlowFilesTransferred(ValidateJson.REL_VALID);
}

private static Stream<Arguments> schemasWithLeadingComments() {
final String schemasWithSingleLineComment = """
// Single line Java comment
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": true
}
""";
final String schemaWithMultiLineComment = """
/*
Multi-line Java comment
*/
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": true
}
""";
return Stream.of(
Arguments.argumentSet("Leading Java single line comment", schemasWithSingleLineComment),
Arguments.argumentSet("Leading Java multi-line comment", schemaWithMultiLineComment)
);
}

private static Stream<Arguments> schemasWithEmbeddedComments() {
final String schemasWithSingleLineComment = """
{
// Single line Java comment
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": true
}
""";
final String schemaWithMultiLineComment = """
{
/*
Multi-line Java comment
*/
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": true
}
""";
return Stream.of(
Arguments.argumentSet("Embedded Java single line comment", schemasWithSingleLineComment),
Arguments.argumentSet("Embedded Java multi-line comment", schemaWithMultiLineComment)
);
}

private static Stream<Arguments> schemasWithTrailingComments() {
final String schemasWithSingleLineComment = """
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": true
}
// Single line Java comment
""";
final String schemaWithMultiLineComment = """
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": true
}
/*
Multi-line Java comment
*/
""";
return Stream.of(
Arguments.argumentSet("Trailing Java single line comment", schemasWithSingleLineComment),
Arguments.argumentSet("Trailing Java multi-line comment", schemaWithMultiLineComment)
);
}

private void assertValidationErrors(Relationship relationship, boolean expected) {
final Map<String, String> attributes = runner.getFlowFilesForRelationship(relationship).getFirst().getAttributes();

Expand Down
Loading