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

Commit

Permalink
rename groups and schemas to their plurals in catalogs and domain obj…
Browse files Browse the repository at this point in the history
…ects
  • Loading branch information
amckenzie committed Nov 30, 2017
1 parent 4a14207 commit 5f244c5
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public Map<String, URL> resolveSchemaLocations() {
final Map<URI, Catalog> catalogs = classpathCatalogLoader.getCatalogs();

for(final URI catalogLocation: catalogs.keySet()) {
for(final Group group: catalogs.get(catalogLocation).getGroup()) {
for(final Schema schema: group.getSchema()) {
for(final Group group: catalogs.get(catalogLocation).getGroups()) {
for(final Schema schema: group.getSchemas()) {
final String location = schema.getLocation();
final Optional<String> baseLocation = ofNullable(group.getBaseLocation());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package uk.gov.justice.schema.catalog;

import static java.lang.String.format;

import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaClient;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class JsonStringToSchemaConverter {

// TODO: test me!!!
public Schema convert(final String schemaJson, final SchemaClient schemaClient) {
return SchemaLoader.builder()
.schemaJson(new JSONObject(new JSONTokener(schemaJson)))
.httpClient(schemaClient)
.build()
.load()
.build();
try {
return SchemaLoader.builder()
.schemaJson(new JSONObject(new JSONTokener(schemaJson)))
.httpClient(schemaClient)
.build()
.load()
.build();
} catch (final JSONException e) {
throw new SchemaCatalogException(format("Failed to convert schema json to Schema Object. Schema json:%n%s", schemaJson), e);
}
}
}
4 changes: 2 additions & 2 deletions catalog-core/src/main/resources/schema_catalog_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": {
"type": "string"
},
"group": {
"groups": {
"type": "array",
"items": {
"type": "object",
Expand All @@ -16,7 +16,7 @@
"name": {
"type": "string"
},
"schema": {
"schemas": {
"type": "array",
"items": {
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public void shouldMapSchemasFoundOnTheClasspathToTheirIds() throws Exception {
final Map<URI, Catalog> catalogPojoMap = ImmutableMap.of(uri, catalog);

when(classpathCatalogLoader.getCatalogs()).thenReturn(catalogPojoMap);
when(catalog.getGroup()).thenReturn(singletonList(group));
when(group.getSchema()).thenReturn(singletonList(schema));
when(catalog.getGroups()).thenReturn(singletonList(group));
when(group.getSchemas()).thenReturn(singletonList(schema));

when(schema.getLocation()).thenReturn("some/path/to.json");
when(schema.getId()).thenReturn("schemaId");
Expand Down Expand Up @@ -85,10 +85,10 @@ public void shouldHandleDuplicates() throws Exception {
final Map<URI, Catalog> catalogPojoMap = ImmutableMap.of(catalogLocation_1, catalog_1, catalogLocation_2, catalog_2);

when(classpathCatalogLoader.getCatalogs()).thenReturn(catalogPojoMap);
when(catalog_1.getGroup()).thenReturn(singletonList(group_1));
when(catalog_2.getGroup()).thenReturn(singletonList(group_2));
when(group_1.getSchema()).thenReturn(singletonList(schema_1));
when(group_2.getSchema()).thenReturn(singletonList(schema_2));
when(catalog_1.getGroups()).thenReturn(singletonList(group_1));
when(catalog_2.getGroups()).thenReturn(singletonList(group_2));
when(group_1.getSchemas()).thenReturn(singletonList(schema_1));
when(group_2.getSchemas()).thenReturn(singletonList(schema_2));

when(schema_1.getLocation()).thenReturn("some/path/to.json");
when(schema_2.getLocation()).thenReturn("some/other/path/to.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ public void shouldLoadCatalogsFromTheClasspath() throws Exception {
final Catalog catalog = catalogs.get(uri);

assertThat(catalog.getName(), is("my catalog"));
assertThat(catalog.getGroup().size(), is(2));
assertThat(catalog.getGroups().size(), is(2));

assertThat(catalog.getGroup().get(0).getName(), is("standards"));
assertThat(catalog.getGroup().get(0).getBaseLocation(), is("standards/"));
assertThat(catalog.getGroups().get(0).getName(), is("standards"));
assertThat(catalog.getGroups().get(0).getBaseLocation(), is("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.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"));
assertThat(catalog.getGroups().get(0).getSchemas().get(0).getLocation(), is("complex_address.json"));
assertThat(catalog.getGroups().get(0).getSchemas().get(1).getId(), is("http://justice.gov.uk/standards/address.json"));
assertThat(catalog.getGroups().get(0).getSchemas().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.getGroups().get(1).getName(), is("staging interface"));
assertThat(catalog.getGroups().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("context/person.json"));
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"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package uk.gov.justice.schema.catalog;

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 static org.mockito.Mockito.mock;

import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.everit.json.schema.loader.SchemaClient;
import org.json.JSONException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;


@RunWith(MockitoJUnitRunner.class)
public class JsonStringToSchemaConverterTest {

@InjectMocks
private JsonStringToSchemaConverter jsonStringToSchemaConverter;

@Test
public void shouldFailWithErrorMessageIfParsingTheJsonStringFails() throws Exception {

final URL url = getClass().getClassLoader().getResource("json/dodgy-schemas/schema-with-two-ids.json");

assertThat(url, is(notNullValue()));

final String schemaJson = IOUtils.toString(url);

try {
jsonStringToSchemaConverter.convert(schemaJson, mock(SchemaClient.class));
fail();
} catch (final SchemaCatalogException expected) {
assertThat(expected.getCause(), is(instanceOf(JSONException.class)));
assertThat(expected.getMessage(), startsWith("Failed to convert schema json to Schema Object. Schema json:"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "http://justice.gov.uk/context/person.json",
"type": "object",
"id": "http://justice.gov.uk/context/person.json",
"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"
]
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "my catalog",
"group": [
"groups": [
{
"name": "standards",
"baseLocation": "standards/",
"schema": [
"schemas": [
{
"id": "http://justice.gov.uk/standards/complex_address.json",
"location": "complex_address.json"
Expand All @@ -17,7 +17,7 @@
},
{
"name": "staging interface",
"schema": [
"schemas": [
{
"id": "http://justice.gov.uk/context/person.json",
"location": "context/person.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

public class Catalog {

private final List<Group> group;
private final List<Group> groups;
private final String name;

public Catalog(final String name, final List<Group> group) {
this.group = group;
public Catalog(final String name, final List<Group> groups) {
this.groups = groups;
this.name = name;
}

public List<Group> getGroup() {
return group;
public List<Group> getGroups() {
return groups;
}

public String getName() {
Expand All @@ -23,7 +23,7 @@ public String getName() {
@Override
public String toString() {
return "Catalog{" +
"group=" + group +
"groups=" + groups +
", name='" + name + '\'' +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ public class Group {

private final String baseLocation;
private final String name;
private final List<Schema> schema;
private final List<Schema> schemas;

public Group(final String baseLocation, final String name, final List<Schema> schema) {
public Group(final String baseLocation, final String name, final List<Schema> schemas) {
this.baseLocation = baseLocation;
this.name = name;
this.schema = schema;
this.schemas = schemas;
}

public String getBaseLocation() {
Expand All @@ -22,16 +22,16 @@ public String getName() {
return name;
}

public List<Schema> getSchema() {
return schema;
public List<Schema> getSchemas() {
return schemas;
}

@Override
public String toString() {
return "Group{" +
"baseLocation='" + baseLocation + '\'' +
", name='" + name + '\'' +
", schema=" + schema +
", schemas=" + schemas +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void createGroups(final SchemaDef schemaDef, final Map<String, Group> gr

final Group group = groups.get(groupName);

final List<Schema> schemas = group.getSchema();
final List<Schema> schemas = group.getSchemas();

schemas.add(new Schema(schemaDef.getId().toString(), schemaDef.getLocation()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,22 @@ public void shouldParseAListOfSchemaFilesIntoACatalogObject() throws Exception {
final Catalog catalog = catalogObjectGenerator.generate(catalogName, schemaFiles, jsonSchemaPath);

assertThat(catalog.getName(), is(catalogName));
assertThat(catalog.getGroup().size(), is(2));

assertThat(catalog.getGroup().get(0).getName(), is("standards"));
assertThat(catalog.getGroup().get(0).getBaseLocation(), is("standards/"));
assertThat(catalog.getGroup().get(0).getSchema().size(), is(3));
assertThat(catalog.getGroup().get(0).getSchema().get(0).getId(), is("http://justice.gov.uk/standards/address.json"));
assertThat(catalog.getGroup().get(0).getSchema().get(0).getLocation(), is("address.json"));
assertThat(catalog.getGroup().get(0).getSchema().get(1).getId(), is("http://justice.gov.uk/standards/complex_address.json"));
assertThat(catalog.getGroup().get(0).getSchema().get(1).getLocation(), is("complex_address.json"));
assertThat(catalog.getGroup().get(0).getSchema().get(2).getId(), is("http://justice.gov.uk/standards/defendant.json"));
assertThat(catalog.getGroup().get(0).getSchema().get(2).getLocation(), is("defendant.json"));

assertThat(catalog.getGroup().get(1).getName(), is("context"));
assertThat(catalog.getGroup().get(1).getBaseLocation(), is("context/"));
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("person.json"));
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).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"));
assertThat(catalog.getGroups().get(0).getSchemas().get(1).getId(), is("http://justice.gov.uk/standards/complex_address.json"));
assertThat(catalog.getGroups().get(0).getSchemas().get(1).getLocation(), is("complex_address.json"));
assertThat(catalog.getGroups().get(0).getSchemas().get(2).getId(), is("http://justice.gov.uk/standards/defendant.json"));
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).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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ public void shouldGenerateACatalogInTheCorrectLocationFromTheCatalogDomainObject

with(catalogJson)
.assertThat("$.name", is(catalog.getName()))
.assertThat("$.group[0].name", is(catalog.getGroup().get(0).getName()))
.assertThat("$.group[0].baseLocation", is(catalog.getGroup().get(0).getBaseLocation()))
.assertThat("$.group[0].schema[0].id", is(catalog.getGroup().get(0).getSchema().get(0).getId()))
.assertThat("$.group[0].schema[0].location", is(catalog.getGroup().get(0).getSchema().get(0).getLocation()))
.assertThat("$.group[0].schema[1].id", is(catalog.getGroup().get(0).getSchema().get(1).getId()))
.assertThat("$.group[0].schema[1].location", is(catalog.getGroup().get(0).getSchema().get(1).getLocation()))
.assertThat("$.group[1].name", is(catalog.getGroup().get(1).getName()))
.assertThat("$.groups[0].name", is(catalog.getGroups().get(0).getName()))
.assertThat("$.groups[0].baseLocation", is(catalog.getGroups().get(0).getBaseLocation()))
.assertThat("$.groups[0].schemas[0].id", is(catalog.getGroups().get(0).getSchemas().get(0).getId()))
.assertThat("$.groups[0].schemas[0].location", is(catalog.getGroups().get(0).getSchemas().get(0).getLocation()))
.assertThat("$.groups[0].schemas[1].id", is(catalog.getGroups().get(0).getSchemas().get(1).getId()))
.assertThat("$.groups[0].schemas[1].location", is(catalog.getGroups().get(0).getSchemas().get(1).getLocation()))
.assertThat("$.groups[1].name", is(catalog.getGroups().get(1).getName()))
.assertNotDefined("$.group[1].baseLocation")
.assertThat("$.group[1].schema[0].id", is(catalog.getGroup().get(1).getSchema().get(0).getId()))
.assertThat("$.group[1].schema[0].location", is(catalog.getGroup().get(1).getSchema().get(0).getLocation()))
.assertThat("$.groups[1].schemas[0].id", is(catalog.getGroups().get(1).getSchemas().get(0).getId()))
.assertThat("$.groups[1].schemas[0].location", is(catalog.getGroups().get(1).getSchemas().get(0).getLocation()))
;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "my catalog",
"group": [
"groups": [
{
"name": "staging interface",
"schema": [
"schemas": [
{
"id": "http://justice.gov.uk/context/person.json",
"location": "context/person.json"
Expand All @@ -13,7 +13,7 @@
{
"name": "standards",
"baseLocation": "standards/",
"schema": [
"schemas": [
{
"id": "http://justice.gov.uk/standards/complex_address.json",
"location": "complex_address.json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"group": [
"groups": [
{
"name": "staging interface",
"schema": [
"schemas": [
{
"id": "http://justice.gov.uk/context/person.json",
"location": "context/person.json"
Expand All @@ -12,7 +12,7 @@
{
"name": "standards",
"baseLocation": "standards/",
"schema": [
"schemas": [
{
"id": "http://justice.gov.uk/standards/complex_address.json",
"location": "complex_address.json"
Expand Down

0 comments on commit 5f244c5

Please sign in to comment.