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

Commit

Permalink
Merge pull request #582 from CJSCommonPlatform/fix-query-schema-catalog
Browse files Browse the repository at this point in the history
Fix query schema catalog
  • Loading branch information
Allan Mckenzie authored May 2, 2018
2 parents c8df465 + 82e189b commit 379b06a
Show file tree
Hide file tree
Showing 26 changed files with 431 additions and 261 deletions.
Original file line number Diff line number Diff line change
@@ -1,53 +1,32 @@
package uk.gov.justice.services.core.mapping;

import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toMap;

import uk.gov.justice.services.core.extension.BeanInstantiater;

import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

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

/**
* Constructs a cache of action name to media type from the List of {@link ActionNameToMediaTypesMapper}
* beans provided by the {@link ActionNameToMediaTypesMappingObserver}.
*/
@ApplicationScoped
public class DefaultMediaTypesMappingCache implements MediaTypesMappingCache {

@Inject
ActionNameToMediaTypesMappingObserver actionNameToMediaTypesMappingObserver;

@Inject
BeanInstantiater beanInstantiater;
MediaTypesMappingCacheInitialiser mediaTypesMappingCacheInitialiser;

private Map<String, MediaTypes> actionNameToMediaTypesCache;

/**
* Gets the List of {@link ActionNameToMediaTypesMapper} beans, instantiates them and collects all
* entries into a single Map of action name to media type.
*/
@PostConstruct
public void initialise() {
actionNameToMediaTypesCache = actionNameToMediaTypesMappingObserver.getNameMediaTypesMappers().stream()
.flatMap(this::mapEntriesFrom)
.collect(toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(schemaId_1, schemaId_2) -> schemaId_1
));
}

