This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
077c5f1
commit 2ac6f80
Showing
13 changed files
with
363 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
.../java/uk/gov/justice/maven/generator/io/files/parser/SubscriptionDescriptorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...a/uk/gov/justice/maven/generator/io/files/parser/SubscriptionDescriptorFileValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
...ava/uk/gov/justice/maven/generator/io/files/parser/SubscriptionDescriptorIOException.java
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
...in/java/uk/gov/justice/maven/generator/io/files/parser/YamlFileToJsonObjectConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
subscription-descriptor-parser/src/main/resources/json/schema/subscription-schema.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
Oops, something went wrong.