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

Commit

Permalink
Change schema location
Browse files Browse the repository at this point in the history
  • Loading branch information
amckenzie committed Dec 14, 2017
1 parent 0209106 commit bfd56fe
Show file tree
Hide file tree
Showing 27 changed files with 191 additions and 231 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package uk.gov.justice.schema.catalog;

/**
* Constants used in the Catalog generation
*/
public class CatalogContext {

private static final String CATALOG_LOCATION = "META-INF/";
private static final String CATALOG_FILENAME = "schema_catalog.json";

public static final String AN_EMPTY_STRING = "";

/**
* @return The file name of the json catalog.
*/
public String getCatalogFilename() {
return CATALOG_FILENAME;
}

/**
* @return The location of the json catalog.
*/
public String getCatalogLocation() {
return CATALOG_LOCATION;
}

/**
* @return The full path of the json catalog.
*/
public String getCatalogFullPath() {
return CATALOG_LOCATION + CATALOG_FILENAME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,21 @@ public ClasspathResourceLoader classpathResourceLoader() {
return new ClasspathResourceLoader();
}

/**
* @return a new instance of {@link CatalogContext}
*/
public CatalogContext catalogContext() {
return new CatalogContext();
}

/**
* @return a new instance of {@link ClasspathCatalogLoader}
*/
public ClasspathCatalogLoader classpathCatalogLoader() {
return new ClasspathCatalogLoader(
objectMapper(),
classpathResourceLoader(),
catalogContext(),
urlConverter());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@
*/
public class ClasspathCatalogLoader {

private static final String DEFAULT_JSON_CATALOG_LOCATION = "json/schema/schema_catalog.json";

private final ObjectMapper objectMapper;
private final ClasspathResourceLoader classpathResourceLoader;
private final CatalogContext catalogContext;
private final UrlConverter urlConverter;

public ClasspathCatalogLoader(final ObjectMapper objectMapper,
final ClasspathResourceLoader classpathResourceLoader,
final CatalogContext catalogContext,
final UrlConverter urlConverter) {
this.objectMapper = objectMapper;
this.classpathResourceLoader = classpathResourceLoader;
this.catalogContext = catalogContext;
this.urlConverter = urlConverter;
}

Expand All @@ -55,10 +56,11 @@ private Catalog loadCatalog(final URL catalogUrl) {
}

private List<URL> listAllCatalogsFromClasspath() {
final String catalogFullPath = catalogContext.getCatalogFullPath();
try {
return classpathResourceLoader.getResources(getClass(), DEFAULT_JSON_CATALOG_LOCATION);
return classpathResourceLoader.getResources(getClass(), catalogFullPath);
} catch (final IOException e) {
throw new SchemaCatalogException(format("Failed to load the catalogs from the classpath for location '%s'", DEFAULT_JSON_CATALOG_LOCATION), e);
throw new SchemaCatalogException(format("Failed to load the catalogs from the classpath for location '%s'", catalogFullPath), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
public class SchemaResolver {

private static final String AN_EMPTY_STRING = "";
private static final String RELATIVE_SCHEMA_DIRECTORY = "../json/schema/";

private final UrlConverter urlConverter;
private final UriResolver uriResolver;
Expand All @@ -57,7 +58,8 @@ public URL resolve(
final Optional<String> schemaBaseLocation) {

try {
final URI schemaUri = new URI(schemaBaseLocation.orElse(AN_EMPTY_STRING)).resolve(relativeLocationOfSchema);
final String baseLocation = RELATIVE_SCHEMA_DIRECTORY + schemaBaseLocation.orElse(AN_EMPTY_STRING);
final URI schemaUri = new URI(baseLocation).resolve(relativeLocationOfSchema);
final URI resolvedUri = uriResolver.resolve(uriOfCatalogFile, schemaUri);

return urlConverter.toUrl(resolvedUri);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uk.gov.justice.schema.catalog;

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

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

@RunWith(MockitoJUnitRunner.class)
public class CatalogContextTest {

@InjectMocks
private CatalogContext catalogContext;

@Test
public void shouldGetCatalogFilename() throws Exception {
assertThat(catalogContext.getCatalogFilename(), is("schema_catalog.json"));
}

@Test
public void shouldGetTheCatalogLocation() throws Exception {
assertThat(catalogContext.getCatalogLocation(), is("META-INF/"));
}

@Test
public void shouldGetTheCatalogFullPath() throws Exception {
assertThat(catalogContext.getCatalogFullPath(), is("META-INF/schema_catalog.json"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public void shouldCreateAClasspathResourceLoader() throws Exception {
assertThat(classpathResourceLoader, is(notNullValue()));
}

@Test
public void shouldCreateACatalogGenerationContext() throws Exception {
assertThat(catalogObjectFactory.catalogContext(), is(instanceOf(CatalogContext.class)));
}

@Test
public void shouldCreateAClasspathCatalogLoader() throws Exception {

Expand All @@ -68,6 +73,9 @@ public void shouldCreateAClasspathCatalogLoader() throws Exception {
final ClasspathResourceLoader classpathResourceLoader = getPrivateField("classpathResourceLoader", classpathCatalogLoader, ClasspathResourceLoader.class);
assertThat(classpathResourceLoader, is(notNullValue()));

final CatalogContext catalogContext = getPrivateField("catalogContext", classpathCatalogLoader, CatalogContext.class);
assertThat(catalogContext, is(notNullValue()));

final UrlConverter urlConverter = getPrivateField("urlConverter", classpathCatalogLoader, UrlConverter.class);
assertThat(urlConverter, is(notNullValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void shouldMapSchemasFoundOnTheClasspathToTheirIds() throws Exception {
final Group group = mock(Group.class);
final Schema schema = mock(Schema.class);

final URI uri = new URI("file:/src/main/schema.json");
final URI uri = new URI("file:/src/main/resources/META-INF/schema.json");

final Map<URI, Catalog> catalogPojoMap = ImmutableMap.of(uri, catalog);

Expand All @@ -65,7 +65,7 @@ public void shouldMapSchemasFoundOnTheClasspathToTheirIds() throws Exception {
assertThat(schemaLocations.size(), is(1));

assertThat(schemaLocations.containsKey("schemaId"), is(true));
assertThat(schemaLocations.get("schemaId").toString(), is("file:/src/main/some/path/to.json"));
assertThat(schemaLocations.get("schemaId").toString(), is("file:/src/main/resources/json/schema/some/path/to.json"));
}

@Test
Expand All @@ -78,8 +78,8 @@ public void shouldHandleDuplicates() throws Exception {
final Schema schema_1 = mock(Schema.class);
final Schema schema_2 = mock(Schema.class);

final URI catalogLocation_1 = new URI("file:/src/main/catalog.json");
final URI catalogLocation_2 = new URI("file:/src/main/another-catalog.json");
final URI catalogLocation_1 = new URI("file:/src/main/resources/META-INF/catalog.json");
final URI catalogLocation_2 = new URI("file:/src/main/resources/META-INF/another-catalog.json");

final Map<URI, Catalog> catalogPojoMap = ImmutableMap.of(catalogLocation_1, catalog_1, catalogLocation_2, catalog_2);

Expand All @@ -99,8 +99,8 @@ public void shouldHandleDuplicates() throws Exception {
assertThat(schemaLocations.size(), is(1));

assertThat(schemaLocations.containsKey("schemaId"), is(true));
assertThat(schemaLocations.get("schemaId").toString(), is("file:/src/main/some/path/to.json"));
assertThat(schemaLocations.get("schemaId").toString(), is("file:/src/main/resources/json/schema/some/path/to.json"));

verify(logger).warn("Found duplicate schema id 'schemaId' for schemaLocations 'file:/src/main/some/path/to.json' and 'file:/src/main/some/other/path/to.json'");
verify(logger).warn("Found duplicate schema id 'schemaId' for schemaLocations 'file:/src/main/resources/json/schema/some/path/to.json' and 'file:/src/main/resources/json/schema/some/other/path/to.json'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,16 @@
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.URI;
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();

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

@InjectMocks
private ClasspathCatalogLoader classpathCatalogLoader;
private final ClasspathCatalogLoader classpathCatalogLoader = new CatalogObjectFactory().classpathCatalogLoader();

@Test
public void shouldLoadCatalogsFromTheClasspath() throws Exception {
Expand All @@ -49,7 +27,7 @@ public void shouldLoadCatalogsFromTheClasspath() throws Exception {
final URI uri = catalogs.keySet().iterator().next();

assertThat(uri.toString(), startsWith("file:/"));
assertThat(uri.toString(), endsWith("/json-schema-catalog/catalog-core/target/test-classes/json/schema/schema_catalog.json"));
assertThat(uri.toString(), endsWith("/json-schema-catalog/catalog-core/target/test-classes/META-INF/schema_catalog.json"));

final Catalog catalog = catalogs.get(uri);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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)
Expand All @@ -31,6 +32,10 @@ public class ClasspathCatalogLoaderTest {
@Mock
private ClasspathResourceLoader classpathResourceLoader;

@SuppressWarnings("unused")
@Spy
private final CatalogContext catalogContext = new CatalogContext();

@Mock
private UrlConverter urlConverter;

Expand All @@ -44,7 +49,7 @@ public void shouldThrowExceptionIfLoadingFileThrowsIOException() throws Exceptio
final URL url = new URL("file://src/code/my-file.txt");
final URI uri = url.toURI();

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

Expand All @@ -62,14 +67,14 @@ public void shouldThrowExceptionIfLoadingResourcesThrowsIOException() throws Exc

final IOException ioException = new IOException("Ooops");

when(classpathResourceLoader.getResources(ClasspathCatalogLoader.class, "json/schema/schema_catalog.json")).thenThrow(ioException);
when(classpathResourceLoader.getResources(ClasspathCatalogLoader.class, "META-INF/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'"));
assertThat(expected.getMessage(), startsWith("Failed to load the catalogs from the classpath for location 'META-INF/schema_catalog.json'"));

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,33 @@ public class SchemaResolverTest {
@Test
public void shouldResolveTheLocationToAUrlUsingTheCatalogUrlAndBaseLocation() throws Exception {

final URI absoluteCatalogUri = new URI("file:/src/main/catalog-file.json");
final URI absoluteCatalogUri = new URI("file:/src/main/resources/META-INF/catalog-file.json");

final String schemaFileLocation = "some/path/to/schema.json";
final Optional<String> baseLocation = of("base/location/");

final URL resolvedUri = schemaResolver.resolve(absoluteCatalogUri, schemaFileLocation, baseLocation);

assertThat(resolvedUri.toString(), is("file:/src/main/base/location/some/path/to/schema.json"));
assertThat(resolvedUri.toString(), is("file:/src/main/resources/json/schema/base/location/some/path/to/schema.json"));
}

@Test
public void shouldResolveTheLocationToAUrlUsingTheCatalogUrlAndAnEmptyBaseLocation() throws Exception {

final URI absoluteCatalogUri = new URI("file:/src/main/catalog-file.json");
final URI absoluteCatalogUri = new URI("file:/src/main/resources/META-INF/catalog-file.json");

final String schemaFileLocation = "some/path/to/schema.json";
final Optional<String> baseLocation = empty();

final URL resolvedUri = schemaResolver.resolve(absoluteCatalogUri, schemaFileLocation, baseLocation);

assertThat(resolvedUri.toString(), is("file:/src/main/some/path/to/schema.json"));
assertThat(resolvedUri.toString(), is("file:/src/main/resources/json/schema/some/path/to/schema.json"));
}

@Test
public void shouldFailIfResolvingTheUrlThrowsAURISyntaxException() throws Exception {

final URI catalogUri = new URI("file:/src/main/catalog-file.json");
final URI catalogUri = new URI("file:/src/main/resources/META-INF/catalog-file.json");

final String fileLocation = "some/path/to/schema.json";
final Optional<String> baseLocation = of("this path is silly");
Expand All @@ -74,7 +74,7 @@ public void shouldFailIfResolvingTheUrlThrowsAURISyntaxException() throws Except
fail();
} catch (final SchemaCatalogException expected) {
assertThat(expected.getCause(), is(instanceOf(URISyntaxException.class)));
assertThat(expected.getMessage(), is("Failed to resolve 'file:/src/main/catalog-file.json', to file location 'some/path/to/schema.json', with base location 'this path is silly'"));
assertThat(expected.getMessage(), is("Failed to resolve 'file:/src/main/resources/META-INF/catalog-file.json', to file location 'some/path/to/schema.json', with base location 'this path is silly'"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import uk.gov.justice.schema.catalog.SchemaCatalogException;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -61,4 +62,30 @@ public void shouldFailIfConvertingAStringToAUriThrowsAURISyntaxException() throw
assertThat(expected.getMessage(), is("Failed to convert URI 'this is not a uri' to URL"));
}
}

@Test
public void shouldFailIfConvertingAUrlToAUriThrowsAURISyntaxException() throws Exception {

final URL url = new URL("file:/this is not a uri");
try {
urlConverter.toUri(url);
fail();
} catch (final SchemaCatalogException expected) {
assertThat(expected.getCause(), is(instanceOf(URISyntaxException.class)));
assertThat(expected.getMessage(), is("Failed to convert URL 'file:/this is not a uri' to URI"));
}
}

@Test
public void shouldFailIfConvertingAUriToAUrlThrowsAMalformedURLException() throws Exception {

final URI uri = new URI("silly:/this-is-not-a-url");
try {
urlConverter.toUrl(uri);
fail();
} catch (final SchemaCatalogException expected) {
assertThat(expected.getCause(), is(instanceOf(MalformedURLException.class)));
assertThat(expected.getMessage(), is("Failed to convert URI 'silly:/this-is-not-a-url' to URL"));
}
}
}
Loading

0 comments on commit bfd56fe

Please sign in to comment.