diff --git a/README.md b/README.md new file mode 100644 index 0000000..ebf4a39 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Json Schema Catalog diff --git a/catalog-core/pom.xml b/catalog-core/pom.xml new file mode 100644 index 0000000..f21a7d4 --- /dev/null +++ b/catalog-core/pom.xml @@ -0,0 +1,70 @@ + + + + json-schema-catalog + uk.gov.justice.schema + 1.0.0-SNAPSHOT + + 4.0.0 + + catalog-core + + + + javax + javaee-api + provided + + + org.glassfish + javax.json + + + uk.gov.justice.schema + everit-schema-client + ${project.version} + + + uk.gov.justice.schema + catalog-domain + ${project.version} + + + uk.gov.justice.utils + utilities-core + ${utilities.version} + + + com.github.everit-org.json-schema + org.everit.json.schema + + + commons-io + commons-io + + + org.raml + raml-parser + + + + + junit + junit + test + + + org.mockito + mockito-core + test + + + com.jayway.jsonpath + json-path-assert + test + + + + diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogLoader.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogLoader.java new file mode 100644 index 0000000..6525ffa --- /dev/null +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogLoader.java @@ -0,0 +1,37 @@ +package uk.gov.justice.schema.catalog; + +import uk.gov.justice.schema.client.SchemaClientFactory; + +import java.util.Map; + +import org.everit.json.schema.Schema; + +public class CatalogLoader { + + private final SchemaResolverAndLoader schemaResolverAndLoader; + private final CatalogToSchemaResolver catalogToSchemaResolver; + private final JsonSchemaLoader jsonSchemaLoader; + private final SchemaClientFactory schemaClientFactory; + + public CatalogLoader( + final SchemaResolverAndLoader schemaResolverAndLoader, + final CatalogToSchemaResolver catalogToSchemaResolver, + final JsonSchemaLoader jsonSchemaLoader, + final SchemaClientFactory schemaClientFactory) { + this.schemaResolverAndLoader = schemaResolverAndLoader; + this.catalogToSchemaResolver = catalogToSchemaResolver; + this.jsonSchemaLoader = jsonSchemaLoader; + this.schemaClientFactory = schemaClientFactory; + } + + public Map loadCatalogsFromClasspath() { + + final Map urlsToJson = jsonSchemaLoader.loadJsonFrom( + catalogToSchemaResolver.resolveSchemaLocations() + ); + + return schemaResolverAndLoader.loadSchemas( + urlsToJson, + schemaClientFactory.create(urlsToJson)); + } +} diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogToSchemaResolver.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogToSchemaResolver.java new file mode 100644 index 0000000..2ba9635 --- /dev/null +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/CatalogToSchemaResolver.java @@ -0,0 +1,43 @@ +package uk.gov.justice.schema.catalog; + +import static java.util.Optional.ofNullable; +import static java.util.stream.Collectors.toMap; + +import uk.gov.justice.schema.catalog.domain.Group; +import uk.gov.justice.schema.catalog.domain.Schema; + +import java.net.URL; +import java.util.AbstractMap.SimpleEntry; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; + +public class CatalogToSchemaResolver { + + private final ClasspathCatalogLoader classpathCatalogLoader; + private final SchemaResolver schemaResolver; + + public CatalogToSchemaResolver(final ClasspathCatalogLoader classpathCatalogLoader, final SchemaResolver schemaResolver) { + this.classpathCatalogLoader = classpathCatalogLoader; + this.schemaResolver = schemaResolver; + } + + public Map resolveSchemaLocations() { + + return classpathCatalogLoader.getCatalogs().entrySet().stream() + .flatMap(catalogEntry -> catalogEntry.getValue().getGroup().stream() + .flatMap(group -> group.getSchema().stream() + .map(schema -> resolveToUrl(group, schema, catalogEntry.getKey())))) + .collect(toMap(Entry::getKey, Entry::getValue)); + } + + private Entry resolveToUrl(final Group group, final Schema schema, final URL catalogUrl) { + final String location = schema.getLocation(); + final Optional baseLocation = ofNullable(group.getBaseLocation()); + final URL url = schemaResolver.resolve(catalogUrl, location, baseLocation); + + return new SimpleEntry<>(schema.getId(), url); + } + + +} diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoader.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoader.java new file mode 100644 index 0000000..e127a05 --- /dev/null +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoader.java @@ -0,0 +1,53 @@ +package uk.gov.justice.schema.catalog; + +import static java.lang.String.format; + +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 java.io.IOException; +import java.net.URI; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class ClasspathCatalogLoader { + + private static final String DEFAULT_JSON_CATALOG_LOCATION = "json/schema/schema_catalog.json"; + + private final ObjectMapper objectMapper; + private final ClasspathResourceLoader classpathResourceLoader; + + public ClasspathCatalogLoader(final ObjectMapper objectMapper, final ClasspathResourceLoader classpathResourceLoader) { + this.objectMapper = objectMapper; + this.classpathResourceLoader = classpathResourceLoader; + } + + public Map getCatalogs() { + + final Map map = new HashMap<>(); + + for (final URI uri : listAllCatalogsFromClasspath()) { + try { + final Catalog catalog = objectMapper.readValue(uri.toURL(), CatalogWrapper.class).getCatalog(); + map.put(uri.toURL(), catalog); + } catch (IOException e) { + throw new SchemaCatalogException(format("Failed to convert to json loaded from '%s' to a Catalog pojo", uri.toString()), e); + } + } + + return map; + } + + private List listAllCatalogsFromClasspath() { + try { + return classpathResourceLoader.getResources(getClass(), DEFAULT_JSON_CATALOG_LOCATION); + } catch (final IOException e) { + throw new SchemaCatalogException(format("Failed to load the catalogs from the classpath for location '%s'", DEFAULT_JSON_CATALOG_LOCATION), e); + } + } +} diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/FileContentsAsStringLoader.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/FileContentsAsStringLoader.java new file mode 100644 index 0000000..1c850aa --- /dev/null +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/FileContentsAsStringLoader.java @@ -0,0 +1,21 @@ +package uk.gov.justice.schema.catalog; + +import static java.lang.String.format; +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.io.IOException; +import java.net.URL; + +import org.apache.commons.io.IOUtils; + +public class FileContentsAsStringLoader { + + public String readFileContents(final URL urlOfFile) { + + try { + return IOUtils.toString(urlOfFile, UTF_8); + } catch (final IOException e) { + throw new SchemaCatalogException(format("Failed to read file contents from '%s'", urlOfFile), e); + } + } +} diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonSchemaLoader.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonSchemaLoader.java new file mode 100644 index 0000000..3302866 --- /dev/null +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonSchemaLoader.java @@ -0,0 +1,21 @@ +package uk.gov.justice.schema.catalog; + +import static java.util.stream.Collectors.toMap; + +import java.net.URL; +import java.util.Map; + +public class JsonSchemaLoader { + + private final FileContentsAsStringLoader fileContentsAsStringLoader; + + public JsonSchemaLoader(final FileContentsAsStringLoader fileContentsAsStringLoader) { + this.fileContentsAsStringLoader = fileContentsAsStringLoader; + } + + public Map loadJsonFrom(final Map schemaLocationMap) { + return schemaLocationMap.entrySet() + .stream() + .collect(toMap(Map.Entry::getKey, entry -> fileContentsAsStringLoader.readFileContents(entry.getValue()))); + } +} diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverter.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverter.java new file mode 100644 index 0000000..36d6af7 --- /dev/null +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/JsonStringToSchemaConverter.java @@ -0,0 +1,19 @@ +package uk.gov.justice.schema.catalog; + +import org.everit.json.schema.Schema; +import org.everit.json.schema.loader.SchemaClient; +import org.everit.json.schema.loader.SchemaLoader; +import org.json.JSONObject; +import org.json.JSONTokener; + +public class JsonStringToSchemaConverter { + + public Schema convert(final String schemaJson, final SchemaClient schemaClient) { + return SchemaLoader.builder() + .schemaJson(new JSONObject(new JSONTokener(schemaJson))) + .httpClient(schemaClient) + .build() + .load() + .build(); + } +} diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaCatalogException.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaCatalogException.java new file mode 100644 index 0000000..ff4871a --- /dev/null +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaCatalogException.java @@ -0,0 +1,8 @@ +package uk.gov.justice.schema.catalog; + +public class SchemaCatalogException extends RuntimeException { + + public SchemaCatalogException(final String message, final Throwable cause) { + super(message, cause); + } +} diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaResolver.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaResolver.java new file mode 100644 index 0000000..f435cbb --- /dev/null +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaResolver.java @@ -0,0 +1,29 @@ +package uk.gov.justice.schema.catalog; + +import uk.gov.justice.schema.catalog.util.UrlConverter; + +import java.net.URI; +import java.net.URL; +import java.util.Optional; + +public class SchemaResolver { + + private static final String AN_EMPTY_STRING = ""; + + private final UrlConverter urlConverter; + + public SchemaResolver(final UrlConverter urlConverter) { + this.urlConverter = urlConverter; + } + + public URL resolve( + final URL catalogUrl, + final String fileLocation, + final Optional fileBaseLocation) { + + final URI schemaUri = URI.create(fileBaseLocation.orElse(AN_EMPTY_STRING)).resolve(fileLocation); + final URI uri = urlConverter.toUri(catalogUrl).resolve(schemaUri); + + return urlConverter.toUrl(uri); + } +} diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaResolverAndLoader.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaResolverAndLoader.java new file mode 100644 index 0000000..ee578a5 --- /dev/null +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaResolverAndLoader.java @@ -0,0 +1,25 @@ +package uk.gov.justice.schema.catalog; + +import static java.util.stream.Collectors.toMap; + +import java.util.Map; + +import org.everit.json.schema.Schema; +import org.everit.json.schema.loader.SchemaClient; + +public class SchemaResolverAndLoader { + + private final JsonStringToSchemaConverter jsonStringToSchemaConverter; + + public SchemaResolverAndLoader(final JsonStringToSchemaConverter jsonStringToSchemaConverter) { + this.jsonStringToSchemaConverter = jsonStringToSchemaConverter; + } + + public Map loadSchemas(final Map urlsToJson, final SchemaClient schemaClient) { + + return urlsToJson.entrySet() + .stream() + .collect(toMap(Map.Entry::getKey, entry -> jsonStringToSchemaConverter.convert(entry.getValue(), schemaClient))); + } + +} diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/ClasspathResourceLoader.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/ClasspathResourceLoader.java new file mode 100644 index 0000000..e47becc --- /dev/null +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/ClasspathResourceLoader.java @@ -0,0 +1,24 @@ +package uk.gov.justice.schema.catalog.util; + +import static java.util.Collections.list; +import static java.util.stream.Collectors.toList; + +import java.io.IOException; +import java.net.URI; +import java.net.URL; +import java.util.List; + +public class ClasspathResourceLoader { + + public ClasspathResourceLoader(final UrlConverter urlConverter) { + this.urlConverter = urlConverter; + } + + private final UrlConverter urlConverter; + + + public List getResources(final Class clazz, final String location) throws IOException { + final List urls = list(clazz.getClassLoader().getResources(location)); + return urls.stream().map(urlConverter::toUri).collect(toList()); + } +} diff --git a/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/UrlConverter.java b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/UrlConverter.java new file mode 100644 index 0000000..10a38a9 --- /dev/null +++ b/catalog-core/src/main/java/uk/gov/justice/schema/catalog/util/UrlConverter.java @@ -0,0 +1,29 @@ +package uk.gov.justice.schema.catalog.util; + +import static java.lang.String.format; + +import uk.gov.justice.schema.catalog.SchemaCatalogException; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +public class UrlConverter { + + public URI toUri(final URL url) { + try { + return url.toURI(); + } catch (URISyntaxException e) { + throw new SchemaCatalogException(format("Failed to convert URL '%s' to URI", url), e); + } + } + + public URL toUrl(final URI uri) { + try { + return uri.toURL(); + } catch (MalformedURLException e) { + throw new SchemaCatalogException(format("Failed to convert URI '%s' to URL", uri), e); + } + } +} diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/CatalogLoaderTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/CatalogLoaderTest.java new file mode 100644 index 0000000..ed4ea52 --- /dev/null +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/CatalogLoaderTest.java @@ -0,0 +1,58 @@ +package uk.gov.justice.schema.catalog; + +import static com.google.common.collect.ImmutableMap.of; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import uk.gov.justice.schema.client.LocalFileSystemSchemaClient; +import uk.gov.justice.schema.client.SchemaClientFactory; + +import java.net.URL; +import java.util.Map; + +import org.everit.json.schema.Schema; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class CatalogLoaderTest { + + + @Mock + private SchemaResolverAndLoader schemaResolverAndLoader; + + @Mock + private CatalogToSchemaResolver catalogToSchemaResolver; + + @Mock + private JsonSchemaLoader jsonSchemaLoader; + + @Mock + private SchemaClientFactory schemaClientFactory; + + @InjectMocks + private CatalogLoader catalogLoader; + + @Test + public void shouldLoadCatalogs() throws Exception { + + final Map schemaLocationMap = of("name", new URL("file://src/main/file.txt")); + final Map urlsToJson = of("id", "{\"some\": \"json\"}"); + final Map urlsToSchema = of("id", mock(Schema.class)); + + final LocalFileSystemSchemaClient localFileSystemSchemaClient = mock(LocalFileSystemSchemaClient.class); + + when(catalogToSchemaResolver.resolveSchemaLocations()).thenReturn(schemaLocationMap); + when(jsonSchemaLoader.loadJsonFrom(schemaLocationMap)).thenReturn(urlsToJson); + when(schemaClientFactory.create(urlsToJson)).thenReturn(localFileSystemSchemaClient); + + when(schemaResolverAndLoader.loadSchemas(urlsToJson, localFileSystemSchemaClient)).thenReturn(urlsToSchema); + + assertThat(catalogLoader.loadCatalogsFromClasspath(), is(urlsToSchema)); + } +} diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/CatalogToSchemaResolverTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/CatalogToSchemaResolverTest.java new file mode 100644 index 0000000..f625db0 --- /dev/null +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/CatalogToSchemaResolverTest.java @@ -0,0 +1,64 @@ +package uk.gov.justice.schema.catalog; + +import static java.util.Collections.singletonList; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +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.UrlConverter; + +import java.net.URL; +import java.util.Map; + +import com.google.common.collect.ImmutableMap; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.runners.MockitoJUnitRunner; + + +@RunWith(MockitoJUnitRunner.class) +public class CatalogToSchemaResolverTest { + + @Mock + private ClasspathCatalogLoader classpathCatalogLoader; + + @Spy + @SuppressWarnings("unused") + private final SchemaResolver schemaResolver = new SchemaResolver(new UrlConverter()); + + @InjectMocks + private CatalogToSchemaResolver catalogToSchemaResolver; + + @Test + public void shouldMapSchemasFoundOnTheClasspathToTheirIds() throws Exception { + + final Catalog catalog = mock(Catalog.class); + final Group group = mock(Group.class); + final Schema schema = mock(Schema.class); + + final URL url = new URL("file:/src/main/schema.json"); + + final Map catalogPojoMap = ImmutableMap.of(url, catalog); + + when(classpathCatalogLoader.getCatalogs()).thenReturn(catalogPojoMap); + when(catalog.getGroup()).thenReturn(singletonList(group)); + when(group.getSchema()).thenReturn(singletonList(schema)); + + when(schema.getLocation()).thenReturn("some/path/to.json"); + when(schema.getId()).thenReturn("schemaId"); + + final Map schemaLocations = catalogToSchemaResolver.resolveSchemaLocations(); + + assertThat(schemaLocations.size(), is(1)); + + assertThat(schemaLocations.containsKey("schemaId"), is(true)); + assertThat(schemaLocations.get("schemaId").toString(), is("file:/src/main/some/path/to.json")); + } +} diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoaderIntegrationTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoaderIntegrationTest.java new file mode 100644 index 0000000..254988e --- /dev/null +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoaderIntegrationTest.java @@ -0,0 +1,71 @@ +package uk.gov.justice.schema.catalog; + +import static org.hamcrest.CoreMatchers.endsWith; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.junit.Assert.assertThat; + +import uk.gov.justice.schema.catalog.domain.Catalog; +import uk.gov.justice.schema.catalog.util.ClasspathResourceLoader; +import uk.gov.justice.schema.catalog.util.UrlConverter; +import uk.gov.justice.services.common.converter.jackson.ObjectMapperProducer; + +import java.net.URL; +import java.util.Map; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.hamcrest.CoreMatchers; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Spy; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class ClasspathCatalogLoaderIntegrationTest { + + @Spy + @SuppressWarnings("unused") + private ObjectMapper objectMapper = new ObjectMapperProducer().objectMapper(); + + @Spy + @SuppressWarnings("unused") + private ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader(new UrlConverter()); + + @InjectMocks + private ClasspathCatalogLoader classpathCatalogLoader; + + @Test + public void shouldLoadCatalogsFromTheClasspath() throws Exception { + + final Map catalogs = classpathCatalogLoader.getCatalogs(); + + assertThat(catalogs.size(), is(1)); + + final URL url = catalogs.keySet().iterator().next(); + + assertThat(url.toString(), startsWith("file:/")); + assertThat(url.toString(), endsWith("/json-schema-catalog/catalog-core/target/test-classes/json/schema/schema_catalog.json")); + + final Catalog catalog = catalogs.get(url); + + assertThat(catalog.getName(), is("my catalog")); + assertThat(catalog.getGroup().size(), is(2)); + + assertThat(catalog.getGroup().get(0).getName(), is("standards")); + assertThat(catalog.getGroup().get(0).getBaseLocation(), is("/json/schema/standards")); + + assertThat(catalog.getGroup().get(0).getSchema().size(), is(2)); + assertThat(catalog.getGroup().get(0).getSchema().get(0).getId(), is("http://justice.gov.uk/standards/complex_address.json")); + assertThat(catalog.getGroup().get(0).getSchema().get(0).getLocation(), is("complex_address.json")); + assertThat(catalog.getGroup().get(0).getSchema().get(1).getId(), is("http://justice.gov.uk/standards/address.json")); + assertThat(catalog.getGroup().get(0).getSchema().get(1).getLocation(), is("address.json")); + + assertThat(catalog.getGroup().get(1).getName(), is("staging interface")); + assertThat(catalog.getGroup().get(1).getBaseLocation(), is(CoreMatchers.nullValue())); + + assertThat(catalog.getGroup().get(1).getSchema().size(), is(1)); + assertThat(catalog.getGroup().get(1).getSchema().get(0).getId(), is("http://justice.gov.uk/context/person.json")); + assertThat(catalog.getGroup().get(1).getSchema().get(0).getLocation(), is("/json/schema/context/person.json")); + } +} diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoaderTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoaderTest.java new file mode 100644 index 0000000..e8b3432 --- /dev/null +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/ClasspathCatalogLoaderTest.java @@ -0,0 +1,72 @@ +package uk.gov.justice.schema.catalog; + +import static java.util.Collections.singletonList; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.junit.Assert.assertThat; +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.util.ClasspathResourceLoader; + +import java.io.IOException; +import java.net.URI; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class ClasspathCatalogLoaderTest { + + @Mock + private ObjectMapper objectMapper; + + @Mock + private ClasspathResourceLoader classpathResourceLoader; + + @InjectMocks + private ClasspathCatalogLoader classpathCatalogLoader; + + + @Test + public void shouldThrowExceptionIfLoadingFileThrowsIOException() throws Exception { + + final IOException ioException = new IOException("Ooops"); + + final URI uri = new URI("file://src/code/my-file.txt"); + + + when(classpathResourceLoader.getResources(ClasspathCatalogLoader.class, "json/schema/schema_catalog.json")).thenReturn(singletonList(uri)); + when(objectMapper.readValue(uri.toURL(), CatalogWrapper.class)).thenThrow(ioException); + + try { + classpathCatalogLoader.getCatalogs(); + fail(); + } catch (final SchemaCatalogException expected) { + assertThat(expected.getCause(), is(ioException)); + assertThat(expected.getMessage(), is("Failed to convert to json loaded from 'file://src/code/my-file.txt' to a Catalog pojo")); + } + } + + @Test + public void shouldThrowExceptionIfLoadingResourcesThrowsIOException() throws Exception { + + final IOException ioException = new IOException("Ooops"); + + when(classpathResourceLoader.getResources(ClasspathCatalogLoader.class, "json/schema/schema_catalog.json")).thenThrow(ioException); + + try { + classpathCatalogLoader.getCatalogs(); + fail(); + } catch (final Exception expected) { + assertThat(expected.getCause(), is(ioException)); + assertThat(expected.getMessage(), startsWith("Failed to load the catalogs from the classpath for location 'json/schema/schema_catalog.json'")); + + } + } +} diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/FileContentsAsStringLoaderTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/FileContentsAsStringLoaderTest.java new file mode 100644 index 0000000..4e0022f --- /dev/null +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/FileContentsAsStringLoaderTest.java @@ -0,0 +1,60 @@ +package uk.gov.justice.schema.catalog; + +import static com.jayway.jsonassert.JsonAssert.with; +import static org.hamcrest.CoreMatchers.endsWith; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.FileNotFoundException; +import java.net.URL; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class FileContentsAsStringLoaderTest { + + @InjectMocks + private FileContentsAsStringLoader fileContentsAsStringLoader; + + @Test + public void shouldLoadADocumentAsAString() throws Exception { + + final URL url = new File("src/test/resources/json/schema/context/person.json").toURI().toURL(); + + final String json = fileContentsAsStringLoader.readFileContents(url); + + assertThat(json, is(notNullValue())); + + with(json) + .assertThat("$.$schema", is("http://json-schema.org/draft-04/schema#")) + .assertThat("$.id", is("http://justice.gov.uk/context/person.json")) + .assertThat("$.type", is("object")) + .assertThat("$.properties.nino.type", is("string")) + .assertThat("$.properties.name.type", is("string")) + .assertThat("$.properties.correspondence_address.$ref", is("http://justice.gov.uk/standards/complex_address.json#/definitions/complex_address2")) + ; + } + + @Test + public void shouldFailIfReadingTheFileThrowsAnIOException() throws Exception { + + final URL url = new File("this/file/does/not/exist.json").toURI().toURL(); + + try { + fileContentsAsStringLoader.readFileContents(url); + fail(); + } catch (final SchemaCatalogException expected) { + assertThat(expected.getCause(), is(instanceOf(FileNotFoundException.class))); + assertThat(expected.getMessage(), startsWith("Failed to read file contents from 'file:")); + assertThat(expected.getMessage(), endsWith("this/file/does/not/exist.json'")); + } + } +} diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/JsonSchemaLoaderTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/JsonSchemaLoaderTest.java new file mode 100644 index 0000000..725d54a --- /dev/null +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/JsonSchemaLoaderTest.java @@ -0,0 +1,45 @@ +package uk.gov.justice.schema.catalog; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.when; + +import java.net.URL; +import java.util.Map; + +import com.google.common.collect.ImmutableMap; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class JsonSchemaLoaderTest { + + @Mock + private FileContentsAsStringLoader fileContentsAsStringLoader; + + @InjectMocks + private JsonSchemaLoader jsonSchemaLoader; + + @Test + public void shouldName() throws Exception { + + final URL url_1 = new URL("file:/my/json/schema.json"); + final URL url_2 = new URL("file:/my/other/json/schema.json"); + + final String json_1 = "{\"some\": \"json\"}"; + final String json_2 = "{\"other\": \"json\"}"; + + final Map schemaLocationMap = ImmutableMap.of("id_1", url_1, "id_2", url_2); + + when(fileContentsAsStringLoader.readFileContents(url_1)).thenReturn(json_1); + when(fileContentsAsStringLoader.readFileContents(url_2)).thenReturn(json_2); + + final Map idsToJson = jsonSchemaLoader.loadJsonFrom(schemaLocationMap); + + assertThat(idsToJson.get("id_1"), is(json_1)); + assertThat(idsToJson.get("id_2"), is(json_2)); + } +} diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/SchemaResolverAndLoaderTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/SchemaResolverAndLoaderTest.java new file mode 100644 index 0000000..39762f1 --- /dev/null +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/SchemaResolverAndLoaderTest.java @@ -0,0 +1,78 @@ +package uk.gov.justice.schema.catalog; + +import static com.google.common.collect.ImmutableMap.of; +import static com.jayway.jsonassert.JsonAssert.with; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import uk.gov.justice.schema.client.LocalFileSystemSchemaClient; + +import java.util.Map; + +import org.everit.json.schema.Schema; +import org.everit.json.schema.loader.SchemaClient; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Spy; +import org.mockito.runners.MockitoJUnitRunner; + + +@RunWith(MockitoJUnitRunner.class) +public class SchemaResolverAndLoaderTest { + + private static final String SCHEMA_1 = + "{\n" + + " \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n" + + " \"id\": \"http://justice.gov.uk/context/person.json\",\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"name\": {\n" + + " \"type\": \"string\"\n" + + " }\n" + + " }\n" + + "}\n"; + + private static final String SCHEMA_2 = + "{\n" + + " \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n" + + " \"id\": \"http://justice.gov.uk/context/person.json\",\n" + + " \"type\": \"object\",\n" + + " \"properties\": {\n" + + " \"age\": {\n" + + " \"type\": \"integer\"\n" + + " }\n" + + " }\n" + + "}\n"; + + @Spy @SuppressWarnings("unused") + private final JsonStringToSchemaConverter jsonStringToSchemaConverter = new JsonStringToSchemaConverter(); + + @InjectMocks + private SchemaResolverAndLoader schemaResolverAndLoader; + + @Test + public void shouldLoadSchemasAndMapByTheirIdsIntoTheSchemaDictonary() throws Exception { + + final String id_1 = "id_1"; + final String id_2 = "id_2"; + final Map idsToJson = of(id_1, SCHEMA_1, id_2, SCHEMA_2); + + final SchemaClient schemaClient = new LocalFileSystemSchemaClient(idsToJson); + + final Map idToSchemaMap = schemaResolverAndLoader.loadSchemas(idsToJson, schemaClient); + + assertThat(idToSchemaMap.size(), is(2)); + + final Schema schema_1 = idToSchemaMap.get(id_1); + final Schema schema_2 = idToSchemaMap.get(id_2); + + with(schema_1.toString()) + .assertThat("$.properties.name.type", is("string")) + ; + + with(schema_2.toString()) + .assertThat("$.properties.age.type", is("integer")) + ; + } +} diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/SchemaResolverTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/SchemaResolverTest.java new file mode 100644 index 0000000..72d5f5e --- /dev/null +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/SchemaResolverTest.java @@ -0,0 +1,54 @@ +package uk.gov.justice.schema.catalog; + +import static java.util.Optional.empty; +import static java.util.Optional.of; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import uk.gov.justice.schema.catalog.util.UrlConverter; + +import java.net.URL; +import java.util.Optional; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Spy; +import org.mockito.runners.MockitoJUnitRunner; + + +@RunWith(MockitoJUnitRunner.class) +public class SchemaResolverTest { + + @Spy @SuppressWarnings("unused") + private UrlConverter urlConverter = new UrlConverter(); + + @InjectMocks + private SchemaResolver schemaResolver; + + @Test + public void shouldResolveTheLocationToAUrlUsingTheCatalogUrlAndBaseLocation() throws Exception { + + final URL catalogUrl = new URL("file:/src/main/schema.json"); + + final String fileLocation = "some/path/to.json"; + final Optional baseLocation = of("base/location/"); + + final URL resolvedUrl = schemaResolver.resolve(catalogUrl, fileLocation, baseLocation); + + assertThat(resolvedUrl.toString(), is("file:/src/main/base/location/some/path/to.json")); + } + + @Test + public void shouldResolveTheLocationToAUrlUsingTheCatalogUrlAndAnEmptyBaseLocation() throws Exception { + + final URL catalogUrl = new URL("file:/src/main/schema.json"); + + final String fileLocation = "some/path/to.json"; + final Optional baseLocation = empty(); + + final URL resolvedUrl = schemaResolver.resolve(catalogUrl, fileLocation, baseLocation); + + assertThat(resolvedUrl.toString(), is("file:/src/main/some/path/to.json")); + } +} diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/util/ClasspathResourceLoaderTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/util/ClasspathResourceLoaderTest.java new file mode 100644 index 0000000..a3201be --- /dev/null +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/util/ClasspathResourceLoaderTest.java @@ -0,0 +1,26 @@ +package uk.gov.justice.schema.catalog.util; + +import static org.hamcrest.CoreMatchers.endsWith; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.net.URI; +import java.util.List; + +import org.junit.Test; + +public class ClasspathResourceLoaderTest { + + private final UrlConverter urlConverter = new UrlConverter(); + private final ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader(urlConverter); + + @Test + public void shouldLoadResourcesFromTheClasspath() throws Exception { + + final String filename = "json/random-classpath-resource.txt"; + final List resources = classpathResourceLoader.getResources(getClass(), filename); + + assertThat(resources.size(), is(1)); + assertThat(resources.get(0).toString(), endsWith(filename)); + } +} diff --git a/catalog-core/src/test/java/uk/gov/justice/schema/catalog/util/UrlConverterTest.java b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/util/UrlConverterTest.java new file mode 100644 index 0000000..f86e4e6 --- /dev/null +++ b/catalog-core/src/test/java/uk/gov/justice/schema/catalog/util/UrlConverterTest.java @@ -0,0 +1,38 @@ +package uk.gov.justice.schema.catalog.util; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.net.URI; +import java.net.URL; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + + +@RunWith(MockitoJUnitRunner.class) +public class UrlConverterTest { + + @InjectMocks + private UrlConverter urlConverter; + + @Test + public void shouldConvertAAurlToAnUri() throws Exception { + + final URL url = new URL("file://src/main/fred.txt"); + final URI uri = urlConverter.toUri(url); + + assertThat(uri.toString(), is("file://src/main/fred.txt")); + } + + @Test + public void shouldConvertAnUrlToAnUri() throws Exception { + + final URI uri = new URI("file://src/main/fred.txt"); + final URL url = urlConverter.toUrl(uri); + + assertThat(url.toString(), is("file://src/main/fred.txt")); + } +} diff --git a/everit-schema-client/src/test/resources/catalog_test_valid.json b/catalog-core/src/test/resources/catalog_test_valid.json similarity index 100% rename from everit-schema-client/src/test/resources/catalog_test_valid.json rename to catalog-core/src/test/resources/catalog_test_valid.json diff --git a/catalog-core/src/test/resources/json/random-classpath-resource.txt b/catalog-core/src/test/resources/json/random-classpath-resource.txt new file mode 100644 index 0000000..9d43527 --- /dev/null +++ b/catalog-core/src/test/resources/json/random-classpath-resource.txt @@ -0,0 +1 @@ +Stuff diff --git a/catalog-core/src/test/resources/json/schema/context/person.json b/catalog-core/src/test/resources/json/schema/context/person.json new file mode 100644 index 0000000..c27c64b --- /dev/null +++ b/catalog-core/src/test/resources/json/schema/context/person.json @@ -0,0 +1,21 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/context/person.json", + "type": "object", + "properties": { + "nino": { + "type": "string" + }, + "name": { + "type": "string" + }, + "correspondence_address": + {"$ref" : "http://justice.gov.uk/standards/complex_address.json#/definitions/complex_address2"} + + }, + "required": [ + "nino", + "name", + "correspondence_address" + ] +} diff --git a/everit-schema-client/src/test/resources/json/schema/schema_catalog.json b/catalog-core/src/test/resources/json/schema/schema_catalog.json similarity index 99% rename from everit-schema-client/src/test/resources/json/schema/schema_catalog.json rename to catalog-core/src/test/resources/json/schema/schema_catalog.json index 50a6c2f..a16272a 100644 --- a/everit-schema-client/src/test/resources/json/schema/schema_catalog.json +++ b/catalog-core/src/test/resources/json/schema/schema_catalog.json @@ -18,7 +18,6 @@ }, { "name": "staging interface", - "schema": [ { "id": "http://justice.gov.uk/context/person.json", @@ -28,4 +27,4 @@ } ] } -} \ No newline at end of file +} diff --git a/everit-schema-client/src/test/resources/json/schema/standards/address.json b/catalog-core/src/test/resources/json/schema/standards/address.json similarity index 100% rename from everit-schema-client/src/test/resources/json/schema/standards/address.json rename to catalog-core/src/test/resources/json/schema/standards/address.json diff --git a/everit-schema-client/src/test/resources/json/schema/standards/complex_address.json b/catalog-core/src/test/resources/json/schema/standards/complex_address.json similarity index 100% rename from everit-schema-client/src/test/resources/json/schema/standards/complex_address.json rename to catalog-core/src/test/resources/json/schema/standards/complex_address.json diff --git a/everit-schema-client/src/test/resources/json/schema/standards/defendant.json b/catalog-core/src/test/resources/json/schema/standards/defendant.json similarity index 100% rename from everit-schema-client/src/test/resources/json/schema/standards/defendant.json rename to catalog-core/src/test/resources/json/schema/standards/defendant.json diff --git a/schema-client-common/pom.xml b/catalog-domain/pom.xml similarity index 86% rename from schema-client-common/pom.xml rename to catalog-domain/pom.xml index be0e528..9c6399a 100644 --- a/schema-client-common/pom.xml +++ b/catalog-domain/pom.xml @@ -9,7 +9,7 @@ 4.0.0 - schema-client-common + catalog-domain @@ -21,11 +21,6 @@ org.glassfish javax.json - - uk.gov.justice.generators - pojo-generation-core - ${pojo-generator.version} - uk.gov.justice.utils utilities-core @@ -57,4 +52,4 @@ - \ No newline at end of file + diff --git a/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Catalog.java b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Catalog.java new file mode 100644 index 0000000..10f86bc --- /dev/null +++ b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Catalog.java @@ -0,0 +1,23 @@ +package uk.gov.justice.schema.catalog.domain; + +import java.util.List; + +public class Catalog { + + private final List group; + private final String name; + + public Catalog(final List group, final String name) { + this.group = group; + this.name = name; + } + + public List getGroup() { + return group; + } + + public String getName() { + return name; + } + +} diff --git a/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/pojo/CatalogWrapper.java b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/CatalogWrapper.java similarity index 82% rename from schema-client-common/src/main/java/uk/gov/justice/schema/catalog/pojo/CatalogWrapper.java rename to catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/CatalogWrapper.java index ad3dd44..953e956 100644 --- a/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/pojo/CatalogWrapper.java +++ b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/CatalogWrapper.java @@ -1,4 +1,4 @@ -package uk.gov.justice.schema.catalog.pojo; +package uk.gov.justice.schema.catalog.domain; public class CatalogWrapper { diff --git a/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Group.java b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Group.java new file mode 100644 index 0000000..e937345 --- /dev/null +++ b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Group.java @@ -0,0 +1,28 @@ +package uk.gov.justice.schema.catalog.domain; + +import java.util.List; + +public class Group { + + private final String baseLocation; + private final String name; + private final List schema; + + public Group(final String baseLocation, final String name, final List schema) { + this.baseLocation = baseLocation; + this.name = name; + this.schema = schema; + } + + public String getBaseLocation() { + return baseLocation; + } + + public String getName() { + return name; + } + + public List getSchema() { + return schema; + } +} diff --git a/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Schema.java b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Schema.java new file mode 100644 index 0000000..9003b86 --- /dev/null +++ b/catalog-domain/src/main/java/uk/gov/justice/schema/catalog/domain/Schema.java @@ -0,0 +1,20 @@ +package uk.gov.justice.schema.catalog.domain; + +public class Schema { + private final String id; + + private final String location; + + public Schema(final String id, final String location) { + this.id = id; + this.location = location; + } + + public String getId() { + return id; + } + + public String getLocation() { + return location; + } +} diff --git a/everit-schema-client/pom.xml b/everit-schema-client/pom.xml index 58f6890..47d31c8 100644 --- a/everit-schema-client/pom.xml +++ b/everit-schema-client/pom.xml @@ -13,42 +13,10 @@ everit-schema-client - - javax - javaee-api - provided - - - org.glassfish - javax.json - - - uk.gov.justice.schema - schema-client-common - ${project.version} - - - uk.gov.justice.generators - pojo-generation-core - ${pojo-generator.version} - - - uk.gov.justice.utils - utilities-core - ${utilities.version} - com.github.everit-org.json-schema org.everit.json.schema - - commons-io - commons-io - - - org.raml - raml-parser - @@ -61,6 +29,16 @@ mockito-core test + + com.jayway.jsonpath + json-path-assert + test + + + commons-io + commons-io + test + - \ No newline at end of file + diff --git a/everit-schema-client/src/main/java/uk/gov/justice/schema/catalog/BulkSchemaResolver.java b/everit-schema-client/src/main/java/uk/gov/justice/schema/catalog/BulkSchemaResolver.java deleted file mode 100644 index ff5056a..0000000 --- a/everit-schema-client/src/main/java/uk/gov/justice/schema/catalog/BulkSchemaResolver.java +++ /dev/null @@ -1,46 +0,0 @@ -package uk.gov.justice.schema.catalog; - -import java.io.InputStream; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class BulkSchemaResolver extends DictionarySchemaResolver { - - //private static SchemaDictionary schemaDictionary = new SchemaDictionary(); - - private static final Logger LOG = LoggerFactory.getLogger(BulkSchemaResolver.class); - private final SchemaDefinitionCache schemaDefinitionCache; - - public BulkSchemaResolver(SchemaDefinitionCache cache) { - this.schemaDefinitionCache = cache; - } - - @Override - public InputStream get(String url) { - - LOG.info("Resolving schema: {}", url); - - //Resolve for other IDs and translations - //TODO - String id = url; - - if (schemaDefinitionCache.contains(id)) { - //being defined or re-defined - - //test for schema being re-defined - //if (schemaDictionary.contains(id)) - - LOG.info("Schema dependency found in the bulk definition cache", url); - - String jsonSchemaDefinition = schemaDefinitionCache.getSchema(id); - return convertStringToInputStream(jsonSchemaDefinition); - - - } else { - return super.get(url); - } - - } - -} diff --git a/everit-schema-client/src/main/java/uk/gov/justice/schema/catalog/CatalogLoader.java b/everit-schema-client/src/main/java/uk/gov/justice/schema/catalog/CatalogLoader.java deleted file mode 100644 index 7c2160f..0000000 --- a/everit-schema-client/src/main/java/uk/gov/justice/schema/catalog/CatalogLoader.java +++ /dev/null @@ -1,271 +0,0 @@ -package uk.gov.justice.schema.catalog; - -import static java.lang.String.format; - -import uk.gov.justice.schema.catalog.pojo.Catalog; -import uk.gov.justice.schema.catalog.pojo.CatalogWrapper; -import uk.gov.justice.schema.catalog.pojo.Group; -import uk.gov.justice.schema.catalog.pojo.Schema; -import uk.gov.justice.services.common.converter.jackson.ObjectMapperProducer; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; -import java.nio.charset.Charset; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.inject.Inject; - -import org.apache.commons.io.IOUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -public class CatalogLoader { - - private static final String JSON_CATALOG_LOCATION = "json/schema/schema_catalog.json"; - - private static final Logger LOG = LoggerFactory.getLogger(CatalogLoader.class); - private static final String PROTOCOL_FILE = "file"; - private static final String PROTOCOL_JAR = "jar"; - private static final String PROTOCOL_VFS = "vfs"; - - - private final ObjectMapperProducer objectMapperProducer = new ObjectMapperProducer(); - - @Inject - SchemaLoader schemaLoader; - - protected List listAllCatalogsFromClasspath() { - Enumeration urlEnumeration = null; - List urls = null; - - LOG.info(format("Listing all schema definition files matching: %s", JSON_CATALOG_LOCATION)); - - try { - urlEnumeration = CatalogLoader.class.getClassLoader().getResources(JSON_CATALOG_LOCATION); - urls = enumTOList(urlEnumeration); - - } catch (IOException e) { - LOG.error("Failed to list the catalogs from the classpath", e); - - } - return urls; - } - - - public void loadCatalogsFromClasspath() { - //list all catalogs - List catalogURLs = listAllCatalogsFromClasspath(); - - if (catalogURLs == null || catalogURLs.isEmpty()) { - LOG.info("No catalog files found in the classpath"); - return; - - } - - loadCatalogs(catalogURLs); - } - - public void loadCatalog(URL url) { - List list = new ArrayList(); - list.add(url); - loadCatalogs(list); - } - - protected void loadCatalogs(List catalogURLs) { - - if (catalogURLs == null || catalogURLs.isEmpty()) { - LOG.info("No catalog files found in the classpath"); - return; - - } - - //read catalogs and create a dictionary of POJOs - Map catalogPojoMap = readCatalogsAndValidate(catalogURLs); - - if (catalogPojoMap.isEmpty()) { - LOG.info("No catalogs were read"); - return; - } - - Map schemaLocationMap = new HashMap<>(); - for (URL url : catalogPojoMap.keySet()) { - Catalog catalog = catalogPojoMap.get(url); - LOG.info("URL (\"{}\") defines catalog (\"{}\")", url.toString(), catalog.getName()); - - LOG.info("Protocol of the url is: {}", url.getProtocol()); - LOG.info("{}", url.getFile()); - - - for (Group group : catalog.getGroup()) { - LOG.info("Catalog {} defines {}", catalog.getName(), group.getName()); - - //base location for the groups - String baseLocation = group.getBaseLocation(); - boolean isBaseLocationDefined = true; - //not doing much with this variable here - if (baseLocation == null || baseLocation.equals("")) { - isBaseLocationDefined = false; - } - for (Schema schema : - group.getSchema()) { - - LOG.info("Found schema catalog definition id: {}", schema.getId()); - - //TODO - //(Feature) Translate the URL to an absolute URL - //URL newURL = new URL() - URL resolvedURL = null; - try { - //TODO fix the resolution of the urls - resolvedURL = resolveLocation(url, group.getBaseLocation(), schema.getLocation()); - } catch (MalformedURLException e) { - LOG.error(format("Failed to generate url for %s", url.toString(), baseLocation, schema.getLocation()), e); - break; - } - - - schemaLocationMap.put(schema.getId(), resolvedURL); - - } - } - } - - - //obtain the id -> jar + location mapping for each schema - - - schemaLoader.bulkLoadSchemas(schemaLocationMap); - - //load schema - - - } - - - private URL resolveLocation(URL catalogUrl, String baseLocation, String location) throws MalformedURLException { - - URL url = null; - - boolean ignoreBaseLocation = false; - if (baseLocation == null || baseLocation.equals("")) { - ignoreBaseLocation = true; - } - - File file = new File(location); - if (file.getAbsolutePath().equals(location)) { - ignoreBaseLocation = true; - } - - if (!ignoreBaseLocation) { - file = new File(baseLocation, location); - } - - if (catalogUrl.getProtocol().toLowerCase().equals(PROTOCOL_FILE)) { - url = new URL(PROTOCOL_FILE + ":" + file.getAbsolutePath()); - } else if (catalogUrl.getProtocol().toLowerCase().equals(PROTOCOL_JAR)) { - String jarFilename = extractJarName(catalogUrl.getFile()); - url = new URL(PROTOCOL_JAR + ":" + PROTOCOL_FILE + ":" + jarFilename + "!" + file.getAbsolutePath()); - } else if (catalogUrl.getProtocol().toLowerCase().equals(PROTOCOL_VFS)) { - - String jarFilename = extractVFSContextName(catalogUrl.getFile()); - url = new URL(PROTOCOL_VFS + ":" + jarFilename + file.getAbsolutePath()); - } else { - return new URL("localhost:80892/UNDEFINED"); - } - - LOG.info("URL translated into {}", url.toString()); - - return url; - } - - private String extractVFSContextName(String file) { - final String searchIndex = ".jar"; - - int start = 0; - int end = file.toLowerCase().indexOf(searchIndex, start); - return file.substring(start, end + searchIndex.length()); - } - - public static String extractJarName(String jarURL) { - final String searchIndex = PROTOCOL_FILE + ":"; - - int start = jarURL.toLowerCase().indexOf(searchIndex); - int end = jarURL.indexOf('!', start); - return jarURL.substring(start + searchIndex.length(), end); - - } - - protected Map readCatalogsAndValidate(List catalogURLs) { - Map map = new HashMap<>(); - - for (URL url : catalogURLs) { - - String jsonString = null; - InputStream in = null; - try { - LOG.info("Attempting to read file {}", url); - in = url.openStream(); - jsonString = IOUtils.toString(in, Charset.defaultCharset()); - - //File file = new File(url.toURI()); - //jsonString = FileUtils.readFileToString(file, Charset.defaultCharset()); - - } catch (IOException e) { - LOG.error(format("Failed to read %s", url.toString()), e); - break; - } finally { - IOUtils.closeQuietly(in); - } - try { - if (jsonString != null) { - Catalog catalog = objectMapperProducer.objectMapper().readValue(jsonString, CatalogWrapper.class).getCatalog(); - - //add the validation step here - map.put(url, catalog); - } - } catch (IOException e) { - LOG.error(format("Failed to convert to a Catalog Pojo: %s", jsonString), e); - } - - - } - - return map; - } - - /** - * Validates the catalogs and returns a list of validated catalogs - */ - private List validateCatalogs(List catalogJsonContents) { - return catalogJsonContents; - - } - - private List enumTOList(Enumeration e) { - List list = new ArrayList(); - while (e.hasMoreElements()) - list.add(e.nextElement()); - return list; - } - - - public static void main(String args[]) throws URISyntaxException - - { - CatalogLoader catalogLoader = new CatalogLoader(); - catalogLoader.loadCatalogsFromClasspath(); - List urls = catalogLoader.listAllCatalogsFromClasspath(); - Paths.get(urls.get(0).toURI()).getFileName(); - } - -} diff --git a/everit-schema-client/src/main/java/uk/gov/justice/schema/catalog/DictionarySchemaResolver.java b/everit-schema-client/src/main/java/uk/gov/justice/schema/catalog/DictionarySchemaResolver.java deleted file mode 100644 index fa79f27..0000000 --- a/everit-schema-client/src/main/java/uk/gov/justice/schema/catalog/DictionarySchemaResolver.java +++ /dev/null @@ -1,50 +0,0 @@ -package uk.gov.justice.schema.catalog; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; - -import org.everit.json.schema.Schema; -import org.everit.json.schema.loader.SchemaClient; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class DictionarySchemaResolver implements SchemaClient { - - protected static final SchemaDictionary schemaDictionary = new SchemaDictionary(); - - private static final Logger LOG = LoggerFactory.getLogger(DictionarySchemaResolver.class); - - @Override - public InputStream get(String url) { - - LOG.info("Resolving schema: %s", url); - - //Resolve for other IDs and translations - //TODO - String id = url; - - //simple resolve - Schema schema = schemaDictionary.getSchema(id); - - if (schema == null) { - LOG.info("Schema: {} not found in the dictionary", url); - return null; - } else { - LOG.info("Dictionary hit for schema: {}", url); - return convertStringToInputStream(schema.toString()); - } - - - } - - public static InputStream convertStringToInputStream(String schemaString) { - if (schemaString == null) { - return null; - } else { - //handle UTF and charsets - return new ByteArrayInputStream(schemaString.getBytes()); - } - } - - -} diff --git a/everit-schema-client/src/main/java/uk/gov/justice/schema/catalog/SchemaLoader.java b/everit-schema-client/src/main/java/uk/gov/justice/schema/catalog/SchemaLoader.java deleted file mode 100644 index cee9e30..0000000 --- a/everit-schema-client/src/main/java/uk/gov/justice/schema/catalog/SchemaLoader.java +++ /dev/null @@ -1,130 +0,0 @@ -package uk.gov.justice.schema.catalog; - -import static java.lang.String.format; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.charset.Charset; -import java.util.Map; - -import javax.annotation.PostConstruct; -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.context.Initialized; -import javax.enterprise.event.Observes; -import javax.inject.Inject; - -import org.apache.commons.io.IOUtils; -import org.everit.json.schema.Schema; -import org.everit.json.schema.loader.SchemaClient; -import org.json.JSONObject; -import org.json.JSONTokener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -public class SchemaLoader { - - @Inject - SchemaDictionary schemaDictionary; - - private static final Logger LOG = LoggerFactory.getLogger(SchemaLoader.class); - - private static final String UTF_8 = "UTF-8"; - - private static final Charset CHARSET_UTF8 = Charset.forName(UTF_8); - - private static final String PROTOCOL_FILE = "file"; - private static final String PROTOCOL_JAR = "jar"; - private static final String PROTOCOL_VFS = "vfs"; - - - - public void init(@Observes @Initialized(ApplicationScoped.class) Object init) { - } - - @PostConstruct - private void startup() { - final String message = "Application has started with resources %s"; - LOG.info(format(message, schemaDictionary)); - } - - public String readSchema(URL url) throws IOException { - - InputStream in = null; - String schemaString = null; - try { - if (url.getProtocol().toLowerCase().equals(PROTOCOL_JAR) || url.getProtocol().toLowerCase().equals(PROTOCOL_VFS)) { - in = url.openStream(); - } else if (url.getProtocol().toLowerCase().equals(PROTOCOL_FILE)) { - in = SchemaLoader.class.getResourceAsStream(url.getFile()); - } - - - schemaString = IOUtils.toString(in, CHARSET_UTF8); - - } catch (IOException e) { - LOG.error(format("Failed to read the schema url: %s", url.toString()), e); - throw e; - } finally { - IOUtils.closeQuietly(in); - } - return schemaString; - } - - public void loadSchema(URL url) throws IOException { - String jsonSchema = readSchema(url); - SchemaClient resolver = new DictionarySchemaResolver(); - - - Schema schema = org.everit.json.schema.loader.SchemaLoader.builder() - - .schemaJson( - new JSONObject(new JSONTokener(jsonSchema))).httpClient(resolver) - .build().load().build(); - String id = schema.getId(); - schemaDictionary.putSchema(id, schema); - - } - - - public void bulkLoadSchemas(Map schemaLocationMap) { - if (schemaLocationMap == null || schemaLocationMap.isEmpty()) { - LOG.info("Bulk load didn't happen since the schema map given is empty"); - return; - } - SchemaDefinitionCache cache = new SchemaDefinitionCache(); - for (String id : schemaLocationMap.keySet() - ) { - try { - String jsonDefinition = readSchema(schemaLocationMap.get(id)); - cache.putSchema(id, jsonDefinition); - } catch (IOException e) { - cache.clear(); - return; - } - } - - processBulkLoadCache(cache); - - } - - private void processBulkLoadCache(SchemaDefinitionCache cache) { - LOG.info("Reference for schemaDictionary is: {}", schemaDictionary); - SchemaClient resolver = new BulkSchemaResolver(cache); - int count = 0; - for (String id : cache.getIDSet() - ) { - Schema schema = org.everit.json.schema.loader.SchemaLoader.builder() - - .schemaJson( - new JSONObject(new JSONTokener(cache.getSchema(id)))).httpClient(resolver) - .build().load().build(); - schemaDictionary.putSchema(id, schema); - count++; - } - LOG.info("Loaded {} schema(s) to the schema dictionary. Now contains {} schema(s)", count, schemaDictionary.size()); - cache.clear(); - } - -} diff --git a/everit-schema-client/src/main/java/uk/gov/justice/schema/client/LocalFileSystemSchemaClient.java b/everit-schema-client/src/main/java/uk/gov/justice/schema/client/LocalFileSystemSchemaClient.java new file mode 100644 index 0000000..cb27b97 --- /dev/null +++ b/everit-schema-client/src/main/java/uk/gov/justice/schema/client/LocalFileSystemSchemaClient.java @@ -0,0 +1,29 @@ +package uk.gov.justice.schema.client; + +import static java.lang.String.format; +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.Map; + +import org.everit.json.schema.loader.SchemaClient; + +public class LocalFileSystemSchemaClient implements SchemaClient { + + private final Map urlsToJson; + + public LocalFileSystemSchemaClient(final Map urlsToJson) { + this.urlsToJson = urlsToJson; + } + + @Override + public InputStream get(final String url) { + + if (urlsToJson.containsKey(url)) { + return new ByteArrayInputStream(urlsToJson.get(url).getBytes(UTF_8)); + } + + throw new SchemaClientException(format("Failed to find schema with id '%s'", url)); + } +} diff --git a/everit-schema-client/src/main/java/uk/gov/justice/schema/client/SchemaClientException.java b/everit-schema-client/src/main/java/uk/gov/justice/schema/client/SchemaClientException.java new file mode 100644 index 0000000..fe3ba6e --- /dev/null +++ b/everit-schema-client/src/main/java/uk/gov/justice/schema/client/SchemaClientException.java @@ -0,0 +1,8 @@ +package uk.gov.justice.schema.client; + +public class SchemaClientException extends RuntimeException { + + public SchemaClientException(final String message) { + super(message); + } +} diff --git a/everit-schema-client/src/main/java/uk/gov/justice/schema/client/SchemaClientFactory.java b/everit-schema-client/src/main/java/uk/gov/justice/schema/client/SchemaClientFactory.java new file mode 100644 index 0000000..924087b --- /dev/null +++ b/everit-schema-client/src/main/java/uk/gov/justice/schema/client/SchemaClientFactory.java @@ -0,0 +1,12 @@ +package uk.gov.justice.schema.client; + +import java.util.Map; + +import org.everit.json.schema.loader.SchemaClient; + +public class SchemaClientFactory { + + public SchemaClient create(final Map urlsToJson) { + return new LocalFileSystemSchemaClient(urlsToJson); + } +} diff --git a/everit-schema-client/src/test/java/uk/gov/justice/schema/catalog/AbstractTestHelper.java b/everit-schema-client/src/test/java/uk/gov/justice/schema/catalog/AbstractTestHelper.java deleted file mode 100644 index de7453d..0000000 --- a/everit-schema-client/src/test/java/uk/gov/justice/schema/catalog/AbstractTestHelper.java +++ /dev/null @@ -1,32 +0,0 @@ -package uk.gov.justice.schema.catalog; - -import java.io.File; - -import org.apache.commons.io.FileUtils; - -public abstract class AbstractTestHelper { - - public static String getFileContents(String path) throws Exception - { - - String parent = AbstractTestHelper.class.getResource("/").getPath(); - File file = new File(parent, path); - return FileUtils.readFileToString(file); - } - - public static File getFile(String path) throws Exception - { - - String parent = AbstractTestHelper.class.getResource("/").getPath(); - File file = new File(parent, path); - return file; - } - - public static String getFilepath(String path) throws Exception - { - - String parent = AbstractTestHelper.class.getResource("/").getPath(); - File file = new File(parent, path); - return file.getAbsolutePath(); - } -} diff --git a/everit-schema-client/src/test/java/uk/gov/justice/schema/catalog/CatalogLoaderTest.java b/everit-schema-client/src/test/java/uk/gov/justice/schema/catalog/CatalogLoaderTest.java deleted file mode 100644 index bc05165..0000000 --- a/everit-schema-client/src/test/java/uk/gov/justice/schema/catalog/CatalogLoaderTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package uk.gov.justice.schema.catalog; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; - - -//@RunWith(MockitoJUnitRunner.class) -@RunWith(MockitoJUnitRunner.class) -public class CatalogLoaderTest { - - @Mock - SchemaDictionary schemaDictionary; - - @Mock - SchemaLoader schemaLoader; - - - @InjectMocks - CatalogLoader catalogLoader; - - - @Test - public void shouldLoadCatalogs() throws Exception { - catalogLoader.loadCatalogsFromClasspath(); - - } -} \ No newline at end of file diff --git a/everit-schema-client/src/test/java/uk/gov/justice/schema/catalog/HelperMethodsTest.java b/everit-schema-client/src/test/java/uk/gov/justice/schema/catalog/HelperMethodsTest.java deleted file mode 100644 index 2556d41..0000000 --- a/everit-schema-client/src/test/java/uk/gov/justice/schema/catalog/HelperMethodsTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package uk.gov.justice.schema.catalog; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -public class HelperMethodsTest { - - @Test - public void testExtractJarName() { - String input = "jar:file:/Users/joao/projects/MOJ/CJSCommonPlatform/myserviceparent/schema-catalog/target/schema-catalog-1.0-SNAPSHOT.jar!/json/schema/schema_catalog.json"; - assertEquals("/Users/joao/projects/MOJ/CJSCommonPlatform/myserviceparent/schema-catalog/target/schema-catalog-1.0-SNAPSHOT.jar", CatalogLoader.extractJarName(input)); - - } - - -} diff --git a/everit-schema-client/src/test/java/uk/gov/justice/schema/catalog/JsonToPojoTest.java b/everit-schema-client/src/test/java/uk/gov/justice/schema/catalog/JsonToPojoTest.java deleted file mode 100644 index 694ebdf..0000000 --- a/everit-schema-client/src/test/java/uk/gov/justice/schema/catalog/JsonToPojoTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package uk.gov.justice.schema.catalog; - -import uk.gov.justice.schema.catalog.pojo.Catalog; -import uk.gov.justice.schema.catalog.pojo.CatalogWrapper; -import uk.gov.justice.services.common.converter.jackson.ObjectMapperProducer; - -import org.junit.Test; - - -//@RunWith(CDITestRunner) -public class JsonToPojoTest { - - private static final String JSON_EXAMPLES_VALIDATE_DIR = "/"; - - @Test - public void testSimpleCatalogLoad() throws Exception { - String jsonString = AbstractTestHelper.getFileContents(JSON_EXAMPLES_VALIDATE_DIR + "catalog_test_valid.json"); -// JsonObjectToObjectConverter jsonConverter = new JsonObjectToObjectConverter(); -// // JsonObject j = new JsonObject() -// final JSONObject jsonObject = new JSONObject(jsonString); -// jsonConverter.convert((JsonObject) jsonObject, Catalog.class); - -// ObjectMapper objectMapper = new ObjectMapper(); -// Catalog catalog = objectMapper.readValue(jsonString, Catalog.class); - - ObjectMapperProducer objectMapperProducer = new ObjectMapperProducer(); - Catalog catalog = objectMapperProducer.objectMapper().readValue(jsonString, CatalogWrapper.class).getCatalog(); - - System.out.println(catalog.getName()); - System.out.println(catalog.getGroup().get(0).getName()); - - - } - - -} diff --git a/everit-schema-client/src/test/java/uk/gov/justice/schema/client/LocalFileSystemSchemaClientTest.java b/everit-schema-client/src/test/java/uk/gov/justice/schema/client/LocalFileSystemSchemaClientTest.java new file mode 100644 index 0000000..3557c95 --- /dev/null +++ b/everit-schema-client/src/test/java/uk/gov/justice/schema/client/LocalFileSystemSchemaClientTest.java @@ -0,0 +1,53 @@ +package uk.gov.justice.schema.client; + +import static com.google.common.collect.ImmutableMap.of; +import static com.jayway.jsonassert.JsonAssert.with; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; + +public class LocalFileSystemSchemaClientTest { + + @Test + public void shouldFindSchemaJsonByItsUrlIdAndReturnAsAnInputStream() throws Exception { + + final Map urlsToJson = of("url", "{\"some\": \"json\"}"); + + final LocalFileSystemSchemaClient localFileSystemSchemaClient = new LocalFileSystemSchemaClient(urlsToJson); + + final InputStream inputStream = localFileSystemSchemaClient.get("url"); + + final String json = IOUtils.toString(inputStream, UTF_8); + + with(json) + .assertThat("$.some", is("json")) + ; + + inputStream.close(); + } + + @Test + public void shouldFailIfNoSchemaFoundForTheUrlId() throws Exception { + + final String url = "file:/some/url/or/other"; + + final Map urlsToJson = new HashMap<>(); + + final LocalFileSystemSchemaClient localFileSystemSchemaClient = new LocalFileSystemSchemaClient(urlsToJson); + + try { + localFileSystemSchemaClient.get(url); + fail(); + } catch (final SchemaClientException expected) { + assertThat(expected.getMessage(), is("Failed to find schema with id 'file:/some/url/or/other'")); + } + } +} diff --git a/everit-schema-client/src/test/java/uk/gov/justice/schema/client/SchemaClientFactoryTest.java b/everit-schema-client/src/test/java/uk/gov/justice/schema/client/SchemaClientFactoryTest.java new file mode 100644 index 0000000..9e5dd59 --- /dev/null +++ b/everit-schema-client/src/test/java/uk/gov/justice/schema/client/SchemaClientFactoryTest.java @@ -0,0 +1,37 @@ +package uk.gov.justice.schema.client; + +import static com.google.common.collect.ImmutableMap.of; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.lang.reflect.Field; +import java.util.Map; + +import org.everit.json.schema.loader.SchemaClient; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class SchemaClientFactoryTest { + + @InjectMocks + private SchemaClientFactory schemaClientFactory; + + @Test + public void shouldCreateANewLocalFileSystemSchemaClient() throws Exception { + + final Map urlsToJson = of("id", "{\"some\": \"json\"}"); + + final SchemaClient schemaClient = schemaClientFactory.create(urlsToJson); + + assertThat(schemaClient, is(instanceOf(LocalFileSystemSchemaClient.class))); + + final Field idsToJsonField = schemaClient.getClass().getDeclaredField("urlsToJson"); + idsToJsonField.setAccessible(true); + + assertThat(idsToJsonField.get(schemaClient), is(urlsToJson)); + } +} diff --git a/integration-test/pom.xml b/integration-test/pom.xml new file mode 100644 index 0000000..02193fa --- /dev/null +++ b/integration-test/pom.xml @@ -0,0 +1,45 @@ + + + + json-schema-catalog + uk.gov.justice.schema + 1.0.0-SNAPSHOT + + 4.0.0 + + integration-test + + + + + uk.gov.justice.schema + catalog-core + ${project.version} + test + + + uk.gov.justice.utils + utilities-core + ${utilities.version} + test + + + junit + junit + test + + + org.mockito + mockito-core + test + + + com.jayway.jsonpath + json-path-assert + test + + + + diff --git a/integration-test/src/test/java/uk/gov/justice/schema/catalog/CatalogLoaderIT.java b/integration-test/src/test/java/uk/gov/justice/schema/catalog/CatalogLoaderIT.java new file mode 100644 index 0000000..d252891 --- /dev/null +++ b/integration-test/src/test/java/uk/gov/justice/schema/catalog/CatalogLoaderIT.java @@ -0,0 +1,77 @@ +package uk.gov.justice.schema.catalog; + +import static com.jayway.jsonassert.JsonAssert.with; +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.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 ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader(urlConverter); + private final ClasspathCatalogLoader classpathCatalogLoader = new ClasspathCatalogLoader(objectMapper, classpathResourceLoader); + private final SchemaResolver schemaResolver = new SchemaResolver(urlConverter); + 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 idsToSchemaMap = catalogLoader.loadCatalogsFromClasspath(); + + assertThat(idsToSchemaMap.size(), is(3)); + + final String id_1 = "http://justice.gov.uk/standards/address.json"; + final String json_1 = idsToSchemaMap.get(id_1).toString(); + + with(json_1) + .assertThat("$.id", is(id_1)) + .assertThat("$.type", is("object")) + .assertThat("$.properties.city.type", is("string")) + .assertThat("$.properties.postcode.type", is("string")) + .assertThat("$.properties.addressline1.type", is("string")) + .assertThat("$.properties.addressline2.type", is("string")) + ; + + final String id_2 = "http://justice.gov.uk/context/person.json"; + final String json_2 = idsToSchemaMap.get(id_2).toString(); + + with(json_2) + .assertThat("$.id", is(id_2)) + .assertThat("$.type", is("object")) + .assertThat("$.properties.correspondence_address.$ref", is("http://justice.gov.uk/standards/complex_address.json#/definitions/complex_address2")) + .assertThat("$.properties.name.type", is("string")) + .assertThat("$.properties.nino.type", is("string")) + ; + + final String id_3 = "http://justice.gov.uk/standards/complex_address.json"; + final String json_3 = idsToSchemaMap.get(id_3).toString(); + + with(json_3) + .assertThat("$.id", is(id_3)) + .assertThat("$.allOf[0].type", is("object")) + .assertThat("$.allOf[1].allOf[0].$ref", is("#/definitions/complex_address")) + ; + } +} diff --git a/everit-schema-client/src/test/resources/json/schema/context/person.json b/integration-test/src/test/resources/json/schema/context/person.json similarity index 100% rename from everit-schema-client/src/test/resources/json/schema/context/person.json rename to integration-test/src/test/resources/json/schema/context/person.json diff --git a/integration-test/src/test/resources/json/schema/schema_catalog.json b/integration-test/src/test/resources/json/schema/schema_catalog.json new file mode 100644 index 0000000..908c942 --- /dev/null +++ b/integration-test/src/test/resources/json/schema/schema_catalog.json @@ -0,0 +1,30 @@ +{ + "catalog": { + "name": "my catalog", + "group": [ + { + "name": "staging interface", + "schema": [ + { + "id": "http://justice.gov.uk/context/person.json", + "location": "context/person.json" + } + ] + }, + { + "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" + } + ] + } + ] + } +} diff --git a/integration-test/src/test/resources/json/schema/standards/address.json b/integration-test/src/test/resources/json/schema/standards/address.json new file mode 100644 index 0000000..eea5825 --- /dev/null +++ b/integration-test/src/test/resources/json/schema/standards/address.json @@ -0,0 +1,25 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/standards/address.json", + "type": "object", + "properties": { + "addressline1": { + "type": "string" + }, + "addressline2": { + "type": "string" + }, + "city": { + "type": "string" + }, + "postcode": { + "type": "string" + } + }, + "required": [ + "addressline1", + "city", + "postcode" + ] + +} \ No newline at end of file diff --git a/integration-test/src/test/resources/json/schema/standards/complex_address.json b/integration-test/src/test/resources/json/schema/standards/complex_address.json new file mode 100644 index 0000000..290bf06 --- /dev/null +++ b/integration-test/src/test/resources/json/schema/standards/complex_address.json @@ -0,0 +1,65 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "id": "http://justice.gov.uk/standards/complex_address.json", + "type": "object", + "definitions": { + "complex_address": { + "type": "object", + "properties": { + "addressline1": { + "type": "string" + }, + "addressline2": { + "type": "string" + }, + "city": { + "type": "string" + }, + "postcode": { + "type": "string" + }, + "country": { + "type": "string" + } + }, + "required": [ + "addressline1", + "city", + "postcode", + "country" + ] + }, + "complex_address2": { + "type": "object", + "properties": { + "addressline1": { + "type": "string" + }, + "addressline2": { + "type": "string" + }, + "city": { + "type": "string" + }, + "postcode": { + "type": "string" + }, + "ss": { + "type": "string" + } + }, + "required": [ + "addressline1", + "city", + "postcode", + "ss" + ] + } + }, + "allOf": [ + { + "$ref": "#/definitions/complex_address" + } + ] +} + diff --git a/integration-test/src/test/resources/json/schema/standards/defendant.json b/integration-test/src/test/resources/json/schema/standards/defendant.json new file mode 100644 index 0000000..a90e8d8 --- /dev/null +++ b/integration-test/src/test/resources/json/schema/standards/defendant.json @@ -0,0 +1,22 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "personalInfo": { + "type": "string" + }, + "currentAddress": { + "type": "string" + }, + "correspondenceAddress": { + "type": "string" + }, + "courtCentreId": { + "type": "string" + } + }, + "required": [ + "personalInfo", + "currentAddress" + ] +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 7b8fac7..29a0cce 100644 --- a/pom.xml +++ b/pom.xml @@ -16,15 +16,16 @@ pom - schema-client-common + catalog-domain everit-schema-client + integration-test + catalog-core json-schema-catalog 1.21.0 1.10.0 - 1.0.0 @@ -45,4 +46,4 @@ - \ No newline at end of file + diff --git a/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/SchemaDefinitionCache.java b/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/SchemaDefinitionCache.java deleted file mode 100644 index ebc6deb..0000000 --- a/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/SchemaDefinitionCache.java +++ /dev/null @@ -1,37 +0,0 @@ -package uk.gov.justice.schema.catalog; - -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - - -class SchemaDefinitionCache { - - private Map schemas = new ConcurrentHashMap(); - - - public boolean contains(String id) { - return this.schemas.containsKey(id); - } - - public String getSchema(String id) { - return this.schemas.get(id); - } - - public void putSchema(String id, String schema) { - this.schemas.put(id, schema); - } - - public void clear() { - this.schemas.clear(); - } - - public Set getIDSet() { - return this.schemas.keySet(); - } - - public int size() - { - return this.schemas.size(); - } -} diff --git a/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/SchemaDictionary.java b/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/SchemaDictionary.java deleted file mode 100644 index 104dcfb..0000000 --- a/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/SchemaDictionary.java +++ /dev/null @@ -1,43 +0,0 @@ -package uk.gov.justice.schema.catalog; - -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -import javax.enterprise.context.ApplicationScoped; - -import org.everit.json.schema.Schema; - -@ApplicationScoped -public class SchemaDictionary { - - - private Map schemas = new ConcurrentHashMap(); - - - public boolean contains(String id) { - return this.schemas.containsKey(id); - } - - public Schema getSchema(String id) { - return this.schemas.get(id); - } - - public void putSchema(String id, Schema schema) { - this.schemas.put(id, schema); - } - - public void clear() { - this.schemas.clear(); - } - - public Set getIDSet() { - return this.schemas.keySet(); - } - - public int size() - { - return this.schemas.size(); - } - -} diff --git a/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/pojo/Catalog.java b/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/pojo/Catalog.java deleted file mode 100644 index 84984ee..0000000 --- a/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/pojo/Catalog.java +++ /dev/null @@ -1,46 +0,0 @@ -package uk.gov.justice.schema.catalog.pojo; - -import java.util.List; - -public class Catalog { - private final List group; - - private final String name; - - public Catalog(final List group, final String name) { - this.group = group; - this.name = name; - } - - public List getGroup() { - return group; - } - - public String getName() { - return name; - } - - public static Builder catalog() { - return new Builder(); - } - - public static class Builder { - private List group; - - private String name; - - public Builder withGroup(final List group) { - this.group = group; - return this; - } - - public Builder withName(final String name) { - this.name = name; - return this; - } - - public Catalog build() { - return new Catalog(group, name); - } - } -} diff --git a/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/pojo/Group.java b/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/pojo/Group.java deleted file mode 100644 index ee7bbad..0000000 --- a/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/pojo/Group.java +++ /dev/null @@ -1,60 +0,0 @@ -package uk.gov.justice.schema.catalog.pojo; - -import java.util.List; - -public class Group { - private final String baseLocation; - - private final String name; - - private final List schema; - - public Group(final String baseLocation, final String name, final List schema) { - this.baseLocation = baseLocation; - this.name = name; - this.schema = schema; - } - - public String getBaseLocation() { - return baseLocation; - } - - public String getName() { - return name; - } - - public List getSchema() { - return schema; - } - - public static Builder group() { - return new Builder(); - } - - public static class Builder { - private String baseLocation; - - private String name; - - private List schema; - - public Builder withBaseLocation(final String baseLocation) { - this.baseLocation = baseLocation; - return this; - } - - public Builder withName(final String name) { - this.name = name; - return this; - } - - public Builder withSchema(final List schema) { - this.schema = schema; - return this; - } - - public Group build() { - return new Group(baseLocation, name, schema); - } - } -} diff --git a/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/pojo/Schema.java b/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/pojo/Schema.java deleted file mode 100644 index 3028258..0000000 --- a/schema-client-common/src/main/java/uk/gov/justice/schema/catalog/pojo/Schema.java +++ /dev/null @@ -1,44 +0,0 @@ -package uk.gov.justice.schema.catalog.pojo; - -public class Schema { - private final String id; - - private final String location; - - public Schema(final String id, final String location) { - this.id = id; - this.location = location; - } - - public String getId() { - return id; - } - - public String getLocation() { - return location; - } - - public static Builder schema() { - return new Builder(); - } - - public static class Builder { - private String id; - - private String location; - - public Builder withId(final String id) { - this.id = id; - return this; - } - - public Builder withLocation(final String location) { - this.location = location; - return this; - } - - public Schema build() { - return new Schema(id, location); - } - } -}