com.github.fge.jsonschema.core.report.ProcessingMessage Java Examples

The following examples show how to use com.github.fge.jsonschema.core.report.ProcessingMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpenApiSchemaValidator.java    From syndesis with Apache License 2.0 6 votes vote down vote up
/**
 * Validates given specification Json and add validation errors and warnings to given open api info model builder.
 * @param specRoot the specification as Json root.
 * @param modelBuilder the model builder receiving all validation errors and warnings.
 */
default void validateJSonSchema(JsonNode specRoot, OpenApiModelInfo.Builder modelBuilder) {
    try {
        final ProcessingReport report = getSchema().validate(specRoot);
        final List<Violation> errors = new ArrayList<>();
        final List<Violation> warnings = new ArrayList<>();
        for (final ProcessingMessage message : report) {
            final boolean added = append(errors, message, Optional.of("error"));
            if (!added) {
                append(warnings, message, Optional.empty());
            }
        }

        modelBuilder.addAllErrors(errors);
        modelBuilder.addAllWarnings(warnings);

    } catch (ProcessingException ex) {
        LoggerFactory.getLogger(OpenApiSchemaValidator.class).error("Unable to load the schema file embedded in the artifact", ex);
        modelBuilder.addError(new Violation.Builder()
            .error("error").property("")
            .message("Unable to load the OpenAPI schema file embedded in the artifact")
            .build());
    }
}
 
Example #2
Source File: OpenApiSchemaValidator.java    From syndesis with Apache License 2.0 6 votes vote down vote up
/**
 * Append violations with level filtering.
 * @param violations the list of violations receiving new entries.
 * @param message the current processing message.
 * @param requiredLevel level of violation message.
 * @return true when violation has been added false when skipped.
 */
default boolean append(final List<Violation> violations, final ProcessingMessage message, final Optional<String> requiredLevel) {
    if (requiredLevel.isPresent()) {
        final Optional<String> level = ofNullable(message.asJson()).flatMap(node -> ofNullable(node.get("level")))
            .flatMap(node -> ofNullable(node.textValue()));

        if (!level.equals(requiredLevel)) {
            return false; // skip
        }
    }

    final Optional<String> property = ofNullable(message.asJson()).flatMap(node -> ofNullable(node.get("instance")))
        .flatMap(node -> ofNullable(node.get("pointer"))).flatMap(node -> ofNullable(node.textValue()));

    final Optional<String> error = ofNullable(message.asJson()).flatMap(node -> ofNullable(node.get("domain")))
        .flatMap(node -> ofNullable(node.textValue()));

    violations.add(new Violation.Builder().error(error.orElse("")).message(message.getMessage()).property(property.orElse("")).build());

    return true;
}
 
Example #3
Source File: JsonSchemaValidator.java    From microcks with Apache License 2.0 6 votes vote down vote up
/**
 * Validate a Json object representing by its text against a schema object representing byt its
 * text too. Validation is a deep one: its pursue checking children nodes on a failed parent. Validation
 * is respectful of Json schema spec semantics regarding additional or unknown attributes: schema must
 * explicitely set <code>additionalProperties</code> to false if you want to consider unknown attributes
 * as validation errors. It returns a list of validation error messages.
 * @param schemaNode The Json schema specification as a Jackson node
 * @param jsonNode The Json object as a Jackson node
 * @return The list of validation failures. If empty, json object is valid !
 * @throws ProcessingException if json node does not represent valid Schema
 */
public static List<String> validateJson(JsonNode schemaNode, JsonNode jsonNode) throws ProcessingException {
   List<String> errors = new ArrayList<>();

   final JsonSchema jsonSchemaNode = extractJsonSchemaNode(schemaNode);

   // Ask for a deep check to get a full error report.
   ProcessingReport report = jsonSchemaNode.validate(jsonNode, true);
   if (!report.isSuccess()) {
      for (ProcessingMessage processingMessage : report) {
         errors.add(processingMessage.getMessage());
      }
   }

   return errors;
}
 
