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

Commit

Permalink
Beans annotated only and refactor injection
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo authored and amckenzie committed Dec 5, 2017
1 parent 33ce423 commit 168b998
Show file tree
Hide file tree
Showing 25 changed files with 365 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@

public class Catalog {

private final RawCatalog rawCatalog;
private final SchemaClientFactory schemaClientFactory;
private final JsonStringToSchemaConverter jsonStringToSchemaConverter;
@Inject
RawCatalog rawCatalog;

@Inject
public Catalog(
final RawCatalog rawCatalog,
final SchemaClientFactory schemaClientFactory,
final JsonStringToSchemaConverter jsonStringToSchemaConverter) {
this.rawCatalog = rawCatalog;
this.schemaClientFactory = schemaClientFactory;
this.jsonStringToSchemaConverter = jsonStringToSchemaConverter;
}
SchemaClientFactory schemaClientFactory;

@Inject
JsonStringToSchemaConverter jsonStringToSchemaConverter;


public Optional<Schema> getSchema(final String schemaId) {

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

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;

@ApplicationScoped
public class CatalogProducer {

@Produces
public Catalog catalog() {
return new ObjectFactory().catalog();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,23 @@
import java.util.Map;
import java.util.Optional;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.slf4j.Logger;

public class CatalogToSchemaResolver {

private final ClasspathCatalogLoader classpathCatalogLoader;
private final SchemaResolver schemaResolver;
private final Logger logger;
@Inject
ClasspathCatalogLoader classpathCatalogLoader;

@Inject
public CatalogToSchemaResolver(
final ClasspathCatalogLoader classpathCatalogLoader,
final SchemaResolver schemaResolver,
final Logger logger) {
this.classpathCatalogLoader = classpathCatalogLoader;
this.schemaResolver = schemaResolver;
this.logger = logger;
}
SchemaResolver schemaResolver;

@Inject
Logger logger;

/**
*
* @return Mapping from schemaId to a schema location url
*/
public Map<String, URL> resolveSchemaLocations() {
Expand All @@ -44,16 +38,16 @@ 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).getGroups()) {
for(final Schema schema: group.getSchemas()) {
for (final URI catalogLocation : catalogs.keySet()) {
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());

final String schemaId = schema.getId();
final URL schemaLocationUrl = schemaResolver.resolve(catalogLocation, location, baseLocation);

if(schemaLocations.containsKey(schemaId)) {
if (schemaLocations.containsKey(schemaId)) {
final URL otherLocation = schemaLocations.get(schemaId);
logger.warn(format("Found duplicate schema id '%s' for schemaLocations '%s' and '%s'", schemaId, otherLocation, schemaLocationUrl));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Map;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -21,18 +22,14 @@ 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 UrlConverter urlConverter;
@Inject
ObjectMapper objectMapper;

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

@Inject
UrlConverter urlConverter;

public Map<URI, Catalog> getCatalogs() {
return listAllCatalogsFromClasspath().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.io.IOException;
import java.net.URL;

import javax.enterprise.context.ApplicationScoped;

import org.apache.commons.io.IOUtils;

public class FileContentsAsStringLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@

import java.util.Map;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

public class JsonSchemaFileLoader {

private final FileContentsAsStringLoader fileContentsAsStringLoader;
private final CatalogToSchemaResolver catalogToSchemaResolver;
@Inject
FileContentsAsStringLoader fileContentsAsStringLoader;

@Inject
public JsonSchemaFileLoader(
final FileContentsAsStringLoader fileContentsAsStringLoader,
final CatalogToSchemaResolver catalogToSchemaResolver) {
this.fileContentsAsStringLoader = fileContentsAsStringLoader;
this.catalogToSchemaResolver = catalogToSchemaResolver;
}
CatalogToSchemaResolver catalogToSchemaResolver;

public Map<String, String> loadSchemas() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static java.lang.String.format;

import javax.enterprise.context.ApplicationScoped;

import org.everit.json.schema.Schema;
import org.everit.json.schema.loader.SchemaClient;
import org.everit.json.schema.loader.SchemaLoader;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package uk.gov.justice.schema.catalog;

import uk.gov.justice.schema.catalog.util.ClasspathResourceLoader;
import uk.gov.justice.schema.catalog.util.UriResolver;
import uk.gov.justice.schema.catalog.util.UrlConverter;
import uk.gov.justice.services.common.converter.jackson.ObjectMapperProducer;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.LoggerFactory;

public class ObjectFactory {

public UrlConverter urlConverter() {
return new UrlConverter();
}

public ObjectMapper objectMapper() {
return new ObjectMapperProducer().objectMapper();
}

public UriResolver uriResolver() {
return new UriResolver();
}

public JsonStringToSchemaConverter jsonStringToSchemaConverter() {
return new JsonStringToSchemaConverter();
}

public ClasspathResourceLoader classpathResourceLoader() {
return new ClasspathResourceLoader();
}

public ClasspathCatalogLoader classpathCatalogLoader() {
final ClasspathCatalogLoader classpathCatalogLoader = new ClasspathCatalogLoader();
classpathCatalogLoader.objectMapper = objectMapper();
classpathCatalogLoader.classpathResourceLoader = classpathResourceLoader();
classpathCatalogLoader.urlConverter = urlConverter();

return classpathCatalogLoader;
}

public SchemaResolver schemaResolver() {
final SchemaResolver schemaResolver = new SchemaResolver();
schemaResolver.urlConverter = urlConverter();
schemaResolver.uriResolver = uriResolver();
return schemaResolver;
}

public CatalogToSchemaResolver catalogToSchemaResolver() {
final CatalogToSchemaResolver catalogToSchemaResolver = new CatalogToSchemaResolver();
catalogToSchemaResolver.classpathCatalogLoader = classpathCatalogLoader();
catalogToSchemaResolver.schemaResolver = schemaResolver();
catalogToSchemaResolver.logger = LoggerFactory.getLogger(CatalogToSchemaResolver.class);

return catalogToSchemaResolver;
}

public FileContentsAsStringLoader fileContentsAsStringLoader() {
return new FileContentsAsStringLoader();
}

public JsonSchemaFileLoader jsonSchemaFileLoader() {
final JsonSchemaFileLoader jsonSchemaFileLoader = new JsonSchemaFileLoader();
jsonSchemaFileLoader.fileContentsAsStringLoader = fileContentsAsStringLoader();
jsonSchemaFileLoader.catalogToSchemaResolver = catalogToSchemaResolver();

return jsonSchemaFileLoader;
}

public SchemaClientFactory schemaClientFactory() {
return new SchemaClientFactory();
}

public Catalog catalog() {
final Catalog catalog = new Catalog();
catalog.rawCatalog = rawCatalog();
catalog.schemaClientFactory = schemaClientFactory();
catalog.jsonStringToSchemaConverter = jsonStringToSchemaConverter();

return catalog;
}

public RawCatalog rawCatalog() {
final RawCatalog rawCatalog = new RawCatalog();
rawCatalog.jsonSchemaFileLoader = jsonSchemaFileLoader();
rawCatalog.initialize();
return rawCatalog;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
import java.util.Optional;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

public class RawCatalog {

private final JsonSchemaFileLoader jsonSchemaFileLoader;
@Inject
JsonSchemaFileLoader jsonSchemaFileLoader;

private Map<String, String> schemaIdsToRawJsonSchemaCache = new HashMap<>();

@Inject
public RawCatalog(final JsonSchemaFileLoader jsonSchemaFileLoader) {
this.jsonSchemaFileLoader = jsonSchemaFileLoader;
}

@PostConstruct
public void initialize() {
schemaIdsToRawJsonSchemaCache = jsonSchemaFileLoader.loadSchemas();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uk.gov.justice.schema.catalog;

import javax.enterprise.context.ApplicationScoped;

import org.everit.json.schema.loader.SchemaClient;

public class SchemaClientFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@
import java.net.URL;
import java.util.Optional;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

public class SchemaResolver {

private static final String AN_EMPTY_STRING = "";

private final UrlConverter urlConverter;
private final UriResolver uriResolver;
@Inject
UrlConverter urlConverter;

@Inject
public SchemaResolver(final UrlConverter urlConverter, final UriResolver uriResolver) {
this.urlConverter = urlConverter;
this.uriResolver = uriResolver;
}
UriResolver uriResolver;

public URL resolve(
final URI catalogUri,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.net.URL;
import java.util.List;

import javax.enterprise.context.ApplicationScoped;

public class ClasspathResourceLoader {

public List<URL> getResources(final Class<?> clazz, final String location) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.net.URI;
import java.net.URISyntaxException;

import javax.enterprise.context.ApplicationScoped;

public class UriResolver {

public URI resolve(final URI baseUri, final URI otherUri) throws URISyntaxException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.net.URISyntaxException;
import java.net.URL;

import javax.enterprise.context.ApplicationScoped;

public class UrlConverter {

public URI toUri(final URL url) {
Expand Down
2 changes: 1 addition & 1 deletion catalog-core/src/main/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
version="1.1" bean-discovery-mode="annotated">
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package uk.gov.justice.schema.catalog;

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

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 CatalogProducerTest {

@InjectMocks
private CatalogProducer catalogProducer;

@Test
public void shouldCreateTheCatalog() throws Exception {

final Catalog catalog = catalogProducer.catalog();

assertThat(catalog, is(notNullValue()));

assertThat(getPrivateField(catalog, "rawCatalog", RawCatalog.class), is(notNullValue()));
assertThat(getPrivateField(catalog, "schemaClientFactory", SchemaClientFactory.class), is(notNullValue()));
assertThat(getPrivateField(catalog, "jsonStringToSchemaConverter", JsonStringToSchemaConverter.class), is(notNullValue()));
}

@SuppressWarnings("unchecked")
private <T> T getPrivateField(final Catalog catalog, final String fieldName, @SuppressWarnings("unused") final Class<T> clazz) throws Exception {

final Field field = catalog.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return (T) field.get(catalog);
}
}
Loading

0 comments on commit 168b998

Please sign in to comment.