Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
add yaml schema validator
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedFarouk-HMCTS committed Mar 6, 2018
1 parent 077c5f1 commit 2ac6f80
Show file tree
Hide file tree
Showing 13 changed files with 363 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<properties>
<common-bom.version>1.21.0</common-bom.version>
<cpp.repo.name>generator-maven-plugin</cpp.repo.name>
<jackson-dataformat-yaml-version>2.8.7</jackson-dataformat-yaml-version>
<jackson-dataformat-yaml.version>2.8.7</jackson-dataformat-yaml.version>
</properties>

<scm>
Expand Down
6 changes: 5 additions & 1 deletion subscription-descriptor-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson-dataformat-yaml-version}</version>
<version>${jackson-dataformat-yaml.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
Expand All @@ -44,6 +44,10 @@
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.github.everit-org.json-schema</groupId>
<artifactId>org.everit.json.schema</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uk.gov.justice.maven.generator.io.files.parser;

public class SubscriptionDescriptorException extends RuntimeException {
public SubscriptionDescriptorException(final String message, final Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package uk.gov.justice.maven.generator.io.files.parser;

import static com.fasterxml.jackson.annotation.JsonCreator.Mode.PROPERTIES;
import static java.lang.String.format;

import uk.gov.justice.domain.subscriptiondescriptor.SubscriptionDescriptor;
import uk.gov.justice.domain.subscriptiondescriptor.SubscriptionDescriptorDef;

import java.io.File;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.stream.Collectors;
Expand All @@ -16,27 +17,40 @@
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.everit.json.schema.ValidationException;

public class SubscriptionDescriptorFileParser implements FileParser<SubscriptionDescriptor> {


private SubscriptionDescriptorFileValidator subscriptionDescriptorFileValidator;

public SubscriptionDescriptorFileParser(final SubscriptionDescriptorFileValidator subscriptionDescriptorFileValidator) {
this.subscriptionDescriptorFileValidator = subscriptionDescriptorFileValidator;
}
@Override
public Collection<SubscriptionDescriptor> parse(final Path baseDir, final Collection<Path> paths) {
return paths.stream()
.map(path -> read(baseDir.resolve(path).toAbsolutePath().toString()))
.map(path -> read(baseDir.resolve(path).toAbsolutePath()))
.collect(Collectors.toList());
}

private SubscriptionDescriptor read(final String filePath) {
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory())
.registerModule(new Jdk8Module())
.registerModule(new ParameterNamesModule(PROPERTIES));
private SubscriptionDescriptor read(final Path filePath) {
try {
subscriptionDescriptorFileValidator.validate(filePath);

mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory())
.registerModule(new Jdk8Module())
.registerModule(new ParameterNamesModule(PROPERTIES));

try {
return mapper.readValue(new File(filePath), SubscriptionDescriptorDef.class).getSubscriptionDescriptor();
} catch (IOException e) {
throw new SubscriptionDescriptorIOException("Failed to read subscriptions yaml file", e);
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);

return mapper.readValue(filePath.toFile(), SubscriptionDescriptorDef.class).getSubscriptionDescriptor();
} catch (final NoSuchFileException e) {
throw new SubscriptionDescriptorException(format("No such subscriptions YAML file %s ", filePath), e);
} catch (final ValidationException e) {
throw new SubscriptionDescriptorException(format("Failed to validate subscriptions yaml file %s ", filePath), e);
} catch (final IOException e) {
throw new SubscriptionDescriptorException(format("Failed to read subscriptions yaml file %s ", filePath), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public class SubscriptionDescriptorFileParserFactory implements FileParserFactor

@Override
public SubscriptionDescriptorFileParser create() {
return new SubscriptionDescriptorFileParser();
return new SubscriptionDescriptorFileParser(new SubscriptionDescriptorFileValidator(new YamlFileToJsonObjectConverter()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package uk.gov.justice.maven.generator.io.files.parser;

import static java.lang.String.format;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;

import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;

public class SubscriptionDescriptorFileValidator {

private YamlFileToJsonObjectConverter yamlFileToJsonObjectConverter;

private static final String SCHEMA_FILE = "/json/schema/subscription-schema.json";

public SubscriptionDescriptorFileValidator(final YamlFileToJsonObjectConverter yamlFileToJsonObjectConverter) {
this.yamlFileToJsonObjectConverter = yamlFileToJsonObjectConverter;
}

public void validate(final Path filePath) {
try {
final JSONObject convert = yamlFileToJsonObjectConverter.convert(filePath);
schema().validate(convert);
} catch (IOException ex) {
throw new SubscriptionDescriptorException(format("Unable to convert to JSON file %s ", filePath.toString()), ex);
}

}

private Schema schema() {
try (final InputStream schemaFileStream = this.getClass().getResourceAsStream(SCHEMA_FILE)) {
return SchemaLoader.builder()
.schemaJson(new JSONObject(new JSONTokener(schemaFileStream)))
.build().load().build();
} catch (final IOException ex) {
throw new SubscriptionDescriptorException(format("Unable to load JSON schema %s from classpath", SCHEMA_FILE), ex);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package uk.gov.justice.maven.generator.io.files.parser;

import static java.nio.file.Files.readAllLines;
import static java.util.stream.Collectors.joining;

import java.io.IOException;
import java.nio.file.Path;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.json.JSONObject;

public class YamlFileToJsonObjectConverter {

final String lineSeparator = System.getProperty("line.separator");

public JSONObject convert(final Path filePath) throws IOException {

final String yamlStr = readAllLines(filePath)
.stream()
.collect(joining(lineSeparator));

final Object yamlObject = new ObjectMapper(new YAMLFactory())
.readValue(yamlStr, Object.class);

final ObjectMapper jsonWriter = new ObjectMapper();

return new JSONObject(jsonWriter.writeValueAsString(yamlObject));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"$id": "http://example.com/example.json",
"type": "object",
"definitions": {},
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"subscription_descriptor": {
"$id": "/properties/subscription_descriptor",
"type": "object",
"properties": {
"spec_version": {
"$id": "/properties/subscription_descriptor/properties/spec_version",
"type": "string",
"title": "The Spec_version Schema ",
"examples": [
"1.0.0"
]
},
"service": {
"$id": "/properties/subscription_descriptor/properties/service",
"type": "string",
"title": "The Service Schema ",
"examples": [
"examplecontext"
]
},
"service_component": {
"$id": "/properties/subscription_descriptor/properties/service_component",
"type": "string",
"title": "The Service_component Schema ",
"examples": [
"EVENT_LISTENER"
]
},
"subscriptions": {
"$id": "/properties/subscription_descriptor/properties/subscriptions",
"type": "array",
"items": {
"$id": "/properties/subscription_descriptor/properties/subscriptions/items",
"type": "object",
"properties": {
"name": {
"$id": "/properties/subscription_descriptor/properties/subscriptions/items/properties/name",
"type": "string",
"title": "The Name Schema ",
"examples": [
"subscription1"
]
},
"events": {
"$id": "/properties/subscription_descriptor/properties/subscriptions/items/properties/events",
"type": "array",
"items": {
"$id": "/properties/subscription_descriptor/properties/subscriptions/items/properties/events/items",
"type": "object",
"properties": {
"name": {
"$id": "/properties/subscription_descriptor/properties/subscriptions/items/properties/events/items/properties/name",
"type": "string",
"title": "The Name Schema ",
"examples": [
"example.recipe-added"
]
},
"schema_uri": {
"$id": "/properties/subscription_descriptor/properties/subscriptions/items/properties/events/items/properties/schema_uri",
"type": "string",
"title": "The Schema_uri Schema ",
"pattern": "^http|https:(\\/?\\/?)[^\\s]+$",
"examples": [
"http://justice.gov.uk/json/schemas/domains/example/example.recipe-added.json"
]
}
},
"required": [
"name",
"schema_uri"
]
}
},
"eventsource": {
"$id": "/properties/subscription_descriptor/properties/subscriptions/items/properties/eventsource",
"type": "object",
"properties": {
"name": {
"$id": "/properties/subscription_descriptor/properties/subscriptions/items/properties/eventsource/properties/name",
"type": "string",
"title": "The Name of event source ",
"examples": [
"examplecontext"
]
},
"location": {
"$id": "/properties/subscription_descriptor/properties/subscriptions/items/properties/eventsource/properties/location",
"type": "object",
"properties": {
"jms_uri": {
"$id": "/properties/subscription_descriptor/properties/subscriptions/items/properties/eventsource/properties/location/properties/jms_uri",
"type": "string",
"title": "The Jms_uri Schema ",
"pattern": "^jms:(\\/?\\/?)[^\\s]+$",
"examples": [
"jms:topic:example.event?timeToLive=1000"
]
},
"rest_uri": {
"$id": "/properties/subscription_descriptor/properties/subscriptions/items/properties/eventsource/properties/location/properties/rest_uri",
"type": "string",
"title": "The Rest_uri Schema ",
"pattern": "^http|https:(\\/?\\/?)[^\\s]+$",
"examples": [
"http://justice.gov.uk/json/schemas/domains/example/example.recipe-added.json"
]
}
},
"required": [
"jms_uri",
"rest_uri"
]
}
},
"required": [
"name",
"location"
]
}
},
"required": [
"name",
"events",
"eventsource"
]
}
}
},
"required": [
"spec_version",
"service",
"service_component",
"subscriptions"
]
}
},
"required": [
"subscription_descriptor"
]
}
Loading

0 comments on commit 2ac6f80

Please sign in to comment.