@Override
public Optional<MediaTypes> mediaTypesFor(final String actionName) {
return ofNullable(actionNameToMediaTypesCache.get(actionName));
}
public synchronized Optional<MediaTypes> mediaTypesFor(final String actionName) {

private Stream<Map.Entry<String, MediaTypes>> mapEntriesFrom(final Bean<?> bean) {
return ((ActionNameToMediaTypesMapper) beanInstantiater.instantiate(bean)).getActionNameToMediaTypesMap().entrySet().stream();
if (actionNameToMediaTypesCache == null) {
actionNameToMediaTypesCache = mediaTypesMappingCacheInitialiser.initialiseCache();
}

return ofNullable(actionNameToMediaTypesCache.get(actionName));
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package uk.gov.justice.services.core.mapping;

import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toMap;

import uk.gov.justice.services.core.extension.BeanInstantiater;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Stream;

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

/**
Expand All @@ -23,34 +16,17 @@
public class DefaultSchemaIdMappingCache implements SchemaIdMappingCache {

@Inject
SchemaIdMappingObserver schemaIdMappingObserver;

@Inject
BeanInstantiater beanInstantiater;
SchemaIdMappingCacheInitialiser schemaIdMappingCacheInitialiser;

private Map<MediaType, String> mediaTypeToSchemaIdCache;

/**
* Gets the List of {@link MediaTypeToSchemaIdMapper} beans, instantiates them and collects all
* entries into a single Map of media type to schema id.
*/
@PostConstruct
public void initialise() {
mediaTypeToSchemaIdCache = schemaIdMappingObserver.getMediaTypeToSchemaIdMappers().stream()
.flatMap(this::mapEntriesFrom)
.collect(toMap(
Entry::getKey,
Entry::getValue,
(schemaId_1, schemaId_2) -> schemaId_1
));
}

@Override
public Optional<String> schemaIdFor(final MediaType mediaType) {
return ofNullable(mediaTypeToSchemaIdCache.get(mediaType));
}
public synchronized Optional<String> schemaIdFor(final MediaType mediaType) {

private Stream<Entry<MediaType, String>> mapEntriesFrom(final Bean<?> bean) {
return ((MediaTypeToSchemaIdMapper) beanInstantiater.instantiate(bean)).getMediaTypeToSchemaIdMap().entrySet().stream();
if(mediaTypeToSchemaIdCache == null) {
mediaTypeToSchemaIdCache = schemaIdMappingCacheInitialiser.initialiseCache();
}

return ofNullable(mediaTypeToSchemaIdCache.get(mediaType));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package uk.gov.justice.services.core.mapping;

import static java.util.stream.Collectors.toMap;

import uk.gov.justice.services.core.extension.BeanInstantiater;

import java.util.Map;
import java.util.stream.Stream;

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

@ApplicationScoped
public class MediaTypesMappingCacheInitialiser {

@Inject
ActionNameToMediaTypesMappingObserver actionNameToMediaTypesMappingObserver;

@Inject
BeanInstantiater beanInstantiater;

public Map<String, MediaTypes> initialiseCache() {
return actionNameToMediaTypesMappingObserver.getNameMediaTypesMappers().stream()
.flatMap(this::mapEntriesFrom)
.collect(toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(schemaId_1, schemaId_2) -> schemaId_1
));
}

private Stream<Map.Entry<String, MediaTypes>> mapEntriesFrom(final Bean<?> bean) {
return ((ActionNameToMediaTypesMapper) beanInstantiater.instantiate(bean)).getActionNameToMediaTypesMap().entrySet().stream();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uk.gov.justice.services.core.mapping;

import static java.util.stream.Collectors.toMap;

import uk.gov.justice.services.core.extension.BeanInstantiater;

import java.util.Map;
import java.util.stream.Stream;

import javax.enterprise.inject.spi.Bean;
import javax.inject.Inject;

public class SchemaIdMappingCacheInitialiser {

@Inject
SchemaIdMappingObserver schemaIdMappingObserver;

@Inject
BeanInstantiater beanInstantiater;

public Map<MediaType, String> initialiseCache() {
return schemaIdMappingObserver.getMediaTypeToSchemaIdMappers().stream()
.flatMap(this::mapEntriesFrom)
.collect(toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(schemaId_1, schemaId_2) -> schemaId_1
));
}

private Stream<Map.Entry<MediaType, String>> mapEntriesFrom(final Bean<?> bean) {
return ((MediaTypeToSchemaIdMapper) beanInstantiater.instantiate(bean)).getMediaTypeToSchemaIdMap().entrySet().stream();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import uk.gov.justice.services.core.mapping.DefaultMediaTypesMappingCache;
import uk.gov.justice.services.core.mapping.DefaultNameToMediaTypeConverter;
import uk.gov.justice.services.core.mapping.DefaultSchemaIdMappingCache;
import uk.gov.justice.services.core.mapping.MediaTypesMappingCacheInitialiser;
import uk.gov.justice.services.core.mapping.SchemaIdMappingCacheInitialiser;
import uk.gov.justice.services.core.mapping.SchemaIdMappingObserver;
import uk.gov.justice.services.core.requester.RequesterProducer;
import uk.gov.justice.services.core.sender.SenderProducer;
Expand Down Expand Up @@ -156,7 +158,10 @@ public class AllEventsHandlerIT {
MediaTypeProvider.class,
EnvelopeInspector.class,
RequesterProducer.class,
BackwardsCompatibleJsonSchemaValidator.class
BackwardsCompatibleJsonSchemaValidator.class,

MediaTypesMappingCacheInitialiser.class,
SchemaIdMappingCacheInitialiser.class
})
public WebApp war() {
return new WebApp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import uk.gov.justice.services.core.mapping.DefaultMediaTypesMappingCache;
import uk.gov.justice.services.core.mapping.DefaultNameToMediaTypeConverter;
import uk.gov.justice.services.core.mapping.DefaultSchemaIdMappingCache;
import uk.gov.justice.services.core.mapping.MediaTypesMappingCacheInitialiser;
import uk.gov.justice.services.core.mapping.SchemaIdMappingCacheInitialiser;
import uk.gov.justice.services.core.mapping.SchemaIdMappingObserver;
import uk.gov.justice.services.core.requester.RequesterProducer;
import uk.gov.justice.services.core.sender.SenderProducer;
Expand Down Expand Up @@ -156,7 +158,10 @@ public class CustomServiceComponentHandlerIT {
MediaTypeProvider.class,
EnvelopeInspector.class,
RequesterProducer.class,
BackwardsCompatibleJsonSchemaValidator.class
BackwardsCompatibleJsonSchemaValidator.class,

MediaTypesMappingCacheInitialiser.class,
SchemaIdMappingCacheInitialiser.class

})
public WebApp war() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
import uk.gov.justice.services.core.mapping.DefaultMediaTypesMappingCache;
import uk.gov.justice.services.core.mapping.DefaultNameToMediaTypeConverter;
import uk.gov.justice.services.core.mapping.DefaultSchemaIdMappingCache;
import uk.gov.justice.services.core.mapping.MediaTypesMappingCacheInitialiser;
import uk.gov.justice.services.core.mapping.SchemaIdMappingCacheInitialiser;
import uk.gov.justice.services.core.mapping.SchemaIdMappingObserver;
import uk.gov.justice.services.core.requester.RequesterProducer;
import uk.gov.justice.services.core.sender.SenderProducer;
Expand Down Expand Up @@ -163,7 +165,10 @@ public class EventHandlerIT {
MediaTypeProvider.class,
EnvelopeInspector.class,
RequesterProducer.class,
BackwardsCompatibleJsonSchemaValidator.class
BackwardsCompatibleJsonSchemaValidator.class,

MediaTypesMappingCacheInitialiser.class,
SchemaIdMappingCacheInitialiser.class
})
public WebApp war() {
return new WebApp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import uk.gov.justice.services.core.mapping.DefaultMediaTypesMappingCache;
import uk.gov.justice.services.core.mapping.DefaultNameToMediaTypeConverter;
import uk.gov.justice.services.core.mapping.DefaultSchemaIdMappingCache;
import uk.gov.justice.services.core.mapping.MediaTypesMappingCacheInitialiser;
import uk.gov.justice.services.core.mapping.SchemaIdMappingCacheInitialiser;
import uk.gov.justice.services.core.mapping.SchemaIdMappingObserver;
import uk.gov.justice.services.core.requester.Requester;
import uk.gov.justice.services.core.requester.RequesterProducer;
Expand Down Expand Up @@ -165,7 +167,10 @@ public class SenderRequesterHandlerIT {
MediaTypeProvider.class,
EnvelopeInspector.class,
RequesterProducer.class,
BackwardsCompatibleJsonSchemaValidator.class
BackwardsCompatibleJsonSchemaValidator.class,

MediaTypesMappingCacheInitialiser.class,
SchemaIdMappingCacheInitialiser.class
})
public WebApp war() {
return new WebApp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;

@RunWith(MockitoJUnitRunner.class)
public class DefaultJsonValidationLoggerHelperTest {
Expand Down Expand Up @@ -139,4 +136,4 @@ private JSONObject badObject() throws IOException {
final InputStream inputStream = this.getClass().getResourceAsStream(format(JSON_LOCATION_PATTERN, "fail"));
return new JSONObject(IOUtils.toString(inputStream, defaultCharset().name()));
}
}
}
Loading

0 comments on commit 379b06a

Please sign in to comment.