Example #4
Source File: ValidationService.java    From yaml-json-validator-maven-plugin with Apache License 2.0 6 votes vote down vote up
private void validateAgainstSchema(final JsonNode spec, final ValidationResult validationResult) {
    if (schema == null) {
        return;
    }
    try {
        final ProcessingReport report = schema.validate(spec);
        if (!report.isSuccess()) {
            validationResult.encounteredError();
        }
        for (final ProcessingMessage processingMessage : report) {
            validationResult.addMessage(processingMessage.toString());
        }
    } catch (final ProcessingException e) {
        validationResult.addMessage(e.getMessage());
        validationResult.encounteredError();
    }
}
 
Example #5
Source File: AbstractSchemaValidatorTest.java    From elucidate-server with MIT License 5 votes vote down vote up
protected void validateJson(String jsonFileName) throws IOException, ProcessingException, JsonLdError {

        JsonNode schema = getSchema();
        assertNotNull(schema);

        String jsonStr = getJson(jsonFileName);
        assertNotNull(jsonStr);

        Object jsonObj = JsonUtils.fromString(jsonStr);
        List<Object> expandedJson = JsonLdProcessor.expand(jsonObj);
        jsonStr = JsonUtils.toString(expandedJson);
        JsonNode json = JsonLoader.fromString(jsonStr);

        JsonValidator jsonValidator = JsonSchemaFactory.byDefault().getValidator();
        ProcessingReport processingReport = jsonValidator.validate(schema, json);
        assertNotNull(processingReport);
        if (!processingReport.isSuccess()) {

            ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode();
            assertNotNull(jsonArray);

            Iterator<ProcessingMessage> iterator = processingReport.iterator();
            while (iterator.hasNext()) {
                ProcessingMessage processingMessage = iterator.next();
                jsonArray.add(processingMessage.asJson());
            }

            String errorJson = JsonUtils.toPrettyString(jsonArray);
            assertNotNull(errorJson);

            fail(errorJson);
        }
    }
 
Example #6
Source File: Processing.java    From json-schema-validator-demo with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected static JsonNode buildReport(final ProcessingReport report)
{
    final ArrayNode ret = FACTORY.arrayNode();
    for (final ProcessingMessage message: report)
        ret.add(message.asJson());
    return ret;
}
 
Example #7
Source File: JsonPatch.java    From json-schema-validator-demo with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static JsonNode buildReport(final ProcessingReport report)
{
    final ArrayNode ret = JacksonUtils.nodeFactory().arrayNode();
    for (final ProcessingMessage message: report)
        ret.add(message.asJson());
    return ret;
}
 
Example #8
Source File: ErrorProcessor.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns set of {@link SwaggerError} created from a validation report.
 * 
 * @param report
 *            to process
 * @return set of validation errors
 */
public Set<SwaggerError> processReport(ProcessingReport report) {
    final Set<SwaggerError> errors = new HashSet<>();
    if (report != null) {
        for (Iterator<ProcessingMessage> it = report.iterator(); it.hasNext();) {
            errors.addAll(processMessage(it.next()));
        }
    }
    return errors;
}
 
Example #9
Source File: JsonSchemaAssertions.java    From Bastion with GNU General Public License v3.0 5 votes vote down vote up
private void assertResponseConformsToSchema(JsonNode response) throws ProcessingException, IOException {
    ProcessingReport validationReport = JsonSchemaFactory.byDefault()
            .getJsonSchema(getExpectedSchema()).validate(response);
    if (!validationReport.isSuccess()) {
        String messages = StreamSupport.stream(validationReport.spliterator(), false)
                .map(ProcessingMessage::getMessage)
                .collect(Collectors.joining(", "));
        Assert.fail(String.format("Actual response body is not as specified. The following message(s) where produced during validation; %s.", messages));
    }
}
 
