This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
261 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
catalog-core/src/main/java/uk/gov/justice/schema/catalog/SchemaCatalogResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package uk.gov.justice.schema.catalog; | ||
|
||
import uk.gov.justice.schema.catalog.client.SchemaClientFactory; | ||
|
||
import org.everit.json.schema.Schema; | ||
import org.json.JSONObject; | ||
|
||
public class SchemaCatalogResolver { | ||
|
||
private final RawCatalog rawCatalog; | ||
private final SchemaClientFactory schemaClientFactory; | ||
private final JsonToSchemaConverter jsonStringToSchemaConverter; | ||
|
||
public SchemaCatalogResolver(final RawCatalog rawCatalog, | ||
final SchemaClientFactory schemaClientFactory, | ||
final JsonToSchemaConverter jsonStringToSchemaConverter) { | ||
this.rawCatalog = rawCatalog; | ||
this.schemaClientFactory = schemaClientFactory; | ||
this.jsonStringToSchemaConverter = jsonStringToSchemaConverter; | ||
} | ||
|
||
public Schema loadSchema(final JSONObject jsonSchema) { | ||
return jsonStringToSchemaConverter.convert( | ||
jsonSchema, | ||
schemaClientFactory.create(rawCatalog)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
catalog-core/src/test/java/uk/gov/justice/schema/catalog/SchemaCatalogResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package uk.gov.justice.schema.catalog; | ||
|
||
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.client.SchemaClientFactory; | ||
|
||
import org.everit.json.schema.Schema; | ||
import org.everit.json.schema.loader.SchemaClient; | ||
import org.json.JSONObject; | ||
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 SchemaCatalogResolverTest { | ||
|
||
@Mock | ||
private RawCatalog rawCatalog; | ||
|
||
@Mock | ||
private SchemaClientFactory schemaClientFactory; | ||
|
||
@Mock | ||
private JsonToSchemaConverter jsonStringToSchemaConverter; | ||
|
||
@InjectMocks | ||
private SchemaCatalogResolver schemaCatalogResolver; | ||
|
||
@Test | ||
public void shouldResolveSchema() { | ||
final Schema schema = mock(Schema.class); | ||
final JSONObject jsonSchema = mock(JSONObject.class); | ||
final SchemaClient schemaClient = mock(SchemaClient.class); | ||
|
||
when(schemaClientFactory.create(rawCatalog)).thenReturn(schemaClient); | ||
when(jsonStringToSchemaConverter.convert(jsonSchema, schemaClient)).thenReturn(schema); | ||
|
||
final Schema resultSchema = schemaCatalogResolver.loadSchema(jsonSchema); | ||
|
||
assertThat(resultSchema, is(schema)); | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
...stice/schema/catalog/CatalogProducer.java → ...stice/schema/service/CatalogProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...ma-service/src/main/java/uk/gov/justice/schema/service/SchemaCatalogResolverProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package uk.gov.justice.schema.service; | ||
|
||
import uk.gov.justice.schema.catalog.CatalogObjectFactory; | ||
import uk.gov.justice.schema.catalog.SchemaCatalogResolver; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.inject.Produces; | ||
|
||
/** | ||
* A Producer for the {@link SchemaCatalogResolver} should you be running in a CDI container | ||
*/ | ||
@ApplicationScoped | ||
public class SchemaCatalogResolverProducer { | ||
|
||
/** | ||
* @return a new instance of {@link SchemaCatalogResolver} | ||
*/ | ||
@Produces | ||
public SchemaCatalogResolver schemaCatalogResolver() { | ||
return new CatalogObjectFactory().schemaCatalogResolver(); | ||
} | ||
} |
5 changes: 4 additions & 1 deletion
5
...e/schema/catalog/CatalogProducerTest.java → ...e/schema/service/CatalogProducerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
schema-service/src/test/java/uk/gov/justice/schema/service/SchemaCatalogResolverIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package uk.gov.justice.schema.service; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import uk.gov.justice.schema.catalog.SchemaCatalogResolver; | ||
|
||
import java.io.InputStream; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.apache.openejb.jee.Application; | ||
import org.apache.openejb.jee.WebApp; | ||
import org.apache.openejb.junit.ApplicationComposer; | ||
import org.apache.openejb.testing.Classes; | ||
import org.apache.openejb.testing.Module; | ||
import org.everit.json.schema.Schema; | ||
import org.everit.json.schema.SchemaException; | ||
import org.json.JSONObject; | ||
import org.json.JSONTokener; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(ApplicationComposer.class) | ||
public class SchemaCatalogResolverIT { | ||
|
||
@Inject | ||
private SchemaCatalogResolver schemaCatalogResolver; | ||
|
||
@Module | ||
@Classes(cdi = true, value = { | ||
SchemaCatalogResolverProducer.class | ||
}) | ||
public WebApp war() { | ||
return new WebApp() | ||
.contextRoot("schema-catalog-resolver-test") | ||
.addServlet("SchemaCatalogResolverIT", Application.class.getName()); | ||
} | ||
|
||
@Test | ||
public void shouldReturnResolvedSchema() throws Exception { | ||
try (final InputStream schemaFileStream = this.getClass().getResourceAsStream("/json/schema/context/person.json")) { | ||
final JSONObject schemaJsonObject = new JSONObject(new JSONTokener(schemaFileStream)); | ||
|
||
final Schema schema = schemaCatalogResolver.loadSchema(schemaJsonObject); | ||
|
||
assertThat(schema.getId(), is("http://justice.gov.uk/context/person.json")); | ||
assertTrue(schema.definesProperty("correspondence_address")); | ||
} | ||
} | ||
|
||
@Test(expected = SchemaException.class) | ||
public void shouldReturnInvalidSchema() throws Exception { | ||
try (final InputStream schemaFileStream = this.getClass().getResourceAsStream("/json/schema/context/invalid_reference.json")) { | ||
final JSONObject schemaJsonObject = new JSONObject(new JSONTokener(schemaFileStream)); | ||
|
||
schemaCatalogResolver.loadSchema(schemaJsonObject); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...ervice/src/test/java/uk/gov/justice/schema/service/SchemaCatalogResolverProducerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package uk.gov.justice.schema.service; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import uk.gov.justice.schema.catalog.JsonToSchemaConverter; | ||
import uk.gov.justice.schema.catalog.RawCatalog; | ||
import uk.gov.justice.schema.catalog.SchemaCatalogResolver; | ||
import uk.gov.justice.schema.catalog.client.SchemaClientFactory; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class SchemaCatalogResolverProducerTest { | ||
|
||
@InjectMocks | ||
private SchemaCatalogResolverProducer schemaCatalogResolverProducer; | ||
|
||
@Test | ||
public void shouldCreateTheCatalog() throws Exception { | ||
|
||
final SchemaCatalogResolver schemaCatalogResolver = schemaCatalogResolverProducer.schemaCatalogResolver(); | ||
|
||
assertThat(schemaCatalogResolver, is(notNullValue())); | ||
|
||
assertThat(getPrivateField(schemaCatalogResolver, "rawCatalog", RawCatalog.class), is(notNullValue())); | ||
assertThat(getPrivateField(schemaCatalogResolver, "schemaClientFactory", SchemaClientFactory.class), is(notNullValue())); | ||
assertThat(getPrivateField(schemaCatalogResolver, "jsonStringToSchemaConverter", JsonToSchemaConverter.class), is(notNullValue())); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private <T> T getPrivateField(final SchemaCatalogResolver schemaCatalogResolver, final String fieldName, @SuppressWarnings("unused") final Class<T> clazz) throws Exception { | ||
|
||
final Field field = schemaCatalogResolver.getClass().getDeclaredField(fieldName); | ||
field.setAccessible(true); | ||
return (T) field.get(schemaCatalogResolver); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
schema-service/src/test/resources/json/schema/context/invalid_reference.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/invalid_address"} | ||
|
||
}, | ||
"required": [ | ||
"nino", | ||
"name", | ||
"correspondence_address" | ||
] | ||
} |