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

Commit

Permalink
Merge 7966b74 into 38d8eef
Browse files Browse the repository at this point in the history
  • Loading branch information
benouaer committed Jul 4, 2018
2 parents 38d8eef + 7966b74 commit ce6a067
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
[Semantic Versioning](http://semver.org/).

## Unreleased
### Changed
- Added schema catalog resolver to enable loading schemas from JSON objects

## [1.2.4] - 2018-07-03

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ public SchemaResolver schemaResolver() {
return new SchemaResolver(urlConverter(), uriResolver());
}

/**
* @return a new instance of {@link SchemaCatalogResolver}
*/
public SchemaCatalogResolver schemaCatalogResolver() {

return new SchemaCatalogResolver(
rawCatalog(),
schemaClientFactory(),
jsonToSchemaConverter());
}



/**
* @return a new instance of {@link CatalogToSchemaResolver}
*/
Expand Down
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));
}
}
6 changes: 0 additions & 6 deletions catalog-core/src/main/resources/META-INF/beans.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ public void shouldCreateASchemaResolver() throws Exception {
assertThat(uriResolver, is(notNullValue()));
}

@Test
public void shouldCreateASchemaCatalogResolver() throws Exception {

final SchemaCatalogResolver schemaCatalogResolver = catalogObjectFactory.schemaCatalogResolver();
assertThat(schemaCatalogResolver, is(notNullValue()));

final RawCatalog rawCatalog = getPrivateField("rawCatalog", schemaCatalogResolver, RawCatalog.class);
assertThat(rawCatalog, is(notNullValue()));

final SchemaClientFactory schemaClientFactory = getPrivateField("schemaClientFactory", schemaCatalogResolver, SchemaClientFactory.class);
assertThat(schemaClientFactory, is(notNullValue()));

final JsonToSchemaConverter jsonToSchemaConverter = getPrivateField("jsonStringToSchemaConverter", schemaCatalogResolver, JsonToSchemaConverter.class);
assertThat(jsonToSchemaConverter, is(notNullValue()));
}

@Test
public void shouldCreateCatalogToSchemaResolver() throws Exception {

Expand Down
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));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package uk.gov.justice.schema.catalog;
package uk.gov.justice.schema.service;

import uk.gov.justice.schema.catalog.Catalog;
import uk.gov.justice.schema.catalog.CatalogObjectFactory;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
Expand Down
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package uk.gov.justice.schema.catalog;
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.Catalog;
import uk.gov.justice.schema.catalog.JsonToSchemaConverter;
import uk.gov.justice.schema.catalog.RawCatalog;
import uk.gov.justice.schema.catalog.client.SchemaClientFactory;

import java.lang.reflect.Field;
Expand Down
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);
}
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

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

import java.util.Optional;

import javax.inject.Inject;
Expand Down
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"
]
}

0 comments on commit ce6a067

Please sign in to comment.