Example #10
Source File: DataExportRecipeValidator.java    From TomboloDigitalConnector with MIT License 5 votes vote down vote up
public static void display(ProcessingReport report) {
    String logString = "The recipe file contains errors.\n\n";

    for (ProcessingMessage message : report) {
        logString += message.getMessage() + "\n";
        logString += message.toString() + "\n\n";
    }

    log.error(logString);
}
 
Example #11
Source File: ValidationMatchers.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
protected boolean describeProcessingReport(ProcessingReport report, JsonNode item,
        Description mismatchDescription) throws JsonProcessingException {
    if (!report.isSuccess()) {
        ObjectNode objectNode = JacksonUtils.nodeFactory().objectNode();
        objectNode.set(JSONConstants.INSTANCE, item);
        ArrayNode errors = objectNode.putArray(JSONConstants.ERRORS);
        for (ProcessingMessage m : report) {
            errors.add(m.asJson());
        }
        mismatchDescription.appendText(JacksonUtils.prettyPrint(objectNode));
    }
    return report.isSuccess();
}
 
Example #12
Source File: JSONValidator.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
public String encode(ProcessingReport report, JsonNode instance) {
    ObjectNode objectNode = Json.nodeFactory().objectNode();
    objectNode.set(JSONConstants.INSTANCE, instance);
    ArrayNode errors = objectNode.putArray(JSONConstants.ERRORS);
    for (ProcessingMessage m : report) {
        errors.add(m.asJson());
    }
    return Json.print(objectNode);
}
 
Example #13
Source File: JsonSchemaUtils.java    From TestHub with MIT License 5 votes vote down vote up
/**
 * 将需要验证的JsonNode 与 JsonSchema标准对象 进行比较
 *
 * @param schema schema标准对象
 * @param data   需要比对的Schema对象
 */
private static void assertJsonSchema(JsonNode schema, JsonNode data) {
    ProcessingReport report = JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data);
    if (!report.isSuccess()) {
        for (ProcessingMessage aReport : report) {
            Reporter.log(aReport.getMessage(), true);
        }
    }
    Assert.assertTrue(report.isSuccess());
}
 
Example #14
Source File: AbstractMessageConverter.java    From elucidate-server with MIT License 4 votes vote down vote up
protected String validate(String jsonStr, JsonNode validationSchema) throws ProcessingException, IOException {

        JsonNode json = JsonLoader.fromString(jsonStr);

        JsonValidator jsonValidator = JsonSchemaFactory.byDefault().getValidator();
        ProcessingReport processingReport = jsonValidator.validate(validationSchema, json);
        if (!processingReport.isSuccess()) {

            ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode();

            for (ProcessingMessage processingMessage : processingReport) {
                jsonArray.add(processingMessage.asJson());
            }

            return JsonUtils.toPrettyString(jsonArray);
        }

        return null;
    }
 
Example #15
Source File: OnboardingService.java    From cubeai with Apache License 2.0 4 votes vote down vote up
private Boolean parseMetadata(Solution solution, File dataFile) {
    JSONObject metadataJson;

    try {
        String jsonString = FileUtils.readFileToString(dataFile, "UTF-8");
        // solution.setMetadata(jsonString); // 作废metadata字段,直接从文件中获取

        metadataJson = JSONObject.parseObject(jsonString);

        //==========================================================================================================
        // validate schemaVersion
        String schemafile = null;
        String schemaVersion = metadataJson.get("schema").toString();

        if (schemaVersion.contains("3")) {
            schemafile = "/model-schema/model-schema-0.3.0.json";
        } else if (schemaVersion.contains("4")) {
            schemafile = "/model-schema/model-schema-0.4.0.json";
        } else if (schemaVersion.contains("5")) {
            schemafile = "/model-schema/model-schema-0.5.0.json";
        } else {
            log.error("No matching model schema");
            return false;
        }

        JsonNode schema = JsonLoader.fromResource(schemafile);    // 直接以resources为根目录读取
        JsonNode metadataJson1 = JsonLoader.fromFile(dataFile);
        JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        com.github.fge.jsonschema.main.JsonValidator validator = factory.getValidator();
        ProcessingReport report = validator.validate(schema, metadataJson1);
        if (!report.isSuccess()) {
            StringBuilder sb = new StringBuilder();
            for (ProcessingMessage processingMessage : report) {
                if (!processingMessage.getMessage()
                    .equals("the following keywords are unknown and will be ignored: [self]"))
                    sb.append(processingMessage.getMessage() + "\n");
            }
            log.error("Input JSON is not as per schema cause: '" + sb.toString() + "'");
            return false;
        }
        //==========================================================================================================

        if (metadataJson.containsKey("name")) {
            solution.setName(metadataJson.get("name").toString());
        }

        if (metadataJson.containsKey("modelVersion")) {
            solution.setVersion(metadataJson.get("modelVersion").toString());
        } else {
            solution.setVersion("snapshot");
        }
    } catch (Exception e) {
        return false;
    }

    return true;
}
 
