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

Commit

Permalink
Change base location in the catalog to use schemaLocation and any path
Browse files Browse the repository at this point in the history
  • Loading branch information
amckenzie committed Dec 18, 2017
1 parent 00d35c1 commit 08969df
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public class SchemaResolver {

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

private final UrlConverter urlConverter;
private final UriResolver uriResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/resources/json/schema/some/path/to.json"));
assertThat(schemaLocations.get("schemaId").toString(), is("file:/src/main/resources/some/path/to.json"));
}

@Test
Expand Down Expand Up @@ -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/resources/json/schema/some/path/to.json"));
assertThat(schemaLocations.get("schemaId").toString(), is("file:/src/main/resources/some/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'");
verify(logger).warn("Found duplicate schema id 'schemaId' for schemaLocations 'file:/src/main/resources/some/path/to.json' and 'file:/src/main/resources/some/other/path/to.json'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.net.URI;
import java.util.Map;

import org.hamcrest.CoreMatchers;
import org.junit.Test;

public class ClasspathCatalogLoaderIntegrationTest {
Expand All @@ -35,7 +34,7 @@ public void shouldLoadCatalogsFromTheClasspath() throws Exception {
assertThat(catalog.getGroups().size(), is(2));

assertThat(catalog.getGroups().get(0).getName(), is("standards"));
assertThat(catalog.getGroups().get(0).getBaseLocation(), is("standards/"));
assertThat(catalog.getGroups().get(0).getBaseLocation(), is("json/schema/standards/"));

assertThat(catalog.getGroups().get(0).getSchemas().size(), is(2));
assertThat(catalog.getGroups().get(0).getSchemas().get(0).getId(), is("http://justice.gov.uk/standards/complex_address.json"));
Expand All @@ -44,10 +43,10 @@ public void shouldLoadCatalogsFromTheClasspath() throws Exception {
assertThat(catalog.getGroups().get(0).getSchemas().get(1).getLocation(), is("address.json"));

assertThat(catalog.getGroups().get(1).getName(), is("staging interface"));
assertThat(catalog.getGroups().get(1).getBaseLocation(), is(CoreMatchers.nullValue()));
assertThat(catalog.getGroups().get(1).getBaseLocation(), is("json/schema/context/"));

assertThat(catalog.getGroups().get(1).getSchemas().size(), is(1));
assertThat(catalog.getGroups().get(1).getSchemas().get(0).getId(), is("http://justice.gov.uk/context/person.json"));
assertThat(catalog.getGroups().get(1).getSchemas().get(0).getLocation(), is("context/person.json"));
assertThat(catalog.getGroups().get(1).getSchemas().get(0).getLocation(), is("person.json"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void shouldResolveTheLocationToAUrlUsingTheCatalogUrlAndBaseLocation() th

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

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

@Test
Expand All @@ -58,7 +58,7 @@ public void shouldResolveTheLocationToAUrlUsingTheCatalogUrlAndAnEmptyBaseLocati

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

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

@Test
Expand Down
5 changes: 3 additions & 2 deletions catalog-core/src/test/resources/META-INF/schema_catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"groups": [
{
"name": "standards",
"baseLocation": "standards/",
"baseLocation": "json/schema/standards/",
"schemas": [
{
"id": "http://justice.gov.uk/standards/complex_address.json",
Expand All @@ -17,10 +17,11 @@
},
{
"name": "staging interface",
"baseLocation": "json/schema/context/",
"schemas": [
{
"id": "http://justice.gov.uk/context/person.json",
"location": "context/person.json"
"location": "person.json"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@
*
* For example:<br/>
* Given an absolute {@link URL} of a json file
* <pre>file:/path/to/raml/json/schema/group/some/path/some-schema-or-other.json</pre>
* <pre>file:/path/to/raml/json/schema/some/path/some-schema-or-other.json</pre>
* and a path to the json files (a sub path to locate the root of the json schemas)
* <pre>json/schema/</pre>
* would give us the remaining sub path:
* <pre>group/some/path/some-schema-or-other.json</pre>
* which would give use the group name
* <pre>group</pre>
* and the base location
* <pre>group/</pre>
* and the schema location:
* <pre>some/path/some-schema-or-other.json</pre>
*
*
* which would give use the base location
* <pre>json/schema/</pre>
* and the group name
* <pre>some/path</pre>
* and the schema location:
* <pre>some-schema-or-other.json</pre>
*/
public class SchemaDefParser {

private static final char SLASH = '/';
private final SchemaIdParser schemaIdParser;

public SchemaDefParser(final SchemaIdParser schemaIdParser) {
Expand All @@ -60,14 +59,14 @@ public Optional<SchemaDef> parse(final URL schemaFile, final Path jsonSchemaPath
if (id.isPresent()) {
final String fileUrl = schemaFile.toString();

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

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

final String groupName = getGroup(relativeUri, firstSlashIndex);
final String baseLocation = getBaseLocation(groupName, firstSlashIndex);
final String location = relativeUri.substring(firstSlashIndex + 1);
final String groupName = getGroup(relativeUri, lastSlashIndex);
final String baseLocation = getBaseLocation(jsonSchemaString, groupName);
final String location = relativeUri.substring(lastSlashIndex + 1);

final SchemaDef schemaDef = new SchemaDef(schemaFile, id.get(), groupName, baseLocation, location);

Expand All @@ -86,12 +85,12 @@ private String getGroup(final String relativeUri, final int firstSlashIndex) {
return relativeUri.substring(0, firstSlashIndex);
}

private String getBaseLocation(final String groupName, final int firstSlashIndex) {
private String getBaseLocation(final String jsonSchemaPath, final String groupName) {

if(firstSlashIndex == -1) {
return AN_EMPTY_STRING;
if (groupName.isEmpty()) {
return jsonSchemaPath + SLASH;
}

return groupName + "/";
return jsonSchemaPath + SLASH + groupName + SLASH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void shouldParseAListOfSchemaFilesIntoACatalogObject() throws Exception {
assertThat(catalog.getGroups().size(), is(2));

assertThat(catalog.getGroups().get(0).getName(), is("standards"));
assertThat(catalog.getGroups().get(0).getBaseLocation(), is("standards/"));
assertThat(catalog.getGroups().get(0).getBaseLocation(), is("raml/json/schema/standards/"));
assertThat(catalog.getGroups().get(0).getSchemas().size(), is(3));
assertThat(catalog.getGroups().get(0).getSchemas().get(0).getId(), is("http://justice.gov.uk/standards/address.json"));
assertThat(catalog.getGroups().get(0).getSchemas().get(0).getLocation(), is("address.json"));
Expand All @@ -51,7 +51,7 @@ public void shouldParseAListOfSchemaFilesIntoACatalogObject() throws Exception {
assertThat(catalog.getGroups().get(0).getSchemas().get(2).getLocation(), is("defendant.json"));

assertThat(catalog.getGroups().get(1).getName(), is("context"));
assertThat(catalog.getGroups().get(1).getBaseLocation(), is("context/"));
assertThat(catalog.getGroups().get(1).getBaseLocation(), is("raml/json/schema/context/"));
assertThat(catalog.getGroups().get(1).getSchemas().size(), is(1));
assertThat(catalog.getGroups().get(1).getSchemas().get(0).getId(), is("http://justice.gov.uk/context/person.json"));
assertThat(catalog.getGroups().get(1).getSchemas().get(0).getLocation(), is("person.json"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class SchemaDefParserTest {
public void shouldParseTheSchemaUrlIntoIdGroupBaseLocationAndLocation() throws Exception {

final Path jsonSchemaPath = Paths.get("json/schema/");
final URL schemaFile = new URL("file:/path/to/raml/json/schema/group/some/path/some-schema-or-other.json");
final URL schemaFile = new URL("file:/path/to/raml/json/schema/some/path/some-schema-or-other.json");

final String schemaId = "http://justice.gov.uk/context/some-schema-or-other.json";
when(schemaIdParser.parse(schemaFile)).thenReturn(of(new URL(schemaId).toURI()));
Expand All @@ -50,9 +50,9 @@ public void shouldParseTheSchemaUrlIntoIdGroupBaseLocationAndLocation() throws E
final SchemaDef schemaDef = schemaDefOptional.get();
assertThat(schemaDef.getSchemaFile(), is(schemaFile));
assertThat(schemaDef.getId().toString(), is(schemaId));
assertThat(schemaDef.getGroupName(), is("group"));
assertThat(schemaDef.getBaseLocation(), is("group/"));
assertThat(schemaDef.getLocation(), is("some/path/some-schema-or-other.json"));
assertThat(schemaDef.getGroupName(), is("some/path"));
assertThat(schemaDef.getBaseLocation(), is("json/schema/some/path/"));
assertThat(schemaDef.getLocation(), is("some-schema-or-other.json"));
} else {
fail();
}
Expand All @@ -74,7 +74,7 @@ public void shouldHandleSchemasAtTheRootOfTheJsonSchemaDirectory() throws Except
assertThat(schemaDef.getSchemaFile(), is(schemaFile));
assertThat(schemaDef.getId().toString(), is(schemaId));
assertThat(schemaDef.getGroupName(), is(AN_EMPTY_STRING));
assertThat(schemaDef.getBaseLocation(), is(AN_EMPTY_STRING));
assertThat(schemaDef.getBaseLocation(), is("json/schema/"));
assertThat(schemaDef.getLocation(), is("some-schema-or-other.json"));
} else {
fail();
Expand All @@ -97,9 +97,9 @@ public void shouldParseTheSchemaUrlIntoIdGroupBaseLocationAndLocationWithSubDire

assertThat(schemaDef.getSchemaFile(), is(schemaFile));
assertThat(schemaDef.getId().toString(), is(schemaId));
assertThat(schemaDef.getGroupName(), is("a-sub-directory"));
assertThat(schemaDef.getBaseLocation(), is("a-sub-directory/"));
assertThat(schemaDef.getLocation(), is("another-sub-directory/some-schema-or-other.json"));
assertThat(schemaDef.getGroupName(), is("a-sub-directory/another-sub-directory"));
assertThat(schemaDef.getBaseLocation(), is("json/schema/a-sub-directory/another-sub-directory/"));
assertThat(schemaDef.getLocation(), is("some-schema-or-other.json"));
} else {
fail();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
"groups": [
{
"name": "staging interface",
"baseLocation": "json/schema/context/",
"schemas": [
{
"id": "http://justice.gov.uk/context/person.json",
"location": "context/person.json"
"location": "person.json"
}
]
},
{
"name": "standards",
"baseLocation": "standards/",
"baseLocation": "json/schema/standards/",
"schemas": [
{
"id": "http://justice.gov.uk/standards/complex_address.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
"groups": [
{
"name": "staging interface",
"baseLocation": "json/schema/context/",
"schemas": [
{
"id": "http://justice.gov.uk/context/person.json",
"location": "context/person.json"
"location": "person.json"
}
]
},
{
"name": "standards",
"baseLocation": "standards/",
"baseLocation": "json/schema/standards/",
"schemas": [
{
"id": "http://justice.gov.uk/standards/complex_address.json",
Expand Down

0 comments on commit 08969df

Please sign in to comment.