Search in sources :

Example 11 with JsonSchema

use of com.github.fge.jsonschema.main.JsonSchema in project records-management by Alfresco.

the class BaseYamlUnitTest method getSwaggerSchema.

/**
 * Helper method to read in the Swagger JSON schema file
 */
private JsonSchema getSwaggerSchema(final String schemaLocation) throws IOException, ProcessingException {
    JsonSchema swaggerSchema = null;
    final InputStream in = this.getClass().getResourceAsStream(schemaLocation);
    if (in != null) {
        final String swaggerSchemaAsString = IOUtils.toString(in);
        final JsonNode schemaNode = JsonLoader.fromString(swaggerSchemaAsString);
        final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        swaggerSchema = factory.getJsonSchema(schemaNode);
    }
    return swaggerSchema;
}
Also used : InputStream(java.io.InputStream) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) JsonSchemaFactory(com.github.fge.jsonschema.main.JsonSchemaFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 12 with JsonSchema

use of com.github.fge.jsonschema.main.JsonSchema in project records-management by Alfresco.

the class BaseYamlUnitTest method validateYamlFiles.

/**
 * Helper method to validate that all given yaml files are valid readable Swagger format
 */
protected void validateYamlFiles(final Set<String> yamlFileNames) throws ProcessingException, IOException {
    assertFalse("Expected at least 1 yaml file to validate", yamlFileNames.isEmpty());
    final JsonSchema swaggerSchema = getSwaggerSchema(SWAGGER_2_SCHEMA_LOCATION);
    assertNotNull("Failed to obtain the Swagger schema", swaggerSchema);
    for (String yamlFilePath : yamlFileNames) {
        try {
            // check the yaml file is valid against Swagger JSON schema
            assertTrue("Yaml file is not valid Swagger " + OPEN_API_SPECIFICATION + ": " + yamlFilePath, validateYamlFile(yamlFilePath, swaggerSchema));
            // check can read the swagger object to obtain the swagger version
            Swagger swagger = new SwaggerParser().read(yamlFilePath);
            assertEquals("Failed to obtain Swagger version from yaml file " + yamlFilePath, swagger.getSwagger(), OPEN_API_SPECIFICATION);
        } catch (ParserException ex) {
            // ensure the yaml filename is included in the message
            String context = String.format(yamlFilePath + ": %n" + ex.getContext());
            throw new ParserException(context, ex.getContextMark(), ex.getProblem(), ex.getProblemMark());
        }
    }
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) ParserException(com.fasterxml.jackson.dataformat.yaml.snakeyaml.parser.ParserException) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) Swagger(io.swagger.models.Swagger)

Example 13 with JsonSchema

use of com.github.fge.jsonschema.main.JsonSchema in project syndesis by syndesisio.

the class ExtensionSchemaValidationTest method validateStepExtensionTest.

@Test
public void validateStepExtensionTest() throws ProcessingException, IOException {
    String syndesisExtensionSchema = "/syndesis/syndesis-extension-definition-schema.json";
    JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema("resource:" + syndesisExtensionSchema);
    ExtensionConverter converter = new DefaultExtensionConverter();
    Extension extension = new Extension.Builder().extensionId("my-extension").name("Name").description("Description").uses(OptionalInt.empty()).version("1.0.0").schemaVersion(ExtensionConverter.getCurrentSchemaVersion()).addAction(new StepAction.Builder().id("action-1").name("action-1-name").description("Action 1 Description").pattern(Action.Pattern.From).descriptor(new StepDescriptor.Builder().entrypoint("direct:hello").kind(StepAction.Kind.ENDPOINT).build()).build()).build();
    JsonNode tree = converter.toPublicExtension(extension);
    ProcessingReport report = schema.validate(tree);
    assertFalse(report.toString(), report.iterator().hasNext());
    Extension extensionClone = converter.toInternalExtension(tree);
    assertEquals(extensionClone, extension);
}
Also used : Extension(io.syndesis.common.model.extension.Extension) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) StepDescriptor(io.syndesis.common.model.action.StepDescriptor) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Aggregations

JsonSchema (com.github.fge.jsonschema.main.JsonSchema)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 ProcessingReport (com.github.fge.jsonschema.core.report.ProcessingReport)8 JsonSchemaFactory (com.github.fge.jsonschema.main.JsonSchemaFactory)7 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)4 Extension (io.syndesis.common.model.extension.Extension)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 ProcessingMessage (com.github.fge.jsonschema.core.report.ProcessingMessage)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ParserException (com.fasterxml.jackson.dataformat.yaml.snakeyaml.parser.ParserException)1 Swagger (io.swagger.models.Swagger)1 SwaggerParser (io.swagger.parser.SwaggerParser)1 StepDescriptor (io.syndesis.common.model.action.StepDescriptor)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 MalformedURLException (java.net.MalformedURLException)1 Source (javax.xml.transform.Source)1 StreamSource (javax.xml.transform.stream.StreamSource)1