Example #16
Source File: TestMetrics4BearsJsonFile.java    From repairnator with MIT License 4 votes vote down vote up
@Ignore
@Test
public void testRepairnatorJsonFileWithFailingBuild() throws IOException, ProcessingException {
    long buggyBuildCandidateId = 208897371; // https://travis-ci.org/surli/failingProject/builds/208897371

    tmpDir = Files.createTempDirectory("test_repairnator_json_file_failing_build").toFile();

    Build buggyBuildCandidate = this.checkBuildAndReturn(buggyBuildCandidateId, false);

    BuildToBeInspected buildToBeInspected = new BuildToBeInspected(buggyBuildCandidate, null, ScannedBuildStatus.ONLY_FAIL, "test");

    RepairnatorConfig config = RepairnatorConfig.getInstance();
    config.setLauncherMode(LauncherMode.REPAIR);
    config.setRepairTools(new HashSet<>(Arrays.asList("NopolSingleTest")));

    ProjectInspector inspector = new ProjectInspector(buildToBeInspected, tmpDir.getAbsolutePath(), null, null);
    inspector.run();

    // check repairnator.json against schema

    ObjectMapper jsonMapper = new ObjectMapper();
    String workingDir = System.getProperty("user.dir");
    workingDir = workingDir.substring(0, workingDir.lastIndexOf("repairnator/"));
    String jsonSchemaFilePath = workingDir + "resources/repairnator-schema.json";
    File jsonSchemaFile = new File(jsonSchemaFilePath);
    JsonNode schemaObject = jsonMapper.readTree(jsonSchemaFile);

    LoadingConfiguration loadingConfiguration = LoadingConfiguration.newBuilder().dereferencing(Dereferencing.INLINE).freeze();
    JsonSchemaFactory factory = JsonSchemaFactory.newBuilder().setLoadingConfiguration(loadingConfiguration).freeze();

    JsonSchema jsonSchema = factory.getJsonSchema(schemaObject);

    JsonNode repairnatorJsonFile = jsonMapper.readTree(new File(inspector.getRepoToPushLocalPath() + "/repairnator.json"));

    ProcessingReport report = jsonSchema.validate(repairnatorJsonFile);

    String message = "";
    for (ProcessingMessage processingMessage : report) {
        message += processingMessage.toString()+"\n";
    }
    assertTrue(message, report.isSuccess());

    // check correctness of the properties

    File expectedFile = new File(TestMetrics4BearsJsonFile.class.getResource("/json-files/repairnator-208897371.json").getPath());
    String expectedString = FileUtils.readFileToString(expectedFile, StandardCharsets.UTF_8);

    File actualFile = new File(inspector.getRepoToPushLocalPath() + "/repairnator.json");
    String actualString = FileUtils.readFileToString(actualFile, StandardCharsets.UTF_8);

    JSONCompareResult result = JSONCompare.compareJSON(expectedString, actualString, JSONCompareMode.STRICT);
    assertThat(result.isMissingOnField(), is(false));
    assertThat(result.isUnexpectedOnField(), is(false));

    for (FieldComparisonFailure fieldComparisonFailure : result.getFieldFailures()) {
        String fieldComparisonFailureName = fieldComparisonFailure.getField();
        if (fieldComparisonFailureName.equals("tests.failingModule") ||
                fieldComparisonFailureName.equals("reproductionBuggyBuild.projectRootPomPath")) {
            String path = "surli/failingProject/208897371";
            String expected = (String) fieldComparisonFailure.getExpected();
            expected = expected.substring(expected.indexOf(path), expected.length());
            String actual = (String) fieldComparisonFailure.getActual();
            actual = actual.substring(actual.indexOf(path), actual.length());
            assertTrue("Property failing: " + fieldComparisonFailureName,
                    actual.equals(expected));
        } else {
            assertTrue("Property failing: " + fieldComparisonFailureName +
                            "\nexpected: " + fieldComparisonFailure.getExpected() +
                            "\nactual: " + fieldComparisonFailure.getActual(),
                    this.isPropertyToBeIgnored(fieldComparisonFailureName));
        }
    }
}
 
