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

Commit

Permalink
Add example context modules for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo authored and amckenzie committed Nov 20, 2017
1 parent 93bbb74 commit d9f8c1d
Show file tree
Hide file tree
Showing 18 changed files with 344 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package uk.gov.justice.schema.catalog;

import static java.lang.String.format;

import uk.gov.justice.schema.catalog.util.UriResolver;
import uk.gov.justice.schema.catalog.util.UrlConverter;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Optional;

Expand All @@ -13,18 +17,27 @@ public class SchemaResolver {
private static final String AN_EMPTY_STRING = "";

private final UrlConverter urlConverter;
private final UriResolver uriResolver;

@Inject
public SchemaResolver(final UrlConverter urlConverter) {
public SchemaResolver(final UrlConverter urlConverter, final UriResolver uriResolver) {
this.urlConverter = urlConverter;
this.uriResolver = uriResolver;
}

public URL resolve(
final URI catalogUri,
final String fileLocation,
final Optional<String> fileBaseLocation) {

final URI schemaUri = URI.create(fileBaseLocation.orElse(AN_EMPTY_STRING)).resolve(fileLocation);
return urlConverter.toUrl(catalogUri.resolve(schemaUri));
try {
final URI schemaUri = URI.create(fileBaseLocation.orElse(AN_EMPTY_STRING)).resolve(fileLocation);
final URI resolvedUri = uriResolver.resolve(catalogUri, schemaUri);

return urlConverter.toUrl(resolvedUri);

} catch (final IllegalArgumentException | URISyntaxException e) {
throw new SchemaCatalogException(format("Failed to resolve %s, %s, %s", catalogUri, fileLocation, fileBaseLocation.orElse(null)), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.gov.justice.schema.catalog.util;

import java.net.URI;
import java.net.URISyntaxException;

public class UriResolver {

public URI resolve(final URI baseUri, final URI otherUri) throws URISyntaxException {

if (baseUri.isOpaque()) {

final String scheme = baseUri.getScheme();
final String schemeSpecificPart = baseUri.getSchemeSpecificPart();
final String fragment = baseUri.getFragment();
final URI resolvedUri = new URI(schemeSpecificPart).resolve(otherUri);

return new URI(scheme, resolvedUri.toString(), fragment);
}

return baseUri.resolve(otherUri);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import uk.gov.justice.schema.catalog.domain.Catalog;
import uk.gov.justice.schema.catalog.domain.Group;
import uk.gov.justice.schema.catalog.domain.Schema;
import uk.gov.justice.schema.catalog.util.UriResolver;
import uk.gov.justice.schema.catalog.util.UrlConverter;

import java.net.URI;
Expand All @@ -32,7 +33,7 @@ public class CatalogToSchemaResolverTest {

@Spy
@SuppressWarnings("unused")
private final SchemaResolver schemaResolver = new SchemaResolver(new UrlConverter());
private final SchemaResolver schemaResolver = new SchemaResolver(new UrlConverter(), new UriResolver());

@InjectMocks
private CatalogToSchemaResolver catalogToSchemaResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import uk.gov.justice.schema.catalog.util.UriResolver;
import uk.gov.justice.schema.catalog.util.UrlConverter;

import java.net.URI;
Expand All @@ -22,7 +23,11 @@
public class SchemaResolverTest {

@Spy @SuppressWarnings("unused")
private UrlConverter urlConverter = new UrlConverter();
private final UrlConverter urlConverter = new UrlConverter();

@Spy
@SuppressWarnings("unused")
private final UriResolver uriResolver = new UriResolver();

@InjectMocks
private SchemaResolver schemaResolver;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package uk.gov.justice.schema.catalog.util;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.net.URI;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class UriResolverTest {

@InjectMocks
private UriResolver uriResolver;

@Test
public void shouldResolveANonOpaqueUri() throws Exception {
final URI baseUri = new URI("file:/src/target/example-standards-1.0.0-SNAPSHOT.jar!/json/schema/schema_catalog.json");
final URI otherUri = new URI("ingredient.json");

assertThat(uriResolver.resolve(baseUri, otherUri).toString(), is("file:/src/target/example-standards-1.0.0-SNAPSHOT.jar!/json/schema/ingredient.json"));
}

@Test
public void shouldResolveAnOpaqueUri() throws Exception {

final URI baseUri = new URI("jar:file:/src/target/example-standards-1.0.0-SNAPSHOT.jar!/json/schema/schema_catalog.json");
final URI otherUri = new URI("ingredient.json");

assertThat(uriResolver.resolve(baseUri, otherUri).toString(), is("jar:file:/src/target/example-standards-1.0.0-SNAPSHOT.jar!/json/schema/ingredient.json"));
}
}
15 changes: 15 additions & 0 deletions example-context/example-command-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>example-context</artifactId>
<groupId>uk.gov.justice.schema</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>example-command-api</artifactId>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "Eton Mess",
"glutenFree": false,
"ingredients": [
{
"name": "custard",
"quantity": 2
},
{
"name": "egg",
"quantity": 6
},
{
"name": "sugar",
"quantity": 500
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://justice.gov.uk/example/cakeshop/example.add-recipe.json",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Uniquely identifies the cake to be produced from the recipe",
"name": "Name of Cake",
"title": "Name of Cake"
},
"glutenFree": {
"type": "boolean"
},
"ingredients": {
"type": "array",
"items": [
{
"id": "http://justice.gov.uk/example/standard/ingredient.json"
}
],
"minItems": 1,
"description": "List ingredients and quantities for recipe"
}
},
"required": [
"name",
"ingredients",
"glutenFree"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"catalog": {
"name": "example",
"group": [
{
"name": "",
"baseLocation": "",
"schema": [
{
"id": "http://justice.gov.uk/example/cakeshop/example.add-recipe.json",
"location": "example.add-recipe.json"
}
]
}
]
}
}
56 changes: 56 additions & 0 deletions example-context/example-integration-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>example-context</artifactId>
<groupId>uk.gov.justice.schema</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>example-integration-test</artifactId>

<dependencies>
<!-- Test Dependencies -->
<dependency>
<groupId>uk.gov.justice.schema</groupId>
<artifactId>example-command-api</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.gov.justice.schema</groupId>
<artifactId>example-standards</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.gov.justice.schema</groupId>
<artifactId>catalog-core</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.gov.justice.utils</groupId>
<artifactId>utilities-core</artifactId>
<version>${utilities.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package uk.gov.justice.schema.catalog;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import uk.gov.justice.schema.catalog.util.ClasspathResourceLoader;
import uk.gov.justice.schema.catalog.util.UriResolver;
import uk.gov.justice.schema.catalog.util.UrlConverter;
import uk.gov.justice.schema.client.SchemaClientFactory;
import uk.gov.justice.services.common.converter.jackson.ObjectMapperProducer;

import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.everit.json.schema.Schema;
import org.junit.Test;

public class CatalogLoaderIT {

private final FileContentsAsStringLoader fileContentsAsStringLoader = new FileContentsAsStringLoader();
private final JsonStringToSchemaConverter jsonStringToSchemaConverter = new JsonStringToSchemaConverter();
private final SchemaResolverAndLoader schemaResolverAndLoader = new SchemaResolverAndLoader(jsonStringToSchemaConverter);
private final ObjectMapper objectMapper = new ObjectMapperProducer().objectMapper();
private final UrlConverter urlConverter = new UrlConverter();
private final UriResolver uriResolver = new UriResolver();
private final ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader();
private final ClasspathCatalogLoader classpathCatalogLoader = new ClasspathCatalogLoader(objectMapper, classpathResourceLoader, urlConverter);
private final SchemaResolver schemaResolver = new SchemaResolver(urlConverter, uriResolver);
private final CatalogToSchemaResolver catalogToSchemaResolver = new CatalogToSchemaResolver(classpathCatalogLoader, schemaResolver);
private final JsonSchemaLoader jsonSchemaLoader = new JsonSchemaLoader(fileContentsAsStringLoader);
private final SchemaClientFactory schemaClientFactory = new SchemaClientFactory();

private final CatalogLoader catalogLoader = new CatalogLoader(
schemaResolverAndLoader,
catalogToSchemaResolver,
jsonSchemaLoader,
schemaClientFactory);

@Test
public void shouldMapSchemasOnClasspathToTheirIds() throws Exception {

final Map<String, Schema> idsToSchemaMap = catalogLoader.loadCatalogsFromClasspath();

assertThat(idsToSchemaMap.size(), is(2));

final String id_1 = "http://justice.gov.uk/example/standard/ingredient.json";
final String json_1 = idsToSchemaMap.get(id_1).toString();

final String id_2 = "http://justice.gov.uk/example/cakeshop/example.add-recipe.json";
final String json_2 = idsToSchemaMap.get(id_2).toString();
}
}
15 changes: 15 additions & 0 deletions example-context/example-standards/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>example-context</artifactId>
<groupId>uk.gov.justice.schema</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>example-standards</artifactId>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"catalog": {
"name": "standards",
"group": [
{
"name": "cakeshop",
"baseLocation": "standards/",
"schema": [
{
"id": "http://justice.gov.uk/example/standard/ingredient.json",
"location": "ingredient.json"
}
]
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://justice.gov.uk/example/standard/ingredient.json",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"quantity": {
"type": "integer"
}
}
}
Loading

0 comments on commit d9f8c1d

Please sign in to comment.