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

Commit

Permalink
add catalog generation to integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
amckenzie committed Nov 21, 2017
1 parent 592c2ac commit 05c086c
Show file tree
Hide file tree
Showing 38 changed files with 332 additions and 276 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static java.util.stream.Collectors.toMap;

import uk.gov.justice.schema.catalog.domain.Catalog;
import uk.gov.justice.schema.catalog.domain.CatalogWrapper;
import uk.gov.justice.schema.catalog.util.ClasspathResourceLoader;
import uk.gov.justice.schema.catalog.util.UrlConverter;

Expand Down Expand Up @@ -42,7 +41,7 @@ public Map<URI, Catalog> getCatalogs() {

private Catalog loadCatalog(final URL catalogUrl) {
try {
return objectMapper.readValue(catalogUrl, CatalogWrapper.class).getCatalog();
return objectMapper.readValue(catalogUrl, Catalog.class);
} catch (IOException e) {
throw new SchemaCatalogException(format("Failed to convert to json loaded from '%s' to a Catalog pojo", catalogUrl.toString()), e);
}
Expand Down
53 changes: 53 additions & 0 deletions catalog-core/src/main/resources/schema_catalog_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"group": {
"type": "array",
"items": {
"type": "object",
"properties": {
"baseLocation": {
"type": "string"
},
"name": {
"type": "string"
},
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"location": {
"location": "string"
}
},
"additionalProperties": false,
"required": [
"id",
"location"
]
}
}
},
"additionalProperties": false,
"required": [
"baseLocation",
"name",
"schema"
]
}
}
},
"additionalProperties": false,
"required": [
"name",
"group"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;

import uk.gov.justice.schema.catalog.domain.CatalogWrapper;
import uk.gov.justice.schema.catalog.domain.Catalog;
import uk.gov.justice.schema.catalog.util.ClasspathResourceLoader;
import uk.gov.justice.schema.catalog.util.UrlConverter;

Expand Down Expand Up @@ -46,7 +46,7 @@ public void shouldThrowExceptionIfLoadingFileThrowsIOException() throws Exceptio

when(classpathResourceLoader.getResources(ClasspathCatalogLoader.class, "json/schema/schema_catalog.json")).thenReturn(singletonList(url));
when(urlConverter.toUri(url)).thenReturn(uri);
when(objectMapper.readValue(url, CatalogWrapper.class)).thenThrow(ioException);
when(objectMapper.readValue(url, Catalog.class)).thenThrow(ioException);

try {
classpathCatalogLoader.getCatalogs();
Expand Down
54 changes: 26 additions & 28 deletions catalog-core/src/test/resources/json/schema/schema_catalog.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
{
"catalog": {
"name": "my catalog",
"group": [
{
"name": "standards",
"baseLocation": "standards/",
"schema": [
{
"id": "http://justice.gov.uk/standards/complex_address.json",
"location": "complex_address.json"
},
{
"id": "http://justice.gov.uk/standards/address.json",
"location": "address.json"
}
]
},
{
"name": "staging interface",
"schema": [
{
"id": "http://justice.gov.uk/context/person.json",
"location": "context/person.json"
}
]
}
]
}
"name": "my catalog",
"group": [
{
"name": "standards",
"baseLocation": "standards/",
"schema": [
{
"id": "http://justice.gov.uk/standards/complex_address.json",
"location": "complex_address.json"
},
{
"id": "http://justice.gov.uk/standards/address.json",
"location": "address.json"
}
]
},
{
"name": "staging interface",
"schema": [
{
"id": "http://justice.gov.uk/context/person.json",
"location": "context/person.json"
}
]
}
]
}

This file was deleted.

71 changes: 0 additions & 71 deletions catalog-domain/src/main/resources/domain/json_catalog.schema.json

This file was deleted.

10 changes: 0 additions & 10 deletions catalog-generation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@
<artifactId>catalog-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice.maven.generator</groupId>
<artifactId>generator-core</artifactId>
<version>${generator-maven-plugin.version}</version>
</dependency>
<dependency>
<groupId>uk.gov.justice.maven.generator</groupId>
<artifactId>parser-common</artifactId>
<version>${generator-maven-plugin.version}</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
public class CatalogGenerationContext {

private static final String CATALOG_FILENAME = "schema_catalog.json";
private static final String JSON_SCHEMA_PATH = "raml/json/schema/";
public static final String AN_EMPTY_STRING = "";

public String getCatalogFilename() {
return CATALOG_FILENAME;
}

public String getJsonSchemaPath() {
return JSON_SCHEMA_PATH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public CatalogGenerationRunner(
this.urlConverter = urlConverter;
}

public void generateCatalog(final String catalogName, final List<URI> schemaFiles, final Path catalogGenerationPath) {
public void generateCatalog(final String catalogName, final List<URI> schemaFiles, final Path catalogGenerationPath, final Path jsonSchemaPath) {

final Catalog catalog = catalogObjectGenerator.generate(
catalogName,
asUrls(schemaFiles));
asUrls(schemaFiles),
jsonSchemaPath);

catalogWriter.write(catalog, catalogGenerationPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import uk.gov.justice.schema.catalog.domain.Schema;

import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -20,10 +21,10 @@ public CatalogObjectGenerator(final SchemaDefParser schemaDefParser) {
this.schemaDefParser = schemaDefParser;
}

public Catalog generate(final String catalogName, final List<URL> schemaFiles) {
public Catalog generate(final String catalogName, final List<URL> schemaFiles, final Path jsonSchemaPath) {

final List<SchemaDef> schemaDefs = schemaFiles.stream()
.map(schemaDefParser::parse)
.map(schemaFile -> schemaDefParser.parse(schemaFile, jsonSchemaPath))
.collect(toList());

final List<Group> groups = asGroups(schemaDefs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void run(final List<URI> schemaFiles, final GeneratorConfig generatorConf
final CatalogGeneratorProperties generatorProperties =
(CatalogGeneratorProperties) generatorConfig.getGeneratorProperties();
final String catalogName = generatorProperties.getCatalogName();
final Path jsonSchemaPath = generatorProperties.getJsonSchemaPath();

catalogGenerationRunner.generateCatalog(catalogName, schemaFiles, catalogGenerationPath);
catalogGenerationRunner.generateCatalog(catalogName, schemaFiles, catalogGenerationPath, jsonSchemaPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CatalogObjectGenerator catalogObjectGenerator() {
}

public SchemaDefParser schemaDefParser() {
return new SchemaDefParser(schemaIdParser(), catalogGenerationConstants());
return new SchemaDefParser(schemaIdParser());
}

public CatalogGenerationRunner catalogGenerationRunner() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,15 @@ public String getBaseLocation() {
public String getLocation() {
return location;
}

@Override
public String toString() {
return "SchemaDef{" +
"schemaFile=" + schemaFile +
", id=" + id +
", groupName='" + groupName + '\'' +
", baseLocation='" + baseLocation + '\'' +
", location='" + location + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@

import java.net.URI;
import java.net.URL;
import java.nio.file.Path;

public class SchemaDefParser {

private final SchemaIdParser schemaIdParser;
private final CatalogGenerationContext catalogGenerationContext;

public SchemaDefParser(
final SchemaIdParser schemaIdParser,
final CatalogGenerationContext catalogGenerationContext) {
public SchemaDefParser(final SchemaIdParser schemaIdParser) {
this.schemaIdParser = schemaIdParser;
this.catalogGenerationContext = catalogGenerationContext;
}

public SchemaDef parse(final URL schemaFile) {
public SchemaDef parse(final URL schemaFile, final Path jsonSchemaPath) {

final URI id = schemaIdParser.parse(schemaFile);
final String fileUrl = schemaFile.toString();

final String jsonSchemaPath = catalogGenerationContext.getJsonSchemaPath();
final String relativeUri = fileUrl.substring(fileUrl.indexOf(jsonSchemaPath) + jsonSchemaPath.length());
final String jsonSchemaPathString = jsonSchemaPath.toString();
final String relativeUri = fileUrl.substring(fileUrl.indexOf(jsonSchemaPathString) + jsonSchemaPathString.length() + 1);

final int firstSlashIndex = relativeUri.indexOf('/');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorProperties;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.maven.plugins.annotations.Parameter;

public class CatalogGeneratorProperties implements GeneratorProperties {

@Parameter
private String catalogName;

@Parameter(defaultValue = "json/schema/")
private String jsonSchemaPath = "json/schema/";

public String getCatalogName() {
return catalogName;
}

public Path getJsonSchemaPath() {
return Paths.get(jsonSchemaPath);
}
}
Loading

0 comments on commit 05c086c

Please sign in to comment.