Example #17
Source File: TestMetrics4BearsJsonFile.java    From repairnator with MIT License 4 votes vote down vote up
@Ignore
@Test
public void testBearsJsonFileWithPassingPassingBuilds() throws IOException, ProcessingException {
    long buggyBuildCandidateId = 386337343; // https://travis-ci.org/fermadeiral/test-repairnator-bears/builds/386337343
    long patchedBuildCandidateId = 386348522; // https://travis-ci.org/fermadeiral/test-repairnator-bears/builds/386348522

    tmpDir = Files.createTempDirectory("test_bears_json_file_passing_passing_builds").toFile();

    Build buggyBuildCandidate = this.checkBuildAndReturn(buggyBuildCandidateId, false);
    Build patchedBuildCandidate = this.checkBuildAndReturn(patchedBuildCandidateId, false);

    BuildToBeInspected buildToBeInspected = new BuildToBeInspected(buggyBuildCandidate, patchedBuildCandidate, ScannedBuildStatus.PASSING_AND_PASSING_WITH_TEST_CHANGES, "test");

    RepairnatorConfig config = RepairnatorConfig.getInstance();
    config.setLauncherMode(LauncherMode.BEARS);

    ProjectInspector4Bears inspector = (ProjectInspector4Bears)InspectorFactory.getBearsInspector(buildToBeInspected, tmpDir.getAbsolutePath(), null);
    inspector.run();

    // check bears.json against schema

    ObjectMapper jsonMapper = new ObjectMapper();
    String workingDir = System.getProperty("user.dir");
    workingDir = workingDir.substring(0, workingDir.lastIndexOf("repairnator/"));
    String jsonSchemaFilePath = workingDir + "resources/bears-schema.json";
    File jsonSchemaFile = new File(jsonSchemaFilePath);
    JsonNode schemaObject = jsonMapper.readTree(jsonSchemaFile);

    LoadingConfiguration loadingConfiguration = LoadingConfiguration.newBuilder().dereferencing(Dereferencing.INLINE).freeze();
    JsonSchemaFactory factory = JsonSchemaFactory.newBuilder().setLoadingConfiguration(loadingConfiguration).freeze();

    JsonSchema jsonSchema = factory.getJsonSchema(schemaObject);

    JsonNode bearsJsonFile = jsonMapper.readTree(new File(inspector.getRepoToPushLocalPath() + "/bears.json"));

    ProcessingReport report = jsonSchema.validate(bearsJsonFile);

    String message = "";
    for (ProcessingMessage processingMessage : report) {
        message += processingMessage.toString()+"\n";
    }
    assertTrue(message, report.isSuccess());

    // check correctness of the properties

    File expectedFile = new File(TestMetrics4BearsJsonFile.class.getResource("/json-files/bears-386337343-386348522.json").getPath());
    String expectedString = FileUtils.readFileToString(expectedFile, StandardCharsets.UTF_8);

    File actualFile = new File(inspector.getRepoToPushLocalPath() + "/bears.json");
    String actualString = FileUtils.readFileToString(actualFile, StandardCharsets.UTF_8);

    JSONCompareResult result = JSONCompare.compareJSON(expectedString, actualString, JSONCompareMode.STRICT);
    assertThat(result.isMissingOnField(), is(false));
    assertThat(result.isUnexpectedOnField(), is(false));

    for (FieldComparisonFailure fieldComparisonFailure : result.getFieldFailures()) {
        String fieldComparisonFailureName = fieldComparisonFailure.getField();
        if (fieldComparisonFailureName.equals("tests.failingModule") ||
                fieldComparisonFailureName.equals("reproductionBuggyBuild.projectRootPomPath")) {
            String path = "fermadeiral/test-repairnator-bears/386337343";
            String expected = (String) fieldComparisonFailure.getExpected();
            expected = expected.substring(expected.indexOf(path), expected.length());
            String actual = (String) fieldComparisonFailure.getActual();
            actual = actual.substring(actual.indexOf(path), actual.length());
            assertTrue("Property failing: " + fieldComparisonFailureName,
                    actual.equals(expected));
        } else {
            assertTrue("Property failing: " + fieldComparisonFailureName +
                            "\nexpected: " + fieldComparisonFailure.getExpected() +
                            "\nactual: " + fieldComparisonFailure.getActual(),
                    this.isPropertyToBeIgnored(fieldComparisonFailureName));
        }
    }
}
 
Example #18
Source File: JsonParser.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Override
public void processTuple(byte[] tuple)
{
  if (tuple == null) {
    if (err.isConnected()) {
      err.emit(new KeyValPair<String, String>(null, "null tuple"));
    }
    errorTupleCount++;
    return;
  }
  String incomingString = new String(tuple);
  try {
    if (schema != null) {
      ProcessingReport report = null;
      JsonNode data = JsonLoader.fromString(incomingString);
      report = schema.validate(data);
      if (report != null && !report.isSuccess()) {
        Iterator<ProcessingMessage> iter = report.iterator();
        StringBuilder s = new StringBuilder();
        while (iter.hasNext()) {
          ProcessingMessage pm = iter.next();
          s.append(pm.asJson().get("instance").findValue("pointer")).append(":").append(pm.asJson().get("message"))
              .append(",");
        }
        s.setLength(s.length() - 1);
        errorTupleCount++;
        if (err.isConnected()) {
          err.emit(new KeyValPair<String, String>(incomingString, s.toString()));
        }
        return;
      }
    }
    if (parsedOutput.isConnected()) {
      parsedOutput.emit(new JSONObject(incomingString));
      parsedOutputCount++;
    }
    if (out.isConnected()) {
      out.emit(objMapper.readValue(tuple, clazz));
      emittedObjectCount++;
    }
  } catch (JSONException | ProcessingException | IOException e) {
    errorTupleCount++;
    if (err.isConnected()) {
      err.emit(new KeyValPair<String, String>(incomingString, e.getMessage()));
    }
    logger.error("Failed to parse json tuple {}, Exception = {} ", tuple, e);
  }
}
 
Example #19
Source File: JsonSchemaViolationCause.java    From raml-tester with Apache License 2.0 4 votes vote down vote up
public JsonSchemaViolationCause(ProcessingReport report) {
    messages = new ArrayList<>();
    for (final ProcessingMessage message : report) {
        messages.add(message);
    }
}
 
Example #20
Source File: JsonSchemaViolationCause.java    From raml-tester with Apache License 2.0 4 votes vote down vote up
public List<ProcessingMessage> getMessages() {
    return messages;
}
 
Example #21
Source File: ErrorProcessor.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Returns set of {@link SwaggerError} created from a validation message.
 * 
 * @param message
 *            to process
 * @return set of validation errors
 */
public Set<SwaggerError> processMessage(ProcessingMessage message) {
    return fromNode(message.asJson(), 0);
}