From 825baec46704ef2d613e572a264710348a643ae5 Mon Sep 17 00:00:00 2001 From: m55624 Date: Wed, 27 Sep 2017 16:06:51 +0100 Subject: [PATCH 01/72] gh-1297 - FederatedStore changed to use a cache for Graph storage --- .../java/uk/gov/gchq/gaffer/cache/ICache.java | 22 +- .../gov/gchq/gaffer/cache/ICacheService.java | 42 +- .../exception/OverwritingException.java | 2 +- .../java/uk/gov/gchq/gaffer/store/Store.java | 9 +- .../gaffer/store/exception/package-info.java | 20 - .../store/library/FileGraphLibrary.java | 2 +- .../gaffer/store/library/GraphLibrary.java | 2 +- .../store/library/HashMapGraphLibrary.java | 2 +- .../gaffer/store/library/NoGraphLibrary.java | 2 +- .../store/library/FileGraphLibraryTest.java | 2 +- .../library/HashMapGraphLibraryTest.java | 2 +- .../gaffer/federatedstore/FederatedStore.java | 371 +++++++++--------- .../FederatedStoreSchemaTest.java | 5 +- .../federatedstore/FederatedStoreTest.java | 5 +- .../impl/FederatedAddGraphHandlerTest.java | 8 +- .../properties/federatedStoreTest.properties | 1 + .../singleUseMockAccStore.properties | 1 + .../singleUseMockMapStore.properties | 1 + .../singleUseMockMapStoreAlt.properties | 1 + 19 files changed, 258 insertions(+), 242 deletions(-) rename core/{store/src/main/java/uk/gov/gchq/gaffer/store => common-util/src/main/java/uk/gov/gchq/gaffer/commonutil}/exception/OverwritingException.java (97%) delete mode 100644 core/store/src/main/java/uk/gov/gchq/gaffer/store/exception/package-info.java diff --git a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICache.java b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICache.java index 35aef1b44d6..b80e713dd2a 100644 --- a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICache.java +++ b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICache.java @@ -17,7 +17,7 @@ package uk.gov.gchq.gaffer.cache; import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; -import uk.gov.gchq.gaffer.core.exception.Status; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import java.util.Collection; import java.util.Set; @@ -29,7 +29,7 @@ * @param The object type that acts as the key for the cache * @param The value that is stored in the cache */ -public interface ICache { +public interface ICache { /** * Retrieve the value associated with the given key. @@ -42,7 +42,7 @@ public interface ICache { /** * Add a new key-value pair to the cache. * - * @param key the key to add + * @param key the key to add * @param value the value to add * @throws CacheOperationException if there is an error adding the new key-value pair to the cache */ @@ -51,15 +51,19 @@ public interface ICache { /** * Add a new key-value pair to the cache, but only if there is existing entry associated with the specified key. * - * @param key the key to add + * @param key the key to add * @param value the value to add * @throws CacheOperationException if the specified key already exists in the cache with a non-null value */ - default void putSafe(final K key, final V value) throws CacheOperationException { + default void putSafe(final K key, final V value) throws OverwritingException, CacheOperationException { if (null == get(key)) { - put(key, value); + try { + put(key, value); + } catch (CacheOperationException e) { + throw e; + } } else { - throw new CacheOperationException("Cache entry already exists for key: " + key, Status.CONFLICT); + throw new OverwritingException("Cache entry already exists for key: " + key); } } @@ -73,14 +77,14 @@ default void putSafe(final K key, final V value) throws CacheOperationException /** * Get all values present in the cache. * - * @return a {@link Collection} containing all of the cache values + * @return a {@link Collection} containing all of the cache values */ Collection getAllValues(); /** * Get all keys present in the cache. * - * @return a {@link Set} containing all of the cache keys + * @return a {@link Set} containing all of the cache keys */ Set getAllKeys(); diff --git a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICacheService.java b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICacheService.java index b007e1c9e73..ad31f46eb62 100644 --- a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICacheService.java +++ b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICacheService.java @@ -45,8 +45,8 @@ public interface ICacheService { * Get the named cache from the cache service. * * @param cacheName the name of the cache to retrieve - * @param The object type that acts as the key for the cache - * @param The value that is stored in the cache + * @param The object type that acts as the key for the cache + * @param The value that is stored in the cache * @return the requested cache object */ ICache getCache(final String cacheName); @@ -55,9 +55,9 @@ public interface ICacheService { * Get the value associated with the specified cache and key. * * @param cacheName the name of the cache to look in - * @param key the key of the entry to lookup - * @param The object type that acts as the key for the cache - * @param The value that is stored in the cache + * @param key the key of the entry to lookup + * @param The object type that acts as the key for the cache + * @param The value that is stored in the cache * @return the requested cache object */ default V getFromCache(final String cacheName, final K key) { @@ -69,10 +69,10 @@ default V getFromCache(final String cacheName, final K key) { * Add a new key-value pair to the specified cache. * * @param cacheName the name of the cache - * @param key the key to add - * @param value the value to add - * @param The object type that acts as the key for the cache - * @param The value that is stored in the cache + * @param key the key to add + * @param value the value to add + * @param The object type that acts as the key for the cache + * @param The value that is stored in the cache * @throws CacheOperationException if there is an error adding the new key-value pair to the cache */ default void putInCache(final String cacheName, final K key, final V value) throws CacheOperationException { @@ -81,14 +81,14 @@ default void putInCache(final String cacheName, final K key, final V valu } /** - * Add a new key-value pair to the specified cache, but only if there is existing + * Add a new key-value pair to the specified cache, but only if there is no existing * entry associated with the specified key. * * @param cacheName the name of the cache - * @param key the key to add - * @param value the value to add - * @param The object type that acts as the key for the cache - * @param The value that is stored in the cache + * @param key the key to add + * @param value the value to add + * @param The object type that acts as the key for the cache + * @param The value that is stored in the cache * @throws CacheOperationException if the specified key already exists in the cache with a non-null value */ default void putSafeInCache(final String cacheName, final K key, final V value) throws CacheOperationException { @@ -100,9 +100,9 @@ default void putSafeInCache(final String cacheName, final K key, final V * Remove the entry associated with the specified key from the specified cache. * * @param cacheName the name of the cache to look in - * @param key the key of the entry to remove - * @param The object type that acts as the key for the cache - * @param The value that is stored in the cache + * @param key the key of the entry to remove + * @param The object type that acts as the key for the cache + * @param The value that is stored in the cache */ default void removeFromCache(final String cacheName, final K key) { final ICache cache = getCache(cacheName); @@ -113,8 +113,8 @@ default void removeFromCache(final String cacheName, final K key) { * Get all of the values associated with the specified cache. * * @param cacheName the name of the cache to look in - * @param The object type that acts as the key for the cache - * @param The value that is stored in the cache + * @param The object type that acts as the key for the cache + * @param The value that is stored in the cache * @return the requested cache objects */ default Collection getAllValuesFromCache(final String cacheName) { @@ -126,8 +126,8 @@ default Collection getAllValuesFromCache(final String cacheName) { * Get all of the keys associated with the specified cache. * * @param cacheName the name of the cache to look in - * @param The object type that acts as the key for the cache - * @param The value that is stored in the cache + * @param The object type that acts as the key for the cache + * @param The value that is stored in the cache * @return the requested cache keys */ default Set getAllKeysFromCache(final String cacheName) { diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/exception/OverwritingException.java b/core/common-util/src/main/java/uk/gov/gchq/gaffer/commonutil/exception/OverwritingException.java similarity index 97% rename from core/store/src/main/java/uk/gov/gchq/gaffer/store/exception/OverwritingException.java rename to core/common-util/src/main/java/uk/gov/gchq/gaffer/commonutil/exception/OverwritingException.java index b443ca2ab34..dc52cdd6abe 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/exception/OverwritingException.java +++ b/core/common-util/src/main/java/uk/gov/gchq/gaffer/commonutil/exception/OverwritingException.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package uk.gov.gchq.gaffer.store.exception; +package uk.gov.gchq.gaffer.commonutil.exception; /** * An {@code OverwritingException} should be thrown when a condition will cause diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java index 490b0f7f221..5385e1fb51c 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java @@ -662,6 +662,10 @@ protected final OperationHandler getOperationHandler(final Class operationChain, final Context context, final String msg, final JobStatus jobStatus) { final JobDetail newJobDetail = new JobDetail(context.getJobId(), context .getUser() @@ -786,9 +790,4 @@ private void addConfiguredOperationHandlers() { } } } - - private void startCacheServiceLoader(final StoreProperties properties) { - CacheServiceLoader.initialise(properties.getProperties()); - } - } diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/exception/package-info.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/exception/package-info.java deleted file mode 100644 index 0075dbc6d63..00000000000 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/exception/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2017 Crown Copyright - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Exceptions relating to the Gaffer Store class. - */ -package uk.gov.gchq.gaffer.store.exception; diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/FileGraphLibrary.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/FileGraphLibrary.java index 98dc0cf5571..9536ba2ab12 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/FileGraphLibrary.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/FileGraphLibrary.java @@ -22,7 +22,7 @@ import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException; import uk.gov.gchq.gaffer.store.StoreProperties; -import uk.gov.gchq.gaffer.store.exception.OverwritingException; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import java.io.FileOutputStream; import java.io.IOException; diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/GraphLibrary.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/GraphLibrary.java index 5fe30f39733..ef7193b8f8e 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/GraphLibrary.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/GraphLibrary.java @@ -20,7 +20,7 @@ import uk.gov.gchq.gaffer.commonutil.StringUtil; import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.store.StoreProperties; -import uk.gov.gchq.gaffer.store.exception.OverwritingException; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.store.schema.Schema; import java.util.regex.Pattern; diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibrary.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibrary.java index d83005fc34e..aa4649d0381 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibrary.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibrary.java @@ -18,7 +18,7 @@ import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.store.StoreProperties; -import uk.gov.gchq.gaffer.store.exception.OverwritingException; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import java.util.HashMap; import java.util.Map; diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/NoGraphLibrary.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/NoGraphLibrary.java index ef95d02287b..e5bb6279d05 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/NoGraphLibrary.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/NoGraphLibrary.java @@ -21,7 +21,7 @@ import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.store.StoreProperties; -import uk.gov.gchq.gaffer.store.exception.OverwritingException; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; /** * A {@code NoGraphLibrary} will not store any relationships between graphIds, diff --git a/core/store/src/test/java/uk/gov/gchq/gaffer/store/library/FileGraphLibraryTest.java b/core/store/src/test/java/uk/gov/gchq/gaffer/store/library/FileGraphLibraryTest.java index 16870f18893..b9b7fe5d9dd 100644 --- a/core/store/src/test/java/uk/gov/gchq/gaffer/store/library/FileGraphLibraryTest.java +++ b/core/store/src/test/java/uk/gov/gchq/gaffer/store/library/FileGraphLibraryTest.java @@ -23,7 +23,7 @@ import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.store.StoreProperties; -import uk.gov.gchq.gaffer.store.exception.OverwritingException; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.store.schema.Schema; import java.io.File; diff --git a/core/store/src/test/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibraryTest.java b/core/store/src/test/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibraryTest.java index ab7b0433148..8286a2de546 100644 --- a/core/store/src/test/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibraryTest.java +++ b/core/store/src/test/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibraryTest.java @@ -22,7 +22,7 @@ import uk.gov.gchq.gaffer.commonutil.TestGroups; import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.store.StoreProperties; -import uk.gov.gchq.gaffer.store.exception.OverwritingException; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.store.schema.Schema; import uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition; diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index abc5c179401..7f1c7005e8f 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -17,11 +17,14 @@ package uk.gov.gchq.gaffer.federatedstore; import com.google.common.base.Strings; -import com.google.common.collect.Maps; import com.google.common.collect.Sets; import org.apache.commons.lang3.StringUtils; +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; +import uk.gov.gchq.gaffer.cache.ICacheService; +import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; import uk.gov.gchq.gaffer.commonutil.StreamUtil; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; import uk.gov.gchq.gaffer.data.element.Element; import uk.gov.gchq.gaffer.data.element.id.EntityId; @@ -53,7 +56,6 @@ import uk.gov.gchq.gaffer.store.StoreException; import uk.gov.gchq.gaffer.store.StoreProperties; import uk.gov.gchq.gaffer.store.StoreTrait; -import uk.gov.gchq.gaffer.store.exception.OverwritingException; import uk.gov.gchq.gaffer.store.library.GraphLibrary; import uk.gov.gchq.gaffer.store.operation.handler.OperationHandler; import uk.gov.gchq.gaffer.store.operation.handler.OutputOperationHandler; @@ -69,9 +71,9 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.Set; import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * A Store that encapsulates a collection of sub-graphs and executes operations @@ -97,15 +99,27 @@ public class FederatedStore extends Store { private static final String SCHEMA = "schema"; private static final String PROPERTIES = "properties"; private static final String ID = "id"; - private final Map graphs = Maps.newHashMap(); + private static final String CACHE_SERVICE_NAME = "federatedStoreGraphs"; + private ICacheService cacheService; private Set traits = new HashSet<>(); private Set customPropertiesAuths; - private static List getCleanStrings(final String value) { - final List values = Arrays.asList(StringUtils.stripAll(value.split(SCHEMA_DEL_REGEX))); - values.remove(""); - values.remove(null); - return values; + /** + * Initialise this FederatedStore with any sub-graphs defined within the + * properties. + * + * @param graphId the graphId to label this FederatedStore. + * @param unused unused + * @param properties properties to initialise this FederatedStore with, can + * contain details on graphs to add to scope. + * @throws StoreException exception + */ + @Override + public void initialise(final String graphId, final Schema unused, final StoreProperties properties) throws StoreException { + super.initialise(graphId, new Schema(), properties); + setCacheService(); + loadCustomPropertiesAuths(); + loadGraphs(); } /** @@ -144,6 +158,138 @@ public static OP updateOperationForGraph(final OP operati return resultOp; } + /** + * Adds graphs to the scope of FederatedStore. + *

+ * To be used by the FederatedStore and Handlers only. Users should add + * graphs via the {@link AddGraph} operation. + * + * @param graphs the graph to add + */ + public void addGraphs(final Graph... graphs) { + for (final Graph graph : graphs) { + if (cacheService == null) { + startCacheServiceLoader(graph.getStoreProperties()); + setCacheService(); + } + _add(graph); + } + } + + /** + * Removes graphs from the scope of FederatedStore. + *

+ * To be used by the FederatedStore and Handlers only. Users should remove + * graphs via the {@link RemoveGraph} operation. + * + * @param graphId to be removed from scope + */ + public void remove(final String graphId) { + cacheService.removeFromCache(CACHE_SERVICE_NAME, graphId); + updateMergedGraphConfig(); + } + + + /** + * @return All the graphId(s) within scope of this FederatedStore. + */ + public Set getAllGraphIds() { + return Collections.unmodifiableSet(cacheService.getAllKeysFromCache(CACHE_SERVICE_NAME)); + } + + /** + * @return {@link Store#getTraits()} + */ + @Override + public Set getTraits() { + return traits; + } + + /** + * Gets a collection of graph objects within FederatedStore scope from the + * given csv of graphIds. + *

+ * if graphIdsCsv is null then all graph objects within FederatedStore + * scope are returned. + * + * @param graphIdsCsv the csv of graphIds to get, null returns all graphs. + * @return the graph collection. + */ + public Collection getGraphs(final String graphIdsCsv) { + if (null == graphIdsCsv) { + if (cacheService == null) { + return new ArrayList<>(); + } else { + return cacheService.getAllValuesFromCache(CACHE_SERVICE_NAME); + } + } + + final String[] graphIds = graphIdsCsv.split(","); + final Collection graphs = new ArrayList<>(); + for (final String graphId : graphIds) { + if (cacheService.getFromCache(CACHE_SERVICE_NAME, graphId) != null) { + graphs.add(cacheService.getFromCache(CACHE_SERVICE_NAME, graphId)); + } + } + return graphs; + } + + /** + * The FederatedStore at time of initialisation, can set the auths required + * to allow users to use custom {@link StoreProperties} outside the + * scope of the {@link GraphLibrary}. + * + * @param user the user needing validation for custom property usage. + * @return boolean permission + */ + public boolean isLimitedToLibraryProperties(final User user) { + return null != this.customPropertiesAuths && Collections.disjoint(user.getOpAuths(), this.customPropertiesAuths); + } + + @Override + protected void addAdditionalOperationHandlers() { + // Override the Operations that don't have an output + getSupportedOperations() + .stream() + .filter(op -> !Output.class.isAssignableFrom(op) && !AddElements.class.equals(op)) + .forEach(op -> addOperationHandler(op, new FederatedOperationHandler())); + + addOperationHandler(GetAllGraphIds.class, new FederatedGetAllGraphIDHandler()); + addOperationHandler(AddGraph.class, new FederatedAddGraphHandler()); + addOperationHandler(RemoveGraph.class, new FederatedRemoveGraphHandler()); + } + + @Override + protected OutputOperationHandler> getGetElementsHandler() { + return new FederatedGetElementsHandler(); + } + + @Override + protected OutputOperationHandler> getGetAllElementsHandler() { + return new FederatedGetAllElementsHandler(); + } + + @Override + protected OutputOperationHandler> getAdjacentIdsHandler() { + return new FederatedGetAdjacentIdsHandler(); + } + + @Override + protected OperationHandler getAddElementsHandler() { + return new FederatedOperationAddElementsHandler(); + } + + @Override + protected Class getRequiredParentSerialiserClass() { + return Serialiser.class; + } + + @Override + protected Object doUnhandledOperation(final Operation operation, + final Context context) { + throw new UnsupportedOperationException(); + } + private static View createValidView(final View view, final Schema delegateGraphSchema) { View newView; if (view.hasGroups()) { @@ -171,21 +317,11 @@ private static View createValidView(final View view, final Schema delegateGraphS return newView; } - /** - * Initialise this FederatedStore with any sub-graphs defined within the - * properties. - * - * @param graphId the graphId to label this FederatedStore. - * @param unused unused - * @param properties properties to initialise this FederatedStore with, can - * contain details on graphs to add to scope. - * @throws StoreException exception - */ - @Override - public void initialise(final String graphId, final Schema unused, final StoreProperties properties) throws StoreException { - super.initialise(graphId, new Schema(), properties); - loadCustomPropertiesAuths(); - loadGraphs(); + private static List getCleanStrings(final String value) { + final List values = Arrays.asList(StringUtils.stripAll(value.split(SCHEMA_DEL_REGEX))); + values.remove(""); + values.remove(null); + return values; } private void loadCustomPropertiesAuths() { @@ -196,17 +332,24 @@ private void loadCustomPropertiesAuths() { } private void loadGraphs() { - final HashSet graphIds = getGraphIds(); + final Set graphIds = getGraphIds(); + final Set graphsToLoad = new HashSet<>(); for (final String graphId : graphIds) { - + final Graph graph; final Builder builder = new Builder().config(new GraphConfig.Builder() .graphId(graphId) .library(getGraphLibrary()) .build()); resolveConfiguration(graphId, builder); - addGraphs(builder); + try { + graph = builder.build(); + } catch (final Exception e) { + throw new IllegalArgumentException(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Graph", ""), e); + } + graphsToLoad.add(graph); } + addGraphs(graphsToLoad.toArray(new Graph[graphsToLoad.size()])); } private void resolveConfiguration(final String graphId, final Builder builder) { @@ -244,18 +387,6 @@ private void resolveSchema(final String graphId, final Builder builder) { addSchemaFromFile(graphId, builder); } - private void addGraphs(final Builder... builders) { - for (final Builder builder : builders) { - final Graph graph; - try { - graph = builder.build(); - } catch (final Exception e) { - throw new IllegalArgumentException(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Graph", ""), e); - } - addGraphs(graph); - } - } - private void addSchemaFromLibrary(final String graphId, final Builder builder) { final String schemaIdValue = getValueOf(graphId, SCHEMA, ID); if (!Strings.isNullOrEmpty(schemaIdValue)) { @@ -315,7 +446,7 @@ private String getValueOf(final String graphId, final String properties, final S return getProperties().get(key); } - private HashSet getGraphIds() { + private Set getGraphIds() { final HashSet graphIds = Sets.newHashSet(); final String idKey = GAFFER_FEDERATED_STORE + GRAPH_IDS; final String graphIdValue = getProperties().get(idKey); @@ -325,31 +456,19 @@ private HashSet getGraphIds() { return graphIds; } - /** - * Adds graphs to the scope of FederatedStore. - *

- * To be used by the FederatedStore and Handlers only. Users should add - * graphs via the {@link AddGraph} operation. - * - * @param graphs the graph to add - */ - public void addGraphs(final Graph... graphs) { - for (final Graph graph : graphs) { - _add(graph); - } - } - private void _add(final Graph newGraph) { final String graphId = newGraph.getGraphId(); - if (graphs.containsKey(graphId)) { - throw new OverwritingException((String.format(USER_IS_ATTEMPTING_TO_OVERWRITE_A_GRAPH_WITHIN_FEDERATED_STORE_GRAPH_ID_S, graphId))); - } - Schema.Builder schemaBuilder = new Schema.Builder(); final Set newTraits = Sets.newHashSet(StoreTrait.values()); - for (final Graph graph : graphs.values()) { - schemaBuilder = schemaBuilder.merge(graph.getSchema()); - newTraits.retainAll(graph.getStoreTraits()); + if (cacheService != null) { + List cachedGraphs = cacheService.getAllValuesFromCache(CACHE_SERVICE_NAME) + .stream() + .map(element -> (Graph) element) + .collect(Collectors.toList()); + for (final Graph graph : cachedGraphs) { + schemaBuilder = schemaBuilder.merge(graph.getSchema()); + newTraits.retainAll(graph.getStoreTraits()); + } } schemaBuilder.merge(newGraph.getSchema()); @@ -358,7 +477,13 @@ private void _add(final Graph newGraph) { final Schema newSchema = schemaBuilder.build(); //An exception would be thrown here if something was wrong merging the schema. - graphs.put(graphId, newGraph); + try { + cacheService.putSafeInCache(CACHE_SERVICE_NAME, graphId, newGraph); + } catch (OverwritingException e) { + throw new OverwritingException((String.format(USER_IS_ATTEMPTING_TO_OVERWRITE_A_GRAPH_WITHIN_FEDERATED_STORE_GRAPH_ID_S, graphId))); + } catch (CacheOperationException e) { + throw new RuntimeException(e); + } schema = newSchema; traits = Collections.unmodifiableSet(newTraits); @@ -370,126 +495,18 @@ private void _add(final Graph newGraph) { private void updateMergedGraphConfig() { Schema.Builder schemaBuilder = new Schema.Builder(); final Set newTraits = Sets.newHashSet(StoreTrait.values()); - for (final Graph graph : graphs.values()) { - schemaBuilder = schemaBuilder.merge(graph.getSchema()); - newTraits.retainAll(graph.getStoreTraits()); + for (final Object graph : cacheService.getAllValuesFromCache(CACHE_SERVICE_NAME)) { + final Graph graph2 = (Graph) graph; + schemaBuilder = schemaBuilder.merge(graph2.getSchema()); + newTraits.retainAll(graph2.getStoreTraits()); } - schema = schemaBuilder.build(); traits = Collections.unmodifiableSet(newTraits); } - @Override - protected void addAdditionalOperationHandlers() { - // Override the Operations that don't have an output - getSupportedOperations() - .stream() - .filter(op -> !Output.class.isAssignableFrom(op) && !AddElements.class.equals(op)) - .forEach(op -> addOperationHandler(op, new FederatedOperationHandler())); - - addOperationHandler(GetAllGraphIds.class, new FederatedGetAllGraphIDHandler()); - addOperationHandler(AddGraph.class, new FederatedAddGraphHandler()); - addOperationHandler(RemoveGraph.class, new FederatedRemoveGraphHandler()); - } - - /** - * Removes graphs from the scope of FederatedStore. - *

- * To be used by the FederatedStore and Handlers only. Users should remove - * graphs via the {@link RemoveGraph} operation. - * - * @param graphId to be removed from scope - */ - public void remove(final String graphId) { - graphs.remove(graphId); - updateMergedGraphConfig(); - } - - - /** - * @return All the graphId(s) within scope of this FederatedStore. - */ - public Set getAllGraphIds() { - return Collections.unmodifiableSet(graphs.keySet()); - } - - /** - * @return {@link Store#getTraits()} - */ - @Override - public Set getTraits() { - return traits; - } - - /** - * Gets a collection of graph objects within FederatedStore scope from the - * given csv of graphIds. - *

- * if graphIdsCsv is null then all graph objects within FederatedStore - * scope - * are returned. - * - * @param graphIdsCsv the csv of graphIds to get, null returns all graphs. - * @return the graph collection. - */ - public Collection getGraphs(final String graphIdsCsv) { - if (null == graphIdsCsv) { - return graphs.values(); - } - - final String[] graphIds = graphIdsCsv.split(","); - final Collection filteredGraphs = new ArrayList<>(); - for (final String graphId : graphIds) { - if (graphs.containsKey(graphId)) { - final Graph graph = graphs.get(graphId); - if (null != graph) { - filteredGraphs.add(graph); - } - } + private void setCacheService() { + if (CacheServiceLoader.getService() != null) { + this.cacheService = CacheServiceLoader.getService(); } - return filteredGraphs; - } - - @Override - protected OutputOperationHandler> getGetElementsHandler() { - return new FederatedGetElementsHandler(); - } - - @Override - protected OutputOperationHandler> getGetAllElementsHandler() { - return new FederatedGetAllElementsHandler(); - } - - @Override - protected OutputOperationHandler> getAdjacentIdsHandler() { - return new FederatedGetAdjacentIdsHandler(); - } - - @Override - protected OperationHandler getAddElementsHandler() { - return new FederatedOperationAddElementsHandler(); - } - - @Override - protected Class getRequiredParentSerialiserClass() { - return Serialiser.class; - } - - @Override - protected Object doUnhandledOperation(final Operation operation, - final Context context) { - throw new UnsupportedOperationException(); - } - - /** - * The FederatedStore at time of initialisation, can set the auths required - * to allow users to use custom {@link StoreProperties} outside the - * scope of the {@link GraphLibrary}. - * - * @param user the user needing validation for custom property usage. - * @return boolean permission - */ - public boolean isLimitedToLibraryProperties(final User user) { - return null != this.customPropertiesAuths && Collections.disjoint(user.getOpAuths(), this.customPropertiesAuths); } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java index 4034d2e48dc..e0e6626a139 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java @@ -23,6 +23,7 @@ import uk.gov.gchq.gaffer.accumulostore.AccumuloProperties; import uk.gov.gchq.gaffer.accumulostore.MockAccumuloStore; +import uk.gov.gchq.gaffer.cache.util.CacheProperties; import uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException; import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; import uk.gov.gchq.gaffer.operation.Operation; @@ -59,9 +60,11 @@ public class FederatedStoreSchemaTest { public void setUp() throws Exception { ACCUMULO_PROPERTIES.setStoreClass(MockAccumuloStore.class.getName()); ACCUMULO_PROPERTIES.setStorePropertiesClass(AccumuloProperties.class); + ACCUMULO_PROPERTIES.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); FEDERATED_PROPERTIES.setStoreClass(FederatedStore.class.getName()); FEDERATED_PROPERTIES.setStorePropertiesClass(StoreProperties.class); + FEDERATED_PROPERTIES.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); fStore = new FederatedStore(); fStore.initialise(TEST_FED_STORE, null, FEDERATED_PROPERTIES); @@ -118,7 +121,7 @@ public void shouldNotDeadLockWhenPreviousAddGraphHasSchemaCollision() throws Exc assertTrue(e instanceof SchemaException); assertEquals("Element group properties cannot be defined in different" + " schema parts, they must all be defined in a single " + - "schema part. Please fix this group: e1",e.getMessage()); + "schema part. Please fix this group: e1", e.getMessage()); } try { diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index c210d362943..aa8ad46f625 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -24,6 +24,7 @@ import org.mockito.Mockito; import uk.gov.gchq.gaffer.accumulostore.SingleUseAccumuloStore; +import uk.gov.gchq.gaffer.cache.util.CacheProperties; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; import uk.gov.gchq.gaffer.commonutil.pair.Pair; @@ -94,6 +95,7 @@ public class FederatedStoreTest { public void setUp() throws Exception { store = new FederatedStore(); federatedProperties = new StoreProperties(); + federatedProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); HashMapGraphLibrary.clear(); } @@ -818,8 +820,9 @@ public void shouldReturnNoGraphsFromEmptyString() throws StoreException { } @Test - public void shouldReturnGraphsWithLeadingCommaString() throws StoreException { + public void shouldReturnGraphsWithLeadingCommaString() throws StoreException, InterruptedException { // Given + federatedProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); final List> graphLists = populateGraphs(2, 4); final Collection expectedGraphs = graphLists.get(0); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java index 2d997a3f3ee..4b86d71b42d 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java @@ -22,6 +22,7 @@ import uk.gov.gchq.gaffer.accumulostore.AccumuloProperties; import uk.gov.gchq.gaffer.accumulostore.MockAccumuloStore; +import uk.gov.gchq.gaffer.cache.util.CacheProperties; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.data.element.Element; @@ -32,7 +33,7 @@ import uk.gov.gchq.gaffer.operation.impl.get.GetAllElements; import uk.gov.gchq.gaffer.store.Context; import uk.gov.gchq.gaffer.store.StoreProperties; -import uk.gov.gchq.gaffer.store.exception.OverwritingException; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.store.library.GraphLibrary; import uk.gov.gchq.gaffer.store.schema.Schema; import uk.gov.gchq.gaffer.user.User; @@ -59,6 +60,7 @@ public void shouldAddGraph() throws Exception { StoreProperties storeProperties = new StoreProperties(); storeProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); + storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); assertEquals(0, store.getGraphs(null).size()); @@ -109,6 +111,7 @@ public void shouldAddGraphUsingLibrary() throws Exception { StoreProperties storeProperties = new StoreProperties(); storeProperties.set(StoreProperties.STORE_CLASS, "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); storeProperties.set(StoreProperties.STORE_PROPERTIES_CLASS, "uk.gov.gchq.gaffer.store.StoreProperties"); + storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); assertEquals(0, store.getGraphs(null).size()); @@ -163,6 +166,7 @@ public void shouldNotOverwriteGraph() throws Exception { StoreProperties storeProperties = new StoreProperties(); storeProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); + storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); assertEquals(0, store.getGraphs(null).size()); @@ -206,6 +210,7 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { StoreProperties graphStoreProperties = new StoreProperties(); graphStoreProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); + graphStoreProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); assertEquals(0, store.getGraphs(null).size()); @@ -266,6 +271,7 @@ public void shouldAddGraphWithAuthsAndAddingUser() throws Exception { AccumuloProperties storeProperties = new AccumuloProperties(); storeProperties.setStorePropertiesClass(AccumuloProperties.class); storeProperties.setStoreClass(MockAccumuloStore.class); + storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); new FederatedAddGraphHandler().doOperation( new AddGraph.Builder() diff --git a/store-implementation/federated-store/src/test/resources/properties/federatedStoreTest.properties b/store-implementation/federated-store/src/test/resources/properties/federatedStoreTest.properties index e8fb537cefc..caff3b42441 100644 --- a/store-implementation/federated-store/src/test/resources/properties/federatedStoreTest.properties +++ b/store-implementation/federated-store/src/test/resources/properties/federatedStoreTest.properties @@ -26,3 +26,4 @@ gaffer.federatedstore.edgeGraph.auths=one, two gaffer.federatedstore.entityGraph.properties.file=properties/singleUseMockMapStore.properties gaffer.federatedstore.entityGraph.schema.file=schema/basicEntitySchema.json gaffer.federatedstore.entityGraph.auths=one, two +gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties index 364120debad..81d6bdd0365 100644 --- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties +++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties @@ -19,3 +19,4 @@ accumulo.instance=mockInstanceID1234 accumulo.zookeepers=aZookeeper accumulo.user=bob accumulo.password=password +gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties index ef5df3c6aa5..b0cdf7ead8b 100644 --- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties +++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties @@ -15,3 +15,4 @@ # gaffer.store.class=uk.gov.gchq.gaffer.mapstore.SingleUseMapStore gaffer.store.properties.class=uk.gov.gchq.gaffer.mapstore.MapStoreProperties +gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties index d0110e8629b..39e1547a5fb 100644 --- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties +++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties @@ -16,3 +16,4 @@ gaffer.store.class=uk.gov.gchq.gaffer.mapstore.SingleUseMapStore gaffer.store.properties.class=uk.gov.gchq.gaffer.mapstore.MapStoreProperties unusualkey=value +gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService From 046883c94930978a930f80bd70ac28b727ba64f6 Mon Sep 17 00:00:00 2001 From: m55624 Date: Wed, 27 Sep 2017 17:08:53 +0100 Subject: [PATCH 02/72] gh-1297 - test fixes --- .../java/uk/gov/gchq/gaffer/cache/ICache.java | 5 +- .../cache/impl/HashMapCacheServiceTest.java | 6 ++- .../store/library/FileGraphLibrary.java | 2 +- .../gaffer/store/library/GraphLibrary.java | 2 +- .../store/library/HashMapGraphLibrary.java | 2 +- .../gaffer/store/library/NoGraphLibrary.java | 2 +- .../named/cache/NamedOperationCacheTest.java | 3 +- .../gaffer/federatedstore/FederatedStore.java | 7 ++- ...tAuth.java => FederatedStoreAuthTest.java} | 11 ++-- .../federatedstore/FederatedStoreTest.java | 2 +- .../impl/FederatedAddGraphHandlerTest.java | 51 ++++++++++--------- .../impl/FederatedRemoveGraphHandlerTest.java | 7 ++- 12 files changed, 58 insertions(+), 42 deletions(-) rename store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/{FederatedStoreTestAuth.java => FederatedStoreAuthTest.java} (86%) diff --git a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICache.java b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICache.java index b80e713dd2a..a83857307e3 100644 --- a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICache.java +++ b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/ICache.java @@ -53,13 +53,14 @@ public interface ICache { * * @param key the key to add * @param value the value to add - * @throws CacheOperationException if the specified key already exists in the cache with a non-null value + * @throws CacheOperationException if there is an error adding the new key-value pair to the cache + * @throws OverwritingException if the specified key already exists in the cache with a non-null value */ default void putSafe(final K key, final V value) throws OverwritingException, CacheOperationException { if (null == get(key)) { try { put(key, value); - } catch (CacheOperationException e) { + } catch (final CacheOperationException e) { throw e; } } else { diff --git a/core/cache/src/test/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheServiceTest.java b/core/cache/src/test/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheServiceTest.java index 2d5af218ecc..dfbf8e6337d 100644 --- a/core/cache/src/test/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheServiceTest.java +++ b/core/cache/src/test/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheServiceTest.java @@ -24,6 +24,7 @@ import uk.gov.gchq.gaffer.cache.ICache; import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import static org.junit.Assert.assertEquals; @@ -37,6 +38,7 @@ public class HashMapCacheServiceTest { public void before() { service.initialise(null); } + @After public void after() { service.shutdown(); @@ -92,11 +94,11 @@ public void shouldOnlyUpdateIfInstructed() throws CacheOperationException { try { service.putSafeInCache(CACHE_NAME, "test", 2); Assert.fail("Expected an exception"); - } catch (final CacheOperationException e) { + } catch (final OverwritingException e) { assertEquals((Integer) 1, service.getFromCache(CACHE_NAME, "test")); } - service.putInCache(CACHE_NAME,"test", 2); + service.putInCache(CACHE_NAME, "test", 2); assertEquals((Integer) 2, service.getFromCache(CACHE_NAME, "test")); } diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/FileGraphLibrary.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/FileGraphLibrary.java index 9536ba2ab12..09ee0b8f0a0 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/FileGraphLibrary.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/FileGraphLibrary.java @@ -19,10 +19,10 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import org.apache.commons.io.FileUtils; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException; import uk.gov.gchq.gaffer.store.StoreProperties; -import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import java.io.FileOutputStream; import java.io.IOException; diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/GraphLibrary.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/GraphLibrary.java index ef7193b8f8e..c24a855553e 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/GraphLibrary.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/GraphLibrary.java @@ -18,9 +18,9 @@ import uk.gov.gchq.gaffer.commonutil.JsonUtil; import uk.gov.gchq.gaffer.commonutil.StringUtil; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.store.StoreProperties; -import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.store.schema.Schema; import java.util.regex.Pattern; diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibrary.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibrary.java index aa4649d0381..d47e5317099 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibrary.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibrary.java @@ -16,9 +16,9 @@ package uk.gov.gchq.gaffer.store.library; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.store.StoreProperties; -import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import java.util.HashMap; import java.util.Map; diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/NoGraphLibrary.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/NoGraphLibrary.java index e5bb6279d05..269068c610e 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/NoGraphLibrary.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/NoGraphLibrary.java @@ -19,9 +19,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.store.StoreProperties; -import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; /** * A {@code NoGraphLibrary} will not store any relationships between graphIds, diff --git a/core/store/src/test/java/uk/gov/gchq/gaffer/store/operation/handler/named/cache/NamedOperationCacheTest.java b/core/store/src/test/java/uk/gov/gchq/gaffer/store/operation/handler/named/cache/NamedOperationCacheTest.java index 832f22ff739..de99871df52 100644 --- a/core/store/src/test/java/uk/gov/gchq/gaffer/store/operation/handler/named/cache/NamedOperationCacheTest.java +++ b/core/store/src/test/java/uk/gov/gchq/gaffer/store/operation/handler/named/cache/NamedOperationCacheTest.java @@ -12,6 +12,7 @@ import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.cache.impl.HashMapCacheService; import uk.gov.gchq.gaffer.cache.util.CacheProperties; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; import uk.gov.gchq.gaffer.named.operation.NamedOperationDetail; import uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException; @@ -91,7 +92,7 @@ public void shouldAddNamedOperation() throws CacheOperationFailedException { @Test public void shouldThrowExceptionIfNamedOperationAlreadyExists() throws CacheOperationFailedException { cache.addNamedOperation(standard, false, standardUser); - exception.expect(CacheOperationFailedException.class); + exception.expect(OverwritingException.class); cache.addNamedOperation(alternative, false, advancedUser); } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 7f1c7005e8f..b66bde20687 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -167,11 +167,10 @@ public static OP updateOperationForGraph(final OP operati * @param graphs the graph to add */ public void addGraphs(final Graph... graphs) { + if (cacheService == null) { + throw new RuntimeException("No cache has been set, please initialise the FederatedStore instance"); + } for (final Graph graph : graphs) { - if (cacheService == null) { - startCacheServiceLoader(graph.getStoreProperties()); - setCacheService(); - } _add(graph); } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTestAuth.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java similarity index 86% rename from store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTestAuth.java rename to store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java index 4c83640a27e..33340082749 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTestAuth.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java @@ -18,6 +18,7 @@ import org.junit.Test; +import uk.gov.gchq.gaffer.cache.util.CacheProperties; import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; import uk.gov.gchq.gaffer.federatedstore.operation.handler.impl.FederatedAddGraphHandler; import uk.gov.gchq.gaffer.federatedstore.operation.handler.impl.FederatedGetAllElementsHandler; @@ -33,7 +34,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -public class FederatedStoreTestAuth { +public class FederatedStoreAuthTest { @Test public void shouldAddGraphWithHook() throws Exception { @@ -42,16 +43,20 @@ public void shouldAddGraphWithHook() throws Exception { Schema expectedSchema = new Schema.Builder().build(); String expectedGraphId = "testGraphID"; + String expectedGraphId1 = "testGraphID1"; StoreProperties storeProperties = new StoreProperties(); storeProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); + storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); assertEquals(0, store.getGraphs(null).size()); + store.initialise(expectedGraphId, null, storeProperties); + FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); federatedAddGraphHandler.doOperation( new AddGraph.Builder() - .graphId(expectedGraphId) + .graphId(expectedGraphId1) .schema(expectedSchema) .storeProperties(storeProperties) .graphAuths("auth1") @@ -63,7 +68,7 @@ public void shouldAddGraphWithHook() throws Exception { assertEquals(1, graphs.size()); Graph next = graphs.iterator().next(); - assertEquals(expectedGraphId, next.getGraphId()); + assertEquals(expectedGraphId1, next.getGraphId()); assertEquals(expectedSchema, next.getSchema()); final FederatedGetAllElementsHandler federatedGetAllElementsHandler = new FederatedGetAllElementsHandler(); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index aa8ad46f625..c2115cfcf59 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -820,7 +820,7 @@ public void shouldReturnNoGraphsFromEmptyString() throws StoreException { } @Test - public void shouldReturnGraphsWithLeadingCommaString() throws StoreException, InterruptedException { + public void shouldReturnGraphsWithLeadingCommaString() throws StoreException { // Given federatedProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java index 4b86d71b42d..5a13eec0712 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java @@ -23,6 +23,7 @@ import uk.gov.gchq.gaffer.accumulostore.AccumuloProperties; import uk.gov.gchq.gaffer.accumulostore.MockAccumuloStore; import uk.gov.gchq.gaffer.cache.util.CacheProperties; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.data.element.Element; @@ -33,7 +34,6 @@ import uk.gov.gchq.gaffer.operation.impl.get.GetAllElements; import uk.gov.gchq.gaffer.store.Context; import uk.gov.gchq.gaffer.store.StoreProperties; -import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.store.library.GraphLibrary; import uk.gov.gchq.gaffer.store.schema.Schema; import uk.gov.gchq.gaffer.user.User; @@ -49,6 +49,8 @@ public class FederatedAddGraphHandlerTest { public static final String GAFFER_FEDERATEDSTORE_CUSTOM_PROPERTIES_AUTHS = "gaffer.federatedstore.customPropertiesAuths"; + private static final String FEDERATED_STORE_GRAPH_ID = "federatedStore"; + private static final String EXPECTED_GRAPH_ID = "testGraphID"; @Test public void shouldAddGraph() throws Exception { @@ -56,7 +58,6 @@ public void shouldAddGraph() throws Exception { FederatedStore store = new FederatedStore(); Schema expectedSchema = new Schema.Builder().build(); - String expectedGraphId = "testGraphID"; StoreProperties storeProperties = new StoreProperties(); storeProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); @@ -64,10 +65,12 @@ public void shouldAddGraph() throws Exception { assertEquals(0, store.getGraphs(null).size()); + store.initialise(FEDERATED_STORE_GRAPH_ID, new Schema(), storeProperties); + FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); federatedAddGraphHandler.doOperation( new AddGraph.Builder() - .graphId(expectedGraphId) + .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) .storeProperties(storeProperties) .build(), @@ -78,12 +81,12 @@ public void shouldAddGraph() throws Exception { assertEquals(1, graphs.size()); Graph next = graphs.iterator().next(); - assertEquals(expectedGraphId, next.getGraphId()); + assertEquals(EXPECTED_GRAPH_ID, next.getGraphId()); assertEquals(expectedSchema, next.getSchema()); federatedAddGraphHandler.doOperation( new AddGraph.Builder() - .graphId(expectedGraphId + "b") + .graphId(EXPECTED_GRAPH_ID + "b") .schema(expectedSchema) .storeProperties(storeProperties) .build(), @@ -95,8 +98,8 @@ public void shouldAddGraph() throws Exception { assertEquals(2, graphs.size()); Iterator iterator = graphs.iterator(); next = iterator.next(); - assertEquals(expectedGraphId, next.getGraphId()); - assertEquals(expectedGraphId + "b", iterator.next().getGraphId()); + assertEquals(EXPECTED_GRAPH_ID, next.getGraphId()); + assertEquals(EXPECTED_GRAPH_ID + "b", iterator.next().getGraphId()); } @Test @@ -104,9 +107,7 @@ public void shouldAddGraphUsingLibrary() throws Exception { FederatedStore store = new FederatedStore(); - Schema expectedSchema = new Schema.Builder().build(); - String expectedGraphId = "testGraphID"; StoreProperties storeProperties = new StoreProperties(); storeProperties.set(StoreProperties.STORE_CLASS, "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); @@ -115,10 +116,12 @@ public void shouldAddGraphUsingLibrary() throws Exception { assertEquals(0, store.getGraphs(null).size()); + store.initialise(FEDERATED_STORE_GRAPH_ID, new Schema(), storeProperties); + FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); federatedAddGraphHandler.doOperation( new AddGraph.Builder() - .graphId(expectedGraphId) + .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) .storeProperties(storeProperties) .build(), @@ -129,11 +132,11 @@ public void shouldAddGraphUsingLibrary() throws Exception { assertEquals(1, graphs.size()); Graph next = graphs.iterator().next(); - assertEquals(expectedGraphId, next.getGraphId()); + assertEquals(EXPECTED_GRAPH_ID, next.getGraphId()); assertEquals(expectedSchema, next.getSchema()); final GraphLibrary mock = Mockito.mock(GraphLibrary.class); - final String graphIdB = expectedGraphId + "b"; + final String graphIdB = EXPECTED_GRAPH_ID + "b"; BDDMockito.given(mock.get(graphIdB)).willReturn(new Pair<>(expectedSchema, storeProperties)); BDDMockito.given(mock.exists(graphIdB)).willReturn(true); store.setGraphLibrary(mock); @@ -150,7 +153,7 @@ public void shouldAddGraphUsingLibrary() throws Exception { assertEquals(2, graphs.size()); Iterator iterator = graphs.iterator(); next = iterator.next(); - assertEquals(expectedGraphId, next.getGraphId()); + assertEquals(EXPECTED_GRAPH_ID, next.getGraphId()); assertEquals(graphIdB, iterator.next().getGraphId()); Mockito.verify(mock, Mockito.times(3)).get(graphIdB); @@ -162,7 +165,6 @@ public void shouldNotOverwriteGraph() throws Exception { FederatedStore store = new FederatedStore(); Schema expectedSchema = new Schema.Builder().build(); - String expectedGraphId = "testGraphID"; StoreProperties storeProperties = new StoreProperties(); storeProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); @@ -170,11 +172,13 @@ public void shouldNotOverwriteGraph() throws Exception { assertEquals(0, store.getGraphs(null).size()); + store.initialise(FEDERATED_STORE_GRAPH_ID, new Schema(), storeProperties); + FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); federatedAddGraphHandler.doOperation( new AddGraph.Builder() - .graphId(expectedGraphId) + .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) .storeProperties(storeProperties) .build(), @@ -184,14 +188,14 @@ public void shouldNotOverwriteGraph() throws Exception { try { federatedAddGraphHandler.doOperation( new AddGraph.Builder() - .graphId(expectedGraphId) + .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) .storeProperties(storeProperties) .build(), new Context(new User("TestUser")), store); } catch (final OverwritingException e) { - assertEquals(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE_A_GRAPH_WITHIN_FEDERATED_STORE_GRAPH_ID_S, "testGraphID"), e.getMessage()); + assertEquals(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE_A_GRAPH_WITHIN_FEDERATED_STORE_GRAPH_ID_S, EXPECTED_GRAPH_ID), e.getMessage()); } } @@ -202,24 +206,24 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { final StoreProperties federatedProperties = new StoreProperties(); federatedProperties.set(GAFFER_FEDERATEDSTORE_CUSTOM_PROPERTIES_AUTHS, "auth1,auth2"); + federatedProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); FederatedStore store = new FederatedStore(); - store.initialise("FederatedStore", null, federatedProperties); Schema expectedSchema = new Schema.Builder().build(); - String expectedGraphId = "testGraphID"; StoreProperties graphStoreProperties = new StoreProperties(); graphStoreProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); - graphStoreProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); assertEquals(0, store.getGraphs(null).size()); + store.initialise(FEDERATED_STORE_GRAPH_ID, null, federatedProperties); + FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); try { federatedAddGraphHandler.doOperation( new AddGraph.Builder() - .graphId(expectedGraphId) + .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) .storeProperties(graphStoreProperties) .build(), @@ -235,7 +239,7 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { federatedAddGraphHandler.doOperation( new AddGraph.Builder() - .graphId(expectedGraphId) + .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) .storeProperties(graphStoreProperties) .build(), @@ -246,7 +250,7 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { store); assertEquals(1, store.getGraphs(null).size()); - assertEquals(expectedGraphId, store.getGraphs(null).iterator().next().getGraphId()); + assertEquals(EXPECTED_GRAPH_ID, store.getGraphs(null).iterator().next().getGraphId()); } @@ -260,6 +264,7 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { public void shouldAddGraphWithAuthsAndAddingUser() throws Exception { StoreProperties fedStoreProperties = new StoreProperties(); fedStoreProperties.setStoreClass(FederatedStore.class); + fedStoreProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); FederatedStore store = new FederatedStore(); store.initialise("testFedStore", null, fedStoreProperties); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java index f81db438b0d..f18dfeeff02 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java @@ -18,6 +18,7 @@ import org.junit.Test; +import uk.gov.gchq.gaffer.cache.util.CacheProperties; import uk.gov.gchq.gaffer.federatedstore.FederatedStore; import uk.gov.gchq.gaffer.federatedstore.operation.RemoveGraph; import uk.gov.gchq.gaffer.graph.Graph; @@ -35,13 +36,15 @@ public class FederatedRemoveGraphHandlerTest { @Test public void shouldRemoveGraph() throws Exception { - + FederatedStore store = new FederatedStore(); String graphId = "testGraphId"; StoreProperties storeProperties = new StoreProperties(); storeProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); + storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); + + store.initialise("FederatedStore", new Schema(), storeProperties); - FederatedStore store = new FederatedStore(); store.addGraphs(new Graph.Builder() .config(new GraphConfig(graphId)) .addSchema(new Schema.Builder().build()) From 5f0f42c2300896985a7b68fa94feaa8005825bd7 Mon Sep 17 00:00:00 2001 From: m55624 Date: Thu, 28 Sep 2017 10:11:05 +0100 Subject: [PATCH 03/72] gh-1297 - test fixes --- .../cache/impl/HazelcastCacheServiceTest.java | 3 +- .../gaffer/cache/impl/HazelcastCacheTest.java | 5 +- .../cache/impl/JcsCacheServiceTest.java | 7 +- .../gchq/gaffer/cache/impl/JcsCacheTest.java | 5 +- .../gaffer/federatedstore/FederatedStore.java | 4 +- .../FederatedStoreAuthTest.java | 8 +- .../federatedstore/FederatedStoreTest.java | 1 - .../impl/FederatedAddGraphHandlerTest.java | 74 ++++++++++--------- .../impl/FederatedRemoveGraphHandlerTest.java | 19 +++-- .../predefinedFederatedStore.properties | 1 + 10 files changed, 70 insertions(+), 57 deletions(-) diff --git a/library/cache-library/hazelcast-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/HazelcastCacheServiceTest.java b/library/cache-library/hazelcast-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/HazelcastCacheServiceTest.java index 8726f6f9f4f..aa4a8b7f37c 100644 --- a/library/cache-library/hazelcast-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/HazelcastCacheServiceTest.java +++ b/library/cache-library/hazelcast-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/HazelcastCacheServiceTest.java @@ -32,6 +32,7 @@ import uk.gov.gchq.gaffer.cache.util.CacheProperties; import uk.gov.gchq.gaffer.commonutil.CommonTestConstants; import uk.gov.gchq.gaffer.commonutil.StreamUtil; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import java.io.File; import java.io.IOException; @@ -162,7 +163,7 @@ public void shouldOnlyUpdateIfInstructed() throws CacheOperationException { try { service.putSafeInCache(CACHE_NAME, "test", 2); fail("Expected an exception"); - } catch (final CacheOperationException e) { + } catch (final OverwritingException e) { assertEquals((Integer) 1, service.getFromCache(CACHE_NAME, "test")); } diff --git a/library/cache-library/hazelcast-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/HazelcastCacheTest.java b/library/cache-library/hazelcast-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/HazelcastCacheTest.java index 92ef3faa23e..eaa71b931cd 100644 --- a/library/cache-library/hazelcast-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/HazelcastCacheTest.java +++ b/library/cache-library/hazelcast-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/HazelcastCacheTest.java @@ -9,6 +9,7 @@ import org.junit.Test; import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -34,7 +35,7 @@ public void before() throws CacheOperationException { } @Test - public void shouldThrowAnExceptionIfEntryAlreadyExistsWhenUsingPutSafe(){ + public void shouldThrowAnExceptionIfEntryAlreadyExistsWhenUsingPutSafe() { try { cache.put("test", 1); } catch (final CacheOperationException e) { @@ -43,7 +44,7 @@ public void shouldThrowAnExceptionIfEntryAlreadyExistsWhenUsingPutSafe(){ try { cache.putSafe("test", 1); fail(); - } catch (final CacheOperationException e) { + } catch (final OverwritingException | CacheOperationException e) { assertEquals("Cache entry already exists for key: test", e.getMessage()); } } diff --git a/library/cache-library/jcs-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/JcsCacheServiceTest.java b/library/cache-library/jcs-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/JcsCacheServiceTest.java index 597d54f7aa9..2ae83f54136 100644 --- a/library/cache-library/jcs-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/JcsCacheServiceTest.java +++ b/library/cache-library/jcs-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/JcsCacheServiceTest.java @@ -28,6 +28,7 @@ import uk.gov.gchq.gaffer.cache.ICache; import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; import uk.gov.gchq.gaffer.cache.util.CacheProperties; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import java.io.File; import java.util.Properties; @@ -43,7 +44,7 @@ public class JcsCacheServiceTest { private JcsCacheService service = new JcsCacheService(); private static final String TEST_REGION = "test"; private static final String ALTERNATIVE_TEST_REGION = "alternativeTest"; - private static final String AGE_OFF_REGION="ageOff"; + private static final String AGE_OFF_REGION = "ageOff"; private Properties serviceProps = new Properties(); @Rule @@ -151,11 +152,11 @@ public void shouldOnlyUpdateIfInstructed() throws CacheOperationException { try { service.putSafeInCache(TEST_REGION, "test", 2); fail("Expected an exception"); - } catch (final CacheOperationException e) { + } catch (final OverwritingException e) { Assert.assertEquals((Integer) 1, service.getFromCache(TEST_REGION, "test")); } - service.putInCache(TEST_REGION,"test", 2); + service.putInCache(TEST_REGION, "test", 2); Assert.assertEquals((Integer) 2, service.getFromCache(TEST_REGION, "test")); } diff --git a/library/cache-library/jcs-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/JcsCacheTest.java b/library/cache-library/jcs-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/JcsCacheTest.java index 587975db85a..4604c7e736f 100644 --- a/library/cache-library/jcs-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/JcsCacheTest.java +++ b/library/cache-library/jcs-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/JcsCacheTest.java @@ -8,6 +8,7 @@ import org.junit.Test; import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -31,12 +32,12 @@ public void before() throws CacheOperationException { @Test - public void shouldThrowAnExceptionIfEntryAlreadyExistsWhenUsingPutSafe(){ + public void shouldThrowAnExceptionIfEntryAlreadyExistsWhenUsingPutSafe() { try { cache.put("test", 1); cache.putSafe("test", 1); fail(); - } catch (final CacheOperationException e) { + } catch (final OverwritingException | CacheOperationException e) { assertEquals("Cache entry already exists for key: test", e.getMessage()); } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index b66bde20687..94386aa614c 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -478,9 +478,9 @@ private void _add(final Graph newGraph) { try { cacheService.putSafeInCache(CACHE_SERVICE_NAME, graphId, newGraph); - } catch (OverwritingException e) { + } catch (final OverwritingException e) { throw new OverwritingException((String.format(USER_IS_ATTEMPTING_TO_OVERWRITE_A_GRAPH_WITHIN_FEDERATED_STORE_GRAPH_ID_S, graphId))); - } catch (CacheOperationException e) { + } catch (final CacheOperationException e) { throw new RuntimeException(e); } schema = newSchema; diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java index 33340082749..08294b61c45 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java @@ -42,8 +42,8 @@ public void shouldAddGraphWithHook() throws Exception { FederatedStore store = new FederatedStore(); Schema expectedSchema = new Schema.Builder().build(); + String federatedGraphId = "federatedStoreGraphId"; String expectedGraphId = "testGraphID"; - String expectedGraphId1 = "testGraphID1"; StoreProperties storeProperties = new StoreProperties(); storeProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); @@ -51,12 +51,12 @@ public void shouldAddGraphWithHook() throws Exception { assertEquals(0, store.getGraphs(null).size()); - store.initialise(expectedGraphId, null, storeProperties); + store.initialise(federatedGraphId, null, storeProperties); FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); federatedAddGraphHandler.doOperation( new AddGraph.Builder() - .graphId(expectedGraphId1) + .graphId(expectedGraphId) .schema(expectedSchema) .storeProperties(storeProperties) .graphAuths("auth1") @@ -68,7 +68,7 @@ public void shouldAddGraphWithHook() throws Exception { assertEquals(1, graphs.size()); Graph next = graphs.iterator().next(); - assertEquals(expectedGraphId1, next.getGraphId()); + assertEquals(expectedGraphId, next.getGraphId()); assertEquals(expectedSchema, next.getSchema()); final FederatedGetAllElementsHandler federatedGetAllElementsHandler = new FederatedGetAllElementsHandler(); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index c2115cfcf59..886ab72da81 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -822,7 +822,6 @@ public void shouldReturnNoGraphsFromEmptyString() throws StoreException { @Test public void shouldReturnGraphsWithLeadingCommaString() throws StoreException { // Given - federatedProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); final List> graphLists = populateGraphs(2, 4); final Collection expectedGraphs = graphLists.get(0); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java index 5a13eec0712..a29422740a0 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java @@ -49,8 +49,12 @@ public class FederatedAddGraphHandlerTest { public static final String GAFFER_FEDERATEDSTORE_CUSTOM_PROPERTIES_AUTHS = "gaffer.federatedstore.customPropertiesAuths"; - private static final String FEDERATED_STORE_GRAPH_ID = "federatedStore"; + private static final String FEDERATEDSTORE_GRAPH_ID = "federatedStore"; private static final String EXPECTED_GRAPH_ID = "testGraphID"; + private static final String EXPECTED_GRAPH_ID_2 = "testGraphID2"; + private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; + private static final String FEDERATEDSTORE_CLASS_STRING = "uk.gov.gchq.gaffer.federatedstore.FederatedStore"; + private static final String TEST_USER = "testUser"; @Test public void shouldAddGraph() throws Exception { @@ -60,12 +64,12 @@ public void shouldAddGraph() throws Exception { Schema expectedSchema = new Schema.Builder().build(); StoreProperties storeProperties = new StoreProperties(); - storeProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); - storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); + storeProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); + storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); assertEquals(0, store.getGraphs(null).size()); - store.initialise(FEDERATED_STORE_GRAPH_ID, new Schema(), storeProperties); + store.initialise(FEDERATEDSTORE_GRAPH_ID, new Schema(), storeProperties); FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); federatedAddGraphHandler.doOperation( @@ -74,7 +78,7 @@ public void shouldAddGraph() throws Exception { .schema(expectedSchema) .storeProperties(storeProperties) .build(), - new Context(new User("TestUser")), + new Context(new User(TEST_USER)), store); Collection graphs = store.getGraphs(null); @@ -86,11 +90,11 @@ public void shouldAddGraph() throws Exception { federatedAddGraphHandler.doOperation( new AddGraph.Builder() - .graphId(EXPECTED_GRAPH_ID + "b") + .graphId(EXPECTED_GRAPH_ID_2) .schema(expectedSchema) .storeProperties(storeProperties) .build(), - new Context(new User("TestUser")), + new Context(new User(TEST_USER)), store); graphs = store.getGraphs(null); @@ -99,7 +103,7 @@ public void shouldAddGraph() throws Exception { Iterator iterator = graphs.iterator(); next = iterator.next(); assertEquals(EXPECTED_GRAPH_ID, next.getGraphId()); - assertEquals(EXPECTED_GRAPH_ID + "b", iterator.next().getGraphId()); + assertEquals(EXPECTED_GRAPH_ID_2, iterator.next().getGraphId()); } @Test @@ -110,13 +114,13 @@ public void shouldAddGraphUsingLibrary() throws Exception { Schema expectedSchema = new Schema.Builder().build(); StoreProperties storeProperties = new StoreProperties(); - storeProperties.set(StoreProperties.STORE_CLASS, "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); + storeProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); storeProperties.set(StoreProperties.STORE_PROPERTIES_CLASS, "uk.gov.gchq.gaffer.store.StoreProperties"); - storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); + storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); assertEquals(0, store.getGraphs(null).size()); - store.initialise(FEDERATED_STORE_GRAPH_ID, new Schema(), storeProperties); + store.initialise(FEDERATEDSTORE_GRAPH_ID, new Schema(), storeProperties); FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); federatedAddGraphHandler.doOperation( @@ -125,7 +129,7 @@ public void shouldAddGraphUsingLibrary() throws Exception { .schema(expectedSchema) .storeProperties(storeProperties) .build(), - new Context(new User("TestUser")), + new Context(new User(TEST_USER)), store); Collection graphs = store.getGraphs(null); @@ -136,16 +140,16 @@ public void shouldAddGraphUsingLibrary() throws Exception { assertEquals(expectedSchema, next.getSchema()); final GraphLibrary mock = Mockito.mock(GraphLibrary.class); - final String graphIdB = EXPECTED_GRAPH_ID + "b"; - BDDMockito.given(mock.get(graphIdB)).willReturn(new Pair<>(expectedSchema, storeProperties)); - BDDMockito.given(mock.exists(graphIdB)).willReturn(true); + final String graphId2 = EXPECTED_GRAPH_ID_2; + BDDMockito.given(mock.get(graphId2)).willReturn(new Pair<>(expectedSchema, storeProperties)); + BDDMockito.given(mock.exists(graphId2)).willReturn(true); store.setGraphLibrary(mock); federatedAddGraphHandler.doOperation( new AddGraph.Builder() - .graphId(graphIdB) + .graphId(graphId2) .build(), - new Context(new User("TestUser")), + new Context(new User(TEST_USER)), store); graphs = store.getGraphs(null); @@ -154,9 +158,9 @@ public void shouldAddGraphUsingLibrary() throws Exception { Iterator iterator = graphs.iterator(); next = iterator.next(); assertEquals(EXPECTED_GRAPH_ID, next.getGraphId()); - assertEquals(graphIdB, iterator.next().getGraphId()); + assertEquals(graphId2, iterator.next().getGraphId()); - Mockito.verify(mock, Mockito.times(3)).get(graphIdB); + Mockito.verify(mock, Mockito.times(3)).get(graphId2); } @Test @@ -167,12 +171,12 @@ public void shouldNotOverwriteGraph() throws Exception { Schema expectedSchema = new Schema.Builder().build(); StoreProperties storeProperties = new StoreProperties(); - storeProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); - storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); + storeProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); + storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); assertEquals(0, store.getGraphs(null).size()); - store.initialise(FEDERATED_STORE_GRAPH_ID, new Schema(), storeProperties); + store.initialise(FEDERATEDSTORE_GRAPH_ID, new Schema(), storeProperties); FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); @@ -182,7 +186,7 @@ public void shouldNotOverwriteGraph() throws Exception { .schema(expectedSchema) .storeProperties(storeProperties) .build(), - new Context(new User("TestUser")), + new Context(new User(TEST_USER)), store); try { @@ -192,7 +196,7 @@ public void shouldNotOverwriteGraph() throws Exception { .schema(expectedSchema) .storeProperties(storeProperties) .build(), - new Context(new User("TestUser")), + new Context(new User(TEST_USER)), store); } catch (final OverwritingException e) { assertEquals(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE_A_GRAPH_WITHIN_FEDERATED_STORE_GRAPH_ID_S, EXPECTED_GRAPH_ID), e.getMessage()); @@ -206,17 +210,17 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { final StoreProperties federatedProperties = new StoreProperties(); federatedProperties.set(GAFFER_FEDERATEDSTORE_CUSTOM_PROPERTIES_AUTHS, "auth1,auth2"); - federatedProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); + federatedProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); FederatedStore store = new FederatedStore(); Schema expectedSchema = new Schema.Builder().build(); StoreProperties graphStoreProperties = new StoreProperties(); - graphStoreProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); + graphStoreProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); assertEquals(0, store.getGraphs(null).size()); - store.initialise(FEDERATED_STORE_GRAPH_ID, null, federatedProperties); + store.initialise(FEDERATEDSTORE_GRAPH_ID, null, federatedProperties); FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); @@ -227,7 +231,7 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { .schema(expectedSchema) .storeProperties(graphStoreProperties) .build(), - new Context(new User("TestUser")), + new Context(new User(TEST_USER)), store); fail("Exception not thrown"); } catch (OperationException e) { @@ -244,7 +248,7 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { .storeProperties(graphStoreProperties) .build(), new Context(new User.Builder() - .userId("TestUser") + .userId(TEST_USER) .opAuth("auth1") .build()), store); @@ -264,10 +268,10 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { public void shouldAddGraphWithAuthsAndAddingUser() throws Exception { StoreProperties fedStoreProperties = new StoreProperties(); fedStoreProperties.setStoreClass(FederatedStore.class); - fedStoreProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); + fedStoreProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); FederatedStore store = new FederatedStore(); - store.initialise("testFedStore", null, fedStoreProperties); + store.initialise(FEDERATEDSTORE_GRAPH_ID, null, fedStoreProperties); Schema expectedSchema = new Schema.Builder().build(); @@ -276,23 +280,23 @@ public void shouldAddGraphWithAuthsAndAddingUser() throws Exception { AccumuloProperties storeProperties = new AccumuloProperties(); storeProperties.setStorePropertiesClass(AccumuloProperties.class); storeProperties.setStoreClass(MockAccumuloStore.class); - storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); + storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); new FederatedAddGraphHandler().doOperation( new AddGraph.Builder() - .graphId("testGraphID") + .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) .storeProperties(storeProperties) .graphAuths("testAuth") .build(), - new Context(new User("TestUser")), + new Context(new User(TEST_USER)), store); final CloseableIterable elements = new FederatedGetAllElementsHandler().doOperation( new GetAllElements(), new Context(new User.Builder() - .userId("TestUser") + .userId(TEST_USER) .build()), store); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java index f18dfeeff02..5f337173b52 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java @@ -34,19 +34,24 @@ public class FederatedRemoveGraphHandlerTest { + private static final String FEDERATEDSTORE_GRAPH_ID = "federatedStore"; + private static final String EXPECTED_GRAPH_ID = "testGraphID"; + private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; + private static final String FEDERATEDSTORE_CLASS_STRING = "uk.gov.gchq.gaffer.federatedstore.FederatedStore"; + private static final String TEST_USER = "testUser"; + @Test public void shouldRemoveGraph() throws Exception { FederatedStore store = new FederatedStore(); - String graphId = "testGraphId"; StoreProperties storeProperties = new StoreProperties(); - storeProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); - storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); + storeProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); + storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); - store.initialise("FederatedStore", new Schema(), storeProperties); + store.initialise(FEDERATEDSTORE_GRAPH_ID, new Schema(), storeProperties); store.addGraphs(new Graph.Builder() - .config(new GraphConfig(graphId)) + .config(new GraphConfig(EXPECTED_GRAPH_ID)) .addSchema(new Schema.Builder().build()) .storeProperties(storeProperties) .build()); @@ -55,9 +60,9 @@ public void shouldRemoveGraph() throws Exception { new FederatedRemoveGraphHandler().doOperation( new RemoveGraph.Builder() - .setGraphId(graphId) + .setGraphId(EXPECTED_GRAPH_ID) .build(), - new Context(new User("TestUser")), + new Context(new User(TEST_USER)), store); Collection graphs = store.getGraphs(null); diff --git a/store-implementation/federated-store/src/test/resources/predefinedFederatedStore.properties b/store-implementation/federated-store/src/test/resources/predefinedFederatedStore.properties index 37cf80f5ab6..2fb7921d00b 100644 --- a/store-implementation/federated-store/src/test/resources/predefinedFederatedStore.properties +++ b/store-implementation/federated-store/src/test/resources/predefinedFederatedStore.properties @@ -14,3 +14,4 @@ # limitations under the License. # gaffer.store.class=uk.gov.gchq.gaffer.federatedstore.PredefinedFederatedStore +gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService \ No newline at end of file From f95d1d54cd2fb0c734184a709b1dc0689b4575c5 Mon Sep 17 00:00:00 2001 From: m55624 Date: Thu, 28 Sep 2017 10:40:55 +0100 Subject: [PATCH 04/72] gh-1297 - test refactoring and fixing --- .../FederatedAccessExceptionTest.java | 27 +++++----- .../FederatedAccessHookAuthTest.java | 6 +-- .../FederatedStoreAuthTest.java | 22 +++++---- .../FederatedStoreSchemaTest.java | 17 ++++--- .../federatedstore/FederatedStoreTest.java | 49 ++++++++++--------- .../operation/AddGraphTest.java | 17 +++---- .../operation/RemoveGraphTest.java | 13 +++-- .../FederatedOperationHandlerTest.java | 7 +-- .../FederatedOperationOutputHandlerTest.java | 8 +-- .../FederatedAddAllElementsHandlerTest.java | 3 +- .../impl/FederatedAddGraphHandlerTest.java | 24 ++++----- 11 files changed, 98 insertions(+), 95 deletions(-) diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessExceptionTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessExceptionTest.java index c94ff53d908..7b5332036d8 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessExceptionTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessExceptionTest.java @@ -24,6 +24,10 @@ import static org.junit.Assert.assertEquals; public class FederatedAccessExceptionTest { + + private static final String HELLO = "hello"; + private static final String OPERATION = "operation"; + @Test public void shouldThrow() throws Exception { try { @@ -35,43 +39,38 @@ public void shouldThrow() throws Exception { @Test public void shouldThrowWithString() throws Exception { - final String hello = "hello"; try { - throw new FederatedAccessException(hello); + throw new FederatedAccessException(HELLO); } catch (FederatedAccessException e) { - assertEquals(hello, e.getMessage()); + assertEquals(HELLO, e.getMessage()); } } @Test public void shouldThrowWithStringCause() throws Exception { - final String hello = "hello"; - final String operation = "operation"; try { - throw new FederatedAccessException(hello, new OperationException(operation)); + throw new FederatedAccessException(HELLO, new OperationException(OPERATION)); } catch (FederatedAccessException e) { - assertEquals(hello, e.getMessage()); - assertEquals(operation, e.getCause().getMessage()); + assertEquals(HELLO, e.getMessage()); + assertEquals(OPERATION, e.getCause().getMessage()); } } @Test public void shouldThrowWithCause() throws Exception { - final String operation = "operation"; try { - throw new FederatedAccessException(new OperationException(operation)); + throw new FederatedAccessException(new OperationException(OPERATION)); } catch (FederatedAccessException e) { - assertEquals(operation, e.getCause().getMessage()); + assertEquals(OPERATION, e.getCause().getMessage()); } } @Test public void shouldThrowWithOther() throws Exception { - final String hello = "hello"; try { - throw new FederatedAccessException(hello, new OperationException("operation"), false, false); + throw new FederatedAccessException(HELLO, new OperationException(OPERATION), false, false); } catch (FederatedAccessException e) { - assertEquals(hello, e.getMessage()); + assertEquals(HELLO, e.getMessage()); } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessHookAuthTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessHookAuthTest.java index 8856d0ded06..e1b761234bd 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessHookAuthTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessHookAuthTest.java @@ -26,10 +26,8 @@ public class FederatedAccessHookAuthTest { - public static final String A = "A"; - public static final String B = "B"; - public static final String AA = "AA"; - public static final String USER = "user"; + private static final String A = "A"; + private static final String B = "B"; @Test public void shouldValidateUserWithMatchingAuth() throws Exception { diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java index 08294b61c45..42b4e3dfd9d 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java @@ -36,39 +36,43 @@ public class FederatedStoreAuthTest { + private static final String FEDERATEDSTORE_GRAPH_ID = "federatedStore"; + private static final String EXPECTED_GRAPH_ID = "testGraphID"; + private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; + private static final String FEDERATEDSTORE_CLASS_STRING = "uk.gov.gchq.gaffer.federatedstore.FederatedStore"; + private static final String TEST_USER = "testUser"; + @Test public void shouldAddGraphWithHook() throws Exception { FederatedStore store = new FederatedStore(); Schema expectedSchema = new Schema.Builder().build(); - String federatedGraphId = "federatedStoreGraphId"; - String expectedGraphId = "testGraphID"; StoreProperties storeProperties = new StoreProperties(); - storeProperties.set("gaffer.store.class", "uk.gov.gchq.gaffer.federatedstore.FederatedStore"); - storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); + storeProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); + storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); assertEquals(0, store.getGraphs(null).size()); - store.initialise(federatedGraphId, null, storeProperties); + store.initialise(FEDERATEDSTORE_GRAPH_ID, null, storeProperties); FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); federatedAddGraphHandler.doOperation( new AddGraph.Builder() - .graphId(expectedGraphId) + .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) .storeProperties(storeProperties) .graphAuths("auth1") .build(), - new Context(new User("TestUser")), + new Context(new User(TEST_USER)), store); Collection graphs = store.getGraphs(null); assertEquals(1, graphs.size()); Graph next = graphs.iterator().next(); - assertEquals(expectedGraphId, next.getGraphId()); + assertEquals(EXPECTED_GRAPH_ID, next.getGraphId()); assertEquals(expectedSchema, next.getSchema()); final FederatedGetAllElementsHandler federatedGetAllElementsHandler = new FederatedGetAllElementsHandler(); @@ -76,7 +80,7 @@ public void shouldAddGraphWithHook() throws Exception { try { federatedGetAllElementsHandler.doOperation( new GetAllElements(), - new Context(new User("TestUser")), + new Context(new User(TEST_USER)), store); } catch (Exception e) { assertTrue(e.getCause().getMessage().contains(String.format(FederatedAccessHook.USER_DOES_NOT_HAVE_CORRECT_AUTHS_TO_ACCESS_THIS_GRAPH_USER_S, ""))); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java index e0e6626a139..1e5157955de 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java @@ -41,30 +41,31 @@ public class FederatedStoreSchemaTest { - public static final String STRING = "string"; - public static final Schema STRING_SCHEMA = new Schema.Builder() + private static final String STRING = "string"; + private static final Schema STRING_SCHEMA = new Schema.Builder() .type(STRING, new TypeDefinition.Builder() .clazz(String.class) .aggregateFunction(new StringConcat()) .build()) .build(); - public static final User TEST_USER = new User("testUser"); - public static final String TEST_FED_STORE = "testFedStore"; + private static final User TEST_USER = new User("testUser"); + private static final String TEST_FED_STORE = "testFedStore"; private FederatedStore fStore; - public static final AccumuloProperties ACCUMULO_PROPERTIES = new AccumuloProperties(); - public static final StoreProperties FEDERATED_PROPERTIES = new StoreProperties(); + private static final AccumuloProperties ACCUMULO_PROPERTIES = new AccumuloProperties(); + private static final StoreProperties FEDERATED_PROPERTIES = new StoreProperties(); + private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; @Before public void setUp() throws Exception { ACCUMULO_PROPERTIES.setStoreClass(MockAccumuloStore.class.getName()); ACCUMULO_PROPERTIES.setStorePropertiesClass(AccumuloProperties.class); - ACCUMULO_PROPERTIES.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); + ACCUMULO_PROPERTIES.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); FEDERATED_PROPERTIES.setStoreClass(FederatedStore.class.getName()); FEDERATED_PROPERTIES.setStorePropertiesClass(StoreProperties.class); - FEDERATED_PROPERTIES.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); + FEDERATED_PROPERTIES.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); fStore = new FederatedStore(); fStore.initialise(TEST_FED_STORE, null, FEDERATED_PROPERTIES); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index 886ab72da81..d4b7fd7e159 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -66,36 +66,37 @@ import static uk.gov.gchq.gaffer.federatedstore.FederatedStore.USER_IS_ATTEMPTING_TO_OVERWRITE_A_GRAPH_WITHIN_FEDERATED_STORE_GRAPH_ID_S; public class FederatedStoreTest { - public static final String PATH_FEDERATED_STORE_PROPERTIES = "/properties/federatedStoreTest.properties"; - public static final String FEDERATED_STORE_ID = "testFederatedStoreId"; - public static final String ACC_ID_1 = "mockAccGraphId1"; - public static final String MAP_ID_1 = "mockMapGraphId1"; - public static final String PATH_ACC_STORE_PROPERTIES = "properties/singleUseMockAccStore.properties"; - public static final String PATH_MAP_STORE_PROPERTIES = "properties/singleUseMockMapStore.properties"; - public static final String PATH_MAP_STORE_PROPERTIES_ALT = "properties/singleUseMockMapStoreAlt.properties"; - public static final String PATH_BASIC_ENTITY_SCHEMA_JSON = "schema/basicEntitySchema.json"; - public static final String PATH_BASIC_EDGE_SCHEMA_JSON = "schema/basicEdgeSchema.json"; - public static final String KEY_ACC_ID1_PROPERTIES_FILE = "gaffer.federatedstore.mockAccGraphId1.properties.file"; - public static final String KEY_MAP_ID1_PROPERTIES_FILE = "gaffer.federatedstore.mockMapGraphId1.properties.file"; - public static final String KEY_MAP_ID1_PROPERTIES_ID = "gaffer.federatedstore.mockMapGraphId1.properties.id"; - public static final String GRAPH_IDS = "gaffer.federatedstore.graphIds"; - public static final String KEY_ACC_ID1_SCHEMA_FILE = "gaffer.federatedstore.mockAccGraphId1.schema.file"; - public static final String KEY_MAP_ID1_SCHEMA_FILE = "gaffer.federatedstore.mockMapGraphId1.schema.file"; - public static final String KEY_MAP_ID1_SCHEMA_ID = "gaffer.federatedstore.mockMapGraphId1.schema.id"; - public static final String PATH_INVALID = "nothing.json"; - public static final String EXCEPTION_NOT_THROWN = "exception not thrown"; - public static final String USER_ID = "testUser"; - public static final User TEST_USER = new User.Builder().userId(USER_ID).opAuths("one", "two").build(); - public static final String PROPS_ID_1 = "PROPS_ID_1"; - public static final String SCHEMA_ID_1 = "SCHEMA_ID_1"; - FederatedStore store; + private static final String PATH_FEDERATED_STORE_PROPERTIES = "/properties/federatedStoreTest.properties"; + private static final String FEDERATED_STORE_ID = "testFederatedStoreId"; + private static final String ACC_ID_1 = "mockAccGraphId1"; + private static final String MAP_ID_1 = "mockMapGraphId1"; + private static final String PATH_ACC_STORE_PROPERTIES = "properties/singleUseMockAccStore.properties"; + private static final String PATH_MAP_STORE_PROPERTIES = "properties/singleUseMockMapStore.properties"; + private static final String PATH_MAP_STORE_PROPERTIES_ALT = "properties/singleUseMockMapStoreAlt.properties"; + private static final String PATH_BASIC_ENTITY_SCHEMA_JSON = "schema/basicEntitySchema.json"; + private static final String PATH_BASIC_EDGE_SCHEMA_JSON = "schema/basicEdgeSchema.json"; + private static final String KEY_ACC_ID1_PROPERTIES_FILE = "gaffer.federatedstore.mockAccGraphId1.properties.file"; + private static final String KEY_MAP_ID1_PROPERTIES_FILE = "gaffer.federatedstore.mockMapGraphId1.properties.file"; + private static final String KEY_MAP_ID1_PROPERTIES_ID = "gaffer.federatedstore.mockMapGraphId1.properties.id"; + private static final String GRAPH_IDS = "gaffer.federatedstore.graphIds"; + private static final String KEY_ACC_ID1_SCHEMA_FILE = "gaffer.federatedstore.mockAccGraphId1.schema.file"; + private static final String KEY_MAP_ID1_SCHEMA_FILE = "gaffer.federatedstore.mockMapGraphId1.schema.file"; + private static final String KEY_MAP_ID1_SCHEMA_ID = "gaffer.federatedstore.mockMapGraphId1.schema.id"; + private static final String PATH_INVALID = "nothing.json"; + private static final String EXCEPTION_NOT_THROWN = "exception not thrown"; + private static final String USER_ID = "testUser"; + private static final User TEST_USER = new User.Builder().userId(USER_ID).opAuths("one", "two").build(); + private static final String PROPS_ID_1 = "PROPS_ID_1"; + private static final String SCHEMA_ID_1 = "SCHEMA_ID_1"; + private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; + private FederatedStore store; private StoreProperties federatedProperties; @Before public void setUp() throws Exception { store = new FederatedStore(); federatedProperties = new StoreProperties(); - federatedProperties.set(CacheProperties.CACHE_SERVICE_CLASS, "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"); + federatedProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); HashMapGraphLibrary.clear(); } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/AddGraphTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/AddGraphTest.java index 4b5d389bfda..1255e5b8f01 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/AddGraphTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/AddGraphTest.java @@ -30,7 +30,8 @@ public class AddGraphTest extends OperationTest { - public static final String expectedKey = "gaffer.store.class"; + private static final String FEDERATEDSTORE_CLASS_STRING = "uk.gov.gchq.gaffer.federatedstore.FederatedStore"; + private static final String EXPECTED_GRAPH_ID = "testGraphID"; @Override protected Set getRequiredFields() { @@ -40,26 +41,24 @@ protected Set getRequiredFields() { @Override public void builderShouldCreatePopulatedOperation() { Schema expectedSchema = new Schema.Builder().build(); - String expectedGraphId = "testGraphID"; StoreProperties storeProperties = new StoreProperties(); - String expectedValue = "uk.gov.gchq.gaffer.federatedstore.FederatedStore"; - storeProperties.set(expectedKey, expectedValue); + storeProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); AddGraph op = new AddGraph.Builder() - .graphId(expectedGraphId) + .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) .storeProperties(storeProperties) .build(); - Assert.assertEquals(expectedGraphId, op.getGraphId()); + Assert.assertEquals(EXPECTED_GRAPH_ID, op.getGraphId()); Assert.assertEquals(expectedSchema, op.getSchema()); - Assert.assertTrue(op.getStoreProperties().containsKey(expectedKey)); - Assert.assertEquals(expectedValue, op.getStoreProperties().get(expectedKey)); + Assert.assertTrue(op.getStoreProperties().containsKey(StoreProperties.STORE_CLASS)); + Assert.assertEquals(FEDERATEDSTORE_CLASS_STRING, op.getStoreProperties().get(StoreProperties.STORE_CLASS)); } @Override public void shouldShallowCloneOperation() { final AddGraph a = new Builder() - .graphId("testAddGraph") + .graphId("graphID") .parentPropertiesId("testPropID") .parentSchemaIds(Lists.newArrayList("testSchemaID")) .schema(new Schema.Builder() diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/RemoveGraphTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/RemoveGraphTest.java index f45df733e0f..d5982915174 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/RemoveGraphTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/RemoveGraphTest.java @@ -29,19 +29,19 @@ public class RemoveGraphTest extends OperationTest { + private static final String EXPECTED_GRAPH_ID = "testGraphID"; + @Test public void shouldSerialiseAndDeserialiseOperation() throws SerialisationException, JsonProcessingException { - String expectedGraphId = "testGraphID"; - RemoveGraph op = new Builder() - .setGraphId(expectedGraphId) + .setGraphId(EXPECTED_GRAPH_ID) .build(); byte[] serialise = toJson(op); RemoveGraph deserialise = fromJson(serialise); - Assert.assertEquals(expectedGraphId, deserialise.getGraphId()); + Assert.assertEquals(EXPECTED_GRAPH_ID, deserialise.getGraphId()); } @Override @@ -51,12 +51,11 @@ protected Set getRequiredFields() { @Override public void builderShouldCreatePopulatedOperation() { - String expectedGraphId = "testGraphID"; RemoveGraph op = new Builder() - .setGraphId(expectedGraphId) + .setGraphId(EXPECTED_GRAPH_ID) .build(); - Assert.assertEquals(expectedGraphId, op.getGraphId()); + Assert.assertEquals(EXPECTED_GRAPH_ID, op.getGraphId()); } @Override diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java index 5c615eaec7c..f7e359e8b15 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java @@ -48,6 +48,7 @@ public class FederatedOperationHandlerTest { public static final String TEST_USER = "testUser"; + public static final String TEST_GRAPH_ID = "testGraphId"; private User user; @Before @@ -123,9 +124,9 @@ public final void shouldMergeResultsFromFieldObjectsWithGivenGraphIds() throws E verify(mockStore4, never()).execute(new OperationChain<>(op).shallowClone(), user); } - private Graph getGraphWithMockStore(final Store mockStore){ + private Graph getGraphWithMockStore(final Store mockStore) { return new Graph.Builder() - .config(new GraphConfig("testGraphId")) + .config(new GraphConfig(TEST_GRAPH_ID)) .store(mockStore) .build(); } @@ -189,7 +190,7 @@ final public void shouldNotThrowException() throws Exception { // When try { - new FederatedOperationHandler().doOperation(op, new Context(user), mockStore); + new FederatedOperationHandler().doOperation(op, new Context(user), mockStore); } catch (Exception e) { fail("Exception should not have been thrown: " + e.getMessage()); } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandlerTest.java index 222fc99d15c..88d37928383 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandlerTest.java @@ -47,6 +47,7 @@ public abstract class FederatedOperationOutputHandlerTest, O> { public static final String TEST_ENTITY = "TestEntity"; + public static final String TEST_GRAPH_ID = "testGraphId"; public static final String TEST_USER = "testUser"; public static final String PROPERTY_TYPE = "property"; protected O o1; @@ -136,10 +137,9 @@ public final void shouldMergeResultsFromFieldObjectsWithGivenGraphIds() throws E @Test final public void shouldThrowException() throws Exception { // Given - final String graphID = "graphId"; final String message = "Test Exception"; final OP op = getExampleOperation(); - op.addOption(FederatedStoreConstants.GRAPH_IDS, graphID); + op.addOption(FederatedStoreConstants.GRAPH_IDS, TEST_GRAPH_ID); Schema unusedSchema = new Schema.Builder().build(); @@ -149,7 +149,7 @@ final public void shouldThrowException() throws Exception { FederatedStore mockStore = Mockito.mock(FederatedStore.class); HashSet filteredGraphs = Sets.newHashSet(getGraphWithMockStore(mockStoreInner)); - Mockito.when(mockStore.getGraphs(graphID)).thenReturn(filteredGraphs); + Mockito.when(mockStore.getGraphs(TEST_GRAPH_ID)).thenReturn(filteredGraphs); // When try { @@ -207,7 +207,7 @@ final public void shouldNotThrowException() throws Exception { private Graph getGraphWithMockStore(final Store mockStore) { return new Graph.Builder() .config(new GraphConfig.Builder() - .graphId("TestGraphId") + .graphId(TEST_GRAPH_ID) .build()) .store(mockStore) .build(); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddAllElementsHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddAllElementsHandlerTest.java index 7c10b32248a..ab4d96bc823 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddAllElementsHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddAllElementsHandlerTest.java @@ -38,6 +38,7 @@ public class FederatedAddAllElementsHandlerTest { public static final String TEST_USER = "testUser"; + private static final String TEST_GRAPH_ID = "testGraphId"; protected User user; @Before @@ -84,7 +85,7 @@ public final void shouldMergeResultsFromFieldObjects() throws Exception { private Graph getGraphWithMockStore(final Store mockStore) { return new Graph.Builder() - .config(new GraphConfig("testGraphId")) + .config(new GraphConfig(TEST_GRAPH_ID)) .store(mockStore) .build(); } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java index a29422740a0..62bac59bc6d 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java @@ -48,13 +48,13 @@ public class FederatedAddGraphHandlerTest { - public static final String GAFFER_FEDERATEDSTORE_CUSTOM_PROPERTIES_AUTHS = "gaffer.federatedstore.customPropertiesAuths"; + private static final String GAFFER_FEDERATEDSTORE_CUSTOM_PROPERTIES_AUTHS = "gaffer.federatedstore.customPropertiesAuths"; private static final String FEDERATEDSTORE_GRAPH_ID = "federatedStore"; private static final String EXPECTED_GRAPH_ID = "testGraphID"; private static final String EXPECTED_GRAPH_ID_2 = "testGraphID2"; private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; private static final String FEDERATEDSTORE_CLASS_STRING = "uk.gov.gchq.gaffer.federatedstore.FederatedStore"; - private static final String TEST_USER = "testUser"; + private static final String TEST_USER_ID = "testUser"; @Test public void shouldAddGraph() throws Exception { @@ -78,7 +78,7 @@ public void shouldAddGraph() throws Exception { .schema(expectedSchema) .storeProperties(storeProperties) .build(), - new Context(new User(TEST_USER)), + new Context(new User(TEST_USER_ID)), store); Collection graphs = store.getGraphs(null); @@ -94,7 +94,7 @@ public void shouldAddGraph() throws Exception { .schema(expectedSchema) .storeProperties(storeProperties) .build(), - new Context(new User(TEST_USER)), + new Context(new User(TEST_USER_ID)), store); graphs = store.getGraphs(null); @@ -129,7 +129,7 @@ public void shouldAddGraphUsingLibrary() throws Exception { .schema(expectedSchema) .storeProperties(storeProperties) .build(), - new Context(new User(TEST_USER)), + new Context(new User(TEST_USER_ID)), store); Collection graphs = store.getGraphs(null); @@ -149,7 +149,7 @@ public void shouldAddGraphUsingLibrary() throws Exception { new AddGraph.Builder() .graphId(graphId2) .build(), - new Context(new User(TEST_USER)), + new Context(new User(TEST_USER_ID)), store); graphs = store.getGraphs(null); @@ -186,7 +186,7 @@ public void shouldNotOverwriteGraph() throws Exception { .schema(expectedSchema) .storeProperties(storeProperties) .build(), - new Context(new User(TEST_USER)), + new Context(new User(TEST_USER_ID)), store); try { @@ -196,7 +196,7 @@ public void shouldNotOverwriteGraph() throws Exception { .schema(expectedSchema) .storeProperties(storeProperties) .build(), - new Context(new User(TEST_USER)), + new Context(new User(TEST_USER_ID)), store); } catch (final OverwritingException e) { assertEquals(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE_A_GRAPH_WITHIN_FEDERATED_STORE_GRAPH_ID_S, EXPECTED_GRAPH_ID), e.getMessage()); @@ -231,7 +231,7 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { .schema(expectedSchema) .storeProperties(graphStoreProperties) .build(), - new Context(new User(TEST_USER)), + new Context(new User(TEST_USER_ID)), store); fail("Exception not thrown"); } catch (OperationException e) { @@ -248,7 +248,7 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { .storeProperties(graphStoreProperties) .build(), new Context(new User.Builder() - .userId(TEST_USER) + .userId(TEST_USER_ID) .opAuth("auth1") .build()), store); @@ -289,14 +289,14 @@ public void shouldAddGraphWithAuthsAndAddingUser() throws Exception { .storeProperties(storeProperties) .graphAuths("testAuth") .build(), - new Context(new User(TEST_USER)), + new Context(new User(TEST_USER_ID)), store); final CloseableIterable elements = new FederatedGetAllElementsHandler().doOperation( new GetAllElements(), new Context(new User.Builder() - .userId(TEST_USER) + .userId(TEST_USER_ID) .build()), store); From ec447bf875f345f35006a4e4f074107bddf0f112 Mon Sep 17 00:00:00 2001 From: m55624 Date: Thu, 28 Sep 2017 13:47:32 +0100 Subject: [PATCH 05/72] gh-1297 - FederatedStore tests added --- .../gchq/gaffer/cache/impl/HashMapCache.java | 2 +- .../gaffer/federatedstore/FederatedStore.java | 11 ++- .../impl/FederatedAddGraphHandler.java | 7 +- .../federatedstore/FederatedStoreTest.java | 77 ++++++++++++++++++- .../predefinedFederatedStore.properties | 2 +- 5 files changed, 92 insertions(+), 7 deletions(-) diff --git a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCache.java b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCache.java index 22d92d9a9e4..2b3ce984c22 100644 --- a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCache.java +++ b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCache.java @@ -29,7 +29,7 @@ * @param The object type that acts as the key for the HashMap * @param The value that is stored in the HashMap */ -public class HashMapCache implements ICache { +public class HashMapCache implements ICache { private final HashMap cache = new HashMap<>(); diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 94386aa614c..912ef0139b1 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -23,6 +23,7 @@ import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.cache.ICacheService; import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; +import uk.gov.gchq.gaffer.cache.util.CacheProperties; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; @@ -116,6 +117,10 @@ public class FederatedStore extends Store { */ @Override public void initialise(final String graphId, final Schema unused, final StoreProperties properties) throws StoreException { + if (properties.get(CacheProperties.CACHE_SERVICE_CLASS) == null) { + throw new StoreException("No cache has been set, please check the property " + + CacheProperties.CACHE_SERVICE_CLASS + " has been set in the Store Properties"); + } super.initialise(graphId, new Schema(), properties); setCacheService(); loadCustomPropertiesAuths(); @@ -166,9 +171,9 @@ public static OP updateOperationForGraph(final OP operati * * @param graphs the graph to add */ - public void addGraphs(final Graph... graphs) { + public void addGraphs(final Graph... graphs) throws StoreException { if (cacheService == null) { - throw new RuntimeException("No cache has been set, please initialise the FederatedStore instance"); + throw new StoreException("No cache has been set, please initialise the FederatedStore instance"); } for (final Graph graph : graphs) { _add(graph); @@ -330,7 +335,7 @@ private void loadCustomPropertiesAuths() { } } - private void loadGraphs() { + private void loadGraphs() throws StoreException { final Set graphIds = getGraphIds(); final Set graphsToLoad = new HashSet<>(); for (final String graphId : graphIds) { diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java index 57cf5c66a42..8811851b9ad 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java @@ -24,6 +24,7 @@ import uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate; import uk.gov.gchq.gaffer.store.Context; import uk.gov.gchq.gaffer.store.Store; +import uk.gov.gchq.gaffer.store.StoreException; import uk.gov.gchq.gaffer.store.operation.handler.OperationHandler; import uk.gov.gchq.gaffer.user.User; @@ -55,7 +56,11 @@ public Void doOperation(final AddGraph operation, final Context context, final S operation.getSchema(), operation.getStoreProperties(), operation.getParentSchemaIds(), operation.getParentPropertiesId(), hook); - ((FederatedStore) store).addGraphs(graph); + try { + ((FederatedStore) store).addGraphs(graph); + } catch (StoreException e) { + throw new OperationException(e.getMessage()); + } return null; } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index d4b7fd7e159..ddc3cedfe97 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -24,6 +24,7 @@ import org.mockito.Mockito; import uk.gov.gchq.gaffer.accumulostore.SingleUseAccumuloStore; +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.cache.util.CacheProperties; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; @@ -100,6 +101,80 @@ public void setUp() throws Exception { HashMapGraphLibrary.clear(); } + @Test + public void shouldInitialiseWithCache() throws StoreException { + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + assertNotNull(CacheServiceLoader.getService()); + } + + @Test + public void shouldThrowExceptionWithoutInitialisation() { + // Given + Graph graphToAdd = new Graph.Builder() + .config(new GraphConfig(ACC_ID_1)) + .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES)) + .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) + .build(); + + // When / Then + try { + store.addGraphs(graphToAdd); + fail("Exception expected"); + } catch (final StoreException e) { + assertTrue(e.getMessage().contains("No cache has been set")); + System.out.println(e); + } + } + + @Test + public void shouldThrowExceptionWhenInitialisedWithNoCacheClassInProperties() throws StoreException { + // Given + StoreProperties storeProperties = new StoreProperties(); + + try { + store.initialise(FEDERATED_STORE_ID, null, storeProperties); + fail("Exception expected"); + } catch (final StoreException e) { + assertTrue(e.getMessage().contains("No cache has been set, please check the property")); + } + } + + @Test + public void shouldAddGraphsToCache() throws StoreException { + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + + Graph graphToAdd = new Graph.Builder() + .config(new GraphConfig(ACC_ID_1)) + .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES)) + .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) + .build(); + + store.addGraphs(graphToAdd); + + assertEquals(store.getGraphs(ACC_ID_1).size(), 1); + + Collection storeGraphs = store.getGraphs(null); + assertTrue(storeGraphs.contains(graphToAdd)); + } + + @Test + public void shouldAddMultipleGraphsToCache() throws StoreException { + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + + List graphsToAdd = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + graphsToAdd.add(new Graph.Builder() + .config(new GraphConfig(ACC_ID_1 + i)) + .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES)) + .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) + .build()); + } + + store.addGraphs(graphsToAdd.toArray(new Graph[graphsToAdd.size()])); + + assertEquals(store.getGraphs(null).size(), 10); + } + @Test public void shouldLoadGraphsWithIds() throws Exception { //Given @@ -988,7 +1063,7 @@ private boolean checkUnexpected(final Collection unexpectedGraphs, final return false; } - private List> populateGraphs(int... expectedIds) { + private List> populateGraphs(int... expectedIds) throws StoreException { final Collection expectedGraphs = new ArrayList<>(); final Collection unexpectedGraphs = new ArrayList<>(); for (int i = 0; i < 5; i++) { diff --git a/store-implementation/federated-store/src/test/resources/predefinedFederatedStore.properties b/store-implementation/federated-store/src/test/resources/predefinedFederatedStore.properties index 2fb7921d00b..c54917930f3 100644 --- a/store-implementation/federated-store/src/test/resources/predefinedFederatedStore.properties +++ b/store-implementation/federated-store/src/test/resources/predefinedFederatedStore.properties @@ -14,4 +14,4 @@ # limitations under the License. # gaffer.store.class=uk.gov.gchq.gaffer.federatedstore.PredefinedFederatedStore -gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService \ No newline at end of file +gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService From 2d316f303ff975f2dbb53e7ab2a182ba54b0a4cc Mon Sep 17 00:00:00 2001 From: m55624 Date: Thu, 28 Sep 2017 14:01:36 +0100 Subject: [PATCH 06/72] gh-1297 - removed access to unused method --- .../src/main/java/uk/gov/gchq/gaffer/store/Store.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java index 5385e1fb51c..00482d13e74 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java @@ -662,10 +662,6 @@ protected final OperationHandler getOperationHandler(final Class operationChain, final Context context, final String msg, final JobStatus jobStatus) { final JobDetail newJobDetail = new JobDetail(context.getJobId(), context .getUser() @@ -790,4 +786,8 @@ private void addConfiguredOperationHandlers() { } } } + + private void startCacheServiceLoader(final StoreProperties properties) { + CacheServiceLoader.initialise(properties.getProperties()); + } } From 9e2954001a1aee773f7b548acfa8296d7c0838cc Mon Sep 17 00:00:00 2001 From: m607123 Date: Tue, 26 Sep 2017 10:42:08 +0100 Subject: [PATCH 07/72] gh-1341 - New function Operations registered in Store --- .../src/main/java/uk/gov/gchq/gaffer/store/Store.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java index 00482d13e74..fa004d4de45 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java @@ -39,6 +39,7 @@ import uk.gov.gchq.gaffer.operation.OperationChain; import uk.gov.gchq.gaffer.operation.OperationChainDAO; import uk.gov.gchq.gaffer.operation.OperationException; +import uk.gov.gchq.gaffer.operation.function.Aggregate; import uk.gov.gchq.gaffer.operation.impl.Count; import uk.gov.gchq.gaffer.operation.impl.CountGroups; import uk.gov.gchq.gaffer.operation.impl.DiscardOutput; @@ -52,6 +53,8 @@ import uk.gov.gchq.gaffer.operation.impl.export.resultcache.ExportToGafferResultCache; import uk.gov.gchq.gaffer.operation.impl.export.set.ExportToSet; import uk.gov.gchq.gaffer.operation.impl.export.set.GetSetExport; +import uk.gov.gchq.gaffer.operation.impl.function.Filter; +import uk.gov.gchq.gaffer.operation.impl.function.Transform; import uk.gov.gchq.gaffer.operation.impl.generate.GenerateElements; import uk.gov.gchq.gaffer.operation.impl.generate.GenerateObjects; import uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds; @@ -90,6 +93,9 @@ import uk.gov.gchq.gaffer.store.operation.handler.export.GetExportsHandler; import uk.gov.gchq.gaffer.store.operation.handler.export.set.ExportToSetHandler; import uk.gov.gchq.gaffer.store.operation.handler.export.set.GetSetExportHandler; +import uk.gov.gchq.gaffer.store.operation.handler.function.AggregateHandler; +import uk.gov.gchq.gaffer.store.operation.handler.function.FilterHandler; +import uk.gov.gchq.gaffer.store.operation.handler.function.TransformHandler; import uk.gov.gchq.gaffer.store.operation.handler.generate.GenerateElementsHandler; import uk.gov.gchq.gaffer.store.operation.handler.generate.GenerateObjectsHandler; import uk.gov.gchq.gaffer.store.operation.handler.job.GetAllJobDetailsHandler; @@ -776,6 +782,11 @@ private void addCoreOpHandlers() { addOperationHandler(CountGroups.class, new CountGroupsHandler()); addOperationHandler(Limit.class, new LimitHandler()); addOperationHandler(DiscardOutput.class, new DiscardOutputHandler()); + + // Function + addOperationHandler(Filter.class, new FilterHandler()); + addOperationHandler(Transform.class, new TransformHandler()); + addOperationHandler(Aggregate.class, new AggregateHandler()); } private void addConfiguredOperationHandlers() { From 2db38e78f41afac3a093e2e338b58ff21994eea1 Mon Sep 17 00:00:00 2001 From: m607123 Date: Tue, 26 Sep 2017 14:00:39 +0100 Subject: [PATCH 08/72] gh-1341 - Test failures fixed --- .../test/java/uk/gov/gchq/gaffer/store/StoreTest.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/core/store/src/test/java/uk/gov/gchq/gaffer/store/StoreTest.java b/core/store/src/test/java/uk/gov/gchq/gaffer/store/StoreTest.java index 5b0e481cf53..85c2c9ca913 100644 --- a/core/store/src/test/java/uk/gov/gchq/gaffer/store/StoreTest.java +++ b/core/store/src/test/java/uk/gov/gchq/gaffer/store/StoreTest.java @@ -49,6 +49,7 @@ import uk.gov.gchq.gaffer.operation.OperationChain; import uk.gov.gchq.gaffer.operation.OperationChainDAO; import uk.gov.gchq.gaffer.operation.OperationException; +import uk.gov.gchq.gaffer.operation.function.Aggregate; import uk.gov.gchq.gaffer.operation.impl.Count; import uk.gov.gchq.gaffer.operation.impl.CountGroups; import uk.gov.gchq.gaffer.operation.impl.DiscardOutput; @@ -63,6 +64,8 @@ import uk.gov.gchq.gaffer.operation.impl.export.resultcache.GetGafferResultCacheExport; import uk.gov.gchq.gaffer.operation.impl.export.set.ExportToSet; import uk.gov.gchq.gaffer.operation.impl.export.set.GetSetExport; +import uk.gov.gchq.gaffer.operation.impl.function.Filter; +import uk.gov.gchq.gaffer.operation.impl.function.Transform; import uk.gov.gchq.gaffer.operation.impl.generate.GenerateElements; import uk.gov.gchq.gaffer.operation.impl.generate.GenerateObjects; import uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds; @@ -505,7 +508,12 @@ public void shouldReturnAllSupportedOperations() throws Exception { Count.class, CountGroups.class, Limit.class, - DiscardOutput.class + DiscardOutput.class, + + // Function + Filter.class, + Transform.class, + Aggregate.class ); expectedOperations.sort(Comparator.comparing(Class::getName)); From bee9e1edca283dbf6de95b5f2d60de4e647e2749 Mon Sep 17 00:00:00 2001 From: p013570 Date: Wed, 27 Sep 2017 09:54:35 +0100 Subject: [PATCH 09/72] gh-1341 - Fixed stream iterable json serialisation --- .../gov/gchq/gaffer/commonutil/iterable/StreamIterable.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/common-util/src/main/java/uk/gov/gchq/gaffer/commonutil/iterable/StreamIterable.java b/core/common-util/src/main/java/uk/gov/gchq/gaffer/commonutil/iterable/StreamIterable.java index 49c0412ab57..918134cbf85 100644 --- a/core/common-util/src/main/java/uk/gov/gchq/gaffer/commonutil/iterable/StreamIterable.java +++ b/core/common-util/src/main/java/uk/gov/gchq/gaffer/commonutil/iterable/StreamIterable.java @@ -16,6 +16,8 @@ package uk.gov.gchq.gaffer.commonutil.iterable; +import com.fasterxml.jackson.annotation.JsonIgnore; + import uk.gov.gchq.gaffer.commonutil.CloseableUtil; import uk.gov.gchq.gaffer.commonutil.stream.StreamSupplier; @@ -46,12 +48,13 @@ public CloseableIterator iterator() { /** * Get a {@link java.util.stream.Stream} from the {@link uk.gov.gchq.gaffer.commonutil.stream.StreamSupplier}. - * + *

* This enables the creation of multiple stream objects from the same base * data, without operating on the same stream multiple times. * * @return a new {@link java.util.stream.Stream} */ + @JsonIgnore public Stream getStream() { return streamSupplier.get(); } From c63cb11543ed74b8abe924f983b042a8b09c0393 Mon Sep 17 00:00:00 2001 From: p013570 Date: Wed, 27 Sep 2017 16:58:11 +0100 Subject: [PATCH 10/72] gh-1349 - added onFailure method to GraphHook --- .../java/uk/gov/gchq/gaffer/graph/Graph.java | 46 ++- .../graph/hook/AddOperationsToChain.java | 13 +- .../gov/gchq/gaffer/graph/hook/GraphHook.java | 18 +- .../gchq/gaffer/graph/hook/Log4jLogger.java | 6 + .../graph/hook/NamedOperationResolver.java | 5 + .../graph/hook/OperationAuthoriser.java | 7 +- .../graph/hook/OperationChainLimiter.java | 5 + .../uk/gov/gchq/gaffer/graph/GraphTest.java | 300 ++++++++++++++++++ .../federatedstore/FederatedAccessHook.java | 5 + 9 files changed, 372 insertions(+), 33 deletions(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java index 50b79622812..db1989d2a5a 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java @@ -150,26 +150,7 @@ public O execute(final Output operation, final User user) throws Operatio * @throws OperationException thrown if the job fails to run. */ public JobDetail executeJob(final OperationChain operationChain, final User user) throws OperationException { - final OperationChain clonedOpChain = operationChain.shallowClone(); - try { - for (final GraphHook graphHook : config.getHooks()) { - graphHook.preExecute(clonedOpChain, user); - } - - updateOperationChainView(clonedOpChain); - - JobDetail result = store.executeJob(clonedOpChain, user); - - for (final GraphHook graphHook : config.getHooks()) { - result = graphHook.postExecute(result, clonedOpChain, user); - } - - return result; - - } catch (final Exception e) { - CloseableUtil.close(clonedOpChain); - throw e; - } + return _execute(store::executeJob, operationChain, user); } /** @@ -184,31 +165,37 @@ public JobDetail executeJob(final OperationChain operationChain, final User u * @throws OperationException if an operation fails */ public O execute(final OperationChain operationChain, final User user) throws OperationException { + return (O) _execute(store::execute, operationChain, user); + } + + private O _execute(final StoreExecuter storeExecuter, final OperationChain operationChain, final User user) throws OperationException { if (null == operationChain) { throw new IllegalArgumentException("operationChain is required"); } - final OperationChain clonedOpChain = operationChain.shallowClone(); + final OperationChain clonedOpChain = operationChain.shallowClone(); O result = null; try { for (final GraphHook graphHook : config.getHooks()) { graphHook.preExecute(clonedOpChain, user); } - updateOperationChainView(clonedOpChain); - - result = store.execute(clonedOpChain, user); - + result = storeExecuter.execute(clonedOpChain, user); for (final GraphHook graphHook : config.getHooks()) { result = graphHook.postExecute(result, clonedOpChain, user); } } catch (final Exception e) { + for (final GraphHook graphHook : config.getHooks()) { + try { + result = graphHook.onFailure(result, clonedOpChain, user); + } catch (final Exception graphHookE) { + LOGGER.warn("Error in graphHook " + graphHook.getClass().getSimpleName() + ": " + graphHookE.getMessage(), graphHookE); + } + } CloseableUtil.close(clonedOpChain); CloseableUtil.close(result); - throw e; } - return result; } @@ -327,6 +314,11 @@ public GraphLibrary getGraphLibrary() { return store.getGraphLibrary(); } + @FunctionalInterface + private interface StoreExecuter { + O execute(final OperationChain operationChain, final User user) throws OperationException; + } + /** *

* Builder for {@link Graph}. diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/AddOperationsToChain.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/AddOperationsToChain.java index b6f5eec6641..d85f1dfc234 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/AddOperationsToChain.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/AddOperationsToChain.java @@ -78,6 +78,11 @@ public T postExecute(final T result, return result; } + @Override + public T onFailure(final T result, final OperationChain opChain, final User user) { + return result; + } + public List getStart() { return defaultOperations.getStart(); } @@ -130,15 +135,15 @@ private List addOperationsToChain(final OperationChain opChain, fi opList.addAll(addOperationsToChain((OperationChain) originalOp, additionalOperations)); } else { final List beforeOps = additionalOperations.getBefore() - .get(originalOp.getClass() - .getName()); + .get(originalOp.getClass() + .getName()); if (null != beforeOps) { opList.addAll(beforeOps); } opList.add(originalOp); final List afterOps = additionalOperations.getAfter() - .get(originalOp.getClass() - .getName()); + .get(originalOp.getClass() + .getName()); if (null != afterOps) { opList.addAll(afterOps); } diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/GraphHook.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/GraphHook.java index 7c289cbad92..bbc6ab9e321 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/GraphHook.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/GraphHook.java @@ -22,7 +22,9 @@ /** * A {@code GraphHook} can be registered with a {@link uk.gov.gchq.gaffer.graph.Graph} and will be - * triggered before and after operation chains are executed on the graph. + * triggered before and after operation chains are executed on the graph. If an + * error occurs whilst running the operation chain the onFailure method will be + * triggered. */ @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "class") public interface GraphHook { @@ -48,4 +50,18 @@ public interface GraphHook { T postExecute(final T result, final OperationChain opChain, final User user); + + /** + * Called from {@link uk.gov.gchq.gaffer.graph.Graph} if an error occurs whilst + * executing the {@link OperationChain}. + * + * @param result the result from the operation chain - likely to be null. + * @param opChain the {@link OperationChain} that was executed. + * @param user the {@link User} who executed the operation chain + * @param the result type + * @return result object + */ + T onFailure(final T result, + final OperationChain opChain, + final User user); } diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/Log4jLogger.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/Log4jLogger.java index d3a543564c7..af8d267e593 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/Log4jLogger.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/Log4jLogger.java @@ -45,4 +45,10 @@ public T postExecute(final T result, final OperationChain operationChain, // No logging required. return result; } + + @Override + public T onFailure(final T result, final OperationChain opChain, final User user) { + LOGGER.warn("Failed to run {} as {}", opChain, user.getUserId()); + return result; + } } diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/NamedOperationResolver.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/NamedOperationResolver.java index 8825876adb3..f7252cc6b0e 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/NamedOperationResolver.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/NamedOperationResolver.java @@ -55,6 +55,11 @@ public T postExecute(final T result, final OperationChain opChain, final return result; } + @Override + public T onFailure(final T result, final OperationChain opChain, final User user) { + return result; + } + private List resolveNamedOperations(final List operations, final User user) { List updatedOperations = new ArrayList<>(operations.size()); for (final Operation operation : operations) { diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/OperationAuthoriser.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/OperationAuthoriser.java index d0952f8ca09..815f5fd1250 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/OperationAuthoriser.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/OperationAuthoriser.java @@ -49,7 +49,7 @@ public class OperationAuthoriser implements GraphHook { * This is done by checking the user's auths against the operation auths. * If an operation cannot be executed then an {@link IllegalAccessError} is thrown. * - * @param user the user to authorise. + * @param user the user to authorise. * @param opChain the operation chain. */ @Override @@ -68,6 +68,11 @@ public T postExecute(final T result, final OperationChain opChain, final return result; } + @Override + public T onFailure(final T result, final OperationChain opChain, final User user) { + return result; + } + /** * Add operation authorisations for a given operation class. * This can be called multiple times for the same operation class and the diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/OperationChainLimiter.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/OperationChainLimiter.java index 834807b9888..5dcc0b01aba 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/OperationChainLimiter.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/hook/OperationChainLimiter.java @@ -75,6 +75,11 @@ public T postExecute(final T result, final OperationChain opChain, final return result; } + @Override + public T onFailure(final T result, final OperationChain opChain, final User user) { + return result; + } + public Map, Integer> getOpScores() { return scorer.getOpScores(); } diff --git a/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphTest.java b/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphTest.java index 237a2689129..bd00d313fe9 100644 --- a/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphTest.java +++ b/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphTest.java @@ -100,8 +100,12 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; public class GraphTest { @@ -537,6 +541,302 @@ public void shouldCallAllGraphHooksAfterJobExecuted() throws OperationException assertSame(actualResult, result3); } + @Test + public void shouldCallAllGraphHooksOnGraphHookPreExecuteFailure() throws OperationException { + // Given + final Operation operation = mock(Operation.class); + final OperationChain opChain = mock(OperationChain.class); + final OperationChain clonedOpChain = mock(OperationChain.class); + given(opChain.shallowClone()).willReturn(clonedOpChain); + given(clonedOpChain.getOperations()).willReturn(Lists.newArrayList(operation)); + + final User user = mock(User.class); + final GraphHook hook1 = mock(GraphHook.class); + final GraphHook hook2 = mock(GraphHook.class); + final Store store = mock(Store.class); + final Schema schema = new Schema(); + + given(store.getSchema()).willReturn(schema); + doThrow(new RuntimeException("Hook2 failed in postExecute")).when(hook1).preExecute(clonedOpChain, user); + given(hook1.onFailure(null, clonedOpChain, user)).willThrow(new RuntimeException("Hook1 failed in onFailure")); + given(hook2.onFailure(null, clonedOpChain, user)).willReturn(null); + + final Graph graph = new Graph.Builder() + .config(new GraphConfig.Builder() + .graphId(GRAPH_ID) + .addHook(hook1) + .addHook(hook2) + .build()) + .storeProperties(StreamUtil.storeProps(getClass())) + .store(store) + .addSchema(schema) + .build(); + + // When / Then + try { + graph.execute(opChain, user); + fail("Exception expected"); + } catch (final RuntimeException e) { + final InOrder inOrder = inOrder(hook1, hook2); + inOrder.verify(hook2, never()).preExecute(any(), any()); + inOrder.verify(hook1, never()).postExecute(any(), any(), any()); + inOrder.verify(hook2, never()).postExecute(any(), any(), any()); + inOrder.verify(hook1).onFailure(eq(null), any(), eq(user)); + inOrder.verify(hook2).onFailure(eq(null), any(), eq(user)); + } + } + + @Test + public void shouldCallAllGraphHooksOnGraphHookPostExecuteFailure() throws OperationException { + // Given + final Operation operation = mock(Operation.class); + final OperationChain opChain = mock(OperationChain.class); + final OperationChain clonedOpChain = mock(OperationChain.class); + given(opChain.shallowClone()).willReturn(clonedOpChain); + given(clonedOpChain.getOperations()).willReturn(Lists.newArrayList(operation)); + + final User user = mock(User.class); + final GraphHook hook1 = mock(GraphHook.class); + final GraphHook hook2 = mock(GraphHook.class); + final Store store = mock(Store.class); + final Object result1 = mock(Object.class); + final Object result2 = mock(Object.class); + final Object result3 = mock(Object.class); + final Schema schema = new Schema(); + + given(store.getSchema()).willReturn(schema); + given(hook1.postExecute(result1, clonedOpChain, user)).willReturn(result2); + given(hook2.postExecute(result2, clonedOpChain, user)).willThrow(new RuntimeException("Hook2 failed in postExecute")); + given(hook1.onFailure(result2, clonedOpChain, user)).willThrow(new RuntimeException("Hook1 failed in onFailure")); + given(hook2.onFailure(result2, clonedOpChain, user)).willReturn(result3); + + final Graph graph = new Graph.Builder() + .config(new GraphConfig.Builder() + .graphId(GRAPH_ID) + .addHook(hook1) + .addHook(hook2) + .build()) + .storeProperties(StreamUtil.storeProps(getClass())) + .store(store) + .addSchema(schema) + .build(); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(OperationChain.class); + given(store.execute(captor.capture(), Mockito.eq(user))).willReturn(result1); + + // When / Then + try { + graph.execute(opChain, user); + fail("Exception expected"); + } catch (final RuntimeException e) { + final InOrder inOrder = inOrder(hook1, hook2); + inOrder.verify(hook1).postExecute(result1, captor.getValue(), user); + inOrder.verify(hook2).postExecute(result2, captor.getValue(), user); + inOrder.verify(hook1).onFailure(result2, captor.getValue(), user); + inOrder.verify(hook2).onFailure(result2, captor.getValue(), user); + final List ops = captor.getValue().getOperations(); + assertEquals(1, ops.size()); + assertSame(operation, ops.get(0)); + } + } + + @Test + public void shouldCallAllGraphHooksOnExecuteFailure() throws OperationException { + // Given + final Operation operation = mock(Operation.class); + final OperationChain opChain = mock(OperationChain.class); + final OperationChain clonedOpChain = mock(OperationChain.class); + given(opChain.shallowClone()).willReturn(clonedOpChain); + given(clonedOpChain.getOperations()).willReturn(Lists.newArrayList(operation)); + + final User user = mock(User.class); + final GraphHook hook1 = mock(GraphHook.class); + final GraphHook hook2 = mock(GraphHook.class); + final Store store = mock(Store.class); + final Schema schema = new Schema(); + + given(store.getSchema()).willReturn(schema); + given(hook1.onFailure(null, clonedOpChain, user)).willThrow(new RuntimeException("Hook1 failed in onFailure")); + given(hook2.onFailure(null, clonedOpChain, user)).willReturn(null); + + final Graph graph = new Graph.Builder() + .config(new GraphConfig.Builder() + .graphId(GRAPH_ID) + .addHook(hook1) + .addHook(hook2) + .build()) + .storeProperties(StreamUtil.storeProps(getClass())) + .store(store) + .addSchema(schema) + .build(); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(OperationChain.class); + given(store.execute(captor.capture(), Mockito.eq(user))).willThrow(new RuntimeException("Store failed to execute operation chain")); + + // When / Then + try { + graph.execute(opChain, user); + fail("Exception expected"); + } catch (final RuntimeException e) { + final InOrder inOrder = inOrder(hook1, hook2); + inOrder.verify(hook1, never()).postExecute(any(), any(), any()); + inOrder.verify(hook2, never()).postExecute(any(), any(), any()); + inOrder.verify(hook1).onFailure(null, captor.getValue(), user); + inOrder.verify(hook2).onFailure(null, captor.getValue(), user); + final List ops = captor.getValue().getOperations(); + assertEquals(1, ops.size()); + assertSame(operation, ops.get(0)); + } + } + + @Test + public void shouldCallAllGraphHooksOnGraphHookPreExecuteFailureWhenRunningJob() throws OperationException { + // Given + final Operation operation = mock(Operation.class); + final OperationChain opChain = mock(OperationChain.class); + final OperationChain clonedOpChain = mock(OperationChain.class); + given(opChain.shallowClone()).willReturn(clonedOpChain); + given(clonedOpChain.getOperations()).willReturn(Lists.newArrayList(operation)); + + final User user = mock(User.class); + final GraphHook hook1 = mock(GraphHook.class); + final GraphHook hook2 = mock(GraphHook.class); + final Store store = mock(Store.class); + final Schema schema = new Schema(); + + given(store.getSchema()).willReturn(schema); + doThrow(new RuntimeException("Hook2 failed in postExecute")).when(hook1).preExecute(clonedOpChain, user); + given(hook1.onFailure(null, clonedOpChain, user)).willThrow(new RuntimeException("Hook1 failed in onFailure")); + given(hook2.onFailure(null, clonedOpChain, user)).willReturn(null); + + final Graph graph = new Graph.Builder() + .config(new GraphConfig.Builder() + .graphId(GRAPH_ID) + .addHook(hook1) + .addHook(hook2) + .build()) + .storeProperties(StreamUtil.storeProps(getClass())) + .store(store) + .addSchema(schema) + .build(); + + // When / Then + try { + graph.executeJob(opChain, user); + fail("Exception expected"); + } catch (final RuntimeException e) { + final InOrder inOrder = inOrder(hook1, hook2); + inOrder.verify(hook2, never()).preExecute(any(), any()); + inOrder.verify(hook1, never()).postExecute(any(), any(), any()); + inOrder.verify(hook2, never()).postExecute(any(), any(), any()); + inOrder.verify(hook1).onFailure(eq(null), any(), eq(user)); + inOrder.verify(hook2).onFailure(eq(null), any(), eq(user)); + } + } + + @Test + public void shouldCallAllGraphHooksOnGraphHookPostExecuteFailureWhenRunningJob() throws OperationException { + // Given + final Operation operation = mock(Operation.class); + final OperationChain opChain = mock(OperationChain.class); + final OperationChain clonedOpChain = mock(OperationChain.class); + given(opChain.shallowClone()).willReturn(clonedOpChain); + given(clonedOpChain.getOperations()).willReturn(Lists.newArrayList(operation)); + + final User user = mock(User.class); + final GraphHook hook1 = mock(GraphHook.class); + final GraphHook hook2 = mock(GraphHook.class); + final Store store = mock(Store.class); + final JobDetail result1 = mock(JobDetail.class); + final JobDetail result2 = mock(JobDetail.class); + final JobDetail result3 = mock(JobDetail.class); + final Schema schema = new Schema(); + + given(store.getSchema()).willReturn(schema); + given(hook1.postExecute(result1, clonedOpChain, user)).willReturn(result2); + given(hook2.postExecute(result2, clonedOpChain, user)).willThrow(new RuntimeException("Hook2 failed in postExecute")); + given(hook1.onFailure(result2, clonedOpChain, user)).willThrow(new RuntimeException("Hook1 failed in onFailure")); + given(hook2.onFailure(result2, clonedOpChain, user)).willReturn(result3); + + final Graph graph = new Graph.Builder() + .config(new GraphConfig.Builder() + .graphId(GRAPH_ID) + .addHook(hook1) + .addHook(hook2) + .build()) + .storeProperties(StreamUtil.storeProps(getClass())) + .store(store) + .addSchema(schema) + .build(); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(OperationChain.class); + given(store.executeJob(captor.capture(), Mockito.eq(user))).willReturn(result1); + + // When / Then + try { + graph.executeJob(opChain, user); + fail("Exception expected"); + } catch (final RuntimeException e) { + final InOrder inOrder = inOrder(hook1, hook2); + inOrder.verify(hook1).postExecute(result1, captor.getValue(), user); + inOrder.verify(hook2).postExecute(result2, captor.getValue(), user); + inOrder.verify(hook1).onFailure(result2, captor.getValue(), user); + inOrder.verify(hook2).onFailure(result2, captor.getValue(), user); + final List ops = captor.getValue().getOperations(); + assertEquals(1, ops.size()); + assertSame(operation, ops.get(0)); + } + } + + @Test + public void shouldCallAllGraphHooksOnExecuteFailureWhenRunningJob() throws OperationException { + // Given + final Operation operation = mock(Operation.class); + final OperationChain opChain = mock(OperationChain.class); + final OperationChain clonedOpChain = mock(OperationChain.class); + given(opChain.shallowClone()).willReturn(clonedOpChain); + given(clonedOpChain.getOperations()).willReturn(Lists.newArrayList(operation)); + + final User user = mock(User.class); + final GraphHook hook1 = mock(GraphHook.class); + final GraphHook hook2 = mock(GraphHook.class); + final Store store = mock(Store.class); + final Schema schema = new Schema(); + + given(store.getSchema()).willReturn(schema); + given(hook1.onFailure(null, clonedOpChain, user)).willThrow(new RuntimeException("Hook1 failed in onFailure")); + given(hook2.onFailure(null, clonedOpChain, user)).willReturn(null); + + final Graph graph = new Graph.Builder() + .config(new GraphConfig.Builder() + .graphId(GRAPH_ID) + .addHook(hook1) + .addHook(hook2) + .build()) + .storeProperties(StreamUtil.storeProps(getClass())) + .store(store) + .addSchema(schema) + .build(); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(OperationChain.class); + given(store.executeJob(captor.capture(), Mockito.eq(user))).willThrow(new RuntimeException("Store failed to execute operation chain")); + + // When / Then + try { + graph.executeJob(opChain, user); + fail("Exception expected"); + } catch (final RuntimeException e) { + final InOrder inOrder = inOrder(hook1, hook2); + inOrder.verify(hook1, never()).postExecute(any(), any(), any()); + inOrder.verify(hook2, never()).postExecute(any(), any(), any()); + inOrder.verify(hook1).onFailure(null, captor.getValue(), user); + inOrder.verify(hook2).onFailure(null, captor.getValue(), user); + final List ops = captor.getValue().getOperations(); + assertEquals(1, ops.size()); + assertSame(operation, ops.get(0)); + } + } + @Test public void shouldConstructGraphAndCreateViewWithGroups() { // Given diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessHook.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessHook.java index c03c63e9d72..752cbb75941 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessHook.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessHook.java @@ -99,6 +99,11 @@ public T postExecute(final T result, final OperationChain opChain, final return result; } + @Override + public T onFailure(final T result, final OperationChain opChain, final User user) { + return result; + } + public static class Builder { private final FederatedAccessHook hook = new FederatedAccessHook(); private final Builder self = this; From 99cedb33b5dadf83a352990dd1223f920bf23880 Mon Sep 17 00:00:00 2001 From: p013570 Date: Wed, 27 Sep 2017 17:48:48 +0100 Subject: [PATCH 11/72] gh-1349 - fed-store is now built alongside map-store --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index b8107ba9783..40e4a093ce1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,10 +15,9 @@ env: - RELEASE=true - MODULES=:gaffer2,:core,:cache,:common-util,:data,:exception,:graph,:operation,:serialisation,:store,:type - MODULES=:accumulo-store,:spark-accumulo-library,:accumulo-rest - - MODULES=:federated-store - MODULES=:hbase-store,:hbase-rest - MODULES=:parquet-store,:parquet-rest - - MODULES=:integration-test,:map-store,:map-rest + - MODULES=:integration-test,:federated-store,:map-store,:map-rest - MODULES=:example,:basic,:basic-model,:basic-rest,:road-traffic,:road-traffic-model,:road-traffic-generators,:road-traffic-rest,:road-traffic-demo,:federated-demo - MODULES=:rest-api,:core-rest,:store-implementation,:proxy-store - MODULES=:flink-library,:hdfs-library,:spark,:spark-library From b669b64b2b3bfebddd0c9bebe6630208ee7e14ec Mon Sep 17 00:00:00 2001 From: m607123 Date: Wed, 27 Sep 2017 13:08:05 +0100 Subject: [PATCH 12/72] Aggregate refactored --- .../gchq/gaffer/operation/{ => impl}/function/Aggregate.java | 2 +- .../store/operation/handler/function/AggregateHandler.java | 2 +- .../store/operation/handler/function/AggregateHandlerTest.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename core/operation/src/main/java/uk/gov/gchq/gaffer/operation/{ => impl}/function/Aggregate.java (98%) diff --git a/core/operation/src/main/java/uk/gov/gchq/gaffer/operation/function/Aggregate.java b/core/operation/src/main/java/uk/gov/gchq/gaffer/operation/impl/function/Aggregate.java similarity index 98% rename from core/operation/src/main/java/uk/gov/gchq/gaffer/operation/function/Aggregate.java rename to core/operation/src/main/java/uk/gov/gchq/gaffer/operation/impl/function/Aggregate.java index f82d039503e..0959f0be13d 100644 --- a/core/operation/src/main/java/uk/gov/gchq/gaffer/operation/function/Aggregate.java +++ b/core/operation/src/main/java/uk/gov/gchq/gaffer/operation/impl/function/Aggregate.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package uk.gov.gchq.gaffer.operation.function; +package uk.gov.gchq.gaffer.operation.impl.function; import com.fasterxml.jackson.core.type.TypeReference; import org.apache.commons.lang3.exception.CloneFailedException; diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/operation/handler/function/AggregateHandler.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/operation/handler/function/AggregateHandler.java index 47307ce6f5c..9d01e2cfc13 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/operation/handler/function/AggregateHandler.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/operation/handler/function/AggregateHandler.java @@ -19,7 +19,7 @@ import uk.gov.gchq.gaffer.data.elementdefinition.view.View; import uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition; import uk.gov.gchq.gaffer.operation.OperationException; -import uk.gov.gchq.gaffer.operation.function.Aggregate; +import uk.gov.gchq.gaffer.operation.impl.function.Aggregate; import uk.gov.gchq.gaffer.operation.util.AggregatePair; import uk.gov.gchq.gaffer.store.Context; import uk.gov.gchq.gaffer.store.Store; diff --git a/core/store/src/test/java/uk/gov/gchq/gaffer/store/operation/handler/function/AggregateHandlerTest.java b/core/store/src/test/java/uk/gov/gchq/gaffer/store/operation/handler/function/AggregateHandlerTest.java index dea1326d3f1..eb7d0fa8bbd 100644 --- a/core/store/src/test/java/uk/gov/gchq/gaffer/store/operation/handler/function/AggregateHandlerTest.java +++ b/core/store/src/test/java/uk/gov/gchq/gaffer/store/operation/handler/function/AggregateHandlerTest.java @@ -24,7 +24,7 @@ import uk.gov.gchq.gaffer.data.element.Entity; import uk.gov.gchq.gaffer.data.element.function.ElementAggregator; import uk.gov.gchq.gaffer.operation.OperationException; -import uk.gov.gchq.gaffer.operation.function.Aggregate; +import uk.gov.gchq.gaffer.operation.impl.function.Aggregate; import uk.gov.gchq.gaffer.operation.util.AggregatePair; import uk.gov.gchq.gaffer.store.Context; import uk.gov.gchq.gaffer.store.Store; From 889c69ecbbc3fb5015a0ba608a02b33f9a2ac6cb Mon Sep 17 00:00:00 2001 From: m607123 Date: Wed, 27 Sep 2017 13:42:26 +0100 Subject: [PATCH 13/72] Fixed residual incorrect package references --- core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java | 2 +- .../store/src/test/java/uk/gov/gchq/gaffer/store/StoreTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java index fa004d4de45..893c966c40c 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java @@ -39,7 +39,6 @@ import uk.gov.gchq.gaffer.operation.OperationChain; import uk.gov.gchq.gaffer.operation.OperationChainDAO; import uk.gov.gchq.gaffer.operation.OperationException; -import uk.gov.gchq.gaffer.operation.function.Aggregate; import uk.gov.gchq.gaffer.operation.impl.Count; import uk.gov.gchq.gaffer.operation.impl.CountGroups; import uk.gov.gchq.gaffer.operation.impl.DiscardOutput; @@ -53,6 +52,7 @@ import uk.gov.gchq.gaffer.operation.impl.export.resultcache.ExportToGafferResultCache; import uk.gov.gchq.gaffer.operation.impl.export.set.ExportToSet; import uk.gov.gchq.gaffer.operation.impl.export.set.GetSetExport; +import uk.gov.gchq.gaffer.operation.impl.function.Aggregate; import uk.gov.gchq.gaffer.operation.impl.function.Filter; import uk.gov.gchq.gaffer.operation.impl.function.Transform; import uk.gov.gchq.gaffer.operation.impl.generate.GenerateElements; diff --git a/core/store/src/test/java/uk/gov/gchq/gaffer/store/StoreTest.java b/core/store/src/test/java/uk/gov/gchq/gaffer/store/StoreTest.java index 85c2c9ca913..39b00d61d03 100644 --- a/core/store/src/test/java/uk/gov/gchq/gaffer/store/StoreTest.java +++ b/core/store/src/test/java/uk/gov/gchq/gaffer/store/StoreTest.java @@ -49,7 +49,7 @@ import uk.gov.gchq.gaffer.operation.OperationChain; import uk.gov.gchq.gaffer.operation.OperationChainDAO; import uk.gov.gchq.gaffer.operation.OperationException; -import uk.gov.gchq.gaffer.operation.function.Aggregate; +import uk.gov.gchq.gaffer.operation.impl.function.Aggregate; import uk.gov.gchq.gaffer.operation.impl.Count; import uk.gov.gchq.gaffer.operation.impl.CountGroups; import uk.gov.gchq.gaffer.operation.impl.DiscardOutput; From 6d5bb85b8cce3b9c8e0da22d51184477ea7b9696 Mon Sep 17 00:00:00 2001 From: p013570 Date: Wed, 27 Sep 2017 18:00:02 +0100 Subject: [PATCH 14/72] gh-1349 - merged 2 travis build modules together gh-1297 - test fixes gh-1297 - test fixes gh-1297 - AddGraphHandlerTest updates --- .travis.yml | 3 +- .../export/graph/handler/GraphDelegate.java | 1 + .../impl/FederatedAddGraphHandlerTest.java | 92 +++++++++---------- 3 files changed, 46 insertions(+), 50 deletions(-) diff --git a/.travis.yml b/.travis.yml index 40e4a093ce1..dc85ff0a1e7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,8 +18,7 @@ env: - MODULES=:hbase-store,:hbase-rest - MODULES=:parquet-store,:parquet-rest - MODULES=:integration-test,:federated-store,:map-store,:map-rest - - MODULES=:example,:basic,:basic-model,:basic-rest,:road-traffic,:road-traffic-model,:road-traffic-generators,:road-traffic-rest,:road-traffic-demo,:federated-demo - - MODULES=:rest-api,:core-rest,:store-implementation,:proxy-store + - MODULES=:rest-api,:core-rest,:store-implementation,:proxy-store,:example,:basic,:basic-model,:basic-rest,:road-traffic,:road-traffic-model,:road-traffic-generators,:road-traffic-rest,:road-traffic-demo,:federated-demo - MODULES=:flink-library,:hdfs-library,:spark,:spark-library - MODULES=:library,:time-library - MODULES=:cache-library,:sketches-library,:bitmap-library,:hazelcast-cache-service,:jcs-cache-service diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java index 1d46b3f3b08..cd82681a9ee 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java @@ -158,6 +158,7 @@ private static Graph createGraphWithoutLibrary(final Store store, final String g .addSchema(resolveSchema) .storeProperties(resolveProperties) .addToLibrary(false) + .store(store) .build(); } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java index 62bac59bc6d..70f61bf76a1 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java @@ -16,6 +16,7 @@ package uk.gov.gchq.gaffer.federatedstore.operation.handler.impl; +import org.junit.Before; import org.junit.Test; import org.mockito.BDDMockito; import org.mockito.Mockito; @@ -55,28 +56,35 @@ public class FederatedAddGraphHandlerTest { private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; private static final String FEDERATEDSTORE_CLASS_STRING = "uk.gov.gchq.gaffer.federatedstore.FederatedStore"; private static final String TEST_USER_ID = "testUser"; + private FederatedStore store; + private StoreProperties federatedStoreProperties; + + @Before + public void setUp() { + this.store = new FederatedStore(); + federatedStoreProperties = new StoreProperties(); + federatedStoreProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); + federatedStoreProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); + } @Test public void shouldAddGraph() throws Exception { - - FederatedStore store = new FederatedStore(); - Schema expectedSchema = new Schema.Builder().build(); - StoreProperties storeProperties = new StoreProperties(); - storeProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); - storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); + StoreProperties graphStoreProperties = new StoreProperties(); + graphStoreProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); + graphStoreProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); assertEquals(0, store.getGraphs(null).size()); - store.initialise(FEDERATEDSTORE_GRAPH_ID, new Schema(), storeProperties); + store.initialise(FEDERATEDSTORE_GRAPH_ID, new Schema(), federatedStoreProperties); FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); federatedAddGraphHandler.doOperation( new AddGraph.Builder() .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) - .storeProperties(storeProperties) + .storeProperties(graphStoreProperties) .build(), new Context(new User(TEST_USER_ID)), store); @@ -92,7 +100,7 @@ public void shouldAddGraph() throws Exception { new AddGraph.Builder() .graphId(EXPECTED_GRAPH_ID_2) .schema(expectedSchema) - .storeProperties(storeProperties) + .storeProperties(graphStoreProperties) .build(), new Context(new User(TEST_USER_ID)), store); @@ -108,26 +116,22 @@ public void shouldAddGraph() throws Exception { @Test public void shouldAddGraphUsingLibrary() throws Exception { - - FederatedStore store = new FederatedStore(); - Schema expectedSchema = new Schema.Builder().build(); - StoreProperties storeProperties = new StoreProperties(); - storeProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); - storeProperties.set(StoreProperties.STORE_PROPERTIES_CLASS, "uk.gov.gchq.gaffer.store.StoreProperties"); - storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); + StoreProperties graphStoreProperties = new StoreProperties(); + graphStoreProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); + graphStoreProperties.set(StoreProperties.STORE_PROPERTIES_CLASS, "uk.gov.gchq.gaffer.store.StoreProperties"); assertEquals(0, store.getGraphs(null).size()); - store.initialise(FEDERATEDSTORE_GRAPH_ID, new Schema(), storeProperties); + store.initialise(FEDERATEDSTORE_GRAPH_ID, new Schema(), federatedStoreProperties); FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); federatedAddGraphHandler.doOperation( new AddGraph.Builder() .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) - .storeProperties(storeProperties) + .storeProperties(graphStoreProperties) .build(), new Context(new User(TEST_USER_ID)), store); @@ -141,7 +145,7 @@ public void shouldAddGraphUsingLibrary() throws Exception { final GraphLibrary mock = Mockito.mock(GraphLibrary.class); final String graphId2 = EXPECTED_GRAPH_ID_2; - BDDMockito.given(mock.get(graphId2)).willReturn(new Pair<>(expectedSchema, storeProperties)); + BDDMockito.given(mock.get(graphId2)).willReturn(new Pair<>(expectedSchema, graphStoreProperties)); BDDMockito.given(mock.exists(graphId2)).willReturn(true); store.setGraphLibrary(mock); @@ -165,18 +169,19 @@ public void shouldAddGraphUsingLibrary() throws Exception { @Test public void shouldNotOverwriteGraph() throws Exception { - - FederatedStore store = new FederatedStore(); - Schema expectedSchema = new Schema.Builder().build(); - StoreProperties storeProperties = new StoreProperties(); - storeProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); - storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); + StoreProperties graphStoreProperties = new StoreProperties(); + graphStoreProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); + <<<<<<>>>>>>f32d04ea8...gh - 1297 - test fixes assertEquals(0, store.getGraphs(null).size()); - store.initialise(FEDERATEDSTORE_GRAPH_ID, new Schema(), storeProperties); + store.initialise(FEDERATEDSTORE_GRAPH_ID, new Schema(), federatedStoreProperties); FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); @@ -184,7 +189,7 @@ public void shouldNotOverwriteGraph() throws Exception { new AddGraph.Builder() .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) - .storeProperties(storeProperties) + .storeProperties(graphStoreProperties) .build(), new Context(new User(TEST_USER_ID)), store); @@ -194,7 +199,7 @@ public void shouldNotOverwriteGraph() throws Exception { new AddGraph.Builder() .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) - .storeProperties(storeProperties) + .storeProperties(graphStoreProperties) .build(), new Context(new User(TEST_USER_ID)), store); @@ -206,22 +211,18 @@ public void shouldNotOverwriteGraph() throws Exception { @Test public void shouldAddGraphIDOnlyWithAuths() throws Exception { + federatedStoreProperties.set(GAFFER_FEDERATEDSTORE_CUSTOM_PROPERTIES_AUTHS, "auth1,auth2"); - - final StoreProperties federatedProperties = new StoreProperties(); - federatedProperties.set(GAFFER_FEDERATEDSTORE_CUSTOM_PROPERTIES_AUTHS, "auth1,auth2"); - federatedProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); - FederatedStore store = new FederatedStore(); + store.initialise(FEDERATEDSTORE_GRAPH_ID, null, federatedStoreProperties); Schema expectedSchema = new Schema.Builder().build(); StoreProperties graphStoreProperties = new StoreProperties(); graphStoreProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); + graphStoreProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); assertEquals(0, store.getGraphs(null).size()); - store.initialise(FEDERATEDSTORE_GRAPH_ID, null, federatedProperties); - FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); try { @@ -236,11 +237,11 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { fail("Exception not thrown"); } catch (OperationException e) { assertEquals("User is limited to only using parentPropertiesId from the graphLibrary," + - " but found storeProperties:{gaffer.store.class=uk.gov.gchq.gaffer.federatedstore.FederatedStore}", + " but found storeProperties:{gaffer.store.class=uk.gov.gchq.gaffer.federatedstore.FederatedStore," + + " gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService}", e.getMessage()); } - federatedAddGraphHandler.doOperation( new AddGraph.Builder() .graphId(EXPECTED_GRAPH_ID) @@ -266,27 +267,22 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { */ @Test public void shouldAddGraphWithAuthsAndAddingUser() throws Exception { - StoreProperties fedStoreProperties = new StoreProperties(); - fedStoreProperties.setStoreClass(FederatedStore.class); - fedStoreProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); - - FederatedStore store = new FederatedStore(); - store.initialise(FEDERATEDSTORE_GRAPH_ID, null, fedStoreProperties); + store.initialise(FEDERATEDSTORE_GRAPH_ID, null, federatedStoreProperties); Schema expectedSchema = new Schema.Builder().build(); assertEquals(0, store.getGraphs(null).size()); - AccumuloProperties storeProperties = new AccumuloProperties(); - storeProperties.setStorePropertiesClass(AccumuloProperties.class); - storeProperties.setStoreClass(MockAccumuloStore.class); - storeProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); + AccumuloProperties graphStoreProperties = new AccumuloProperties(); + graphStoreProperties.setStorePropertiesClass(AccumuloProperties.class); + graphStoreProperties.setStoreClass(MockAccumuloStore.class); + graphStoreProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); new FederatedAddGraphHandler().doOperation( new AddGraph.Builder() .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) - .storeProperties(storeProperties) + .storeProperties(graphStoreProperties) .graphAuths("testAuth") .build(), new Context(new User(TEST_USER_ID)), From 9d4eaae170d54d812b78723a363933fc518a9558 Mon Sep 17 00:00:00 2001 From: m55624 Date: Mon, 2 Oct 2017 10:13:02 +0100 Subject: [PATCH 15/72] gh-1297 - final test fixes --- .../operation/export/graph/handler/GraphDelegate.java | 1 - .../handler/impl/FederatedAddGraphHandlerTest.java | 6 ++---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java index cd82681a9ee..1d46b3f3b08 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java @@ -158,7 +158,6 @@ private static Graph createGraphWithoutLibrary(final Store store, final String g .addSchema(resolveSchema) .storeProperties(resolveProperties) .addToLibrary(false) - .store(store) .build(); } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java index 70f61bf76a1..6142bf5175e 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java @@ -121,6 +121,7 @@ public void shouldAddGraphUsingLibrary() throws Exception { StoreProperties graphStoreProperties = new StoreProperties(); graphStoreProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); graphStoreProperties.set(StoreProperties.STORE_PROPERTIES_CLASS, "uk.gov.gchq.gaffer.store.StoreProperties"); + graphStoreProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); assertEquals(0, store.getGraphs(null).size()); @@ -173,11 +174,8 @@ public void shouldNotOverwriteGraph() throws Exception { StoreProperties graphStoreProperties = new StoreProperties(); graphStoreProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); - <<<<<<>>>>>>f32d04ea8...gh - 1297 - test fixes + graphStoreProperties.set(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); assertEquals(0, store.getGraphs(null).size()); From cfcd2af8c64fe151ee1acd13eeebeb405ffeda08 Mon Sep 17 00:00:00 2001 From: m55624 Date: Mon, 2 Oct 2017 11:27:08 +0100 Subject: [PATCH 16/72] gh-1297 - checkstyle fixes --- .../uk/gov/gchq/gaffer/federatedstore/FederatedStore.java | 7 ++++--- .../operation/handler/impl/FederatedAddGraphHandler.java | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 912ef0139b1..7748f63005b 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -109,11 +109,11 @@ public class FederatedStore extends Store { * Initialise this FederatedStore with any sub-graphs defined within the * properties. * - * @param graphId the graphId to label this FederatedStore. + * @param graphId the graphId to label this FederatedStore * @param unused unused * @param properties properties to initialise this FederatedStore with, can - * contain details on graphs to add to scope. - * @throws StoreException exception + * contain details on graphs to add to scope + * @throws StoreException if no cache has been set */ @Override public void initialise(final String graphId, final Schema unused, final StoreProperties properties) throws StoreException { @@ -170,6 +170,7 @@ public static OP updateOperationForGraph(final OP operati * graphs via the {@link AddGraph} operation. * * @param graphs the graph to add + * @throws StoreException if no cache has been set */ public void addGraphs(final Graph... graphs) throws StoreException { if (cacheService == null) { diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java index 8811851b9ad..25a88dcd2ff 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java @@ -58,7 +58,7 @@ public Void doOperation(final AddGraph operation, final Context context, final S try { ((FederatedStore) store).addGraphs(graph); - } catch (StoreException e) { + } catch (final StoreException e) { throw new OperationException(e.getMessage()); } return null; From ddabdf72f0d2a6c1410fe2f4bcd4ad3876e38a8f Mon Sep 17 00:00:00 2001 From: m55624 Date: Tue, 3 Oct 2017 13:35:38 +0100 Subject: [PATCH 17/72] gh-1297 - FederatedStoreCache added --- .../java/uk/gov/gchq/gaffer/store/Store.java | 2 +- .../gaffer/federatedstore/FederatedStore.java | 47 ++++++------ .../federatedstore/FederatedStoreCache.java | 72 +++++++++++++++++++ .../federatedstore/FederatedStoreTest.java | 22 ++++-- 4 files changed, 118 insertions(+), 25 deletions(-) create mode 100644 store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java index 22f3dddd32b..e0f37b4834c 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java @@ -798,7 +798,7 @@ private void addConfiguredOperationHandlers() { } } - private void startCacheServiceLoader(final StoreProperties properties) { + protected void startCacheServiceLoader(final StoreProperties properties) { CacheServiceLoader.initialise(properties.getProperties()); } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 048a74b0649..c33db1ed63f 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -22,7 +22,7 @@ import org.apache.commons.lang3.StringUtils; import uk.gov.gchq.gaffer.cache.CacheServiceLoader; -import uk.gov.gchq.gaffer.cache.ICacheService; +import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; import uk.gov.gchq.gaffer.cache.util.CacheProperties; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; @@ -99,8 +99,7 @@ public class FederatedStore extends Store { private static final GraphConfigEnum PROPERTIES = GraphConfigEnum.properties; private static final LocationEnum ID = LocationEnum.id; private static final LocationEnum FILE = LocationEnum.file; - private static final String CACHE_SERVICE_NAME = "federatedStoreGraphs"; - private ICacheService cacheService; + FederatedStoreCache federatedStoreCache = new FederatedStoreCache(); private FederatedGraphStorage graphStorage = new FederatedGraphStorage(); private Set customPropertiesAuths; @@ -122,7 +121,6 @@ public void initialise(final String graphId, final Schema unused, final StorePro + CacheProperties.CACHE_SERVICE_CLASS + " has been set in the Store Properties"); } super.initialise(graphId, new Schema(), properties); - setCacheService(); loadCustomPropertiesAuths(); loadGraphs(); } @@ -175,7 +173,7 @@ public static OP updateOperationForGraph(final OP operati * @throws StoreException if no cache has been set */ public void addGraphs(final Set graphAuths, final String addingUserId, final Graph... graphs) throws StoreException { - if (cacheService == null) { + if (federatedStoreCache.getCache() == null) { throw new StoreException("No cache has been set, please initialise the FederatedStore instance"); } FederatedAccess access = new FederatedAccess(graphAuths, addingUserId); @@ -193,7 +191,7 @@ public void addGraphs(final Set graphAuths, final String addingUserId, f * @param graphId to be removed from scope */ public void remove(final String graphId) { - cacheService.removeFromCache(CACHE_SERVICE_NAME, graphId); + federatedStoreCache.deleteFromCache(graphId); graphStorage.remove(graphId); } @@ -335,15 +333,23 @@ private void loadCustomPropertiesAuths() { private void loadGraphs() throws StoreException { final Set graphIds = getGraphIds(); for (final String graphId : graphIds) { - final Builder builder = new Builder().config(new GraphConfig.Builder() - .graphId(graphId) - .library(getGraphLibrary()) - .build()); + if (federatedStoreCache.getAllGraphIds().contains(graphId)) { + try { + Graph graph = federatedStoreCache.getFromCache(graphId); + addGraphs(resolveAuths(graphId), null, graph); + } catch (CacheOperationException e) { + throw new RuntimeException(e); + } + } else { + final Builder builder = new Builder().config(new GraphConfig.Builder() + .graphId(graphId) + .library(getGraphLibrary()) + .build()); - resolveConfiguration(graphId, builder); + resolveConfiguration(graphId, builder); - final Set auths = resolveAuths(graphId); - addGraphs(auths, null, builder); + addGraphs(resolveAuths(graphId), null, builder); + } } } @@ -455,7 +461,7 @@ private void _add(final Graph newGraph, final FederatedAccess access) { graphStorage.put(newGraph, access); final String graphId = newGraph.getGraphId(); try { - cacheService.putSafeInCache(CACHE_SERVICE_NAME, graphId, newGraph); + federatedStoreCache.addSafeToCache(newGraph); } catch (final OverwritingException e) { throw new OverwritingException((String.format("User is attempting to overwrite a graph within the cacheService. GraphId: %s", graphId))); } catch (final Exception e) { @@ -468,13 +474,14 @@ private void _add(final Graph newGraph, final FederatedAccess access) { } @Override - public Schema getSchema() { - return graphStorage.getMergedSchema(); + protected void startCacheServiceLoader(final StoreProperties properties) { + if (federatedStoreCache.getCache() == null) { + CacheServiceLoader.initialise(properties.getProperties()); + } } - private void setCacheService() { - if (CacheServiceLoader.getService() != null) { - this.cacheService = CacheServiceLoader.getService(); - } + @Override + public Schema getSchema() { + return graphStorage.getMergedSchema(); } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java new file mode 100644 index 00000000000..0d41700f222 --- /dev/null +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java @@ -0,0 +1,72 @@ +/* + * Copyright 2017 Crown Copyright + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.gchq.gaffer.federatedstore; + +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; +import uk.gov.gchq.gaffer.cache.ICache; +import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; +import uk.gov.gchq.gaffer.graph.Graph; + +import java.util.Set; + +/** + * Wrapper around the {@link CacheServiceLoader} to provide an + * interface for handling the {@link uk.gov.gchq.gaffer.graph.Graph}s + * within a {@link uk.gov.gchq.gaffer.federatedstore.FederatedStore}. + */ +public class FederatedStoreCache { + + private static final String CACHE_SERVICE_NAME = "federatedStoreGraphs"; + + public ICache getCache() { + if (CacheServiceLoader.getService() != null) { + return CacheServiceLoader.getService().getCache(CACHE_SERVICE_NAME); + } else { + return null; + } + } + + public Set getAllGraphIds() { + return CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME); + } + + public void addToCache(Graph graph) throws CacheOperationException { + CacheServiceLoader.getService().putInCache(CACHE_SERVICE_NAME, graph.getGraphId(), graph); + } + + public void addSafeToCache(Graph graph) throws CacheOperationException { + CacheServiceLoader.getService().putSafeInCache(CACHE_SERVICE_NAME, graph.getGraphId(), graph); + } + + public Graph getFromCache(String graphId) throws CacheOperationException { + final Graph graph = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); + + if (null != graph) { + return graph; + } else { + throw new CacheOperationException("No graph found in the cache with graphId: " + graphId); + } + } + + public void deleteFromCache(String graphId) { + CacheServiceLoader.getService().removeFromCache(CACHE_SERVICE_NAME, graphId); + } + + public void clearCache() throws CacheOperationException { + CacheServiceLoader.getService().clearCache(CACHE_SERVICE_NAME); + } +} diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index 19bbb81e16f..eae94a1a2fd 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -86,6 +86,7 @@ public class FederatedStoreTest { private static final String ALL_USERS = FederatedStoreUser.ALL_USERS; private static final HashSet GRAPH_AUTHS = Sets.newHashSet(ALL_USERS); private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; + private static final String CACHE_SERVICE_NAME = "federatedStoreGraphs"; private static User authUser; private static User testUser; @@ -121,7 +122,7 @@ public void shouldThrowExceptionWithoutInitialisation() { // When / Then try { - store.addGraphs(null,TEST_USER,graphToAdd); + store.addGraphs(null, TEST_USER, graphToAdd); fail("Exception expected"); } catch (final StoreException e) { assertTrue(e.getMessage().contains("No cache has been set")); @@ -153,11 +154,15 @@ public void shouldAddGraphsToCache() throws StoreException { .build(); store.addGraphs(null, TEST_USER, graphToAdd); - - assertEquals(store.getGraphs(testUser, ACC_ID_1).size(), 1); + assertEquals(1, store.getGraphs(testUser, ACC_ID_1).size()); Collection storeGraphs = store.getGraphs(testUser, null); + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1)); assertTrue(storeGraphs.contains(graphToAdd)); + + store = new FederatedStore(); + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1)); } @Test @@ -175,7 +180,16 @@ public void shouldAddMultipleGraphsToCache() throws StoreException { store.addGraphs(null, TEST_USER, graphsToAdd.toArray(new Graph[graphsToAdd.size()])); - assertEquals(store.getGraphs(testUser, null).size(), 10); + for (int i = 0; i < 10; i++) { + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1 + i)); + } + + store = new FederatedStore(); + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + + for (int i = 0; i < 10; i++) { + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1 + i)); + } } @Test From 516915ea49ed3565c74f2ba1f23afbb3335b1c0e Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Tue, 3 Oct 2017 13:46:26 +0100 Subject: [PATCH 18/72] gh-1297 Adding GraphSerialisable for FederatedStore cache. --- .../java/uk/gov/gchq/gaffer/graph/Graph.java | 4 + .../gchq/gaffer/graph/GraphSerialisable.java | 142 ++++++++++++++++++ .../gaffer/graph/GraphSerialisableTest.java | 79 ++++++++++ 3 files changed, 225 insertions(+) create mode 100644 core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java create mode 100644 core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphSerialisableTest.java diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java index 5e0962292fc..a18e8bbc006 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java @@ -314,6 +314,10 @@ public GraphLibrary getGraphLibrary() { return store.getGraphLibrary(); } + protected GraphConfig getConfig() { + return config; + } + @FunctionalInterface private interface StoreExecuter { O execute(final OperationChain operationChain, final User user) throws OperationException; diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java new file mode 100644 index 00000000000..937d5831015 --- /dev/null +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java @@ -0,0 +1,142 @@ +/* + * Copyright 2017 Crown Copyright + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.gchq.gaffer.graph; + +import org.apache.commons.lang3.builder.EqualsBuilder; + +import uk.gov.gchq.gaffer.commonutil.ToStringBuilder; +import uk.gov.gchq.gaffer.exception.SerialisationException; +import uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser; +import uk.gov.gchq.gaffer.store.StoreProperties; +import uk.gov.gchq.gaffer.store.schema.Schema; + +import java.io.Serializable; +import java.util.Properties; + +/** + * A Serialisable object which holds the contents for creating Graphs. + * Does not store all the graph data, this only is used to recreate the graph + * after serialisation from the graph elements. + * + * @see GraphSerialisable.Builder + */ +public final class GraphSerialisable implements Serializable { + private static final long serialVersionUID = 2684203367656032583L; + private byte[] schema; + private Properties properties; + private byte[] config; + + public GraphSerialisable() { + } + + private GraphSerialisable(final GraphConfig config, final Schema schema, final Properties properties) { + try { + this.schema = JSONSerialiser.serialise(schema, true); + this.config = JSONSerialiser.serialise(config, true); + } catch (SerialisationException e) { + throw new IllegalArgumentException("Unable to serialise given parameter", e); + } + this.properties = properties; + } + + /** + * @return returns a new {@link Graph} built from the contents of a this + * class. + */ + public Graph buildGraph() { + return new Graph.Builder() + .addSchema(schema) + .addStoreProperties(new StoreProperties(properties)) + .config(config) + .build(); + } + + @Override + public boolean equals(final Object obj) { + final Boolean rtn; + if (null == obj || !(obj instanceof GraphSerialisable)) { + rtn = false; + } else { + final GraphSerialisable that = (GraphSerialisable) obj; + rtn = new EqualsBuilder() + .append(this.getConfig(), that.getConfig()) + .append(this.getSchema(), that.getSchema()) + .append(this.getProperties(), that.getProperties()) + .build(); + } + return rtn; + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("config", new String(this.getConfig())) + .append("schema", new String(this.getSchema())) + .append("properties", this.getProperties()) + .build(); + } + + public byte[] getSchema() { + return schema; + } + + public Properties getProperties() { + return properties; + } + + public byte[] getConfig() { + return config; + } + + public static class Builder { + + private Schema schema; + private Properties properties; + private GraphConfig config; + + public Builder setSchema(final Schema schema) { + this.schema = schema; + return this; + } + + public Builder setProperties(final Properties properties) { + this.properties = properties; + return this; + } + + public Builder setConfig(final GraphConfig config) { + this.config = config; + return this; + } + + public Builder graph(Graph graph) { + schema = graph.getSchema(); + properties = graph.getStoreProperties().getProperties(); + config = graph.getConfig(); + return _self(); + } + + private Builder _self() { + return this; + } + + public GraphSerialisable build() { + return new GraphSerialisable(config, schema, properties); + } + } + +} diff --git a/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphSerialisableTest.java b/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphSerialisableTest.java new file mode 100644 index 00000000000..b00b60f426b --- /dev/null +++ b/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphSerialisableTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 2017 Crown Copyright + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.gchq.gaffer.graph; + +import org.junit.Before; +import org.junit.Test; + +import uk.gov.gchq.gaffer.data.elementdefinition.view.View; +import uk.gov.gchq.gaffer.integration.store.TestStore; +import uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser; +import uk.gov.gchq.gaffer.store.StoreProperties; +import uk.gov.gchq.gaffer.store.schema.Schema; +import uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition; + +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +public class GraphSerialisableTest { + + private GraphConfig config; + private Schema schema; + private Properties properties; + private GraphSerialisable expected; + + @Before + public void setUp() throws Exception { + config = new GraphConfig.Builder() + .graphId("testGraphId") + .view(new View.Builder() + .entity("e1") + .build()) + .build(); + schema = new Schema.Builder() + .entity("e1", new SchemaEntityDefinition.Builder() + .vertex("string") + .build()) + .type("string", String.class) + .build(); + final StoreProperties storeProperties = new StoreProperties(); + storeProperties.setStoreClass(TestStore.class); + properties = storeProperties.getProperties(); + expected = new GraphSerialisable.Builder() + .setSchema(schema) + .setProperties(properties) + .setConfig(config) + .build(); + } + + @Test + public void shouldSerialiseAndDeserialise() throws Exception { + final JavaSerialiser javaSerialiser = new JavaSerialiser(); + final byte[] serialise = javaSerialiser.serialise(expected); + final GraphSerialisable result = (GraphSerialisable) javaSerialiser.deserialise(serialise); + assertEquals(expected, result); + } + + @Test + public void shouldConsumeGraph() throws Exception { + final Graph graph = new Graph.Builder().addSchema(schema).addStoreProperties(new StoreProperties(properties)).config(config).build(); + final GraphSerialisable result = new GraphSerialisable.Builder().graph(graph).build(); + assertEquals(expected, result); + + } +} \ No newline at end of file From b9a0d336a5c1e1e28931389965f078f27e5567bf Mon Sep 17 00:00:00 2001 From: m55624 Date: Tue, 3 Oct 2017 14:14:12 +0100 Subject: [PATCH 19/72] gh-1297 - GraphSerialisable used in FederatedStoreCache --- .../federatedstore/FederatedStoreCache.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java index 0d41700f222..40f6edac448 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java @@ -20,6 +20,8 @@ import uk.gov.gchq.gaffer.cache.ICache; import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; import uk.gov.gchq.gaffer.graph.Graph; +import uk.gov.gchq.gaffer.graph.GraphSerialisable; +import uk.gov.gchq.gaffer.store.StoreProperties; import java.util.Set; @@ -45,15 +47,23 @@ public Set getAllGraphIds() { } public void addToCache(Graph graph) throws CacheOperationException { - CacheServiceLoader.getService().putInCache(CACHE_SERVICE_NAME, graph.getGraphId(), graph); + GraphSerialisable graphSerialisable = new GraphSerialisable.Builder().graph(graph).build(); + CacheServiceLoader.getService().putInCache(CACHE_SERVICE_NAME, graph.getGraphId(), graphSerialisable); } public void addSafeToCache(Graph graph) throws CacheOperationException { - CacheServiceLoader.getService().putSafeInCache(CACHE_SERVICE_NAME, graph.getGraphId(), graph); + GraphSerialisable graphSerialisable = new GraphSerialisable.Builder().graph(graph).build(); + CacheServiceLoader.getService().putSafeInCache(CACHE_SERVICE_NAME, graph.getGraphId(), graphSerialisable); } public Graph getFromCache(String graphId) throws CacheOperationException { - final Graph graph = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); + final GraphSerialisable graphSerialisable = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); + + Graph graph = new Graph.Builder() + .config(graphSerialisable.getConfig()) + .addStoreProperties(new StoreProperties(graphSerialisable.getProperties())) + .addSchema(graphSerialisable.getSchema()) + .build(); if (null != graph) { return graph; From 3256afaa47a7cd99e338e5d6f6ba4fcc00d8e99e Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Tue, 3 Oct 2017 14:30:36 +0100 Subject: [PATCH 20/72] gh-1297 tostring update --- .../java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java index 937d5831015..58218c2fb00 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java @@ -18,6 +18,7 @@ import org.apache.commons.lang3.builder.EqualsBuilder; +import uk.gov.gchq.gaffer.commonutil.StringUtil; import uk.gov.gchq.gaffer.commonutil.ToStringBuilder; import uk.gov.gchq.gaffer.exception.SerialisationException; import uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser; @@ -84,8 +85,8 @@ public boolean equals(final Object obj) { @Override public String toString() { return new ToStringBuilder(this) - .append("config", new String(this.getConfig())) - .append("schema", new String(this.getSchema())) + .append("config", StringUtil.toString(this.getConfig())) + .append("schema", StringUtil.toString(this.getSchema())) .append("properties", this.getProperties()) .build(); } From 0e0bcb59e81c88f8f5ff4c1a9d2cb94222bb62dd Mon Sep 17 00:00:00 2001 From: m55624 Date: Tue, 3 Oct 2017 15:26:48 +0100 Subject: [PATCH 21/72] FederatedStoreTest fixes --- .../gaffer/federatedstore/FederatedStore.java | 14 +- .../federatedstore/FederatedStoreTest.java | 158 ++++++++---------- 2 files changed, 77 insertions(+), 95 deletions(-) diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 5dd051fc103..3f158c018a8 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -459,12 +459,14 @@ private Set getGraphIds() { private void _add(final Graph newGraph, final FederatedAccess access) { graphStorage.put(newGraph, access); final String graphId = newGraph.getGraphId(); - try { - federatedStoreCache.addSafeToCache(newGraph); - } catch (final OverwritingException e) { - throw new OverwritingException((String.format("User is attempting to overwrite a graph within the cacheService. GraphId: %s", graphId))); - } catch (final Exception e) { - throw new RuntimeException(e); + if (!federatedStoreCache.getAllGraphIds().contains(graphId)) { + try { + federatedStoreCache.addToCache(newGraph); + } catch (final OverwritingException e) { + throw new OverwritingException((String.format("User is attempting to overwrite a graph within the cacheService. GraphId: %s", graphId))); + } catch (final Exception e) { + throw new RuntimeException(e); + } } if (null != getGraphLibrary()) { diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index eae94a1a2fd..576bdb10b25 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -27,7 +27,6 @@ import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; -import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.data.element.Edge; import uk.gov.gchq.gaffer.data.element.Element; import uk.gov.gchq.gaffer.data.element.Entity; @@ -585,17 +584,13 @@ public void shouldNotUseSchema() throws Exception { @Test public void shouldAddGraphFromLibrary() throws Exception { //Given - final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); - Mockito.when(mockLibrary.exists(MAP_ID_1)).thenReturn(true); - Mockito.when(mockLibrary.get(MAP_ID_1)).thenReturn( - new Pair<>( - new Schema.Builder() - .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build(), - StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)) - )); + final Schema schema = new Schema.Builder() + .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) + .build(); + final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); + graphLibrary.add(MAP_ID_1, schema, StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES))); - store.setGraphLibrary(mockLibrary); + store.setGraphLibrary(graphLibrary); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); //When @@ -615,17 +610,13 @@ public void shouldAddNamedGraphFromGraphIDKeyButDefinedInLibrary() throws Except //Given federatedProperties.setGraphIds(MAP_ID_1); federatedProperties.setGraphIds(MAP_ID_1); - final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); - Mockito.when(mockLibrary.exists(MAP_ID_1)).thenReturn(true); - Mockito.when(mockLibrary.get(MAP_ID_1)).thenReturn( - new Pair<>( - new Schema.Builder() - .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build(), - StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)) - )); + GraphLibrary library = new HashMapGraphLibrary(); + library.add(MAP_ID_1, new Schema.Builder() + .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) + .build(), + StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES))); - store.setGraphLibrary(mockLibrary); + store.setGraphLibrary(library); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); //Then @@ -656,17 +647,13 @@ public void shouldAddNamedGraphsFromGraphIDKeyButDefinedInLibraryAndProperties() federatedProperties.setGraphIds(MAP_ID_1 + ", " + ACC_ID_1); federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES); federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON); - final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); - Mockito.when(mockLibrary.exists(MAP_ID_1)).thenReturn(true); - Mockito.when(mockLibrary.get(MAP_ID_1)).thenReturn( - new Pair<>( - new Schema.Builder() - .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build(), - StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)) - )); + final Schema schema = new Schema.Builder() + .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) + .build(); + final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); + graphLibrary.add(MAP_ID_1, schema, StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES))); - store.setGraphLibrary(mockLibrary); + store.setGraphLibrary(graphLibrary); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); //Then @@ -679,15 +666,15 @@ public void shouldAddGraphWithPropertiesFromGraphLibrary() throws Exception { federatedProperties.setGraphIds(MAP_ID_1); federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1); federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); - final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); - Mockito.when(mockLibrary.getProperties(PROPS_ID_1)).thenReturn(StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES)); + final HashMapGraphLibrary library = new HashMapGraphLibrary(); + library.add(PROPS_ID_1, new Schema(), StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES)); - store.setGraphLibrary(mockLibrary); + store.setGraphLibrary(library); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); assertEquals(1, store.getGraphs(testUser, null).size()); - Mockito.verify(mockLibrary).getProperties(PROPS_ID_1); + assertTrue(library.getProperties(PROPS_ID_1).equals(StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES))); } @Test @@ -695,18 +682,17 @@ public void shouldAddGraphWithSchemaFromGraphLibrary() throws Exception { federatedProperties.setGraphIds(MAP_ID_1); federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1); - final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); - Mockito.when(mockLibrary.getSchema(SCHEMA_ID_1)) - .thenReturn(new Schema.Builder() - .json(StreamUtil.openStream(FederatedStore.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build()); + final HashMapGraphLibrary library = new HashMapGraphLibrary(); + final Schema schema = new Schema.Builder() + .json(StreamUtil.openStream(FederatedStore.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) + .build(); + library.add(SCHEMA_ID_1, schema, new StoreProperties()); - store.setGraphLibrary(mockLibrary); + store.setGraphLibrary(library); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); assertEquals(1, store.getGraphs(testUser, null).size()); - - Mockito.verify(mockLibrary).getSchema(SCHEMA_ID_1); + assertTrue(library.getSchema(SCHEMA_ID_1).toString().equals(schema.toString())); } @Test @@ -714,21 +700,20 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibrary() throws Excep federatedProperties.setGraphIds(MAP_ID_1); federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1); federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1); - final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); - Mockito.when(mockLibrary.getProperties(PROPS_ID_1)) - .thenReturn(StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES)); - Mockito.when(mockLibrary.getSchema(SCHEMA_ID_1)) - .thenReturn(new Schema.Builder() - .json(StreamUtil.openStream(FederatedStore.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build()); + final Schema schema = new Schema.Builder() + .id(SCHEMA_ID_1) + .json(StreamUtil.openStream(FederatedStore.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) + .build(); + final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); + graphLibrary.add(MAP_ID_1, schema, StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES)); - store.setGraphLibrary(mockLibrary); + store.setGraphLibrary(graphLibrary); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); assertEquals(1, store.getGraphs(testUser, null).size()); - Mockito.verify(mockLibrary).getSchema(SCHEMA_ID_1); - Mockito.verify(mockLibrary).getProperties(PROPS_ID_1); + assertTrue(graphLibrary.getSchema(SCHEMA_ID_1).toString().equals(schema.toString())); + assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES))); } @Test @@ -737,19 +722,20 @@ public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exce federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1); federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); - final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); final MapStoreProperties prop = new MapStoreProperties(); + prop.setId(PROPS_ID_1); final String unusualKey = "unusualKey"; prop.set(unusualKey, "value"); - Mockito.when(mockLibrary.getProperties(PROPS_ID_1)).thenReturn(prop); + final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); + graphLibrary.add("libraryGraphId", new Schema(), prop); - store.setGraphLibrary(mockLibrary); + store.setGraphLibrary(graphLibrary); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); assertEquals(1, store.getGraphs(testUser, null).size()); assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty(unusualKey) != null); - Mockito.verify(mockLibrary).getProperties(PROPS_ID_1); + assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(prop)); } @Test @@ -758,18 +744,19 @@ public void shouldAddGraphWithSchemaFromGraphLibraryOverridden() throws Exceptio federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1); - final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); - Mockito.when(mockLibrary.getSchema(SCHEMA_ID_1)).thenReturn(new Schema.Builder() + final Schema schema = new Schema.Builder() .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build()); + .build(); + final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); + graphLibrary.add(MAP_ID_1, schema, new StoreProperties()); - store.setGraphLibrary(mockLibrary); + store.setGraphLibrary(graphLibrary); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); assertEquals(1, store.getGraphs(testUser, null).size()); assertTrue(store.getGraphs(testUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity")); - Mockito.verify(mockLibrary).getSchema(SCHEMA_ID_1); + assertTrue(graphLibrary.getSchema(SCHEMA_ID_1).equals(schema)); } @Test @@ -779,24 +766,24 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibraryOverridden() th federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1); - final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); final MapStoreProperties prop = new MapStoreProperties(); final String unusualKey = "unusualKey"; prop.set(unusualKey, "value"); - Mockito.when(mockLibrary.getProperties(PROPS_ID_1)).thenReturn(prop); - Mockito.when(mockLibrary.getSchema(SCHEMA_ID_1)).thenReturn(new Schema.Builder() + final Schema schema = new Schema.Builder() .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build()); + .build(); + final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); + graphLibrary.add(MAP_ID_1, schema, prop); - store.setGraphLibrary(mockLibrary); + store.setGraphLibrary(graphLibrary); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); assertEquals(1, store.getGraphs(testUser, null).size()); assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty(unusualKey) != null); assertTrue(store.getGraphs(testUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity")); - Mockito.verify(mockLibrary).getProperties(PROPS_ID_1); - Mockito.verify(mockLibrary).getSchema(SCHEMA_ID_1); + assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(prop)); + assertTrue(graphLibrary.getSchema(SCHEMA_ID_1).equals(schema)); } @Test @@ -805,23 +792,17 @@ public void should() throws Exception { federatedProperties.setGraphIds(MAP_ID_1); federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); - final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); - Mockito.when(mockLibrary.get(MAP_ID_1)).thenReturn( - new Pair<>( - new Schema.Builder() - .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_EDGE_SCHEMA_JSON)) - .build(), - StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)) - )); + final Schema schema = new Schema.Builder() + .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_EDGE_SCHEMA_JSON)) + .build(); + final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); + graphLibrary.add(MAP_ID_1, schema, StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES))); + final MapStoreProperties prop = new MapStoreProperties(); final String unusualKey = "unusualKey"; prop.set(unusualKey, "value"); - Mockito.when(mockLibrary.getProperties(PROPS_ID_1)).thenReturn(prop); - Mockito.when(mockLibrary.getSchema(SCHEMA_ID_1)).thenReturn(new Schema.Builder() - .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build()); - store.setGraphLibrary(mockLibrary); + store.setGraphLibrary(graphLibrary); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); //No exception to be thrown. @@ -836,13 +817,12 @@ public void shouldNotAllowOverridingOfKnownGraphInLibrary() throws Exception { final Schema schema = new Builder() .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_EDGE_SCHEMA_JSON)) .build(); - + final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)); - final GraphLibrary lib = new HashMapGraphLibrary(); - lib.add(MAP_ID_1, schema, storeProperties); + graphLibrary.add(MAP_ID_1, schema, storeProperties); try { - store.setGraphLibrary(lib); + store.setGraphLibrary(graphLibrary); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); fail("exception should have been thrown"); } catch (final IllegalArgumentException e) { @@ -934,8 +914,8 @@ public void shouldAddGraphIdWithAuths() throws Exception { federatedProperties.setFalseSkipFailedExecution(); //Given - final HashMapGraphLibrary library = new HashMapGraphLibrary(); - library.add(MAP_ID_1, + final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); + graphLibrary.add(MAP_ID_1, new Schema.Builder() .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) .build(), @@ -944,7 +924,7 @@ public void shouldAddGraphIdWithAuths() throws Exception { final Graph fedGraph = new Graph.Builder() .config(new GraphConfig.Builder() .graphId(FEDERATED_STORE_ID) - .library(library) + .library(graphLibrary) .build()) .addStoreProperties(federatedProperties) .build(); From c633797379e40b3677a0d6fd68a683257e127450 Mon Sep 17 00:00:00 2001 From: m55624 Date: Tue, 3 Oct 2017 16:22:15 +0100 Subject: [PATCH 22/72] gh-1297 - more test fixes --- .../federatedstore/FederatedStoreTest.java | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index 576bdb10b25..08f0a6f2f41 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -100,6 +100,7 @@ public void setUp() throws Exception { federatedProperties.setGraphAuth(MAP_ID_1, ALL_USERS); federatedProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); HashMapGraphLibrary.clear(); + CacheServiceLoader.shutdown(); authUser = authUser(); testUser = testUser(); } @@ -119,6 +120,8 @@ public void shouldThrowExceptionWithoutInitialisation() { .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) .build(); + CacheServiceLoader.shutdown(); + // When / Then try { store.addGraphs(null, TEST_USER, graphToAdd); @@ -553,12 +556,6 @@ private Set getElements() throws uk.gov.gchq.gaffer.operation.Operation @Test public void shouldGetAllGraphIdsInUnmodifiableSet() throws Exception { // Given - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); - final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); - Mockito.when(mockLibrary.getProperties(PROPS_ID_1)).thenReturn(StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES)); - store.setGraphLibrary(mockLibrary); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); // When / Then @@ -630,10 +627,9 @@ public void shouldAddGraphFromGraphIDKeyButDefinedProperties() throws Exception federatedProperties.setGraphIds(MAP_ID_1); federatedProperties.setGraphPropFile(MAP_ID_1, PATH_ACC_STORE_PROPERTIES); federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON); + final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); - final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); - - store.setGraphLibrary(mockLibrary); + store.setGraphLibrary(graphLibrary); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); //Then @@ -704,8 +700,10 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibrary() throws Excep .id(SCHEMA_ID_1) .json(StreamUtil.openStream(FederatedStore.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) .build(); + final StoreProperties properties = StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES); + properties.setId(PROPS_ID_1); final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); - graphLibrary.add(MAP_ID_1, schema, StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES)); + graphLibrary.add(MAP_ID_1, schema, properties); store.setGraphLibrary(graphLibrary); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); @@ -713,7 +711,7 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibrary() throws Excep assertEquals(1, store.getGraphs(testUser, null).size()); assertTrue(graphLibrary.getSchema(SCHEMA_ID_1).toString().equals(schema.toString())); - assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES))); + assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(properties)); } @Test From 9bdccc0e64f6a9cfb5c373acdf7d856c9d1a3da6 Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Tue, 3 Oct 2017 17:10:47 +0100 Subject: [PATCH 23/72] gh-1297 GraphSerialisable buildGraph makes an appropriate StoreProperties. --- .../gchq/gaffer/graph/GraphSerialisable.java | 12 ++- .../gchq/gaffer/store/StoreProperties.java | 1 + ...pStorePropertiesGraphSerialisableTest.java | 84 +++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 store-implementation/map-store/src/test/java/uk/gov/gchq/gaffer/mapstore/MapStorePropertiesGraphSerialisableTest.java diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java index 58218c2fb00..d920fd26175 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java @@ -37,6 +37,7 @@ */ public final class GraphSerialisable implements Serializable { private static final long serialVersionUID = 2684203367656032583L; + public static final String COULD_NOT_CONVERT_PROPERTIES_TO_THE_REQUIRED_PROPERTIES_CLASS_S = "Error while creating graph from GraphSerialisable, could not create the required Properties Class: %s"; private byte[] schema; private Properties properties; private byte[] config; @@ -59,9 +60,18 @@ private GraphSerialisable(final GraphConfig config, final Schema schema, final P * class. */ public Graph buildGraph() { + final String storeClass = properties.getProperty(StoreProperties.STORE_PROPERTIES_CLASS); + final StoreProperties storeProperties; + try { + storeProperties = (StoreProperties) Class.forName(storeClass).newInstance(); + } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + throw new RuntimeException(String.format(COULD_NOT_CONVERT_PROPERTIES_TO_THE_REQUIRED_PROPERTIES_CLASS_S, storeClass), e); + } + storeProperties.setProperties(properties); + return new Graph.Builder() .addSchema(schema) - .addStoreProperties(new StoreProperties(properties)) + .addStoreProperties(storeProperties) .config(config) .build(); } diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/StoreProperties.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/StoreProperties.java index 2f1a6b221ba..314f6c0da8d 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/StoreProperties.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/StoreProperties.java @@ -72,6 +72,7 @@ public class StoreProperties implements Cloneable { // Required for loading by reflection. public StoreProperties() { + setStorePropertiesClass(this.getClass()); } public StoreProperties(final String id) { diff --git a/store-implementation/map-store/src/test/java/uk/gov/gchq/gaffer/mapstore/MapStorePropertiesGraphSerialisableTest.java b/store-implementation/map-store/src/test/java/uk/gov/gchq/gaffer/mapstore/MapStorePropertiesGraphSerialisableTest.java new file mode 100644 index 00000000000..c41b8808700 --- /dev/null +++ b/store-implementation/map-store/src/test/java/uk/gov/gchq/gaffer/mapstore/MapStorePropertiesGraphSerialisableTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2017 Crown Copyright + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.gchq.gaffer.mapstore; + +import org.junit.Before; +import org.junit.Test; + +import uk.gov.gchq.gaffer.data.elementdefinition.view.View; +import uk.gov.gchq.gaffer.graph.Graph; +import uk.gov.gchq.gaffer.graph.GraphConfig; +import uk.gov.gchq.gaffer.graph.GraphSerialisable; +import uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser; +import uk.gov.gchq.gaffer.store.schema.Schema; +import uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition; + +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + +public class MapStorePropertiesGraphSerialisableTest { + private GraphConfig config; + private Schema schema; + private Properties properties; + private GraphSerialisable expected; + + @Before + public void setUp() throws Exception { + config = new GraphConfig.Builder() + .graphId("testGraphId") + .view(new View.Builder() + .entity("e1") + .build()) + .build(); + schema = new Schema.Builder() + .entity("e1", new SchemaEntityDefinition.Builder() + .vertex("string") + .build()) + .type("string", String.class) + .build(); + final MapStoreProperties storeProperties = new MapStoreProperties(); + storeProperties.setStorePropertiesClass(MapStoreProperties.class); + properties = storeProperties.getProperties(); + expected = getGraphSerialisable(); + } + + private GraphSerialisable getGraphSerialisable() { + return new GraphSerialisable.Builder() + .setSchema(schema) + .setProperties(properties) + .setConfig(config) + .build(); + } + + @Test + public void shouldSerialiseAndDeserialise() throws Exception { + final JavaSerialiser javaSerialiser = new JavaSerialiser(); + final byte[] serialise = javaSerialiser.serialise(expected); + final GraphSerialisable result = (GraphSerialisable) javaSerialiser.deserialise(serialise); + assertEquals(expected, result); + } + + @Test + public void shouldConsumeGraph() throws Exception { + final MapStoreProperties mapStoreProperties = new MapStoreProperties(); + mapStoreProperties.setProperties(properties); + final Graph graph = new Graph.Builder().addSchema(schema).addStoreProperties(mapStoreProperties).config(config).build(); + final GraphSerialisable result = new GraphSerialisable.Builder().graph(graph).build(); + assertEquals(expected, result); + } +} \ No newline at end of file From 5b73c076744a3766faa3d2d387c00f6153d481b1 Mon Sep 17 00:00:00 2001 From: m55624 Date: Wed, 4 Oct 2017 10:28:10 +0100 Subject: [PATCH 24/72] gh-1297 - reusing graphs from cache test added --- .../federatedstore/FederatedStoreCache.java | 14 +------ .../federatedstore/FederatedStoreTest.java | 37 ++++++++++++++++++- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java index 40f6edac448..6a1b7681c55 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java @@ -21,7 +21,6 @@ import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; import uk.gov.gchq.gaffer.graph.Graph; import uk.gov.gchq.gaffer.graph.GraphSerialisable; -import uk.gov.gchq.gaffer.store.StoreProperties; import java.util.Set; @@ -58,18 +57,7 @@ public void addSafeToCache(Graph graph) throws CacheOperationException { public Graph getFromCache(String graphId) throws CacheOperationException { final GraphSerialisable graphSerialisable = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); - - Graph graph = new Graph.Builder() - .config(graphSerialisable.getConfig()) - .addStoreProperties(new StoreProperties(graphSerialisable.getProperties())) - .addSchema(graphSerialisable.getSchema()) - .build(); - - if (null != graph) { - return graph; - } else { - throw new CacheOperationException("No graph found in the cache with graphId: " + graphId); - } + return graphSerialisable.buildGraph(); } public void deleteFromCache(String graphId) { diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index 08f0a6f2f41..901703ccf81 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -59,6 +59,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static uk.gov.gchq.gaffer.federatedstore.FederatedGraphStorage.USER_IS_ATTEMPTING_TO_OVERWRITE_A_GRAPH_WITHIN_FEDERATED_STORE_GRAPH_ID_S; @@ -194,6 +195,38 @@ public void shouldAddMultipleGraphsToCache() throws StoreException { } } + @Test + public void shouldReuseGraphsAlreadyInCache() throws Exception { + //Check cache is empty + assertNull(CacheServiceLoader.getService()); + + //initialise FedStore + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + + //add something so it will be in the cache + Graph graphToAdd = new Graph.Builder() + .config(new GraphConfig(MAP_ID_1)) + .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) + .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) + .build(); + + store.addGraphs(null, TEST_USER, graphToAdd); + + //check the store and the cache + assertEquals(1, store.getAllGraphIds(testUser).size()); + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(MAP_ID_1)); + + //restart the store + store = null; + store = new FederatedStore(); + federatedProperties.setGraphIds(MAP_ID_1); + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + + //check the graph is already in there from the cache + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(MAP_ID_1)); + assertEquals(1, store.getAllGraphIds(testUser).size()); + } + @Test public void shouldLoadGraphsWithIds() throws Exception { //Given @@ -855,7 +888,7 @@ public void shouldFederatedIfUserHasCorrectAuths() throws Exception { .opAuths("x") .build()); - Assert.assertNull(x); + assertNull(x); } @Test @@ -978,7 +1011,7 @@ public void shouldAddGraphIdWithAuths() throws Exception { assertEquals(1, after); Assert.assertNotNull(elements); Assert.assertTrue(elements.iterator().hasNext()); - Assert.assertNull(x); + assertNull(x); } @Test From 99991e5aad2746db7cf716a3527c2b2896ffa53d Mon Sep 17 00:00:00 2001 From: m55624 Date: Wed, 4 Oct 2017 11:40:44 +0100 Subject: [PATCH 25/72] gh-1297 - Cache tests and javadoc added --- .../gaffer/federatedstore/FederatedStore.java | 11 +- .../federatedstore/FederatedStoreCache.java | 68 +++++++++-- .../impl/FederatedRemoveGraphHandler.java | 7 +- .../FederatedStoreCacheTest.java | 115 ++++++++++++++++++ 4 files changed, 189 insertions(+), 12 deletions(-) create mode 100644 store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 3f158c018a8..7d79a05962e 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -188,9 +188,14 @@ public void addGraphs(final Set graphAuths, final String addingUserId, f * graphs via the {@link RemoveGraph} operation. * * @param graphId to be removed from scope + * @throws StoreException if there was an error removing the {@link uk.gov.gchq.gaffer.graph.Graph} from the store */ - public void remove(final String graphId) { - federatedStoreCache.deleteFromCache(graphId); + public void remove(final String graphId) throws StoreException { + try { + federatedStoreCache.deleteFromCache(graphId); + } catch (CacheOperationException e) { + throw new StoreException(e); + } graphStorage.remove(graphId); } @@ -461,7 +466,7 @@ private void _add(final Graph newGraph, final FederatedAccess access) { final String graphId = newGraph.getGraphId(); if (!federatedStoreCache.getAllGraphIds().contains(graphId)) { try { - federatedStoreCache.addToCache(newGraph); + federatedStoreCache.addGraphToCache(newGraph, false); } catch (final OverwritingException e) { throw new OverwritingException((String.format("User is attempting to overwrite a graph within the cacheService. GraphId: %s", graphId))); } catch (final Exception e) { diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java index 6a1b7681c55..ed8b1a1cba9 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java @@ -33,6 +33,11 @@ public class FederatedStoreCache { private static final String CACHE_SERVICE_NAME = "federatedStoreGraphs"; + /** + * Get the cache for the FederatedStore. + * + * @return the FederatedStore cache + */ public ICache getCache() { if (CacheServiceLoader.getService() != null) { return CacheServiceLoader.getService().getCache(CACHE_SERVICE_NAME); @@ -41,29 +46,76 @@ public ICache getCache() { } } + /** + * Get all the ID's related to the {@link uk.gov.gchq.gaffer.graph.Graph}'s + * stored in the cache. + * + * @return all the Graph ID's within the cache + */ public Set getAllGraphIds() { return CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME); } - public void addToCache(Graph graph) throws CacheOperationException { + /** + * Add the specified {@link uk.gov.gchq.gaffer.graph.Graph} to the cache. + * + * @param graph the {@link uk.gov.gchq.gaffer.graph.Graph} to be added + * @param overwrite if true, overwrite any graphs already in the cache with the same ID + * @throws CacheOperationException if there was an error trying to add to the cache + */ + public void addGraphToCache(Graph graph, boolean overwrite) throws CacheOperationException { GraphSerialisable graphSerialisable = new GraphSerialisable.Builder().graph(graph).build(); - CacheServiceLoader.getService().putInCache(CACHE_SERVICE_NAME, graph.getGraphId(), graphSerialisable); - } - public void addSafeToCache(Graph graph) throws CacheOperationException { - GraphSerialisable graphSerialisable = new GraphSerialisable.Builder().graph(graph).build(); - CacheServiceLoader.getService().putSafeInCache(CACHE_SERVICE_NAME, graph.getGraphId(), graphSerialisable); + if (overwrite) { + CacheServiceLoader.getService().putInCache(CACHE_SERVICE_NAME, graph.getGraphId(), graphSerialisable); + } else { + CacheServiceLoader.getService().putSafeInCache(CACHE_SERVICE_NAME, graph.getGraphId(), graphSerialisable); + } } + /** + * Retrieve the {@link uk.gov.gchq.gaffer.graph.Graph} with the specified ID from the cache. + * + * @param graphId the ID of the {@link uk.gov.gchq.gaffer.graph.Graph} to retrieve + * @return the {@link uk.gov.gchq.gaffer.graph.Graph} related to the specified ID + * @throws CacheOperationException if there was an error trying to access the cache + */ public Graph getFromCache(String graphId) throws CacheOperationException { + if (null == graphId) { + throw new CacheOperationException("Graph ID cannot be null"); + } + final GraphSerialisable graphSerialisable = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); - return graphSerialisable.buildGraph(); + + if (null != graphSerialisable) { + return graphSerialisable.buildGraph(); + } + throw new CacheOperationException("No graph in the cache with Graph ID: " + graphId); } - public void deleteFromCache(String graphId) { + /** + * Delete the {@link uk.gov.gchq.gaffer.graph.Graph} related to the specified ID from the cache. + * + * @param graphId the ID of the {@link uk.gov.gchq.gaffer.graph.Graph} to be deleted + * @throws CacheOperationException if there was an error trying to delete from the cache + */ + public void deleteFromCache(String graphId) throws CacheOperationException { + if (null == graphId) { + throw new CacheOperationException("Graph ID cannot be null"); + } + CacheServiceLoader.getService().removeFromCache(CACHE_SERVICE_NAME, graphId); + + if (null != CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId)) { + throw new CacheOperationException("Failed to remove Graph with ID: " + graphId + " from cache"); + } } + /** + * Clear the cache. + * + * @throws CacheOperationException if there was an error trying to clear the cache + */ public void clearCache() throws CacheOperationException { CacheServiceLoader.getService().clearCache(CACHE_SERVICE_NAME); } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandler.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandler.java index d31b5710213..572366a999e 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandler.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandler.java @@ -21,6 +21,7 @@ import uk.gov.gchq.gaffer.operation.OperationException; import uk.gov.gchq.gaffer.store.Context; import uk.gov.gchq.gaffer.store.Store; +import uk.gov.gchq.gaffer.store.StoreException; import uk.gov.gchq.gaffer.store.operation.handler.OperationHandler; /** @@ -35,7 +36,11 @@ public class FederatedRemoveGraphHandler implements OperationHandler { @Override public Void doOperation(final RemoveGraph operation, final Context context, final Store store) throws OperationException { - ((FederatedStore) store).remove(operation.getGraphId()); + try { + ((FederatedStore) store).remove(operation.getGraphId()); + } catch (StoreException e) { + throw new OperationException(e.getMessage()); + } return null; } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java new file mode 100644 index 00000000000..ea229082564 --- /dev/null +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java @@ -0,0 +1,115 @@ +/* + * Copyright 2017 Crown Copyright + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.gchq.gaffer.federatedstore; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; +import uk.gov.gchq.gaffer.commonutil.StreamUtil; +import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; +import uk.gov.gchq.gaffer.graph.Graph; +import uk.gov.gchq.gaffer.graph.GraphConfig; + +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class FederatedStoreCacheTest { + private static final String PATH_MAP_STORE_PROPERTIES = "properties/singleUseMockMapStore.properties"; + private static final String PATH_BASIC_EDGE_SCHEMA_JSON = "schema/basicEdgeSchema.json"; + private static final String MAP_ID_1 = "mockMapGraphId1"; + private Graph testGraph = new Graph.Builder().config(new GraphConfig(MAP_ID_1)) + .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) + .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) + .build(); + private static FederatedStoreCache federatedStoreCache; + + @BeforeClass + public static void setUp() { + federatedStoreCache = new FederatedStoreCache(); + } + + @Before + public void beforeEach() throws CacheOperationException { + federatedStoreCache.clearCache(); + } + + @Test + public void shouldAddAndGetGraphToCache() throws CacheOperationException { + federatedStoreCache.addGraphToCache(testGraph, false); + Graph cached = federatedStoreCache.getFromCache(MAP_ID_1); + + assertEquals(testGraph.getGraphId(), cached.getGraphId()); + assertEquals(testGraph.getSchema().toString(), cached.getSchema().toString()); + assertEquals(testGraph.getStoreProperties(), cached.getStoreProperties()); + } + + @Test + public void shouldGetAllGraphIdsFromCache() throws CacheOperationException { + federatedStoreCache.addGraphToCache(testGraph, false); + Set cachedGraphIds = federatedStoreCache.getAllGraphIds(); + assertEquals(1, cachedGraphIds.size()); + assertTrue(cachedGraphIds.contains(testGraph.getGraphId())); + } + + @Test + public void shouldDeleteFromCache() throws CacheOperationException { + federatedStoreCache.addGraphToCache(testGraph, false); + Set cachedGraphIds = federatedStoreCache.getAllGraphIds(); + assertEquals(1, cachedGraphIds.size()); + assertTrue(cachedGraphIds.contains(testGraph.getGraphId())); + + federatedStoreCache.deleteFromCache(testGraph.getGraphId()); + Set cachedGraphIdsAfterDelete = federatedStoreCache.getAllGraphIds(); + assertEquals(0, cachedGraphIdsAfterDelete.size()); + } + + @Test + public void shouldThrowExceptionIfGraphAlreadyExistsInCache() throws CacheOperationException { + federatedStoreCache.addGraphToCache(testGraph, false); + try { + federatedStoreCache.addGraphToCache(testGraph, false); + fail("Exception expected"); + } catch (OverwritingException e) { + assertTrue(e.getMessage().contains("Cache entry already exists")); + } + } + + @Test + public void shouldThrowExceptionIfGraphIdToBeRemovedIsNull() throws CacheOperationException { + federatedStoreCache.addGraphToCache(testGraph, false); + try { + federatedStoreCache.deleteFromCache(null); + } catch (CacheOperationException e) { + assertTrue(e.getMessage().contains("Graph ID cannot be null")); + } + } + + @Test + public void shouldThrowExceptionIfGraphIdToGetIsNull() throws CacheOperationException { + federatedStoreCache.addGraphToCache(testGraph, false); + try { + federatedStoreCache.getFromCache(null); + } catch (CacheOperationException e) { + assertTrue(e.getMessage().contains("Graph ID cannot be null")); + } + } +} From ff42fea3305132ec77df9129fd4ec57b191cac27 Mon Sep 17 00:00:00 2001 From: m55624 Date: Wed, 4 Oct 2017 12:12:20 +0100 Subject: [PATCH 26/72] gh-1297 - GraphSerialisable checkstyle fixes --- .../gov/gchq/gaffer/graph/GraphSerialisable.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java index d920fd26175..5680b5e3ca3 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java @@ -17,6 +17,7 @@ package uk.gov.gchq.gaffer.graph; import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import uk.gov.gchq.gaffer.commonutil.StringUtil; import uk.gov.gchq.gaffer.commonutil.ToStringBuilder; @@ -49,7 +50,7 @@ private GraphSerialisable(final GraphConfig config, final Schema schema, final P try { this.schema = JSONSerialiser.serialise(schema, true); this.config = JSONSerialiser.serialise(config, true); - } catch (SerialisationException e) { + } catch (final SerialisationException e) { throw new IllegalArgumentException("Unable to serialise given parameter", e); } this.properties = properties; @@ -64,7 +65,7 @@ public Graph buildGraph() { final StoreProperties storeProperties; try { storeProperties = (StoreProperties) Class.forName(storeClass).newInstance(); - } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new RuntimeException(String.format(COULD_NOT_CONVERT_PROPERTIES_TO_THE_REQUIRED_PROPERTIES_CLASS_S, storeClass), e); } storeProperties.setProperties(properties); @@ -101,6 +102,15 @@ public String toString() { .build(); } + @Override + public int hashCode() { + return new HashCodeBuilder(13, 31) + .append(this.getConfig()) + .append(this.getSchema()) + .append(this.getProperties()) + .build(); + } + public byte[] getSchema() { return schema; } @@ -134,7 +144,7 @@ public Builder setConfig(final GraphConfig config) { return this; } - public Builder graph(Graph graph) { + public Builder graph(final Graph graph) { schema = graph.getSchema(); properties = graph.getStoreProperties().getProperties(); config = graph.getConfig(); From 9acb92c276f6766308b4d6a109ab4ffe95689a46 Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Wed, 4 Oct 2017 13:09:52 +0100 Subject: [PATCH 27/72] gh-1297 improving the creating StoreProperties logic. --- .../java/uk/gov/gchq/gaffer/graph/Graph.java | 17 ++++++++++++++--- .../gchq/gaffer/graph/GraphSerialisable.java | 12 +----------- .../accumulostore/AccumuloProperties.java | 4 ++++ .../FederatedStoreProperties.java | 1 - .../gchq/gaffer/hbasestore/HBaseProperties.java | 10 ++++++++-- .../gaffer/mapstore/MapStoreProperties.java | 11 ++++++++--- .../parquetstore/ParquetStoreProperties.java | 4 +++- .../gchq/gaffer/proxystore/ProxyProperties.java | 7 +++++++ 8 files changed, 45 insertions(+), 21 deletions(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java index a18e8bbc006..9879041e768 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java @@ -98,9 +98,11 @@ public final class Graph { * and * {@link uk.gov.gchq.gaffer.data.elementdefinition.view.View}. * - * @param config a {@link GraphConfig} used to store the configuration for a + * @param config a {@link GraphConfig} used to store the configuration for + * a * Graph. - * @param schema a {@link Schema} that defines the graph. Should be the copy + * @param schema a {@link Schema} that defines the graph. Should be the + * copy * of the schema that the store is initialised with. * @param store a {@link Store} used to store the elements and handle * operations. @@ -327,7 +329,8 @@ private interface StoreExecuter { *

* Builder for {@link Graph}. *

- * We recommend instantiating a Graph from a graphConfig.json file, a schema + * We recommend instantiating a Graph from a graphConfig.json file, a + * schema * directory and a store.properties file. * For example: *
@@ -466,6 +469,10 @@ public Builder parentStorePropertiesId(final String parentStorePropertiesId) {
             return this;
         }
 
+        public Builder storeProperties(final Properties properties) {
+            return storeProperties(StoreProperties.loadStoreProperties(properties));
+        }
+
         public Builder storeProperties(final StoreProperties properties) {
             this.properties = properties;
             if (null != properties) {
@@ -496,6 +503,10 @@ public Builder storeProperties(final URI propertiesURI) {
             return this;
         }
 
+        public Builder addStoreProperties(final Properties properties) {
+            return addStoreProperties(StoreProperties.loadStoreProperties(properties));
+        }
+
         public Builder addStoreProperties(final StoreProperties updateProperties) {
             if (null == this.properties) {
                 storeProperties(updateProperties);
diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java
index d920fd26175..0c68016a6ad 100644
--- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java
+++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java
@@ -22,7 +22,6 @@
 import uk.gov.gchq.gaffer.commonutil.ToStringBuilder;
 import uk.gov.gchq.gaffer.exception.SerialisationException;
 import uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser;
-import uk.gov.gchq.gaffer.store.StoreProperties;
 import uk.gov.gchq.gaffer.store.schema.Schema;
 
 import java.io.Serializable;
@@ -60,18 +59,9 @@ private GraphSerialisable(final GraphConfig config, final Schema schema, final P
      * class.
      */
     public Graph buildGraph() {
-        final String storeClass = properties.getProperty(StoreProperties.STORE_PROPERTIES_CLASS);
-        final StoreProperties storeProperties;
-        try {
-            storeProperties = (StoreProperties) Class.forName(storeClass).newInstance();
-        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
-            throw new RuntimeException(String.format(COULD_NOT_CONVERT_PROPERTIES_TO_THE_REQUIRED_PROPERTIES_CLASS_S, storeClass), e);
-        }
-        storeProperties.setProperties(properties);
-
         return new Graph.Builder()
                 .addSchema(schema)
-                .addStoreProperties(storeProperties)
+                .addStoreProperties(properties)
                 .config(config)
                 .build();
     }
diff --git a/store-implementation/accumulo-store/src/main/java/uk/gov/gchq/gaffer/accumulostore/AccumuloProperties.java b/store-implementation/accumulo-store/src/main/java/uk/gov/gchq/gaffer/accumulostore/AccumuloProperties.java
index 2a8443eb7ac..39b5f683470 100644
--- a/store-implementation/accumulo-store/src/main/java/uk/gov/gchq/gaffer/accumulostore/AccumuloProperties.java
+++ b/store-implementation/accumulo-store/src/main/java/uk/gov/gchq/gaffer/accumulostore/AccumuloProperties.java
@@ -66,10 +66,14 @@ public class AccumuloProperties extends StoreProperties {
 
     public AccumuloProperties() {
         super();
+        setStoreClass(AccumuloStore.class);
     }
 
     public AccumuloProperties(final Path propFileLocation) {
         super(propFileLocation);
+        if (null == getStoreClass()) {
+            setStoreClass(AccumuloStore.class);
+        }
     }
 
     public static AccumuloProperties loadStoreProperties(final InputStream storePropertiesStream) {
diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java
index 4b8f7afcf72..b03de20d0c4 100644
--- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java
+++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java
@@ -32,7 +32,6 @@ public class FederatedStoreProperties extends StoreProperties {
 
     public FederatedStoreProperties() {
         setStoreClass(FederatedStore.class);
-        setStorePropertiesClass(FederatedStoreProperties.class);
     }
 
     public void setGraphIds(final String graphIdsCSV) {
diff --git a/store-implementation/hbase-store/src/main/java/uk/gov/gchq/gaffer/hbasestore/HBaseProperties.java b/store-implementation/hbase-store/src/main/java/uk/gov/gchq/gaffer/hbasestore/HBaseProperties.java
index ef036ebd8a9..a6b1613f3c3 100755
--- a/store-implementation/hbase-store/src/main/java/uk/gov/gchq/gaffer/hbasestore/HBaseProperties.java
+++ b/store-implementation/hbase-store/src/main/java/uk/gov/gchq/gaffer/hbasestore/HBaseProperties.java
@@ -28,7 +28,8 @@
 /**
  * HBaseProperties contains specific configuration information for the
  * hbase store, such as database connection strings. It wraps
- * {@link uk.gov.gchq.gaffer.data.element.Properties} and lazy loads the all properties from
+ * {@link uk.gov.gchq.gaffer.data.element.Properties} and lazy loads the all
+ * properties from
  * a file when first used.
  */
 public class HBaseProperties extends StoreProperties {
@@ -47,10 +48,14 @@ public class HBaseProperties extends StoreProperties {
 
     public HBaseProperties() {
         super();
+        setStoreClass(HBaseStore.class);
     }
 
     public HBaseProperties(final Path propFileLocation) {
         super(propFileLocation);
+        if (null == getStoreClass()) {
+            setStoreClass(HBaseStore.class);
+        }
     }
 
     public static HBaseProperties loadStoreProperties(final InputStream storePropertiesStream) {
@@ -148,7 +153,8 @@ public int getMaxEntriesForBatchScanner() {
      * Set the max number of items that should be read into the scanner at any
      * one time
      *
-     * @param maxEntriesForBatchScanner the max number of items that should be read into the scanner at any one time
+     * @param maxEntriesForBatchScanner the max number of items that should be
+     *                                  read into the scanner at any one time
      */
     public void setMaxEntriesForBatchScanner(final String maxEntriesForBatchScanner) {
         set(MAX_ENTRIES_FOR_BATCH_SCANNER, maxEntriesForBatchScanner);
diff --git a/store-implementation/map-store/src/main/java/uk/gov/gchq/gaffer/mapstore/MapStoreProperties.java b/store-implementation/map-store/src/main/java/uk/gov/gchq/gaffer/mapstore/MapStoreProperties.java
index e3148eab4de..730ce52b34a 100644
--- a/store-implementation/map-store/src/main/java/uk/gov/gchq/gaffer/mapstore/MapStoreProperties.java
+++ b/store-implementation/map-store/src/main/java/uk/gov/gchq/gaffer/mapstore/MapStoreProperties.java
@@ -39,19 +39,24 @@ public class MapStoreProperties extends StoreProperties {
     public static final String STATIC_MAP_DEFAULT = "false";
 
     /**
-     * Property name for the ingest buffer size. If the value is set to less than 1 then
-     * no buffer is used and elements are added directly to the underlying maps.
+     * Property name for the ingest buffer size. If the value is set to less
+     * than 1 then
+     * no buffer is used and elements are added directly to the underlying
+     * maps.
      */
     public static final String INGEST_BUFFER_SIZE = "gaffer.store.mapstore.map.ingest.buffer.size";
     public static final int INGEST_BUFFER_SIZE_DEFAULT = 0;
 
     public MapStoreProperties() {
         super();
-        set(STORE_CLASS, MapStore.class.getName());
+        setStoreClass(MapStore.class);
     }
 
     public MapStoreProperties(final Path propFileLocation) {
         super(propFileLocation);
+        if (null == getStoreClass()) {
+            setStoreClass(MapStore.class);
+        }
     }
 
     public static MapStoreProperties loadStoreProperties(final InputStream storePropertiesStream) {
diff --git a/store-implementation/parquet-store/src/main/java/uk/gov/gchq/gaffer/parquetstore/ParquetStoreProperties.java b/store-implementation/parquet-store/src/main/java/uk/gov/gchq/gaffer/parquetstore/ParquetStoreProperties.java
index cdda459e07e..71aa0cad818 100644
--- a/store-implementation/parquet-store/src/main/java/uk/gov/gchq/gaffer/parquetstore/ParquetStoreProperties.java
+++ b/store-implementation/parquet-store/src/main/java/uk/gov/gchq/gaffer/parquetstore/ParquetStoreProperties.java
@@ -62,11 +62,13 @@ public class ParquetStoreProperties extends StoreProperties implements Serializa
     public ParquetStoreProperties() {
         super();
         this.setStoreClass(ParquetStore.class);
-        this.setStorePropertiesClass(getClass());
     }
 
     public ParquetStoreProperties(final Path propFileLocation) {
         super(propFileLocation);
+        if (null == getStoreClass()) {
+            setStoreClass(ParquetStore.class);
+        }
     }
 
     public String getDataDir() {
diff --git a/store-implementation/proxy-store/src/main/java/uk/gov/gchq/gaffer/proxystore/ProxyProperties.java b/store-implementation/proxy-store/src/main/java/uk/gov/gchq/gaffer/proxystore/ProxyProperties.java
index 8f49c88a297..0b881aa04ea 100644
--- a/store-implementation/proxy-store/src/main/java/uk/gov/gchq/gaffer/proxystore/ProxyProperties.java
+++ b/store-implementation/proxy-store/src/main/java/uk/gov/gchq/gaffer/proxystore/ProxyProperties.java
@@ -44,14 +44,21 @@ public class ProxyProperties extends StoreProperties {
     private static final String GAFFER_REST_API_VERSION = "v2";
 
     public ProxyProperties() {
+        setStoreClass(ProxyStore.class);
     }
 
     public ProxyProperties(final Path propFileLocation) {
         super(propFileLocation);
+        if (null == getStoreClass()) {
+            setStoreClass(ProxyStore.class);
+        }
     }
 
     public ProxyProperties(final Properties props) {
         super(props);
+        if (null == getStoreClass()) {
+            setStoreClass(ProxyStore.class);
+        }
     }
 
     public int getConnectTimeout() {

From 4757dedbaae01b34959b9e3a68b64b09b778be04 Mon Sep 17 00:00:00 2001
From: GCHQDeveloper404 
Date: Wed, 4 Oct 2017 17:12:43 +0100
Subject: [PATCH 28/72] gh-1297 updating test to demonstrate that Library
 should not change.

---
 .../gaffer/federatedstore/FederatedStoreTest.java    | 12 ++++++------
 .../properties/singleUseMockMapStoreAlt.properties   |  2 +-
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
index 901703ccf81..9b7d755c44e 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
@@ -751,22 +751,22 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibrary() throws Excep
     public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exception {
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1);
-        federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
+        federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES_ALT);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
         final MapStoreProperties prop = new MapStoreProperties();
         prop.setId(PROPS_ID_1);
-        final String unusualKey = "unusualKey";
-        prop.set(unusualKey, "value");
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
-        graphLibrary.add("libraryGraphId", new Schema(), prop);
+        graphLibrary.addProperties(PROPS_ID_1, prop);
+        assertFalse(graphLibrary.getProperties(PROPS_ID_1).containsKey("unusualKey"));
 
         store.setGraphLibrary(graphLibrary);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
         assertEquals(1, store.getGraphs(testUser, null).size());
-        assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty(unusualKey) != null);
+        assertFalse(graphLibrary.getProperties(PROPS_ID_1).containsKey("unusualKey"));
+        assertEquals(prop.getProperties(), graphLibrary.getProperties(PROPS_ID_1).getProperties());
+        assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty("unusualKey") != null);
 
-        assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(prop));
     }
 
     @Test
diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties
index 39e1547a5fb..67a538714dd 100644
--- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties
+++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties
@@ -15,5 +15,5 @@
 #
 gaffer.store.class=uk.gov.gchq.gaffer.mapstore.SingleUseMapStore
 gaffer.store.properties.class=uk.gov.gchq.gaffer.mapstore.MapStoreProperties
-unusualkey=value
+unusualKey=value
 gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService

From 1a728b6d7cae4565c7b1888a671335980f47399e Mon Sep 17 00:00:00 2001
From: m55624 
Date: Wed, 4 Oct 2017 17:30:15 +0100
Subject: [PATCH 29/72] gh-1297 - removed incorrect cache properties from non
 FederatedStore properties files

---
 .../test/resources/properties/singleUseMockAccStore.properties   | 1 -
 .../test/resources/properties/singleUseMockMapStore.properties   | 1 -
 .../resources/properties/singleUseMockMapStoreAlt.properties     | 1 -
 3 files changed, 3 deletions(-)

diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties
index 81d6bdd0365..364120debad 100644
--- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties
+++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties
@@ -19,4 +19,3 @@ accumulo.instance=mockInstanceID1234
 accumulo.zookeepers=aZookeeper
 accumulo.user=bob
 accumulo.password=password
-gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService
diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties
index b0cdf7ead8b..ef5df3c6aa5 100644
--- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties
+++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties
@@ -15,4 +15,3 @@
 #
 gaffer.store.class=uk.gov.gchq.gaffer.mapstore.SingleUseMapStore
 gaffer.store.properties.class=uk.gov.gchq.gaffer.mapstore.MapStoreProperties
-gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService
diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties
index 67a538714dd..921dc1f8a7b 100644
--- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties
+++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties
@@ -16,4 +16,3 @@
 gaffer.store.class=uk.gov.gchq.gaffer.mapstore.SingleUseMapStore
 gaffer.store.properties.class=uk.gov.gchq.gaffer.mapstore.MapStoreProperties
 unusualKey=value
-gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService

From 02427c456bc66c0f1e45c4442824936c1ad9f83e Mon Sep 17 00:00:00 2001
From: GCHQDeveloper404 
Date: Wed, 4 Oct 2017 17:34:43 +0100
Subject: [PATCH 30/72] gh-1297 updating test to demonstrate that Library
 should not change.

---
 .../gaffer/federatedstore/FederatedStoreTest.java    | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
index 9b7d755c44e..c48815b1068 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
@@ -82,6 +82,8 @@ public class FederatedStoreTest {
     private static final String EXCEPTION_NOT_THROWN = "exception not thrown";
     private static final String USER_ID = "testUser";
     private static final String PROPS_ID_1 = "PROPS_ID_1";
+    public static final String UNUSUAL_KEY = "unusualKey";
+    public static final String KEY_DOES_NOT_BELONG = UNUSUAL_KEY + " wasn't added to " + PROPS_ID_1 + " it should not be there";
     private static final String SCHEMA_ID_1 = "SCHEMA_ID_1";
     private static final String ALL_USERS = FederatedStoreUser.ALL_USERS;
     private static final HashSet GRAPH_AUTHS = Sets.newHashSet(ALL_USERS);
@@ -757,15 +759,15 @@ public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exce
         prop.setId(PROPS_ID_1);
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
         graphLibrary.addProperties(PROPS_ID_1, prop);
-        assertFalse(graphLibrary.getProperties(PROPS_ID_1).containsKey("unusualKey"));
+        assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY));
 
         store.setGraphLibrary(graphLibrary);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
         assertEquals(1, store.getGraphs(testUser, null).size());
-        assertFalse(graphLibrary.getProperties(PROPS_ID_1).containsKey("unusualKey"));
+        assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY));
         assertEquals(prop.getProperties(), graphLibrary.getProperties(PROPS_ID_1).getProperties());
-        assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty("unusualKey") != null);
+        assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty(UNUSUAL_KEY) != null);
 
     }
 
@@ -798,7 +800,7 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibraryOverridden() th
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
         federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1);
         final MapStoreProperties prop = new MapStoreProperties();
-        final String unusualKey = "unusualKey";
+        final String unusualKey = UNUSUAL_KEY;
         prop.set(unusualKey, "value");
         final Schema schema = new Schema.Builder()
                 .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
@@ -830,7 +832,7 @@ public void should() throws Exception {
         graphLibrary.add(MAP_ID_1, schema, StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)));
 
         final MapStoreProperties prop = new MapStoreProperties();
-        final String unusualKey = "unusualKey";
+        final String unusualKey = UNUSUAL_KEY;
         prop.set(unusualKey, "value");
 
         store.setGraphLibrary(graphLibrary);

From dffc4f01ff8ad15415aa9cd63c280fa93cac5c11 Mon Sep 17 00:00:00 2001
From: m55624 
Date: Wed, 4 Oct 2017 20:58:01 +0100
Subject: [PATCH 31/72] gh-1297 - added test that should break

---
 .../federatedstore/FederatedStoreTest.java    | 33 +++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
index c48815b1068..c6b02fb93ac 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
@@ -771,6 +771,39 @@ public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exce
 
     }
 
+    @Test
+    public void shouldThrowExceptionWhenCreatingConfigWithDifferentGraphIdInLibraryAndConfig() {
+        final GraphLibrary library = new HashMapGraphLibrary();
+        final MapStoreProperties properties = new MapStoreProperties();
+        properties.setId("propId1");
+        properties.set("gaffer.store.class", "uk.gov.gchq.gaffer.mapstore.SingleUseMapStore");
+        library.add("graphId", new Schema(), properties);
+
+        // This builds fine
+        final GraphConfig config = new GraphConfig.Builder().graphId("graphId")
+                .library(library)
+                .build();
+
+        final Graph graph = new Graph.Builder().config(config).build();
+
+        // This below config fails, because when you build the config the graphId
+        // supplied and the library graphId supplied are different.  This
+        // is the same as lines 348 - 351 in FederatedStore.java, where the two ID's
+        // can be different, so when you try to build there are no properties.
+
+        // This means the only way currently you can get the right properties in
+        // is to specify the same graphId for the FederatedStore as the graphLibrary graphId
+        // although then obviously this will error as you are trying to overwrite
+        // the graphLibrary with new Properties as the graphIds are the same.
+
+        final GraphConfig failedConfig = new GraphConfig.Builder().graphId("graphId1")
+                .library(library)
+                .build();
+
+        final Graph failedGraph = new Graph.Builder().config(failedConfig).build();
+
+    }
+
     @Test
     public void shouldAddGraphWithSchemaFromGraphLibraryOverridden() throws Exception {
         federatedProperties.setGraphIds(MAP_ID_1);

From b14e8c370b94d63aee95fcf09086420e0fd607cc Mon Sep 17 00:00:00 2001
From: GCHQDeveloper404 
Date: Thu, 5 Oct 2017 12:20:44 +0100
Subject: [PATCH 32/72] gh-1297 - amendment

---
 .../gchq/gaffer/federatedstore/FederatedStoreTest.java   | 4 +++-
 .../handler/impl/FederatedAddGraphHandlerTest.java       | 9 ++++-----
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
index c6b02fb93ac..f9e59e2e09f 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
@@ -38,6 +38,7 @@
 import uk.gov.gchq.gaffer.graph.GraphConfig;
 import uk.gov.gchq.gaffer.mapstore.MapStore;
 import uk.gov.gchq.gaffer.mapstore.MapStoreProperties;
+import uk.gov.gchq.gaffer.mapstore.SingleUseMapStore;
 import uk.gov.gchq.gaffer.operation.impl.add.AddElements;
 import uk.gov.gchq.gaffer.operation.impl.get.GetAllElements;
 import uk.gov.gchq.gaffer.store.StoreException;
@@ -774,9 +775,10 @@ public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exce
     @Test
     public void shouldThrowExceptionWhenCreatingConfigWithDifferentGraphIdInLibraryAndConfig() {
         final GraphLibrary library = new HashMapGraphLibrary();
+        HashMapGraphLibrary.clear();
         final MapStoreProperties properties = new MapStoreProperties();
         properties.setId("propId1");
-        properties.set("gaffer.store.class", "uk.gov.gchq.gaffer.mapstore.SingleUseMapStore");
+        properties.setStoreClass(SingleUseMapStore.class);
         library.add("graphId", new Schema(), properties);
 
         // This builds fine
diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java
index 4516b206abb..8d2571824ab 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java
@@ -63,7 +63,7 @@ public class FederatedAddGraphHandlerTest {
     private FederatedStore store;
     private FederatedStoreProperties federatedStoreProperties;
 
-    
+
     @Before
     public void setUp() throws Exception {
         this.store = new FederatedStore();
@@ -84,7 +84,7 @@ public void shouldAddGraph() throws Exception {
 
         assertEquals(0, store.getGraphs(testUser, null).size());
         assertEquals(0, store.getGraphs(testUser, null).size());
-        
+
         FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler();
         federatedAddGraphHandler.doOperation(
                 new AddGraph.Builder()
@@ -242,9 +242,8 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception {
                     store);
             fail("Exception not thrown");
         } catch (OperationException e) {
-            assertEquals("User is limited to only using parentPropertiesId from the graphLibrary," +
-                            " but found storeProperties:{gaffer.store.class=uk.gov.gchq.gaffer.mapstore.MapStore}",
-                    e.getMessage());
+            assertTrue(e.getMessage().contains("User is limited to only using parentPropertiesId from the graphLibrary," +
+                    " but found storeProperties:{gaffer.store.class=uk.gov.gchq.gaffer.mapstore.MapStore"));
         }
 
         federatedAddGraphHandler.doOperation(

From 5308f0a43bbcc29a055862ab257ab3a9aac64759 Mon Sep 17 00:00:00 2001
From: m55624 
Date: Mon, 9 Oct 2017 11:35:37 +0100
Subject: [PATCH 33/72] gh-1297 - FederatedStorePublicAccessTest fixes

---
 .../FederatedStorePublicAccessTest.java         | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java
index 2552eedd197..99785cb0b89 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java
@@ -32,6 +32,8 @@ public class FederatedStorePublicAccessTest {
     public static final String GRAPH_1 = "graph1";
     public static final String PROP_1 = "prop1";
     public static final String SCHEMA_1 = "schema1";
+    public static final String TEST_FED_STORE_ID = "testFedStore";
+    private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService";
     private FederatedStore store;
     private FederatedStoreProperties fedProps;
     private HashMapGraphLibrary library;
@@ -40,6 +42,7 @@ public class FederatedStorePublicAccessTest {
     @Before
     public void setUp() throws Exception {
         fedProps = new FederatedStoreProperties();
+        fedProps.setCacheProperties(CACHE_SERVICE_CLASS_STRING);
         fedProps.setGraphIds(GRAPH_1);
         fedProps.setGraphPropId(GRAPH_1, PROP_1);
         fedProps.setGraphSchemaId(GRAPH_1, SCHEMA_1);
@@ -56,7 +59,7 @@ public void setUp() throws Exception {
 
     @Test
     public void shouldNotBePublicWhenAllGraphsDefaultedPrivateAndGraphIsDefaultedPrivate() throws Exception {
-        store.initialise("testFedStore", null, fedProps);
+        store.initialise(TEST_FED_STORE_ID, null, fedProps);
         final Iterable execute = store.execute(new GetAllGraphIds(), blankUser);
         Assert.assertFalse(execute.iterator().hasNext());
     }
@@ -65,7 +68,7 @@ public void shouldNotBePublicWhenAllGraphsDefaultedPrivateAndGraphIsDefaultedPri
     @Test
     public void shouldBePublicWhenAllGraphsDefaultedPrivateAndGraphIsSetPublic() throws Exception {
         fedProps.setTrueGraphIsPublicValue(GRAPH_1);
-        store.initialise("testFedStore", null, fedProps);
+        store.initialise(TEST_FED_STORE_ID, null, fedProps);
         final Iterable execute = store.execute(new GetAllGraphIds(), blankUser);
         Assert.assertTrue(execute.iterator().hasNext());
     }
@@ -73,7 +76,7 @@ public void shouldBePublicWhenAllGraphsDefaultedPrivateAndGraphIsSetPublic() thr
     @Test
     public void shouldNotBePublicWhenAllGraphsDefaultedPrivateAndGraphIsSetPrivate() throws Exception {
         fedProps.setFalseGraphIsPublicValue(GRAPH_1);
-        store.initialise("testFedStore", null, fedProps);
+        store.initialise(TEST_FED_STORE_ID, null, fedProps);
         final Iterable execute = store.execute(new GetAllGraphIds(), blankUser);
         Assert.assertFalse(execute.iterator().hasNext());
     }
@@ -82,7 +85,7 @@ public void shouldNotBePublicWhenAllGraphsDefaultedPrivateAndGraphIsSetPrivate()
     public void shouldNotBePublicWhenAllGraphsSetPrivateAndGraphIsSetPublic() throws Exception {
         fedProps.setFalseGraphsCanHavePublicAccess();
         fedProps.setTrueGraphIsPublicValue(GRAPH_1);
-        store.initialise("testFedStore", null, fedProps);
+        store.initialise(TEST_FED_STORE_ID, null, fedProps);
         final Iterable execute = store.execute(new GetAllGraphIds(), blankUser);
         Assert.assertFalse(execute.iterator().hasNext());
     }
@@ -91,7 +94,7 @@ public void shouldNotBePublicWhenAllGraphsSetPrivateAndGraphIsSetPublic() throws
     public void shouldNotBePublicWhenAllGraphsSetPrivateAndGraphIsSetPrivate() throws Exception {
         fedProps.setFalseGraphsCanHavePublicAccess();
         fedProps.setFalseGraphIsPublicValue(GRAPH_1);
-        store.initialise("testFedStore", null, fedProps);
+        store.initialise(TEST_FED_STORE_ID, null, fedProps);
         final Iterable execute = store.execute(new GetAllGraphIds(), blankUser);
         Assert.assertFalse(execute.iterator().hasNext());
     }
@@ -101,7 +104,7 @@ public void shouldNotBePublicWhenAllGraphsSetPrivateAndGraphIsSetPrivate() throw
     public void shouldNotBePublicWhenAllGraphsSetPublicAndGraphIsSetPrivate() throws Exception {
         fedProps.setTrueGraphsCanHavePublicAccess();
         fedProps.setFalseGraphIsPublicValue(GRAPH_1);
-        store.initialise("testFedStore", null, fedProps);
+        store.initialise(TEST_FED_STORE_ID, null, fedProps);
         final Iterable execute = store.execute(new GetAllGraphIds(), blankUser);
         Assert.assertFalse(execute.iterator().hasNext());
     }
@@ -110,7 +113,7 @@ public void shouldNotBePublicWhenAllGraphsSetPublicAndGraphIsSetPrivate() throws
     public void shouldBePublicWhenAllGraphsSetPublicAndGraphIsSetPublic() throws Exception {
         fedProps.setTrueGraphsCanHavePublicAccess();
         fedProps.setTrueGraphIsPublicValue(GRAPH_1);
-        store.initialise("testFedStore", null, fedProps);
+        store.initialise(TEST_FED_STORE_ID, null, fedProps);
         final Iterable execute = store.execute(new GetAllGraphIds(), blankUser);
         Assert.assertTrue(execute.iterator().hasNext());
     }

From e152ab3591f8c363c99ca9df4ce961ac43f0bd74 Mon Sep 17 00:00:00 2001
From: m55624 
Date: Mon, 9 Oct 2017 13:58:27 +0100
Subject: [PATCH 34/72] gh-1297 - test fixes

---
 .../gchq/gaffer/federatedstore/FederatedStoreAuthTest.java    | 4 +++-
 .../uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java | 2 +-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java
index 7d2255a99bf..7f4b760a50d 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java
@@ -86,9 +86,11 @@ public void shouldAddGraphWithAuth() throws Exception {
         assertEquals(EXPECTED_GRAPH_ID, next.getGraphId());
         assertEquals(expectedSchema, next.getSchema());
 
+        Context blankContext = new Context(blankUser());
+
         final Iterable execute = store.execute(
                 new GetAllGraphIds(),
-                blankUser());
+                blankContext);
 
         assertFalse(execute.iterator().hasNext());
     }
diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
index 2ee6b377195..a4b101079c2 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
@@ -759,7 +759,7 @@ public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exce
         final MapStoreProperties prop = new MapStoreProperties();
         prop.setId(PROPS_ID_1);
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
-        graphLibrary.addProperties(PROPS_ID_1, prop);
+        graphLibrary.addProperties(prop);
         assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY));
 
         store.setGraphLibrary(graphLibrary);

From 007b6a0dfbb14937853364e6864422bc6c1ceb00 Mon Sep 17 00:00:00 2001
From: m55624 
Date: Tue, 10 Oct 2017 10:00:20 +0100
Subject: [PATCH 35/72] gh-1297 - FederatedStoreTest refactoring

---
 .../federatedstore/FederatedStoreTest.java    | 287 ++++++++++--------
 1 file changed, 158 insertions(+), 129 deletions(-)

diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
index a4b101079c2..9df0ec051ba 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
@@ -25,6 +25,7 @@
 
 import uk.gov.gchq.gaffer.accumulostore.SingleUseAccumuloStore;
 import uk.gov.gchq.gaffer.cache.CacheServiceLoader;
+import uk.gov.gchq.gaffer.commonutil.JsonUtil;
 import uk.gov.gchq.gaffer.commonutil.StreamUtil;
 import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable;
 import uk.gov.gchq.gaffer.data.element.Edge;
@@ -84,7 +85,7 @@ public class FederatedStoreTest {
     private static final String USER_ID = "testUser";
     private static final String PROPS_ID_1 = "PROPS_ID_1";
     public static final String UNUSUAL_KEY = "unusualKey";
-    public static final String KEY_DOES_NOT_BELONG = UNUSUAL_KEY + " wasn't added to " + PROPS_ID_1 + " it should not be there";
+    public static final String KEY_DOES_NOT_BELONG = UNUSUAL_KEY + " was added to " + PROPS_ID_1 + " it should not be there";
     private static final String SCHEMA_ID_1 = "SCHEMA_ID_1";
     private static final String ALL_USERS = FederatedStoreUser.ALL_USERS;
     private static final HashSet GRAPH_AUTHS = Sets.newHashSet(ALL_USERS);
@@ -129,10 +130,9 @@ public void shouldThrowExceptionWithoutInitialisation() {
         // When / Then
         try {
             store.addGraphs(null, TEST_USER, graphToAdd);
-            fail("Exception expected");
+            fail(EXCEPTION_NOT_THROWN);
         } catch (final StoreException e) {
             assertTrue(e.getMessage().contains("No cache has been set"));
-            System.out.println(e);
         }
     }
 
@@ -141,9 +141,10 @@ public void shouldThrowExceptionWhenInitialisedWithNoCacheClassInProperties() th
         // Given
         StoreProperties storeProperties = new StoreProperties();
 
+        // When / Then
         try {
             store.initialise(FEDERATED_STORE_ID, null, storeProperties);
-            fail("Exception expected");
+            fail(EXCEPTION_NOT_THROWN);
         } catch (final StoreException e) {
             assertTrue(e.getMessage().contains("No cache has been set, please check the property"));
         }
@@ -151,6 +152,7 @@ public void shouldThrowExceptionWhenInitialisedWithNoCacheClassInProperties() th
 
     @Test
     public void shouldAddGraphsToCache() throws StoreException {
+        // Given
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
         Graph graphToAdd = new Graph.Builder()
@@ -159,22 +161,31 @@ public void shouldAddGraphsToCache() throws StoreException {
                 .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON))
                 .build();
 
+        // When
         store.addGraphs(null, TEST_USER, graphToAdd);
+
+        // Then
         assertEquals(1, store.getGraphs(testUser, ACC_ID_1).size());
 
+        // When
         Collection storeGraphs = store.getGraphs(testUser, null);
+
+        // Then
         assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1));
         assertTrue(storeGraphs.contains(graphToAdd));
 
+        // When
         store = new FederatedStore();
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
+
+        // Then
         assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1));
     }
 
     @Test
     public void shouldAddMultipleGraphsToCache() throws StoreException {
+        // Given
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
-
         List graphsToAdd = new ArrayList<>();
         for (int i = 0; i < 10; i++) {
             graphsToAdd.add(new Graph.Builder()
@@ -184,15 +195,19 @@ public void shouldAddMultipleGraphsToCache() throws StoreException {
                     .build());
         }
 
+        // When
         store.addGraphs(null, TEST_USER, graphsToAdd.toArray(new Graph[graphsToAdd.size()]));
 
+        // Then
         for (int i = 0; i < 10; i++) {
             assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1 + i));
         }
 
+        // When
         store = new FederatedStore();
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
+        // Then
         for (int i = 0; i < 10; i++) {
             assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1 + i));
         }
@@ -232,20 +247,18 @@ public void shouldReuseGraphsAlreadyInCache() throws Exception {
 
     @Test
     public void shouldLoadGraphsWithIds() throws Exception {
-        //Given
+        // Given
         federatedProperties.setGraphIds(ACC_ID_1 + "," + MAP_ID_1);
         federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
 
-        //Then
+        // When
         int before = store.getGraphs(testUser, null).size();
-
-        //When
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
-        //Then
+        // Then
         Collection graphs = store.getGraphs(testUser, null);
         int after = graphs.size();
         assertEquals(0, before);
@@ -258,20 +271,18 @@ public void shouldLoadGraphsWithIds() throws Exception {
 
     @Test
     public void shouldThrowErrorForFailedSchema() throws Exception {
-        //Given
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_INVALID);
 
-        //When
+        // When / Then
         try {
             store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
+            fail(EXCEPTION_NOT_THROWN);
         } catch (final IllegalArgumentException e) {
-            //Then
             assertEquals(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Schema", "graphId: " + MAP_ID_1 + " schemaPath: " + PATH_INVALID), e.getMessage());
-            return;
         }
-        fail(EXCEPTION_NOT_THROWN);
     }
 
     @Test
@@ -281,15 +292,13 @@ public void shouldThrowErrorForFailedProperty() throws Exception {
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_INVALID);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
 
-        //When
+        //When / Then
         try {
             store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
+            fail(EXCEPTION_NOT_THROWN);
         } catch (final IllegalArgumentException e) {
-            //Then
             assertEquals(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Property", "graphId: " + MAP_ID_1 + " propertyPath: " + PATH_INVALID), e.getMessage());
-            return;
         }
-        fail(EXCEPTION_NOT_THROWN);
     }
 
     @Test
@@ -297,15 +306,13 @@ public void shouldThrowErrorForIncompleteBuilder() throws Exception {
         //Given
         federatedProperties.setGraphIds(MAP_ID_1);
 
-        //When
+        //When / Then
         try {
             store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
+            fail(EXCEPTION_NOT_THROWN);
         } catch (final IllegalArgumentException e) {
-            //Then
             assertTrue(e.getMessage().contains(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Graph", "")));
-            return;
         }
-        fail(EXCEPTION_NOT_THROWN);
     }
 
     @Test
@@ -314,38 +321,42 @@ public void shouldNotAllowOverwritingOfGraphWithFederatedScope() throws Exceptio
         federatedProperties.setGraphIds(ACC_ID_1);
         federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON);
-
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
+        // When / Then
         try {
             store.addGraphs(null, null, new Graph.Builder()
                     .config(new GraphConfig(ACC_ID_1))
                     .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES))
                     .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON))
                     .build());
+            fail(EXCEPTION_NOT_THROWN);
         } catch (final Exception e) {
             assertEquals(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, ACC_ID_1), e.getMessage());
-            return;
         }
-        fail(EXCEPTION_NOT_THROWN);
     }
 
-    @Test(expected = UnsupportedOperationException.class)
+    @Test
     public void shouldDoUnhandledOperation() throws Exception {
-        store.doUnhandledOperation(null, null);
+        // When / Then
+        try {
+            store.doUnhandledOperation(null, null);
+            fail(EXCEPTION_NOT_THROWN);
+        } catch (UnsupportedOperationException e) {
+            assertNotNull(e);
+        }
     }
 
     @Test
     public void shouldUpdateTraitsWhenNewGraphIsAdded() throws Exception {
+        // Given
         federatedProperties.setGraphIds(ACC_ID_1);
         federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON);
-
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
-
-        //With less Traits
         Set before = store.getTraits();
 
+        // When
         store.addGraphs(null, null, new Graph.Builder()
                 .config(new GraphConfig(MAP_ID_1))
                 .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES))
@@ -354,6 +365,8 @@ public void shouldUpdateTraitsWhenNewGraphIsAdded() throws Exception {
 
         //includes same as before but with more Traits
         Set after = store.getTraits();
+
+        // Then
         assertEquals("Sole graph has 9 traits, so all traits of the federatedStore is 9", 9, before.size());
         assertEquals("the two graphs share 5 traits", 5, after.size());
         assertNotEquals(before, after);
@@ -362,40 +375,41 @@ public void shouldUpdateTraitsWhenNewGraphIsAdded() throws Exception {
 
     @Test
     public void shouldUpdateSchemaWhenNewGraphIsAdded() throws Exception {
+        // Given
         federatedProperties.setGraphIds(ACC_ID_1);
         federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON);
-
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
-
         Schema before = store.getSchema();
 
+        // When
         store.addGraphs(null, null, new Graph.Builder()
                 .config(new GraphConfig(MAP_ID_1))
                 .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES))
                 .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON))
                 .build());
-
         Schema after = store.getSchema();
+
+        // Then
         assertNotEquals(before, after);
     }
 
     @Test
     public void shouldUpdateTraitsToMinWhenGraphIsRemoved() throws Exception {
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1 + "," + ACC_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
         federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON);
-
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
-        //With less Traits
+        // When
         Set before = store.getTraits();
         store.remove(MAP_ID_1, testUser);
         Set after = store.getTraits();
 
-        //includes same as before but with more Traits
+        // Then
         assertEquals("Shared traits between the two graphs should be " + 5, 5, before.size());
         assertEquals("Shared traits counter-intuitively will go up after removing graph, because the sole remaining graph has 9 traits", 9, after.size());
         assertNotEquals(before, after);
@@ -404,19 +418,20 @@ public void shouldUpdateTraitsToMinWhenGraphIsRemoved() throws Exception {
 
     @Test
     public void shouldUpdateSchemaWhenNewGraphIsRemoved() throws Exception {
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1 + "," + ACC_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
         federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON);
-
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
-
         Schema before = store.getSchema();
 
+        // When
         store.remove(MAP_ID_1, testUser);
-
         Schema after = store.getSchema();
+
+        // Then
         assertNotEquals(before, after);
     }
 
@@ -427,64 +442,65 @@ public void shouldFailWithIncompleteSchema() throws Exception {
         federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(ACC_ID_1, "/schema/edgeX2NoTypesSchema.json");
 
-
+        // When / Then
         try {
             store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
+            fail(EXCEPTION_NOT_THROWN);
         } catch (final Exception e) {
             assertTrue(e.getMessage().contains(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Graph", "")));
-            return;
         }
-        fail(EXCEPTION_NOT_THROWN);
     }
 
     @Test
     public void shouldTakeCompleteSchemaFromTwoFiles() throws Exception {
-        //Given
+        // Given
         federatedProperties.setGraphIds(ACC_ID_1);
         federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(ACC_ID_1, "/schema/edgeX2NoTypesSchema.json" + ", /schema/edgeTypeSchema.json");
-
-
         int before = store.getGraphs(testUser, null).size();
+
+        // When
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
         int after = store.getGraphs(testUser, null).size();
 
+        // Then
         assertEquals(0, before);
         assertEquals(1, after);
     }
 
     @Test
     public void shouldAddTwoGraphs() throws Exception {
+        // Given
         final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(FederatedStoreITs.class, PATH_FEDERATED_STORE_PROPERTIES));
         federatedProperties.setProperties(storeProperties.getProperties());
-        // When
         int sizeBefore = store.getGraphs(authUser, null).size();
+
+        // When
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
         int sizeAfter = store.getGraphs(authUser, null).size();
 
-        //Then
+        // Then
         assertEquals(0, sizeBefore);
         assertEquals(2, sizeAfter);
     }
 
     @Test
     public void shouldCombineTraitsToMin() throws Exception {
+        // Given
         final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(FederatedStoreITs.class, PATH_FEDERATED_STORE_PROPERTIES));
         federatedProperties.setProperties(storeProperties.getProperties());
-
-        //Given
         HashSet traits = new HashSet<>();
         traits.addAll(SingleUseAccumuloStore.TRAITS);
         traits.retainAll(MapStore.TRAITS);
-
-        //When
         Set before = store.getTraits();
         int sizeBefore = before.size();
+
+        // When
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
         Set after = store.getTraits();
         int sizeAfter = after.size();
 
-        //Then
+        // Then
         assertEquals(5, MapStore.TRAITS.size());
         assertEquals(9, SingleUseAccumuloStore.TRAITS.size());
         assertNotEquals(SingleUseAccumuloStore.TRAITS, MapStore.TRAITS);
@@ -495,19 +511,23 @@ public void shouldCombineTraitsToMin() throws Exception {
 
     @Test
     public void shouldContainNoElements() throws Exception {
+        // Given
         final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(FederatedStoreITs.class, PATH_FEDERATED_STORE_PROPERTIES));
         federatedProperties.setProperties(storeProperties.getProperties());
+        // When
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
         Set after = getElements();
+
+        // Then
         assertEquals(0, after.size());
     }
 
     @Test
     public void shouldAddEdgesToOneGraph() throws Exception {
+        // Given
         final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(FederatedStoreITs.class, PATH_FEDERATED_STORE_PROPERTIES));
         federatedProperties.setProperties(storeProperties.getProperties());
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
-
         AddElements op = new AddElements.Builder()
                 .input(new Edge.Builder()
                         .group("BasicEdge")
@@ -517,23 +537,27 @@ public void shouldAddEdgesToOneGraph() throws Exception {
                         .build())
                 .build();
 
+        // When
         store.execute(op, new Context(authUser));
 
+        // Then
         assertEquals(1, getElements().size());
     }
 
     @Test
     public void shouldReturnGraphIds() throws Exception {
-        //Given
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1 + "," + ACC_ID_1);
         federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
-        store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
+        // When
+        store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
         Collection allGraphIds = store.getAllGraphIds(testUser);
 
+        // Then
         assertEquals(2, allGraphIds.size());
         assertTrue(allGraphIds.contains(ACC_ID_1));
         assertTrue(allGraphIds.contains(MAP_ID_1));
@@ -542,53 +566,44 @@ public void shouldReturnGraphIds() throws Exception {
 
     @Test
     public void shouldUpdateGraphIds() throws Exception {
-        //Given
+        // Given
         federatedProperties.setGraphIds(ACC_ID_1);
         federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
+        // When
         Collection allGraphId = store.getAllGraphIds(testUser);
 
+        // Then
         assertEquals(1, allGraphId.size());
         assertTrue(allGraphId.contains(ACC_ID_1));
         assertFalse(allGraphId.contains(MAP_ID_1));
 
+        // When
         store.addGraphs(GRAPH_AUTHS, null, new Graph.Builder()
                 .config(new GraphConfig(MAP_ID_1))
                 .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES))
                 .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_ENTITY_SCHEMA_JSON))
                 .build());
-
-
         Collection allGraphId2 = store.getAllGraphIds(testUser);
 
+        // Then
         assertEquals(2, allGraphId2.size());
         assertTrue(allGraphId2.contains(ACC_ID_1));
         assertTrue(allGraphId2.contains(MAP_ID_1));
 
+        // When
         store.remove(ACC_ID_1, testUser);
-
         Collection allGraphId3 = store.getAllGraphIds(testUser);
 
+        // Then
         assertEquals(1, allGraphId3.size());
         assertFalse(allGraphId3.contains(ACC_ID_1));
         assertTrue(allGraphId3.contains(MAP_ID_1));
 
     }
 
-    private Set getElements() throws uk.gov.gchq.gaffer.operation.OperationException {
-        CloseableIterable elements = store
-                .execute(new GetAllElements.Builder()
-                        .view(new View.Builder()
-                                .edges(store.getSchema().getEdgeGroups())
-                                .entities(store.getSchema().getEntityGroups())
-                                .build())
-                        .build(), new Context(authUser));
-
-        return (null == elements) ? Sets.newHashSet() : Sets.newHashSet(elements);
-    }
-
     @Test
     public void shouldGetAllGraphIdsInUnmodifiableSet() throws Exception {
         // Given
@@ -597,7 +612,7 @@ public void shouldGetAllGraphIdsInUnmodifiableSet() throws Exception {
         // When / Then
         try {
             store.getAllGraphIds(testUser).add("newId");
-            fail("Exception expected");
+            fail(EXCEPTION_NOT_THROWN);
         } catch (UnsupportedOperationException e) {
             assertNotNull(e);
         }
@@ -605,42 +620,46 @@ public void shouldGetAllGraphIdsInUnmodifiableSet() throws Exception {
 
     @Test
     public void shouldNotUseSchema() throws Exception {
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
         final Schema unusedMock = Mockito.mock(Schema.class);
+
+        // Then
         Mockito.verifyNoMoreInteractions(unusedMock);
 
+        // When
         store.initialise(FEDERATED_STORE_ID, unusedMock, federatedProperties);
     }
 
     @Test
     public void shouldAddGraphFromLibrary() throws Exception {
-        //Given
+        // Given
         final Schema schema = new Schema.Builder()
                 .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
                 .build();
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
         graphLibrary.add(MAP_ID_1, schema, StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)));
-
         store.setGraphLibrary(graphLibrary);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
-        //When
+        // When
         final int before = store.getGraphs(testUser, null).size();
         store.execute(new AddGraph.Builder()
                 .graphId(MAP_ID_1)
                 .build(), new Context(testUser));
 
         final int after = store.getGraphs(testUser, null).size();
-        //Then
+
+        // Then
         assertEquals(0, before);
         assertEquals(1, after);
     }
 
     @Test
     public void shouldAddNamedGraphFromGraphIDKeyButDefinedInLibrary() throws Exception {
-        //Given
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphIds(MAP_ID_1);
         GraphLibrary library = new HashMapGraphLibrary();
@@ -648,34 +667,34 @@ public void shouldAddNamedGraphFromGraphIDKeyButDefinedInLibrary() throws Except
                         .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
                         .build(),
                 StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)));
-
         store.setGraphLibrary(library);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
-        //Then
+        // Then
         final int after = store.getGraphs(testUser, null).size();
         assertEquals(1, after);
     }
 
     @Test
     public void shouldAddGraphFromGraphIDKeyButDefinedProperties() throws Exception {
-        //Given
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_ACC_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON);
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
-
         store.setGraphLibrary(graphLibrary);
+
+        // When
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
-        //Then
+        // Then
         final int after = store.getGraphs(testUser, null).size();
         assertEquals(1, after);
     }
 
     @Test
     public void shouldAddNamedGraphsFromGraphIDKeyButDefinedInLibraryAndProperties() throws Exception {
-        //Given
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1 + ", " + ACC_ID_1);
         federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON);
@@ -685,32 +704,36 @@ public void shouldAddNamedGraphsFromGraphIDKeyButDefinedInLibraryAndProperties()
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
         graphLibrary.add(MAP_ID_1, schema, StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)));
 
+        // When
         store.setGraphLibrary(graphLibrary);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
-        //Then
+        // Then
         final int after = store.getGraphs(testUser, null).size();
         assertEquals(2, after);
     }
 
     @Test
     public void shouldAddGraphWithPropertiesFromGraphLibrary() throws Exception {
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
         final HashMapGraphLibrary library = new HashMapGraphLibrary();
         library.add(PROPS_ID_1, new Schema(), StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES));
 
+        // When
         store.setGraphLibrary(library);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
+        // Then
         assertEquals(1, store.getGraphs(testUser, null).size());
-
         assertTrue(library.getProperties(PROPS_ID_1).equals(StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES)));
     }
 
     @Test
     public void shouldAddGraphWithSchemaFromGraphLibrary() throws Exception {
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1);
@@ -720,15 +743,18 @@ public void shouldAddGraphWithSchemaFromGraphLibrary() throws Exception {
                 .build();
         library.add(SCHEMA_ID_1, schema, new StoreProperties());
 
+        // When
         store.setGraphLibrary(library);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
+        // Then
         assertEquals(1, store.getGraphs(testUser, null).size());
         assertTrue(library.getSchema(SCHEMA_ID_1).toString().equals(schema.toString()));
     }
 
     @Test
     public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibrary() throws Exception {
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1);
         federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1);
@@ -741,17 +767,19 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibrary() throws Excep
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
         graphLibrary.add(MAP_ID_1, schema, properties);
 
+        // When
         store.setGraphLibrary(graphLibrary);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
+        // Then
         assertEquals(1, store.getGraphs(testUser, null).size());
-
-        assertTrue(graphLibrary.getSchema(SCHEMA_ID_1).toString().equals(schema.toString()));
+        assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_1).toJson(false), schema.toJson(false)));
         assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(properties));
     }
 
-    //@Test
+    @Test
     public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exception {
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES_ALT);
@@ -762,9 +790,11 @@ public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exce
         graphLibrary.addProperties(prop);
         assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY));
 
+        // When
         store.setGraphLibrary(graphLibrary);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
+        // Then
         assertEquals(1, store.getGraphs(testUser, null).size());
         assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY));
         assertEquals(prop.getProperties(), graphLibrary.getProperties(PROPS_ID_1).getProperties());
@@ -772,29 +802,33 @@ public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exce
 
     }
 
-    //@Test
+    @Test
     public void shouldAddGraphWithSchemaFromGraphLibraryOverridden() throws Exception {
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
         federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1);
         final Schema schema = new Schema.Builder()
+                .id(SCHEMA_ID_1)
                 .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
                 .build();
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
-        graphLibrary.add(MAP_ID_1, schema, new StoreProperties());
+        graphLibrary.addSchema(schema);
 
+        // When
         store.setGraphLibrary(graphLibrary);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
+        // Then
         assertEquals(1, store.getGraphs(testUser, null).size());
         assertTrue(store.getGraphs(testUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity"));
-
         assertTrue(graphLibrary.getSchema(SCHEMA_ID_1).equals(schema));
     }
 
-    //@Test
+    @Test
     public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibraryOverridden() throws Exception {
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
@@ -809,42 +843,21 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibraryOverridden() th
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
         graphLibrary.add(MAP_ID_1, schema, prop);
 
+        // When
         store.setGraphLibrary(graphLibrary);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
+        // Then
         assertEquals(1, store.getGraphs(testUser, null).size());
         assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty(unusualKey) != null);
         assertTrue(store.getGraphs(testUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity"));
-
         assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(prop));
         assertTrue(graphLibrary.getSchema(SCHEMA_ID_1).equals(schema));
     }
 
-    @Test
-    public void should() throws Exception {
-        //Given
-        federatedProperties.setGraphIds(MAP_ID_1);
-        federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
-        federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
-        final Schema schema = new Schema.Builder()
-                .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_EDGE_SCHEMA_JSON))
-                .build();
-        final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
-        graphLibrary.add(MAP_ID_1, schema, StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)));
-
-        final MapStoreProperties prop = new MapStoreProperties();
-        final String unusualKey = UNUSUAL_KEY;
-        prop.set(unusualKey, "value");
-
-        store.setGraphLibrary(graphLibrary);
-        store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
-
-        //No exception to be thrown.
-    }
-
     @Test
     public void shouldNotAllowOverridingOfKnownGraphInLibrary() throws Exception {
-        //Given
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES_ALT);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
@@ -855,10 +868,11 @@ public void shouldNotAllowOverridingOfKnownGraphInLibrary() throws Exception {
         final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES));
         graphLibrary.add(MAP_ID_1, schema, storeProperties);
 
+        // When / Then
         try {
             store.setGraphLibrary(graphLibrary);
             store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
-            fail("exception should have been thrown");
+            fail(EXCEPTION_NOT_THROWN);
         } catch (final IllegalArgumentException e) {
             assertTrue(e.getCause().getMessage().contains("GraphId " + MAP_ID_1 + " already exists with a different store properties:"));
         }
@@ -866,9 +880,8 @@ public void shouldNotAllowOverridingOfKnownGraphInLibrary() throws Exception {
 
     @Test
     public void shouldFederatedIfUserHasCorrectAuths() throws Exception {
-        //Given
+        // Given
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
-
         store.addGraphs(GRAPH_AUTHS, null, new Graph.Builder()
                 .config(new GraphConfig.Builder()
                         .graphId(MAP_ID_1)
@@ -877,20 +890,24 @@ public void shouldFederatedIfUserHasCorrectAuths() throws Exception {
                 .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_ENTITY_SCHEMA_JSON))
                 .build());
 
+        // When
         final CloseableIterable elements = store.execute(new GetAllElements(),
                 new Context(new User.Builder()
                         .userId(USER_ID)
                         .opAuth(ALL_USERS)
                         .build()));
 
+        // Then
         Assert.assertFalse(elements.iterator().hasNext());
 
+        // When
         final CloseableIterable x = store.execute(new GetAllElements(),
                 new Context(new User.Builder()
                         .userId(USER_ID)
                         .opAuths("x")
                         .build()));
 
+        // Then
         assertNull(x);
     }
 
@@ -945,9 +962,8 @@ public void shouldReturnGraphsWithLeadingCommaString() throws StoreException {
 
     @Test
     public void shouldAddGraphIdWithAuths() throws Exception {
+        // Given
         federatedProperties.setFalseSkipFailedExecution();
-
-        //Given
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
         graphLibrary.add(MAP_ID_1,
                 new Schema.Builder()
@@ -963,7 +979,7 @@ public void shouldAddGraphIdWithAuths() throws Exception {
                 .addStoreProperties(federatedProperties)
                 .build();
 
-        //When
+        // When
         int before = 0;
         for (String ignore : fedGraph.execute(
                 new GetAllGraphIds(),
@@ -1009,7 +1025,7 @@ public void shouldAddGraphIdWithAuths() throws Exception {
                         .opAuths("x")
                         .build());
 
-        //Then
+        // Then
         assertEquals(0, before);
         assertEquals(1, after);
         Assert.assertNotNull(elements);
@@ -1019,6 +1035,7 @@ public void shouldAddGraphIdWithAuths() throws Exception {
 
     @Test
     public void shouldThrowWithPropertiesErrorFromGraphLibrary() throws Exception {
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
@@ -1028,34 +1045,34 @@ public void shouldThrowWithPropertiesErrorFromGraphLibrary() throws Exception {
 
         store.setGraphLibrary(mockLibrary);
 
+        // When / Then
         try {
             store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
-            fail("exception not thrown");
+            fail(EXCEPTION_NOT_THROWN);
         } catch (IllegalArgumentException e) {
             assertEquals(error, e.getCause().getMessage());
         }
-
         Mockito.verify(mockLibrary).getProperties(PROPS_ID_1);
     }
 
     @Test
     public void shouldThrowWithSchemaErrorFromGraphLibrary() throws Exception {
+        // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1);
         final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class);
         final String error = "Something went wrong";
         Mockito.when(mockLibrary.getSchema(SCHEMA_ID_1)).thenThrow(new IllegalArgumentException(error));
-
         store.setGraphLibrary(mockLibrary);
 
+        // When / Then
         try {
             store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
-            fail("exception not thrown");
+            fail(EXCEPTION_NOT_THROWN);
         } catch (IllegalArgumentException e) {
             assertEquals(error, e.getCause().getMessage());
         }
-
         Mockito.verify(mockLibrary).getSchema(SCHEMA_ID_1);
     }
 
@@ -1111,4 +1128,16 @@ private List> populateGraphs(int... expectedIds) throws StoreE
         graphLists.add(unexpectedGraphs);
         return graphLists;
     }
+
+    private Set getElements() throws uk.gov.gchq.gaffer.operation.OperationException {
+        CloseableIterable elements = store
+                .execute(new GetAllElements.Builder()
+                        .view(new View.Builder()
+                                .edges(store.getSchema().getEdgeGroups())
+                                .entities(store.getSchema().getEntityGroups())
+                                .build())
+                        .build(), new Context(authUser));
+
+        return (null == elements) ? Sets.newHashSet() : Sets.newHashSet(elements);
+    }
 }

From fe9cce58c65eba0f94c07177a20cc3a44b629501 Mon Sep 17 00:00:00 2001
From: m55624 
Date: Tue, 10 Oct 2017 13:33:04 +0100
Subject: [PATCH 36/72] merged StoreProps id fixes

---
 core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java     | 1 +
 .../uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java    | 1 +
 2 files changed, 2 insertions(+)

diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java
index aab9722eede..7ae13846cd2 100644
--- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java
+++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java
@@ -857,6 +857,7 @@ private void updateStore(final GraphConfig config) {
                     mergedStoreProperties = properties;
                 } else {
                     mergedStoreProperties.getProperties().putAll(properties.getProperties());
+                    mergedStoreProperties.setId(mergedStoreProperties.getId() + "," + properties.getId());
                 }
             }
 
diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
index 8de73e85821..d2810d138b2 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
@@ -797,6 +797,7 @@ public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exce
 
         // Then
         assertEquals(1, store.getGraphs(testUser, null).size());
+        assertTrue(store.getGraphs(testUser, MAP_ID_1).iterator().next().getStoreProperties().containsKey(UNUSUAL_KEY));
         assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY));
         assertEquals(prop.getProperties(), graphLibrary.getProperties(PROPS_ID_1).getProperties());
         assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty(UNUSUAL_KEY) != null);

From c5e0e22256ff52749f29fb0299681dda67f65608 Mon Sep 17 00:00:00 2001
From: m55624 
Date: Tue, 10 Oct 2017 17:20:34 +0100
Subject: [PATCH 37/72] gh-1297 - FederatedStore logic refactoring and test
 fixes

---
 .../java/uk/gov/gchq/gaffer/graph/Graph.java  | 41 ++++++++-----
 .../gov/gchq/gaffer/store/schema/Schema.java  |  2 +-
 .../gaffer/federatedstore/FederatedStore.java | 61 +++----------------
 .../FederatedStoreProperties.java             |  4 +-
 .../FederatedStorePublicAccessTest.java       |  4 +-
 .../federatedstore/FederatedStoreTest.java    | 61 ++++++++-----------
 .../FederatedStoreWrongGraphIDsTest.java      |  4 +-
 .../singleUseMockAccStore.properties          |  1 +
 .../singleUseMockMapStore.properties          |  1 +
 .../singleUseMockMapStoreAlt.properties       |  1 +
 .../resources/schema/basicEdgeSchema.json     |  1 +
 .../resources/schema/basicEntitySchema.json   |  1 +
 12 files changed, 70 insertions(+), 112 deletions(-)

diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java
index 7ae13846cd2..7efc54e6f73 100644
--- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java
+++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java
@@ -752,6 +752,7 @@ public Graph build() {
                 config.setGraphId(store.getGraphId());
             }
 
+            updateStoreProperties(config);
             updateSchema(config);
 
             if (null != config.getLibrary() && config.getLibrary().exists(config.getGraphId())) {
@@ -808,15 +809,17 @@ private void updateSchema(final GraphConfig config) {
 
             if (null != parentSchemaIds) {
                 for (final String parentSchemaId : parentSchemaIds) {
-                    final Schema parentSchema = config.getLibrary().getSchema(parentSchemaId);
-                    if (null != parentSchema) {
-                        if (null == mergedParentSchema) {
-                            mergedParentSchema = parentSchema;
-                        } else {
-                            mergedParentSchema = new Schema.Builder()
-                                    .merge(mergedParentSchema)
-                                    .merge(parentSchema)
-                                    .build();
+                    if (null != parentSchemaId) {
+                        final Schema parentSchema = config.getLibrary().getSchema(parentSchemaId);
+                        if (null != parentSchema) {
+                            if (null == mergedParentSchema) {
+                                mergedParentSchema = parentSchema;
+                            } else {
+                                mergedParentSchema = new Schema.Builder()
+                                        .merge(mergedParentSchema)
+                                        .merge(parentSchema)
+                                        .build();
+                            }
                         }
                     }
                 }
@@ -846,26 +849,32 @@ private void updateSchema(final GraphConfig config) {
             }
         }
 
-        private void updateStore(final GraphConfig config) {
+        private void updateStoreProperties(final GraphConfig config) {
             StoreProperties mergedStoreProperties = null;
             if (null != parentStorePropertiesId) {
                 mergedStoreProperties = config.getLibrary().getProperties(parentStorePropertiesId);
+                mergedStoreProperties.setId(parentStorePropertiesId);
             }
 
             if (null != properties) {
                 if (null == mergedStoreProperties) {
                     mergedStoreProperties = properties;
                 } else {
+                    if (null != properties.getId() && !mergedStoreProperties.getId().equals(properties.getId())) {
+                        mergedStoreProperties.setId(mergedStoreProperties.getId() + "," + properties.getId());
+                    }
                     mergedStoreProperties.getProperties().putAll(properties.getProperties());
-                    mergedStoreProperties.setId(mergedStoreProperties.getId() + "," + properties.getId());
                 }
             }
+            properties = mergedStoreProperties;
+        }
 
+        private void updateStore(final GraphConfig config) {
             if (null == store) {
-                store = Store.createStore(config.getGraphId(), cloneSchema(schema), mergedStoreProperties);
+                store = Store.createStore(config.getGraphId(), cloneSchema(schema), properties);
             } else if ((null != config.getGraphId() && !config.getGraphId().equals(store.getGraphId()))
                     || (null != schema)
-                    || (null != mergedStoreProperties && !mergedStoreProperties.equals(store.getProperties()))) {
+                    || (null != properties && !properties.equals(store.getProperties()))) {
                 if (null == config.getGraphId()) {
                     config.setGraphId(store.getGraphId());
                 }
@@ -873,12 +882,12 @@ private void updateStore(final GraphConfig config) {
                     schema = store.getSchema();
                 }
 
-                if (null == mergedStoreProperties) {
-                    mergedStoreProperties = store.getProperties();
+                if (null == properties) {
+                    properties = store.getProperties();
                 }
 
                 try {
-                    store.initialise(config.getGraphId(), cloneSchema(schema), mergedStoreProperties);
+                    store.initialise(config.getGraphId(), cloneSchema(schema), properties);
                 } catch (final StoreException e) {
                     throw new IllegalArgumentException("Unable to initialise the store with the given graphId, schema and properties", e);
                 }
diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/schema/Schema.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/schema/Schema.java
index 6308e960aed..f338b5068a2 100644
--- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/schema/Schema.java
+++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/schema/Schema.java
@@ -369,7 +369,7 @@ public CHILD_CLASS merge(final Schema schema) {
                 getThisSchema().setId(schema.getId());
             } else if (!StringUtils.isEmpty(schema.getId()) && !getThisSchema().getId().equals(schema.getId())) {
                 // Both schemas have an id, as we are creating a new schema we need to create a new schema ID.
-                getThisSchema().setId(getThisSchema().getId() + "," + schema.getId());
+                getThisSchema().setId(getThisSchema().getId() + "_" + schema.getId());
             }
 
             if (getThisSchema().getEntities().isEmpty()) {
diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java
index c0f127a1db7..e090c4757f7 100644
--- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java
+++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java
@@ -366,7 +366,7 @@ private void loadGraphs() throws StoreException {
             if (federatedStoreCache.getAllGraphIds().contains(graphId)) {
                 try {
                     Graph graph = federatedStoreCache.getFromCache(graphId);
-                    addGraphs(resolveAuths(graphId), null, graph);
+                    addGraphs(resolveAuths(graphId), null, resolveIsPublic(graphId), graph);
                 } catch (final CacheOperationException e) {
                     throw new RuntimeException(e);
                 }
@@ -374,13 +374,14 @@ private void loadGraphs() throws StoreException {
                 final Builder builder = new Builder().config(new GraphConfig.Builder()
                         .graphId(graphId)
                         .library(getGraphLibrary())
-                        .build());
+                        .build())
+                        .addParentSchemaIds(getValueOf(graphId, SCHEMA, ID))
+                        .parentStorePropertiesId(getValueOf(graphId, PROPERTIES, ID));
 
-                resolveConfiguration(graphId, builder);
+                addPropertiesFromFile(graphId, builder);
+                addSchemaFromFile(graphId, builder);
 
-                final Set auths = resolveAuths(graphId);
-                final boolean isPublic = resolveIsPublic(graphId);
-                addGraphs(auths, null, isPublic, builder);
+                addGraphs(resolveAuths(graphId), null, resolveIsPublic(graphId), builder);
             }
         }
     }
@@ -389,31 +390,11 @@ private boolean resolveIsPublic(final String graphId) {
         return Boolean.valueOf(getProperties().getGraphIsPublicValue(graphId));
     }
 
-    private void resolveConfiguration(final String graphId, final Builder builder) {
-        resolveSchema(graphId, builder);
-
-        resolveProperties(graphId, builder);
-    }
-
     private Set resolveAuths(final String graphId) {
         final String value = getProperties().getGraphAuthsValue(graphId);
         return Strings.isNullOrEmpty(value) ? null : Sets.newHashSet(getCleanStrings(value));
     }
 
-    private void resolveProperties(final String graphId, final Builder builder) {
-        addPropertiesFromLibrary(graphId, builder);
-
-        //this method is allowed to override properties from file
-        addPropertiesFromFile(graphId, builder);
-    }
-
-    private void resolveSchema(final String graphId, final Builder builder) {
-        addSchemaFromLibrary(graphId, builder);
-
-        //this method is allowed to override schema from file
-        addSchemaFromFile(graphId, builder);
-    }
-
     private void addGraphs(final Set graphAuths,
                            final String userId, final boolean isPublic, final Builder... builders) throws StoreException {
 
@@ -428,20 +409,6 @@ private void addGraphs(final Set graphAuths,
         }
     }
 
-    private void addSchemaFromLibrary(final String graphId, final Builder builder) {
-        final String schemaIdValue = getValueOf(graphId, SCHEMA, ID);
-        if (!Strings.isNullOrEmpty(schemaIdValue)) {
-            final GraphLibrary graphLibrary = getGraphLibrary();
-            if (null != graphLibrary) {
-                try {
-                    builder.addSchema(graphLibrary.getSchema(schemaIdValue));
-                } catch (final Exception e) {
-                    throw new IllegalArgumentException(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Schema", "graphId: " + graphId + " schemaId: " + schemaIdValue), e);
-                }
-            }
-        }
-    }
-
     private void addSchemaFromFile(final String graphId, final Builder builder) {
         final String schemaFileValue = getValueOf(graphId, SCHEMA, FILE);
         if (!Strings.isNullOrEmpty(schemaFileValue)) {
@@ -460,17 +427,6 @@ private void addSchemaFromFile(final String graphId, final Builder builder) {
         }
     }
 
-    private void addPropertiesFromLibrary(final String graphId, final Builder builder) {
-        final String propIdValue = getValueOf(graphId, PROPERTIES, ID);
-        if (!Strings.isNullOrEmpty(propIdValue)) {
-            try {
-                builder.addStoreProperties(getGraphLibrary().getProperties(propIdValue));
-            } catch (final Exception e) {
-                throw new IllegalArgumentException(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Property", "graphId: " + graphId + " propertyId: " + propIdValue), e);
-            }
-        }
-    }
-
     private void addPropertiesFromFile(final String graphId, final Builder builder) {
         final String propFileValue = getValueOf(graphId, PROPERTIES, FILE);
         if (!Strings.isNullOrEmpty(propFileValue)) {
@@ -509,7 +465,8 @@ private void _add(final Graph newGraph, final FederatedAccess access) {
         }
 
         if (null != getGraphLibrary()) {
-            getGraphLibrary().add(graphId, newGraph.getSchema(), newGraph.getStoreProperties());
+            getGraphLibrary().addProperties(newGraph.getStoreProperties());
+            getGraphLibrary().addSchema(newGraph.getSchema());
         }
     }
 
diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java
index fbf6c62de4f..449d6252fb0 100644
--- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java
+++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java
@@ -112,12 +112,12 @@ public void setGraphSchemaFile(final String graphId, final String file) {
         set(key, file);
     }
 
-    public void setGraphPropId(final String graphId, final String id) {
+    public void setParentPropId(final String graphId, final String id) {
         final String key = getKeyGraphConfig(graphId, GraphConfigEnum.PROPERTIES, LocationEnum.ID);
         set(key, id);
     }
 
-    public void setGraphSchemaId(final String graphId, final String id) {
+    public void setParentSchemaId(final String graphId, final String id) {
         final String key = getKeyGraphConfig(graphId, GraphConfigEnum.SCHEMA, LocationEnum.ID);
         set(key, id);
     }
diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java
index 2be4dcbb2e4..db652d00a0f 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java
@@ -44,8 +44,8 @@ public void setUp() throws Exception {
         fedProps = new FederatedStoreProperties();
         fedProps.setCacheProperties(CACHE_SERVICE_CLASS_STRING);
         fedProps.setGraphIds(GRAPH_1);
-        fedProps.setGraphPropId(GRAPH_1, PROP_1);
-        fedProps.setGraphSchemaId(GRAPH_1, SCHEMA_1);
+        fedProps.setParentPropId(GRAPH_1, PROP_1);
+        fedProps.setParentSchemaId(GRAPH_1, SCHEMA_1);
 
         store = new FederatedStore();
         library = new HashMapGraphLibrary();
diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
index d2810d138b2..61a506f563c 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
@@ -85,6 +85,7 @@ public class FederatedStoreTest {
     private static final String EXCEPTION_NOT_THROWN = "exception not thrown";
     private static final String USER_ID = "testUser";
     private static final String PROPS_ID_1 = "PROPS_ID_1";
+    private static final String PROPS_ID_2 = "PROPS_ID_2";
     public static final String UNUSUAL_KEY = "unusualKey";
     public static final String KEY_DOES_NOT_BELONG = UNUSUAL_KEY + " was added to " + PROPS_ID_1 + " it should not be there";
     private static final String SCHEMA_ID_1 = "SCHEMA_ID_1";
@@ -718,10 +719,10 @@ public void shouldAddNamedGraphsFromGraphIDKeyButDefinedInLibraryAndProperties()
     public void shouldAddGraphWithPropertiesFromGraphLibrary() throws Exception {
         // Given
         federatedProperties.setGraphIds(MAP_ID_1);
-        federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1);
+        federatedProperties.setParentPropId(MAP_ID_1, PROPS_ID_1);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
         final HashMapGraphLibrary library = new HashMapGraphLibrary();
-        library.add(PROPS_ID_1, new Schema(), StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES));
+        library.addProperties(StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES));
 
         // When
         store.setGraphLibrary(library);
@@ -737,12 +738,13 @@ public void shouldAddGraphWithSchemaFromGraphLibrary() throws Exception {
         // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
-        federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1);
+        federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_1);
         final HashMapGraphLibrary library = new HashMapGraphLibrary();
         final Schema schema = new Schema.Builder()
+                .id(SCHEMA_ID_1)
                 .json(StreamUtil.openStream(FederatedStore.class, PATH_BASIC_ENTITY_SCHEMA_JSON))
                 .build();
-        library.add(SCHEMA_ID_1, schema, new StoreProperties());
+        library.addSchema(schema);
 
         // When
         store.setGraphLibrary(library);
@@ -757,8 +759,8 @@ public void shouldAddGraphWithSchemaFromGraphLibrary() throws Exception {
     public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibrary() throws Exception {
         // Given
         federatedProperties.setGraphIds(MAP_ID_1);
-        federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1);
-        federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1);
+        federatedProperties.setParentPropId(MAP_ID_1, PROPS_ID_1);
+        federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_1);
         final Schema schema = new Schema.Builder()
                 .id(SCHEMA_ID_1)
                 .json(StreamUtil.openStream(FederatedStore.class, PATH_BASIC_ENTITY_SCHEMA_JSON))
@@ -782,7 +784,7 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibrary() throws Excep
     public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exception {
         // Given
         federatedProperties.setGraphIds(MAP_ID_1);
-        federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1);
+        federatedProperties.setParentPropId(MAP_ID_1, PROPS_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES_ALT);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
         final MapStoreProperties prop = new MapStoreProperties();
@@ -797,7 +799,7 @@ public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exce
 
         // Then
         assertEquals(1, store.getGraphs(testUser, null).size());
-        assertTrue(store.getGraphs(testUser, MAP_ID_1).iterator().next().getStoreProperties().containsKey(UNUSUAL_KEY));
+        assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().containsKey(UNUSUAL_KEY));
         assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY));
         assertEquals(prop.getProperties(), graphLibrary.getProperties(PROPS_ID_1).getProperties());
         assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty(UNUSUAL_KEY) != null);
@@ -810,13 +812,14 @@ public void shouldAddGraphWithSchemaFromGraphLibraryOverridden() throws Exceptio
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
-        federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1);
+        federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_1);
         final Schema schema = new Schema.Builder()
                 .id(SCHEMA_ID_1)
                 .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
                 .build();
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
         graphLibrary.addSchema(schema);
+        assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_1).toJson(false), schema.toJson(false)));
 
         // When
         store.setGraphLibrary(graphLibrary);
@@ -825,25 +828,29 @@ public void shouldAddGraphWithSchemaFromGraphLibraryOverridden() throws Exceptio
         // Then
         assertEquals(1, store.getGraphs(testUser, null).size());
         assertTrue(store.getGraphs(testUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity"));
-        assertTrue(graphLibrary.getSchema(SCHEMA_ID_1).equals(schema));
+        assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_1).toJson(false), schema.toJson(false)));
     }
 
     @Test
     public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibraryOverridden() throws Exception {
         // Given
         federatedProperties.setGraphIds(MAP_ID_1);
-        federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1);
-        federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
+        federatedProperties.setParentPropId(MAP_ID_1, PROPS_ID_1);
+        federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES_ALT);
         federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
-        federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1);
+        federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_1);
         final MapStoreProperties prop = new MapStoreProperties();
         final String unusualKey = UNUSUAL_KEY;
+        prop.setId(PROPS_ID_1);
         prop.set(unusualKey, "value");
         final Schema schema = new Schema.Builder()
+                .id(SCHEMA_ID_1)
                 .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
                 .build();
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
-        graphLibrary.add(MAP_ID_1, schema, prop);
+        graphLibrary.addSchema(schema);
+        graphLibrary.addProperties(prop);
+        assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY));
 
         // When
         store.setGraphLibrary(graphLibrary);
@@ -851,6 +858,8 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibraryOverridden() th
 
         // Then
         assertEquals(1, store.getGraphs(testUser, null).size());
+        assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().containsKey(UNUSUAL_KEY));
+        assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY));
         assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty(unusualKey) != null);
         assertTrue(store.getGraphs(testUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity"));
         assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(prop));
@@ -1040,34 +1049,12 @@ public void shouldAddGraphIdWithAuths() throws Exception {
         Assert.assertTrue(elements.iterator().hasNext());
     }
 
-    @Test
-    public void shouldThrowWithPropertiesErrorFromGraphLibrary() throws Exception {
-        // Given
-        federatedProperties.setGraphIds(MAP_ID_1);
-        federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1);
-        federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
-        final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class);
-        final String error = "Something went wrong";
-        Mockito.when(mockLibrary.getProperties(PROPS_ID_1)).thenThrow(new IllegalArgumentException(error));
-
-        store.setGraphLibrary(mockLibrary);
-
-        // When / Then
-        try {
-            store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
-            fail(EXCEPTION_NOT_THROWN);
-        } catch (IllegalArgumentException e) {
-            assertEquals(error, e.getCause().getMessage());
-        }
-        Mockito.verify(mockLibrary).getProperties(PROPS_ID_1);
-    }
-
     @Test
     public void shouldThrowWithSchemaErrorFromGraphLibrary() throws Exception {
         // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
-        federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1);
+        federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_1);
         final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class);
         final String error = "Something went wrong";
         Mockito.when(mockLibrary.getSchema(SCHEMA_ID_1)).thenThrow(new IllegalArgumentException(error));
diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java
index a768b08eb41..2637429686e 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java
@@ -57,8 +57,8 @@ public class FederatedStoreWrongGraphIDsTest {
     public void setUp() throws Exception {
         fedProps = new FederatedStoreProperties();
         fedProps.setGraphIds(GRAPH_1);
-        fedProps.setGraphPropId(GRAPH_1, PROP_1);
-        fedProps.setGraphSchemaId(GRAPH_1, SCHEMA_1);
+        fedProps.setParentPropId(GRAPH_1, PROP_1);
+        fedProps.setParentSchemaId(GRAPH_1, SCHEMA_1);
         fedProps.setTrueGraphIsPublicValue(GRAPH_1);
 
         store = new FederatedStore();
diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties
index 364120debad..9007e63a1fe 100644
--- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties
+++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties
@@ -13,6 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
+gaffer.store.id=PROPS_ID_1
 gaffer.store.class=uk.gov.gchq.gaffer.accumulostore.SingleUseMockAccumuloStore
 gaffer.store.properties.class=uk.gov.gchq.gaffer.accumulostore.AccumuloProperties
 accumulo.instance=mockInstanceID1234
diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties
index ef5df3c6aa5..36056340505 100644
--- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties
+++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties
@@ -13,5 +13,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
+gaffer.store.id=PROPS_ID_1
 gaffer.store.class=uk.gov.gchq.gaffer.mapstore.SingleUseMapStore
 gaffer.store.properties.class=uk.gov.gchq.gaffer.mapstore.MapStoreProperties
diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties
index 921dc1f8a7b..8f52a82d6f0 100644
--- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties
+++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties
@@ -13,6 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
+gaffer.store.id=PROPS_ID_2
 gaffer.store.class=uk.gov.gchq.gaffer.mapstore.SingleUseMapStore
 gaffer.store.properties.class=uk.gov.gchq.gaffer.mapstore.MapStoreProperties
 unusualKey=value
diff --git a/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json b/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json
index 30dcd3d214c..070838330c8 100644
--- a/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json
+++ b/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json
@@ -13,6 +13,7 @@
       ]
     }
   },
+  "id": "SCHEMA_ID_2",
   "types": {
     "vertex.string": {
       "class": "java.lang.String"
diff --git a/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json b/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json
index 76580692b70..4febb8999ca 100644
--- a/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json
+++ b/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json
@@ -7,6 +7,7 @@
       }
     }
   },
+  "id": "SCHEMA_ID_2",
   "types": {
     "vertex.string": {
       "class": "java.lang.String"

From 51ebdecf739e2862dfc7e16066667f585be2547b Mon Sep 17 00:00:00 2001
From: m55624 
Date: Wed, 11 Oct 2017 10:17:03 +0100
Subject: [PATCH 38/72] gh-1297 - all FederatedStore tests now working

---
 .../java/uk/gov/gchq/gaffer/graph/Graph.java  |  7 ++-
 .../federatedstore/FederatedStoreTest.java    | 60 +++++++++++--------
 .../resources/schema/basicEdgeSchema.json     |  1 -
 .../resources/schema/basicEntitySchema.json   |  1 -
 .../schema/basicEntitySchemaWithSchemaId.json | 25 ++++++++
 5 files changed, 63 insertions(+), 31 deletions(-)
 create mode 100644 store-implementation/federated-store/src/test/resources/schema/basicEntitySchemaWithSchemaId.json

diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java
index 7efc54e6f73..82f2bb41a64 100644
--- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java
+++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java
@@ -753,6 +753,7 @@ public Graph build() {
             }
 
             updateStoreProperties(config);
+
             updateSchema(config);
 
             if (null != config.getLibrary() && config.getLibrary().exists(config.getGraphId())) {
@@ -860,11 +861,11 @@ private void updateStoreProperties(final GraphConfig config) {
                 if (null == mergedStoreProperties) {
                     mergedStoreProperties = properties;
                 } else {
-                    if (null != properties.getId() && !mergedStoreProperties.getId().equals(properties.getId())) {
-                        mergedStoreProperties.setId(mergedStoreProperties.getId() + "," + properties.getId());
-                    }
                     mergedStoreProperties.getProperties().putAll(properties.getProperties());
                 }
+                if (null != properties.getId() && !mergedStoreProperties.getId().equals(properties.getId())) {
+                    mergedStoreProperties.setId(mergedStoreProperties.getId() + "_" + properties.getId());
+                }
             }
             properties = mergedStoreProperties;
         }
diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
index 61a506f563c..d4894c35724 100644
--- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
+++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java
@@ -80,15 +80,16 @@ public class FederatedStoreTest {
     private static final String PATH_MAP_STORE_PROPERTIES = "properties/singleUseMockMapStore.properties";
     private static final String PATH_MAP_STORE_PROPERTIES_ALT = "properties/singleUseMockMapStoreAlt.properties";
     private static final String PATH_BASIC_ENTITY_SCHEMA_JSON = "schema/basicEntitySchema.json";
+    private static final String PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON = "schema/basicEntitySchemaWithSchemaId.json";
     private static final String PATH_BASIC_EDGE_SCHEMA_JSON = "schema/basicEdgeSchema.json";
     private static final String PATH_INVALID = "nothing.json";
     private static final String EXCEPTION_NOT_THROWN = "exception not thrown";
     private static final String USER_ID = "testUser";
     private static final String PROPS_ID_1 = "PROPS_ID_1";
-    private static final String PROPS_ID_2 = "PROPS_ID_2";
     public static final String UNUSUAL_KEY = "unusualKey";
     public static final String KEY_DOES_NOT_BELONG = UNUSUAL_KEY + " was added to " + PROPS_ID_1 + " it should not be there";
     private static final String SCHEMA_ID_1 = "SCHEMA_ID_1";
+    private static final String SCHEMA_ID_2 = "SCHEMA_ID_2";
     private static final String ALL_USERS = FederatedStoreUser.ALL_USERS;
     private static final HashSet GRAPH_AUTHS = Sets.newHashSet(ALL_USERS);
     private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService";
@@ -639,6 +640,7 @@ public void shouldNotUseSchema() throws Exception {
     public void shouldAddGraphFromLibrary() throws Exception {
         // Given
         final Schema schema = new Schema.Builder()
+                .id(SCHEMA_ID_1)
                 .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
                 .build();
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
@@ -663,12 +665,13 @@ public void shouldAddGraphFromLibrary() throws Exception {
     public void shouldAddNamedGraphFromGraphIDKeyButDefinedInLibrary() throws Exception {
         // Given
         federatedProperties.setGraphIds(MAP_ID_1);
-        federatedProperties.setGraphIds(MAP_ID_1);
+        final Schema schema = new Schema.Builder()
+                .id(SCHEMA_ID_1)
+                .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
+                .build();
+        final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES));
         GraphLibrary library = new HashMapGraphLibrary();
-        library.add(MAP_ID_1, new Schema.Builder()
-                        .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
-                        .build(),
-                StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)));
+        library.add(MAP_ID_1, schema, storeProperties);
         store.setGraphLibrary(library);
         store.initialise(FEDERATED_STORE_ID, null, federatedProperties);
 
@@ -682,7 +685,7 @@ public void shouldAddGraphFromGraphIDKeyButDefinedProperties() throws Exception
         // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_ACC_STORE_PROPERTIES);
-        federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON);
+        federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON);
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
         store.setGraphLibrary(graphLibrary);
 
@@ -699,8 +702,9 @@ public void shouldAddNamedGraphsFromGraphIDKeyButDefinedInLibraryAndProperties()
         // Given
         federatedProperties.setGraphIds(MAP_ID_1 + ", " + ACC_ID_1);
         federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES);
-        federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON);
+        federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON);
         final Schema schema = new Schema.Builder()
+                .id(SCHEMA_ID_1)
                 .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
                 .build();
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
@@ -720,7 +724,7 @@ public void shouldAddGraphWithPropertiesFromGraphLibrary() throws Exception {
         // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setParentPropId(MAP_ID_1, PROPS_ID_1);
-        federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
+        federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON);
         final HashMapGraphLibrary library = new HashMapGraphLibrary();
         library.addProperties(StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES));
 
@@ -759,8 +763,6 @@ public void shouldAddGraphWithSchemaFromGraphLibrary() throws Exception {
     public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibrary() throws Exception {
         // Given
         federatedProperties.setGraphIds(MAP_ID_1);
-        federatedProperties.setParentPropId(MAP_ID_1, PROPS_ID_1);
-        federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_1);
         final Schema schema = new Schema.Builder()
                 .id(SCHEMA_ID_1)
                 .json(StreamUtil.openStream(FederatedStore.class, PATH_BASIC_ENTITY_SCHEMA_JSON))
@@ -776,6 +778,8 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibrary() throws Excep
 
         // Then
         assertEquals(1, store.getGraphs(testUser, null).size());
+        graphLibrary.getSchema(SCHEMA_ID_1).toJson(false);
+        schema.toJson(false);
         assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_1).toJson(false), schema.toJson(false)));
         assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(properties));
     }
@@ -786,7 +790,7 @@ public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exce
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setParentPropId(MAP_ID_1, PROPS_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES_ALT);
-        federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
+        federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON);
         final MapStoreProperties prop = new MapStoreProperties();
         prop.setId(PROPS_ID_1);
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
@@ -811,15 +815,15 @@ public void shouldAddGraphWithSchemaFromGraphLibraryOverridden() throws Exceptio
         // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES);
-        federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
-        federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_1);
+        federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON);
+        federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_2);
         final Schema schema = new Schema.Builder()
-                .id(SCHEMA_ID_1)
-                .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
+                .id(SCHEMA_ID_2)
+                .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_EDGE_SCHEMA_JSON))
                 .build();
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
         graphLibrary.addSchema(schema);
-        assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_1).toJson(false), schema.toJson(false)));
+        assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_2).toJson(false), schema.toJson(false)));
 
         // When
         store.setGraphLibrary(graphLibrary);
@@ -828,7 +832,7 @@ public void shouldAddGraphWithSchemaFromGraphLibraryOverridden() throws Exceptio
         // Then
         assertEquals(1, store.getGraphs(testUser, null).size());
         assertTrue(store.getGraphs(testUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity"));
-        assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_1).toJson(false), schema.toJson(false)));
+        assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_2).toJson(false), schema.toJson(false)));
     }
 
     @Test
@@ -836,20 +840,23 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibraryOverridden() th
         // Given
         federatedProperties.setGraphIds(MAP_ID_1);
         federatedProperties.setParentPropId(MAP_ID_1, PROPS_ID_1);
+        federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_2);
+
         federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES_ALT);
-        federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON);
-        federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_1);
+        federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON);
+
         final MapStoreProperties prop = new MapStoreProperties();
-        final String unusualKey = UNUSUAL_KEY;
         prop.setId(PROPS_ID_1);
-        prop.set(unusualKey, "value");
+
         final Schema schema = new Schema.Builder()
-                .id(SCHEMA_ID_1)
-                .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
+                .id(SCHEMA_ID_2)
+                .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_EDGE_SCHEMA_JSON))
                 .build();
+
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
         graphLibrary.addSchema(schema);
         graphLibrary.addProperties(prop);
+
         assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY));
 
         // When
@@ -860,10 +867,10 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibraryOverridden() th
         assertEquals(1, store.getGraphs(testUser, null).size());
         assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().containsKey(UNUSUAL_KEY));
         assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY));
-        assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty(unusualKey) != null);
+        assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty(UNUSUAL_KEY) != null);
         assertTrue(store.getGraphs(testUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity"));
         assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(prop));
-        assertTrue(graphLibrary.getSchema(SCHEMA_ID_1).equals(schema));
+        assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_2).toJson(false), schema.toJson(false)));
     }
 
     @Test
@@ -979,6 +986,7 @@ public void shouldAddGraphIdWithAuths() throws Exception {
         final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary();
         graphLibrary.add(MAP_ID_1,
                 new Schema.Builder()
+                        .id(SCHEMA_ID_1)
                         .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON))
                         .build(),
                 StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)));
diff --git a/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json b/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json
index 070838330c8..30dcd3d214c 100644
--- a/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json
+++ b/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json
@@ -13,7 +13,6 @@
       ]
     }
   },
-  "id": "SCHEMA_ID_2",
   "types": {
     "vertex.string": {
       "class": "java.lang.String"
diff --git a/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json b/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json
index 4febb8999ca..76580692b70 100644
--- a/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json
+++ b/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json
@@ -7,7 +7,6 @@
       }
     }
   },
-  "id": "SCHEMA_ID_2",
   "types": {
     "vertex.string": {
       "class": "java.lang.String"
diff --git a/store-implementation/federated-store/src/test/resources/schema/basicEntitySchemaWithSchemaId.json b/store-implementation/federated-store/src/test/resources/schema/basicEntitySchemaWithSchemaId.json
new file mode 100644
index 00000000000..6a9bc52ea64
--- /dev/null
+++ b/store-implementation/federated-store/src/test/resources/schema/basicEntitySchemaWithSchemaId.json
@@ -0,0 +1,25 @@
+{
+  "id": "SCHEMA_ID_1",
+  "entities": {
+    "BasicEntity": {
+      "vertex": "vertex.string",
+      "properties": {
+        "property1": "simpleProperty"
+      }
+    }
+  },
+  "types": {
+    "vertex.string": {
+      "class": "java.lang.String"
+    },
+    "simpleProperty": {
+      "class": "java.lang.Integer",
+      "aggregateFunction": {
+        "class": "uk.gov.gchq.koryphe.impl.binaryoperator.Sum"
+      },
+      "serialiser": {
+        "class": "uk.gov.gchq.gaffer.serialisation.implementation.raw.CompactRawIntegerSerialiser"
+      }
+    }
+  }
+}
\ No newline at end of file

From 75694babe8781d1a3853f0f10f1aac4f5caa7491 Mon Sep 17 00:00:00 2001
From: m55624 
Date: Wed, 11 Oct 2017 11:14:55 +0100
Subject: [PATCH 39/72] gh-1297 - fixed all other broken tests

---
 .../uk/gov/gchq/gaffer/federatedstore/FederatedStore.java    | 5 ++---
 .../federatedstore/FederatedStoreGraphVisibilityTest.java    | 5 +++++
 .../federatedstore/FederatedStoreWrongGraphIDsTest.java      | 2 ++
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java
index e090c4757f7..a15ec5b28e2 100644
--- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java
+++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java
@@ -59,7 +59,6 @@
 import uk.gov.gchq.gaffer.store.StoreException;
 import uk.gov.gchq.gaffer.store.StoreProperties;
 import uk.gov.gchq.gaffer.store.StoreTrait;
-import uk.gov.gchq.gaffer.store.library.GraphLibrary;
 import uk.gov.gchq.gaffer.store.operation.handler.OperationHandler;
 import uk.gov.gchq.gaffer.store.operation.handler.OutputOperationHandler;
 import uk.gov.gchq.gaffer.store.schema.Schema;
@@ -82,7 +81,7 @@
  * against them and returns results as though it was a single graph.
  * 

* To create a FederatedStore you need to initialise the store with a - * graphId and (if graphId is not known by the {@link GraphLibrary}) the + * graphId and (if graphId is not known by the {@link uk.gov.gchq.gaffer.store.library.GraphLibrary}) the * {@link * Schema} and {@link StoreProperties}. * @@ -256,7 +255,7 @@ public Collection getGraphs(final User user, final String graphIdsCsv) { /** * The FederatedStore at time of initialisation, can set the auths required * to allow users to use custom {@link StoreProperties} outside the - * scope of the {@link GraphLibrary}. + * scope of the {@link uk.gov.gchq.gaffer.store.library.GraphLibrary}. * * @param user the user needing validation for custom property usage. * @return boolean permission diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreGraphVisibilityTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreGraphVisibilityTest.java index cbf0902e3e4..092f41e8629 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreGraphVisibilityTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreGraphVisibilityTest.java @@ -32,6 +32,7 @@ import uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition; import uk.gov.gchq.gaffer.user.User; +import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; @@ -60,6 +61,7 @@ public void setUp() throws Exception { final HashMapGraphLibrary library = new HashMapGraphLibrary(); final Schema aSchema = new Schema.Builder() + .id("b") .entity("e1", new SchemaEntityDefinition.Builder() .vertex("string") .build()) @@ -67,6 +69,7 @@ public void setUp() throws Exception { .build(); final AccumuloProperties accProp = new AccumuloProperties(); + accProp.setId("a"); accProp.setStoreClass(SingleUseMockAccumuloStore.class.getName()); accProp.setStorePropertiesClass(AccumuloProperties.class); @@ -91,6 +94,7 @@ public void shouldNotShowHiddenGraphId() throws Exception { new AddGraph.Builder() .graphId("g1") .parentPropertiesId("a") + .parentSchemaIds(Arrays.asList("b")) .build(), addingUser); @@ -98,6 +102,7 @@ public void shouldNotShowHiddenGraphId() throws Exception { new AddGraph.Builder() .graphId("g2") .parentPropertiesId("a") + .parentSchemaIds(Arrays.asList("b")) .graphAuths("auth1") .build(), addingUser); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java index 2637429686e..f7eafb39462 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java @@ -47,6 +47,7 @@ public class FederatedStoreWrongGraphIDsTest { public static final String THERE_SHOULD_BE_ONE_ELEMENT = "There should be one element"; public static final String EXCEPTION_NOT_AS_EXPECTED = "Exception not as expected"; public static final String USING_THE_WRONG_GRAPH_ID_SHOULD_HAVE_THROWN_EXCEPTION = "Using the wrong graphId should have thrown exception."; + private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; private FederatedStore store; private FederatedStoreProperties fedProps; private HashMapGraphLibrary library; @@ -60,6 +61,7 @@ public void setUp() throws Exception { fedProps.setParentPropId(GRAPH_1, PROP_1); fedProps.setParentSchemaId(GRAPH_1, SCHEMA_1); fedProps.setTrueGraphIsPublicValue(GRAPH_1); + fedProps.setCacheProperties(CACHE_SERVICE_CLASS_STRING); store = new FederatedStore(); library = new HashMapGraphLibrary(); From be5b2d52dcbea248ab08998ed1946c253ece7ce0 Mon Sep 17 00:00:00 2001 From: m55624 Date: Wed, 11 Oct 2017 14:02:45 +0100 Subject: [PATCH 40/72] gh-1297 - invalid cache class string test added --- .../gaffer/federatedstore/FederatedStoreTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index d4894c35724..8e23d00ec4e 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -93,6 +93,7 @@ public class FederatedStoreTest { private static final String ALL_USERS = FederatedStoreUser.ALL_USERS; private static final HashSet GRAPH_AUTHS = Sets.newHashSet(ALL_USERS); private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; + private static final String INVALID_CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.invalid"; private static final String CACHE_SERVICE_NAME = "federatedStoreGraphs"; private static User authUser; private static User testUser; @@ -1104,6 +1105,17 @@ private boolean checkUnexpected(final Collection unexpectedGraphs, final return false; } + @Test + public void shouldThrowExceptionWithInvalidCacheClass() throws StoreException { + federatedProperties.setCacheProperties(INVALID_CACHE_SERVICE_CLASS_STRING); + try { + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + fail(EXCEPTION_NOT_THROWN); + } catch (final IllegalArgumentException e) { + assertTrue(e.getMessage().contains("Failed to instantiate cache")); + } + } + private List> populateGraphs(int... expectedIds) throws StoreException { final Collection expectedGraphs = new ArrayList<>(); final Collection unexpectedGraphs = new ArrayList<>(); From 395f75deaac58a3c1d38354330c3f33aa3d8c1a5 Mon Sep 17 00:00:00 2001 From: m55624 Date: Wed, 11 Oct 2017 14:24:02 +0100 Subject: [PATCH 41/72] gh-1297 - FederatedStoreCache exception updates and test updates --- .../federatedstore/FederatedStoreCache.java | 16 ++------------ .../FederatedStoreCacheTest.java | 21 ++++++++++--------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java index f2d7a9ec897..1e664e4f4e9 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java @@ -78,19 +78,11 @@ public void addGraphToCache(final Graph graph, final boolean overwrite) throws C * * @param graphId the ID of the {@link uk.gov.gchq.gaffer.graph.Graph} to retrieve * @return the {@link uk.gov.gchq.gaffer.graph.Graph} related to the specified ID - * @throws CacheOperationException if there was an error trying to access the cache */ - public Graph getFromCache(final String graphId) throws CacheOperationException { - if (null == graphId) { - throw new CacheOperationException("Graph ID cannot be null"); - } - + public Graph getFromCache(final String graphId) { final GraphSerialisable graphSerialisable = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); - if (null != graphSerialisable) { - return graphSerialisable.buildGraph(); - } - throw new CacheOperationException("No graph in the cache with Graph ID: " + graphId); + return (null == graphSerialisable) ? null : graphSerialisable.buildGraph(); } /** @@ -100,10 +92,6 @@ public Graph getFromCache(final String graphId) throws CacheOperationException { * @throws CacheOperationException if there was an error trying to delete from the cache */ public void deleteFromCache(final String graphId) throws CacheOperationException { - if (null == graphId) { - throw new CacheOperationException("Graph ID cannot be null"); - } - CacheServiceLoader.getService().removeFromCache(CACHE_SERVICE_NAME, graphId); if (null != CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId)) { diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java index ea229082564..d6fe3e782a8 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java @@ -20,30 +20,38 @@ import org.junit.BeforeClass; import org.junit.Test; +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; +import uk.gov.gchq.gaffer.cache.util.CacheProperties; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.graph.Graph; import uk.gov.gchq.gaffer.graph.GraphConfig; +import java.util.Properties; import java.util.Set; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; public class FederatedStoreCacheTest { private static final String PATH_MAP_STORE_PROPERTIES = "properties/singleUseMockMapStore.properties"; private static final String PATH_BASIC_EDGE_SCHEMA_JSON = "schema/basicEdgeSchema.json"; + private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; private static final String MAP_ID_1 = "mockMapGraphId1"; private Graph testGraph = new Graph.Builder().config(new GraphConfig(MAP_ID_1)) .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) .build(); private static FederatedStoreCache federatedStoreCache; + private static Properties properties = new Properties(); @BeforeClass public static void setUp() { + properties.setProperty(CacheProperties.CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_STRING); + CacheServiceLoader.initialise(properties); federatedStoreCache = new FederatedStoreCache(); } @@ -96,20 +104,13 @@ public void shouldThrowExceptionIfGraphAlreadyExistsInCache() throws CacheOperat @Test public void shouldThrowExceptionIfGraphIdToBeRemovedIsNull() throws CacheOperationException { federatedStoreCache.addGraphToCache(testGraph, false); - try { - federatedStoreCache.deleteFromCache(null); - } catch (CacheOperationException e) { - assertTrue(e.getMessage().contains("Graph ID cannot be null")); - } + federatedStoreCache.deleteFromCache(null); + assertEquals(1, federatedStoreCache.getAllGraphIds().size()); } @Test public void shouldThrowExceptionIfGraphIdToGetIsNull() throws CacheOperationException { federatedStoreCache.addGraphToCache(testGraph, false); - try { - federatedStoreCache.getFromCache(null); - } catch (CacheOperationException e) { - assertTrue(e.getMessage().contains("Graph ID cannot be null")); - } + assertNull(federatedStoreCache.getFromCache(null)); } } From 5117cd49ce1a8c6bee5a2404f773fb3ad2edcea0 Mon Sep 17 00:00:00 2001 From: m55624 Date: Wed, 11 Oct 2017 14:38:12 +0100 Subject: [PATCH 42/72] gh-1297 - FederatedStore fix --- .../uk/gov/gchq/gaffer/federatedstore/FederatedStore.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index a15ec5b28e2..91ef35bc703 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -363,12 +363,8 @@ private void loadGraphs() throws StoreException { final Set graphIds = getGraphIds(); for (final String graphId : graphIds) { if (federatedStoreCache.getAllGraphIds().contains(graphId)) { - try { - Graph graph = federatedStoreCache.getFromCache(graphId); - addGraphs(resolveAuths(graphId), null, resolveIsPublic(graphId), graph); - } catch (final CacheOperationException e) { - throw new RuntimeException(e); - } + Graph graph = federatedStoreCache.getFromCache(graphId); + addGraphs(resolveAuths(graphId), null, resolveIsPublic(graphId), graph); } else { final Builder builder = new Builder().config(new GraphConfig.Builder() .graphId(graphId) From c8270ba2d271f750bbb1604f3de4939321d93bab Mon Sep 17 00:00:00 2001 From: m55624 Date: Thu, 12 Oct 2017 09:36:29 +0100 Subject: [PATCH 43/72] gh-1297 - library addition in FedStore fixes --- .../gaffer/federatedstore/FederatedStore.java | 3 +-- .../FederatedAccessAuthTest.java | 1 + .../FederatedStoreGraphVisibilityTest.java | 22 +++++++++++-------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 91ef35bc703..68d05335893 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -460,8 +460,7 @@ private void _add(final Graph newGraph, final FederatedAccess access) { } if (null != getGraphLibrary()) { - getGraphLibrary().addProperties(newGraph.getStoreProperties()); - getGraphLibrary().addSchema(newGraph.getSchema()); + getGraphLibrary().add(newGraph.getGraphId(), newGraph.getSchema(), newGraph.getStoreProperties()); } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessAuthTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessAuthTest.java index 87f11a6299f..140eff4992b 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessAuthTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessAuthTest.java @@ -30,6 +30,7 @@ import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreUser.testUser; public class FederatedAccessAuthTest { + public static final String AuthX = "X"; User testUser; diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreGraphVisibilityTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreGraphVisibilityTest.java index 092f41e8629..2f147dde755 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreGraphVisibilityTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreGraphVisibilityTest.java @@ -45,12 +45,16 @@ public class FederatedStoreGraphVisibilityTest { + private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; + private static final String TEST_STORE_PROPS_ID = "testStorePropsId"; + private static final String TEST_SCHEMA_ID = "testSchemaId"; + private static final String TEST_GRAPH_ID = "testGraphId"; + private static final String TEST_FED_GRAPH_ID = "testFedGraphId"; private static User addingUser; private static User nonAddingUser; private static User authNonAddingUser; private Graph fedGraph; private FederatedStoreProperties fedProperties; - private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; @Before public void setUp() throws Exception { @@ -61,7 +65,7 @@ public void setUp() throws Exception { final HashMapGraphLibrary library = new HashMapGraphLibrary(); final Schema aSchema = new Schema.Builder() - .id("b") + .id(TEST_SCHEMA_ID) .entity("e1", new SchemaEntityDefinition.Builder() .vertex("string") .build()) @@ -69,15 +73,15 @@ public void setUp() throws Exception { .build(); final AccumuloProperties accProp = new AccumuloProperties(); - accProp.setId("a"); + accProp.setId(TEST_STORE_PROPS_ID); accProp.setStoreClass(SingleUseMockAccumuloStore.class.getName()); accProp.setStorePropertiesClass(AccumuloProperties.class); - library.add("a", aSchema, accProp); + library.add(TEST_GRAPH_ID, aSchema, accProp); fedGraph = new Builder() .config(new GraphConfig.Builder() - .graphId("testFedGraph") + .graphId(TEST_FED_GRAPH_ID) .library(library) .build()) .addStoreProperties(fedProperties) @@ -93,16 +97,16 @@ public void shouldNotShowHiddenGraphId() throws Exception { fedGraph.execute( new AddGraph.Builder() .graphId("g1") - .parentPropertiesId("a") - .parentSchemaIds(Arrays.asList("b")) + .parentPropertiesId(TEST_STORE_PROPS_ID) + .parentSchemaIds(Arrays.asList(TEST_SCHEMA_ID)) .build(), addingUser); fedGraph.execute( new AddGraph.Builder() .graphId("g2") - .parentPropertiesId("a") - .parentSchemaIds(Arrays.asList("b")) + .parentPropertiesId(TEST_STORE_PROPS_ID) + .parentSchemaIds(Arrays.asList(TEST_SCHEMA_ID)) .graphAuths("auth1") .build(), addingUser); From a88955142e77cab45d85d00a13cdf4be6fd25d92 Mon Sep 17 00:00:00 2001 From: m55624 Date: Thu, 12 Oct 2017 11:03:27 +0100 Subject: [PATCH 44/72] gh-1297 - updated javadoc and removed method only used in tests --- .../gaffer/federatedstore/FederatedStore.java | 42 +++++++++---------- .../federatedstore/FederatedStoreTest.java | 38 ++++++++--------- .../PredefinedFederatedStore.java | 4 +- .../impl/FederatedRemoveGraphHandlerTest.java | 4 +- 4 files changed, 43 insertions(+), 45 deletions(-) diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 68d05335893..0d0c04e7339 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -82,8 +82,7 @@ *

* To create a FederatedStore you need to initialise the store with a * graphId and (if graphId is not known by the {@link uk.gov.gchq.gaffer.store.library.GraphLibrary}) the - * {@link - * Schema} and {@link StoreProperties}. + * {@link Schema} and {@link StoreProperties}. * * @see #initialise(String, Schema, StoreProperties) * @see Store @@ -126,6 +125,11 @@ public void initialise(final String graphId, final Schema unused, final StorePro loadGraphs(); } + /** + * Get this Store's {@link uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties}. + * + * @return the instance of {@link uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties}, this may contain details such as database connection details. + */ @Override public FederatedStoreProperties getProperties() { return (FederatedStoreProperties) super.getProperties(); @@ -173,8 +177,7 @@ public static OP updateOperationForGraph(final OP operati * To be used by the FederatedStore and Handlers only. Users should add * graphs via the {@link AddGraph} operation. * public access will be ignored if the FederatedStore denies this action - * at - * initialisation, will default to usual access with addingUserId and + * at initialisation, will default to usual access with addingUserId and * graphAuths * * @param addingUserId the adding userId @@ -196,10 +199,6 @@ public void addGraphs(final Set graphAuths, final String addingUserId, f } } - public void addGraphs(final Set graphAuths, final String addingUserId, final Graph... graphs) throws StoreException { - addGraphs(graphAuths, addingUserId, false, graphs); - } - /** * Removes graphs from the scope of FederatedStore. *

@@ -228,7 +227,6 @@ public Collection getAllGraphIds(final User user) { return graphStorage.getAllIds(user); } - /** * @return {@link Store#getTraits()} */ @@ -313,6 +311,19 @@ protected Object doUnhandledOperation(final Operation operation, throw new UnsupportedOperationException(); } + @Override + protected void startCacheServiceLoader( + final StoreProperties properties) { + if (federatedStoreCache.getCache() == null) { + CacheServiceLoader.initialise(properties.getProperties()); + } + } + + @Override + public Schema getSchema() { + return graphStorage.getMergedSchema(); + } + private static View createValidView(final View view, final Schema delegateGraphSchema) { View newView; if (view.hasGroups()) { @@ -463,17 +474,4 @@ private void _add(final Graph newGraph, final FederatedAccess access) { getGraphLibrary().add(newGraph.getGraphId(), newGraph.getSchema(), newGraph.getStoreProperties()); } } - - @Override - protected void startCacheServiceLoader( - final StoreProperties properties) { - if (federatedStoreCache.getCache() == null) { - CacheServiceLoader.initialise(properties.getProperties()); - } - } - - @Override - public Schema getSchema() { - return graphStorage.getMergedSchema(); - } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index 8e23d00ec4e..e3300773d93 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -133,7 +133,7 @@ public void shouldThrowExceptionWithoutInitialisation() { // When / Then try { - store.addGraphs(null, TEST_USER, graphToAdd); + store.addGraphs(null, TEST_USER, false, graphToAdd); fail(EXCEPTION_NOT_THROWN); } catch (final StoreException e) { assertTrue(e.getMessage().contains("No cache has been set")); @@ -166,7 +166,7 @@ public void shouldAddGraphsToCache() throws StoreException { .build(); // When - store.addGraphs(null, TEST_USER, graphToAdd); + store.addGraphs(null, TEST_USER, false, graphToAdd); // Then assertEquals(1, store.getGraphs(testUser, ACC_ID_1).size()); @@ -200,7 +200,7 @@ public void shouldAddMultipleGraphsToCache() throws StoreException { } // When - store.addGraphs(null, TEST_USER, graphsToAdd.toArray(new Graph[graphsToAdd.size()])); + store.addGraphs(null, TEST_USER, false, graphsToAdd.toArray(new Graph[graphsToAdd.size()])); // Then for (int i = 0; i < 10; i++) { @@ -232,7 +232,7 @@ public void shouldReuseGraphsAlreadyInCache() throws Exception { .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) .build(); - store.addGraphs(null, TEST_USER, graphToAdd); + store.addGraphs(null, TEST_USER, false, graphToAdd); //check the store and the cache assertEquals(1, store.getAllGraphIds(testUser).size()); @@ -329,7 +329,7 @@ public void shouldNotAllowOverwritingOfGraphWithFederatedScope() throws Exceptio // When / Then try { - store.addGraphs(null, null, new Graph.Builder() + store.addGraphs(null, null, false, new Graph.Builder() .config(new GraphConfig(ACC_ID_1)) .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES)) .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) @@ -361,7 +361,7 @@ public void shouldUpdateTraitsWhenNewGraphIsAdded() throws Exception { Set before = store.getTraits(); // When - store.addGraphs(null, null, new Graph.Builder() + store.addGraphs(null, null, false, new Graph.Builder() .config(new GraphConfig(MAP_ID_1)) .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) @@ -387,7 +387,7 @@ public void shouldUpdateSchemaWhenNewGraphIsAdded() throws Exception { Schema before = store.getSchema(); // When - store.addGraphs(null, null, new Graph.Builder() + store.addGraphs(null, null, false, new Graph.Builder() .config(new GraphConfig(MAP_ID_1)) .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) @@ -585,7 +585,7 @@ public void shouldUpdateGraphIds() throws Exception { assertFalse(allGraphId.contains(MAP_ID_1)); // When - store.addGraphs(GRAPH_AUTHS, null, new Graph.Builder() + store.addGraphs(GRAPH_AUTHS, null, false, new Graph.Builder() .config(new GraphConfig(MAP_ID_1)) .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) @@ -901,7 +901,7 @@ public void shouldNotAllowOverridingOfKnownGraphInLibrary() throws Exception { public void shouldFederatedIfUserHasCorrectAuths() throws Exception { // Given store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - store.addGraphs(GRAPH_AUTHS, null, new Graph.Builder() + store.addGraphs(GRAPH_AUTHS, null, false, new Graph.Builder() .config(new GraphConfig.Builder() .graphId(MAP_ID_1) .build()) @@ -1096,15 +1096,6 @@ public void shouldReturnASingleGraph() throws StoreException { assertFalse(checkUnexpected(unexpectedGraphs, returnedGraphs)); } - private boolean checkUnexpected(final Collection unexpectedGraphs, final Collection returnedGraphs) { - for (Graph graph : unexpectedGraphs) { - if (returnedGraphs.contains(graph)) { - return true; - } - } - return false; - } - @Test public void shouldThrowExceptionWithInvalidCacheClass() throws StoreException { federatedProperties.setCacheProperties(INVALID_CACHE_SERVICE_CLASS_STRING); @@ -1116,6 +1107,15 @@ public void shouldThrowExceptionWithInvalidCacheClass() throws StoreException { } } + private boolean checkUnexpected(final Collection unexpectedGraphs, final Collection returnedGraphs) { + for (Graph graph : unexpectedGraphs) { + if (returnedGraphs.contains(graph)) { + return true; + } + } + return false; + } + private List> populateGraphs(int... expectedIds) throws StoreException { final Collection expectedGraphs = new ArrayList<>(); final Collection unexpectedGraphs = new ArrayList<>(); @@ -1127,7 +1127,7 @@ private List> populateGraphs(int... expectedIds) throws StoreE .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) .build(); - store.addGraphs(Sets.newHashSet(ALL_USERS), null, tempGraph); + store.addGraphs(Sets.newHashSet(ALL_USERS), null, false, tempGraph); for (final int j : expectedIds) { if (i == j) { expectedGraphs.add(tempGraph); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/PredefinedFederatedStore.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/PredefinedFederatedStore.java index 0c14d6f3436..2ea5cfb9d63 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/PredefinedFederatedStore.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/PredefinedFederatedStore.java @@ -33,7 +33,7 @@ public void initialise(final String graphId, final Schema schema, final StorePro super.initialise(graphId, schema, properties); // Accumulo store just contains edges - addGraphs(null, User.UNKNOWN_USER_ID, new Graph.Builder() + addGraphs(null, User.UNKNOWN_USER_ID, false, new Graph.Builder() .config(new GraphConfig("AccumuloStoreContainingEdges")) .addSchema(new Schema.Builder() .merge(schema) @@ -43,7 +43,7 @@ public void initialise(final String graphId, final Schema schema, final StorePro .build()); // Map store just contains entities - addGraphs(null, User.UNKNOWN_USER_ID, new Graph.Builder() + addGraphs(null, User.UNKNOWN_USER_ID, false, new Graph.Builder() .config(new GraphConfig("MapStoreContainingEntities")) .addSchema(new Schema.Builder() .merge(schema) diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java index e848fe2612b..cd08a3568c6 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java @@ -51,12 +51,12 @@ public void shouldRemoveGraph() throws Exception { FederatedStore store = new FederatedStore(); final FederatedStoreProperties federatedStoreProperties = new FederatedStoreProperties(); federatedStoreProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); - + store.initialise(FEDERATEDSTORE_GRAPH_ID, null, federatedStoreProperties); AccumuloProperties storeProperties = new AccumuloProperties(); storeProperties.setStoreClass(SingleUseMockAccumuloStore.class); - store.addGraphs(testUser.getOpAuths(), null, new Graph.Builder() + store.addGraphs(testUser.getOpAuths(), null, false, new Graph.Builder() .config(new GraphConfig(EXPECTED_GRAPH_ID)) .addSchema(new Schema.Builder().build()) .storeProperties(storeProperties) From b9e9d597e288f618b1aee7a71398025970926f18 Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Fri, 13 Oct 2017 17:19:00 +0100 Subject: [PATCH 45/72] gh-1297 PR Suggested Changes --- .../java/uk/gov/gchq/gaffer/graph/Graph.java | 4 +- .../gchq/gaffer/graph/GraphSerialisable.java | 1 - .../gchq/gaffer/store/StoreProperties.java | 2 +- .../gaffer/federatedstore/FederatedStore.java | 102 +++--- .../federatedstore/FederatedStoreCache.java | 10 +- .../FederatedStoreProperties.java | 14 +- .../impl/FederatedAddGraphHandler.java | 5 +- .../impl/FederatedRemoveGraphHandler.java | 5 +- .../FederatedStoreAuthTest.java | 4 +- .../FederatedStoreGraphVisibilityTest.java | 91 +++-- .../FederatedStorePublicAccessTest.java | 4 +- .../federatedstore/FederatedStoreTest.java | 317 ++++++++++-------- .../FederatedStoreWrongGraphIDsTest.java | 4 +- .../operation/AddGraphTest.java | 11 +- .../FederatedOperationHandlerTest.java | 2 +- .../impl/FederatedAddGraphHandlerTest.java | 5 +- .../gaffer/hbasestore/HBaseProperties.java | 8 +- .../gaffer/mapstore/MapStoreProperties.java | 8 +- .../parquetstore/ParquetStoreProperties.java | 8 +- .../gaffer/proxystore/ProxyProperties.java | 12 +- 20 files changed, 346 insertions(+), 271 deletions(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java index 82f2bb41a64..d2f8e8d6213 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java @@ -862,9 +862,7 @@ private void updateStoreProperties(final GraphConfig config) { mergedStoreProperties = properties; } else { mergedStoreProperties.getProperties().putAll(properties.getProperties()); - } - if (null != properties.getId() && !mergedStoreProperties.getId().equals(properties.getId())) { - mergedStoreProperties.setId(mergedStoreProperties.getId() + "_" + properties.getId()); + mergedStoreProperties.setId(null); } } properties = mergedStoreProperties; diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java index 001e8f09117..2da48f3c02a 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java @@ -37,7 +37,6 @@ */ public final class GraphSerialisable implements Serializable { private static final long serialVersionUID = 2684203367656032583L; - public static final String COULD_NOT_CONVERT_PROPERTIES_TO_THE_REQUIRED_PROPERTIES_CLASS_S = "Error while creating graph from GraphSerialisable, could not create the required Properties Class: %s"; private byte[] schema; private Properties properties; private byte[] config; diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/StoreProperties.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/StoreProperties.java index 36d92089c6b..308cd775263 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/StoreProperties.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/StoreProperties.java @@ -72,7 +72,7 @@ public class StoreProperties implements Cloneable { // Required for loading by reflection. public StoreProperties() { - setStorePropertiesClass(this.getClass()); + updateStorePropertiesClass(); } public StoreProperties(final String id) { diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 0d0c04e7339..65e258f6e52 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -22,7 +22,6 @@ import org.apache.commons.lang3.StringUtils; import uk.gov.gchq.gaffer.cache.CacheServiceLoader; -import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; import uk.gov.gchq.gaffer.cache.util.CacheProperties; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; @@ -89,7 +88,6 @@ * @see Graph */ public class FederatedStore extends Store { - public static final String USER_IS_ATTEMPTING_TO_OVERWRITE_A_GRAPH_WITHIN_FEDERATED_STORE_GRAPH_ID_S = "User is attempting to overwrite a graph within FederatedStore. GraphId: %s"; protected static final String S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2 = "%s was not able to be created with the supplied properties.%n%s"; private static final String SCHEMA_DEL_REGEX = Pattern.quote(","); private static final GraphConfigEnum SCHEMA = GraphConfigEnum.SCHEMA; @@ -113,10 +111,8 @@ public class FederatedStore extends Store { */ @Override public void initialise(final String graphId, final Schema unused, final StoreProperties properties) throws StoreException { - if (properties.get(CacheProperties.CACHE_SERVICE_CLASS) == null) { - throw new StoreException("No cache has been set, please check the property " - + CacheProperties.CACHE_SERVICE_CLASS + " has been set in the Store Properties"); - } + setProperties(properties); + validateCacheProperties(); super.initialise(graphId, new Schema(), properties); loadCustomPropertiesAuths(); @@ -128,7 +124,8 @@ public void initialise(final String graphId, final Schema unused, final StorePro /** * Get this Store's {@link uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties}. * - * @return the instance of {@link uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties}, this may contain details such as database connection details. + * @return the instance of {@link uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties}, + * this may contain details such as database connection details. */ @Override public FederatedStoreProperties getProperties() { @@ -187,16 +184,19 @@ public static OP updateOperationForGraph(final OP operati * @throws StoreException if no cache has been set */ - public void addGraphs(final Set graphAuths, final String addingUserId, final boolean isPublic, final Graph... graphs) throws StoreException { - if (federatedStoreCache.getCache() == null) { - throw new StoreException("No cache has been set, please initialise the FederatedStore instance"); - } + public void addGraphs(final Set graphAuths, final String addingUserId, final boolean isPublic, final Graph... graphs) { + validateCache(); - FederatedAccess access = new FederatedAccess(graphAuths, addingUserId, isPublicAccessAllowed && isPublic); + FederatedAccess access = new FederatedAccess(graphAuths, addingUserId, isPublicAccessAllowed && isPublic); - for (final Graph graph : graphs) { - _add(graph, access); - } + for (final Graph graph : graphs) { + _add(graph, access); + } + } + + @Deprecated + public void addGraphs(final Set graphAuths, final String addingUserId, final Graph... graphs) { + addGraphs(graphAuths, addingUserId, false, graphs); } /** @@ -207,14 +207,12 @@ public void addGraphs(final Set graphAuths, final String addingUserId, f * * @param graphId to be removed from scope * @param user to match visibility against - * @throws StoreException if there was an error removing the {@link uk.gov.gchq.gaffer.graph.Graph} from the store + * @throws StoreException if there was an error removing the {@link + * uk.gov.gchq.gaffer.graph.Graph} from the store */ - public void remove(final String graphId, final User user) throws StoreException { - try { - federatedStoreCache.deleteFromCache(graphId); - } catch (final CacheOperationException e) { - throw new StoreException(e); - } + public void remove(final String graphId, final User user) { + validateCache(); + federatedStoreCache.deleteFromCache(graphId); graphStorage.remove(graphId, user); } @@ -312,8 +310,7 @@ protected Object doUnhandledOperation(final Operation operation, } @Override - protected void startCacheServiceLoader( - final StoreProperties properties) { + protected void startCacheServiceLoader(final StoreProperties properties) { if (federatedStoreCache.getCache() == null) { CacheServiceLoader.initialise(properties.getProperties()); } @@ -373,21 +370,25 @@ private void loadCustomPropertiesAuths() { private void loadGraphs() throws StoreException { final Set graphIds = getGraphIds(); for (final String graphId : graphIds) { - if (federatedStoreCache.getAllGraphIds().contains(graphId)) { + final Set graphAuths = resolveAuths(graphId); + final boolean isPublic = resolveIsPublic(graphId); + + if (federatedStoreCache.contains(graphId)) { Graph graph = federatedStoreCache.getFromCache(graphId); - addGraphs(resolveAuths(graphId), null, resolveIsPublic(graphId), graph); + addGraphs(graphAuths, null, isPublic, graph); } else { - final Builder builder = new Builder().config(new GraphConfig.Builder() - .graphId(graphId) - .library(getGraphLibrary()) - .build()) + final Graph.Builder builder = new Builder() + .config(new GraphConfig.Builder() + .graphId(graphId) + .library(getGraphLibrary()) + .build()) .addParentSchemaIds(getValueOf(graphId, SCHEMA, ID)) .parentStorePropertiesId(getValueOf(graphId, PROPERTIES, ID)); addPropertiesFromFile(graphId, builder); addSchemaFromFile(graphId, builder); - addGraphs(resolveAuths(graphId), null, resolveIsPublic(graphId), builder); + addGraphs(graphAuths, null, isPublic, builder); } } } @@ -401,9 +402,7 @@ private Set resolveAuths(final String graphId) { return Strings.isNullOrEmpty(value) ? null : Sets.newHashSet(getCleanStrings(value)); } - private void addGraphs(final Set graphAuths, - final String userId, final boolean isPublic, final Builder... builders) throws StoreException { - + private void addGraphs(final Set graphAuths, final String userId, final boolean isPublic, final Builder... builders) throws StoreException { for (final Builder builder : builders) { final Graph graph; try { @@ -427,7 +426,7 @@ private void addSchemaFromFile(final String graphId, final Builder builder) { builder.addSchema(StreamUtil.openStream(FederatedStore.class, schemaPath)); } } catch (final Exception e) { - throw new IllegalArgumentException(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Schema", "graphId: " + graphId + " schemaPath: " + schemaPath), e); + throw new IllegalArgumentException(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Schema", "GraphId: " + graphId + " schemaPath: " + schemaPath), e); } } } @@ -439,7 +438,7 @@ private void addPropertiesFromFile(final String graphId, final Builder builder) try { builder.addStoreProperties(propFileValue); } catch (final Exception e) { - throw new IllegalArgumentException(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Property", "graphId: " + graphId + " propertyPath: " + propFileValue), e); + throw new IllegalArgumentException(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Property", "GraphId: " + graphId + " propertyPath: " + propFileValue), e); } } } @@ -457,10 +456,19 @@ private Set getGraphIds() { return graphIds; } - private void _add(final Graph newGraph, final FederatedAccess access) { + private void _add(final Graph newGraph, final FederatedAccess access){ + addToCache(newGraph); + graphStorage.put(newGraph, access); + + if (null != getGraphLibrary()) { + getGraphLibrary().add(newGraph.getGraphId(), newGraph.getSchema(), newGraph.getStoreProperties()); + } + } + + private void addToCache(final Graph newGraph) { final String graphId = newGraph.getGraphId(); - if (!federatedStoreCache.getAllGraphIds().contains(graphId)) { + if (!federatedStoreCache.contains(graphId)) { try { federatedStoreCache.addGraphToCache(newGraph, false); } catch (final OverwritingException e) { @@ -468,10 +476,24 @@ private void _add(final Graph newGraph, final FederatedAccess access) { } catch (final Exception e) { throw new RuntimeException(e); } + } else { + final Graph fromCache = federatedStoreCache.getFromCache(graphId); + if (!newGraph.equals(fromCache)) { + throw new RuntimeException(String.format("Error adding graph, GraphId is known within the cache, but is different. GraphId: %s", graphId)); + } } + } - if (null != getGraphLibrary()) { - getGraphLibrary().add(newGraph.getGraphId(), newGraph.getSchema(), newGraph.getStoreProperties()); + private void validateCacheProperties() throws StoreException { + if (getProperties().getCacheProperties() == null) { + throw new StoreException("No cache has been set, please check the property " + + CacheProperties.CACHE_SERVICE_CLASS + " has been set in the Store Properties"); + } + } + + private void validateCache() { + if (federatedStoreCache.getCache() == null) { + throw new RuntimeException("No cache has been set, please initialise the FederatedStore instance"); } } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java index 1e664e4f4e9..ad78d32aaf0 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java @@ -91,12 +91,8 @@ public Graph getFromCache(final String graphId) { * @param graphId the ID of the {@link uk.gov.gchq.gaffer.graph.Graph} to be deleted * @throws CacheOperationException if there was an error trying to delete from the cache */ - public void deleteFromCache(final String graphId) throws CacheOperationException { + public void deleteFromCache(final String graphId) { CacheServiceLoader.getService().removeFromCache(CACHE_SERVICE_NAME, graphId); - - if (null != CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId)) { - throw new CacheOperationException("Failed to remove Graph with ID: " + graphId + " from cache"); - } } /** @@ -107,4 +103,8 @@ public void deleteFromCache(final String graphId) throws CacheOperationException public void clearCache() throws CacheOperationException { CacheServiceLoader.getService().clearCache(CACHE_SERVICE_NAME); } + + public boolean contains(final String graphId) { + return getAllGraphIds().contains(graphId); + } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java index 449d6252fb0..31d5c513860 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java @@ -61,9 +61,11 @@ public class FederatedStoreProperties extends StoreProperties { */ private static final String IS_PUBLIC = "isPublic"; public static final String IS_PUBLIC_DEFAULT = String.valueOf(false); + public static final String CACHE_SERVICE_CLASS = CacheProperties.CACHE_SERVICE_CLASS; + public static final String CACHE_SERVICE_CLASS_DEFAULT = null; public FederatedStoreProperties() { - setStoreClass(FederatedStore.class); + super(FederatedStore.class); } public static FederatedStoreProperties loadStoreProperties(final String pathStr) { @@ -112,12 +114,12 @@ public void setGraphSchemaFile(final String graphId, final String file) { set(key, file); } - public void setParentPropId(final String graphId, final String id) { + public void setGraphPropId(final String graphId, final String id) { final String key = getKeyGraphConfig(graphId, GraphConfigEnum.PROPERTIES, LocationEnum.ID); set(key, id); } - public void setParentSchemaId(final String graphId, final String id) { + public void setGraphSchemaId(final String graphId, final String id) { final String key = getKeyGraphConfig(graphId, GraphConfigEnum.SCHEMA, LocationEnum.ID); set(key, id); } @@ -151,7 +153,11 @@ private String getKeyGraphIsPublic(final String graphId) { } public void setCacheProperties(final String cacheServiceClassString) { - set(CacheProperties.CACHE_SERVICE_CLASS, cacheServiceClassString); + set(CACHE_SERVICE_CLASS, cacheServiceClassString); + } + + public String getCacheProperties() { + return get(CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_DEFAULT); } /** diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java index 42f09bc8692..42e86796d66 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java @@ -23,7 +23,6 @@ import uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate; import uk.gov.gchq.gaffer.store.Context; import uk.gov.gchq.gaffer.store.Store; -import uk.gov.gchq.gaffer.store.StoreException; import uk.gov.gchq.gaffer.store.operation.handler.OperationHandler; import uk.gov.gchq.gaffer.user.User; @@ -50,8 +49,8 @@ public Void doOperation(final AddGraph operation, final Context context, final S try { ((FederatedStore) store).addGraphs(operation.getGraphAuths(), context.getUser().getUserId(), operation.getIsPublic(), graph); - } catch (final StoreException e) { - throw new OperationException(e.getMessage()); + } catch (final Exception e) { + throw new OperationException("Error adding graph: " + operation.getGraphId(), e); } return null; } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandler.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandler.java index 56683d8ab66..fade5e5166e 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandler.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandler.java @@ -21,7 +21,6 @@ import uk.gov.gchq.gaffer.operation.OperationException; import uk.gov.gchq.gaffer.store.Context; import uk.gov.gchq.gaffer.store.Store; -import uk.gov.gchq.gaffer.store.StoreException; import uk.gov.gchq.gaffer.store.operation.handler.OperationHandler; /** @@ -38,8 +37,8 @@ public class FederatedRemoveGraphHandler implements OperationHandler graphIds = fedGraph.execute( new GetAllGraphIds(), nonAddingUser); @@ -159,4 +211,5 @@ public void shouldNotShowHiddenGraphId() throws Exception { assertTrue(sets.contains("g2")); } + } \ No newline at end of file diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java index db652d00a0f..2be4dcbb2e4 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java @@ -44,8 +44,8 @@ public void setUp() throws Exception { fedProps = new FederatedStoreProperties(); fedProps.setCacheProperties(CACHE_SERVICE_CLASS_STRING); fedProps.setGraphIds(GRAPH_1); - fedProps.setParentPropId(GRAPH_1, PROP_1); - fedProps.setParentSchemaId(GRAPH_1, SCHEMA_1); + fedProps.setGraphPropId(GRAPH_1, PROP_1); + fedProps.setGraphSchemaId(GRAPH_1, SCHEMA_1); store = new FederatedStore(); library = new HashMapGraphLibrary(); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index e3300773d93..41e56dfab2a 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -114,141 +114,6 @@ public void setUp() throws Exception { testUser = testUser(); } - @Test - public void shouldInitialiseWithCache() throws StoreException { - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - assertNotNull(CacheServiceLoader.getService()); - } - - @Test - public void shouldThrowExceptionWithoutInitialisation() { - // Given - Graph graphToAdd = new Graph.Builder() - .config(new GraphConfig(ACC_ID_1)) - .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES)) - .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) - .build(); - - CacheServiceLoader.shutdown(); - - // When / Then - try { - store.addGraphs(null, TEST_USER, false, graphToAdd); - fail(EXCEPTION_NOT_THROWN); - } catch (final StoreException e) { - assertTrue(e.getMessage().contains("No cache has been set")); - } - } - - @Test - public void shouldThrowExceptionWhenInitialisedWithNoCacheClassInProperties() throws StoreException { - // Given - StoreProperties storeProperties = new StoreProperties(); - - // When / Then - try { - store.initialise(FEDERATED_STORE_ID, null, storeProperties); - fail(EXCEPTION_NOT_THROWN); - } catch (final StoreException e) { - assertTrue(e.getMessage().contains("No cache has been set, please check the property")); - } - } - - @Test - public void shouldAddGraphsToCache() throws StoreException { - // Given - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - - Graph graphToAdd = new Graph.Builder() - .config(new GraphConfig(ACC_ID_1)) - .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES)) - .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) - .build(); - - // When - store.addGraphs(null, TEST_USER, false, graphToAdd); - - // Then - assertEquals(1, store.getGraphs(testUser, ACC_ID_1).size()); - - // When - Collection storeGraphs = store.getGraphs(testUser, null); - - // Then - assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1)); - assertTrue(storeGraphs.contains(graphToAdd)); - - // When - store = new FederatedStore(); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - - // Then - assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1)); - } - - @Test - public void shouldAddMultipleGraphsToCache() throws StoreException { - // Given - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - List graphsToAdd = new ArrayList<>(); - for (int i = 0; i < 10; i++) { - graphsToAdd.add(new Graph.Builder() - .config(new GraphConfig(ACC_ID_1 + i)) - .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES)) - .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) - .build()); - } - - // When - store.addGraphs(null, TEST_USER, false, graphsToAdd.toArray(new Graph[graphsToAdd.size()])); - - // Then - for (int i = 0; i < 10; i++) { - assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1 + i)); - } - - // When - store = new FederatedStore(); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - - // Then - for (int i = 0; i < 10; i++) { - assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1 + i)); - } - } - - @Test - public void shouldReuseGraphsAlreadyInCache() throws Exception { - //Check cache is empty - assertNull(CacheServiceLoader.getService()); - - //initialise FedStore - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - - //add something so it will be in the cache - Graph graphToAdd = new Graph.Builder() - .config(new GraphConfig(MAP_ID_1)) - .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) - .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) - .build(); - - store.addGraphs(null, TEST_USER, false, graphToAdd); - - //check the store and the cache - assertEquals(1, store.getAllGraphIds(testUser).size()); - assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(MAP_ID_1)); - - //restart the store - store = null; - store = new FederatedStore(); - federatedProperties.setGraphIds(MAP_ID_1); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - - //check the graph is already in there from the cache - assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(MAP_ID_1)); - assertEquals(1, store.getAllGraphIds(testUser).size()); - } - @Test public void shouldLoadGraphsWithIds() throws Exception { // Given @@ -340,15 +205,9 @@ public void shouldNotAllowOverwritingOfGraphWithFederatedScope() throws Exceptio } } - @Test + @Test(expected = UnsupportedOperationException.class) public void shouldDoUnhandledOperation() throws Exception { - // When / Then - try { store.doUnhandledOperation(null, null); - fail(EXCEPTION_NOT_THROWN); - } catch (UnsupportedOperationException e) { - assertNotNull(e); - } } @Test @@ -358,6 +217,8 @@ public void shouldUpdateTraitsWhenNewGraphIsAdded() throws Exception { federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES); federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + + //With less Traits Set before = store.getTraits(); // When @@ -409,11 +270,13 @@ public void shouldUpdateTraitsToMinWhenGraphIsRemoved() throws Exception { store.initialise(FEDERATED_STORE_ID, null, federatedProperties); // When + //with less Traits Set before = store.getTraits(); store.remove(MAP_ID_1, testUser); Set after = store.getTraits(); // Then + //includes same as before but with more Traits assertEquals("Shared traits between the two graphs should be " + 5, 5, before.size()); assertEquals("Shared traits counter-intuitively will go up after removing graph, because the sole remaining graph has 9 traits", 9, after.size()); assertNotEquals(before, after); @@ -724,7 +587,7 @@ public void shouldAddNamedGraphsFromGraphIDKeyButDefinedInLibraryAndProperties() public void shouldAddGraphWithPropertiesFromGraphLibrary() throws Exception { // Given federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setParentPropId(MAP_ID_1, PROPS_ID_1); + federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1); federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON); final HashMapGraphLibrary library = new HashMapGraphLibrary(); library.addProperties(StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES)); @@ -743,7 +606,7 @@ public void shouldAddGraphWithSchemaFromGraphLibrary() throws Exception { // Given federatedProperties.setGraphIds(MAP_ID_1); federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); - federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_1); + federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1); final HashMapGraphLibrary library = new HashMapGraphLibrary(); final Schema schema = new Schema.Builder() .id(SCHEMA_ID_1) @@ -789,7 +652,7 @@ public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibrary() throws Excep public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exception { // Given federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setParentPropId(MAP_ID_1, PROPS_ID_1); + federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1); federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES_ALT); federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON); final MapStoreProperties prop = new MapStoreProperties(); @@ -817,7 +680,7 @@ public void shouldAddGraphWithSchemaFromGraphLibraryOverridden() throws Exceptio federatedProperties.setGraphIds(MAP_ID_1); federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON); - federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_2); + federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_2); final Schema schema = new Schema.Builder() .id(SCHEMA_ID_2) .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_EDGE_SCHEMA_JSON)) @@ -840,8 +703,8 @@ public void shouldAddGraphWithSchemaFromGraphLibraryOverridden() throws Exceptio public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibraryOverridden() throws Exception { // Given federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setParentPropId(MAP_ID_1, PROPS_ID_1); - federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_2); + federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1); + federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_2); federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES_ALT); federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON); @@ -1058,12 +921,33 @@ public void shouldAddGraphIdWithAuths() throws Exception { Assert.assertTrue(elements.iterator().hasNext()); } + @Test + public void shouldThrowWithPropertiesErrorFromGraphLibrary() throws Exception { + federatedProperties.setGraphIds(MAP_ID_1); + federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1); + federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); + final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); + final String error = "Something went wrong"; + Mockito.when(mockLibrary.getProperties(PROPS_ID_1)).thenThrow(new IllegalArgumentException(error)); + + store.setGraphLibrary(mockLibrary); + + try { + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + fail("exception not thrown"); + } catch (IllegalArgumentException e) { + assertEquals(error, e.getCause().getMessage()); + } + + Mockito.verify(mockLibrary).getProperties(PROPS_ID_1); + } + @Test public void shouldThrowWithSchemaErrorFromGraphLibrary() throws Exception { // Given federatedProperties.setGraphIds(MAP_ID_1); federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); - federatedProperties.setParentSchemaId(MAP_ID_1, SCHEMA_ID_1); + federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1); final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); final String error = "Something went wrong"; Mockito.when(mockLibrary.getSchema(SCHEMA_ID_1)).thenThrow(new IllegalArgumentException(error)); @@ -1107,6 +991,141 @@ public void shouldThrowExceptionWithInvalidCacheClass() throws StoreException { } } + @Test + public void shouldReuseGraphsAlreadyInCache() throws Exception { + //Check cache is empty + assertNull(CacheServiceLoader.getService()); + + //initialise FedStore + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + + //add something so it will be in the cache + Graph graphToAdd = new Graph.Builder() + .config(new GraphConfig(MAP_ID_1)) + .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) + .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) + .build(); + + store.addGraphs(null, TEST_USER, false, graphToAdd); + + //check the store and the cache + assertEquals(1, store.getAllGraphIds(testUser).size()); + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(MAP_ID_1)); + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(MAP_ID_1)); + + //restart the store + store = new FederatedStore(); + federatedProperties.setGraphIds(MAP_ID_1); + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + + //check the graph is already in there from the cache + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(MAP_ID_1)); + assertEquals(1, store.getAllGraphIds(testUser).size()); + } + + @Test + public void shouldInitialiseWithCache() throws StoreException { + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + assertNotNull(CacheServiceLoader.getService()); + } + + @Test + public void shouldThrowExceptionWithoutInitialisation() { + // Given + Graph graphToAdd = new Graph.Builder() + .config(new GraphConfig(ACC_ID_1)) + .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES)) + .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) + .build(); + + CacheServiceLoader.shutdown(); + + // When / Then + try { + store.addGraphs(null, TEST_USER, false, graphToAdd); + fail(EXCEPTION_NOT_THROWN); + } catch (final Exception e) { + assertTrue(e.getMessage().contains("No cache has been set")); + } + } + + @Test + public void shouldThrowExceptionWhenInitialisedWithNoCacheClassInProperties() throws StoreException { + // Given + StoreProperties storeProperties = new StoreProperties(); + + // When / Then + try { + store.initialise(FEDERATED_STORE_ID, null, storeProperties); + fail(EXCEPTION_NOT_THROWN); + } catch (final StoreException e) { + assertTrue(e.getMessage().contains("No cache has been set, please check the property")); + } + } + + @Test + public void shouldAddGraphsToCache() throws StoreException { + // Given + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + + Graph graphToAdd = new Graph.Builder() + .config(new GraphConfig(ACC_ID_1)) + .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES)) + .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) + .build(); + + // When + store.addGraphs(null, TEST_USER, false, graphToAdd); + + // Then + assertEquals(1, store.getGraphs(testUser, ACC_ID_1).size()); + + // When + Collection storeGraphs = store.getGraphs(testUser, null); + + // Then + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1)); + assertTrue(storeGraphs.contains(graphToAdd)); + + // When + store = new FederatedStore(); + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + + // Then + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1)); + } + + @Test + public void shouldAddMultipleGraphsToCache() throws StoreException { + // Given + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + List graphsToAdd = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + graphsToAdd.add(new Graph.Builder() + .config(new GraphConfig(ACC_ID_1 + i)) + .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES)) + .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) + .build()); + } + + // When + store.addGraphs(null, TEST_USER, false, graphsToAdd.toArray(new Graph[graphsToAdd.size()])); + + // Then + for (int i = 0; i < 10; i++) { + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1 + i)); + } + + // When + store = new FederatedStore(); + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + + // Then + for (int i = 0; i < 10; i++) { + assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1 + i)); + } + } + private boolean checkUnexpected(final Collection unexpectedGraphs, final Collection returnedGraphs) { for (Graph graph : unexpectedGraphs) { if (returnedGraphs.contains(graph)) { diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java index f7eafb39462..71baeae8efe 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java @@ -58,8 +58,8 @@ public class FederatedStoreWrongGraphIDsTest { public void setUp() throws Exception { fedProps = new FederatedStoreProperties(); fedProps.setGraphIds(GRAPH_1); - fedProps.setParentPropId(GRAPH_1, PROP_1); - fedProps.setParentSchemaId(GRAPH_1, SCHEMA_1); + fedProps.setGraphPropId(GRAPH_1, PROP_1); + fedProps.setGraphSchemaId(GRAPH_1, SCHEMA_1); fedProps.setTrueGraphIsPublicValue(GRAPH_1); fedProps.setCacheProperties(CACHE_SERVICE_CLASS_STRING); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/AddGraphTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/AddGraphTest.java index 1255e5b8f01..381ab76cda2 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/AddGraphTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/AddGraphTest.java @@ -22,6 +22,7 @@ import org.junit.Test; import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph.Builder; +import uk.gov.gchq.gaffer.mapstore.MapStoreProperties; import uk.gov.gchq.gaffer.operation.OperationTest; import uk.gov.gchq.gaffer.store.StoreProperties; import uk.gov.gchq.gaffer.store.schema.Schema; @@ -30,7 +31,6 @@ public class AddGraphTest extends OperationTest { - private static final String FEDERATEDSTORE_CLASS_STRING = "uk.gov.gchq.gaffer.federatedstore.FederatedStore"; private static final String EXPECTED_GRAPH_ID = "testGraphID"; @Override @@ -41,8 +41,7 @@ protected Set getRequiredFields() { @Override public void builderShouldCreatePopulatedOperation() { Schema expectedSchema = new Schema.Builder().build(); - StoreProperties storeProperties = new StoreProperties(); - storeProperties.set(StoreProperties.STORE_CLASS, FEDERATEDSTORE_CLASS_STRING); + StoreProperties storeProperties = new MapStoreProperties(); AddGraph op = new AddGraph.Builder() .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) @@ -51,14 +50,14 @@ public void builderShouldCreatePopulatedOperation() { Assert.assertEquals(EXPECTED_GRAPH_ID, op.getGraphId()); Assert.assertEquals(expectedSchema, op.getSchema()); - Assert.assertTrue(op.getStoreProperties().containsKey(StoreProperties.STORE_CLASS)); - Assert.assertEquals(FEDERATEDSTORE_CLASS_STRING, op.getStoreProperties().get(StoreProperties.STORE_CLASS)); + Assert.assertNotNull(op.getStoreProperties().getStorePropertiesClassName()); + Assert.assertEquals(MapStoreProperties.class, op.getStoreProperties().getStorePropertiesClass()); } @Override public void shouldShallowCloneOperation() { final AddGraph a = new Builder() - .graphId("graphID") + .graphId("graphId") .parentPropertiesId("testPropID") .parentSchemaIds(Lists.newArrayList("testSchemaID")) .schema(new Schema.Builder() diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java index 586f0780ebc..0fd27754008 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java @@ -191,7 +191,7 @@ final public void shouldNotThrowException() throws Exception { // When try { - new FederatedOperationHandler().doOperation(op, new Context(user), mockStore); + new FederatedOperationHandler().doOperation(op, context, mockStore); } catch (Exception e) { fail("Exception should not have been thrown: " + e.getMessage()); } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java index 6a9c352f80e..2142858b769 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java @@ -49,7 +49,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static uk.gov.gchq.gaffer.federatedstore.FederatedStore.USER_IS_ATTEMPTING_TO_OVERWRITE_A_GRAPH_WITHIN_FEDERATED_STORE_GRAPH_ID_S; +import static uk.gov.gchq.gaffer.federatedstore.FederatedGraphStorage.USER_IS_ATTEMPTING_TO_OVERWRITE; import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreUser.authUser; import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreUser.testUser; @@ -82,7 +82,6 @@ public void shouldAddGraph() throws Exception { MapStoreProperties storeProperties = new MapStoreProperties(); - assertEquals(0, store.getGraphs(testUser, null).size()); assertEquals(0, store.getGraphs(testUser, null).size()); FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); @@ -212,7 +211,7 @@ public void shouldNotOverwriteGraph() throws Exception { new Context(testUser), store); } catch (final OverwritingException e) { - assertEquals(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE_A_GRAPH_WITHIN_FEDERATED_STORE_GRAPH_ID_S, EXPECTED_GRAPH_ID), e.getMessage()); + assertEquals(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, EXPECTED_GRAPH_ID), e.getMessage()); } } diff --git a/store-implementation/hbase-store/src/main/java/uk/gov/gchq/gaffer/hbasestore/HBaseProperties.java b/store-implementation/hbase-store/src/main/java/uk/gov/gchq/gaffer/hbasestore/HBaseProperties.java index bb008db0577..ac7feabde12 100755 --- a/store-implementation/hbase-store/src/main/java/uk/gov/gchq/gaffer/hbasestore/HBaseProperties.java +++ b/store-implementation/hbase-store/src/main/java/uk/gov/gchq/gaffer/hbasestore/HBaseProperties.java @@ -47,15 +47,11 @@ public class HBaseProperties extends StoreProperties { public static final String MAX_ENTRIES_FOR_BATCH_SCANNER_DEFAULT = "50000"; public HBaseProperties() { - super(); - setStoreClass(HBaseStore.class); + super(HBaseStore.class); } public HBaseProperties(final Path propFileLocation) { - super(propFileLocation); - if (null == getStoreClass()) { - setStoreClass(HBaseStore.class); - } + super(propFileLocation, HBaseStore.class); } public static HBaseProperties loadStoreProperties(final String pathStr) { diff --git a/store-implementation/map-store/src/main/java/uk/gov/gchq/gaffer/mapstore/MapStoreProperties.java b/store-implementation/map-store/src/main/java/uk/gov/gchq/gaffer/mapstore/MapStoreProperties.java index 655fde3f4a2..1488d3e4295 100644 --- a/store-implementation/map-store/src/main/java/uk/gov/gchq/gaffer/mapstore/MapStoreProperties.java +++ b/store-implementation/map-store/src/main/java/uk/gov/gchq/gaffer/mapstore/MapStoreProperties.java @@ -48,15 +48,11 @@ public class MapStoreProperties extends StoreProperties { public static final int INGEST_BUFFER_SIZE_DEFAULT = 0; public MapStoreProperties() { - super(); - setStoreClass(MapStore.class); + super(MapStore.class); } public MapStoreProperties(final Path propFileLocation) { - super(propFileLocation); - if (null == getStoreClass()) { - setStoreClass(MapStore.class); - } + super(propFileLocation, MapStore.class); } public static MapStoreProperties loadStoreProperties(final String pathStr) { diff --git a/store-implementation/parquet-store/src/main/java/uk/gov/gchq/gaffer/parquetstore/ParquetStoreProperties.java b/store-implementation/parquet-store/src/main/java/uk/gov/gchq/gaffer/parquetstore/ParquetStoreProperties.java index 93b3b7ecfa5..97a01789fee 100644 --- a/store-implementation/parquet-store/src/main/java/uk/gov/gchq/gaffer/parquetstore/ParquetStoreProperties.java +++ b/store-implementation/parquet-store/src/main/java/uk/gov/gchq/gaffer/parquetstore/ParquetStoreProperties.java @@ -63,15 +63,11 @@ public class ParquetStoreProperties extends StoreProperties implements Serializa private static final long serialVersionUID = 7695540336792378185L; public ParquetStoreProperties() { - super(); - this.setStoreClass(ParquetStore.class); + super(ParquetStore.class); } public ParquetStoreProperties(final Path propFileLocation) { - super(propFileLocation); - if (null == getStoreClass()) { - setStoreClass(ParquetStore.class); - } + super(propFileLocation, ParquetStore.class); } public static ParquetStoreProperties loadStoreProperties(final String pathStr) { diff --git a/store-implementation/proxy-store/src/main/java/uk/gov/gchq/gaffer/proxystore/ProxyProperties.java b/store-implementation/proxy-store/src/main/java/uk/gov/gchq/gaffer/proxystore/ProxyProperties.java index ef17645a97c..fdf2b7f0b36 100644 --- a/store-implementation/proxy-store/src/main/java/uk/gov/gchq/gaffer/proxystore/ProxyProperties.java +++ b/store-implementation/proxy-store/src/main/java/uk/gov/gchq/gaffer/proxystore/ProxyProperties.java @@ -45,21 +45,15 @@ public class ProxyProperties extends StoreProperties { private static final String GAFFER_REST_API_VERSION = "v2"; public ProxyProperties() { - setStoreClass(ProxyStore.class); + super(ProxyStore.class); } public ProxyProperties(final Path propFileLocation) { - super(propFileLocation); - if (null == getStoreClass()) { - setStoreClass(ProxyStore.class); - } + super(propFileLocation, ProxyStore.class); } public ProxyProperties(final Properties props) { - super(props); - if (null == getStoreClass()) { - setStoreClass(ProxyStore.class); - } + super(props, ProxyStore.class); } public static ProxyProperties loadStoreProperties(final String pathStr) { From c201927b5f0ef2c62c1f054f671ec88d81e5dbc2 Mon Sep 17 00:00:00 2001 From: m55624 Date: Mon, 16 Oct 2017 12:42:40 +0100 Subject: [PATCH 46/72] gh-1297 - test fixes and logic updates --- .../java/uk/gov/gchq/gaffer/graph/Graph.java | 2 +- .../gaffer/federatedstore/FederatedStore.java | 48 ++++++++++++------- .../FederatedStorePublicAccessTest.java | 2 + .../FederatedStoreSchemaTest.java | 2 + .../federatedstore/FederatedStoreTest.java | 3 +- .../FederatedStoreWrongGraphIDsTest.java | 2 + .../impl/FederatedAddGraphHandlerTest.java | 2 + .../impl/FederatedRemoveGraphHandlerTest.java | 2 + .../predefinedFederatedStore.properties | 1 - 9 files changed, 43 insertions(+), 21 deletions(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java index d2f8e8d6213..d0c5b95f734 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java @@ -862,7 +862,7 @@ private void updateStoreProperties(final GraphConfig config) { mergedStoreProperties = properties; } else { mergedStoreProperties.getProperties().putAll(properties.getProperties()); - mergedStoreProperties.setId(null); + mergedStoreProperties.setId(null); } } properties = mergedStoreProperties; diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 65e258f6e52..1500d4f3faa 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -22,7 +22,7 @@ import org.apache.commons.lang3.StringUtils; import uk.gov.gchq.gaffer.cache.CacheServiceLoader; -import uk.gov.gchq.gaffer.cache.util.CacheProperties; +import uk.gov.gchq.gaffer.commonutil.JsonUtil; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; @@ -98,6 +98,7 @@ public class FederatedStore extends Store { private FederatedGraphStorage graphStorage = new FederatedGraphStorage(); private Set customPropertiesAuths; public Boolean isPublicAccessAllowed = Boolean.valueOf(IS_PUBLIC_ACCESS_ALLOWED_DEFAULT); + public Boolean isCacheEnabled = false; /** * Initialise this FederatedStore with any sub-graphs defined within the @@ -185,13 +186,15 @@ public static OP updateOperationForGraph(final OP operati */ public void addGraphs(final Set graphAuths, final String addingUserId, final boolean isPublic, final Graph... graphs) { + if (isCacheEnabled) { validateCache(); + } - FederatedAccess access = new FederatedAccess(graphAuths, addingUserId, isPublicAccessAllowed && isPublic); + FederatedAccess access = new FederatedAccess(graphAuths, addingUserId, isPublicAccessAllowed && isPublic); - for (final Graph graph : graphs) { - _add(graph, access); - } + for (final Graph graph : graphs) { + _add(graph, access); + } } @Deprecated @@ -211,8 +214,10 @@ public void addGraphs(final Set graphAuths, final String addingUserId, f * uk.gov.gchq.gaffer.graph.Graph} from the store */ public void remove(final String graphId, final User user) { - validateCache(); - federatedStoreCache.deleteFromCache(graphId); + if (isCacheEnabled) { + validateCache(); + federatedStoreCache.deleteFromCache(graphId); + } graphStorage.remove(graphId, user); } @@ -311,8 +316,10 @@ protected Object doUnhandledOperation(final Operation operation, @Override protected void startCacheServiceLoader(final StoreProperties properties) { - if (federatedStoreCache.getCache() == null) { - CacheServiceLoader.initialise(properties.getProperties()); + if (isCacheEnabled) { + if (federatedStoreCache.getCache() == null) { + CacheServiceLoader.initialise(properties.getProperties()); + } } } @@ -373,9 +380,11 @@ private void loadGraphs() throws StoreException { final Set graphAuths = resolveAuths(graphId); final boolean isPublic = resolveIsPublic(graphId); - if (federatedStoreCache.contains(graphId)) { - Graph graph = federatedStoreCache.getFromCache(graphId); - addGraphs(graphAuths, null, isPublic, graph); + if (isCacheEnabled) { + if (federatedStoreCache.contains(graphId)) { + Graph graph = federatedStoreCache.getFromCache(graphId); + addGraphs(graphAuths, null, isPublic, graph); + } } else { final Graph.Builder builder = new Builder() .config(new GraphConfig.Builder() @@ -456,8 +465,10 @@ private Set getGraphIds() { return graphIds; } - private void _add(final Graph newGraph, final FederatedAccess access){ - addToCache(newGraph); + private void _add(final Graph newGraph, final FederatedAccess access) { + if (isCacheEnabled) { + addToCache(newGraph); + } graphStorage.put(newGraph, access); @@ -478,16 +489,17 @@ private void addToCache(final Graph newGraph) { } } else { final Graph fromCache = federatedStoreCache.getFromCache(graphId); - if (!newGraph.equals(fromCache)) { + if (newGraph.getStoreProperties().getProperties().equals(fromCache.getStoreProperties().getProperties()) + && JsonUtil.equals(newGraph.getSchema().toJson(false), fromCache.getSchema().toJson(false)) + && newGraph.getGraphId().equals(fromCache.getGraphId())) { throw new RuntimeException(String.format("Error adding graph, GraphId is known within the cache, but is different. GraphId: %s", graphId)); } } } private void validateCacheProperties() throws StoreException { - if (getProperties().getCacheProperties() == null) { - throw new StoreException("No cache has been set, please check the property " - + CacheProperties.CACHE_SERVICE_CLASS + " has been set in the Store Properties"); + if (getProperties().getCacheProperties() != null) { + isCacheEnabled = true; } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java index 2be4dcbb2e4..70d9fbd13fd 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java @@ -20,6 +20,7 @@ import org.junit.Before; import org.junit.Test; +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.federatedstore.operation.GetAllGraphIds; import uk.gov.gchq.gaffer.mapstore.MapStoreProperties; import uk.gov.gchq.gaffer.store.Context; @@ -41,6 +42,7 @@ public class FederatedStorePublicAccessTest { @Before public void setUp() throws Exception { + CacheServiceLoader.shutdown(); fedProps = new FederatedStoreProperties(); fedProps.setCacheProperties(CACHE_SERVICE_CLASS_STRING); fedProps.setGraphIds(GRAPH_1); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java index f9f55496174..94eb0e07381 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java @@ -22,6 +22,7 @@ import uk.gov.gchq.gaffer.accumulostore.AccumuloProperties; import uk.gov.gchq.gaffer.accumulostore.MockAccumuloStore; +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException; import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; import uk.gov.gchq.gaffer.operation.Operation; @@ -56,6 +57,7 @@ public class FederatedStoreSchemaTest { @Before public void setUp() throws Exception { + CacheServiceLoader.shutdown(); ACCUMULO_PROPERTIES.setId("accProp"); ACCUMULO_PROPERTIES.setStoreClass(MockAccumuloStore.class); ACCUMULO_PROPERTIES.setStorePropertiesClass(AccumuloProperties.class); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index 41e56dfab2a..882aed6e2ab 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -103,6 +103,7 @@ public class FederatedStoreTest { @Before public void setUp() throws Exception { + CacheServiceLoader.shutdown(); store = new FederatedStore(); federatedProperties = new FederatedStoreProperties(); federatedProperties.setGraphAuth(ACC_ID_1, ALL_USERS); @@ -207,7 +208,7 @@ public void shouldNotAllowOverwritingOfGraphWithFederatedScope() throws Exceptio @Test(expected = UnsupportedOperationException.class) public void shouldDoUnhandledOperation() throws Exception { - store.doUnhandledOperation(null, null); + store.doUnhandledOperation(null, null); } @Test diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java index 71baeae8efe..f6c4b33601f 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java @@ -20,6 +20,7 @@ import org.junit.Before; import org.junit.Test; +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; import uk.gov.gchq.gaffer.data.element.Element; import uk.gov.gchq.gaffer.data.element.Entity; @@ -56,6 +57,7 @@ public class FederatedStoreWrongGraphIDsTest { @Before public void setUp() throws Exception { + CacheServiceLoader.shutdown(); fedProps = new FederatedStoreProperties(); fedProps.setGraphIds(GRAPH_1); fedProps.setGraphPropId(GRAPH_1, PROP_1); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java index 2142858b769..03f22ce139b 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java @@ -24,6 +24,7 @@ import uk.gov.gchq.gaffer.accumulostore.AccumuloProperties; import uk.gov.gchq.gaffer.accumulostore.SingleUseMockAccumuloStore; +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; import uk.gov.gchq.gaffer.commonutil.pair.Pair; @@ -66,6 +67,7 @@ public class FederatedAddGraphHandlerTest { @Before public void setUp() throws Exception { + CacheServiceLoader.shutdown(); this.store = new FederatedStore(); federatedStoreProperties = new FederatedStoreProperties(); federatedStoreProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java index cd08a3568c6..851f2267746 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedRemoveGraphHandlerTest.java @@ -21,6 +21,7 @@ import uk.gov.gchq.gaffer.accumulostore.AccumuloProperties; import uk.gov.gchq.gaffer.accumulostore.SingleUseMockAccumuloStore; +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.federatedstore.FederatedStore; import uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties; import uk.gov.gchq.gaffer.federatedstore.operation.RemoveGraph; @@ -43,6 +44,7 @@ public class FederatedRemoveGraphHandlerTest { @Before public void setUp() throws Exception { + CacheServiceLoader.shutdown(); testUser = testUser(); } diff --git a/store-implementation/federated-store/src/test/resources/predefinedFederatedStore.properties b/store-implementation/federated-store/src/test/resources/predefinedFederatedStore.properties index c54917930f3..37cf80f5ab6 100644 --- a/store-implementation/federated-store/src/test/resources/predefinedFederatedStore.properties +++ b/store-implementation/federated-store/src/test/resources/predefinedFederatedStore.properties @@ -14,4 +14,3 @@ # limitations under the License. # gaffer.store.class=uk.gov.gchq.gaffer.federatedstore.PredefinedFederatedStore -gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService From 99b60a733760a8527a5be0c907b30f351d304623 Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Mon, 16 Oct 2017 15:46:50 +0100 Subject: [PATCH 47/72] gh-1297 bug fix x2 and minor test adjustment. --- .../java/uk/gov/gchq/gaffer/store/Store.java | 88 +++++++++++++------ .../gaffer/federatedstore/FederatedStore.java | 49 ++++++----- .../federatedstore/FederatedStoreCache.java | 1 - .../FederatedStoreProperties.java | 10 +++ .../FederatedStoreSchemaTest.java | 7 +- .../federatedstore/FederatedStoreTest.java | 14 +-- .../impl/FederatedAddGraphHandlerTest.java | 18 ++-- 7 files changed, 112 insertions(+), 75 deletions(-) diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java index af960e65cc0..cd1e60699ec 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/Store.java @@ -134,11 +134,15 @@ import java.util.concurrent.Executors; /** - * A {@code Store} backs a Graph and is responsible for storing the {@link uk.gov.gchq.gaffer.data.element.Element}s and + * A {@code Store} backs a Graph and is responsible for storing the {@link + * uk.gov.gchq.gaffer.data.element.Element}s and * handling {@link Operation}s. - * {@link Operation}s and their corresponding {@link OperationHandler}s are registered in a map and used to handle - * provided operations - allowing different store implementations to handle the same operations in their own store specific way. - * Optional functionality can be added to store implementations defined by the {@link uk.gov.gchq.gaffer.store.StoreTrait}s. + * {@link Operation}s and their corresponding {@link OperationHandler}s are + * registered in a map and used to handle + * provided operations - allowing different store implementations to handle the + * same operations in their own store specific way. + * Optional functionality can be added to store implementations defined by the + * {@link uk.gov.gchq.gaffer.store.StoreTrait}s. */ public abstract class Store { private static final Logger LOGGER = LoggerFactory.getLogger(Store.class); @@ -148,12 +152,14 @@ public abstract class Store { protected final OperationChainValidator opChainValidator; private final SchemaOptimiser schemaOptimiser; /** - * The schema - contains the type of {@link uk.gov.gchq.gaffer.data.element.Element}s to be stored and how to aggregate the elements. + * The schema - contains the type of {@link uk.gov.gchq.gaffer.data.element.Element}s + * to be stored and how to aggregate the elements. */ private Schema schema; /** - * The store properties - contains specific configuration information for the store - such as database connection strings. + * The store properties - contains specific configuration information for + * the store - such as database connection strings. */ private StoreProperties properties; @@ -175,7 +181,7 @@ public static Store createStore(final String graphId, final byte[] schema, final public static Store createStore(final String graphId, final Schema schema, final StoreProperties storeProperties) { if (null == storeProperties) { - throw new IllegalArgumentException("Store properties are required to create a store"); + throw new IllegalArgumentException("Store properties are required to create a store. graphId: " + graphId); } final String storeClass = storeProperties.getStoreClass(); @@ -221,7 +227,8 @@ public void initialise(final String graphId, final Schema schema, final StorePro } /** - * Returns true if the Store can handle the provided trait and false if it cannot. + * Returns true if the Store can handle the provided trait and false if it + * cannot. * * @param storeTrait the Class of the Processor to be checked. * @return true if the Processor can be handled and false if it cannot. @@ -232,9 +239,11 @@ public boolean hasTrait(final StoreTrait storeTrait) { } /** - * Returns the {@link uk.gov.gchq.gaffer.store.StoreTrait}s for this store. Most stores should support FILTERING. + * Returns the {@link uk.gov.gchq.gaffer.store.StoreTrait}s for this store. + * Most stores should support FILTERING. *

- * If you use Operation.validateFilter(Element) in you handlers, it will deal with the filtering for you. + * If you use Operation.validateFilter(Element) in you handlers, it will + * deal with the filtering for you. *

* * @return the {@link uk.gov.gchq.gaffer.store.StoreTrait}s for this store. @@ -246,7 +255,8 @@ public boolean hasTrait(final StoreTrait storeTrait) { * * @param operation the operation to execute. * @param context the context executing the operation - * @throws OperationException thrown by the operation handler if the operation fails. + * @throws OperationException thrown by the operation handler if the + * operation fails. */ public void execute(final Operation operation, final Context context) throws OperationException { execute(OperationChain.wrap(operation), context); @@ -259,7 +269,8 @@ public void execute(final Operation operation, final Context context) throws Ope * @param context the context executing the operation * @param the output type of the operation * @return the result of executing the operation - * @throws OperationException thrown by the operation handler if the operation fails. + * @throws OperationException thrown by the operation handler if the + * operation fails. */ public O execute(final Output operation, final Context context) throws OperationException { return execute(OperationChain.wrap(operation), context); @@ -375,8 +386,10 @@ public Set> getNextOperations(final Class getPropertiesClass() { } /** - * Throws a {@link SchemaException} if the Vertex Serialiser is inconsistent. + * Throws a {@link SchemaException} if the Vertex Serialiser is + * inconsistent. */ protected void validateConsistentVertex() { if (null != getSchema().getVertexSerialiser() && !getSchema().getVertexSerialiser() @@ -503,7 +519,8 @@ protected void validateConsistentVertex() { } /** - * Ensures that each of the GroupBy properties in the {@link SchemaElementDefinition} is consistent, + * Ensures that each of the GroupBy properties in the {@link + * SchemaElementDefinition} is consistent, * otherwise an error is added to the {@link ValidationResult}. * * @param schemaElementDefinitionEntry A map of SchemaElementDefinitions @@ -571,26 +588,34 @@ protected void addOperationChainOptimisers(final List n } /** - * Any additional operations that a store can handle should be registered in this method by calling addOperationHandler(...) + * Any additional operations that a store can handle should be registered in + * this method by calling addOperationHandler(...) */ protected abstract void addAdditionalOperationHandlers(); /** - * Get this Stores implementation of the handler for {@link uk.gov.gchq.gaffer.operation.impl.get.GetElements}. All Stores must implement this. + * Get this Stores implementation of the handler for {@link + * uk.gov.gchq.gaffer.operation.impl.get.GetElements}. All Stores must + * implement this. * - * @return the implementation of the handler for {@link uk.gov.gchq.gaffer.operation.impl.get.GetElements} + * @return the implementation of the handler for {@link + * uk.gov.gchq.gaffer.operation.impl.get.GetElements} */ protected abstract OutputOperationHandler> getGetElementsHandler(); /** - * Get this Stores implementation of the handler for {@link uk.gov.gchq.gaffer.operation.impl.get.GetAllElements}. All Stores must implement this. + * Get this Stores implementation of the handler for {@link + * uk.gov.gchq.gaffer.operation.impl.get.GetAllElements}. All Stores must + * implement this. * - * @return the implementation of the handler for {@link uk.gov.gchq.gaffer.operation.impl.get.GetAllElements} + * @return the implementation of the handler for {@link + * uk.gov.gchq.gaffer.operation.impl.get.GetAllElements} */ protected abstract OutputOperationHandler> getGetAllElementsHandler(); /** - * Get this Stores implementation of the handler for {@link GetAdjacentIds}. + * Get this Stores implementation of the handler for {@link + * GetAdjacentIds}. * All Stores must implement this. * * @return the implementation of the handler for {@link GetAdjacentIds} @@ -598,18 +623,22 @@ protected void addOperationChainOptimisers(final List n protected abstract OutputOperationHandler> getAdjacentIdsHandler(); /** - * Get this Stores implementation of the handler for {@link uk.gov.gchq.gaffer.operation.impl.add.AddElements}. + * Get this Stores implementation of the handler for {@link + * uk.gov.gchq.gaffer.operation.impl.add.AddElements}. * All Stores must implement this. * - * @return the implementation of the handler for {@link uk.gov.gchq.gaffer.operation.impl.add.AddElements} + * @return the implementation of the handler for {@link + * uk.gov.gchq.gaffer.operation.impl.add.AddElements} */ protected abstract OperationHandler getAddElementsHandler(); /** - * Get this Store's implementation of the handler for {@link uk.gov.gchq.gaffer.operation.OperationChain}. + * Get this Store's implementation of the handler for {@link + * uk.gov.gchq.gaffer.operation.OperationChain}. * All Stores must implement this. * - * @return the implementation of the handler for {@link uk.gov.gchq.gaffer.operation.OperationChain} + * @return the implementation of the handler for {@link + * uk.gov.gchq.gaffer.operation.OperationChain} */ protected OperationHandler> getOperationChainHandler() { return new OperationChainHandler<>(opChainValidator, opChainOptimisers); @@ -625,7 +654,8 @@ protected HashMap getSchemaElements() { protected abstract Class getRequiredParentSerialiserClass(); /** - * Should deal with any unhandled operations, simply throws an {@link UnsupportedOperationException}. + * Should deal with any unhandled operations, simply throws an {@link + * UnsupportedOperationException}. * * @param operation the operation that does not have a registered handler. * @param context operation execution context diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 1500d4f3faa..bc7c4df7bda 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -80,7 +80,8 @@ * against them and returns results as though it was a single graph. *

* To create a FederatedStore you need to initialise the store with a - * graphId and (if graphId is not known by the {@link uk.gov.gchq.gaffer.store.library.GraphLibrary}) the + * graphId and (if graphId is not known by the {@link uk.gov.gchq.gaffer.store.library.GraphLibrary}) + * the * {@link Schema} and {@link StoreProperties}. * * @see #initialise(String, Schema, StoreProperties) @@ -88,17 +89,19 @@ * @see Graph */ public class FederatedStore extends Store { + public static final String ERROR_ADDING_GRAPH_TO_CACHE = "Error adding graph, GraphId is known within the cache, but %s is different. GraphId: %s"; protected static final String S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2 = "%s was not able to be created with the supplied properties.%n%s"; private static final String SCHEMA_DEL_REGEX = Pattern.quote(","); private static final GraphConfigEnum SCHEMA = GraphConfigEnum.SCHEMA; private static final GraphConfigEnum PROPERTIES = GraphConfigEnum.PROPERTIES; private static final LocationEnum ID = LocationEnum.ID; private static final LocationEnum FILE = LocationEnum.FILE; - FederatedStoreCache federatedStoreCache = new FederatedStoreCache(); + private FederatedStoreCache federatedStoreCache = new FederatedStoreCache(); private FederatedGraphStorage graphStorage = new FederatedGraphStorage(); private Set customPropertiesAuths; - public Boolean isPublicAccessAllowed = Boolean.valueOf(IS_PUBLIC_ACCESS_ALLOWED_DEFAULT); - public Boolean isCacheEnabled = false; + private Boolean isPublicAccessAllowed = Boolean.valueOf(IS_PUBLIC_ACCESS_ALLOWED_DEFAULT); + private Boolean isCacheEnabled = false; + private boolean isInitialised = false; /** * Initialise this FederatedStore with any sub-graphs defined within the @@ -112,6 +115,7 @@ public class FederatedStore extends Store { */ @Override public void initialise(final String graphId, final Schema unused, final StoreProperties properties) throws StoreException { + isInitialised = true; setProperties(properties); validateCacheProperties(); super.initialise(graphId, new Schema(), properties); @@ -182,11 +186,10 @@ public static OP updateOperationForGraph(final OP operati * @param graphs the graph to add * @param isPublic if this class should have public access. * @param graphAuths the access auths for the graph being added - * @throws StoreException if no cache has been set */ public void addGraphs(final Set graphAuths, final String addingUserId, final boolean isPublic, final Graph... graphs) { - if (isCacheEnabled) { + if (isCacheEnabled || !isInitialised) { validateCache(); } @@ -210,8 +213,6 @@ public void addGraphs(final Set graphAuths, final String addingUserId, f * * @param graphId to be removed from scope * @param user to match visibility against - * @throws StoreException if there was an error removing the {@link - * uk.gov.gchq.gaffer.graph.Graph} from the store */ public void remove(final String graphId, final User user) { if (isCacheEnabled) { @@ -380,11 +381,9 @@ private void loadGraphs() throws StoreException { final Set graphAuths = resolveAuths(graphId); final boolean isPublic = resolveIsPublic(graphId); - if (isCacheEnabled) { - if (federatedStoreCache.contains(graphId)) { - Graph graph = federatedStoreCache.getFromCache(graphId); - addGraphs(graphAuths, null, isPublic, graph); - } + if (isCacheEnabled && federatedStoreCache.contains(graphId)) { + Graph graph = federatedStoreCache.getFromCache(graphId); + addGraphs(graphAuths, null, isPublic, graph); } else { final Graph.Builder builder = new Builder() .config(new GraphConfig.Builder() @@ -479,7 +478,9 @@ private void _add(final Graph newGraph, final FederatedAccess access) { private void addToCache(final Graph newGraph) { final String graphId = newGraph.getGraphId(); - if (!federatedStoreCache.contains(graphId)) { + if (federatedStoreCache.contains(graphId)) { + validateSameAsFromCache(newGraph, graphId); + } else { try { federatedStoreCache.addGraphToCache(newGraph, false); } catch (final OverwritingException e) { @@ -487,17 +488,25 @@ private void addToCache(final Graph newGraph) { } catch (final Exception e) { throw new RuntimeException(e); } + } + } + + private void validateSameAsFromCache(final Graph newGraph, final String graphId) { + final Graph fromCache = federatedStoreCache.getFromCache(graphId); + if (!newGraph.getStoreProperties().getProperties().equals(fromCache.getStoreProperties().getProperties())) { + throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, GraphConfigEnum.PROPERTIES.toString(), graphId)); } else { - final Graph fromCache = federatedStoreCache.getFromCache(graphId); - if (newGraph.getStoreProperties().getProperties().equals(fromCache.getStoreProperties().getProperties()) - && JsonUtil.equals(newGraph.getSchema().toJson(false), fromCache.getSchema().toJson(false)) - && newGraph.getGraphId().equals(fromCache.getGraphId())) { - throw new RuntimeException(String.format("Error adding graph, GraphId is known within the cache, but is different. GraphId: %s", graphId)); + if (!JsonUtil.equals(newGraph.getSchema().toJson(false), fromCache.getSchema().toJson(false))) { + throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, GraphConfigEnum.SCHEMA.toString(), graphId)); + } else { + if (!newGraph.getGraphId().equals(fromCache.getGraphId())) { + throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, "GraphId", graphId)); + } } } } - private void validateCacheProperties() throws StoreException { + private void validateCacheProperties() { if (getProperties().getCacheProperties() != null) { isCacheEnabled = true; } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java index ad78d32aaf0..7a5e1e66863 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java @@ -89,7 +89,6 @@ public Graph getFromCache(final String graphId) { * Delete the {@link uk.gov.gchq.gaffer.graph.Graph} related to the specified ID from the cache. * * @param graphId the ID of the {@link uk.gov.gchq.gaffer.graph.Graph} to be deleted - * @throws CacheOperationException if there was an error trying to delete from the cache */ public void deleteFromCache(final String graphId) { CacheServiceLoader.getService().removeFromCache(CACHE_SERVICE_NAME, graphId); diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java index 31d5c513860..59b3f991a9b 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java @@ -171,6 +171,11 @@ public enum GraphConfigEnum { GraphConfigEnum(final String value) { this.value = value; } + + @Override + public String toString() { + return value; + } } /** @@ -184,6 +189,11 @@ public enum LocationEnum { LocationEnum(final String value) { this.value = value; } + + @Override + public String toString() { + return value; + } } public String getValueOf(final String graphId, final GraphConfigEnum graphConfigEnum, final LocationEnum location) { diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java index 94eb0e07381..0aa7098ae62 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreSchemaTest.java @@ -23,7 +23,6 @@ import uk.gov.gchq.gaffer.accumulostore.AccumuloProperties; import uk.gov.gchq.gaffer.accumulostore.MockAccumuloStore; import uk.gov.gchq.gaffer.cache.CacheServiceLoader; -import uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException; import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; import uk.gov.gchq.gaffer.operation.Operation; import uk.gov.gchq.gaffer.store.Context; @@ -110,13 +109,9 @@ public void shouldNotDeadLockWhenPreviousAddGraphHasSchemaCollision() throws Exc .build()), TEST_CONTEXT); } catch (final Exception e) { addingGraphBWasSuccessful = false; - if (e instanceof SchemaException) { assertEquals("Element group properties cannot be defined in different" + " schema parts, they must all be defined in a single " + - "schema part. Please fix this group: e1", e.getMessage()); - } else { - throw e; - } + "schema part. Please fix this group: e1", e.getCause().getMessage()); } try { diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index 882aed6e2ab..cebe02a0211 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -32,6 +32,7 @@ import uk.gov.gchq.gaffer.data.element.Element; import uk.gov.gchq.gaffer.data.element.Entity; import uk.gov.gchq.gaffer.data.elementdefinition.view.View; +import uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties.GraphConfigEnum; import uk.gov.gchq.gaffer.federatedstore.integration.FederatedStoreITs; import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; import uk.gov.gchq.gaffer.federatedstore.operation.GetAllGraphIds; @@ -64,7 +65,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static uk.gov.gchq.gaffer.federatedstore.FederatedGraphStorage.USER_IS_ATTEMPTING_TO_OVERWRITE; +import static uk.gov.gchq.gaffer.federatedstore.FederatedStore.ERROR_ADDING_GRAPH_TO_CACHE; import static uk.gov.gchq.gaffer.federatedstore.FederatedStore.S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2; import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreUser.TEST_USER; import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreUser.authUser; @@ -151,7 +152,7 @@ public void shouldThrowErrorForFailedSchema() throws Exception { store.initialise(FEDERATED_STORE_ID, null, federatedProperties); fail(EXCEPTION_NOT_THROWN); } catch (final IllegalArgumentException e) { - assertEquals(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Schema", "graphId: " + MAP_ID_1 + " schemaPath: " + PATH_INVALID), e.getMessage()); + assertEquals(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Schema", "GraphId: " + MAP_ID_1 + " schemaPath: " + PATH_INVALID), e.getMessage()); } } @@ -167,7 +168,7 @@ public void shouldThrowErrorForFailedProperty() throws Exception { store.initialise(FEDERATED_STORE_ID, null, federatedProperties); fail(EXCEPTION_NOT_THROWN); } catch (final IllegalArgumentException e) { - assertEquals(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Property", "graphId: " + MAP_ID_1 + " propertyPath: " + PATH_INVALID), e.getMessage()); + assertEquals(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Property", "GraphId: " + MAP_ID_1 + " propertyPath: " + PATH_INVALID), e.getMessage()); } } @@ -202,7 +203,7 @@ public void shouldNotAllowOverwritingOfGraphWithFederatedScope() throws Exceptio .build()); fail(EXCEPTION_NOT_THROWN); } catch (final Exception e) { - assertEquals(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, ACC_ID_1), e.getMessage()); + assertEquals(String.format(ERROR_ADDING_GRAPH_TO_CACHE, GraphConfigEnum.SCHEMA, ACC_ID_1), e.getMessage()); } } @@ -1051,16 +1052,15 @@ public void shouldThrowExceptionWithoutInitialisation() { } @Test - public void shouldThrowExceptionWhenInitialisedWithNoCacheClassInProperties() throws StoreException { + public void shouldNotThrowExceptionWhenInitialisedWithNoCacheClassInProperties() throws StoreException { // Given StoreProperties storeProperties = new StoreProperties(); // When / Then try { store.initialise(FEDERATED_STORE_ID, null, storeProperties); - fail(EXCEPTION_NOT_THROWN); } catch (final StoreException e) { - assertTrue(e.getMessage().contains("No cache has been set, please check the property")); + fail("FederatedStore does not have to have a cache."); } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java index 03f22ce139b..b037b41f725 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java @@ -19,15 +19,11 @@ import com.google.common.collect.Sets; import org.junit.Before; import org.junit.Test; -import org.mockito.BDDMockito; -import org.mockito.Mockito; import uk.gov.gchq.gaffer.accumulostore.AccumuloProperties; import uk.gov.gchq.gaffer.accumulostore.SingleUseMockAccumuloStore; import uk.gov.gchq.gaffer.cache.CacheServiceLoader; -import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; -import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.data.element.Element; import uk.gov.gchq.gaffer.federatedstore.FederatedStore; import uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties; @@ -39,6 +35,7 @@ import uk.gov.gchq.gaffer.store.Context; import uk.gov.gchq.gaffer.store.StoreProperties; import uk.gov.gchq.gaffer.store.library.GraphLibrary; +import uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary; import uk.gov.gchq.gaffer.store.schema.Schema; import uk.gov.gchq.gaffer.user.User; @@ -155,10 +152,9 @@ public void shouldAddGraphUsingLibrary() throws Exception { assertEquals(EXPECTED_GRAPH_ID, next.getGraphId()); assertEquals(expectedSchema, next.getSchema()); - final GraphLibrary mock = Mockito.mock(GraphLibrary.class); - BDDMockito.given(mock.get(EXPECTED_GRAPH_ID_2)).willReturn(new Pair<>(expectedSchema, storeProperties)); - BDDMockito.given(mock.exists(EXPECTED_GRAPH_ID_2)).willReturn(true); - store.setGraphLibrary(mock); + final GraphLibrary library = new HashMapGraphLibrary(); + library.add(EXPECTED_GRAPH_ID_2,expectedSchema,storeProperties); + store.setGraphLibrary(library); federatedAddGraphHandler.doOperation( new AddGraph.Builder() @@ -178,8 +174,6 @@ public void shouldAddGraphUsingLibrary() throws Exception { assertTrue(set.contains(EXPECTED_GRAPH_ID)); assertTrue(set.contains(EXPECTED_GRAPH_ID_2)); - - Mockito.verify(mock, Mockito.times(3)).get(EXPECTED_GRAPH_ID_2); } @Test @@ -212,8 +206,8 @@ public void shouldNotOverwriteGraph() throws Exception { .build(), new Context(testUser), store); - } catch (final OverwritingException e) { - assertEquals(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, EXPECTED_GRAPH_ID), e.getMessage()); + } catch (final Exception e) { + assertEquals(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, EXPECTED_GRAPH_ID), e.getCause().getMessage()); } } From 3b613a56d8fd10b4068eb8c1f790e58c103b8b9d Mon Sep 17 00:00:00 2001 From: m55624 Date: Mon, 16 Oct 2017 20:32:30 +0100 Subject: [PATCH 48/72] gh-1297 - small Graph fix --- core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java index 5bfe7705d61..17dddbaef2c 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java @@ -878,7 +878,6 @@ private void updateStoreProperties(final GraphConfig config) { mergedStoreProperties.setId(null); } mergedStoreProperties.getProperties().putAll(properties.getProperties()); - mergedStoreProperties.setId(null); } } properties = mergedStoreProperties; From ec9230dee30984a297a6d86534d7e0557b53f087 Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Tue, 17 Oct 2017 12:25:23 +0100 Subject: [PATCH 49/72] gh-1297 FederatedStore tests added for re-startup of graphs added post initialisation. --- .../gaffer/federatedstore/FederatedStore.java | 49 ++++--- .../FederatedStoreMultiCacheTest.java | 133 ++++++++++++++++++ 2 files changed, 166 insertions(+), 16 deletions(-) create mode 100644 store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreMultiCacheTest.java diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index bc7c4df7bda..a7ffdded36f 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -382,23 +382,40 @@ private void loadGraphs() throws StoreException { final boolean isPublic = resolveIsPublic(graphId); if (isCacheEnabled && federatedStoreCache.contains(graphId)) { - Graph graph = federatedStoreCache.getFromCache(graphId); - addGraphs(graphAuths, null, isPublic, graph); + makeGraphFromCache(graphId, graphAuths, isPublic); } else { - final Graph.Builder builder = new Builder() - .config(new GraphConfig.Builder() - .graphId(graphId) - .library(getGraphLibrary()) - .build()) - .addParentSchemaIds(getValueOf(graphId, SCHEMA, ID)) - .parentStorePropertiesId(getValueOf(graphId, PROPERTIES, ID)); - - addPropertiesFromFile(graphId, builder); - addSchemaFromFile(graphId, builder); - - addGraphs(graphAuths, null, isPublic, builder); + makeGraphFromProperties(graphId, graphAuths, isPublic); } } + makeGraphsRemainingInCache(graphIds); + } + + private void makeGraphFromCache(final String graphId, final Set graphAuths, final boolean isPublic) { + Graph graph = federatedStoreCache.getFromCache(graphId); + addGraphs(graphAuths, null, isPublic, graph); + } + + private void makeGraphFromProperties(final String graphId, final Set graphAuths, final boolean isPublic) throws StoreException { + final Builder builder = new Builder() + .config(new GraphConfig.Builder() + .graphId(graphId) + .library(getGraphLibrary()) + .build()) + .addParentSchemaIds(getValueOf(graphId, SCHEMA, ID)) + .parentStorePropertiesId(getValueOf(graphId, PROPERTIES, ID)); + + addPropertiesFromFile(graphId, builder); + addSchemaFromFile(graphId, builder); + + addGraphs(graphAuths, null, isPublic, builder); + } + + private void makeGraphsRemainingInCache(final Set graphIds) { + final Set allGraphIds = federatedStoreCache.getAllGraphIds(); + allGraphIds.removeAll(graphIds); + for (String graphId : allGraphIds) { + makeGraphFromCache(graphId, null, false); + } } private boolean resolveIsPublic(final String graphId) { @@ -410,7 +427,7 @@ private Set resolveAuths(final String graphId) { return Strings.isNullOrEmpty(value) ? null : Sets.newHashSet(getCleanStrings(value)); } - private void addGraphs(final Set graphAuths, final String userId, final boolean isPublic, final Builder... builders) throws StoreException { + private void addGraphs(final Set graphAuths, final String addingUserId, final boolean isPublic, final Builder... builders) throws StoreException { for (final Builder builder : builders) { final Graph graph; try { @@ -418,7 +435,7 @@ private void addGraphs(final Set graphAuths, final String userId, final } catch (final Exception e) { throw new IllegalArgumentException(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Graph", ""), e); } - addGraphs(graphAuths, userId, isPublic, graph); + addGraphs(graphAuths, addingUserId, isPublic, graph); } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreMultiCacheTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreMultiCacheTest.java new file mode 100644 index 00000000000..d609b2ca90a --- /dev/null +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreMultiCacheTest.java @@ -0,0 +1,133 @@ +/* + * Copyright 2017 Crown Copyright + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.gchq.gaffer.federatedstore; + +import org.junit.Before; +import org.junit.Test; + +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; +import uk.gov.gchq.gaffer.commonutil.StreamUtil; +import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; +import uk.gov.gchq.gaffer.mapstore.MapStoreProperties; +import uk.gov.gchq.gaffer.store.Context; +import uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary; +import uk.gov.gchq.gaffer.store.schema.Schema; +import uk.gov.gchq.gaffer.user.User; + +import java.util.Collection; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class FederatedStoreMultiCacheTest { + + public static final String FEDERATED_STORE_ID = "testFederatedStoreId"; + public static final String MAP_ID_1 = "mockMapGraphId1"; + public static final String PATH_MAP_STORE_PROPERTIES = "properties/singleUseMockMapStore.properties"; + public static final String PATH_BASIC_ENTITY_SCHEMA_JSON = "schema/basicEntitySchema.json"; + public static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; + public static User authUser = FederatedStoreUser.authUser(); + public static User testUser = FederatedStoreUser.testUser(); + public FederatedStore store; + public FederatedStoreProperties federatedStoreProperties; + public Collection originalStoreIds; + public FederatedStore store2; + public User blankUser; + + @Before + public void setUp() throws Exception { + HashMapGraphLibrary.clear(); + CacheServiceLoader.shutdown(); + federatedStoreProperties = new FederatedStoreProperties(); + federatedStoreProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); + store = new FederatedStore(); + store.initialise(FEDERATED_STORE_ID, null, federatedStoreProperties); + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1) + .graphAuths(FederatedStoreUser.AUTH_1) + .isPublic(false) + .storeProperties(MapStoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES)) + .schema(Schema.fromJson(StreamUtil.openStream(Schema.class, PATH_BASIC_ENTITY_SCHEMA_JSON))) + .build(), new Context.Builder() + .user(testUser) + .build()); + + + store2 = new FederatedStore(); + store2.initialise(FEDERATED_STORE_ID + 1, null, federatedStoreProperties); + blankUser = FederatedStoreUser.blankUser(); + } + + @Test + public void shouldInitialiseByCacheToContainSameGraphsForAddingUser() throws Exception { + originalStoreIds = store.getAllGraphIds(testUser); + final int firstStoreSize = originalStoreIds.size(); + assertEquals("adding user should have visibility of first store graphs", 1, firstStoreSize); + Collection storeGetIds2 = store2.getAllGraphIds(testUser); + assertEquals("adding user should have same visibility of second store graphs", firstStoreSize, storeGetIds2.size()); + assertTrue(originalStoreIds.containsAll(storeGetIds2)); + } + + @Test + public void shouldInitialiseByCacheToContainSameGraphsForAuthUser() throws Exception { + originalStoreIds = store.getAllGraphIds(authUser); + final int firstStoreSize = originalStoreIds.size(); + + assertEquals("auth user should have visibility of first store graphs", 1, firstStoreSize); + Collection storeGetIds2 = store2.getAllGraphIds(authUser); + assertEquals("auth user should have same visibility of second store graphs", firstStoreSize, storeGetIds2.size()); + assertTrue(originalStoreIds.containsAll(storeGetIds2)); + } + + @Test + public void shouldInitialiseByCacheToContainSameGraphsForBlankUser() throws Exception { + originalStoreIds = store.getAllGraphIds(blankUser); + final int firstStoreSize = originalStoreIds.size(); + + assertEquals("There should be 1 graphs", 1, store.getAllGraphIds(testUser).size()); + + assertEquals("blank user should not have visibility of first store graphs", 0, firstStoreSize); + Collection storeGetIds2 = store2.getAllGraphIds(blankUser); + assertEquals("blank user should have same visibility of second store graphs", firstStoreSize, storeGetIds2.size()); + assertEquals("blank user should have same visibility of second store graphs", firstStoreSize, storeGetIds2.size()); + assertTrue(originalStoreIds.containsAll(storeGetIds2)); + } + + + @Test + public void shouldInitialiseByCacheToContainSamePublicGraphsForBlankUser() throws Exception { + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1 + 1) + .isPublic(true) + .storeProperties(MapStoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES)) + .schema(Schema.fromJson(StreamUtil.openStream(Schema.class, PATH_BASIC_ENTITY_SCHEMA_JSON))) + .build(), new Context.Builder() + .user(testUser) + .build()); + + + assertEquals("There should be 2 graphs", 2, store.getAllGraphIds(testUser).size()); + + originalStoreIds = store.getAllGraphIds(blankUser); + final int firstStoreSize = originalStoreIds.size(); + + assertEquals("blank user should have visibility of public graph", 1, firstStoreSize); + Collection storeGetIds2 = store2.getAllGraphIds(blankUser); + assertEquals("blank user should have same visibility of second store graphs", firstStoreSize, storeGetIds2.size()); + assertTrue(originalStoreIds.containsAll(storeGetIds2)); + } +} From fbd55f10c15b57f49de9e34d38e6aaca4c9b6fba Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Wed, 18 Oct 2017 21:16:11 +0100 Subject: [PATCH 50/72] gh-1297 FederatedStore FederatedAccess added to cache, to pass tests. --- .../gaffer/federatedstore/FederatedStore.java | 43 +++++++------ .../federatedstore/FederatedStoreCache.java | 61 +++++++++++-------- .../FederatedStoreCacheTest.java | 14 ++--- .../FederatedStoreMultiCacheTest.java | 2 + 4 files changed, 71 insertions(+), 49 deletions(-) diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index a7ffdded36f..ecfbdbd1014 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -22,6 +22,7 @@ import org.apache.commons.lang3.StringUtils; import uk.gov.gchq.gaffer.cache.CacheServiceLoader; +import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; import uk.gov.gchq.gaffer.commonutil.JsonUtil; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; @@ -189,12 +190,16 @@ public static OP updateOperationForGraph(final OP operati */ public void addGraphs(final Set graphAuths, final String addingUserId, final boolean isPublic, final Graph... graphs) { + FederatedAccess access = new FederatedAccess(graphAuths, addingUserId, isPublicAccessAllowed && isPublic); + + addGraphs(access, graphs); + } + + public void addGraphs(final FederatedAccess access, final Graph... graphs) { if (isCacheEnabled || !isInitialised) { validateCache(); } - FederatedAccess access = new FederatedAccess(graphAuths, addingUserId, isPublicAccessAllowed && isPublic); - for (final Graph graph : graphs) { _add(graph, access); } @@ -378,21 +383,21 @@ private void loadCustomPropertiesAuths() { private void loadGraphs() throws StoreException { final Set graphIds = getGraphIds(); for (final String graphId : graphIds) { - final Set graphAuths = resolveAuths(graphId); - final boolean isPublic = resolveIsPublic(graphId); - if (isCacheEnabled && federatedStoreCache.contains(graphId)) { - makeGraphFromCache(graphId, graphAuths, isPublic); + makeGraphFromCache(graphId); } else { - makeGraphFromProperties(graphId, graphAuths, isPublic); + makeGraphFromProperties(graphId, resolveAuths(graphId), resolveIsPublic(graphId)); } } - makeGraphsRemainingInCache(graphIds); + if (isCacheEnabled) { + makeGraphsRemainingInCache(graphIds); + } } - private void makeGraphFromCache(final String graphId, final Set graphAuths, final boolean isPublic) { + private void makeGraphFromCache(final String graphId) { Graph graph = federatedStoreCache.getFromCache(graphId); - addGraphs(graphAuths, null, isPublic, graph); + final FederatedAccess accessFromCache = federatedStoreCache.getAccessFromCache(graphId); + addGraphs(accessFromCache, graph); } private void makeGraphFromProperties(final String graphId, final Set graphAuths, final boolean isPublic) throws StoreException { @@ -411,10 +416,12 @@ private void makeGraphFromProperties(final String graphId, final Set gra } private void makeGraphsRemainingInCache(final Set graphIds) { - final Set allGraphIds = federatedStoreCache.getAllGraphIds(); - allGraphIds.removeAll(graphIds); - for (String graphId : allGraphIds) { - makeGraphFromCache(graphId, null, false); + if (isCacheEnabled) { + final Set allGraphIds = Sets.newHashSet(federatedStoreCache.getAllGraphIds()); + allGraphIds.removeAll(graphIds); + for (String graphId : allGraphIds) { + makeGraphFromCache(graphId); + } } } @@ -483,7 +490,7 @@ private Set getGraphIds() { private void _add(final Graph newGraph, final FederatedAccess access) { if (isCacheEnabled) { - addToCache(newGraph); + addToCache(newGraph, access); } graphStorage.put(newGraph, access); @@ -493,16 +500,16 @@ private void _add(final Graph newGraph, final FederatedAccess access) { } } - private void addToCache(final Graph newGraph) { + private void addToCache(final Graph newGraph, final FederatedAccess access) { final String graphId = newGraph.getGraphId(); if (federatedStoreCache.contains(graphId)) { validateSameAsFromCache(newGraph, graphId); } else { try { - federatedStoreCache.addGraphToCache(newGraph, false); + federatedStoreCache.addGraphToCache(newGraph, access, false); } catch (final OverwritingException e) { throw new OverwritingException((String.format("User is attempting to overwrite a graph within the cacheService. GraphId: %s", graphId))); - } catch (final Exception e) { + } catch (CacheOperationException e) { throw new RuntimeException(e); } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java index 7a5e1e66863..0129a6b3bc0 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java @@ -18,20 +18,23 @@ import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.cache.ICache; +import uk.gov.gchq.gaffer.cache.ICacheService; import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; +import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.graph.Graph; import uk.gov.gchq.gaffer.graph.GraphSerialisable; +import java.util.Collections; import java.util.Set; /** - * Wrapper around the {@link CacheServiceLoader} to provide an - * interface for handling the {@link uk.gov.gchq.gaffer.graph.Graph}s - * within a {@link uk.gov.gchq.gaffer.federatedstore.FederatedStore}. + * Wrapper around the {@link CacheServiceLoader} to provide an interface for + * handling the {@link Graph}s within a {@link uk.gov.gchq.gaffer.federatedstore.FederatedStore}. */ public class FederatedStoreCache { private static final String CACHE_SERVICE_NAME = "federatedStoreGraphs"; + public static final String ERROR_ADDING_GRAPH_TO_CACHE_GRAPH_ID_S = "Error adding graph to cache. graphId: %s"; /** * Get the cache for the FederatedStore. @@ -47,50 +50,60 @@ public ICache getCache() { } /** - * Get all the ID's related to the {@link uk.gov.gchq.gaffer.graph.Graph}'s - * stored in the cache. + * Get all the ID's related to the {@link Graph}'s stored in the cache. * - * @return all the Graph ID's within the cache + * @return all the Graph ID's within the cache as unmodifiable set. */ public Set getAllGraphIds() { - return CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME); + final Set allKeysFromCache = CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME); + return (null == allKeysFromCache) ? null : Collections.unmodifiableSet(allKeysFromCache); } /** - * Add the specified {@link uk.gov.gchq.gaffer.graph.Graph} to the cache. + * Add the specified {@link Graph} to the cache. * - * @param graph the {@link uk.gov.gchq.gaffer.graph.Graph} to be added + * @param graph the {@link Graph} to be added * @param overwrite if true, overwrite any graphs already in the cache with the same ID * @throws CacheOperationException if there was an error trying to add to the cache */ - public void addGraphToCache(final Graph graph, final boolean overwrite) throws CacheOperationException { - GraphSerialisable graphSerialisable = new GraphSerialisable.Builder().graph(graph).build(); - - if (overwrite) { - CacheServiceLoader.getService().putInCache(CACHE_SERVICE_NAME, graph.getGraphId(), graphSerialisable); - } else { - CacheServiceLoader.getService().putSafeInCache(CACHE_SERVICE_NAME, graph.getGraphId(), graphSerialisable); + public void addGraphToCache(final Graph graph, final FederatedAccess access, final boolean overwrite) throws CacheOperationException { + Pair pair = new Pair<>(new GraphSerialisable.Builder().graph(graph).build(), access); + final ICacheService service = CacheServiceLoader.getService(); + try { + if (overwrite) { + service.putInCache(CACHE_SERVICE_NAME, graph.getGraphId(), pair); + } else { + service.putSafeInCache(CACHE_SERVICE_NAME, graph.getGraphId(), pair); + } + } catch (final CacheOperationException e) { + throw new CacheOperationException(String.format(ERROR_ADDING_GRAPH_TO_CACHE_GRAPH_ID_S, graph.getGraphId()), e); } } /** - * Retrieve the {@link uk.gov.gchq.gaffer.graph.Graph} with the specified ID from the cache. + * Retrieve the {@link Graph} with the specified ID from the cache. * - * @param graphId the ID of the {@link uk.gov.gchq.gaffer.graph.Graph} to retrieve - * @return the {@link uk.gov.gchq.gaffer.graph.Graph} related to the specified ID + * @param graphId the ID of the {@link Graph} to retrieve + * @return the {@link Graph} related to the specified ID */ - public Graph getFromCache(final String graphId) { - final GraphSerialisable graphSerialisable = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); + public Graph getFromCache(final String graphId) { + final Pair fromCache = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); + final GraphSerialisable graphSerialisable = (null == fromCache) ? null : fromCache.getFirst(); return (null == graphSerialisable) ? null : graphSerialisable.buildGraph(); } + public FederatedAccess getAccessFromCache(final String graphId) { + final Pair fromCache = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); + return fromCache.getSecond(); + } + /** - * Delete the {@link uk.gov.gchq.gaffer.graph.Graph} related to the specified ID from the cache. + * Delete the {@link Graph} related to the specified ID from the cache. * - * @param graphId the ID of the {@link uk.gov.gchq.gaffer.graph.Graph} to be deleted + * @param graphId the ID of the {@link Graph} to be deleted */ - public void deleteFromCache(final String graphId) { + public void deleteFromCache(final String graphId) { CacheServiceLoader.getService().removeFromCache(CACHE_SERVICE_NAME, graphId); } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java index d6fe3e782a8..b4ecd0634d3 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java @@ -62,7 +62,7 @@ public void beforeEach() throws CacheOperationException { @Test public void shouldAddAndGetGraphToCache() throws CacheOperationException { - federatedStoreCache.addGraphToCache(testGraph, false); + federatedStoreCache.addGraphToCache(testGraph, null, false); Graph cached = federatedStoreCache.getFromCache(MAP_ID_1); assertEquals(testGraph.getGraphId(), cached.getGraphId()); @@ -72,7 +72,7 @@ public void shouldAddAndGetGraphToCache() throws CacheOperationException { @Test public void shouldGetAllGraphIdsFromCache() throws CacheOperationException { - federatedStoreCache.addGraphToCache(testGraph, false); + federatedStoreCache.addGraphToCache(testGraph, null, false); Set cachedGraphIds = federatedStoreCache.getAllGraphIds(); assertEquals(1, cachedGraphIds.size()); assertTrue(cachedGraphIds.contains(testGraph.getGraphId())); @@ -80,7 +80,7 @@ public void shouldGetAllGraphIdsFromCache() throws CacheOperationException { @Test public void shouldDeleteFromCache() throws CacheOperationException { - federatedStoreCache.addGraphToCache(testGraph, false); + federatedStoreCache.addGraphToCache(testGraph, null, false); Set cachedGraphIds = federatedStoreCache.getAllGraphIds(); assertEquals(1, cachedGraphIds.size()); assertTrue(cachedGraphIds.contains(testGraph.getGraphId())); @@ -92,9 +92,9 @@ public void shouldDeleteFromCache() throws CacheOperationException { @Test public void shouldThrowExceptionIfGraphAlreadyExistsInCache() throws CacheOperationException { - federatedStoreCache.addGraphToCache(testGraph, false); + federatedStoreCache.addGraphToCache(testGraph, null, false); try { - federatedStoreCache.addGraphToCache(testGraph, false); + federatedStoreCache.addGraphToCache(testGraph, null, false); fail("Exception expected"); } catch (OverwritingException e) { assertTrue(e.getMessage().contains("Cache entry already exists")); @@ -103,14 +103,14 @@ public void shouldThrowExceptionIfGraphAlreadyExistsInCache() throws CacheOperat @Test public void shouldThrowExceptionIfGraphIdToBeRemovedIsNull() throws CacheOperationException { - federatedStoreCache.addGraphToCache(testGraph, false); + federatedStoreCache.addGraphToCache(testGraph, null, false); federatedStoreCache.deleteFromCache(null); assertEquals(1, federatedStoreCache.getAllGraphIds().size()); } @Test public void shouldThrowExceptionIfGraphIdToGetIsNull() throws CacheOperationException { - federatedStoreCache.addGraphToCache(testGraph, false); + federatedStoreCache.addGraphToCache(testGraph, null, false); assertNull(federatedStoreCache.getFromCache(null)); } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreMultiCacheTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreMultiCacheTest.java index d609b2ca90a..bc1e8f5bf67 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreMultiCacheTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreMultiCacheTest.java @@ -119,6 +119,8 @@ public void shouldInitialiseByCacheToContainSamePublicGraphsForBlankUser() throw .user(testUser) .build()); + store2 = new FederatedStore(); + store2.initialise(FEDERATED_STORE_ID + 1, null, federatedStoreProperties); assertEquals("There should be 2 graphs", 2, store.getAllGraphIds(testUser).size()); From 324e2c41e5527f58544d7bcd8412adab64c74f4c Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Thu, 19 Oct 2017 17:51:30 +0100 Subject: [PATCH 51/72] gh-1297 MIGRATION Cache Added to FederatedGraphStorage and Init of FederatedStore Graphs has been delete. Tests modified and bugs found. --- .../gchq/gaffer/graph/GraphSerialisable.java | 10 + .../export/graph/handler/GraphDelegate.java | 38 +- .../gov/gchq/gaffer/operation/Operation.java | 16 + .../federatedstore/FederatedGraphStorage.java | 109 +++ .../gaffer/federatedstore/FederatedStore.java | 201 +---- .../federatedstore/FederatedStoreCache.java | 15 +- .../FederatedStoreConstants.java | 7 + .../FederatedStoreProperties.java | 126 --- .../FederatedOperationAddElementsHandler.java | 4 +- .../handler/FederatedOperationHandler.java | 4 +- .../FederatedOperationOutputHandler.java | 3 +- .../impl/FederatedAddGraphHandler.java | 14 +- .../FederatedStoreAuthTest.java | 1 - .../FederatedStoreCacheTest.java | 4 +- .../FederatedStorePublicAccessTest.java | 70 +- .../federatedstore/FederatedStoreTest.java | 786 ++++++++---------- .../FederatedStoreWrongGraphIDsTest.java | 7 +- .../FederatedOperationHandlerTest.java | 9 +- .../properties/federatedStoreTest.properties | 28 - .../singleUseMockAccStore.properties | 2 +- .../singleUseMockMapStore.properties | 2 +- .../singleUseMockMapStoreAlt.properties | 2 +- .../resources/schema/basicEdgeSchema.json | 1 + .../resources/schema/basicEntitySchema.json | 1 + .../schema/basicEntitySchemaWithSchemaId.json | 25 - 25 files changed, 628 insertions(+), 857 deletions(-) delete mode 100644 store-implementation/federated-store/src/test/resources/properties/federatedStoreTest.properties delete mode 100644 store-implementation/federated-store/src/test/resources/schema/basicEntitySchemaWithSchemaId.json diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java index 2da48f3c02a..7da5cf0c59c 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java @@ -23,6 +23,7 @@ import uk.gov.gchq.gaffer.commonutil.ToStringBuilder; import uk.gov.gchq.gaffer.exception.SerialisationException; import uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser; +import uk.gov.gchq.gaffer.store.library.GraphLibrary; import uk.gov.gchq.gaffer.store.schema.Schema; import java.io.Serializable; @@ -59,10 +60,19 @@ private GraphSerialisable(final GraphConfig config, final Schema schema, final P * class. */ public Graph buildGraph() { + return buildGraph(null); + } + + /** + * @return returns a new {@link Graph} built from the contents of a this + * class. + */ + public Graph buildGraph(final GraphLibrary library) { return new Graph.Builder() .addSchema(schema) .addStoreProperties(properties) .config(config) + .config(new GraphConfig.Builder().library(library).build()) .build(); } diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java index ec1c01c503c..f9eacceabac 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java @@ -16,6 +16,7 @@ package uk.gov.gchq.gaffer.operation.export.graph.handler; +import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.graph.Graph; import uk.gov.gchq.gaffer.graph.Graph.Builder; import uk.gov.gchq.gaffer.graph.GraphConfig; @@ -39,6 +40,10 @@ * @see ExportToOtherGraphHandler */ public final class GraphDelegate { + + public static final String SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S = "Schema could not be found in the graphLibrary with id: %s"; + public static final String STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S = "Store properties could not be found in the graphLibrary with id: %s"; + private GraphDelegate() { // Private constructor to prevent instantiation. } @@ -174,29 +179,46 @@ public static void validate(final Store store, final String graphId, final Schem result.addError("parentStorePropertiesId cannot be used without a GraphLibrary"); } } else if (graphLibrary.exists(graphId)) { + if (null != parentSchemaIds) { - result.addError("GraphId " + graphId + " already exists so you cannot use a different schema. Do not set the parentSchemaIds field."); - } - if (null != schema) { - result.addError("GraphId " + graphId + " already exists so you cannot provide a different schema. Do not set the schema field."); + Schema.Builder IdFromLibrary = new Schema.Builder(); + for (String parentSchemaId : parentSchemaIds) { + IdFromLibrary.merge(graphLibrary.getSchema(parentSchemaId)); + } + Schema fromLibrary = graphLibrary.get(graphId).getFirst(); + if (!fromLibrary.toString().equals(IdFromLibrary.build().toString())) { + result.addError("GraphId " + graphId + " already exists so you cannot use a different schema. Do not set the parentSchemaIds field."); + } } + if (null != parentStorePropertiesId) { - result.addError("GraphId " + graphId + " already exists so you cannot use different store properties. Do not set the parentStorePropertiesId field."); + StoreProperties fromLibrary = graphLibrary.get(graphId).getSecond(); + if (!fromLibrary.equals(graphLibrary.getProperties(parentStorePropertiesId))) { + result.addError("GraphId " + graphId + " already exists so you cannot use different store properties. Do not set the parentStorePropertiesId field."); + } + } + + Pair schemaStorePropertiesPair = graphLibrary.get(graphId); + Schema schemaFromLib = schemaStorePropertiesPair.getFirst(); + if (null != schema && !schema.toString().equals(schemaFromLib.toString())) { + result.addError("GraphId " + graphId + " already exists so you cannot provide a different schema. Do not set the schema field."); } - if (null != storeProperties) { + + StoreProperties propertyFromLib = schemaStorePropertiesPair.getSecond(); + if (null != storeProperties && !propertyFromLib.equals(storeProperties)) { result.addError("GraphId " + graphId + " already exists so you cannot provide different store properties. Do not set the storeProperties field."); } } else { if (null != parentSchemaIds) { for (final String exportParentSchemaId : parentSchemaIds) { if (null == store.getGraphLibrary().getSchema(exportParentSchemaId)) { - result.addError("Schema could not be found in the graphLibrary with id: " + parentSchemaIds); + result.addError(String.format(SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, parentSchemaIds)); } } } if (null != parentStorePropertiesId) { if (null == store.getGraphLibrary().getProperties(parentStorePropertiesId)) { - result.addError("Store properties could not be found in the graphLibrary with id: " + parentStorePropertiesId); + result.addError(String.format(STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, parentStorePropertiesId)); } } } diff --git a/core/operation/src/main/java/uk/gov/gchq/gaffer/operation/Operation.java b/core/operation/src/main/java/uk/gov/gchq/gaffer/operation/Operation.java index 0c29c6edd34..f9b18512702 100644 --- a/core/operation/src/main/java/uk/gov/gchq/gaffer/operation/Operation.java +++ b/core/operation/src/main/java/uk/gov/gchq/gaffer/operation/Operation.java @@ -134,6 +134,22 @@ default String getOption(final String name) { return getOptions().get(name); } + /** + * Gets an operation option by its given name. + * + * @param name the name of the option + * @return the value of the option + */ + default String getOption(final String name, final String defaultValue) { + final String rtn; + if (null == getOptions()) { + rtn = defaultValue; + } else { + rtn = getOptions().get(name); + } + return (null == rtn) ? defaultValue : rtn; + } + @JsonGetter("options") default Map _getNullOrOptions() { if (null == getOptions()) { diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java index df584986d9b..fa67cbde1cd 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java @@ -18,11 +18,16 @@ import com.google.common.collect.Sets; +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; +import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; +import uk.gov.gchq.gaffer.commonutil.JsonUtil; import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException; import uk.gov.gchq.gaffer.federatedstore.util.FederatedStoreUtil; import uk.gov.gchq.gaffer.graph.Graph; +import uk.gov.gchq.gaffer.graph.GraphSerialisable; import uk.gov.gchq.gaffer.store.Context; +import uk.gov.gchq.gaffer.store.library.GraphLibrary; import uk.gov.gchq.gaffer.store.schema.Schema; import uk.gov.gchq.gaffer.store.schema.Schema.Builder; import uk.gov.gchq.gaffer.user.User; @@ -40,10 +45,23 @@ import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS; public class FederatedGraphStorage { + public static final String ERROR_ADDING_GRAPH_TO_CACHE = "Error adding graph, GraphId is known within the cache, but %s is different. GraphId: %s"; public static final String USER_IS_ATTEMPTING_TO_OVERWRITE = "User is attempting to overwrite a graph within FederatedStore. GraphId: %s"; public static final String ACCESS_IS_NULL = "Can not put graph into storage without a FederatedAccess key."; public static final String GRAPH_IDS_NOT_VISIBLE = "The following graphIds are not visible or do not exist: %s"; private Map> storage = new HashMap<>(); + private FederatedStoreCache federatedStoreCache = new FederatedStoreCache(); + private Boolean isCacheEnabled = false; + private GraphLibrary library; + + + protected void startCacheServiceLoader(final FederatedStoreProperties properties) { + if (isCachePropertiesSet(properties)) { + isCacheEnabled = true; + initCache(properties); + makeAllGraphsFromCache(); + } + } /** * places a collections of graphs into storage, protected by the given @@ -76,6 +94,10 @@ public void put(final Graph graph, final FederatedAccess access) { throw new IllegalArgumentException(ACCESS_IS_NULL); } + if (isCacheEnabled()) { + addToCache(graph, access); + } + Set existingGraphs = storage.get(access); if (null == existingGraphs) { existingGraphs = Sets.newHashSet(graph); @@ -130,6 +152,9 @@ public boolean remove(final String graphId, final User user) { for (final Graph graph : graphs) { if (graph.getGraphId().equals(graphId)) { graphs.remove(graph); + if (isCacheEnabled()) { + federatedStoreCache.deleteFromCache(graphId); + } isRemoved = true; } } @@ -247,4 +272,88 @@ private Stream getAllStream(final User user) { .filter(entry -> isValidToView(user, entry.getKey())) .flatMap(entry -> entry.getValue().stream()); } + + private void addToCache(final Graph newGraph, final FederatedAccess access) { + final String graphId = newGraph.getGraphId(); + if (federatedStoreCache.contains(graphId)) { + validateSameAsFromCache(newGraph, graphId); + } else { + try { + federatedStoreCache.addGraphToCache(newGraph, access, false); + } catch (final OverwritingException e) { + throw new OverwritingException((String.format("User is attempting to overwrite a graph within the cacheService. GraphId: %s", graphId))); + } catch (final CacheOperationException e) { + throw new RuntimeException(e); + } + } + } + + private void validateSameAsFromCache(final Graph newGraph, final String graphId) { + final Graph fromCache = federatedStoreCache.getGraphSerialisableFromCache(graphId).buildGraph(library); + if (!newGraph.getStoreProperties().getProperties().equals(fromCache.getStoreProperties().getProperties())) { + throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, GraphConfigEnum.PROPERTIES.toString(), graphId)); + } else { + if (!JsonUtil.equals(newGraph.getSchema().toJson(false), fromCache.getSchema().toJson(false))) { + throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, GraphConfigEnum.SCHEMA.toString(), graphId)); + } else { + if (!newGraph.getGraphId().equals(fromCache.getGraphId())) { + throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, "GraphId", graphId)); + } + } + } + } + + + /** + * Enum for the Graph Properties or Schema + */ + public enum GraphConfigEnum { + SCHEMA("schema"), PROPERTIES("properties"); + + private final String value; + + GraphConfigEnum(final String value) { + this.value = value; + } + + @Override + public String toString() { + return value; + } + } + + private Boolean isCacheEnabled() { + boolean rtn = false; + if (isCacheEnabled) { + if (federatedStoreCache.getCache() == null) { + throw new RuntimeException("No cache has been set, please initialise the FederatedStore instance"); + } + rtn = true; + } + return rtn; + } + + private void makeGraphFromCache(final String graphId) { + final GraphSerialisable serialisable = federatedStoreCache.getGraphSerialisableFromCache(graphId); + final Graph graph = serialisable.buildGraph(library); + final FederatedAccess accessFromCache = federatedStoreCache.getAccessFromCache(graphId); + put(graph, accessFromCache); + } + + private boolean isCachePropertiesSet(final FederatedStoreProperties properties) { + return properties.getCacheProperties() != null; + } + + private void initCache(final FederatedStoreProperties properties) { + if (federatedStoreCache.getCache() == null) { + CacheServiceLoader.initialise(properties.getProperties()); + } + } + + private void makeAllGraphsFromCache() { + final Set allGraphIds = federatedStoreCache.getAllGraphIds(); + for (String graphId : allGraphIds) { + makeGraphFromCache(graphId); + } + } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 1cafe96863e..3ea19289279 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -19,17 +19,10 @@ import com.google.common.base.Strings; import com.google.common.collect.Sets; -import uk.gov.gchq.gaffer.cache.CacheServiceLoader; -import uk.gov.gchq.gaffer.cache.exception.CacheOperationException; -import uk.gov.gchq.gaffer.commonutil.JsonUtil; -import uk.gov.gchq.gaffer.commonutil.StreamUtil; -import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; import uk.gov.gchq.gaffer.data.element.Element; import uk.gov.gchq.gaffer.data.element.id.EntityId; import uk.gov.gchq.gaffer.data.elementdefinition.view.View; -import uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties.GraphConfigEnum; -import uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties.LocationEnum; import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; import uk.gov.gchq.gaffer.federatedstore.operation.GetAllGraphIds; import uk.gov.gchq.gaffer.federatedstore.operation.RemoveGraph; @@ -46,8 +39,6 @@ import uk.gov.gchq.gaffer.federatedstore.operation.handler.impl.FederatedGetElementsHandler; import uk.gov.gchq.gaffer.federatedstore.operation.handler.impl.FederatedRemoveGraphHandler; import uk.gov.gchq.gaffer.graph.Graph; -import uk.gov.gchq.gaffer.graph.Graph.Builder; -import uk.gov.gchq.gaffer.graph.GraphConfig; import uk.gov.gchq.gaffer.operation.Operation; import uk.gov.gchq.gaffer.operation.graph.OperationView; import uk.gov.gchq.gaffer.operation.impl.Validate; @@ -72,13 +63,10 @@ import uk.gov.gchq.gaffer.store.schema.ViewValidator; import uk.gov.gchq.gaffer.user.User; -import java.io.File; -import java.nio.file.Paths; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashMap; -import java.util.List; import java.util.Map; import java.util.Set; @@ -100,18 +88,9 @@ * @see Graph */ public class FederatedStore extends Store { - public static final String ERROR_ADDING_GRAPH_TO_CACHE = "Error adding graph, GraphId is known within the cache, but %s is different. GraphId: %s"; - protected static final String S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2 = "%s was not able to be created with the supplied properties.%n%s"; - private static final GraphConfigEnum SCHEMA = GraphConfigEnum.SCHEMA; - private static final GraphConfigEnum PROPERTIES = GraphConfigEnum.PROPERTIES; - private static final LocationEnum ID = LocationEnum.ID; - private static final LocationEnum FILE = LocationEnum.FILE; - private FederatedStoreCache federatedStoreCache = new FederatedStoreCache(); private FederatedGraphStorage graphStorage = new FederatedGraphStorage(); private Set customPropertiesAuths; private Boolean isPublicAccessAllowed = Boolean.valueOf(IS_PUBLIC_ACCESS_ALLOWED_DEFAULT); - private Boolean isCacheEnabled = false; - private boolean isInitialised = false; /** * Initialise this FederatedStore with any sub-graphs defined within the @@ -125,15 +104,9 @@ public class FederatedStore extends Store { */ @Override public void initialise(final String graphId, final Schema unused, final StoreProperties properties) throws StoreException { - isInitialised = true; - setProperties(properties); - validateCacheProperties(); super.initialise(graphId, new Schema(), properties); - loadCustomPropertiesAuths(); - + customPropertiesAuths = getCustomPropertiesAuths(); isPublicAccessAllowed = Boolean.valueOf(getProperties().getIsPublicAccessAllowed()); - - loadGraphs(); } /** @@ -210,10 +183,6 @@ public void addGraphs(final Set graphAuths, final String addingUserId, f } public void addGraphs(final FederatedAccess access, final Graph... graphs) { - if (isCacheEnabled || !isInitialised) { - validateCache(); - } - for (final Graph graph : graphs) { _add(graph, access); } @@ -237,10 +206,6 @@ public void addGraphs(final Set graphAuths, final String addingUserId, f * @param user to match visibility against */ public void remove(final String graphId, final User user) { - if (isCacheEnabled) { - validateCache(); - federatedStoreCache.deleteFromCache(graphId); - } graphStorage.remove(graphId, user); } @@ -381,12 +346,8 @@ protected Object doUnhandledOperation(final Operation operation, } @Override - protected void startCacheServiceLoader(final StoreProperties properties) { - if (isCacheEnabled) { - if (federatedStoreCache.getCache() == null) { - CacheServiceLoader.initialise(properties.getProperties()); - } - } + protected void startCacheServiceLoader(final StoreProperties unused) { + graphStorage.startCacheServiceLoader(getProperties()); } private static View createValidView(final View view, final Schema delegateGraphSchema) { @@ -416,124 +377,12 @@ private static View createValidView(final View view, final Schema delegateGraphS return newView; } - private void loadCustomPropertiesAuths() { + private Set getCustomPropertiesAuths() { final String value = getProperties().getCustomPropsValue(); - if (!Strings.isNullOrEmpty(value)) { - customPropertiesAuths = Sets.newHashSet(getCleanStrings(value)); - } - } - - private void loadGraphs() throws StoreException { - final Set graphIds = getGraphIds(); - for (final String graphId : graphIds) { - if (isCacheEnabled && federatedStoreCache.contains(graphId)) { - makeGraphFromCache(graphId); - } else { - makeGraphFromProperties(graphId, resolveAuths(graphId), resolveIsPublic(graphId)); - } - } - if (isCacheEnabled) { - makeGraphsRemainingInCache(graphIds); - } - } - - private void makeGraphFromCache(final String graphId) { - Graph graph = federatedStoreCache.getFromCache(graphId); - final FederatedAccess accessFromCache = federatedStoreCache.getAccessFromCache(graphId); - addGraphs(accessFromCache, graph); - } - - private void makeGraphFromProperties(final String graphId, final Set graphAuths, final boolean isPublic) throws StoreException { - final Builder builder = new Builder() - .config(new GraphConfig.Builder() - .graphId(graphId) - .library(getGraphLibrary()) - .build()) - .addParentSchemaIds(getValueOf(graphId, SCHEMA, ID)) - .parentStorePropertiesId(getValueOf(graphId, PROPERTIES, ID)); - - addPropertiesFromFile(graphId, builder); - addSchemaFromFile(graphId, builder); - - addGraphs(graphAuths, null, isPublic, builder); - } - - private void makeGraphsRemainingInCache(final Set graphIds) { - if (isCacheEnabled) { - federatedStoreCache.getAllGraphIds().stream() - .filter(s -> !graphIds.contains(s)) - .forEach(this::makeGraphFromCache); - } - } - - private boolean resolveIsPublic(final String graphId) { - return Boolean.valueOf(getProperties().getGraphIsPublicValue(graphId)); - } - - private Set resolveAuths(final String graphId) { - final String value = getProperties().getGraphAuthsValue(graphId); - return Strings.isNullOrEmpty(value) ? null : Sets.newHashSet(getCleanStrings(value)); - } - - private void addGraphs(final Set graphAuths, final String addingUserId, final boolean isPublic, final Builder... builders) throws StoreException { - for (final Builder builder : builders) { - final Graph graph; - try { - graph = builder.build(); - } catch (final Exception e) { - throw new IllegalArgumentException(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Graph", ""), e); - } - addGraphs(graphAuths, addingUserId, isPublic, graph); - } - } - - private void addSchemaFromFile(final String graphId, final Builder builder) { - final String schemaFileValue = getValueOf(graphId, SCHEMA, FILE); - if (!Strings.isNullOrEmpty(schemaFileValue)) { - List schemas = getCleanStrings(schemaFileValue); - for (final String schemaPath : schemas) { - try { - if (new File(schemaPath).exists()) { - builder.addSchema(Paths.get(schemaPath)); - } else { - builder.addSchema(StreamUtil.openStream(FederatedStore.class, schemaPath)); - } - } catch (final Exception e) { - throw new IllegalArgumentException(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Schema", "GraphId: " + graphId + " schemaPath: " + schemaPath), e); - } - } - } - } - - private void addPropertiesFromFile(final String graphId, final Builder builder) { - final String propFileValue = getValueOf(graphId, PROPERTIES, FILE); - if (!Strings.isNullOrEmpty(propFileValue)) { - try { - builder.addStoreProperties(propFileValue); - } catch (final Exception e) { - throw new IllegalArgumentException(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Property", "GraphId: " + graphId + " propertyPath: " + propFileValue), e); - } - } - } - - private String getValueOf(final String graphId, final GraphConfigEnum graphConfigEnum, final LocationEnum locationEnum) { - return getProperties().getValueOf(graphId, graphConfigEnum, locationEnum); - } - - private Set getGraphIds() { - final HashSet graphIds = Sets.newHashSet(); - final String graphIdValue = getProperties().getGraphIdsValue(); - if (!Strings.isNullOrEmpty(graphIdValue)) { - graphIds.addAll(getCleanStrings(graphIdValue)); - } - return graphIds; + return (Strings.isNullOrEmpty(value)) ? null : Sets.newHashSet(getCleanStrings(value)); } private void _add(final Graph newGraph, final FederatedAccess access) { - if (isCacheEnabled) { - addToCache(newGraph, access); - } - graphStorage.put(newGraph, access); if (null != getGraphLibrary()) { @@ -541,45 +390,5 @@ private void _add(final Graph newGraph, final FederatedAccess access) { } } - private void addToCache(final Graph newGraph, final FederatedAccess access) { - final String graphId = newGraph.getGraphId(); - if (federatedStoreCache.contains(graphId)) { - validateSameAsFromCache(newGraph, graphId); - } else { - try { - federatedStoreCache.addGraphToCache(newGraph, access, false); - } catch (final OverwritingException e) { - throw new OverwritingException((String.format("User is attempting to overwrite a graph within the cacheService. GraphId: %s", graphId))); - } catch (final CacheOperationException e) { - throw new RuntimeException(e); - } - } - } - private void validateSameAsFromCache(final Graph newGraph, final String graphId) { - final Graph fromCache = federatedStoreCache.getFromCache(graphId); - if (!newGraph.getStoreProperties().getProperties().equals(fromCache.getStoreProperties().getProperties())) { - throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, GraphConfigEnum.PROPERTIES.toString(), graphId)); - } else { - if (!JsonUtil.equals(newGraph.getSchema().toJson(false), fromCache.getSchema().toJson(false))) { - throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, GraphConfigEnum.SCHEMA.toString(), graphId)); - } else { - if (!newGraph.getGraphId().equals(fromCache.getGraphId())) { - throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, "GraphId", graphId)); - } - } - } - } - - private void validateCacheProperties() { - if (getProperties().getCacheProperties() != null) { - isCacheEnabled = true; - } - } - - private void validateCache() { - if (federatedStoreCache.getCache() == null) { - throw new RuntimeException("No cache has been set, please initialise the FederatedStore instance"); - } - } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java index dd2c5f1dcdc..f738304846c 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java @@ -87,13 +87,24 @@ public void addGraphToCache(final Graph graph, final FederatedAccess access, fin * @param graphId the ID of the {@link Graph} to retrieve * @return the {@link Graph} related to the specified ID */ - - public Graph getFromCache(final String graphId) { + public Graph getGraphFromCache(final String graphId) { final Pair fromCache = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); final GraphSerialisable graphSerialisable = (null == fromCache) ? null : fromCache.getFirst(); return (null == graphSerialisable) ? null : graphSerialisable.buildGraph(); } + /** + * Retrieve the {@link Graph} with the specified ID from the cache. + * + * @param graphId the ID of the {@link Graph} to retrieve + * @return the {@link Graph} related to the specified ID + */ + + public GraphSerialisable getGraphSerialisableFromCache(final String graphId) { + final Pair fromCache = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); + return (null == fromCache) ? null : fromCache.getFirst(); + } + public FederatedAccess getAccessFromCache(final String graphId) { final Pair fromCache = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); return fromCache.getSecond(); diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java index 20edb0ab863..1a47e0091e3 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java @@ -16,14 +16,21 @@ package uk.gov.gchq.gaffer.federatedstore; +import uk.gov.gchq.gaffer.operation.Operation; + public final class FederatedStoreConstants { public static final String PREFIX_GAFFER_FEDERATED_STORE = "gaffer.federatedstore"; // Operation options public static final String KEY_OPERATION_OPTIONS_GRAPH_IDS = PREFIX_GAFFER_FEDERATED_STORE + ".operation.graphIds"; public static final String KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE = PREFIX_GAFFER_FEDERATED_STORE + ".operation.skipFailedFederatedStoreExecute"; + public static final String DEFAULT_VALUE_KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE = String.valueOf(false); private FederatedStoreConstants() { // private constructor to prevent users instantiating this class as it // only contains constants. } + + public static String getSkipFailedFederatedStoreExecute(Operation op) { + return op.getOption(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE, DEFAULT_VALUE_KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE); + } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java index 59b3f991a9b..d883ff04552 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java @@ -22,9 +22,6 @@ import java.io.InputStream; import java.nio.file.Path; -import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE; -import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.PREFIX_GAFFER_FEDERATED_STORE; - /** * Additional {@link StoreProperties} for the {@link FederatedStore}. @@ -42,12 +39,6 @@ public class FederatedStoreProperties extends StoreProperties { */ public static final String CUSTOM_PROPERTIES_AUTHS = "gaffer.federatedstore.customPropertiesAuths"; public static final String CUSTOM_PROPERTIES_AUTHS_DEFAULT = null; - /** - * This is used.... - * e.g gaffer.federatedstore.graphIds=graph1,graph2 - */ - public static final String GRAPH_IDS = "gaffer.federatedstore.graphIds"; - public static final String GRAPH_IDS_DEFAULT = null; /** * This is used.... @@ -80,78 +71,10 @@ public static FederatedStoreProperties loadStoreProperties(final Path storePrope return StoreProperties.loadStoreProperties(storePropertiesPath, FederatedStoreProperties.class); } - public void setGraphIds(final String graphIdsCSV) { - set(GRAPH_IDS, graphIdsCSV); - } - - public void setTrueSkipFailedExecution() { - setSkipFailedExecution(true); - } - - public void setFalseSkipFailedExecution() { - setSkipFailedExecution(false); - } - public void setCustomPropertyAuths(final String auths) { set(CUSTOM_PROPERTIES_AUTHS, auths); } - public void setGraphAuth(final String graphId, final String authCSV) { - set(getKeyGraphAuths(graphId), authCSV); - } - - public void setSkipFailedExecution(final boolean b) { - set(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE, Boolean.toString(b)); - } - - public void setGraphPropFile(final String graphId, final String file) { - final String key = getKeyGraphConfig(graphId, GraphConfigEnum.PROPERTIES, LocationEnum.FILE); - set(key, file); - } - - public void setGraphSchemaFile(final String graphId, final String file) { - final String key = getKeyGraphConfig(graphId, GraphConfigEnum.SCHEMA, LocationEnum.FILE); - set(key, file); - } - - public void setGraphPropId(final String graphId, final String id) { - final String key = getKeyGraphConfig(graphId, GraphConfigEnum.PROPERTIES, LocationEnum.ID); - set(key, id); - } - - public void setGraphSchemaId(final String graphId, final String id) { - final String key = getKeyGraphConfig(graphId, GraphConfigEnum.SCHEMA, LocationEnum.ID); - set(key, id); - } - - private static String getKeyGraphConfig(final String graphId, final GraphConfigEnum graphConfigEnum, final LocationEnum locationEnum) { - return String.format("%s.%s.%s.%s", PREFIX_GAFFER_FEDERATED_STORE, graphId, graphConfigEnum.value, locationEnum.value); - } - - private static String getKeyGraphAuths(final String graphId) { - return String.format("%s.%s.%s", PREFIX_GAFFER_FEDERATED_STORE, graphId, AUTHS); - } - - public String getGraphIsPublicValue(final String graphId) { - return get(getKeyGraphIsPublic(graphId), IS_PUBLIC_DEFAULT); - } - - public void setGraphIsPublicValue(final String graphId, final boolean b) { - set(getKeyGraphIsPublic(graphId), String.valueOf(b)); - } - - public void setTrueGraphIsPublicValue(final String graphId) { - setGraphIsPublicValue(graphId, true); - } - - public void setFalseGraphIsPublicValue(final String graphId) { - setGraphIsPublicValue(graphId, false); - } - - private String getKeyGraphIsPublic(final String graphId) { - return String.format("%s.%s.%s", PREFIX_GAFFER_FEDERATED_STORE, graphId, IS_PUBLIC); - } - public void setCacheProperties(final String cacheServiceClassString) { set(CACHE_SERVICE_CLASS, cacheServiceClassString); } @@ -160,59 +83,10 @@ public String getCacheProperties() { return get(CACHE_SERVICE_CLASS, CACHE_SERVICE_CLASS_DEFAULT); } - /** - * Enum for the Graph Properties or Schema - */ - public enum GraphConfigEnum { - SCHEMA("schema"), PROPERTIES("properties"); - - private final String value; - - GraphConfigEnum(final String value) { - this.value = value; - } - - @Override - public String toString() { - return value; - } - } - - /** - * Enum for the location of the {@link GraphConfigEnum} - */ - public enum LocationEnum { - FILE("file"), ID("id"); - - private final String value; - - LocationEnum(final String value) { - this.value = value; - } - - @Override - public String toString() { - return value; - } - } - - public String getValueOf(final String graphId, final GraphConfigEnum graphConfigEnum, final LocationEnum location) { - final String key = getKeyGraphConfig(graphId, graphConfigEnum, location); - return this.get(key); - } - public String getCustomPropsValue() { return this.get(CUSTOM_PROPERTIES_AUTHS, CUSTOM_PROPERTIES_AUTHS_DEFAULT); } - public String getGraphAuthsValue(final String graphId) { - return this.get(getKeyGraphAuths(graphId)); - } - - public String getGraphIdsValue() { - return this.get(GRAPH_IDS, GRAPH_IDS_DEFAULT); - } - public String getIsPublicAccessAllowed() { return get(IS_PUBLIC_ACCESS_ALLOWED, IS_PUBLIC_ACCESS_ALLOWED_DEFAULT); } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationAddElementsHandler.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationAddElementsHandler.java index 2d74426c08a..f503cad00b0 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationAddElementsHandler.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationAddElementsHandler.java @@ -32,7 +32,7 @@ import java.util.Set; import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS; -import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE; +import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.getSkipFailedFederatedStoreExecute; /** *

@@ -74,7 +74,7 @@ public Object doOperation(final AddElements addElements, final Context context, addElementsClone.setInput(retain); graph.execute(addElementsClone, context.getUser()); } catch (final Exception e) { - if (!Boolean.valueOf(addElements.getOption(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE))) { + if (!Boolean.valueOf(getSkipFailedFederatedStoreExecute(addElements))) { throw new OperationException("Graph failed to execute operation. Graph: " + graph.getGraphId() + " Operation: " + addElements.getClass().getSimpleName(), e); } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandler.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandler.java index 6b639725aa7..c0bf225fb0b 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandler.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandler.java @@ -27,7 +27,7 @@ import java.util.Collection; import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS; -import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE; +import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.getSkipFailedFederatedStoreExecute; /** * A handler for Operations with no output for FederatedStore @@ -45,7 +45,7 @@ public Object doOperation(final Operation operation, final Context context, fina try { graph.execute(updatedOp, context); } catch (final Exception e) { - if (!Boolean.valueOf(updatedOp.getOption(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE))) { + if (!Boolean.valueOf(getSkipFailedFederatedStoreExecute(updatedOp))) { throw new OperationException("Failed to execute " + operation.getClass().getSimpleName() + " on graph " + graph.getGraphId(), e); } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandler.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandler.java index a4b87a38a3f..e33273cca41 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandler.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandler.java @@ -30,6 +30,7 @@ import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS; import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE; +import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.getSkipFailedFederatedStoreExecute; /** * A abstract handler for Operations with output for FederatedStore @@ -52,7 +53,7 @@ public O doOperation(final OP operation, final Context context, final Store stor try { execute = graph.execute(updatedOp, context.getUser()); } catch (final Exception e) { - if (!Boolean.valueOf(updatedOp.getOption(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE))) { + if (!Boolean.valueOf(getSkipFailedFederatedStoreExecute(updatedOp))) { final String additionalInfo = String.format("set the skip and continue flag: %s for operation: %s", KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE, operation.getClass().getSimpleName()); diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java index 42e86796d66..f15cd6fd2b5 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java @@ -34,6 +34,9 @@ * @see GraphDelegate */ public class FederatedAddGraphHandler implements OperationHandler { + + public static final String ERROR_BUILDING_GRAPH_GRAPH_ID_S = "Error building graph. graphId: %s"; + @Override public Void doOperation(final AddGraph operation, final Context context, final Store store) throws OperationException { final User user = context.getUser(); @@ -43,9 +46,14 @@ public Void doOperation(final AddGraph operation, final Context context, final S throw new OperationException("User is limited to only using parentPropertiesId from the graphLibrary, but found storeProperties:" + operation.getProperties().toString()); } - final Graph graph = GraphDelegate.createGraph(store, operation.getGraphId(), - operation.getSchema(), operation.getStoreProperties(), - operation.getParentSchemaIds(), operation.getParentPropertiesId()); + final Graph graph; + try { + graph = GraphDelegate.createGraph(store, operation.getGraphId(), + operation.getSchema(), operation.getStoreProperties(), + operation.getParentSchemaIds(), operation.getParentPropertiesId()); + } catch (final Exception e) { + throw new OperationException(String.format(ERROR_BUILDING_GRAPH_GRAPH_ID_S, operation.getGraphId()), e); + } try { ((FederatedStore) store).addGraphs(operation.getGraphAuths(), context.getUser().getUserId(), operation.getIsPublic(), graph); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java index 1eeaeb90e81..6fa05a093ff 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreAuthTest.java @@ -60,7 +60,6 @@ public void shouldAddGraphWithAuth() throws Exception { FederatedStoreProperties federatedStoreProperties = new FederatedStoreProperties(); federatedStoreProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); - federatedStoreProperties.setFalseSkipFailedExecution(); MapStoreProperties storeProperties = new MapStoreProperties(); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java index b4ecd0634d3..de305c15688 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCacheTest.java @@ -63,7 +63,7 @@ public void beforeEach() throws CacheOperationException { @Test public void shouldAddAndGetGraphToCache() throws CacheOperationException { federatedStoreCache.addGraphToCache(testGraph, null, false); - Graph cached = federatedStoreCache.getFromCache(MAP_ID_1); + Graph cached = federatedStoreCache.getGraphFromCache(MAP_ID_1); assertEquals(testGraph.getGraphId(), cached.getGraphId()); assertEquals(testGraph.getSchema().toString(), cached.getSchema().toString()); @@ -111,6 +111,6 @@ public void shouldThrowExceptionIfGraphIdToBeRemovedIsNull() throws CacheOperati @Test public void shouldThrowExceptionIfGraphIdToGetIsNull() throws CacheOperationException { federatedStoreCache.addGraphToCache(testGraph, null, false); - assertNull(federatedStoreCache.getFromCache(null)); + assertNull(federatedStoreCache.getGraphFromCache(null)); } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java index 70d9fbd13fd..3b2721adc1e 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStorePublicAccessTest.java @@ -16,17 +16,20 @@ package uk.gov.gchq.gaffer.federatedstore; -import org.junit.Assert; +import com.google.common.collect.Lists; import org.junit.Before; import org.junit.Test; import uk.gov.gchq.gaffer.cache.CacheServiceLoader; +import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; import uk.gov.gchq.gaffer.federatedstore.operation.GetAllGraphIds; import uk.gov.gchq.gaffer.mapstore.MapStoreProperties; import uk.gov.gchq.gaffer.store.Context; import uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary; import uk.gov.gchq.gaffer.store.schema.Schema; +import static org.junit.Assert.assertEquals; + public class FederatedStorePublicAccessTest { @@ -38,16 +41,14 @@ public class FederatedStorePublicAccessTest { private FederatedStore store; private FederatedStoreProperties fedProps; private HashMapGraphLibrary library; - private Context blankContext; + private Context blankUserContext; + private Context testUserContext; @Before public void setUp() throws Exception { CacheServiceLoader.shutdown(); fedProps = new FederatedStoreProperties(); fedProps.setCacheProperties(CACHE_SERVICE_CLASS_STRING); - fedProps.setGraphIds(GRAPH_1); - fedProps.setGraphPropId(GRAPH_1, PROP_1); - fedProps.setGraphSchemaId(GRAPH_1, SCHEMA_1); store = new FederatedStore(); library = new HashMapGraphLibrary(); @@ -59,68 +60,81 @@ public void setUp() throws Exception { library.addProperties(mapStoreProperties); library.addSchema(new Schema.Builder().id(SCHEMA_1).build()); store.setGraphLibrary(library); - blankContext = new Context(FederatedStoreUser.blankUser()); + blankUserContext = new Context(FederatedStoreUser.blankUser()); + testUserContext = new Context(FederatedStoreUser.testUser()); } @Test public void shouldNotBePublicWhenAllGraphsDefaultedPrivateAndGraphIsDefaultedPrivate() throws Exception { store.initialise(TEST_FED_STORE_ID, null, fedProps); - final Iterable execute = store.execute(new GetAllGraphIds(), blankContext); - Assert.assertFalse(execute.iterator().hasNext()); + store.execute(new AddGraph.Builder() + .graphId(GRAPH_1) + .parentPropertiesId(PROP_1) + .parentSchemaIds(Lists.newArrayList(SCHEMA_1)) + .build(), testUserContext); + getAllGraphsIdsHasNext(false); } - @Test public void shouldBePublicWhenAllGraphsDefaultedPrivateAndGraphIsSetPublic() throws Exception { - fedProps.setTrueGraphIsPublicValue(GRAPH_1); store.initialise(TEST_FED_STORE_ID, null, fedProps); - final Iterable execute = store.execute(new GetAllGraphIds(), blankContext); - Assert.assertTrue(execute.iterator().hasNext()); + store.execute(getAddGraphOp(true), testUserContext); + getAllGraphsIdsHasNext(true); } + @Test public void shouldNotBePublicWhenAllGraphsDefaultedPrivateAndGraphIsSetPrivate() throws Exception { - fedProps.setFalseGraphIsPublicValue(GRAPH_1); store.initialise(TEST_FED_STORE_ID, null, fedProps); - final Iterable execute = store.execute(new GetAllGraphIds(), blankContext); - Assert.assertFalse(execute.iterator().hasNext()); + store.execute(getAddGraphOp(false), testUserContext); + getAllGraphsIdsHasNext(false); } @Test public void shouldNotBePublicWhenAllGraphsSetPrivateAndGraphIsSetPublic() throws Exception { fedProps.setFalseGraphsCanHavePublicAccess(); - fedProps.setTrueGraphIsPublicValue(GRAPH_1); store.initialise(TEST_FED_STORE_ID, null, fedProps); - final Iterable execute = store.execute(new GetAllGraphIds(), blankContext); - Assert.assertFalse(execute.iterator().hasNext()); + store.execute(getAddGraphOp(true), testUserContext); + getAllGraphsIdsHasNext(false); } @Test public void shouldNotBePublicWhenAllGraphsSetPrivateAndGraphIsSetPrivate() throws Exception { fedProps.setFalseGraphsCanHavePublicAccess(); - fedProps.setFalseGraphIsPublicValue(GRAPH_1); store.initialise(TEST_FED_STORE_ID, null, fedProps); - final Iterable execute = store.execute(new GetAllGraphIds(), blankContext); - Assert.assertFalse(execute.iterator().hasNext()); + store.execute(getAddGraphOp(false), testUserContext); + getAllGraphsIdsHasNext(false); } - @Test public void shouldNotBePublicWhenAllGraphsSetPublicAndGraphIsSetPrivate() throws Exception { fedProps.setTrueGraphsCanHavePublicAccess(); - fedProps.setFalseGraphIsPublicValue(GRAPH_1); store.initialise(TEST_FED_STORE_ID, null, fedProps); - final Iterable execute = store.execute(new GetAllGraphIds(), blankContext); - Assert.assertFalse(execute.iterator().hasNext()); + store.execute(getAddGraphOp(false), testUserContext); + getAllGraphsIdsHasNext(false); } @Test public void shouldBePublicWhenAllGraphsSetPublicAndGraphIsSetPublic() throws Exception { fedProps.setTrueGraphsCanHavePublicAccess(); - fedProps.setTrueGraphIsPublicValue(GRAPH_1); store.initialise(TEST_FED_STORE_ID, null, fedProps); - final Iterable execute = store.execute(new GetAllGraphIds(), blankContext); - Assert.assertTrue(execute.iterator().hasNext()); + store.execute(getAddGraphOp(true), testUserContext); + getAllGraphsIdsHasNext(true); + } + + + private AddGraph getAddGraphOp(final boolean isPublic) { + return new AddGraph.Builder() + .isPublic(isPublic) + .graphId(GRAPH_1) + .parentPropertiesId(PROP_1) + .parentSchemaIds(Lists.newArrayList(SCHEMA_1)) + .build(); + } + + private void getAllGraphsIdsHasNext(final boolean expected) throws uk.gov.gchq.gaffer.operation.OperationException { + final Iterable execute = store.execute(new GetAllGraphIds(), blankUserContext); + assertEquals(expected, execute.iterator().hasNext()); } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index 83320a2d57e..ab4b6e950e0 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -18,13 +18,13 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; -import org.junit.Assert; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; -import uk.gov.gchq.gaffer.accumulostore.SingleUseAccumuloStore; import uk.gov.gchq.gaffer.cache.CacheServiceLoader; +import uk.gov.gchq.gaffer.commonutil.CommonConstants; import uk.gov.gchq.gaffer.commonutil.JsonUtil; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; @@ -32,14 +32,13 @@ import uk.gov.gchq.gaffer.data.element.Element; import uk.gov.gchq.gaffer.data.element.Entity; import uk.gov.gchq.gaffer.data.elementdefinition.view.View; -import uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties.GraphConfigEnum; -import uk.gov.gchq.gaffer.federatedstore.integration.FederatedStoreITs; import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; import uk.gov.gchq.gaffer.federatedstore.operation.GetAllGraphIds; +import uk.gov.gchq.gaffer.federatedstore.operation.handler.impl.FederatedAddGraphHandler; import uk.gov.gchq.gaffer.graph.Graph; import uk.gov.gchq.gaffer.graph.GraphConfig; -import uk.gov.gchq.gaffer.mapstore.MapStoreProperties; import uk.gov.gchq.gaffer.operation.Operation; +import uk.gov.gchq.gaffer.operation.OperationException; import uk.gov.gchq.gaffer.operation.impl.add.AddElements; import uk.gov.gchq.gaffer.operation.impl.get.GetAllElements; import uk.gov.gchq.gaffer.store.Context; @@ -53,6 +52,7 @@ import uk.gov.gchq.gaffer.user.User; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; @@ -65,15 +65,19 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static uk.gov.gchq.gaffer.federatedstore.FederatedStore.ERROR_ADDING_GRAPH_TO_CACHE; -import static uk.gov.gchq.gaffer.federatedstore.FederatedStore.S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2; import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreUser.TEST_USER; -import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreUser.authUser; -import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreUser.testUser; +import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreUser.blankUser; import static uk.gov.gchq.gaffer.federatedstore.operation.handler.FederatedOperationOutputHandler.NO_RESULTS_TO_MERGE_ERROR; +import static uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate.SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S; +import static uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate.STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S; public class FederatedStoreTest { - private static final String PATH_FEDERATED_STORE_PROPERTIES = "/properties/federatedStoreTest.properties"; + public static final String ID_SCHEMA_ENTITY = "basicEntitySchema"; + public static final String ID_SCHEMA_EDGE = "basicEdgeSchema"; + public static final String ID_PROPS_ACC = "mockAccProps"; + public static final String ID_PROPS_MAP = "mockMapProps"; + public static final String INVALID = "invalid"; + public static final String ID_PROPS_MAP_ALT = "mockMapPropsAlt"; private static final String FEDERATED_STORE_ID = "testFederatedStoreId"; private static final String ACC_ID_1 = "mockAccGraphId1"; private static final String MAP_ID_1 = "mockMapGraphId1"; @@ -81,60 +85,68 @@ public class FederatedStoreTest { private static final String PATH_MAP_STORE_PROPERTIES = "properties/singleUseMockMapStore.properties"; private static final String PATH_MAP_STORE_PROPERTIES_ALT = "properties/singleUseMockMapStoreAlt.properties"; private static final String PATH_BASIC_ENTITY_SCHEMA_JSON = "schema/basicEntitySchema.json"; - private static final String PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON = "schema/basicEntitySchemaWithSchemaId.json"; private static final String PATH_BASIC_EDGE_SCHEMA_JSON = "schema/basicEdgeSchema.json"; - private static final String PATH_INVALID = "nothing.json"; private static final String EXCEPTION_NOT_THROWN = "exception not thrown"; - private static final String USER_ID = "testUser"; - private static final String PROPS_ID_1 = "PROPS_ID_1"; + private static final String USER_ID = "blankUser"; public static final String UNUSUAL_KEY = "unusualKey"; - public static final String KEY_DOES_NOT_BELONG = UNUSUAL_KEY + " was added to " + PROPS_ID_1 + " it should not be there"; - private static final String SCHEMA_ID_1 = "SCHEMA_ID_1"; - private static final String SCHEMA_ID_2 = "SCHEMA_ID_2"; + public static final String KEY_DOES_NOT_BELONG = UNUSUAL_KEY + " was added to " + ID_PROPS_MAP + " it should not be there"; private static final String ALL_USERS = FederatedStoreUser.ALL_USERS; private static final HashSet GRAPH_AUTHS = Sets.newHashSet(ALL_USERS); private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; private static final String INVALID_CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.invalid"; private static final String CACHE_SERVICE_NAME = "federatedStoreGraphs"; - private static User authUser; - private static User testUser; - private static Context authUserContext; - private static Context testUserContext; - - FederatedStore store; + public static final String PATH_INCOMPLETE_SCHEMA = "/schema/edgeX2NoTypesSchema.json"; + public static final String PATH_INCOMPLETE_SCHEMA_PART_2 = "/schema/edgeTypeSchema.json"; + private FederatedStore store; private FederatedStoreProperties federatedProperties; + private HashMapGraphLibrary library; + private Context userContext; + private User blankUser; @Before public void setUp() throws Exception { - CacheServiceLoader.shutdown(); - store = new FederatedStore(); + clearCache(); federatedProperties = new FederatedStoreProperties(); - federatedProperties.setGraphAuth(ACC_ID_1, ALL_USERS); - federatedProperties.setGraphAuth(MAP_ID_1, ALL_USERS); - federatedProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); + + clearLibrary(); + library = new HashMapGraphLibrary(); + library.addProperties(getPropertiesFromPath(PATH_ACC_STORE_PROPERTIES)); + library.addProperties(getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES)); + library.addProperties(getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES_ALT)); + library.addSchema(getSchemaFromPath(PATH_BASIC_EDGE_SCHEMA_JSON)); + library.addSchema(getSchemaFromPath(PATH_BASIC_ENTITY_SCHEMA_JSON)); + + store = new FederatedStore(); + store.setGraphLibrary(library); + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + + userContext = new Context(blankUser()); + blankUser = blankUser(); + } + + private void clearLibrary() { HashMapGraphLibrary.clear(); - CacheServiceLoader.shutdown(); - authUser = authUser(); - testUser = testUser(); - authUserContext = new Context(authUser); - testUserContext = new Context(testUser); + } + + @After + public void tearDown() throws Exception { + assertEquals("Library has changed: " + ID_PROPS_ACC, library.getProperties(ID_PROPS_ACC), getPropertiesFromPath(PATH_ACC_STORE_PROPERTIES)); + assertEquals("Library has changed: " + ID_PROPS_MAP, library.getProperties(ID_PROPS_MAP), getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES)); + assertEquals("Library has changed: " + ID_PROPS_MAP_ALT, library.getProperties(ID_PROPS_MAP_ALT), getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES_ALT)); + assertEquals("Library has changed: " + ID_SCHEMA_EDGE, new String(library.getSchema(ID_SCHEMA_EDGE).toJson(false), CommonConstants.UTF_8), new String(getSchemaFromPath(PATH_BASIC_EDGE_SCHEMA_JSON).toJson(false), CommonConstants.UTF_8)); + assertTrue("Library has changed: " + ID_SCHEMA_ENTITY, JsonUtil.equals(library.getSchema(ID_SCHEMA_ENTITY).toJson(false), getSchemaFromPath(PATH_BASIC_ENTITY_SCHEMA_JSON).toJson(false))); } @Test public void shouldLoadGraphsWithIds() throws Exception { - // Given - federatedProperties.setGraphIds(ACC_ID_1 + "," + MAP_ID_1); - federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON); - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); - // When - int before = store.getGraphs(testUser, null).size(); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + int before = store.getGraphs(blankUser, null).size(); + + addGraphWithIds(MAP_ID_1, ID_PROPS_MAP, ID_SCHEMA_EDGE); + addGraphWithIds(ACC_ID_1, ID_PROPS_ACC, ID_SCHEMA_ENTITY); // Then - Collection graphs = store.getGraphs(testUser, null); + Collection graphs = store.getGraphs(blankUser, null); int after = graphs.size(); assertEquals(0, before); assertEquals(2, after); @@ -145,69 +157,77 @@ public void shouldLoadGraphsWithIds() throws Exception { } @Test - public void shouldThrowErrorForFailedSchema() throws Exception { - // Given - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_INVALID); - + public void shouldThrowErrorForFailedSchemaID() throws Exception { // When / Then try { - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + addGraphWithIds(MAP_ID_1, ID_PROPS_MAP, INVALID); fail(EXCEPTION_NOT_THROWN); - } catch (final IllegalArgumentException e) { - assertEquals(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Schema", "GraphId: " + MAP_ID_1 + " schemaPath: " + PATH_INVALID), e.getMessage()); + } catch (final Exception e) { + assertContains(e.getCause(), SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, Arrays.toString(new String[]{INVALID})); } } @Test - public void shouldThrowErrorForFailedProperty() throws Exception { - //Given - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_INVALID); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); - + public void shouldThrowErrorForFailedPropertyID() throws Exception { //When / Then try { - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + addGraphWithIds(MAP_ID_1, INVALID, ID_SCHEMA_EDGE); fail(EXCEPTION_NOT_THROWN); - } catch (final IllegalArgumentException e) { - assertEquals(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Property", "GraphId: " + MAP_ID_1 + " propertyPath: " + PATH_INVALID), e.getMessage()); + } catch (final Exception e) { + assertContains(e.getCause(), STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, INVALID); } } @Test - public void shouldThrowErrorForIncompleteBuilder() throws Exception { - //Given - federatedProperties.setGraphIds(MAP_ID_1); + public void shouldThrowErrorForMissingProperty() throws Exception { + //When / Then + try { + ArrayList schemas = Lists.newArrayList(ID_SCHEMA_EDGE); + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1) + .isPublic(true) + .parentSchemaIds(schemas) + .build(), userContext); + fail("a graph was created without a defined properties"); + } catch (final IllegalArgumentException e) { + assertContains(e, STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, INVALID); + } + } + @Test + public void shouldThrowErrorForMissingSchema() throws Exception { //When / Then try { - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - fail(EXCEPTION_NOT_THROWN); + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1) + .isPublic(true) + .parentPropertiesId(ID_PROPS_MAP) + .build(), userContext); + fail("a graph was created without a defined schema"); } catch (final IllegalArgumentException e) { - assertTrue(e.getMessage().contains(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Graph", ""))); + assertContains(e, STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, INVALID); } } @Test - public void shouldNotAllowOverwritingOfGraphWithFederatedScope() throws Exception { + public void shouldNotAllowOverwritingOfGraphWithinFederatedScope() throws Exception { //Given - federatedProperties.setGraphIds(ACC_ID_1); - federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + addGraphWithIds(MAP_ID_1, ID_PROPS_MAP, ID_SCHEMA_ENTITY); // When / Then try { - store.addGraphs(null, null, false, new Graph.Builder() - .config(new GraphConfig(ACC_ID_1)) - .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES)) - .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) - .build()); + addGraphWithIds(MAP_ID_1, ID_PROPS_MAP, ID_SCHEMA_EDGE); + fail(EXCEPTION_NOT_THROWN); + } catch (final Exception e) { + assertContains(e.getCause(), "GraphId " + MAP_ID_1 + " already exists"); + } + + // When / Then + try { + addGraphWithIds(MAP_ID_1, ID_PROPS_MAP_ALT, ID_SCHEMA_ENTITY); fail(EXCEPTION_NOT_THROWN); } catch (final Exception e) { - assertEquals(String.format(ERROR_ADDING_GRAPH_TO_CACHE, GraphConfigEnum.SCHEMA, ACC_ID_1), e.getMessage()); + assertContains(e.getCause(), "GraphId " + MAP_ID_1 + " already exists"); } } @@ -219,19 +239,12 @@ public void shouldDoUnhandledOperation() throws Exception { @Test public void shouldAlwaysReturnSupportedTraits() throws Exception { // Given - federatedProperties.setGraphIds(ACC_ID_1); - federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + addGraphWithIds(ACC_ID_1, ID_PROPS_ACC, ID_SCHEMA_ENTITY); Set before = store.getTraits(); // When - store.addGraphs(null, null, false, new Graph.Builder() - .config(new GraphConfig(MAP_ID_1)) - .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) - .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build()); + addGraphWithPaths(MAP_ID_1, PATH_MAP_STORE_PROPERTIES, PATH_BASIC_ENTITY_SCHEMA_JSON); Set after = store.getTraits(); assertEquals(StoreTrait.values().length, before.size()); @@ -242,20 +255,10 @@ public void shouldAlwaysReturnSupportedTraits() throws Exception { @Test public void shouldUpdateSchemaWhenNewGraphIsAdded() throws Exception { // Given - federatedProperties.setGraphIds(ACC_ID_1); - federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - Schema before = store.getSchema((Operation) null, authUserContext); - - store.addGraphs(null, authUserContext.getUser().getUserId(), false, new Graph.Builder() - .config(new GraphConfig(MAP_ID_1)) - .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) - .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) - .build()); - - Schema after = store.getSchema((Operation) null, authUserContext); - + addGraphWithPaths(ACC_ID_1, PATH_ACC_STORE_PROPERTIES, PATH_BASIC_ENTITY_SCHEMA_JSON); + Schema before = store.getSchema((Operation) null, blankUser); + addGraphWithPaths(MAP_ID_1, PATH_MAP_STORE_PROPERTIES, PATH_BASIC_EDGE_SCHEMA_JSON); + Schema after = store.getSchema((Operation) null, blankUser); // Then assertNotEquals(before, after); } @@ -263,49 +266,39 @@ public void shouldUpdateSchemaWhenNewGraphIsAdded() throws Exception { @Test public void shouldUpdateSchemaWhenNewGraphIsRemoved() throws Exception { // Given - federatedProperties.setGraphIds(MAP_ID_1 + "," + ACC_ID_1); - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); - federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + addGraphWithPaths(ACC_ID_1, PATH_ACC_STORE_PROPERTIES, PATH_BASIC_ENTITY_SCHEMA_JSON); + Schema was = store.getSchema((Operation) null, blankUser); + addGraphWithPaths(MAP_ID_1, PATH_MAP_STORE_PROPERTIES, PATH_BASIC_EDGE_SCHEMA_JSON); - Schema before = store.getSchema((Operation) null, authUserContext); + Schema before = store.getSchema((Operation) null, blankUser); // When - store.remove(MAP_ID_1, testUser); + store.remove(MAP_ID_1, blankUser); - Schema after = store.getSchema((Operation) null, authUserContext); - assertNotEquals(before, after); + Schema after = store.getSchema((Operation) null, blankUser); + assertNotEquals(before.toString(), after.toString()); + assertEquals(was.toString(), after.toString()); } @Test public void shouldFailWithIncompleteSchema() throws Exception { - //Given - federatedProperties.setGraphIds(ACC_ID_1); - federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(ACC_ID_1, "/schema/edgeX2NoTypesSchema.json"); - // When / Then try { - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + addGraphWithPaths(ACC_ID_1, PATH_ACC_STORE_PROPERTIES, PATH_INCOMPLETE_SCHEMA); fail(EXCEPTION_NOT_THROWN); } catch (final Exception e) { - assertTrue(e.getMessage().contains(String.format(S1_WAS_NOT_ABLE_TO_BE_CREATED_WITH_THE_SUPPLIED_PROPERTIES_GRAPH_ID_S2, "Graph", ""))); + assertContains(e, FederatedAddGraphHandler.ERROR_BUILDING_GRAPH_GRAPH_ID_S, ACC_ID_1); } } @Test public void shouldTakeCompleteSchemaFromTwoFiles() throws Exception { // Given - federatedProperties.setGraphIds(ACC_ID_1); - federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(ACC_ID_1, "/schema/edgeX2NoTypesSchema.json" + ", /schema/edgeTypeSchema.json"); - int before = store.getGraphs(testUser, null).size(); + int before = store.getGraphs(blankUser, null).size(); + addGraphWithPaths(ACC_ID_1, PATH_ACC_STORE_PROPERTIES, PATH_INCOMPLETE_SCHEMA, PATH_INCOMPLETE_SCHEMA_PART_2); // When - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - int after = store.getGraphs(testUser, null).size(); + int after = store.getGraphs(blankUser, null).size(); // Then assertEquals(0, before); @@ -315,13 +308,13 @@ public void shouldTakeCompleteSchemaFromTwoFiles() throws Exception { @Test public void shouldAddTwoGraphs() throws Exception { // Given - final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(FederatedStoreITs.class, PATH_FEDERATED_STORE_PROPERTIES)); - federatedProperties.setProperties(storeProperties.getProperties()); - int sizeBefore = store.getGraphs(authUser, null).size(); + int sizeBefore = store.getGraphs(blankUser, null).size(); // When - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - int sizeAfter = store.getGraphs(authUser, null).size(); + addGraphWithPaths(MAP_ID_1, PATH_MAP_STORE_PROPERTIES, PATH_BASIC_ENTITY_SCHEMA_JSON); + addGraphWithPaths(ACC_ID_1, PATH_ACC_STORE_PROPERTIES, PATH_BASIC_EDGE_SCHEMA_JSON); + + int sizeAfter = store.getGraphs(blankUser, null).size(); // Then assertEquals(0, sizeBefore); @@ -330,11 +323,8 @@ public void shouldAddTwoGraphs() throws Exception { @Test public void shouldContainNoElements() throws Exception { - // Given - final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(FederatedStoreITs.class, PATH_FEDERATED_STORE_PROPERTIES)); - federatedProperties.setProperties(storeProperties.getProperties()); // When - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + addGraphWithPaths(MAP_ID_1, PATH_MAP_STORE_PROPERTIES, PATH_BASIC_ENTITY_SCHEMA_JSON); Set after = getElements(); // Then @@ -344,9 +334,8 @@ public void shouldContainNoElements() throws Exception { @Test public void shouldAddEdgesToOneGraph() throws Exception { // Given - final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(FederatedStoreITs.class, PATH_FEDERATED_STORE_PROPERTIES)); - federatedProperties.setProperties(storeProperties.getProperties()); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + addGraphWithPaths(MAP_ID_1, PATH_MAP_STORE_PROPERTIES, PATH_BASIC_EDGE_SCHEMA_JSON); + AddElements op = new AddElements.Builder() .input(new Edge.Builder() .group("BasicEdge") @@ -357,7 +346,7 @@ public void shouldAddEdgesToOneGraph() throws Exception { .build(); // When - store.execute(op, new Context(authUser)); + store.execute(op, userContext); // Then assertEquals(1, getElements().size()); @@ -366,15 +355,11 @@ public void shouldAddEdgesToOneGraph() throws Exception { @Test public void shouldReturnGraphIds() throws Exception { // Given - federatedProperties.setGraphIds(MAP_ID_1 + "," + ACC_ID_1); - federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON); - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); + addGraphWithPaths(ACC_ID_1, PATH_ACC_STORE_PROPERTIES, PATH_BASIC_ENTITY_SCHEMA_JSON); + addGraphWithPaths(MAP_ID_1, PATH_MAP_STORE_PROPERTIES, PATH_BASIC_EDGE_SCHEMA_JSON); // When - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - Collection allGraphIds = store.getAllGraphIds(testUser); + Collection allGraphIds = store.getAllGraphIds(blankUser); // Then assertEquals(2, allGraphIds.size()); @@ -386,13 +371,11 @@ public void shouldReturnGraphIds() throws Exception { @Test public void shouldUpdateGraphIds() throws Exception { // Given - federatedProperties.setGraphIds(ACC_ID_1); - federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_JSON); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + addGraphWithPaths(ACC_ID_1, PATH_ACC_STORE_PROPERTIES, PATH_BASIC_ENTITY_SCHEMA_JSON); + // When - Collection allGraphId = store.getAllGraphIds(testUser); + Collection allGraphId = store.getAllGraphIds(blankUser); // Then assertEquals(1, allGraphId.size()); @@ -400,12 +383,8 @@ public void shouldUpdateGraphIds() throws Exception { assertFalse(allGraphId.contains(MAP_ID_1)); // When - store.addGraphs(GRAPH_AUTHS, null, false, new Graph.Builder() - .config(new GraphConfig(MAP_ID_1)) - .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) - .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build()); - Collection allGraphId2 = store.getAllGraphIds(testUser); + addGraphWithIds(MAP_ID_1, ID_PROPS_MAP, ID_SCHEMA_ENTITY); + Collection allGraphId2 = store.getAllGraphIds(blankUser); // Then assertEquals(2, allGraphId2.size()); @@ -413,8 +392,8 @@ public void shouldUpdateGraphIds() throws Exception { assertTrue(allGraphId2.contains(MAP_ID_1)); // When - store.remove(ACC_ID_1, testUser); - Collection allGraphId3 = store.getAllGraphIds(testUser); + store.remove(ACC_ID_1, blankUser); + Collection allGraphId3 = store.getAllGraphIds(blankUser); // Then assertEquals(1, allGraphId3.size()); @@ -426,11 +405,19 @@ public void shouldUpdateGraphIds() throws Exception { @Test public void shouldGetAllGraphIdsInUnmodifiableSet() throws Exception { // Given - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + addGraphWithPaths(MAP_ID_1, PATH_MAP_STORE_PROPERTIES, PATH_BASIC_ENTITY_SCHEMA_JSON); // When / Then + Collection allGraphIds = store.getAllGraphIds(blankUser); try { - store.getAllGraphIds(testUser).add("newId"); + allGraphIds.add("newId"); + fail(EXCEPTION_NOT_THROWN); + } catch (UnsupportedOperationException e) { + assertNotNull(e); + } + + try { + allGraphIds.remove("newId"); fail(EXCEPTION_NOT_THROWN); } catch (UnsupportedOperationException e) { assertNotNull(e); @@ -440,304 +427,205 @@ public void shouldGetAllGraphIdsInUnmodifiableSet() throws Exception { @Test public void shouldNotUseSchema() throws Exception { // Given - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); final Schema unusedMock = Mockito.mock(Schema.class); - - // Then - Mockito.verifyNoMoreInteractions(unusedMock); - // When store.initialise(FEDERATED_STORE_ID, unusedMock, federatedProperties); + addGraphWithPaths(MAP_ID_1, PATH_MAP_STORE_PROPERTIES, PATH_BASIC_EDGE_SCHEMA_JSON); + // Then + Mockito.verifyNoMoreInteractions(unusedMock); } @Test public void shouldAddGraphFromLibrary() throws Exception { // Given - final Schema schema = new Schema.Builder() - .id(SCHEMA_ID_1) - .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build(); - final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); - graphLibrary.add(MAP_ID_1, schema, StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES))); - store.setGraphLibrary(graphLibrary); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + library.add(MAP_ID_1, library.getSchema(ID_SCHEMA_ENTITY), library.getProperties(ID_PROPS_MAP)); // When - final int before = store.getGraphs(testUser, null).size(); + final int before = store.getGraphs(blankUser, null).size(); store.execute(new AddGraph.Builder() .graphId(MAP_ID_1) - .build(), new Context(testUser)); + .build(), new Context(blankUser)); - final int after = store.getGraphs(testUser, null).size(); + final int after = store.getGraphs(blankUser, null).size(); // Then assertEquals(0, before); assertEquals(1, after); } - @Test - public void shouldAddNamedGraphFromGraphIDKeyButDefinedInLibrary() throws Exception { - // Given - federatedProperties.setGraphIds(MAP_ID_1); - final Schema schema = new Schema.Builder() - .id(SCHEMA_ID_1) - .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build(); - final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)); - GraphLibrary library = new HashMapGraphLibrary(); - library.add(MAP_ID_1, schema, storeProperties); - store.setGraphLibrary(library); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - - // Then - final int after = store.getGraphs(testUser, null).size(); - assertEquals(1, after); - } - - @Test - public void shouldAddGraphFromGraphIDKeyButDefinedProperties() throws Exception { - // Given - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_ACC_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON); - final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); - store.setGraphLibrary(graphLibrary); - - // When - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - - // Then - final int after = store.getGraphs(testUser, null).size(); - assertEquals(1, after); - } - - @Test - public void shouldAddNamedGraphsFromGraphIDKeyButDefinedInLibraryAndProperties() throws Exception { - // Given - federatedProperties.setGraphIds(MAP_ID_1 + ", " + ACC_ID_1); - federatedProperties.setGraphPropFile(ACC_ID_1, PATH_ACC_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(ACC_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON); - final Schema schema = new Schema.Builder() - .id(SCHEMA_ID_1) - .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build(); - final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); - graphLibrary.add(MAP_ID_1, schema, StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES))); - - // When - store.setGraphLibrary(graphLibrary); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); - - // Then - final int after = store.getGraphs(testUser, null).size(); - assertEquals(2, after); - } - @Test public void shouldAddGraphWithPropertiesFromGraphLibrary() throws Exception { - // Given - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON); - final HashMapGraphLibrary library = new HashMapGraphLibrary(); - library.addProperties(StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES)); - // When - store.setGraphLibrary(library); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1) + .parentPropertiesId(ID_PROPS_MAP) + .isPublic(true) + .schema(getSchemaFromPath(PATH_BASIC_ENTITY_SCHEMA_JSON)) + .build(), userContext); + // Then - assertEquals(1, store.getGraphs(testUser, null).size()); - assertTrue(library.getProperties(PROPS_ID_1).equals(StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES))); + assertEquals(1, store.getGraphs(blankUser, null).size()); + assertTrue(library.getProperties(ID_PROPS_MAP).equals(getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES))); } @Test public void shouldAddGraphWithSchemaFromGraphLibrary() throws Exception { - // Given - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); - federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1); - final HashMapGraphLibrary library = new HashMapGraphLibrary(); - final Schema schema = new Schema.Builder() - .id(SCHEMA_ID_1) - .json(StreamUtil.openStream(FederatedStore.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build(); - library.addSchema(schema); - // When - store.setGraphLibrary(library); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1) + .storeProperties(getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES)) + .isPublic(true) + .parentSchemaIds(Lists.newArrayList(ID_SCHEMA_ENTITY)) + .build(), userContext); + // Then - assertEquals(1, store.getGraphs(testUser, null).size()); - assertTrue(library.getSchema(SCHEMA_ID_1).toString().equals(schema.toString())); + assertEquals(1, store.getGraphs(blankUser, null).size()); + assertTrue(library.getSchema(ID_SCHEMA_ENTITY).toString().equals(getSchemaFromPath(PATH_BASIC_ENTITY_SCHEMA_JSON).toString())); } @Test public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibrary() throws Exception { - // Given - federatedProperties.setGraphIds(MAP_ID_1); - final Schema schema = new Schema.Builder() - .id(SCHEMA_ID_1) - .json(StreamUtil.openStream(FederatedStore.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build(); - final StoreProperties properties = StoreProperties.loadStoreProperties(PATH_MAP_STORE_PROPERTIES); - properties.setId(PROPS_ID_1); - final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); - graphLibrary.add(MAP_ID_1, schema, properties); - // When - store.setGraphLibrary(graphLibrary); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + addGraphWithIds(MAP_ID_1, ID_PROPS_MAP, ID_SCHEMA_ENTITY); // Then - assertEquals(1, store.getGraphs(testUser, null).size()); - graphLibrary.getSchema(SCHEMA_ID_1).toJson(false); - schema.toJson(false); - assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_1).toJson(false), schema.toJson(false))); - assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(properties)); + assertEquals(1, store.getGraphs(blankUser, null).size()); + Graph graph = store.getGraphs(blankUser, MAP_ID_1).iterator().next(); + assertEquals(getSchemaFromPath(PATH_BASIC_ENTITY_SCHEMA_JSON).toString(), graph.getSchema().toString()); + assertEquals(getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES), graph.getStoreProperties()); + } @Test public void shouldAddGraphWithPropertiesFromGraphLibraryOverridden() throws Exception { // Given - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1); - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES_ALT); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON); - final MapStoreProperties prop = new MapStoreProperties(); - prop.setId(PROPS_ID_1); - final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); - graphLibrary.addProperties(prop); - assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY)); + assertFalse(KEY_DOES_NOT_BELONG, library.getProperties(ID_PROPS_MAP).containsKey(UNUSUAL_KEY)); // When - store.setGraphLibrary(graphLibrary); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + Builder schema = new Builder(); + for (String path : new String[]{PATH_BASIC_ENTITY_SCHEMA_JSON}) { + schema.merge(getSchemaFromPath(path)); + } + + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1) + .storeProperties(getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES_ALT)) + .parentPropertiesId(ID_PROPS_MAP) + .isPublic(true) + .schema(schema.build()) + .build(), userContext); // Then - assertEquals(1, store.getGraphs(testUser, null).size()); - assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().containsKey(UNUSUAL_KEY)); - assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY)); - assertEquals(prop.getProperties(), graphLibrary.getProperties(PROPS_ID_1).getProperties()); - assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty(UNUSUAL_KEY) != null); + assertEquals(1, store.getGraphs(blankUser, null).size()); + assertTrue(store.getGraphs(blankUser, null).iterator().next().getStoreProperties().containsKey(UNUSUAL_KEY)); + assertFalse(KEY_DOES_NOT_BELONG, library.getProperties(ID_PROPS_MAP).containsKey(UNUSUAL_KEY)); + assertTrue(store.getGraphs(blankUser, null).iterator().next().getStoreProperties().getProperties().getProperty(UNUSUAL_KEY) != null); } @Test public void shouldAddGraphWithSchemaFromGraphLibraryOverridden() throws Exception { - // Given - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON); - federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_2); - final Schema schema = new Schema.Builder() - .id(SCHEMA_ID_2) - .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_EDGE_SCHEMA_JSON)) - .build(); - final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); - graphLibrary.addSchema(schema); - assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_2).toJson(false), schema.toJson(false))); - - // When - store.setGraphLibrary(graphLibrary); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + ArrayList schemas = Lists.newArrayList(ID_SCHEMA_ENTITY); + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1) + .isPublic(true) + .schema(getSchemaFromPath(PATH_BASIC_EDGE_SCHEMA_JSON)) + .parentSchemaIds(schemas) + .parentPropertiesId(ID_PROPS_MAP) + .build(), userContext); // Then - assertEquals(1, store.getGraphs(testUser, null).size()); - assertTrue(store.getGraphs(testUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity")); - assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_2).toJson(false), schema.toJson(false))); + assertEquals(1, store.getGraphs(blankUser, null).size()); + assertTrue(store.getGraphs(blankUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity")); + } @Test public void shouldAddGraphWithPropertiesAndSchemaFromGraphLibraryOverridden() throws Exception { // Given - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1); - federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_2); - - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES_ALT); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_ENTITY_SCHEMA_WITH_SCHEMA_ID_JSON); - - final MapStoreProperties prop = new MapStoreProperties(); - prop.setId(PROPS_ID_1); - - final Schema schema = new Schema.Builder() - .id(SCHEMA_ID_2) - .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_EDGE_SCHEMA_JSON)) - .build(); - - final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); - graphLibrary.addSchema(schema); - graphLibrary.addProperties(prop); - - assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY)); + assertFalse(KEY_DOES_NOT_BELONG, library.getProperties(ID_PROPS_MAP).containsKey(UNUSUAL_KEY)); // When - store.setGraphLibrary(graphLibrary); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + Builder tempSchema = new Builder(); + for (String path : new String[]{PATH_BASIC_EDGE_SCHEMA_JSON}) { + tempSchema.merge(getSchemaFromPath(path)); + } + + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1) + .isPublic(true) + .storeProperties(getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES_ALT)) + .parentPropertiesId(ID_PROPS_MAP) + .schema(tempSchema.build()) + .parentSchemaIds(Lists.newArrayList(ID_SCHEMA_ENTITY)) + .build(), userContext); // Then - assertEquals(1, store.getGraphs(testUser, null).size()); - assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().containsKey(UNUSUAL_KEY)); - assertFalse(KEY_DOES_NOT_BELONG, graphLibrary.getProperties(PROPS_ID_1).containsKey(UNUSUAL_KEY)); - assertTrue(store.getGraphs(testUser, null).iterator().next().getStoreProperties().getProperties().getProperty(UNUSUAL_KEY) != null); - assertTrue(store.getGraphs(testUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity")); - assertTrue(graphLibrary.getProperties(PROPS_ID_1).equals(prop)); - assertTrue(JsonUtil.equals(graphLibrary.getSchema(SCHEMA_ID_2).toJson(false), schema.toJson(false))); + assertEquals(1, store.getGraphs(blankUser, null).size()); + assertTrue(store.getGraphs(blankUser, null).iterator().next().getStoreProperties().containsKey(UNUSUAL_KEY)); + assertFalse(KEY_DOES_NOT_BELONG, library.getProperties(ID_PROPS_MAP).containsKey(UNUSUAL_KEY)); + assertTrue(store.getGraphs(blankUser, null).iterator().next().getStoreProperties().getProperties().getProperty(UNUSUAL_KEY) != null); + assertTrue(store.getGraphs(blankUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity")); } @Test public void shouldNotAllowOverridingOfKnownGraphInLibrary() throws Exception { // Given - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES_ALT); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); - final Schema schema = new Builder() - .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_EDGE_SCHEMA_JSON)) - .build(); - final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); - final StoreProperties storeProperties = StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES)); - graphLibrary.add(MAP_ID_1, schema, storeProperties); + library.add(MAP_ID_1, getSchemaFromPath(PATH_BASIC_ENTITY_SCHEMA_JSON), getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES)); // When / Then try { - store.setGraphLibrary(graphLibrary); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1) + .parentPropertiesId(ID_PROPS_MAP_ALT) + .isPublic(true) + .build(), userContext); fail(EXCEPTION_NOT_THROWN); - } catch (final IllegalArgumentException e) { - assertTrue(e.getCause().getMessage().contains("GraphId " + MAP_ID_1 + " already exists with a different store properties:")); + } catch (final Exception e) { + assertContains(e.getCause(), "GraphId " + MAP_ID_1 + " already exists so you cannot use different store properties"); + } + + // When / Then + try { + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1) + .parentSchemaIds(Lists.newArrayList(ID_SCHEMA_EDGE)) + .isPublic(true) + .build(), userContext); + fail(EXCEPTION_NOT_THROWN); + } catch (final Exception e) { + assertContains(e.getCause(), "GraphId " + MAP_ID_1 + " already exists so you cannot use a different schema"); } } @Test public void shouldFederatedIfUserHasCorrectAuths() throws Exception { // Given - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + store.addGraphs(GRAPH_AUTHS, null, false, new Graph.Builder() .config(new GraphConfig.Builder() .graphId(MAP_ID_1) .build()) - .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) - .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) + .storeProperties(getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES)) + .addSchema(getSchemaFromPath(PATH_BASIC_ENTITY_SCHEMA_JSON)) .build()); // When final CloseableIterable elements = store.execute(new GetAllElements(), new Context(new User.Builder() - .userId(USER_ID) + .userId(blankUser.getUserId()) .opAuth(ALL_USERS) .build())); // Then - Assert.assertFalse(elements.iterator().hasNext()); + assertFalse(elements.iterator().hasNext()); try { store.execute(new GetAllElements(), new Context(new User.Builder() - .userId(USER_ID) + .userId(blankUser.getUserId()) .opAuths("x") .build())); fail("expected exception"); @@ -749,13 +637,13 @@ public void shouldFederatedIfUserHasCorrectAuths() throws Exception { @Test public void shouldReturnSpecificGraphsFromCSVString() throws StoreException { // Given - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + final List> graphLists = populateGraphs(1, 2, 4); final Collection expectedGraphs = graphLists.get(0); final Collection unexpectedGraphs = graphLists.get(1); // When - final Collection returnedGraphs = store.getGraphs(testUser, "mockGraphId1,mockGraphId2,mockGraphId4"); + final Collection returnedGraphs = store.getGraphs(blankUser, "mockGraphId1,mockGraphId2,mockGraphId4"); // Then assertTrue(returnedGraphs.size() == 3); @@ -766,12 +654,12 @@ public void shouldReturnSpecificGraphsFromCSVString() throws StoreException { @Test public void shouldReturnNoGraphsFromEmptyString() throws StoreException { // Given - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + final List> graphLists = populateGraphs(); final Collection expectedGraphs = graphLists.get(0); // When - final Collection returnedGraphs = store.getGraphs(testUser, ""); + final Collection returnedGraphs = store.getGraphs(blankUser, ""); // Then assertTrue(returnedGraphs.isEmpty()); @@ -781,13 +669,12 @@ public void shouldReturnNoGraphsFromEmptyString() throws StoreException { @Test public void shouldReturnGraphsWithLeadingCommaString() throws StoreException { // Given - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); final List> graphLists = populateGraphs(2, 4); final Collection expectedGraphs = graphLists.get(0); final Collection unexpectedGraphs = graphLists.get(1); // When - final Collection returnedGraphs = store.getGraphs(testUser, ",mockGraphId2,mockGraphId4"); + final Collection returnedGraphs = store.getGraphs(blankUser, ",mockGraphId2,mockGraphId4"); // Then assertTrue(returnedGraphs.size() == 2); @@ -798,28 +685,21 @@ public void shouldReturnGraphsWithLeadingCommaString() throws StoreException { @Test public void shouldAddGraphIdWithAuths() throws Exception { // Given - federatedProperties.setFalseSkipFailedExecution(); - final HashMapGraphLibrary graphLibrary = new HashMapGraphLibrary(); - graphLibrary.add(MAP_ID_1, - new Schema.Builder() - .id(SCHEMA_ID_1) - .json(StreamUtil.openStream(this.getClass(), PATH_BASIC_ENTITY_SCHEMA_JSON)) - .build(), - StoreProperties.loadStoreProperties(StreamUtil.openStream(this.getClass(), PATH_MAP_STORE_PROPERTIES))); - final Graph fedGraph = new Graph.Builder() .config(new GraphConfig.Builder() .graphId(FEDERATED_STORE_ID) - .library(graphLibrary) + .library(library) .build()) .addStoreProperties(federatedProperties) .build(); + addGraphWithIds(MAP_ID_1, ID_PROPS_MAP, ID_SCHEMA_ENTITY); + // When int before = 0; for (String ignore : fedGraph.execute( new GetAllGraphIds(), - authUser)) { + blankUser)) { before++; } @@ -828,13 +708,13 @@ public void shouldAddGraphIdWithAuths() throws Exception { .graphAuths("auth") .graphId(MAP_ID_1) .build(), - authUser); + blankUser); int after = 0; for (String ignore : fedGraph.execute( new GetAllGraphIds(), - authUser)) { + blankUser)) { after++; } @@ -842,10 +722,10 @@ public void shouldAddGraphIdWithAuths() throws Exception { fedGraph.execute(new AddElements.Builder() .input(new Entity.Builder() .group("BasicEntity") - .vertex("hi") + .vertex("v1") .build()) .build(), - authUser); + blankUser); final CloseableIterable elements = fedGraph.execute( new GetAllElements(), @@ -869,62 +749,71 @@ public void shouldAddGraphIdWithAuths() throws Exception { // Then assertEquals(0, before); assertEquals(1, after); - Assert.assertNotNull(elements); - Assert.assertTrue(elements.iterator().hasNext()); + assertNotNull(elements); + assertTrue(elements.iterator().hasNext()); } @Test public void shouldThrowWithPropertiesErrorFromGraphLibrary() throws Exception { - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropId(MAP_ID_1, PROPS_ID_1); - federatedProperties.setGraphSchemaFile(MAP_ID_1, PATH_BASIC_EDGE_SCHEMA_JSON); + Builder schema = new Builder(); + for (String path : new String[]{PATH_BASIC_EDGE_SCHEMA_JSON}) { + schema.merge(getSchemaFromPath(path)); + } final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); - final String error = "Something went wrong"; - Mockito.when(mockLibrary.getProperties(PROPS_ID_1)).thenThrow(new IllegalArgumentException(error)); - + final String error = "test Something went wrong"; + Mockito.when(mockLibrary.getProperties(ID_PROPS_MAP)).thenThrow(new IllegalArgumentException(error)); store.setGraphLibrary(mockLibrary); - + clearCache(); + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); try { - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1) + .parentPropertiesId(ID_PROPS_MAP) + .isPublic(true) + .schema(schema.build()) + .build(), userContext); + fail("exception not thrown"); - } catch (IllegalArgumentException e) { + } catch (Exception e) { assertEquals(error, e.getCause().getMessage()); } - Mockito.verify(mockLibrary).getProperties(PROPS_ID_1); + Mockito.verify(mockLibrary).getProperties(ID_PROPS_MAP); } @Test public void shouldThrowWithSchemaErrorFromGraphLibrary() throws Exception { // Given - federatedProperties.setGraphIds(MAP_ID_1); - federatedProperties.setGraphPropFile(MAP_ID_1, PATH_MAP_STORE_PROPERTIES); - federatedProperties.setGraphSchemaId(MAP_ID_1, SCHEMA_ID_1); final GraphLibrary mockLibrary = Mockito.mock(GraphLibrary.class); - final String error = "Something went wrong"; - Mockito.when(mockLibrary.getSchema(SCHEMA_ID_1)).thenThrow(new IllegalArgumentException(error)); + final String error = "test Something went wrong"; + Mockito.when(mockLibrary.getSchema(ID_SCHEMA_ENTITY)).thenThrow(new IllegalArgumentException(error)); store.setGraphLibrary(mockLibrary); - + clearCache(); + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); // When / Then try { - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + store.execute(new AddGraph.Builder() + .graphId(MAP_ID_1) + .storeProperties(getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES)) + .isPublic(true) + .parentSchemaIds(Lists.newArrayList(ID_SCHEMA_ENTITY)) + .build(), userContext); fail(EXCEPTION_NOT_THROWN); - } catch (IllegalArgumentException e) { + } catch (Exception e) { assertEquals(error, e.getCause().getMessage()); } - Mockito.verify(mockLibrary).getSchema(SCHEMA_ID_1); + Mockito.verify(mockLibrary).getSchema(ID_SCHEMA_ENTITY); } @Test public void shouldReturnASingleGraph() throws StoreException { // Given - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); final List> graphLists = populateGraphs(1); final Collection expectedGraphs = graphLists.get(0); final Collection unexpectedGraphs = graphLists.get(1); // When - final Collection returnedGraphs = store.getGraphs(testUser, "mockGraphId1"); + final Collection returnedGraphs = store.getGraphs(blankUser, "mockGraphId1"); // Then assertTrue(returnedGraphs.size() == 1); @@ -936,6 +825,7 @@ public void shouldReturnASingleGraph() throws StoreException { public void shouldThrowExceptionWithInvalidCacheClass() throws StoreException { federatedProperties.setCacheProperties(INVALID_CACHE_SERVICE_CLASS_STRING); try { + clearCache(); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); fail(EXCEPTION_NOT_THROWN); } catch (final IllegalArgumentException e) { @@ -946,6 +836,7 @@ public void shouldThrowExceptionWithInvalidCacheClass() throws StoreException { @Test public void shouldReuseGraphsAlreadyInCache() throws Exception { //Check cache is empty + federatedProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); assertNull(CacheServiceLoader.getService()); //initialise FedStore @@ -958,31 +849,36 @@ public void shouldReuseGraphsAlreadyInCache() throws Exception { .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) .build(); - store.addGraphs(null, TEST_USER, false, graphToAdd); + store.addGraphs(null, TEST_USER, true, graphToAdd); //check the store and the cache - assertEquals(1, store.getAllGraphIds(testUser).size()); + assertEquals(1, store.getAllGraphIds(blankUser).size()); assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(MAP_ID_1)); assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(MAP_ID_1)); //restart the store store = new FederatedStore(); - federatedProperties.setGraphIds(MAP_ID_1); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); //check the graph is already in there from the cache assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(MAP_ID_1)); - assertEquals(1, store.getAllGraphIds(testUser).size()); + assertEquals(1, store.getAllGraphIds(blankUser).size()); } @Test public void shouldInitialiseWithCache() throws StoreException { + assertNull(CacheServiceLoader.getService()); + federatedProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); + assertNull(CacheServiceLoader.getService()); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); assertNotNull(CacheServiceLoader.getService()); } @Test - public void shouldThrowExceptionWithoutInitialisation() { + public void shouldThrowExceptionWithoutInitialisation() throws StoreException { + federatedProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + // Given Graph graphToAdd = new Graph.Builder() .config(new GraphConfig(ACC_ID_1)) @@ -990,7 +886,7 @@ public void shouldThrowExceptionWithoutInitialisation() { .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_EDGE_SCHEMA_JSON)) .build(); - CacheServiceLoader.shutdown(); + clearCache(); // When / Then try { @@ -1004,11 +900,11 @@ public void shouldThrowExceptionWithoutInitialisation() { @Test public void shouldNotThrowExceptionWhenInitialisedWithNoCacheClassInProperties() throws StoreException { // Given - StoreProperties storeProperties = new StoreProperties(); + federatedProperties = new FederatedStoreProperties(); // When / Then try { - store.initialise(FEDERATED_STORE_ID, null, storeProperties); + store.initialise(FEDERATED_STORE_ID, null, federatedProperties); } catch (final StoreException e) { fail("FederatedStore does not have to have a cache."); } @@ -1016,9 +912,10 @@ public void shouldNotThrowExceptionWhenInitialisedWithNoCacheClassInProperties() @Test public void shouldAddGraphsToCache() throws StoreException { - // Given + federatedProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + // Given Graph graphToAdd = new Graph.Builder() .config(new GraphConfig(ACC_ID_1)) .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_ACC_STORE_PROPERTIES)) @@ -1026,13 +923,13 @@ public void shouldAddGraphsToCache() throws StoreException { .build(); // When - store.addGraphs(null, TEST_USER, false, graphToAdd); + store.addGraphs(null, TEST_USER, true, graphToAdd); // Then - assertEquals(1, store.getGraphs(testUser, ACC_ID_1).size()); + assertEquals(1, store.getGraphs(blankUser, ACC_ID_1).size()); // When - Collection storeGraphs = store.getGraphs(testUser, null); + Collection storeGraphs = store.getGraphs(blankUser, null); // Then assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1)); @@ -1040,7 +937,7 @@ public void shouldAddGraphsToCache() throws StoreException { // When store = new FederatedStore(); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + // Then assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(ACC_ID_1)); @@ -1048,8 +945,10 @@ public void shouldAddGraphsToCache() throws StoreException { @Test public void shouldAddMultipleGraphsToCache() throws StoreException { - // Given + federatedProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + // Given + List graphsToAdd = new ArrayList<>(); for (int i = 0; i < 10; i++) { graphsToAdd.add(new Graph.Builder() @@ -1069,7 +968,7 @@ public void shouldAddMultipleGraphsToCache() throws StoreException { // When store = new FederatedStore(); - store.initialise(FEDERATED_STORE_ID, null, federatedProperties); + // Then for (int i = 0; i < 10; i++) { @@ -1097,7 +996,7 @@ private List> populateGraphs(int... expectedIds) throws StoreE .storeProperties(StreamUtil.openStream(FederatedStoreTest.class, PATH_MAP_STORE_PROPERTIES)) .addSchema(StreamUtil.openStream(FederatedStoreTest.class, PATH_BASIC_ENTITY_SCHEMA_JSON)) .build(); - store.addGraphs(Sets.newHashSet(ALL_USERS), null, false, tempGraph); + store.addGraphs(Sets.newHashSet(ALL_USERS), null, true, tempGraph); for (final int j : expectedIds) { if (i == j) { expectedGraphs.add(tempGraph); @@ -1120,8 +1019,49 @@ private Set getElements() throws uk.gov.gchq.gaffer.operation.Operation .edges(store.getSchema().getEdgeGroups()) .entities(store.getSchema().getEntityGroups()) .build()) - .build(), new Context(authUser)); + .build(), new Context(blankUser)); return (null == elements) ? Sets.newHashSet() : Sets.newHashSet(elements); } + + private void assertContains(final Throwable e, final String format, final String... s) { + boolean contains = e.getMessage().contains(String.format(format, s)); + assertTrue(e.getMessage(), contains); + } + + private void addGraphWithIds(final String graphId, final String propertiesId, final String... schemaId) throws OperationException { + ArrayList schemas = Lists.newArrayList(schemaId); + store.execute(new AddGraph.Builder() + .graphId(graphId) + .parentPropertiesId(propertiesId) + .isPublic(true) + .parentSchemaIds(schemas) + .build(), userContext); + } + + private void addGraphWithPaths(final String graphId, final String propertiesPath, final String... schemaPath) throws OperationException { + Schema.Builder schema = new Builder(); + for (String path : schemaPath) { + schema.merge(getSchemaFromPath(path)); + } + + store.execute(new AddGraph.Builder() + .graphId(graphId) + .storeProperties(getPropertiesFromPath(propertiesPath)) + .isPublic(true) + .schema(schema.build()) + .build(), userContext); + } + + private StoreProperties getPropertiesFromPath(final String pathMapStoreProperties) { + return StoreProperties.loadStoreProperties(pathMapStoreProperties); + } + + private Schema getSchemaFromPath(final String path) { + return Schema.fromJson(StreamUtil.openStream(Schema.class, path)); + } + + private void clearCache() { + CacheServiceLoader.shutdown(); + } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java index f6c4b33601f..2d728cf2504 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreWrongGraphIDsTest.java @@ -16,6 +16,7 @@ package uk.gov.gchq.gaffer.federatedstore; +import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.junit.Before; import org.junit.Test; @@ -24,6 +25,7 @@ import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; import uk.gov.gchq.gaffer.data.element.Element; import uk.gov.gchq.gaffer.data.element.Entity; +import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; import uk.gov.gchq.gaffer.mapstore.MapStoreProperties; import uk.gov.gchq.gaffer.operation.impl.add.AddElements; import uk.gov.gchq.gaffer.operation.impl.get.GetAllElements; @@ -59,10 +61,6 @@ public class FederatedStoreWrongGraphIDsTest { public void setUp() throws Exception { CacheServiceLoader.shutdown(); fedProps = new FederatedStoreProperties(); - fedProps.setGraphIds(GRAPH_1); - fedProps.setGraphPropId(GRAPH_1, PROP_1); - fedProps.setGraphSchemaId(GRAPH_1, SCHEMA_1); - fedProps.setTrueGraphIsPublicValue(GRAPH_1); fedProps.setCacheProperties(CACHE_SERVICE_CLASS_STRING); store = new FederatedStore(); @@ -88,6 +86,7 @@ public void setUp() throws Exception { @Test public void shouldThrowWhenWrongGraphIDOptionIsUsed() throws Exception { store.initialise(FED_ID, null, fedProps); + store.execute(new AddGraph.Builder().graphId(GRAPH_1).parentPropertiesId(PROP_1).parentSchemaIds(Lists.newArrayList(SCHEMA_1)).isPublic(true).build(), blankContext); final Entity expectedEntity = new Entity.Builder() .group(E1_GROUP) .vertex("v1") diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java index 0fd27754008..4d6d9f022af 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java @@ -157,7 +157,7 @@ public void shouldThrowException() throws Exception { Mockito.when(mockStore.getGraphs(user, graphID)).thenReturn(filteredGraphs); try { new FederatedOperationHandler().doOperation(op, context, mockStore); - Assert.fail("Exception Not thrown"); + fail("Exception Not thrown"); } catch (OperationException e) { Assert.assertEquals(message, e.getCause().getMessage()); } @@ -165,12 +165,13 @@ public void shouldThrowException() throws Exception { } @Test - final public void shouldNotThrowException() throws Exception { + final public void shouldNotThrowExceptionBecauseSkipFlagSetTrue() throws Exception { // Given final String graphID = "1,3"; final Operation op = Mockito.mock(Operation.class); when(op.getOption(KEY_OPERATION_OPTIONS_GRAPH_IDS)).thenReturn(graphID); when(op.getOption(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE)).thenReturn(String.valueOf(true)); + when(op.getOption(eq(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE), any(String.class))).thenReturn(String.valueOf(true)); Schema unusedSchema = new Schema.Builder().build(); @@ -191,7 +192,7 @@ final public void shouldNotThrowException() throws Exception { // When try { - new FederatedOperationHandler().doOperation(op, context, mockStore); + new FederatedOperationHandler().doOperation(op, context, mockStore); } catch (Exception e) { fail("Exception should not have been thrown: " + e.getMessage()); } @@ -200,4 +201,6 @@ final public void shouldNotThrowException() throws Exception { verify(mockStore1, atLeastOnce()).execute(any(OperationChain.class), eq(context)); verify(mockStore2, atLeastOnce()).execute(any(OperationChain.class), eq(context)); } + + } diff --git a/store-implementation/federated-store/src/test/resources/properties/federatedStoreTest.properties b/store-implementation/federated-store/src/test/resources/properties/federatedStoreTest.properties deleted file mode 100644 index 290a4ea2cf0..00000000000 --- a/store-implementation/federated-store/src/test/resources/properties/federatedStoreTest.properties +++ /dev/null @@ -1,28 +0,0 @@ -# -# Copyright 2017 Crown Copyright -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -gaffer.store.class=uk.gov.gchq.gaffer.federatedstore.FederatedStore - -gaffer.federatedstore.graphIds=edgeGraph,entityGraph - -gaffer.federatedstore.edgeGraph.properties.file=properties/singleUseMockAccStore.properties -gaffer.federatedstore.edgeGraph.schema.file=schema/basicEdgeSchema.json -gaffer.federatedstore.edgeGraph.auths=auth1, auth2 - -gaffer.federatedstore.entityGraph.properties.file=properties/singleUseMockMapStore.properties -gaffer.federatedstore.entityGraph.schema.file=schema/basicEntitySchema.json -gaffer.federatedstore.entityGraph.auths=auth1, auth2 -gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.HashMapCacheService diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties index 9007e63a1fe..063ca685c3f 100644 --- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties +++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -gaffer.store.id=PROPS_ID_1 +gaffer.store.id=mockAccProps gaffer.store.class=uk.gov.gchq.gaffer.accumulostore.SingleUseMockAccumuloStore gaffer.store.properties.class=uk.gov.gchq.gaffer.accumulostore.AccumuloProperties accumulo.instance=mockInstanceID1234 diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties index 36056340505..3b4642de584 100644 --- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties +++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties @@ -13,6 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -gaffer.store.id=PROPS_ID_1 +gaffer.store.id=mockMapProps gaffer.store.class=uk.gov.gchq.gaffer.mapstore.SingleUseMapStore gaffer.store.properties.class=uk.gov.gchq.gaffer.mapstore.MapStoreProperties diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties index 8f52a82d6f0..fa696353c6b 100644 --- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties +++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -gaffer.store.id=PROPS_ID_2 +gaffer.store.id=mockMapPropsAlt gaffer.store.class=uk.gov.gchq.gaffer.mapstore.SingleUseMapStore gaffer.store.properties.class=uk.gov.gchq.gaffer.mapstore.MapStoreProperties unusualKey=value diff --git a/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json b/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json index 30dcd3d214c..9f7c92dce64 100644 --- a/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json +++ b/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json @@ -1,4 +1,5 @@ { + "id": "basicEdgeSchema", "edges": { "BasicEdge": { "source": "vertex.string", diff --git a/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json b/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json index 76580692b70..0ce66a4877a 100644 --- a/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json +++ b/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json @@ -1,4 +1,5 @@ { + "id": "basicEntitySchema", "entities": { "BasicEntity": { "vertex": "vertex.string", diff --git a/store-implementation/federated-store/src/test/resources/schema/basicEntitySchemaWithSchemaId.json b/store-implementation/federated-store/src/test/resources/schema/basicEntitySchemaWithSchemaId.json deleted file mode 100644 index 6a9bc52ea64..00000000000 --- a/store-implementation/federated-store/src/test/resources/schema/basicEntitySchemaWithSchemaId.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "id": "SCHEMA_ID_1", - "entities": { - "BasicEntity": { - "vertex": "vertex.string", - "properties": { - "property1": "simpleProperty" - } - } - }, - "types": { - "vertex.string": { - "class": "java.lang.String" - }, - "simpleProperty": { - "class": "java.lang.Integer", - "aggregateFunction": { - "class": "uk.gov.gchq.koryphe.impl.binaryoperator.Sum" - }, - "serialiser": { - "class": "uk.gov.gchq.gaffer.serialisation.implementation.raw.CompactRawIntegerSerialiser" - } - } - } -} \ No newline at end of file From 1715a98d1c8e7fca4a04a16ff08f85e67116f1ab Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Mon, 23 Oct 2017 17:26:29 +0100 Subject: [PATCH 52/72] gh-1297 bugfix GraphDelegate.createGraph GraphDelegate.createGraph should never use the FederatedStores Properties or Schema for a graph when missing. Schema and properties can be used while graphId is known to graph, if they match. --- .../gchq/gaffer/graph/GraphSerialisable.java | 1 + .../export/graph/handler/GraphDelegate.java | 29 ++-- .../ExportToOtherGraphHandlerTest.java | 136 +++++++++++++++++- .../federatedstore/FederatedGraphStorage.java | 2 +- .../FederatedStoreConstants.java | 2 +- .../federatedstore/FederatedStoreTest.java | 13 +- 6 files changed, 157 insertions(+), 26 deletions(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java index 7da5cf0c59c..a6b4d1ebb92 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java @@ -64,6 +64,7 @@ public Graph buildGraph() { } /** + * @param library the library to use and add into the builder. * @return returns a new {@link Graph} built from the contents of a this * class. */ diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java index f9eacceabac..acb6c2e1b33 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java @@ -42,6 +42,7 @@ public final class GraphDelegate { public static final String SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S = "Schema could not be found in the graphLibrary with id: %s"; + public static final String GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S = "GraphId %s cannot be created without defined/known %s"; public static final String STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S = "Store properties could not be found in the graphLibrary with id: %s"; private GraphDelegate() { @@ -94,9 +95,6 @@ private static StoreProperties resolveStoreProperties(final Store store, final S rtn.getProperties().putAll(properties.getProperties()); } } - if (null == rtn) { - rtn = store.getProperties(); - } return rtn; } @@ -128,9 +126,6 @@ private static Schema resolveSchema(final Store store, final Schema schema, fina .build(); } } - if (null == rtn) { - rtn = store.getSchema(); - } return rtn; } @@ -181,12 +176,15 @@ public static void validate(final Store store, final String graphId, final Schem } else if (graphLibrary.exists(graphId)) { if (null != parentSchemaIds) { - Schema.Builder IdFromLibrary = new Schema.Builder(); - for (String parentSchemaId : parentSchemaIds) { - IdFromLibrary.merge(graphLibrary.getSchema(parentSchemaId)); + Schema.Builder idFromLibrary = new Schema.Builder(); + for (final String parentSchemaId : parentSchemaIds) { + Schema tempSchema = graphLibrary.getSchema(parentSchemaId); + if (null != tempSchema) { + idFromLibrary.merge(tempSchema); + } } Schema fromLibrary = graphLibrary.get(graphId).getFirst(); - if (!fromLibrary.toString().equals(IdFromLibrary.build().toString())) { + if (!fromLibrary.toString().equals(idFromLibrary.build().toString())) { result.addError("GraphId " + graphId + " already exists so you cannot use a different schema. Do not set the parentSchemaIds field."); } } @@ -199,13 +197,11 @@ public static void validate(final Store store, final String graphId, final Schem } Pair schemaStorePropertiesPair = graphLibrary.get(graphId); - Schema schemaFromLib = schemaStorePropertiesPair.getFirst(); - if (null != schema && !schema.toString().equals(schemaFromLib.toString())) { + if (null != schema && null != schemaStorePropertiesPair && !schema.toString().equals(schemaStorePropertiesPair.getFirst().toString())) { result.addError("GraphId " + graphId + " already exists so you cannot provide a different schema. Do not set the schema field."); } - StoreProperties propertyFromLib = schemaStorePropertiesPair.getSecond(); - if (null != storeProperties && !propertyFromLib.equals(storeProperties)) { + if (null != storeProperties && null != schemaStorePropertiesPair && !schemaStorePropertiesPair.getSecond().equals(storeProperties)) { result.addError("GraphId " + graphId + " already exists so you cannot provide different store properties. Do not set the storeProperties field."); } } else { @@ -215,11 +211,16 @@ public static void validate(final Store store, final String graphId, final Schem result.addError(String.format(SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, parentSchemaIds)); } } + } else if (null == schema) { + result.addError(String.format(GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S, graphId, "Schema")); } + if (null != parentStorePropertiesId) { if (null == store.getGraphLibrary().getProperties(parentStorePropertiesId)) { result.addError(String.format(STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, parentStorePropertiesId)); } + } else if (null == storeProperties) { + result.addError(String.format(GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S, graphId, "StoreProperties")); } } diff --git a/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java b/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java index 71710937550..b709861e799 100644 --- a/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java +++ b/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java @@ -26,6 +26,7 @@ import uk.gov.gchq.gaffer.commonutil.CommonTestConstants; import uk.gov.gchq.gaffer.commonutil.JsonAssert; import uk.gov.gchq.gaffer.commonutil.StreamUtil; +import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.graph.Graph; import uk.gov.gchq.gaffer.integration.store.TestStore; import uk.gov.gchq.gaffer.operation.Operation; @@ -57,6 +58,7 @@ import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate.GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S; public class ExportToOtherGraphHandlerTest { @@ -417,12 +419,14 @@ public void shouldValidateSchemaIdCannotBeUsedWhenGraphIdAlreadyExists() { GraphLibrary mockLibrary = mock(GraphLibrary.class); given(store.getGraphLibrary()).willReturn(mockLibrary); given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); + given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(new Schema.Builder().id("a").build(), new StoreProperties())); + given(mockLibrary.getSchema("schemaId")).willReturn(new Schema.Builder().id("schemaId").build()); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .parentSchemaIds("schemaId") .build(); - // When / Then + // When / Then` try { validate(export); fail("Exception expected"); @@ -432,12 +436,32 @@ public void shouldValidateSchemaIdCannotBeUsedWhenGraphIdAlreadyExists() { } } + @Test + public void shouldValidateSchemaIdCanBeUsedWhenGraphIdAlreadyExistsAndIsSame() { + // Given + GraphLibrary mockLibrary = mock(GraphLibrary.class); + given(store.getGraphLibrary()).willReturn(mockLibrary); + given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); + given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(new Schema.Builder().id("schemaId").build(), new StoreProperties())); + given(mockLibrary.getSchema("schemaId")).willReturn(new Schema.Builder().id("schemaId").build()); + final ExportToOtherGraph export = new ExportToOtherGraph.Builder() + .graphId(GRAPH_ID + 1) + .parentSchemaIds("schemaId") + .build(); + + // When / Then` + validate(export); + } + @Test public void shouldValidateSchemaCannotBeUsedWhenGraphIdAlreadyExists() { // Given GraphLibrary mockLibrary = mock(GraphLibrary.class); given(store.getGraphLibrary()).willReturn(mockLibrary); given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); + Schema schema1 = new Schema(); + schema1.setId("1"); + given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(schema1, new StoreProperties())); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .schema(new Schema()) @@ -453,12 +477,32 @@ public void shouldValidateSchemaCannotBeUsedWhenGraphIdAlreadyExists() { } } + @Test + public void shouldValidateSchemaUsedWhenGraphIdAlreadyExistsAndIsSame() { + // Given + GraphLibrary mockLibrary = mock(GraphLibrary.class); + given(store.getGraphLibrary()).willReturn(mockLibrary); + given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); + Schema schema1 = new Schema(); + schema1.setId("1"); + given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(schema1, new StoreProperties())); + final ExportToOtherGraph export = new ExportToOtherGraph.Builder() + .graphId(GRAPH_ID + 1) + .schema(schema1) + .build(); + + // When / Then + validate(export); + } + @Test public void shouldValidatePropsIdCannotBeUsedWhenGraphIdAlreadyExists() { // Given GraphLibrary mockLibrary = mock(GraphLibrary.class); given(store.getGraphLibrary()).willReturn(mockLibrary); given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); + given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(new Schema.Builder().id("a").build(), new StoreProperties("other"))); + given(mockLibrary.getProperties("props1")).willReturn(new StoreProperties("props1")); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .parentStorePropertiesId("props1") @@ -474,12 +518,33 @@ public void shouldValidatePropsIdCannotBeUsedWhenGraphIdAlreadyExists() { } } + + @Test + public void shouldValidatePropsIdCanBeUsedWhenGraphIdAlreadyExistsAndIsSame() { + // Given + GraphLibrary mockLibrary = mock(GraphLibrary.class); + given(store.getGraphLibrary()).willReturn(mockLibrary); + given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); + given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(new Schema.Builder().id("a").build(), new StoreProperties("props1"))); + given(mockLibrary.getProperties("props1")).willReturn(new StoreProperties("props1")); + final ExportToOtherGraph export = new ExportToOtherGraph.Builder() + .graphId(GRAPH_ID + 1) + .parentStorePropertiesId("props1") + .build(); + + // When / Then + validate(export); + } + @Test public void shouldValidatePropsCannotBeUsedWhenGraphIdAlreadyExists() { // Given GraphLibrary mockLibrary = mock(GraphLibrary.class); given(store.getGraphLibrary()).willReturn(mockLibrary); given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); + StoreProperties storeProperties1 = new StoreProperties(); + storeProperties1.setId("1"); + given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(new Schema(), storeProperties1)); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .storeProperties(new StoreProperties()) @@ -495,6 +560,25 @@ public void shouldValidatePropsCannotBeUsedWhenGraphIdAlreadyExists() { } } + @Test + public void shouldValidatePropsCanBeUsedWhenGraphIdAlreadyExistsAndIsSame() { + // Given + GraphLibrary mockLibrary = mock(GraphLibrary.class); + given(store.getGraphLibrary()).willReturn(mockLibrary); + given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); + StoreProperties storeProperties1 = new StoreProperties(); + storeProperties1.setId("1"); + given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(new Schema(), storeProperties1)); + final ExportToOtherGraph export = new ExportToOtherGraph.Builder() + .graphId(GRAPH_ID + 1) + .storeProperties(storeProperties1) + .build(); + + // When / Then + validate(export); + } + + @Test public void shouldValidateSchemaIdCannotBeFound() { // Given @@ -504,6 +588,29 @@ public void shouldValidateSchemaIdCannotBeFound() { final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .parentSchemaIds("schemaId") + .storeProperties(new StoreProperties()) + .build(); + + // When / Then + try { + validate(export); + fail("Exception expected"); + } catch (final IllegalArgumentException e) { + assertEquals("Validation errors: \n" + + "Schema could not be found in the graphLibrary with id: [schemaId]", e.getMessage()); + } + } + + @Test + public void shouldValidatePropertiesCannotBeUsedIfNotDefinedOrFound() { + // Given + GraphLibrary mockLibrary = mock(GraphLibrary.class); + given(store.getGraphLibrary()).willReturn(mockLibrary); + given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(false); + final ExportToOtherGraph export = new ExportToOtherGraph.Builder() + .graphId(GRAPH_ID + 1) + .parentSchemaIds("schemaId") + .storeProperties(new StoreProperties()) .build(); // When / Then @@ -524,7 +631,7 @@ public void shouldValidatePropsIdCannotBeFound() { given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(false); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) - .parentStorePropertiesId("propsId") + .schema(new Schema()) .build(); // When / Then @@ -533,7 +640,28 @@ public void shouldValidatePropsIdCannotBeFound() { fail("Exception expected"); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + - "Store properties could not be found in the graphLibrary with id: propsId", e.getMessage()); + String.format(GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S, GRAPH_ID + 1, "StoreProperties"), e.getMessage()); + } + } + + @Test + public void shouldValidateSchemaCannotBeUsedIfNotDefinedOrFound() { + // Given + GraphLibrary mockLibrary = mock(GraphLibrary.class); + given(store.getGraphLibrary()).willReturn(mockLibrary); + given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(false); + final ExportToOtherGraph export = new ExportToOtherGraph.Builder() + .graphId(GRAPH_ID + 1) + .storeProperties(new StoreProperties()) + .build(); + + // When / Then + try { + validate(export); + fail("Exception expected"); + } catch (final IllegalArgumentException e) { + assertEquals("Validation errors: \n" + + String.format(GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S, GRAPH_ID + 1, "Schema"), e.getMessage()); } } @@ -545,6 +673,8 @@ public void shouldPassValidation() { given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(false); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) + .schema(new Schema()) + .storeProperties(new StoreProperties()) .build(); // When diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java index fa67cbde1cd..cb54c5148cd 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java @@ -352,7 +352,7 @@ private void initCache(final FederatedStoreProperties properties) { private void makeAllGraphsFromCache() { final Set allGraphIds = federatedStoreCache.getAllGraphIds(); - for (String graphId : allGraphIds) { + for (final String graphId : allGraphIds) { makeGraphFromCache(graphId); } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java index 1a47e0091e3..d351e49fdc8 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java @@ -30,7 +30,7 @@ private FederatedStoreConstants() { // only contains constants. } - public static String getSkipFailedFederatedStoreExecute(Operation op) { + public static String getSkipFailedFederatedStoreExecute(final Operation op) { return op.getOption(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE, DEFAULT_VALUE_KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE); } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index ab4b6e950e0..838c2fff5fa 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -25,7 +25,6 @@ import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.commonutil.CommonConstants; -import uk.gov.gchq.gaffer.commonutil.JsonUtil; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; import uk.gov.gchq.gaffer.data.element.Edge; @@ -68,6 +67,7 @@ import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreUser.TEST_USER; import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreUser.blankUser; import static uk.gov.gchq.gaffer.federatedstore.operation.handler.FederatedOperationOutputHandler.NO_RESULTS_TO_MERGE_ERROR; +import static uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate.GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S; import static uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate.SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S; import static uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate.STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S; @@ -134,7 +134,7 @@ public void tearDown() throws Exception { assertEquals("Library has changed: " + ID_PROPS_MAP, library.getProperties(ID_PROPS_MAP), getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES)); assertEquals("Library has changed: " + ID_PROPS_MAP_ALT, library.getProperties(ID_PROPS_MAP_ALT), getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES_ALT)); assertEquals("Library has changed: " + ID_SCHEMA_EDGE, new String(library.getSchema(ID_SCHEMA_EDGE).toJson(false), CommonConstants.UTF_8), new String(getSchemaFromPath(PATH_BASIC_EDGE_SCHEMA_JSON).toJson(false), CommonConstants.UTF_8)); - assertTrue("Library has changed: " + ID_SCHEMA_ENTITY, JsonUtil.equals(library.getSchema(ID_SCHEMA_ENTITY).toJson(false), getSchemaFromPath(PATH_BASIC_ENTITY_SCHEMA_JSON).toJson(false))); + assertEquals("Library has changed: " + ID_SCHEMA_ENTITY, new String(library.getSchema(ID_SCHEMA_ENTITY).toJson(false), CommonConstants.UTF_8), new String(getSchemaFromPath(PATH_BASIC_ENTITY_SCHEMA_JSON).toJson(false), CommonConstants.UTF_8)); } @Test @@ -189,8 +189,8 @@ public void shouldThrowErrorForMissingProperty() throws Exception { .parentSchemaIds(schemas) .build(), userContext); fail("a graph was created without a defined properties"); - } catch (final IllegalArgumentException e) { - assertContains(e, STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, INVALID); + } catch (final Exception e) { + assertContains(e.getCause(), GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S, MAP_ID_1, "StoreProperties"); } } @@ -204,8 +204,8 @@ public void shouldThrowErrorForMissingSchema() throws Exception { .parentPropertiesId(ID_PROPS_MAP) .build(), userContext); fail("a graph was created without a defined schema"); - } catch (final IllegalArgumentException e) { - assertContains(e, STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, INVALID); + } catch (final Exception e) { + assertContains(e.getCause(), GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S, MAP_ID_1, "Schema"); } } @@ -539,7 +539,6 @@ public void shouldAddGraphWithSchemaFromGraphLibraryOverridden() throws Exceptio // Then assertEquals(1, store.getGraphs(blankUser, null).size()); assertTrue(store.getGraphs(blankUser, null).iterator().next().getSchema().getEntityGroups().contains("BasicEntity")); - } @Test From 3a340e4d2ccfe06b15703c01a7744ae67faac30b Mon Sep 17 00:00:00 2001 From: m55624 Date: Tue, 31 Oct 2017 15:09:05 +0000 Subject: [PATCH 53/72] gh-1297 - test fixes --- .../export/graph/handler/GraphDelegate.java | 24 +++-- .../ExportToOtherGraphHandlerTest.java | 102 ++++++++---------- 2 files changed, 60 insertions(+), 66 deletions(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java index 69a03989269..1b227603f79 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java @@ -44,6 +44,9 @@ public final class GraphDelegate { public static final String SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S = "Schema could not be found in the graphLibrary with id: %s"; public static final String GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S = "GraphId %s cannot be created without defined/known %s"; public static final String STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S = "Store properties could not be found in the graphLibrary with id: %s"; + public static final String S_CANNOT_BE_USED_WITHOUT_A_GRAPH_LIBRARY = " %s cannot be used without a GraphLibrary"; + public static final String CANNOT_EXPORT_TO_THE_SAME_GRAPH_S = "Cannot export to the same Graph: %s"; + public static final String GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD = "Graph: %s already exists so you cannot use a different %s. Do not set the %s field"; private GraphDelegate() { // Private constructor to prevent instantiation. @@ -93,6 +96,9 @@ private static StoreProperties resolveStoreProperties(final Store store, final S rtn.merge(properties); } } + if (null == rtn) { + rtn = store.getProperties(); + } return rtn; } @@ -119,11 +125,13 @@ private static Schema resolveSchema(final Store store, final Schema schema, fina // delete the old schema id as we are about to modify the schema rtn = new Schema.Builder() .merge(rtn) - .id(null) .merge(schema) .build(); } } + if (null == rtn) { + rtn = store.getSchema(); + } return rtn; } @@ -161,15 +169,15 @@ public static void validate(final Store store, final String graphId, final Schem final ValidationResult result = new ValidationResult(); if (graphId.equals(store.getGraphId())) { - result.addError("Cannot export to the same graph: " + graphId); + result.addError(String.format(CANNOT_EXPORT_TO_THE_SAME_GRAPH_S, graphId)); } if (null == graphLibrary) { // No graph library so we cannot look up the graphId/schemaId/storePropertiesId if (null != parentSchemaIds) { - result.addError("parentSchemaIds cannot be used without a GraphLibrary"); + result.addError(String.format(S_CANNOT_BE_USED_WITHOUT_A_GRAPH_LIBRARY, "parentSchemaIds")); } if (null != parentStorePropertiesId) { - result.addError("parentStorePropertiesId cannot be used without a GraphLibrary"); + result.addError(String.format(S_CANNOT_BE_USED_WITHOUT_A_GRAPH_LIBRARY, "parentStorePropertiesId")); } } else if (graphLibrary.exists(graphId)) { @@ -183,24 +191,24 @@ public static void validate(final Store store, final String graphId, final Schem } Schema fromLibrary = graphLibrary.get(graphId).getFirst(); if (!fromLibrary.toString().equals(idFromLibrary.build().toString())) { - result.addError("GraphId " + graphId + " already exists so you cannot use a different schema. Do not set the parentSchemaIds field."); + result.addError(String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, graphId, "Schema", "parentSchemaIds")); } } if (null != parentStorePropertiesId) { StoreProperties fromLibrary = graphLibrary.get(graphId).getSecond(); if (!fromLibrary.equals(graphLibrary.getProperties(parentStorePropertiesId))) { - result.addError("GraphId " + graphId + " already exists so you cannot use different store properties. Do not set the parentStorePropertiesId field."); + result.addError(String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, graphId, "StoreProperties", "parentStorePropertiesId")); } } Pair schemaStorePropertiesPair = graphLibrary.get(graphId); if (null != schema && null != schemaStorePropertiesPair && !schema.toString().equals(schemaStorePropertiesPair.getFirst().toString())) { - result.addError("GraphId " + graphId + " already exists so you cannot provide a different schema. Do not set the schema field."); + result.addError(String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, graphId, "Schema", "schema")); } if (null != storeProperties && null != schemaStorePropertiesPair && !schemaStorePropertiesPair.getSecond().equals(storeProperties)) { - result.addError("GraphId " + graphId + " already exists so you cannot provide different store properties. Do not set the storeProperties field."); + result.addError(String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, graphId, "StoreProperties", "storeProperties")); } } else { if (null != parentSchemaIds) { diff --git a/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java b/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java index d16df3e1a7a..006c6ae5929 100644 --- a/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java +++ b/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java @@ -53,12 +53,16 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate.CANNOT_EXPORT_TO_THE_SAME_GRAPH_S; import static uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate.GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S; +import static uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate.GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD; +import static uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate.SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S; +import static uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate.STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S; +import static uk.gov.gchq.gaffer.operation.export.graph.handler.GraphDelegate.S_CANNOT_BE_USED_WITHOUT_A_GRAPH_LIBRARY; public class ExportToOtherGraphHandlerTest { @@ -66,6 +70,7 @@ public class ExportToOtherGraphHandlerTest { private static final String STORE_PROPS_ID = "storePropsId"; public static final String STORE_PROPS_ID_1 = STORE_PROPS_ID + 1; private static final String SCHEMA_ID = "schemaId"; + private static final String EXCEPTION_EXPECTED = "Exception expected"; public static final String SCHEMA_ID_2 = SCHEMA_ID + 2; public static final String SCHEMA_ID_1 = SCHEMA_ID + 1; @Rule @@ -81,6 +86,9 @@ public void before() throws IOException { final File graphLibraryFolder = testFolder.newFolder("graphLibrary"); graphLibrary = new FileGraphLibrary(graphLibraryFolder.getPath()); + + given(store.getGraphLibrary()).willReturn(graphLibrary); + given(store.getGraphId()).willReturn(GRAPH_ID); } @Test @@ -93,7 +101,6 @@ public void shouldGetExporterClass() { @Test public void shouldThrowExceptionWhenExportingToSameGraph() { // Given - given(store.getGraphId()).willReturn(GRAPH_ID); graphLibrary.add(GRAPH_ID, SCHEMA_ID, schema, STORE_PROPS_ID, storeProperties); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID) @@ -102,18 +109,17 @@ public void shouldThrowExceptionWhenExportingToSameGraph() { // When / Then try { createGraph(export); - fail("Exception expected"); + fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { - assertTrue(e.getMessage().contains("same graph")); + assertEquals("Validation errors: \n" + + String.format(CANNOT_EXPORT_TO_THE_SAME_GRAPH_S, GRAPH_ID), e.getMessage()); } } @Test public void shouldCreateExporter() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, OperationException { // Given - given(store.getGraphId()).willReturn(GRAPH_ID); graphLibrary.add(GRAPH_ID + 1, SCHEMA_ID, schema, STORE_PROPS_ID, storeProperties); - given(store.getGraphLibrary()).willReturn(graphLibrary); final Context context = mock(Context.class); final User user = new User(); given(context.getUser()).willReturn(user); @@ -148,9 +154,7 @@ public void shouldCreateExporter() throws NoSuchMethodException, InvocationTarge @Test public void shouldCreateNewGraphWithStoreGraphLibrary() { // Given - given(store.getGraphId()).willReturn(GRAPH_ID); graphLibrary.add(GRAPH_ID + 1, schema, storeProperties); - given(store.getGraphLibrary()).willReturn(graphLibrary); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .build(); @@ -169,7 +173,6 @@ public void shouldCreateNewGraphWithStoresStoreProperties() { // Given Schema schema1 = new Schema.Builder().build(); given(store.getProperties()).willReturn(storeProperties); - given(store.getGraphId()).willReturn(GRAPH_ID); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) @@ -191,7 +194,6 @@ public void shouldCreateNewGraphWithStoresSchema() { final StoreProperties storeProperties1 = StoreProperties.loadStoreProperties(StreamUtil.storeProps(getClass())); given(store.getSchema()).willReturn(schema); - given(store.getGraphId()).willReturn(GRAPH_ID); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) @@ -210,13 +212,10 @@ public void shouldCreateNewGraphWithStoresSchema() { @Test public void shouldCreateNewGraphWithParentSchemaId() { // Given - given(store.getGraphId()).willReturn(GRAPH_ID); - Schema schema1 = new Schema.Builder().build(); - graphLibrary.addOrUpdate(GRAPH_ID + 1, schema, storeProperties); + graphLibrary.addOrUpdate(GRAPH_ID + 1, SCHEMA_ID, schema, STORE_PROPS_ID, storeProperties); graphLibrary.addSchema(SCHEMA_ID_1, schema1); - given(store.getGraphLibrary()).willReturn(graphLibrary); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 2) @@ -236,8 +235,6 @@ public void shouldCreateNewGraphWithParentSchemaId() { @Test public void shouldCreateNewGraphWithMergedParentSchemaIdAndProvidedSchema() { // Given - given(store.getGraphId()).willReturn(GRAPH_ID); - Schema schema1 = new Schema.Builder() .entity("entity", new SchemaEntityDefinition.Builder() .vertex("vertex") @@ -255,7 +252,6 @@ public void shouldCreateNewGraphWithMergedParentSchemaIdAndProvidedSchema() { graphLibrary.addOrUpdate(GRAPH_ID + 1, schema, storeProperties); graphLibrary.addSchema(SCHEMA_ID_1, schema1); graphLibrary.addSchema(SCHEMA_ID_2, schema2); - given(store.getGraphLibrary()).willReturn(graphLibrary); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 2) @@ -286,13 +282,10 @@ public void shouldCreateNewGraphWithMergedParentSchemaIdAndProvidedSchema() { @Test public void shouldCreateNewGraphWithParentStorePropertiesId() { // Given - given(store.getGraphId()).willReturn(GRAPH_ID); - StoreProperties storeProperties1 = StoreProperties.loadStoreProperties(StreamUtil.storeProps(getClass())); - graphLibrary.addOrUpdate(GRAPH_ID + 1, schema, storeProperties); + graphLibrary.addOrUpdate(GRAPH_ID + 1, SCHEMA_ID, schema, STORE_PROPS_ID, storeProperties); graphLibrary.addProperties(STORE_PROPS_ID_1, storeProperties1); - given(store.getGraphLibrary()).willReturn(graphLibrary); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 2) @@ -312,13 +305,11 @@ public void shouldCreateNewGraphWithParentStorePropertiesId() { @Test public void shouldCreateNewGraphWithMergedParentStorePropertiesIdAndProvidedStoreProperties() { // Given - given(store.getGraphId()).willReturn(GRAPH_ID); StoreProperties storeProperties1 = StoreProperties.loadStoreProperties(StreamUtil.storeProps(getClass())); - graphLibrary.addOrUpdate(GRAPH_ID + 1, schema, storeProperties); + graphLibrary.addOrUpdate(GRAPH_ID + 1, SCHEMA_ID, schema, STORE_PROPS_ID, storeProperties); graphLibrary.addProperties(STORE_PROPS_ID_1, storeProperties1); - given(store.getGraphLibrary()).willReturn(graphLibrary); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 2) @@ -340,25 +331,25 @@ public void shouldCreateNewGraphWithMergedParentStorePropertiesIdAndProvidedStor @Test public void shouldValidateGraphIdMustBeDifferent() { // Given - given(store.getGraphId()).willReturn(GRAPH_ID); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID) + .schema(schema) + .storeProperties(storeProperties) .build(); // When / Then try { validate(export); - fail("Exception expected"); + fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + - "Cannot export to the same graph: graphId", e.getMessage()); + String.format(CANNOT_EXPORT_TO_THE_SAME_GRAPH_S, "graphId"), e.getMessage()); } } @Test public void shouldValidatePropsIdCannotBeUsedWithoutGraphLibrary() { // Given - given(store.getGraphId()).willReturn(GRAPH_ID); given(store.getGraphLibrary()).willReturn(null); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) @@ -368,10 +359,11 @@ public void shouldValidatePropsIdCannotBeUsedWithoutGraphLibrary() { // When / Then try { validate(export); - fail("Exception expected"); + fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { + System.out.println(e.getMessage()); assertEquals("Validation errors: \n" + - "parentStorePropertiesId cannot be used without a GraphLibrary", e.getMessage()); + String.format(S_CANNOT_BE_USED_WITHOUT_A_GRAPH_LIBRARY, "parentStorePropertiesId"), e.getMessage()); } } @@ -387,21 +379,18 @@ public void shouldValidateSchemaIdCannotBeUsedWithoutGraphLibrary() { // When / Then try { validate(export); - fail("Exception expected"); + fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + - "parentSchemaIds cannot be used without a GraphLibrary", e.getMessage()); + String.format(S_CANNOT_BE_USED_WITHOUT_A_GRAPH_LIBRARY, "parentSchemaIds"), e.getMessage()); } } @Test public void shouldValidateSchemaIdCannotBeUsedWhenGraphIdAlreadyExists() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); - given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(new Schema.Builder().id("a").build(), new StoreProperties())); - given(mockLibrary.getSchema("schemaId")).willReturn(new Schema.Builder().id("schemaId").build()); + graphLibrary.add(GRAPH_ID + 1, SCHEMA_ID_1, new Schema.Builder().edge("edge", new SchemaEdgeDefinition()).build(), STORE_PROPS_ID, new StoreProperties()); + graphLibrary.addSchema(SCHEMA_ID, new Schema.Builder().build()); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .parentSchemaIds(SCHEMA_ID) @@ -410,24 +399,21 @@ public void shouldValidateSchemaIdCannotBeUsedWhenGraphIdAlreadyExists() { // When / Then` try { validate(export); - fail("Exception expected"); + fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + - "GraphId graphId1 already exists so you cannot use a different schema. Do not set the parentSchemaIds field.", e.getMessage()); + String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, "graphId1", "Schema", "parentSchemaIds"), e.getMessage()); } } @Test public void shouldValidateSchemaIdCanBeUsedWhenGraphIdAlreadyExistsAndIsSame() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); - given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(new Schema.Builder().id("schemaId").build(), new StoreProperties())); - given(mockLibrary.getSchema("schemaId")).willReturn(new Schema.Builder().id("schemaId").build()); + graphLibrary.add(GRAPH_ID + 1, SCHEMA_ID, new Schema.Builder().build(), STORE_PROPS_ID, new StoreProperties()); + graphLibrary.addSchema(SCHEMA_ID, new Schema.Builder().build()); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) - .parentSchemaIds("schemaId") + .parentSchemaIds(SCHEMA_ID) .build(); // When / Then` @@ -451,10 +437,10 @@ public void shouldValidateSchemaCannotBeUsedWhenGraphIdAlreadyExists() { // When / Then try { validate(export); - fail("Exception expected"); + fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + - "GraphId graphId1 already exists so you cannot provide a different schema. Do not set the schema field.", e.getMessage()); + String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, "graphId1", "Schema", "schema"), e.getMessage()); } } @@ -492,10 +478,10 @@ public void shouldValidatePropsIdCannotBeUsedWhenGraphIdAlreadyExists() { // When / Then try { validate(export); - fail("Exception expected"); + fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + - "GraphId graphId1 already exists so you cannot use different store properties. Do not set the parentStorePropertiesId field.", e.getMessage()); + String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, "graphId1", "StoreProperties", "parentStorePropertiesId"), e.getMessage()); } } @@ -534,10 +520,10 @@ public void shouldValidatePropsCannotBeUsedWhenGraphIdAlreadyExists() { // When / Then try { validate(export); - fail("Exception expected"); + fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + - "GraphId graphId1 already exists so you cannot provide different store properties. Do not set the storeProperties field.", e.getMessage()); + String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, "graphId1", "StoreProperties", "storeProperties"), e.getMessage()); } } @@ -575,10 +561,10 @@ public void shouldValidateSchemaIdCannotBeFound() { // When / Then try { validate(export); - fail("Exception expected"); + fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + - "Schema could not be found in the graphLibrary with id: [schemaId]", e.getMessage()); + String.format(SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, "[schemaId]"), e.getMessage()); } } @@ -597,10 +583,10 @@ public void shouldValidatePropertiesCannotBeUsedIfNotDefinedOrFound() { // When / Then try { validate(export); - fail("Exception expected"); + fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + - "Schema could not be found in the graphLibrary with id: [schemaId]", e.getMessage()); + String.format(SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, "[schemaId]"), e.getMessage()); } } @@ -619,10 +605,10 @@ public void shouldValidatePropsIdCannotBeFound() { // When / Then try { validate(export); - fail("Exception expected"); + fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + - String.format("Store properties could not be found in the graphLibrary with id: %s", STORE_PROPS_ID), e.getMessage()); + String.format(STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, STORE_PROPS_ID), e.getMessage()); } } @@ -640,7 +626,7 @@ public void shouldValidateSchemaCannotBeUsedIfNotDefinedOrFound() { // When / Then try { validate(export); - fail("Exception expected"); + fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + String.format(GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S, GRAPH_ID + 1, "Schema"), e.getMessage()); From eb6478126baeefaf208cdd7b46e48da758bf2f67 Mon Sep 17 00:00:00 2001 From: m55624 Date: Tue, 31 Oct 2017 15:31:12 +0000 Subject: [PATCH 54/72] gh-1297 - test fixes --- .../ExportToOtherGraphHandlerTest.java | 95 ++++++------------- 1 file changed, 28 insertions(+), 67 deletions(-) diff --git a/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java b/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java index 006c6ae5929..8aa679e1f3e 100644 --- a/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java +++ b/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java @@ -26,7 +26,6 @@ import uk.gov.gchq.gaffer.commonutil.CommonTestConstants; import uk.gov.gchq.gaffer.commonutil.JsonAssert; import uk.gov.gchq.gaffer.commonutil.StreamUtil; -import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.graph.Graph; import uk.gov.gchq.gaffer.integration.store.TestStore; import uk.gov.gchq.gaffer.operation.Operation; @@ -348,7 +347,7 @@ public void shouldValidateGraphIdMustBeDifferent() { } @Test - public void shouldValidatePropsIdCannotBeUsedWithoutGraphLibrary() { + public void shouldValidateParentPropsIdCannotBeUsedWithoutGraphLibrary() { // Given given(store.getGraphLibrary()).willReturn(null); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() @@ -368,7 +367,7 @@ public void shouldValidatePropsIdCannotBeUsedWithoutGraphLibrary() { } @Test - public void shouldValidateSchemaIdCannotBeUsedWithoutGraphLibrary() { + public void shouldValidateParentSchemaIdCannotBeUsedWithoutGraphLibrary() { // Given given(store.getGraphLibrary()).willReturn(null); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() @@ -387,7 +386,7 @@ public void shouldValidateSchemaIdCannotBeUsedWithoutGraphLibrary() { } @Test - public void shouldValidateSchemaIdCannotBeUsedWhenGraphIdAlreadyExists() { + public void shouldValidateParentSchemaIdCannotBeUsedWhenGraphIdAlreadyExists() { // Given graphLibrary.add(GRAPH_ID + 1, SCHEMA_ID_1, new Schema.Builder().edge("edge", new SchemaEdgeDefinition()).build(), STORE_PROPS_ID, new StoreProperties()); graphLibrary.addSchema(SCHEMA_ID, new Schema.Builder().build()); @@ -407,7 +406,7 @@ public void shouldValidateSchemaIdCannotBeUsedWhenGraphIdAlreadyExists() { } @Test - public void shouldValidateSchemaIdCanBeUsedWhenGraphIdAlreadyExistsAndIsSame() { + public void shouldValidateParentSchemaIdCanBeUsedWhenGraphIdAlreadyExistsAndIsSame() { // Given graphLibrary.add(GRAPH_ID + 1, SCHEMA_ID, new Schema.Builder().build(), STORE_PROPS_ID, new StoreProperties()); graphLibrary.addSchema(SCHEMA_ID, new Schema.Builder().build()); @@ -423,12 +422,7 @@ public void shouldValidateSchemaIdCanBeUsedWhenGraphIdAlreadyExistsAndIsSame() { @Test public void shouldValidateSchemaCannotBeUsedWhenGraphIdAlreadyExists() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); - Schema schema1 = new Schema(); - schema1.setId("1"); - given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(schema1, new StoreProperties())); + graphLibrary.add(GRAPH_ID + 1, SCHEMA_ID, new Schema.Builder().edge("testEdge", new SchemaEdgeDefinition()).build(), STORE_PROPS_ID, new StoreProperties()); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .schema(new Schema()) @@ -447,12 +441,8 @@ public void shouldValidateSchemaCannotBeUsedWhenGraphIdAlreadyExists() { @Test public void shouldValidateSchemaUsedWhenGraphIdAlreadyExistsAndIsSame() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); - Schema schema1 = new Schema(); - schema1.setId("1"); - given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(schema1, new StoreProperties())); + Schema schema1 = new Schema.Builder().edge("testEdge", new SchemaEdgeDefinition()).build(); + graphLibrary.add(GRAPH_ID + 1, SCHEMA_ID + 1, schema1, STORE_PROPS_ID, new StoreProperties()); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .schema(schema1) @@ -463,16 +453,14 @@ public void shouldValidateSchemaUsedWhenGraphIdAlreadyExistsAndIsSame() { } @Test - public void shouldValidatePropsIdCannotBeUsedWhenGraphIdAlreadyExists() { + public void shouldValidateParentPropsIdCannotBeUsedWhenGraphIdAlreadyExists() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); - given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(new Schema.Builder().id("a").build(), new StoreProperties("other"))); - given(mockLibrary.getProperties("props1")).willReturn(new StoreProperties("props1")); + StoreProperties storeProperties1 = new StoreProperties(); + storeProperties1.set("testKey", "testValue"); + graphLibrary.add(GRAPH_ID + 1, SCHEMA_ID, new Schema.Builder().build(), STORE_PROPS_ID, storeProperties1); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) - .parentStorePropertiesId(STORE_PROPS_ID_1) + .parentStorePropertiesId(STORE_PROPS_ID + 1) .build(); // When / Then @@ -487,16 +475,12 @@ public void shouldValidatePropsIdCannotBeUsedWhenGraphIdAlreadyExists() { @Test - public void shouldValidatePropsIdCanBeUsedWhenGraphIdAlreadyExistsAndIsSame() { + public void shouldValidateParentPropsIdCanBeUsedWhenGraphIdAlreadyExistsAndIsSame() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); - given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(new Schema.Builder().id("a").build(), new StoreProperties("props1"))); - given(mockLibrary.getProperties("props1")).willReturn(new StoreProperties("props1")); + graphLibrary.add(GRAPH_ID + 1, SCHEMA_ID, new Schema(), STORE_PROPS_ID_1, new StoreProperties()); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) - .parentStorePropertiesId("props1") + .parentStorePropertiesId(STORE_PROPS_ID_1) .build(); // When / Then @@ -506,12 +490,9 @@ public void shouldValidatePropsIdCanBeUsedWhenGraphIdAlreadyExistsAndIsSame() { @Test public void shouldValidatePropsCannotBeUsedWhenGraphIdAlreadyExists() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); StoreProperties storeProperties1 = new StoreProperties(); - storeProperties1.setId("1"); - given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(new Schema(), storeProperties1)); + storeProperties1.set("testKey", "testValue"); + graphLibrary.add(GRAPH_ID + 1, SCHEMA_ID, new Schema.Builder().build(), STORE_PROPS_ID, storeProperties1); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .storeProperties(new StoreProperties()) @@ -530,12 +511,9 @@ public void shouldValidatePropsCannotBeUsedWhenGraphIdAlreadyExists() { @Test public void shouldValidatePropsCanBeUsedWhenGraphIdAlreadyExistsAndIsSame() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(true); StoreProperties storeProperties1 = new StoreProperties(); - storeProperties1.setId("1"); - given(mockLibrary.get(GRAPH_ID + 1)).willReturn(new Pair<>(new Schema(), storeProperties1)); + storeProperties1.set("testKey", "testValue"); + graphLibrary.add(GRAPH_ID + 1, SCHEMA_ID, new Schema.Builder().build(), STORE_PROPS_ID, storeProperties1); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .storeProperties(storeProperties1) @@ -545,13 +523,9 @@ public void shouldValidatePropsCanBeUsedWhenGraphIdAlreadyExistsAndIsSame() { validate(export); } - @Test - public void shouldValidateSchemaIdCannotBeFound() { + public void shouldThrowExceptionSchemaIdCannotBeFound() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(false); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .parentSchemaIds(SCHEMA_ID) @@ -569,15 +543,12 @@ public void shouldValidateSchemaIdCannotBeFound() { } @Test - public void shouldValidatePropertiesCannotBeUsedIfNotDefinedOrFound() { + public void shouldThrowExceptionPropsIdCannotBeFound() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(false); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) - .parentSchemaIds(SCHEMA_ID) - .storeProperties(new StoreProperties()) + .schema(new Schema()) + .parentStorePropertiesId(STORE_PROPS_ID) .build(); // When / Then @@ -586,20 +557,16 @@ public void shouldValidatePropertiesCannotBeUsedIfNotDefinedOrFound() { fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + - String.format(SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, "[schemaId]"), e.getMessage()); + String.format(STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, STORE_PROPS_ID), e.getMessage()); } } @Test - public void shouldValidatePropsIdCannotBeFound() { + public void shouldThrowExceptionPropertiesCannotBeUsedIfNotDefinedOrFound() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(false); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .schema(new Schema()) - .parentStorePropertiesId(STORE_PROPS_ID) .build(); // When / Then @@ -608,16 +575,13 @@ public void shouldValidatePropsIdCannotBeFound() { fail(EXCEPTION_EXPECTED); } catch (final IllegalArgumentException e) { assertEquals("Validation errors: \n" + - String.format(STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, STORE_PROPS_ID), e.getMessage()); + String.format(GRAPH_ID_S_CANNOT_BE_CREATED_WITHOUT_DEFINED_KNOWN_S, GRAPH_ID + 1, "StoreProperties"), e.getMessage()); } } @Test - public void shouldValidateSchemaCannotBeUsedIfNotDefinedOrFound() { + public void shouldThrowExceptionSchemaCannotBeUsedIfNotDefinedOrFound() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(false); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .storeProperties(new StoreProperties()) @@ -634,11 +598,8 @@ public void shouldValidateSchemaCannotBeUsedIfNotDefinedOrFound() { } @Test - public void shouldPassValidation() { + public void shouldValidateWithSchemaAndStorePropertiesSepcified() { // Given - GraphLibrary mockLibrary = mock(GraphLibrary.class); - given(store.getGraphLibrary()).willReturn(mockLibrary); - given(mockLibrary.exists(GRAPH_ID + 1)).willReturn(false); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) .schema(new Schema()) From 902735135cfb6a51eb8da1970b1731830e37af09 Mon Sep 17 00:00:00 2001 From: m55624 Date: Tue, 31 Oct 2017 15:48:04 +0000 Subject: [PATCH 55/72] removal of use of Schema ids --- .../federatedstore/FederatedStoreProperties.java | 7 ------- .../handler/impl/FederatedAddGraphHandler.java | 6 ++++-- .../handler/impl/FederatedAddGraphHandlerTest.java | 11 +++++------ .../src/test/resources/schema/basicEdgeSchema.json | 1 - .../src/test/resources/schema/basicEntitySchema.json | 1 - 5 files changed, 9 insertions(+), 17 deletions(-) diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java index d883ff04552..4fe0d715e0b 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java @@ -40,17 +40,10 @@ public class FederatedStoreProperties extends StoreProperties { public static final String CUSTOM_PROPERTIES_AUTHS = "gaffer.federatedstore.customPropertiesAuths"; public static final String CUSTOM_PROPERTIES_AUTHS_DEFAULT = null; - /** - * This is used.... - * e.g gaffer.federatedstore.graph1.auths=auth1,auth2 - */ - private static final String AUTHS = "auths"; - /** * This is used.... * e.g gaffer.federatedstore.graph1.isPublic=false */ - private static final String IS_PUBLIC = "isPublic"; public static final String IS_PUBLIC_DEFAULT = String.valueOf(false); public static final String CACHE_SERVICE_CLASS = CacheProperties.CACHE_SERVICE_CLASS; public static final String CACHE_SERVICE_CLASS_DEFAULT = null; diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java index f15cd6fd2b5..060848f55f1 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java @@ -36,6 +36,8 @@ public class FederatedAddGraphHandler implements OperationHandler { public static final String ERROR_BUILDING_GRAPH_GRAPH_ID_S = "Error building graph. graphId: %s"; + public static final String ERROR_ADDING_GRAPH_GRAPH_ID_S = "Error adding graph. graphId: %s"; + public static final String USER_IS_LIMITED_TO_ONLY_USING_PARENT_PROPERTIES_ID_FROM_GRAPHLIBRARY_BUT_FOUND_STORE_PROPERTIES_S = "User is limited to only using parentPropertiesId from the graphLibrary, but found storeProperties: %s"; @Override public Void doOperation(final AddGraph operation, final Context context, final Store store) throws OperationException { @@ -43,7 +45,7 @@ public Void doOperation(final AddGraph operation, final Context context, final S boolean isLimitedToLibraryProperties = ((FederatedStore) store).isLimitedToLibraryProperties(user); if (isLimitedToLibraryProperties && null != operation.getStoreProperties()) { - throw new OperationException("User is limited to only using parentPropertiesId from the graphLibrary, but found storeProperties:" + operation.getProperties().toString()); + throw new OperationException(String.format(USER_IS_LIMITED_TO_ONLY_USING_PARENT_PROPERTIES_ID_FROM_GRAPHLIBRARY_BUT_FOUND_STORE_PROPERTIES_S, operation.getProperties().toString())); } final Graph graph; @@ -58,7 +60,7 @@ public Void doOperation(final AddGraph operation, final Context context, final S try { ((FederatedStore) store).addGraphs(operation.getGraphAuths(), context.getUser().getUserId(), operation.getIsPublic(), graph); } catch (final Exception e) { - throw new OperationException("Error adding graph: " + operation.getGraphId(), e); + throw new OperationException(String.format(ERROR_ADDING_GRAPH_GRAPH_ID_S, operation.getGraphId()), e); } return null; } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java index b037b41f725..31104ff9408 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java @@ -56,6 +56,7 @@ public class FederatedAddGraphHandlerTest { private static final String EXPECTED_GRAPH_ID = "testGraphID"; private static final String EXPECTED_GRAPH_ID_2 = "testGraphID2"; private static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService"; + private static final String EXCEPTION_EXPECTED = "Exception expected"; private User testUser; private User authUser; private FederatedStore store; @@ -153,7 +154,7 @@ public void shouldAddGraphUsingLibrary() throws Exception { assertEquals(expectedSchema, next.getSchema()); final GraphLibrary library = new HashMapGraphLibrary(); - library.add(EXPECTED_GRAPH_ID_2,expectedSchema,storeProperties); + library.add(EXPECTED_GRAPH_ID_2, expectedSchema, storeProperties); store.setGraphLibrary(library); federatedAddGraphHandler.doOperation( @@ -206,6 +207,7 @@ public void shouldNotOverwriteGraph() throws Exception { .build(), new Context(testUser), store); + fail(EXCEPTION_EXPECTED); } catch (final Exception e) { assertEquals(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, EXPECTED_GRAPH_ID), e.getCause().getMessage()); } @@ -235,10 +237,9 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { .build(), new Context(testUser), store); - fail("Exception not thrown"); + fail(EXCEPTION_EXPECTED); } catch (OperationException e) { - assertTrue(e.getMessage().contains("User is limited to only using parentPropertiesId from the graphLibrary," + - " but found storeProperties:{gaffer.store.class=uk.gov.gchq.gaffer.mapstore.MapStore")); + assertEquals(String.format(FederatedAddGraphHandler.USER_IS_LIMITED_TO_ONLY_USING_PARENT_PROPERTIES_ID_FROM_GRAPHLIBRARY_BUT_FOUND_STORE_PROPERTIES_S, "{gaffer.store.class=uk.gov.gchq.gaffer.mapstore.MapStore, gaffer.store.properties.class=uk.gov.gchq.gaffer.mapstore.MapStoreProperties}"), e.getMessage()); } federatedAddGraphHandler.doOperation( @@ -256,7 +257,6 @@ public void shouldAddGraphIDOnlyWithAuths() throws Exception { assertEquals(EXPECTED_GRAPH_ID, graphs.iterator().next().getGraphId()); } - /** * Replicating a bug condition when setting auths the * FederatedAddGraphHandler didn't set the adding user. @@ -285,7 +285,6 @@ public void shouldAddGraphWithAuthsAndAddingUser() throws Exception { new Context(testUser), store); - final CloseableIterable elements = new FederatedGetAllElementsHandler().doOperation( new GetAllElements(), new Context(testUser), diff --git a/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json b/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json index 9f7c92dce64..30dcd3d214c 100644 --- a/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json +++ b/store-implementation/federated-store/src/test/resources/schema/basicEdgeSchema.json @@ -1,5 +1,4 @@ { - "id": "basicEdgeSchema", "edges": { "BasicEdge": { "source": "vertex.string", diff --git a/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json b/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json index 0ce66a4877a..76580692b70 100644 --- a/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json +++ b/store-implementation/federated-store/src/test/resources/schema/basicEntitySchema.json @@ -1,5 +1,4 @@ { - "id": "basicEntitySchema", "entities": { "BasicEntity": { "vertex": "vertex.string", From 31ed96b2eee8437797bfb9f4553e4948fe00665b Mon Sep 17 00:00:00 2001 From: m55624 Date: Tue, 31 Oct 2017 19:55:37 +0000 Subject: [PATCH 56/72] gh-1297 - test fixes --- .../gchq/gaffer/cache/impl/HashMapCacheServiceTest.java | 1 - .../graph/handler/ExportToOtherGraphHandlerTest.java | 9 ++++++--- .../uk/gov/gchq/gaffer/store/StorePropertiesTest.java | 1 - .../java/uk/gov/gchq/gaffer/store/schema/SchemaTest.java | 6 ------ core/store/src/test/resources/store.properties | 1 - core/store/src/test/resources/store2.properties | 1 - .../FederatedStoreGraphVisibilityTest.java | 2 -- 7 files changed, 6 insertions(+), 15 deletions(-) diff --git a/core/cache/src/test/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheServiceTest.java b/core/cache/src/test/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheServiceTest.java index dfbf8e6337d..188a79a3e0f 100644 --- a/core/cache/src/test/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheServiceTest.java +++ b/core/cache/src/test/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheServiceTest.java @@ -46,7 +46,6 @@ public void after() { @Test public void shouldReturnInstanceOfHashMapCache() { - // when ICache cache = service.getCache(CACHE_NAME); diff --git a/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java b/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java index 8aa679e1f3e..27ce154e128 100644 --- a/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java +++ b/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java @@ -170,8 +170,10 @@ public void shouldCreateNewGraphWithStoreGraphLibrary() { @Test public void shouldCreateNewGraphWithStoresStoreProperties() { // Given - Schema schema1 = new Schema.Builder().build(); given(store.getProperties()).willReturn(storeProperties); + given(store.getGraphLibrary()).willReturn(null); + + Schema schema1 = new Schema.Builder().build(); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) @@ -190,9 +192,10 @@ public void shouldCreateNewGraphWithStoresStoreProperties() { @Test public void shouldCreateNewGraphWithStoresSchema() { // Given - final StoreProperties storeProperties1 = StoreProperties.loadStoreProperties(StreamUtil.storeProps(getClass())); - given(store.getSchema()).willReturn(schema); + given(store.getGraphLibrary()).willReturn(null); + + final StoreProperties storeProperties1 = StoreProperties.loadStoreProperties(StreamUtil.storeProps(getClass())); final ExportToOtherGraph export = new ExportToOtherGraph.Builder() .graphId(GRAPH_ID + 1) diff --git a/core/store/src/test/java/uk/gov/gchq/gaffer/store/StorePropertiesTest.java b/core/store/src/test/java/uk/gov/gchq/gaffer/store/StorePropertiesTest.java index b9742a7840a..d93628c2d40 100644 --- a/core/store/src/test/java/uk/gov/gchq/gaffer/store/StorePropertiesTest.java +++ b/core/store/src/test/java/uk/gov/gchq/gaffer/store/StorePropertiesTest.java @@ -41,7 +41,6 @@ public void shouldMergeProperties() { props1.merge(props2); // Then - assertEquals("1_2", props1.getId()); assertEquals("value1", props1.get("key1")); assertEquals("value2", props1.get("key2")); assertEquals("value2", props1.get("testKey")); diff --git a/core/store/src/test/java/uk/gov/gchq/gaffer/store/schema/SchemaTest.java b/core/store/src/test/java/uk/gov/gchq/gaffer/store/schema/SchemaTest.java index fcfc907f4ce..62ab9075ac8 100644 --- a/core/store/src/test/java/uk/gov/gchq/gaffer/store/schema/SchemaTest.java +++ b/core/store/src/test/java/uk/gov/gchq/gaffer/store/schema/SchemaTest.java @@ -501,7 +501,6 @@ public void shouldMergeDifferentSchemas() { final String type2 = "type2"; final Serialiser vertexSerialiser = mock(Serialiser.class); final Schema schema1 = new Schema.Builder() - .id("1") .edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder() .property(TestPropertyNames.PROP_1, type1) .build()) @@ -515,7 +514,6 @@ public void shouldMergeDifferentSchemas() { .build(); final Schema schema2 = new Schema.Builder() - .id("2") .entity(TestGroups.ENTITY_2, new SchemaEntityDefinition.Builder() .property(TestPropertyNames.COUNT, typeShared) .build()) @@ -535,7 +533,6 @@ public void shouldMergeDifferentSchemas() { .build(); // Then - assertEquals("1_2_2", mergedSchema.getId()); assertEquals(2, mergedSchema.getEdges().size()); assertEquals(1, mergedSchema.getEdge(TestGroups.EDGE).getPropertyMap().size()); assertEquals(type1, mergedSchema.getEdge(TestGroups.EDGE).getPropertyMap().get(TestPropertyNames.PROP_1)); @@ -563,7 +560,6 @@ public void shouldMergeDifferentSchemasOppositeWayAround() { final String type2 = "type2"; final Serialiser vertexSerialiser = mock(Serialiser.class); final Schema schema1 = new Schema.Builder() - .id("1") .edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder() .property(TestPropertyNames.PROP_1, type1) .build()) @@ -577,7 +573,6 @@ public void shouldMergeDifferentSchemasOppositeWayAround() { .build(); final Schema schema2 = new Schema.Builder() - .id("2") .entity(TestGroups.ENTITY_2, new SchemaEntityDefinition.Builder() .property(TestPropertyNames.COUNT, typeShared) .build()) @@ -597,7 +592,6 @@ public void shouldMergeDifferentSchemasOppositeWayAround() { .build(); // Then - assertEquals("2_1_1", mergedSchema.getId()); assertEquals(2, mergedSchema.getEdges().size()); assertEquals(1, mergedSchema.getEdge(TestGroups.EDGE).getPropertyMap().size()); assertEquals(type1, mergedSchema.getEdge(TestGroups.EDGE).getPropertyMap().get(TestPropertyNames.PROP_1)); diff --git a/core/store/src/test/resources/store.properties b/core/store/src/test/resources/store.properties index 16ad42054ec..4842003380a 100644 --- a/core/store/src/test/resources/store.properties +++ b/core/store/src/test/resources/store.properties @@ -14,6 +14,5 @@ # limitations under the License. # -gaffer.store.id=1 key1=value1 testKey=value1 diff --git a/core/store/src/test/resources/store2.properties b/core/store/src/test/resources/store2.properties index ba0da5c7071..777d953ad1d 100644 --- a/core/store/src/test/resources/store2.properties +++ b/core/store/src/test/resources/store2.properties @@ -14,6 +14,5 @@ # limitations under the License. # -gaffer.store.id=2 key2=value2 testKey=value2 diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreGraphVisibilityTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreGraphVisibilityTest.java index aca128141be..adfc883f96b 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreGraphVisibilityTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreGraphVisibilityTest.java @@ -76,7 +76,6 @@ public void setUp() throws Exception { public void shouldNotShowHiddenGraphIdWithIDs() throws Exception { final Schema aSchema = new Schema.Builder() - .id(TEST_SCHEMA_ID) // <- with ID .entity("e1", new SchemaEntityDefinition.Builder() .vertex("string") .build()) @@ -84,7 +83,6 @@ public void shouldNotShowHiddenGraphIdWithIDs() throws Exception { .build(); final AccumuloProperties accProp = new AccumuloProperties(); - accProp.setId(TEST_STORE_PROPS_ID); // <- with ID accProp.setStoreClass(SingleUseMockAccumuloStore.class.getName()); accProp.setStorePropertiesClass(AccumuloProperties.class); From 2da19c3ab6c905326fcd40cc3c2919e5ed780e64 Mon Sep 17 00:00:00 2001 From: m55624 Date: Tue, 31 Oct 2017 20:27:29 +0000 Subject: [PATCH 57/72] gh-1297 - FederatedStoreTest fixes --- .../federatedstore/FederatedStoreTest.java | 18 +++++++++--------- .../singleUseMockAccStore.properties | 1 - .../singleUseMockMapStore.properties | 1 - .../singleUseMockMapStoreAlt.properties | 1 - 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index 5b49428cb3d..6f81cf12bb8 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -110,11 +110,11 @@ public void setUp() throws Exception { clearLibrary(); library = new HashMapGraphLibrary(); - library.addProperties(getPropertiesFromPath(PATH_ACC_STORE_PROPERTIES)); - library.addProperties(getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES)); - library.addProperties(getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES_ALT)); - library.addSchema(getSchemaFromPath(PATH_BASIC_EDGE_SCHEMA_JSON)); - library.addSchema(getSchemaFromPath(PATH_BASIC_ENTITY_SCHEMA_JSON)); + library.addProperties(ID_PROPS_ACC, getPropertiesFromPath(PATH_ACC_STORE_PROPERTIES)); + library.addProperties(ID_PROPS_MAP, getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES)); + library.addProperties(ID_PROPS_MAP_ALT, getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES_ALT)); + library.addSchema(ID_SCHEMA_EDGE, getSchemaFromPath(PATH_BASIC_EDGE_SCHEMA_JSON)); + library.addSchema(ID_SCHEMA_ENTITY, getSchemaFromPath(PATH_BASIC_ENTITY_SCHEMA_JSON)); store = new FederatedStore(); store.setGraphLibrary(library); @@ -220,7 +220,7 @@ public void shouldNotAllowOverwritingOfGraphWithinFederatedScope() throws Except addGraphWithIds(MAP_ID_1, ID_PROPS_MAP, ID_SCHEMA_EDGE); fail(EXCEPTION_NOT_THROWN); } catch (final Exception e) { - assertContains(e.getCause(), "GraphId " + MAP_ID_1 + " already exists"); + assertContains(e.getCause(), "Graph: " + MAP_ID_1 + " already exists"); } // When / Then @@ -228,7 +228,7 @@ public void shouldNotAllowOverwritingOfGraphWithinFederatedScope() throws Except addGraphWithIds(MAP_ID_1, ID_PROPS_MAP_ALT, ID_SCHEMA_ENTITY); fail(EXCEPTION_NOT_THROWN); } catch (final Exception e) { - assertContains(e.getCause(), "GraphId " + MAP_ID_1 + " already exists"); + assertContains(e.getCause(), "Graph: " + MAP_ID_1 + " already exists"); } } @@ -584,7 +584,7 @@ public void shouldNotAllowOverridingOfKnownGraphInLibrary() throws Exception { .build(), userContext); fail(EXCEPTION_NOT_THROWN); } catch (final Exception e) { - assertContains(e.getCause(), "GraphId " + MAP_ID_1 + " already exists so you cannot use different store properties"); + assertContains(e.getCause(), "Graph: " + MAP_ID_1 + " already exists so you cannot use a different StoreProperties"); } // When / Then @@ -596,7 +596,7 @@ public void shouldNotAllowOverridingOfKnownGraphInLibrary() throws Exception { .build(), userContext); fail(EXCEPTION_NOT_THROWN); } catch (final Exception e) { - assertContains(e.getCause(), "GraphId " + MAP_ID_1 + " already exists so you cannot use a different schema"); + assertContains(e.getCause(), "Graph: " + MAP_ID_1 + " already exists so you cannot use a different Schema"); } } diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties index 063ca685c3f..364120debad 100644 --- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties +++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockAccStore.properties @@ -13,7 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -gaffer.store.id=mockAccProps gaffer.store.class=uk.gov.gchq.gaffer.accumulostore.SingleUseMockAccumuloStore gaffer.store.properties.class=uk.gov.gchq.gaffer.accumulostore.AccumuloProperties accumulo.instance=mockInstanceID1234 diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties index 3b4642de584..ef5df3c6aa5 100644 --- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties +++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStore.properties @@ -13,6 +13,5 @@ # See the License for the specific language governing permissions and # limitations under the License. # -gaffer.store.id=mockMapProps gaffer.store.class=uk.gov.gchq.gaffer.mapstore.SingleUseMapStore gaffer.store.properties.class=uk.gov.gchq.gaffer.mapstore.MapStoreProperties diff --git a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties index fa696353c6b..921dc1f8a7b 100644 --- a/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties +++ b/store-implementation/federated-store/src/test/resources/properties/singleUseMockMapStoreAlt.properties @@ -13,7 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -gaffer.store.id=mockMapPropsAlt gaffer.store.class=uk.gov.gchq.gaffer.mapstore.SingleUseMapStore gaffer.store.properties.class=uk.gov.gchq.gaffer.mapstore.MapStoreProperties unusualKey=value From 8548c939b25205bb4e5ba18e3fac3183125041d3 Mon Sep 17 00:00:00 2001 From: m55624 Date: Wed, 1 Nov 2017 09:57:46 +0000 Subject: [PATCH 58/72] 1297 - tidying up --- .../gaffer/operation/export/graph/handler/GraphDelegate.java | 1 + .../uk/gov/gchq/gaffer/cache/impl/HazelcastCacheTest.java | 4 +++- .../uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java | 4 ++-- .../uk/gov/gchq/gaffer/federatedstore/FederatedStore.java | 2 -- .../gchq/gaffer/federatedstore/FederatedStoreConstants.java | 1 + .../gchq/gaffer/federatedstore/FederatedStoreProperties.java | 3 +-- 6 files changed, 8 insertions(+), 7 deletions(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java index 1b227603f79..de6aeb1f8e3 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java @@ -125,6 +125,7 @@ private static Schema resolveSchema(final Store store, final Schema schema, fina // delete the old schema id as we are about to modify the schema rtn = new Schema.Builder() .merge(rtn) + .id(null) .merge(schema) .build(); } diff --git a/library/cache-library/hazelcast-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/HazelcastCacheTest.java b/library/cache-library/hazelcast-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/HazelcastCacheTest.java index eaa71b931cd..0e6c914b2f8 100644 --- a/library/cache-library/hazelcast-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/HazelcastCacheTest.java +++ b/library/cache-library/hazelcast-cache-service/src/test/java/uk/gov/gchq/gaffer/cache/impl/HazelcastCacheTest.java @@ -44,8 +44,10 @@ public void shouldThrowAnExceptionIfEntryAlreadyExistsWhenUsingPutSafe() { try { cache.putSafe("test", 1); fail(); - } catch (final OverwritingException | CacheOperationException e) { + } catch (final OverwritingException e) { assertEquals("Cache entry already exists for key: test", e.getMessage()); + } catch (final CacheOperationException e) { + fail("Should have thrown an OverwritingException"); } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java index b06c641f354..e02429da291 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java @@ -26,7 +26,7 @@ import java.util.HashSet; import java.util.Set; -import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties.IS_PUBLIC_DEFAULT; +import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.DEFAULT_VALUE_IS_PUBLIC; /** * Conditions required for a {@link User} to have access to a graph within the @@ -57,7 +57,7 @@ * @see #isValidToExecute(User) */ public class FederatedAccess { - private boolean isPublic = Boolean.valueOf(IS_PUBLIC_DEFAULT); + private boolean isPublic = Boolean.valueOf(DEFAULT_VALUE_IS_PUBLIC); private Set graphAuths = new HashSet<>(); private String addingUserId; diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index d9b58bed7e4..66ed6614264 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -359,6 +359,4 @@ private void _add(final Graph newGraph, final FederatedAccess access) { getGraphLibrary().add(newGraph.getGraphId(), newGraph.getSchema(), newGraph.getStoreProperties()); } } - - } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java index d351e49fdc8..fb707709509 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java @@ -24,6 +24,7 @@ public final class FederatedStoreConstants { public static final String KEY_OPERATION_OPTIONS_GRAPH_IDS = PREFIX_GAFFER_FEDERATED_STORE + ".operation.graphIds"; public static final String KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE = PREFIX_GAFFER_FEDERATED_STORE + ".operation.skipFailedFederatedStoreExecute"; public static final String DEFAULT_VALUE_KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE = String.valueOf(false); + public static final String DEFAULT_VALUE_IS_PUBLIC = String.valueOf(false); private FederatedStoreConstants() { // private constructor to prevent users instantiating this class as it diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java index 4fe0d715e0b..38bd7433d7a 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreProperties.java @@ -42,9 +42,8 @@ public class FederatedStoreProperties extends StoreProperties { /** * This is used.... - * e.g gaffer.federatedstore.graph1.isPublic=false + * eg.gaffer.federatedstore.cache.service.class="uk.gov.gchq.gaffer.cache.impl.HashMapCacheService" */ - public static final String IS_PUBLIC_DEFAULT = String.valueOf(false); public static final String CACHE_SERVICE_CLASS = CacheProperties.CACHE_SERVICE_CLASS; public static final String CACHE_SERVICE_CLASS_DEFAULT = null; From a4102f59560c641339e3eb962e8f1b0e79ec0e66 Mon Sep 17 00:00:00 2001 From: p013570 Date: Wed, 1 Nov 2017 14:07:16 +0000 Subject: [PATCH 59/72] gh-1297 - transient graph field in GraphSerialisable Simplified GraphDelegate slightly. Added null checks to builders --- .../elementdefinition/ElementDefinitions.java | 41 +++-- .../data/elementdefinition/view/View.java | 110 ++++++----- .../view/ViewElementDefinition.java | 112 ++++++----- .../java/uk/gov/gchq/gaffer/graph/Graph.java | 171 +++++++++++------ .../uk/gov/gchq/gaffer/graph/GraphConfig.java | 59 +++--- .../gchq/gaffer/graph/GraphSerialisable.java | 69 +++++-- .../export/graph/handler/GraphDelegate.java | 174 ++++++++---------- .../gaffer/graph/GraphSerialisableTest.java | 6 +- .../gov/gchq/gaffer/store/schema/Schema.java | 142 +++++++------- .../store/schema/SchemaElementDefinition.java | 148 ++++++++------- .../federatedstore/FederatedGraphStorage.java | 4 +- .../federatedstore/FederatedStoreCache.java | 2 +- ...pStorePropertiesGraphSerialisableTest.java | 6 +- 13 files changed, 590 insertions(+), 454 deletions(-) diff --git a/core/data/src/main/java/uk/gov/gchq/gaffer/data/elementdefinition/ElementDefinitions.java b/core/data/src/main/java/uk/gov/gchq/gaffer/data/elementdefinition/ElementDefinitions.java index 989f96bb281..bcf85cc6f93 100644 --- a/core/data/src/main/java/uk/gov/gchq/gaffer/data/elementdefinition/ElementDefinitions.java +++ b/core/data/src/main/java/uk/gov/gchq/gaffer/data/elementdefinition/ElementDefinitions.java @@ -226,7 +226,9 @@ public CHILD_CLASS edge(final String group, final EDGE_DEF edgeDef) { @JsonSetter("edges") public CHILD_CLASS edges(final Map edges) { elementDefs.getEdges().clear(); - elementDefs.getEdges().putAll(edges); + if (null != edges) { + elementDefs.getEdges().putAll(edges); + } return self(); } @@ -238,6 +240,9 @@ public CHILD_CLASS edges(final Map edges) { * @return this Builder */ public CHILD_CLASS entity(final String group, final ENTITY_DEF entityDef) { + if (null == entityDef) { + throw new IllegalArgumentException("Entity definition is required"); + } elementDefs.getEntities().put(group, entityDef); return self(); } @@ -245,7 +250,9 @@ public CHILD_CLASS entity(final String group, final ENTITY_DEF entityDef) { @JsonSetter("entities") public CHILD_CLASS entities(final Map entities) { elementDefs.getEntities().clear(); - elementDefs.getEntities().putAll(entities); + if (null != entities) { + elementDefs.getEntities().putAll(entities); + } return self(); } @@ -271,24 +278,26 @@ public CHILD_CLASS json(final Class clazz, final byte[]. } public CHILD_CLASS json(final Class clazz, final Object[] jsonItems) throws SchemaException { - for (final Object jsonItem : jsonItems) { - try { - if (jsonItem instanceof InputStream) { - merge(JSONSerialiser.deserialise((InputStream) jsonItem, clazz)); - } else if (jsonItem instanceof Path) { - final Path path = (Path) jsonItem; - if (Files.isDirectory(path)) { - for (final Path filePath : Files.newDirectoryStream(path)) { - merge(JSONSerialiser.deserialise(Files.readAllBytes(filePath), clazz)); + if (null != jsonItems) { + for (final Object jsonItem : jsonItems) { + try { + if (jsonItem instanceof InputStream) { + merge(JSONSerialiser.deserialise((InputStream) jsonItem, clazz)); + } else if (jsonItem instanceof Path) { + final Path path = (Path) jsonItem; + if (Files.isDirectory(path)) { + for (final Path filePath : Files.newDirectoryStream(path)) { + merge(JSONSerialiser.deserialise(Files.readAllBytes(filePath), clazz)); + } + } else { + merge(JSONSerialiser.deserialise(Files.readAllBytes(path), clazz)); } } else { - merge(JSONSerialiser.deserialise(Files.readAllBytes(path), clazz)); + merge(JSONSerialiser.deserialise((byte[]) jsonItem, clazz)); } - } else { - merge(JSONSerialiser.deserialise((byte[]) jsonItem, clazz)); + } catch (final IOException e) { + throw new SchemaException("Failed to load element definitions from bytes", e); } - } catch (final IOException e) { - throw new SchemaException("Failed to load element definitions from bytes", e); } } diff --git a/core/data/src/main/java/uk/gov/gchq/gaffer/data/elementdefinition/view/View.java b/core/data/src/main/java/uk/gov/gchq/gaffer/data/elementdefinition/view/View.java index 3655254b369..f8832336e7a 100644 --- a/core/data/src/main/java/uk/gov/gchq/gaffer/data/elementdefinition/view/View.java +++ b/core/data/src/main/java/uk/gov/gchq/gaffer/data/elementdefinition/view/View.java @@ -301,10 +301,17 @@ public CHILD_CLASS entity(final String group) { return entity(group, new ViewElementDefinition()); } + @Override + public CHILD_CLASS entity(final String group, final ViewElementDefinition entityDef) { + return super.entity(group, null != entityDef ? entityDef : new ViewElementDefinition()); + } + @JsonIgnore public CHILD_CLASS entities(final Collection groups) { - for (final String group : groups) { - entity(group); + if (null != groups) { + for (final String group : groups) { + entity(group); + } } return self(); @@ -314,16 +321,23 @@ public CHILD_CLASS edge(final String group) { return edge(group, new ViewElementDefinition()); } + @Override + public CHILD_CLASS edge(final String group, final ViewElementDefinition edgeDef) { + return super.edge(group, null != edgeDef ? edgeDef : new ViewElementDefinition()); + } + public CHILD_CLASS edges(final Collection groups) { - for (final String group : groups) { - edge(group); + if (null != groups) { + for (final String group : groups) { + edge(group); + } } return self(); } public CHILD_CLASS globalElements(final GlobalViewElementDefinition... globalElements) { - if (globalElements.length > 0) { + if (null != globalElements && globalElements.length > 0) { if (null == getThisView().globalElements) { getThisView().globalElements = new ArrayList<>(); } @@ -333,7 +347,7 @@ public CHILD_CLASS globalElements(final GlobalViewElementDefinition... globalEle } public CHILD_CLASS globalEntities(final GlobalViewElementDefinition... globalEntities) { - if (globalEntities.length > 0) { + if (null != globalEntities && globalEntities.length > 0) { if (null == getThisView().globalEntities) { getThisView().globalEntities = new ArrayList<>(); } @@ -343,7 +357,7 @@ public CHILD_CLASS globalEntities(final GlobalViewElementDefinition... globalEnt } public CHILD_CLASS globalEdges(final GlobalViewElementDefinition... globalEdges) { - if (globalEdges.length > 0) { + if (null != globalEdges && globalEdges.length > 0) { if (null == getThisView().globalEdges) { getThisView().globalEdges = new ArrayList<>(); } @@ -370,57 +384,59 @@ public CHILD_CLASS json(final byte[]... jsonBytes) throws SchemaException { @Override @JsonIgnore public CHILD_CLASS merge(final View view) { - if (getThisView().getEntities().isEmpty()) { - getThisView().getEntities().putAll(view.getEntities()); - } else { - for (final Map.Entry entry : view.getEntities().entrySet()) { - if (!getThisView().getEntities().containsKey(entry.getKey())) { - entity(entry.getKey(), entry.getValue()); - } else { - final ViewElementDefinition mergedElementDef = new ViewElementDefinition.Builder() - .merge(getThisView().getEntities().get(entry.getKey())) - .merge(entry.getValue()) - .build(); - getThisView().getEntities().put(entry.getKey(), mergedElementDef); + if (null != view) { + if (getThisView().getEntities().isEmpty()) { + getThisView().getEntities().putAll(view.getEntities()); + } else { + for (final Map.Entry entry : view.getEntities().entrySet()) { + if (!getThisView().getEntities().containsKey(entry.getKey())) { + entity(entry.getKey(), entry.getValue()); + } else { + final ViewElementDefinition mergedElementDef = new ViewElementDefinition.Builder() + .merge(getThisView().getEntities().get(entry.getKey())) + .merge(entry.getValue()) + .build(); + getThisView().getEntities().put(entry.getKey(), mergedElementDef); + } } } - } - if (getThisView().getEdges().isEmpty()) { - getThisView().getEdges().putAll(view.getEdges()); - } else { - for (final Map.Entry entry : view.getEdges().entrySet()) { - if (!getThisView().getEdges().containsKey(entry.getKey())) { - edge(entry.getKey(), entry.getValue()); - } else { - final ViewElementDefinition mergedElementDef = new ViewElementDefinition.Builder() - .merge(getThisView().getEdges().get(entry.getKey())) - .merge(entry.getValue()) - .build(); - getThisView().getEdges().put(entry.getKey(), mergedElementDef); + if (getThisView().getEdges().isEmpty()) { + getThisView().getEdges().putAll(view.getEdges()); + } else { + for (final Map.Entry entry : view.getEdges().entrySet()) { + if (!getThisView().getEdges().containsKey(entry.getKey())) { + edge(entry.getKey(), entry.getValue()); + } else { + final ViewElementDefinition mergedElementDef = new ViewElementDefinition.Builder() + .merge(getThisView().getEdges().get(entry.getKey())) + .merge(entry.getValue()) + .build(); + getThisView().getEdges().put(entry.getKey(), mergedElementDef); + } } } - } - if (null != view.globalElements) { - if (null == getThisView().globalElements) { - getThisView().globalElements = new ArrayList<>(); + if (null != view.globalElements) { + if (null == getThisView().globalElements) { + getThisView().globalElements = new ArrayList<>(); + } + getThisView().globalElements.addAll(view.globalElements); } - getThisView().globalElements.addAll(view.globalElements); - } - if (null != view.globalEntities) { - if (null == getThisView().globalEntities) { - getThisView().globalEntities = new ArrayList<>(); + if (null != view.globalEntities) { + if (null == getThisView().globalEntities) { + getThisView().globalEntities = new ArrayList<>(); + } + getThisView().globalEntities.addAll(view.globalEntities); } - getThisView().globalEntities.addAll(view.globalEntities); - } - if (null != view.globalEdges) { - if (null == getThisView().globalEdges) { - getThisView().globalEdges = new ArrayList<>(); + if (null != view.globalEdges) { + if (null == getThisView().globalEdges) { + getThisView().globalEdges = new ArrayList<>(); + } + getThisView().globalEdges.addAll(view.globalEdges); } - getThisView().globalEdges.addAll(view.globalEdges); } return self(); diff --git a/core/data/src/main/java/uk/gov/gchq/gaffer/data/elementdefinition/view/ViewElementDefinition.java b/core/data/src/main/java/uk/gov/gchq/gaffer/data/elementdefinition/view/ViewElementDefinition.java index 0e8ea33341d..4a667916356 100644 --- a/core/data/src/main/java/uk/gov/gchq/gaffer/data/elementdefinition/view/ViewElementDefinition.java +++ b/core/data/src/main/java/uk/gov/gchq/gaffer/data/elementdefinition/view/ViewElementDefinition.java @@ -348,7 +348,11 @@ public CHILD_CLASS transientProperty(final String propertyName, final Class c } public CHILD_CLASS transientProperties(final Map> transientProperties) { - elDef.transientProperties = new LinkedHashMap<>(transientProperties); + if (null == transientProperties) { + elDef.transientProperties = new LinkedHashMap<>(); + } else { + elDef.transientProperties = new LinkedHashMap<>(transientProperties); + } return self(); } @@ -364,7 +368,9 @@ public CHILD_CLASS preAggregationFilter(final ElementFilter preAggregationFilter public CHILD_CLASS preAggregationFilterFunctions(final List> filterFunctions) { getElementDef().preAggregationFilter = new ElementFilter(); - getElementDef().preAggregationFilter.getComponents().addAll(filterFunctions); + if (null != filterFunctions) { + getElementDef().preAggregationFilter.getComponents().addAll(filterFunctions); + } return self(); } @@ -385,7 +391,9 @@ public CHILD_CLASS postAggregationFilter(final ElementFilter postAggregationFilt public CHILD_CLASS postAggregationFilterFunctions(final List> filterFunctions) { getElementDef().postAggregationFilter = new ElementFilter(); - getElementDef().postAggregationFilter.getComponents().addAll(filterFunctions); + if (null != filterFunctions) { + getElementDef().postAggregationFilter.getComponents().addAll(filterFunctions); + } return self(); } @@ -401,7 +409,9 @@ public CHILD_CLASS postTransformFilter(final ElementFilter postTransformFilter) public CHILD_CLASS postTransformFilterFunctions(final List> filterFunctions) { getElementDef().postTransformFilter = new ElementFilter(); - getElementDef().postTransformFilter.getComponents().addAll(filterFunctions); + if (null != filterFunctions) { + getElementDef().postTransformFilter.getComponents().addAll(filterFunctions); + } return self(); } @@ -412,7 +422,9 @@ public CHILD_CLASS transformer(final ElementTransformer transformer) { public CHILD_CLASS transformFunctions(final List> transformFunctions) { getElementDef().transformer = new ElementTransformer(); - getElementDef().transformer.getComponents().addAll(transformFunctions); + if (null != transformFunctions) { + getElementDef().transformer.getComponents().addAll(transformFunctions); + } return self(); } @@ -435,63 +447,67 @@ public CHILD_CLASS json(final byte[] jsonBytes) throws SchemaException { @JsonIgnore protected CHILD_CLASS json(final byte[] jsonBytes, final Class clazz) throws SchemaException { - try { - merge(JSONSerialiser.deserialise(jsonBytes, clazz)); - } catch (final SerialisationException e) { - throw new SchemaException("Unable to deserialise json", e); + if (null != jsonBytes) { + try { + merge(JSONSerialiser.deserialise(jsonBytes, clazz)); + } catch (final SerialisationException e) { + throw new SchemaException("Unable to deserialise json", e); + } } return self(); } public CHILD_CLASS merge(final ViewElementDefinition elementDef) { - for (final Entry> entry : elementDef.getTransientPropertyMap().entrySet()) { - final String newProp = entry.getKey(); - final Class newPropClass = entry.getValue(); - if (!getElementDef().transientProperties.containsKey(newProp)) { - getElementDef().transientProperties.put(newProp, newPropClass); - } else { - final Class clazz = getElementDef().transientProperties.get(newProp); - if (!clazz.equals(newPropClass)) { - throw new SchemaException("Unable to merge schemas. Conflict of transient property classes for " + newProp - + ". Classes are: " + clazz.getName() + " and " + newPropClass.getName()); + if (null != elementDef) { + for (final Entry> entry : elementDef.getTransientPropertyMap().entrySet()) { + final String newProp = entry.getKey(); + final Class newPropClass = entry.getValue(); + if (!getElementDef().transientProperties.containsKey(newProp)) { + getElementDef().transientProperties.put(newProp, newPropClass); + } else { + final Class clazz = getElementDef().transientProperties.get(newProp); + if (!clazz.equals(newPropClass)) { + throw new SchemaException("Unable to merge schemas. Conflict of transient property classes for " + newProp + + ". Classes are: " + clazz.getName() + " and " + newPropClass.getName()); + } } } - } - if (null == getElementDef().preAggregationFilter) { - getElementDef().preAggregationFilter = elementDef.preAggregationFilter; - } else if (null != elementDef.preAggregationFilter) { - getElementDef().preAggregationFilter.getComponents().addAll(elementDef.preAggregationFilter.getComponents()); - } + if (null == getElementDef().preAggregationFilter) { + getElementDef().preAggregationFilter = elementDef.preAggregationFilter; + } else if (null != elementDef.preAggregationFilter) { + getElementDef().preAggregationFilter.getComponents().addAll(elementDef.preAggregationFilter.getComponents()); + } - if (null == getElementDef().postAggregationFilter) { - getElementDef().postAggregationFilter = elementDef.postAggregationFilter; - } else if (null != elementDef.postAggregationFilter) { - getElementDef().postAggregationFilter.getComponents().addAll(elementDef.postAggregationFilter.getComponents()); - } + if (null == getElementDef().postAggregationFilter) { + getElementDef().postAggregationFilter = elementDef.postAggregationFilter; + } else if (null != elementDef.postAggregationFilter) { + getElementDef().postAggregationFilter.getComponents().addAll(elementDef.postAggregationFilter.getComponents()); + } - if (null == getElementDef().postTransformFilter) { - getElementDef().postTransformFilter = elementDef.postTransformFilter; - } else if (null != elementDef.postTransformFilter) { - getElementDef().postTransformFilter.getComponents().addAll(elementDef.postTransformFilter.getComponents()); - } + if (null == getElementDef().postTransformFilter) { + getElementDef().postTransformFilter = elementDef.postTransformFilter; + } else if (null != elementDef.postTransformFilter) { + getElementDef().postTransformFilter.getComponents().addAll(elementDef.postTransformFilter.getComponents()); + } - if (null == getElementDef().transformer) { - getElementDef().transformer = elementDef.transformer; - } else if (null != elementDef.transformer) { - getElementDef().transformer.getComponents().addAll(elementDef.transformer.getComponents()); - } + if (null == getElementDef().transformer) { + getElementDef().transformer = elementDef.transformer; + } else if (null != elementDef.transformer) { + getElementDef().transformer.getComponents().addAll(elementDef.transformer.getComponents()); + } - if (null != elementDef.getGroupBy()) { - getElementDef().groupBy = new LinkedHashSet<>(elementDef.getGroupBy()); - } + if (null != elementDef.getGroupBy()) { + getElementDef().groupBy = new LinkedHashSet<>(elementDef.getGroupBy()); + } - if (null != elementDef.getProperties()) { - properties(elementDef.getProperties()); - } + if (null != elementDef.getProperties()) { + properties(elementDef.getProperties()); + } - if (null != elementDef.getExcludeProperties() && !elementDef.getExcludeProperties().isEmpty()) { - excludeProperties(elementDef.getExcludeProperties()); + if (null != elementDef.getExcludeProperties() && !elementDef.getExcludeProperties().isEmpty()) { + excludeProperties(elementDef.getExcludeProperties()); + } } return self(); diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java index ef5254a1a32..8edfb2d4075 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/Graph.java @@ -16,6 +16,7 @@ package uk.gov.gchq.gaffer.graph; +import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -374,7 +375,7 @@ public static class Builder { private Store store; private StoreProperties properties; private Schema schema; - private String[] parentSchemaIds; + private List parentSchemaIds; private String parentStorePropertiesId; private boolean addToLibrary = true; @@ -496,7 +497,7 @@ public Builder parentStorePropertiesId(final String parentStorePropertiesId) { } public Builder storeProperties(final Properties properties) { - return storeProperties(StoreProperties.loadStoreProperties(properties)); + return storeProperties(null != properties ? StoreProperties.loadStoreProperties(properties) : null); } public Builder storeProperties(final StoreProperties properties) { @@ -508,64 +509,108 @@ public Builder storeProperties(final StoreProperties properties) { } public Builder storeProperties(final String propertiesPath) { - return storeProperties(StoreProperties.loadStoreProperties(propertiesPath)); + return storeProperties(null != propertiesPath ? StoreProperties.loadStoreProperties(propertiesPath) : null); } public Builder storeProperties(final Path propertiesPath) { - return storeProperties(StoreProperties.loadStoreProperties(propertiesPath)); + if (null == propertiesPath) { + properties = null; + } else { + storeProperties(StoreProperties.loadStoreProperties(propertiesPath)); + } + return this; } public Builder storeProperties(final InputStream propertiesStream) { - return storeProperties(StoreProperties.loadStoreProperties(propertiesStream)); + if (null == propertiesStream) { + properties = null; + } else { + storeProperties(StoreProperties.loadStoreProperties(propertiesStream)); + } + return this; } public Builder storeProperties(final URI propertiesURI) { - try { - storeProperties(StreamUtil.openStream(propertiesURI)); - } catch (final IOException e) { - throw new SchemaException("Unable to read storeProperties from URI: " + propertiesURI, e); + if (null != propertiesURI) { + try { + storeProperties(StreamUtil.openStream(propertiesURI)); + } catch (final IOException e) { + throw new SchemaException("Unable to read storeProperties from URI: " + propertiesURI, e); + } } return this; } public Builder addStoreProperties(final Properties properties) { - return addStoreProperties(StoreProperties.loadStoreProperties(properties)); + if (null != properties) { + addStoreProperties(StoreProperties.loadStoreProperties(properties)); + } + return this; } public Builder addStoreProperties(final StoreProperties updateProperties) { - if (null == this.properties) { - storeProperties(updateProperties); - } else { - this.properties.merge(updateProperties); + if (null != updateProperties) { + if (null == this.properties) { + storeProperties(updateProperties); + } else { + this.properties.merge(updateProperties); + } } return this; } public Builder addStoreProperties(final String updatePropertiesPath) { - return addStoreProperties(StoreProperties.loadStoreProperties(updatePropertiesPath)); + if (null != updatePropertiesPath) { + addStoreProperties(StoreProperties.loadStoreProperties(updatePropertiesPath)); + } + return this; } public Builder addStoreProperties(final Path updatePropertiesPath) { - return addStoreProperties(StoreProperties.loadStoreProperties(updatePropertiesPath)); + if (null != updatePropertiesPath) { + addStoreProperties(StoreProperties.loadStoreProperties(updatePropertiesPath)); + } + return this; } public Builder addStoreProperties(final InputStream updatePropertiesStream) { - return addStoreProperties(StoreProperties.loadStoreProperties(updatePropertiesStream)); + if (null != updatePropertiesStream) { + addStoreProperties(StoreProperties.loadStoreProperties(updatePropertiesStream)); + } + return this; } public Builder addStoreProperties(final URI updatePropertiesURI) { - try { - addStoreProperties(StreamUtil.openStream(updatePropertiesURI)); - } catch (final IOException e) { - throw new SchemaException("Unable to read storeProperties from URI: " + updatePropertiesURI, e); + if (null != updatePropertiesURI) { + try { + addStoreProperties(StreamUtil.openStream(updatePropertiesURI)); + } catch (final IOException e) { + throw new SchemaException("Unable to read storeProperties from URI: " + updatePropertiesURI, e); + } } + return this; + } + public Builder addParentSchemaIds(final List parentSchemaIds) { + if (null != parentSchemaIds) { + if (null == this.parentSchemaIds) { + this.parentSchemaIds = new ArrayList<>(parentSchemaIds); + } else { + this.parentSchemaIds.addAll(parentSchemaIds); + } + } return this; } public Builder addParentSchemaIds(final String... parentSchemaIds) { - this.parentSchemaIds = parentSchemaIds; + if (null != parentSchemaIds) { + if (null == this.parentSchemaIds) { + this.parentSchemaIds = Lists.newArrayList(parentSchemaIds); + } else { + Collections.addAll(this.parentSchemaIds, parentSchemaIds); + } + } return this; } @@ -575,7 +620,6 @@ public Builder addSchemas(final Schema... schemaModules) { addSchema(schemaModule); } } - return this; } @@ -591,7 +635,6 @@ public Builder addSchemas(final InputStream... schemaStreams) { } } } - return this; } @@ -601,7 +644,6 @@ public Builder addSchemas(final Path... schemaPaths) { addSchema(schemaPath); } } - return this; } @@ -611,71 +653,79 @@ public Builder addSchemas(final byte[]... schemaBytesArray) { addSchema(schemaBytes); } } - return this; } public Builder addSchema(final Schema schemaModule) { - if (null != schema) { - schema = new Schema.Builder() - .merge(schema) - .merge(schemaModule) - .build(); - } else { - schema = schemaModule; + if (null != schemaModule) { + if (null != schema) { + schema = new Schema.Builder() + .merge(schema) + .merge(schemaModule) + .build(); + } else { + schema = schemaModule; + } } - return this; } public Builder addSchema(final InputStream schemaStream) { - try { - return addSchema(sun.misc.IOUtils.readFully(schemaStream, schemaStream.available(), true)); - } catch (final IOException e) { - throw new SchemaException("Unable to read schema from input stream", e); - } finally { - CloseableUtil.close(schemaStream); + if (null != schemaStream) { + try { + addSchema(sun.misc.IOUtils.readFully(schemaStream, schemaStream.available(), true)); + } catch (final IOException e) { + throw new SchemaException("Unable to read schema from input stream", e); + } finally { + CloseableUtil.close(schemaStream); + } } + return this; } public Builder addSchema(final URI schemaURI) { - try { - addSchema(StreamUtil.openStream(schemaURI)); - } catch (final IOException e) { - throw new SchemaException(UNABLE_TO_READ_SCHEMA_FROM_URI, e); + if (null != schemaURI) { + try { + addSchema(StreamUtil.openStream(schemaURI)); + } catch (final IOException e) { + throw new SchemaException(UNABLE_TO_READ_SCHEMA_FROM_URI, e); + } } - return this; } public Builder addSchemas(final URI... schemaURI) { - try { - addSchemas(StreamUtil.openStreams(schemaURI)); - } catch (final IOException e) { - throw new SchemaException(UNABLE_TO_READ_SCHEMA_FROM_URI, e); + if (null != schemaURI) { + try { + addSchemas(StreamUtil.openStreams(schemaURI)); + } catch (final IOException e) { + throw new SchemaException(UNABLE_TO_READ_SCHEMA_FROM_URI, e); + } } - return this; } public Builder addSchema(final Path schemaPath) { - try { - if (Files.isDirectory(schemaPath)) { - for (final Path path : Files.newDirectoryStream(schemaPath)) { - addSchema(path); + if (null != schemaPath) { + try { + if (Files.isDirectory(schemaPath)) { + for (final Path path : Files.newDirectoryStream(schemaPath)) { + addSchema(path); + } + } else { + addSchema(Files.readAllBytes(schemaPath)); } - } else { - addSchema(Files.readAllBytes(schemaPath)); + } catch (final IOException e) { + throw new SchemaException("Unable to read schema from path: " + schemaPath, e); } - } catch (final IOException e) { - throw new SchemaException("Unable to read schema from path: " + schemaPath, e); } - return this; } public Builder addSchema(final byte[] schemaBytes) { - schemaBytesList.add(schemaBytes); + if (null != schemaBytes) { + schemaBytesList.add(schemaBytes); + } return this; } @@ -858,7 +908,6 @@ private void updateStoreProperties(final GraphConfig config) { StoreProperties mergedStoreProperties = null; if (null != parentStorePropertiesId) { mergedStoreProperties = config.getLibrary().getProperties(parentStorePropertiesId); - mergedStoreProperties.setId(parentStorePropertiesId); } if (null != properties) { diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphConfig.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphConfig.java index afcee80ff38..07c8c728587 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphConfig.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphConfig.java @@ -122,7 +122,7 @@ public static class Builder { public Builder json(final Path path) { try { - return json(Files.readAllBytes(path)); + return json(null != path ? Files.readAllBytes(path) : null); } catch (final IOException e) { throw new IllegalArgumentException("Unable to read graph config from path: " + path, e); } @@ -130,7 +130,7 @@ public Builder json(final Path path) { public Builder json(final URI uri) { try { - json(StreamUtil.openStream(uri)); + json(null != uri ? StreamUtil.openStream(uri) : null); } catch (final IOException e) { throw new IllegalArgumentException("Unable to read graph config from uri: " + uri, e); } @@ -140,7 +140,7 @@ public Builder json(final URI uri) { public Builder json(final InputStream stream) { try { - json(sun.misc.IOUtils.readFully(stream, stream.available(), true)); + json(null != stream ? sun.misc.IOUtils.readFully(stream, stream.available(), true) : null); } catch (final IOException e) { throw new IllegalArgumentException("Unable to read graph config from input stream", e); } @@ -149,27 +149,32 @@ public Builder json(final InputStream stream) { } public Builder json(final byte[] bytes) { - try { - return merge(JSONSerialiser.deserialise(bytes, GraphConfig.class)); - } catch (final IOException e) { - throw new IllegalArgumentException("Unable to deserialise graph config", e); + if (null != bytes) { + try { + merge(JSONSerialiser.deserialise(bytes, GraphConfig.class)); + } catch (final IOException e) { + throw new IllegalArgumentException("Unable to deserialise graph config", e); + } } + return this; } public Builder merge(final GraphConfig config) { - if (null != config.getGraphId()) { - this.config.setGraphId(config.getGraphId()); - } - if (null != config.getView()) { - this.config.setView(config.getView()); + if (null != config) { + if (null != config.getGraphId()) { + this.config.setGraphId(config.getGraphId()); + } + if (null != config.getView()) { + this.config.setView(config.getView()); + } + if (null != config.getLibrary()) { + this.config.setLibrary(config.getLibrary()); + } + if (null != config.getDescription()) { + this.config.setDescription(config.getDescription()); + } + this.config.getHooks().addAll(config.getHooks()); } - if (null != config.getLibrary()) { - this.config.setLibrary(config.getLibrary()); - } - if (null != config.getDescription()) { - this.config.setDescription(config.getDescription()); - } - this.config.getHooks().addAll(config.getHooks()); return this; } @@ -194,16 +199,16 @@ public Builder view(final View view) { } public Builder view(final Path view) { - return view(new View.Builder().json(view).build()); + return view(null != view ? new View.Builder().json(view).build() : null); } public Builder view(final InputStream view) { - return view(new View.Builder().json(view).build()); + return view(null != view ? new View.Builder().json(view).build() : null); } public Builder view(final URI view) { try { - view(StreamUtil.openStream(view)); + view(null != view ? StreamUtil.openStream(view) : null); } catch (final IOException e) { throw new SchemaException("Unable to read view from URI: " + view, e); } @@ -211,7 +216,7 @@ public Builder view(final URI view) { } public Builder view(final byte[] jsonBytes) { - return view(new View.Builder().json(jsonBytes).build()); + return view(null != jsonBytes ? new View.Builder().json(jsonBytes).build() : null); } public Builder addHooks(final Path hooksPath) { @@ -242,12 +247,16 @@ public Builder addHook(final Path hookPath) { } public Builder addHook(final GraphHook graphHook) { - this.config.getHooks().add(graphHook); + if (null != graphHook) { + this.config.getHooks().add(graphHook); + } return this; } public Builder addHooks(final GraphHook... graphHooks) { - Collections.addAll(this.config.getHooks(), graphHooks); + if (null != graphHooks) { + Collections.addAll(this.config.getHooks(), graphHooks); + } return this; } diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java index a6b4d1ebb92..c73637ca9c9 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java @@ -16,6 +16,10 @@ package uk.gov.gchq.gaffer.graph; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonSetter; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; @@ -23,6 +27,7 @@ import uk.gov.gchq.gaffer.commonutil.ToStringBuilder; import uk.gov.gchq.gaffer.exception.SerialisationException; import uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser; +import uk.gov.gchq.gaffer.store.StoreProperties; import uk.gov.gchq.gaffer.store.library.GraphLibrary; import uk.gov.gchq.gaffer.store.schema.Schema; @@ -36,14 +41,13 @@ * * @see GraphSerialisable.Builder */ +@JsonDeserialize(builder = GraphSerialisable.Builder.class) public final class GraphSerialisable implements Serializable { private static final long serialVersionUID = 2684203367656032583L; - private byte[] schema; - private Properties properties; - private byte[] config; - - public GraphSerialisable() { - } + private final byte[] schema; + private final Properties properties; + private final byte[] config; + private transient Graph graph; private GraphSerialisable(final GraphConfig config, final Schema schema, final Properties properties) { try { @@ -59,8 +63,9 @@ private GraphSerialisable(final GraphConfig config, final Schema schema, final P * @return returns a new {@link Graph} built from the contents of a this * class. */ - public Graph buildGraph() { - return buildGraph(null); + @JsonIgnore + public Graph getGraph() { + return getGraph(null); } /** @@ -68,13 +73,20 @@ public Graph buildGraph() { * @return returns a new {@link Graph} built from the contents of a this * class. */ - public Graph buildGraph(final GraphLibrary library) { - return new Graph.Builder() - .addSchema(schema) - .addStoreProperties(properties) - .config(config) - .config(new GraphConfig.Builder().library(library).build()) - .build(); + @JsonIgnore + public Graph getGraph(final GraphLibrary library) { + if (null == graph) { + graph = new Graph.Builder() + .addSchema(schema) + .addStoreProperties(properties) + .config(config) + .config(new GraphConfig.Builder() + .library(library) + .build()) + .build(); + } + + return graph; } @Override @@ -123,27 +135,39 @@ public byte[] getConfig() { return config; } + @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "") public static class Builder { private Schema schema; private Properties properties; private GraphConfig config; - public Builder setSchema(final Schema schema) { + public Builder schema(final Schema schema) { this.schema = schema; return this; } - public Builder setProperties(final Properties properties) { + @JsonSetter("properties") + public Builder properties(final Properties properties) { this.properties = properties; return this; } - public Builder setConfig(final GraphConfig config) { + public Builder properties(final StoreProperties properties) { + if (null == properties) { + this.properties = null; + } else { + this.properties = properties.getProperties(); + } + return this; + } + + public Builder config(final GraphConfig config) { this.config = config; return this; } + @JsonIgnore public Builder graph(final Graph graph) { schema = graph.getSchema(); properties = graph.getStoreProperties().getProperties(); @@ -156,6 +180,15 @@ private Builder _self() { } public GraphSerialisable build() { + if (null == config) { + throw new IllegalArgumentException("config is required"); + } + if (null == schema) { + throw new IllegalArgumentException("schema is required"); + } + if (null == properties) { + throw new IllegalArgumentException("properties is required"); + } return new GraphSerialisable(config, schema, properties); } } diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java index de6aeb1f8e3..118a55f87f2 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/operation/export/graph/handler/GraphDelegate.java @@ -18,7 +18,6 @@ import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.graph.Graph; -import uk.gov.gchq.gaffer.graph.Graph.Builder; import uk.gov.gchq.gaffer.graph.GraphConfig; import uk.gov.gchq.gaffer.store.Store; import uk.gov.gchq.gaffer.store.StoreProperties; @@ -53,29 +52,18 @@ private GraphDelegate() { } public static Graph createGraph(final Store store, final String graphId, final Schema schema, final StoreProperties storeProperties, final List parentSchemaIds, final String parentStorePropertiesId) { - validate(store, graphId, schema, storeProperties, parentSchemaIds, parentStorePropertiesId); - final GraphLibrary graphLibrary = store.getGraphLibrary(); - final Graph rtn; + final Pair existingGraphPair = null != graphLibrary ? graphLibrary.get(graphId) : null; - if (null == graphLibrary) { - rtn = createGraphWithoutLibrary(store, graphId, schema, storeProperties); - } else if (graphLibrary.exists(graphId)) { - rtn = createGraphWithLibraryAndId(graphId, graphLibrary); - } else { - rtn = createGraphAfterResolvingSchemaAndProperties(store, graphId, schema, storeProperties, parentSchemaIds, parentStorePropertiesId); - } - return rtn; - } + validate(store, graphId, schema, storeProperties, parentSchemaIds, parentStorePropertiesId, existingGraphPair); - private static Graph createGraphAfterResolvingSchemaAndProperties(final Store store, final String graphId, final Schema schema, final StoreProperties storeProperties, final List parentSchemaIds, final String parentStorePropertiesId) { - StoreProperties resolvedStoreProperties = resolveStoreProperties(store, storeProperties, parentStorePropertiesId); - Schema resolvedSchema = resolveSchema(store, schema, parentSchemaIds); + final Schema resolvedSchema = resolveSchema(store, schema, parentSchemaIds, existingGraphPair); + final StoreProperties resolvedStoreProperties = resolveStoreProperties(store, storeProperties, parentStorePropertiesId, existingGraphPair); - return new Builder() + return new Graph.Builder() .config(new GraphConfig.Builder() .graphId(graphId) - .library(store.getGraphLibrary()) + .library(graphLibrary) .build()) .addSchema(resolvedSchema) .storeProperties(resolvedStoreProperties) @@ -83,88 +71,85 @@ private static Graph createGraphAfterResolvingSchemaAndProperties(final Store st .build(); } - private static StoreProperties resolveStoreProperties(final Store store, final StoreProperties properties, final String parentStorePropertiesId) { - StoreProperties rtn = null; - - if (null != parentStorePropertiesId) { - rtn = store.getGraphLibrary().getProperties(parentStorePropertiesId); - } - if (null != properties) { - if (null == rtn) { - rtn = properties; - } else { - rtn.merge(properties); + private static StoreProperties resolveStoreProperties(final Store store, final StoreProperties properties, final String parentStorePropertiesId, final Pair existingGraphPair) { + StoreProperties resultProps = null; + if (null != existingGraphPair) { + // If there is an existing graph then ignore any user provided properties and just use the existing properties + resultProps = existingGraphPair.getSecond(); + } else { + final GraphLibrary graphLibrary = store.getGraphLibrary(); + if (null != graphLibrary && null != parentStorePropertiesId) { + resultProps = graphLibrary.getProperties(parentStorePropertiesId); + } + if (null != properties) { + if (null == resultProps) { + resultProps = properties; + } else { + resultProps.merge(properties); + } + } + if (null == resultProps) { + // If no properties have been provided then default to using the store properties + resultProps = store.getProperties(); } } - if (null == rtn) { - rtn = store.getProperties(); - } - return rtn; + return resultProps; } - private static Schema resolveSchema(final Store store, final Schema schema, final List parentSchemaIds) { - final GraphLibrary graphLibrary = store.getGraphLibrary(); - - Schema rtn = null; - if (null != parentSchemaIds) { - if (1 == parentSchemaIds.size()) { - rtn = graphLibrary.getSchema(parentSchemaIds.get(0)); - } else { - final Schema.Builder schemaBuilder = new Schema.Builder(); - for (final String id : parentSchemaIds) { - schemaBuilder.merge(graphLibrary.getSchema(id)); + private static Schema resolveSchema(final Store store, final Schema schema, final List parentSchemaIds, final Pair existingGraphPair) { + Schema resultSchema = null; + if (null != existingGraphPair) { + // If there is an existing graph then ignore any user provided schemas and just use the existing schema + resultSchema = existingGraphPair.getFirst(); + } else { + final GraphLibrary graphLibrary = store.getGraphLibrary(); + if (null != graphLibrary && null != parentSchemaIds) { + if (1 == parentSchemaIds.size()) { + resultSchema = graphLibrary.getSchema(parentSchemaIds.get(0)); + } else { + final Schema.Builder schemaBuilder = new Schema.Builder(); + for (final String id : parentSchemaIds) { + schemaBuilder.merge(graphLibrary.getSchema(id)); + } + resultSchema = schemaBuilder.build(); } - rtn = schemaBuilder.build(); } - } - - if (null != schema) { - if (null == rtn) { - rtn = schema; - } else { - // delete the old schema id as we are about to modify the schema - rtn = new Schema.Builder() - .merge(rtn) - .id(null) - .merge(schema) - .build(); + if (null != schema) { + if (null == resultSchema) { + resultSchema = schema; + } else { + // delete the old schema id as we are about to modify the schema + resultSchema = new Schema.Builder() + .merge(resultSchema) + .merge(schema) + .build(); + } + } + if (null == resultSchema) { + // If no schemas have been provided then default to using the store schema + resultSchema = store.getSchema(); } } - if (null == rtn) { - rtn = store.getSchema(); - } - return rtn; + return resultSchema; } - private static Graph createGraphWithLibraryAndId(final String graphId, final GraphLibrary graphLibrary) { - // If the graphId exists in the graphLibrary then just use it - return new Graph.Builder() - .config(new GraphConfig.Builder() - .graphId(graphId) - .library(graphLibrary) - .build()) - .storeProperties(graphLibrary.get(graphId).getSecond()) - .addSchema(graphLibrary.get(graphId).getFirst()) - .addToLibrary(false) - .build(); - } - - private static Graph createGraphWithoutLibrary(final Store store, final String graphId, final Schema schema, final StoreProperties storeProperties) { - // No store graph library so we create a new Graph - final Schema resolveSchema = (null == schema) ? store.getSchema() : schema; - final StoreProperties resolveProperties = (null == storeProperties) ? store.getProperties() : storeProperties; + public static void validate(final Store store, final String graphId, + final Schema schema, final StoreProperties storeProperties, + final List parentSchemaIds, final String parentStorePropertiesId) { + final Pair existingGraphPair; + if (null == store.getGraphLibrary()) { + existingGraphPair = null; + } else { + existingGraphPair = store.getGraphLibrary().get(graphId); + } - return new Builder() - .config(new GraphConfig.Builder() - .graphId(graphId) - .build()) - .addSchema(resolveSchema) - .storeProperties(resolveProperties) - .addToLibrary(false) - .build(); + validate(store, graphId, schema, storeProperties, parentSchemaIds, parentStorePropertiesId, existingGraphPair); } - public static void validate(final Store store, final String graphId, final Schema schema, final StoreProperties storeProperties, final List parentSchemaIds, final String parentStorePropertiesId) { + public static void validate(final Store store, final String graphId, + final Schema schema, final StoreProperties storeProperties, + final List parentSchemaIds, final String parentStorePropertiesId, + final Pair existingGraphPair) { final GraphLibrary graphLibrary = store.getGraphLibrary(); final ValidationResult result = new ValidationResult(); @@ -180,7 +165,7 @@ public static void validate(final Store store, final String graphId, final Schem if (null != parentStorePropertiesId) { result.addError(String.format(S_CANNOT_BE_USED_WITHOUT_A_GRAPH_LIBRARY, "parentStorePropertiesId")); } - } else if (graphLibrary.exists(graphId)) { + } else if (null != existingGraphPair) { if (null != parentSchemaIds) { Schema.Builder idFromLibrary = new Schema.Builder(); @@ -190,31 +175,30 @@ public static void validate(final Store store, final String graphId, final Schem idFromLibrary.merge(tempSchema); } } - Schema fromLibrary = graphLibrary.get(graphId).getFirst(); + Schema fromLibrary = existingGraphPair.getFirst(); if (!fromLibrary.toString().equals(idFromLibrary.build().toString())) { result.addError(String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, graphId, "Schema", "parentSchemaIds")); } } if (null != parentStorePropertiesId) { - StoreProperties fromLibrary = graphLibrary.get(graphId).getSecond(); + StoreProperties fromLibrary = existingGraphPair.getSecond(); if (!fromLibrary.equals(graphLibrary.getProperties(parentStorePropertiesId))) { result.addError(String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, graphId, "StoreProperties", "parentStorePropertiesId")); } } - Pair schemaStorePropertiesPair = graphLibrary.get(graphId); - if (null != schema && null != schemaStorePropertiesPair && !schema.toString().equals(schemaStorePropertiesPair.getFirst().toString())) { + if (null != schema && !schema.toString().equals(existingGraphPair.getFirst().toString())) { result.addError(String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, graphId, "Schema", "schema")); } - if (null != storeProperties && null != schemaStorePropertiesPair && !schemaStorePropertiesPair.getSecond().equals(storeProperties)) { + if (null != storeProperties && !existingGraphPair.getSecond().equals(storeProperties)) { result.addError(String.format(GRAPH_S_ALREADY_EXISTS_SO_YOU_CANNOT_USE_A_DIFFERENT_S_DO_NOT_SET_THE_S_FIELD, graphId, "StoreProperties", "storeProperties")); } } else { if (null != parentSchemaIds) { for (final String exportParentSchemaId : parentSchemaIds) { - if (null == store.getGraphLibrary().getSchema(exportParentSchemaId)) { + if (null == graphLibrary.getSchema(exportParentSchemaId)) { result.addError(String.format(SCHEMA_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, parentSchemaIds)); } } @@ -223,7 +207,7 @@ public static void validate(final Store store, final String graphId, final Schem } if (null != parentStorePropertiesId) { - if (null == store.getGraphLibrary().getProperties(parentStorePropertiesId)) { + if (null == graphLibrary.getProperties(parentStorePropertiesId)) { result.addError(String.format(STORE_PROPERTIES_COULD_NOT_BE_FOUND_IN_THE_GRAPH_LIBRARY_WITH_ID_S, parentStorePropertiesId)); } } else if (null == storeProperties) { diff --git a/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphSerialisableTest.java b/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphSerialisableTest.java index b00b60f426b..fc34a86ea97 100644 --- a/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphSerialisableTest.java +++ b/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphSerialisableTest.java @@ -55,9 +55,9 @@ public void setUp() throws Exception { storeProperties.setStoreClass(TestStore.class); properties = storeProperties.getProperties(); expected = new GraphSerialisable.Builder() - .setSchema(schema) - .setProperties(properties) - .setConfig(config) + .schema(schema) + .properties(properties) + .config(config) .build(); } diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/schema/Schema.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/schema/Schema.java index 0fcbdbdda06..276e5026363 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/schema/Schema.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/schema/Schema.java @@ -354,17 +354,19 @@ public CHILD_CLASS vertexSerialiserClass(final String vertexSerialiserClass) { } public CHILD_CLASS type(final String typeName, final TypeDefinition type) { - getThisSchema().types.put(typeName, type); + getThisSchema().types.put(typeName, null != type ? type : new TypeDefinition()); return self(); } public CHILD_CLASS type(final String typeName, final Class typeClass) { - return type(typeName, new TypeDefinition(typeClass)); + return type(typeName, null != typeClass ? new TypeDefinition(typeClass) : null); } public CHILD_CLASS types(final Map types) { getThisSchema().types.clear(); - getThisSchema().types.putAll(types); + if (null != types) { + getThisSchema().types.putAll(types); + } return self(); } @@ -381,83 +383,85 @@ public CHILD_CLASS timestampProperty(final String timestampProperty) { @Override @JsonIgnore public CHILD_CLASS merge(final Schema schema) { - validateSharedGroups(getThisSchema().getEntities(), schema.getEntities()); - validateSharedGroups(getThisSchema().getEdges(), schema.getEdges()); - - // Schema ID is deprecated - remove this when ID is removed. - if (null == getThisSchema().getId()) { - getThisSchema().setId(schema.getId()); - } else if (null != schema.getId() - && !schema.getId().equals(getThisSchema().getId())) { - getThisSchema().setId(getThisSchema().getId() + "_" + schema.getId()); - } + if (null != schema) { + validateSharedGroups(getThisSchema().getEntities(), schema.getEntities()); + validateSharedGroups(getThisSchema().getEdges(), schema.getEdges()); + + // Schema ID is deprecated - remove this when ID is removed. + if (null == getThisSchema().getId()) { + getThisSchema().setId(schema.getId()); + } else if (null != schema.getId() + && !schema.getId().equals(getThisSchema().getId())) { + getThisSchema().setId(getThisSchema().getId() + "_" + schema.getId()); + } - if (getThisSchema().getEntities().isEmpty()) { - getThisSchema().getEntities().putAll(schema.getEntities()); - } else { - for (final Map.Entry entry : schema.getEntities().entrySet()) { - if (!getThisSchema().getEntities().containsKey(entry.getKey())) { - entity(entry.getKey(), entry.getValue()); - } else { - final SchemaEntityDefinition mergedElementDef = new SchemaEntityDefinition.Builder() - .merge(getThisSchema().getEntities().get(entry.getKey())) - .merge(entry.getValue()) - .build(); - getThisSchema().getEntities().put(entry.getKey(), mergedElementDef); + if (getThisSchema().getEntities().isEmpty()) { + getThisSchema().getEntities().putAll(schema.getEntities()); + } else { + for (final Map.Entry entry : schema.getEntities().entrySet()) { + if (!getThisSchema().getEntities().containsKey(entry.getKey())) { + entity(entry.getKey(), entry.getValue()); + } else { + final SchemaEntityDefinition mergedElementDef = new SchemaEntityDefinition.Builder() + .merge(getThisSchema().getEntities().get(entry.getKey())) + .merge(entry.getValue()) + .build(); + getThisSchema().getEntities().put(entry.getKey(), mergedElementDef); + } } } - } - if (getThisSchema().getEdges().isEmpty()) { - getThisSchema().getEdges().putAll(schema.getEdges()); - } else { - for (final Map.Entry entry : schema.getEdges().entrySet()) { - if (!getThisSchema().getEdges().containsKey(entry.getKey())) { - edge(entry.getKey(), entry.getValue()); - } else { - final SchemaEdgeDefinition mergedElementDef = new SchemaEdgeDefinition.Builder() - .merge(getThisSchema().getEdges().get(entry.getKey())) - .merge(entry.getValue()) - .build(); - getThisSchema().getEdges().put(entry.getKey(), mergedElementDef); + if (getThisSchema().getEdges().isEmpty()) { + getThisSchema().getEdges().putAll(schema.getEdges()); + } else { + for (final Map.Entry entry : schema.getEdges().entrySet()) { + if (!getThisSchema().getEdges().containsKey(entry.getKey())) { + edge(entry.getKey(), entry.getValue()); + } else { + final SchemaEdgeDefinition mergedElementDef = new SchemaEdgeDefinition.Builder() + .merge(getThisSchema().getEdges().get(entry.getKey())) + .merge(entry.getValue()) + .build(); + getThisSchema().getEdges().put(entry.getKey(), mergedElementDef); + } } } - } - if (null != schema.getVertexSerialiser()) { - if (null == getThisSchema().vertexSerialiser) { - getThisSchema().vertexSerialiser = schema.getVertexSerialiser(); - } else if (!getThisSchema().vertexSerialiser.getClass().equals(schema.getVertexSerialiser().getClass())) { - throw new SchemaException("Unable to merge schemas. Conflict with vertex serialiser, options are: " - + getThisSchema().vertexSerialiser.getClass().getName() + " and " + schema.getVertexSerialiser().getClass().getName()); + if (null != schema.getVertexSerialiser()) { + if (null == getThisSchema().vertexSerialiser) { + getThisSchema().vertexSerialiser = schema.getVertexSerialiser(); + } else if (!getThisSchema().vertexSerialiser.getClass().equals(schema.getVertexSerialiser().getClass())) { + throw new SchemaException("Unable to merge schemas. Conflict with vertex serialiser, options are: " + + getThisSchema().vertexSerialiser.getClass().getName() + " and " + schema.getVertexSerialiser().getClass().getName()); + } } - } - if (null == getThisSchema().visibilityProperty) { - getThisSchema().visibilityProperty = schema.getVisibilityProperty(); - } else if (null != schema.getVisibilityProperty() && !getThisSchema().visibilityProperty.equals(schema.getVisibilityProperty())) { - throw new SchemaException("Unable to merge schemas. Conflict with visibility property, options are: " - + getThisSchema().visibilityProperty + " and " + schema.getVisibilityProperty()); - } + if (null == getThisSchema().visibilityProperty) { + getThisSchema().visibilityProperty = schema.getVisibilityProperty(); + } else if (null != schema.getVisibilityProperty() && !getThisSchema().visibilityProperty.equals(schema.getVisibilityProperty())) { + throw new SchemaException("Unable to merge schemas. Conflict with visibility property, options are: " + + getThisSchema().visibilityProperty + " and " + schema.getVisibilityProperty()); + } - if (null == getThisSchema().timestampProperty) { - getThisSchema().timestampProperty = schema.getTimestampProperty(); - } else if (null != schema.getTimestampProperty() && !getThisSchema().timestampProperty.equals(schema.getTimestampProperty())) { - throw new SchemaException("Unable to merge schemas. Conflict with timestamp property, options are: " - + getThisSchema().timestampProperty + " and " + schema.getTimestampProperty()); - } + if (null == getThisSchema().timestampProperty) { + getThisSchema().timestampProperty = schema.getTimestampProperty(); + } else if (null != schema.getTimestampProperty() && !getThisSchema().timestampProperty.equals(schema.getTimestampProperty())) { + throw new SchemaException("Unable to merge schemas. Conflict with timestamp property, options are: " + + getThisSchema().timestampProperty + " and " + schema.getTimestampProperty()); + } - if (getThisSchema().types.isEmpty()) { - getThisSchema().types.putAll(schema.types); - } else { - for (final Entry entry : schema.types.entrySet()) { - final String newType = entry.getKey(); - final TypeDefinition newTypeDef = entry.getValue(); - final TypeDefinition typeDef = getThisSchema().types.get(newType); - if (null == typeDef) { - getThisSchema().types.put(newType, newTypeDef); - } else { - typeDef.merge(newTypeDef); + if (getThisSchema().types.isEmpty()) { + getThisSchema().types.putAll(schema.types); + } else { + for (final Entry entry : schema.types.entrySet()) { + final String newType = entry.getKey(); + final TypeDefinition newTypeDef = entry.getValue(); + final TypeDefinition typeDef = getThisSchema().types.get(newType); + if (null == typeDef) { + getThisSchema().types.put(newType, newTypeDef); + } else { + typeDef.merge(newTypeDef); + } } } } diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/schema/SchemaElementDefinition.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/schema/SchemaElementDefinition.java index df4a5f9e417..a5e93ab50f7 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/schema/SchemaElementDefinition.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/schema/SchemaElementDefinition.java @@ -571,7 +571,9 @@ public CHILD_CLASS property(final String propertyName, final String typeName) { } public CHILD_CLASS properties(final Map properties) { - if (null == elDef.properties) { + if (null == properties) { + elDef.properties = null; + } else if (null == elDef.properties) { elDef.properties = new LinkedHashMap<>(properties); } else { elDef.properties.putAll(properties); @@ -585,7 +587,9 @@ public CHILD_CLASS identifier(final IdentifierType identifierType, final String } public CHILD_CLASS identifiers(final Map identifiers) { - if (null == elDef.identifiers) { + if (null == identifiers) { + elDef.identifiers = null; + } else if (null == elDef.identifiers) { elDef.identifiers = new LinkedHashMap<>(identifiers); } else { elDef.identifiers.putAll(identifiers); @@ -600,10 +604,12 @@ public CHILD_CLASS aggregator(final ElementAggregator aggregator) { @JsonSetter("aggregateFunctions") public CHILD_CLASS aggregateFunctions(final List>> aggregateFunctions) { - if (null == elDef.aggregator) { - elDef.aggregator = new ElementAggregator(); + if (null != aggregateFunctions) { + if (null == elDef.aggregator) { + elDef.aggregator = new ElementAggregator(); + } + elDef.aggregator.getComponents().addAll(aggregateFunctions); } - elDef.aggregator.getComponents().addAll(aggregateFunctions); return self(); } @@ -614,33 +620,42 @@ public CHILD_CLASS validator(final ElementFilter validator) { @JsonSetter("validateFunctions") public CHILD_CLASS validateFunctions(final List>> predicates) { - if (null == elDef.validator) { - elDef.validator = new ElementFilter(); + if (null != predicates) { + if (null == elDef.validator) { + elDef.validator = new ElementFilter(); + } + elDef.validator.getComponents().addAll(predicates); } - elDef.validator.getComponents().addAll(predicates); return self(); } @SafeVarargs public final CHILD_CLASS validateFunctions(final TupleAdaptedPredicate>... predicates) { - if (null == elDef.validator) { - elDef.validator = new ElementFilter(); + if (null != predicates) { + if (null == elDef.validator) { + elDef.validator = new ElementFilter(); + } + Collections.addAll(elDef.validator.getComponents(), predicates); } - Collections.addAll(elDef.validator.getComponents(), predicates); return self(); } public final CHILD_CLASS validateFunctions(final ElementFilter elementFilter) { - return validateFunctions((List>>) (List) elementFilter.getComponents()); + if (null != elementFilter) { + validateFunctions((List>>) (List) elementFilter.getComponents()); + } + return self(); } public CHILD_CLASS groupBy(final String... propertyName) { - Collections.addAll(elDef.getGroupBy(), propertyName); + if (null != propertyName) { + Collections.addAll(elDef.getGroupBy(), propertyName); + } return self(); } public CHILD_CLASS parents(final String... parents) { - if (parents.length > 0) { + if (null != parents && parents.length > 0) { if (null == elDef.parents) { elDef.parents = new LinkedHashSet<>(); } @@ -661,67 +676,68 @@ public CHILD_CLASS aggregate(final boolean aggregate) { } public CHILD_CLASS merge(final ELEMENT_DEF elementDef) { - if (elDef.properties.isEmpty()) { - elDef.properties.putAll(elementDef.getPropertyMap()); - } else { - for (final Entry entry : elementDef.getPropertyMap().entrySet()) { - final String typeName = elDef.getPropertyTypeName(entry.getKey()); - if (null == typeName) { - elDef.properties.put(entry.getKey(), entry.getValue()); - } else if (!typeName.equals(entry.getValue())) { - throw new SchemaException("Unable to merge element definitions because the property " + entry.getKey() + " exists in both definitions with different types: " + typeName + " and " + entry.getValue()); + if (null != elementDef) { + if (elDef.properties.isEmpty()) { + elDef.properties.putAll(elementDef.getPropertyMap()); + } else { + for (final Entry entry : elementDef.getPropertyMap().entrySet()) { + final String typeName = elDef.getPropertyTypeName(entry.getKey()); + if (null == typeName) { + elDef.properties.put(entry.getKey(), entry.getValue()); + } else if (!typeName.equals(entry.getValue())) { + throw new SchemaException("Unable to merge element definitions because the property " + entry.getKey() + " exists in both definitions with different types: " + typeName + " and " + entry.getValue()); + } } } - } - if (elDef.identifiers.isEmpty()) { - elDef.identifiers.putAll(elementDef.getIdentifierMap()); - } else { - for (final Entry entry : elementDef.getIdentifierMap().entrySet()) { - elDef.identifiers.put(entry.getKey(), entry.getValue()); + if (elDef.identifiers.isEmpty()) { + elDef.identifiers.putAll(elementDef.getIdentifierMap()); + } else { + for (final Entry entry : elementDef.getIdentifierMap().entrySet()) { + elDef.identifiers.put(entry.getKey(), entry.getValue()); + } } - } - - if (null == elDef.validator) { - elDef.validator = elementDef.validator; - } else if (null != elementDef.getOriginalValidateFunctions()) { - final ElementFilter combinedFilter = new ElementFilter(); - combinedFilter.getComponents().addAll(elDef.validator.getComponents()); - combinedFilter.getComponents().addAll(elementDef.validator.getComponents()); - combinedFilter.lock(); - elDef.validator = combinedFilter; - } - elDef.fullValidatorCache = null; - elDef.fullValidatorWithIsACache = null; - - if (null == elDef.aggregator) { - elDef.aggregator = elementDef.aggregator; - } else if (null != elementDef.getOriginalAggregateFunctions()) { - final ElementAggregator combinedAggregator = new ElementAggregator(); - combinedAggregator.getComponents().addAll(elDef.aggregator.getComponents()); - combinedAggregator.getComponents().addAll(elementDef.aggregator.getComponents()); - combinedAggregator.lock(); - elDef.aggregator = combinedAggregator; - } - elDef.propertiesInAggregatorCache = null; - elDef.fullAggregatorCache = null; - elDef.ingestAggregatorCache = null; - elDef.queryAggregatorCacheMap.clear(); - if (null != elementDef.groupBy && !elementDef.groupBy.isEmpty()) { - elDef.groupBy = new LinkedHashSet<>(elementDef.groupBy); - } + if (null == elDef.validator) { + elDef.validator = elementDef.validator; + } else if (null != elementDef.getOriginalValidateFunctions()) { + final ElementFilter combinedFilter = new ElementFilter(); + combinedFilter.getComponents().addAll(elDef.validator.getComponents()); + combinedFilter.getComponents().addAll(elementDef.validator.getComponents()); + combinedFilter.lock(); + elDef.validator = combinedFilter; + } + elDef.fullValidatorCache = null; + elDef.fullValidatorWithIsACache = null; + + if (null == elDef.aggregator) { + elDef.aggregator = elementDef.aggregator; + } else if (null != elementDef.getOriginalAggregateFunctions()) { + final ElementAggregator combinedAggregator = new ElementAggregator(); + combinedAggregator.getComponents().addAll(elDef.aggregator.getComponents()); + combinedAggregator.getComponents().addAll(elementDef.aggregator.getComponents()); + combinedAggregator.lock(); + elDef.aggregator = combinedAggregator; + } + elDef.propertiesInAggregatorCache = null; + elDef.fullAggregatorCache = null; + elDef.ingestAggregatorCache = null; + elDef.queryAggregatorCacheMap.clear(); - if (null != elementDef.parents && !elementDef.parents.isEmpty()) { - elDef.parents = new LinkedHashSet<>(elementDef.parents); - } + if (null != elementDef.groupBy && !elementDef.groupBy.isEmpty()) { + elDef.groupBy = new LinkedHashSet<>(elementDef.groupBy); + } - if (null != elementDef.description) { - elDef.description = elementDef.description; - } + if (null != elementDef.parents && !elementDef.parents.isEmpty()) { + elDef.parents = new LinkedHashSet<>(elementDef.parents); + } - elDef.aggregate = elDef.aggregate && elementDef.aggregate; + if (null != elementDef.description) { + elDef.description = elementDef.description; + } + elDef.aggregate = elDef.aggregate && elementDef.aggregate; + } return self(); } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java index b708367cf2a..5cee38a8576 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java @@ -331,7 +331,7 @@ private void addToCache(final Graph newGraph, final FederatedAccess access) { } private void validateSameAsFromCache(final Graph newGraph, final String graphId) { - final Graph fromCache = federatedStoreCache.getGraphSerialisableFromCache(graphId).buildGraph(library); + final Graph fromCache = federatedStoreCache.getGraphSerialisableFromCache(graphId).getGraph(library); if (!newGraph.getStoreProperties().getProperties().equals(fromCache.getStoreProperties().getProperties())) { throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, GraphConfigEnum.PROPERTIES.toString(), graphId)); } else { @@ -377,7 +377,7 @@ private Boolean isCacheEnabled() { private void makeGraphFromCache(final String graphId) { final GraphSerialisable serialisable = federatedStoreCache.getGraphSerialisableFromCache(graphId); - final Graph graph = serialisable.buildGraph(library); + final Graph graph = serialisable.getGraph(library); final FederatedAccess accessFromCache = federatedStoreCache.getAccessFromCache(graphId); put(graph, accessFromCache); } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java index f738304846c..93f80f684c5 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreCache.java @@ -90,7 +90,7 @@ public void addGraphToCache(final Graph graph, final FederatedAccess access, fin public Graph getGraphFromCache(final String graphId) { final Pair fromCache = CacheServiceLoader.getService().getFromCache(CACHE_SERVICE_NAME, graphId); final GraphSerialisable graphSerialisable = (null == fromCache) ? null : fromCache.getFirst(); - return (null == graphSerialisable) ? null : graphSerialisable.buildGraph(); + return (null == graphSerialisable) ? null : graphSerialisable.getGraph(); } /** diff --git a/store-implementation/map-store/src/test/java/uk/gov/gchq/gaffer/mapstore/MapStorePropertiesGraphSerialisableTest.java b/store-implementation/map-store/src/test/java/uk/gov/gchq/gaffer/mapstore/MapStorePropertiesGraphSerialisableTest.java index c41b8808700..dff8b60d3e8 100644 --- a/store-implementation/map-store/src/test/java/uk/gov/gchq/gaffer/mapstore/MapStorePropertiesGraphSerialisableTest.java +++ b/store-implementation/map-store/src/test/java/uk/gov/gchq/gaffer/mapstore/MapStorePropertiesGraphSerialisableTest.java @@ -59,9 +59,9 @@ public void setUp() throws Exception { private GraphSerialisable getGraphSerialisable() { return new GraphSerialisable.Builder() - .setSchema(schema) - .setProperties(properties) - .setConfig(config) + .schema(schema) + .properties(properties) + .config(config) .build(); } From 37b1c1f449544993f964dba822ca193dc2d162f3 Mon Sep 17 00:00:00 2001 From: p013570 Date: Wed, 1 Nov 2017 14:10:17 +0000 Subject: [PATCH 60/72] gh-1297 - updated null handling in GraphSerialisable --- .../uk/gov/gchq/gaffer/graph/GraphSerialisable.java | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java index c73637ca9c9..94dae41d1b4 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java @@ -51,8 +51,8 @@ public final class GraphSerialisable implements Serializable { private GraphSerialisable(final GraphConfig config, final Schema schema, final Properties properties) { try { - this.schema = JSONSerialiser.serialise(schema, true); - this.config = JSONSerialiser.serialise(config, true); + this.schema = null == schema ? null : JSONSerialiser.serialise(schema, true); + this.config = null == config ? null : JSONSerialiser.serialise(config, true); } catch (final SerialisationException e) { throw new IllegalArgumentException("Unable to serialise given parameter", e); } @@ -180,15 +180,6 @@ private Builder _self() { } public GraphSerialisable build() { - if (null == config) { - throw new IllegalArgumentException("config is required"); - } - if (null == schema) { - throw new IllegalArgumentException("schema is required"); - } - if (null == properties) { - throw new IllegalArgumentException("properties is required"); - } return new GraphSerialisable(config, schema, properties); } } From 8d6183b4e88b6b2238654dfd72ca60f674887bcb Mon Sep 17 00:00:00 2001 From: p013570 Date: Wed, 1 Nov 2017 14:11:45 +0000 Subject: [PATCH 61/72] gh-1297 - updated error messages in GraphSerialisable --- .../java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java index 94dae41d1b4..3d8d3d2b7f6 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java @@ -52,9 +52,13 @@ public final class GraphSerialisable implements Serializable { private GraphSerialisable(final GraphConfig config, final Schema schema, final Properties properties) { try { this.schema = null == schema ? null : JSONSerialiser.serialise(schema, true); + } catch (final SerialisationException e) { + throw new IllegalArgumentException("Unable to serialise schema", e); + } + try { this.config = null == config ? null : JSONSerialiser.serialise(config, true); } catch (final SerialisationException e) { - throw new IllegalArgumentException("Unable to serialise given parameter", e); + throw new IllegalArgumentException("Unable to serialise config", e); } this.properties = properties; } From fcc4ed202237e031f2d8da8ded63fbc267d7f7f9 Mon Sep 17 00:00:00 2001 From: p013570 Date: Wed, 1 Nov 2017 14:15:27 +0000 Subject: [PATCH 62/72] gh-1297 - minor change --- .../java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java index 3d8d3d2b7f6..29580f04c24 100644 --- a/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java +++ b/core/graph/src/main/java/uk/gov/gchq/gaffer/graph/GraphSerialisable.java @@ -148,13 +148,13 @@ public static class Builder { public Builder schema(final Schema schema) { this.schema = schema; - return this; + return _self(); } @JsonSetter("properties") public Builder properties(final Properties properties) { this.properties = properties; - return this; + return _self(); } public Builder properties(final StoreProperties properties) { @@ -163,12 +163,12 @@ public Builder properties(final StoreProperties properties) { } else { this.properties = properties.getProperties(); } - return this; + return _self(); } public Builder config(final GraphConfig config) { this.config = config; - return this; + return _self(); } @JsonIgnore From 1a2545ed92aef6d16834df3567c2afb698e7c625 Mon Sep 17 00:00:00 2001 From: p013570 Date: Wed, 1 Nov 2017 16:42:07 +0000 Subject: [PATCH 63/72] gh-1297 - fixed bugs with federated cache Also moved the graph library into the GraphStorage class - gh-1486 --- .../gchq/gaffer/cache/CacheServiceLoader.java | 7 ++ .../cache/impl/HashMapCacheService.java | 12 +- .../gov/gchq/gaffer/commonutil/pair/Pair.java | 4 +- .../store/library/HashMapGraphLibrary.java | 8 ++ example/federated-demo/README.md | 114 ++++++++++++++++++ .../main/resources/federatedStore.properties | 8 +- .../federatedstore/FederatedAccess.java | 9 +- .../federatedstore/FederatedGraphStorage.java | 84 +++++++------ .../gaffer/federatedstore/FederatedStore.java | 31 +++-- .../FederatedStoreConstants.java | 5 +- .../exception/StorageException.java | 46 +++++++ .../impl/FederatedAddGraphHandler.java | 7 +- .../FederatedStoreMultiCacheTest.java | 9 ++ .../federatedstore/FederatedStoreTest.java | 26 ++-- .../PredefinedFederatedStore.java | 46 ++++--- .../FederatedGetSchemaHandlerTest.java | 18 ++- .../impl/FederatedAddGraphHandlerTest.java | 3 +- .../FederatedOperationChainHandlerTest.java | 10 ++ 18 files changed, 350 insertions(+), 97 deletions(-) create mode 100644 store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/exception/StorageException.java diff --git a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/CacheServiceLoader.java b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/CacheServiceLoader.java index 73bbbce35ac..dd4b0e1ad2b 100644 --- a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/CacheServiceLoader.java +++ b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/CacheServiceLoader.java @@ -79,6 +79,13 @@ public static ICacheService getService() { return service; } + /** + * @return true if the cache is enabled + */ + public static boolean isEnabled() { + return null != service; + } + /** * Gracefully shutdown and reset the cache service. */ diff --git a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheService.java b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheService.java index e193cea1aea..92c12968132 100644 --- a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheService.java +++ b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheService.java @@ -27,12 +27,20 @@ * {@link HashMapCache} as the cache implementation. */ public class HashMapCacheService implements ICacheService { + public static final String STATIC_CACHE = "gaffer.cache.hashmap.static"; - private final HashMap caches = new HashMap<>(); + private static final HashMap staticCaches = new HashMap<>(); + private final HashMap nonStaticCaches = new HashMap<>(); + + private HashMap caches = nonStaticCaches; @Override public void initialise(final Properties properties) { - // do nothing + if (Boolean.parseBoolean(properties.getProperty(STATIC_CACHE))) { + caches = staticCaches; + } else { + caches = nonStaticCaches; + } } @Override diff --git a/core/common-util/src/main/java/uk/gov/gchq/gaffer/commonutil/pair/Pair.java b/core/common-util/src/main/java/uk/gov/gchq/gaffer/commonutil/pair/Pair.java index df733305fa3..2f22df9366b 100755 --- a/core/common-util/src/main/java/uk/gov/gchq/gaffer/commonutil/pair/Pair.java +++ b/core/common-util/src/main/java/uk/gov/gchq/gaffer/commonutil/pair/Pair.java @@ -22,13 +22,15 @@ import uk.gov.gchq.gaffer.commonutil.ToStringBuilder; +import java.io.Serializable; + /** * A simple class to contain a pair of items. * * @param type of first item in the pair * @param type of second item in the pair */ -public class Pair { +public class Pair implements Serializable { private static final long serialVersionUID = 4769405415756562347L; private F first; diff --git a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibrary.java b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibrary.java index d47e5317099..c7f31c62394 100644 --- a/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibrary.java +++ b/core/store/src/main/java/uk/gov/gchq/gaffer/store/library/HashMapGraphLibrary.java @@ -16,6 +16,7 @@ package uk.gov.gchq.gaffer.store.library; +import uk.gov.gchq.gaffer.commonutil.ToStringBuilder; import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.commonutil.pair.Pair; import uk.gov.gchq.gaffer.store.StoreProperties; @@ -72,4 +73,11 @@ protected StoreProperties _getProperties(final String propertiesId) { final StoreProperties storeProperties = PROPERTIES.get(propertiesId); return (null == storeProperties) ? null : storeProperties.clone(); } + + @Override + public String toString() { + return new ToStringBuilder(this) + .append("graphs", GRAPHS) + .toString(); + } } diff --git a/example/federated-demo/README.md b/example/federated-demo/README.md index 59dbc0e7ab6..a000a9da597 100644 --- a/example/federated-demo/README.md +++ b/example/federated-demo/README.md @@ -33,6 +33,100 @@ If you wish to build all of Gaffer first then just remove the "-pl :federated-de The rest api will be deployed to localhost:8080/rest. +To add a Map graph, execute this operation: + +```json +{ + "class": "uk.gov.gchq.gaffer.federatedstore.operation.AddGraph", + "graphId": "mapGraph", + "storeProperties": { + "gaffer.store.class": "uk.gov.gchq.gaffer.mapstore.MapStore", + "gaffer.store.mapstore.static": true + }, + "schema": { + "edges": { + "BasicEdge": { + "source": "vertex", + "destination": "vertex", + "directed": "true", + "properties": { + "count": "count" + } + } + }, + "types": { + "vertex": { + "class": "java.lang.String" + }, + "count": { + "class": "java.lang.Integer", + "aggregateFunction": { + "class": "uk.gov.gchq.koryphe.impl.binaryoperator.Sum" + } + }, + "true": { + "description": "A simple boolean that must always be true.", + "class": "java.lang.Boolean", + "validateFunctions": [ + { + "class": "uk.gov.gchq.koryphe.impl.predicate.IsTrue" + } + ] + } + } + }, + "isPublic": true +} +``` + +And to add an Accumulo graph execute this: + +```json +{ + "class": "uk.gov.gchq.gaffer.federatedstore.operation.AddGraph", + "graphId": "accumuloGraph", + "storeProperties": { + "gaffer.store.class": "uk.gov.gchq.gaffer.accumulostore.MockAccumuloStore", + "accumulo.instance": "someInstanceName", + "accumulo.zookeepers": "aZookeeper", + "accumulo.user": "user01", + "accumulo.password": "password" + }, + "schema": { + "entities": { + "BasicEntity": { + "vertex": "vertex", + "properties": { + "count": "count" + } + } + }, + "types": { + "vertex": { + "class": "java.lang.String" + }, + "count": { + "class": "java.lang.Integer", + "aggregateFunction": { + "class": "uk.gov.gchq.koryphe.impl.binaryoperator.Sum" + } + }, + "true": { + "description": "A simple boolean that must always be true.", + "class": "java.lang.Boolean", + "validateFunctions": [ + { + "class": "uk.gov.gchq.koryphe.impl.predicate.IsTrue" + } + ] + } + } + }, + "isPublic": true +} +``` + + To add some example data execute this json in /graph/operations/execute: ```json @@ -86,4 +180,24 @@ Here is an example of an advanced federated operation chain: } ] } +``` + +To fetch the merged schemas you can run: + +```json +{ + "class": "uk.gov.gchq.gaffer.store.operation.GetSchema", + "compact": false +} +``` + +To fetch just a the schema for the mapGraph you can add an option: +```json +{ + "class": "uk.gov.gchq.gaffer.store.operation.GetSchema", + "compact": false, + "options": { + "gaffer.federatedstore.operation.graphIds": "mapGraph" + } +} ``` \ No newline at end of file diff --git a/example/federated-demo/src/main/resources/federatedStore.properties b/example/federated-demo/src/main/resources/federatedStore.properties index 0218ec7ccee..72093a88617 100644 --- a/example/federated-demo/src/main/resources/federatedStore.properties +++ b/example/federated-demo/src/main/resources/federatedStore.properties @@ -14,10 +14,4 @@ # limitations under the License. # gaffer.store.class=uk.gov.gchq.gaffer.federatedstore.FederatedStore -gaffer.federatedstore.graphIds=accumuloGraph,mapGraph -gaffer.federatedstore.accumuloGraph.properties.file=accumulo/store.properties -gaffer.federatedstore.accumuloGraph.schema.file=accumulo/schema/elements.json,accumulo/schema/types.json -gaffer.federatedstore.accumuloGraph.isPublic=true -gaffer.federatedstore.mapGraph.properties.file=map/store.properties -gaffer.federatedstore.mapGraph.schema.file=map/schema/elements.json,map/schema/types.json -gaffer.federatedstore.mapGraph.auths=user +gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.JcsCacheService \ No newline at end of file diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java index e02429da291..ef458530aae 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java @@ -20,6 +20,7 @@ import uk.gov.gchq.gaffer.user.User; +import java.io.Serializable; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -56,7 +57,9 @@ * * @see #isValidToExecute(User) */ -public class FederatedAccess { +public class FederatedAccess implements Serializable { + private static final long serialVersionUID = 1399629017857618033L; + private boolean isPublic = Boolean.valueOf(DEFAULT_VALUE_IS_PUBLIC); private Set graphAuths = new HashSet<>(); private String addingUserId; @@ -71,6 +74,10 @@ public FederatedAccess(final Set graphAuths, final String addingUser, fi this.isPublic = isPublic; } + public String getAddingUserId() { + return addingUserId; + } + public void setAddingUserId(final String creatorUserId) { this.addingUserId = creatorUserId; } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java index 5cee38a8576..b6ea002e916 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java @@ -23,6 +23,7 @@ import uk.gov.gchq.gaffer.commonutil.JsonUtil; import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException; +import uk.gov.gchq.gaffer.federatedstore.exception.StorageException; import uk.gov.gchq.gaffer.federatedstore.util.FederatedStoreUtil; import uk.gov.gchq.gaffer.graph.Graph; import uk.gov.gchq.gaffer.graph.GraphSerialisable; @@ -56,12 +57,11 @@ public class FederatedGraphStorage { private Map> storage = new HashMap<>(); private FederatedStoreCache federatedStoreCache = new FederatedStoreCache(); private Boolean isCacheEnabled = false; - private GraphLibrary library; + private GraphLibrary graphLibrary; - protected void startCacheServiceLoader(final FederatedStoreProperties properties) { - if (isCachePropertiesSet(properties)) { + protected void startCacheServiceLoader() throws StorageException { + if (CacheServiceLoader.isEnabled()) { isCacheEnabled = true; - initCache(properties); makeAllGraphsFromCache(); } } @@ -74,7 +74,7 @@ protected void startCacheServiceLoader(final FederatedStoreProperties properties * @param access access required to for the graphs, can't be null * @see #put(Graph, FederatedAccess) */ - public void put(final Collection graphs, final FederatedAccess access) { + public void put(final Collection graphs, final FederatedAccess access) throws StorageException { for (final Graph graph : graphs) { put(graph, access); } @@ -90,23 +90,36 @@ public void put(final Collection graphs, final FederatedAccess access) { * @param graph the graph to add to the storage. * @param access access required to for the graph. */ - public void put(final Graph graph, final FederatedAccess access) { - if (exists(graph.getGraphId())) { - throw new OverwritingException((String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, graph.getGraphId()))); - } else if (null == access) { - throw new IllegalArgumentException(ACCESS_IS_NULL); - } + public void put(final Graph graph, final FederatedAccess access) throws StorageException { + try { + if (exists(graph.getGraphId())) { + throw new OverwritingException((String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, graph.getGraphId()))); + } else if (null == access) { + throw new IllegalArgumentException(ACCESS_IS_NULL); + } - if (isCacheEnabled()) { - addToCache(graph, access); - } + if (isCacheEnabled()) { + addToCache(graph, access); + } - Set existingGraphs = storage.get(access); - if (null == existingGraphs) { - existingGraphs = Sets.newHashSet(graph); - storage.put(access, existingGraphs); - } else { - existingGraphs.add(graph); + Set existingGraphs = storage.get(access); + if (null == existingGraphs) { + existingGraphs = Sets.newHashSet(graph); + storage.put(access, existingGraphs); + } else { + existingGraphs.add(graph); + } + + if (null != graphLibrary) { + try { + graphLibrary.add(graph.getGraphId(), graph.getSchema(), graph.getStoreProperties()); + } catch (final Exception e) { + remove(graph.getGraphId(), new User(access.getAddingUserId())); + throw e; + } + } + } catch (final Exception e) { + throw new StorageException("Error adding graph " + graph.getGraphId() + " to storage due to: " + e.getMessage(), e); } } @@ -155,9 +168,7 @@ public boolean remove(final String graphId, final User user) { for (final Graph graph : graphs) { if (graph.getGraphId().equals(graphId)) { graphs.remove(graph); - if (isCacheEnabled()) { - federatedStoreCache.deleteFromCache(graphId); - } + deleteFromCache(graphId); isRemoved = true; } } @@ -167,6 +178,12 @@ public boolean remove(final String graphId, final User user) { return isRemoved; } + private void deleteFromCache(final String graphId) { + if (isCacheEnabled()) { + federatedStoreCache.deleteFromCache(graphId); + } + } + /** * returns all graphs objects matching the given graphIds, that is visible * to the user. @@ -331,7 +348,7 @@ private void addToCache(final Graph newGraph, final FederatedAccess access) { } private void validateSameAsFromCache(final Graph newGraph, final String graphId) { - final Graph fromCache = federatedStoreCache.getGraphSerialisableFromCache(graphId).getGraph(library); + final Graph fromCache = federatedStoreCache.getGraphSerialisableFromCache(graphId).getGraph(graphLibrary); if (!newGraph.getStoreProperties().getProperties().equals(fromCache.getStoreProperties().getProperties())) { throw new RuntimeException(String.format(ERROR_ADDING_GRAPH_TO_CACHE, GraphConfigEnum.PROPERTIES.toString(), graphId)); } else { @@ -345,6 +362,9 @@ private void validateSameAsFromCache(final Graph newGraph, final String graphId) } } + public void setGraphLibrary(final GraphLibrary graphLibrary) { + this.graphLibrary = graphLibrary; + } /** * Enum for the Graph Properties or Schema @@ -375,24 +395,14 @@ private Boolean isCacheEnabled() { return rtn; } - private void makeGraphFromCache(final String graphId) { + private void makeGraphFromCache(final String graphId) throws StorageException { final GraphSerialisable serialisable = federatedStoreCache.getGraphSerialisableFromCache(graphId); - final Graph graph = serialisable.getGraph(library); + final Graph graph = serialisable.getGraph(graphLibrary); final FederatedAccess accessFromCache = federatedStoreCache.getAccessFromCache(graphId); put(graph, accessFromCache); } - private boolean isCachePropertiesSet(final FederatedStoreProperties properties) { - return properties.getCacheProperties() != null; - } - - private void initCache(final FederatedStoreProperties properties) { - if (federatedStoreCache.getCache() == null) { - CacheServiceLoader.initialise(properties.getProperties()); - } - } - - private void makeAllGraphsFromCache() { + private void makeAllGraphsFromCache() throws StorageException { final Set allGraphIds = federatedStoreCache.getAllGraphIds(); for (final String graphId : allGraphIds) { makeGraphFromCache(graphId); diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 66ed6614264..4700b2833c8 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -22,6 +22,7 @@ import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; import uk.gov.gchq.gaffer.data.element.Element; import uk.gov.gchq.gaffer.data.element.id.EntityId; +import uk.gov.gchq.gaffer.federatedstore.exception.StorageException; import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; import uk.gov.gchq.gaffer.federatedstore.operation.FederatedOperationChain; import uk.gov.gchq.gaffer.federatedstore.operation.GetAllGraphIds; @@ -57,6 +58,7 @@ import uk.gov.gchq.gaffer.store.StoreException; import uk.gov.gchq.gaffer.store.StoreProperties; import uk.gov.gchq.gaffer.store.StoreTrait; +import uk.gov.gchq.gaffer.store.library.GraphLibrary; import uk.gov.gchq.gaffer.store.operation.GetSchema; import uk.gov.gchq.gaffer.store.operation.OperationChainValidator; import uk.gov.gchq.gaffer.store.operation.handler.OperationHandler; @@ -78,7 +80,7 @@ * A Store that encapsulates a collection of sub-graphs and executes operations * against them and returns results as though it was a single graph. *

- *

+ *

* To create a FederatedStore you need to initialise the store with a * graphId and (if graphId is not known by the {@link uk.gov.gchq.gaffer.store.library.GraphLibrary}) * the {@link Schema} and {@link StoreProperties}. @@ -109,6 +111,12 @@ public void initialise(final String graphId, final Schema unused, final StorePro isPublicAccessAllowed = Boolean.valueOf(getProperties().getIsPublicAccessAllowed()); } + @Override + public void setGraphLibrary(final GraphLibrary library) { + super.setGraphLibrary(library); + graphStorage.setGraphLibrary(library); + } + /** * Get this Store's {@link uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties}. * @@ -161,20 +169,20 @@ public static OP updateOperationForGraph(final OP operati * @param graphAuths the access auths for the graph being added */ - public void addGraphs(final Set graphAuths, final String addingUserId, final boolean isPublic, final Graph... graphs) { + public void addGraphs(final Set graphAuths, final String addingUserId, final boolean isPublic, final Graph... graphs) throws StorageException { FederatedAccess access = new FederatedAccess(graphAuths, addingUserId, isPublicAccessAllowed && isPublic); addGraphs(access, graphs); } - public void addGraphs(final FederatedAccess access, final Graph... graphs) { + public void addGraphs(final FederatedAccess access, final Graph... graphs) throws StorageException { for (final Graph graph : graphs) { _add(graph, access); } } @Deprecated - public void addGraphs(final Set graphAuths, final String addingUserId, final Graph... graphs) { + public void addGraphs(final Set graphAuths, final String addingUserId, final Graph... graphs) throws StorageException { addGraphs(graphAuths, addingUserId, false, graphs); } @@ -343,8 +351,13 @@ protected Object doUnhandledOperation(final Operation operation, } @Override - protected void startCacheServiceLoader(final StoreProperties unused) { - graphStorage.startCacheServiceLoader(getProperties()); + protected void startCacheServiceLoader(final StoreProperties properties) { + super.startCacheServiceLoader(properties); + try { + graphStorage.startCacheServiceLoader(); + } catch (final StorageException e) { + throw new RuntimeException(e.getMessage(), e); + } } private Set getCustomPropertiesAuths() { @@ -352,11 +365,7 @@ private Set getCustomPropertiesAuths() { return (Strings.isNullOrEmpty(value)) ? null : Sets.newHashSet(getCleanStrings(value)); } - private void _add(final Graph newGraph, final FederatedAccess access) { + private void _add(final Graph newGraph, final FederatedAccess access) throws StorageException { graphStorage.put(newGraph, access); - - if (null != getGraphLibrary()) { - getGraphLibrary().add(newGraph.getGraphId(), newGraph.getSchema(), newGraph.getStoreProperties()); - } } } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java index fb707709509..198561e836f 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreConstants.java @@ -19,10 +19,9 @@ import uk.gov.gchq.gaffer.operation.Operation; public final class FederatedStoreConstants { - public static final String PREFIX_GAFFER_FEDERATED_STORE = "gaffer.federatedstore"; // Operation options - public static final String KEY_OPERATION_OPTIONS_GRAPH_IDS = PREFIX_GAFFER_FEDERATED_STORE + ".operation.graphIds"; - public static final String KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE = PREFIX_GAFFER_FEDERATED_STORE + ".operation.skipFailedFederatedStoreExecute"; + public static final String KEY_OPERATION_OPTIONS_GRAPH_IDS = "gaffer.federatedstore.operation.graphIds"; + public static final String KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE = "gaffer.federatedstore.operation.skipFailedFederatedStoreExecute"; public static final String DEFAULT_VALUE_KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE = String.valueOf(false); public static final String DEFAULT_VALUE_IS_PUBLIC = String.valueOf(false); diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/exception/StorageException.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/exception/StorageException.java new file mode 100644 index 00000000000..a3e25dfb43a --- /dev/null +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/exception/StorageException.java @@ -0,0 +1,46 @@ +/* + * Copyright 2017 Crown Copyright + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.gchq.gaffer.federatedstore.exception; + +import uk.gov.gchq.gaffer.core.exception.GafferCheckedException; + +/** + * An {@code StorageException} should be thrown when a problem occurs accessing + * the federated graph storage. + */ +public class StorageException extends GafferCheckedException { + private static final long serialVersionUID = -1391854996347271983L; + + /** + * Constructs a new storage exception with the specified detail message. + * + * @param message Storage exception detail message. + */ + public StorageException(final String message) { + super(message); + } + + /** + * Constructs a new storage exception with the specified detail message and cause. + * + * @param message Storage exception detail message. + * @param cause Storage exception cause. + */ + public StorageException(final String message, final Throwable cause) { + super(message, cause); + } +} diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java index 060848f55f1..4458249aa23 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandler.java @@ -17,6 +17,7 @@ package uk.gov.gchq.gaffer.federatedstore.operation.handler.impl; import uk.gov.gchq.gaffer.federatedstore.FederatedStore; +import uk.gov.gchq.gaffer.federatedstore.exception.StorageException; import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; import uk.gov.gchq.gaffer.graph.Graph; import uk.gov.gchq.gaffer.operation.OperationException; @@ -35,8 +36,8 @@ */ public class FederatedAddGraphHandler implements OperationHandler { - public static final String ERROR_BUILDING_GRAPH_GRAPH_ID_S = "Error building graph. graphId: %s"; - public static final String ERROR_ADDING_GRAPH_GRAPH_ID_S = "Error adding graph. graphId: %s"; + public static final String ERROR_BUILDING_GRAPH_GRAPH_ID_S = "Error building graph %s"; + public static final String ERROR_ADDING_GRAPH_GRAPH_ID_S = "Error adding graph %s"; public static final String USER_IS_LIMITED_TO_ONLY_USING_PARENT_PROPERTIES_ID_FROM_GRAPHLIBRARY_BUT_FOUND_STORE_PROPERTIES_S = "User is limited to only using parentPropertiesId from the graphLibrary, but found storeProperties: %s"; @Override @@ -59,6 +60,8 @@ public Void doOperation(final AddGraph operation, final Context context, final S try { ((FederatedStore) store).addGraphs(operation.getGraphAuths(), context.getUser().getUserId(), operation.getIsPublic(), graph); + } catch (final StorageException e) { + throw new OperationException(e.getMessage(), e); } catch (final Exception e) { throw new OperationException(String.format(ERROR_ADDING_GRAPH_GRAPH_ID_S, operation.getGraphId()), e); } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreMultiCacheTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreMultiCacheTest.java index bc1e8f5bf67..d6060a4426f 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreMultiCacheTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreMultiCacheTest.java @@ -16,10 +16,12 @@ package uk.gov.gchq.gaffer.federatedstore; +import org.junit.After; import org.junit.Before; import org.junit.Test; import uk.gov.gchq.gaffer.cache.CacheServiceLoader; +import uk.gov.gchq.gaffer.cache.impl.HashMapCacheService; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; import uk.gov.gchq.gaffer.mapstore.MapStoreProperties; @@ -54,6 +56,7 @@ public void setUp() throws Exception { CacheServiceLoader.shutdown(); federatedStoreProperties = new FederatedStoreProperties(); federatedStoreProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); + federatedStoreProperties.set(HashMapCacheService.STATIC_CACHE, String.valueOf(true)); store = new FederatedStore(); store.initialise(FEDERATED_STORE_ID, null, federatedStoreProperties); store.execute(new AddGraph.Builder() @@ -72,6 +75,12 @@ public void setUp() throws Exception { blankUser = FederatedStoreUser.blankUser(); } + @After + public void after() { + HashMapGraphLibrary.clear(); + CacheServiceLoader.shutdown(); + } + @Test public void shouldInitialiseByCacheToContainSameGraphsForAddingUser() throws Exception { originalStoreIds = store.getAllGraphIds(testUser); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index 6f81cf12bb8..3e12dcf58c9 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -24,6 +24,7 @@ import org.mockito.Mockito; import uk.gov.gchq.gaffer.cache.CacheServiceLoader; +import uk.gov.gchq.gaffer.cache.impl.HashMapCacheService; import uk.gov.gchq.gaffer.commonutil.CommonConstants; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable; @@ -107,6 +108,7 @@ public class FederatedStoreTest { public void setUp() throws Exception { clearCache(); federatedProperties = new FederatedStoreProperties(); + federatedProperties.set(HashMapCacheService.STATIC_CACHE, String.valueOf(true)); clearLibrary(); library = new HashMapGraphLibrary(); @@ -135,9 +137,10 @@ public void tearDown() throws Exception { assertEquals("Library has changed: " + ID_PROPS_MAP_ALT, library.getProperties(ID_PROPS_MAP_ALT), getPropertiesFromPath(PATH_MAP_STORE_PROPERTIES_ALT)); assertEquals("Library has changed: " + ID_SCHEMA_EDGE, new String(library.getSchema(ID_SCHEMA_EDGE).toJson(false), CommonConstants.UTF_8), new String(getSchemaFromPath(PATH_BASIC_EDGE_SCHEMA_JSON).toJson(false), CommonConstants.UTF_8)); assertEquals("Library has changed: " + ID_SCHEMA_ENTITY, new String(library.getSchema(ID_SCHEMA_ENTITY).toJson(false), CommonConstants.UTF_8), new String(getSchemaFromPath(PATH_BASIC_ENTITY_SCHEMA_JSON).toJson(false), CommonConstants.UTF_8)); + clearLibrary(); + clearCache(); } - @Test public void shouldLoadGraphsWithIds() throws Exception { // When @@ -635,7 +638,7 @@ public void shouldFederatedIfUserHasCorrectAuths() throws Exception { } @Test - public void shouldReturnSpecificGraphsFromCSVString() throws StoreException { + public void shouldReturnSpecificGraphsFromCSVString() throws Exception { // Given final List> graphLists = populateGraphs(1, 2, 4); @@ -652,7 +655,7 @@ public void shouldReturnSpecificGraphsFromCSVString() throws StoreException { } @Test - public void shouldReturnNoGraphsFromEmptyString() throws StoreException { + public void shouldReturnNoGraphsFromEmptyString() throws Exception { // Given final List> graphLists = populateGraphs(); @@ -667,7 +670,7 @@ public void shouldReturnNoGraphsFromEmptyString() throws StoreException { } @Test - public void shouldReturnGraphsWithLeadingCommaString() throws StoreException { + public void shouldReturnGraphsWithLeadingCommaString() throws Exception { // Given final List> graphLists = populateGraphs(2, 4); final Collection expectedGraphs = graphLists.get(0); @@ -806,7 +809,7 @@ public void shouldThrowWithSchemaErrorFromGraphLibrary() throws Exception { } @Test - public void shouldReturnASingleGraph() throws StoreException { + public void shouldReturnASingleGraph() throws Exception { // Given final List> graphLists = populateGraphs(1); final Collection expectedGraphs = graphLists.get(0); @@ -861,7 +864,7 @@ public void shouldReuseGraphsAlreadyInCache() throws Exception { store.initialise(FEDERATED_STORE_ID, null, federatedProperties); //check the graph is already in there from the cache - assertTrue(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(MAP_ID_1)); + assertTrue("Keys: " + CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME) + " did not contain " + MAP_ID_1, CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME).contains(MAP_ID_1)); assertEquals(1, store.getAllGraphIds(blankUser).size()); } @@ -911,7 +914,7 @@ public void shouldNotThrowExceptionWhenInitialisedWithNoCacheClassInProperties() } @Test - public void shouldAddGraphsToCache() throws StoreException { + public void shouldAddGraphsToCache() throws Exception { federatedProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); @@ -944,7 +947,7 @@ public void shouldAddGraphsToCache() throws StoreException { } @Test - public void shouldAddMultipleGraphsToCache() throws StoreException { + public void shouldAddMultipleGraphsToCache() throws Exception { federatedProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING); store.initialise(FEDERATED_STORE_ID, null, federatedProperties); // Given @@ -985,7 +988,7 @@ private boolean checkUnexpected(final Collection unexpectedGraphs, final return false; } - private List> populateGraphs(int... expectedIds) throws StoreException { + private List> populateGraphs(int... expectedIds) throws Exception { final Collection expectedGraphs = new ArrayList<>(); final Collection unexpectedGraphs = new ArrayList<>(); for (int i = 0; i < 5; i++) { @@ -1025,8 +1028,9 @@ private Set getElements() throws uk.gov.gchq.gaffer.operation.Operation } private void assertContains(final Throwable e, final String format, final String... s) { - boolean contains = e.getMessage().contains(String.format(format, s)); - assertTrue(e.getMessage(), contains); + final String expectedStr = String.format(format, s); + boolean contains = e.getMessage().contains(expectedStr); + assertTrue("\"" + e.getMessage() + "\" does not contain string \"" + expectedStr + "\"", contains); } private void addGraphWithIds(final String graphId, final String propertiesId, final String... schemaId) throws OperationException { diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/PredefinedFederatedStore.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/PredefinedFederatedStore.java index 7fb27881d2d..955f29c95f0 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/PredefinedFederatedStore.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/PredefinedFederatedStore.java @@ -16,11 +16,14 @@ package uk.gov.gchq.gaffer.federatedstore; +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.commonutil.StreamUtil; +import uk.gov.gchq.gaffer.federatedstore.exception.StorageException; import uk.gov.gchq.gaffer.graph.Graph; import uk.gov.gchq.gaffer.graph.GraphConfig; import uk.gov.gchq.gaffer.store.StoreException; import uk.gov.gchq.gaffer.store.StoreProperties; +import uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary; import uk.gov.gchq.gaffer.store.schema.Schema; import uk.gov.gchq.gaffer.user.User; @@ -33,26 +36,33 @@ public class PredefinedFederatedStore extends FederatedStore { @Override public void initialise(final String graphId, final Schema schema, final StoreProperties properties) throws StoreException { + HashMapGraphLibrary.clear(); + CacheServiceLoader.shutdown(); + super.initialise(graphId, schema, properties); // Accumulo store just contains edges - addGraphs(null, User.UNKNOWN_USER_ID, false, new Graph.Builder() - .config(new GraphConfig(ACCUMULO_GRAPH)) - .addSchema(new Schema.Builder() - .merge(schema.clone()) - .entities(Collections.emptyMap()) - .build()) - .storeProperties(StreamUtil.openStream(getClass(), "properties/singleUseMockAccStore.properties")) - .build()); - - // Map store just contains entities - addGraphs(null, User.UNKNOWN_USER_ID, false, new Graph.Builder() - .config(new GraphConfig(MAP_GRAPH)) - .addSchema(new Schema.Builder() - .merge(schema.clone()) - .edges(Collections.emptyMap()) - .build()) - .storeProperties(StreamUtil.openStream(getClass(), "properties/singleUseMockMapStore.properties")) - .build()); + try { + addGraphs(null, User.UNKNOWN_USER_ID, false, new Graph.Builder() + .config(new GraphConfig(ACCUMULO_GRAPH)) + .addSchema(new Schema.Builder() + .merge(schema.clone()) + .entities(Collections.emptyMap()) + .build()) + .storeProperties(StreamUtil.openStream(getClass(), "properties/singleUseMockAccStore.properties")) + .build()); + + // Map store just contains entities + addGraphs(null, User.UNKNOWN_USER_ID, false, new Graph.Builder() + .config(new GraphConfig(MAP_GRAPH)) + .addSchema(new Schema.Builder() + .merge(schema.clone()) + .edges(Collections.emptyMap()) + .build()) + .storeProperties(StreamUtil.openStream(getClass(), "properties/singleUseMockMapStore.properties")) + .build()); + } catch (final StorageException e) { + throw new StoreException(e.getMessage(), e); + } } } \ No newline at end of file diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedGetSchemaHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedGetSchemaHandlerTest.java index 6a9b6e57d23..d915b577cd7 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedGetSchemaHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedGetSchemaHandlerTest.java @@ -16,11 +16,15 @@ package uk.gov.gchq.gaffer.federatedstore.operation.handler; import com.google.common.collect.Lists; +import org.junit.After; import org.junit.Before; import org.junit.Test; import uk.gov.gchq.gaffer.accumulostore.AccumuloProperties; import uk.gov.gchq.gaffer.accumulostore.MockAccumuloStore; +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; +import uk.gov.gchq.gaffer.cache.impl.HashMapCacheService; +import uk.gov.gchq.gaffer.commonutil.JsonAssert; import uk.gov.gchq.gaffer.federatedstore.FederatedStore; import uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties; import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph; @@ -38,7 +42,6 @@ import uk.gov.gchq.gaffer.user.User; import uk.gov.gchq.koryphe.impl.binaryoperator.StringConcat; -import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -64,10 +67,15 @@ public class FederatedGetSchemaHandlerTest { @Before public void setup() throws StoreException { + HashMapGraphLibrary.clear(); + CacheServiceLoader.shutdown(); + handler = new FederatedGetSchemaHandler(); user = new User("testUser"); context = new Context(user); properties = new FederatedStoreProperties(); + properties.set(HashMapCacheService.STATIC_CACHE, String.valueOf(true)); + accProperties = new AccumuloProperties(); accProperties.setStoreClass(MockAccumuloStore.class); @@ -79,6 +87,12 @@ public void setup() throws StoreException { library.clear(); } + @After + public void after() { + HashMapGraphLibrary.clear(); + CacheServiceLoader.shutdown(); + } + @Test public void shouldReturnSchema() throws OperationException { library.addProperties(ACC_PROP_ID, accProperties); @@ -112,7 +126,7 @@ public void shouldReturnSchema() throws OperationException { // Then assertNotNull(result); - assertArrayEquals(edgeSchema.toJson(true), result.toJson(true)); + JsonAssert.assertEquals(edgeSchema.toJson(true), result.toJson(true)); } @Test diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java index 31104ff9408..27f5456e386 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java @@ -209,9 +209,8 @@ public void shouldNotOverwriteGraph() throws Exception { store); fail(EXCEPTION_EXPECTED); } catch (final Exception e) { - assertEquals(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, EXPECTED_GRAPH_ID), e.getCause().getMessage()); + assertTrue(e.getMessage().contains(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, EXPECTED_GRAPH_ID))); } - } @Test diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedOperationChainHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedOperationChainHandlerTest.java index 7112b1a2ee5..5ed62b1abb2 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedOperationChainHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedOperationChainHandlerTest.java @@ -17,8 +17,11 @@ package uk.gov.gchq.gaffer.federatedstore.operation.handler.impl; import com.google.common.collect.Lists; +import org.junit.After; +import org.junit.Before; import org.junit.Test; +import uk.gov.gchq.gaffer.cache.CacheServiceLoader; import uk.gov.gchq.gaffer.commonutil.StreamUtil; import uk.gov.gchq.gaffer.commonutil.TestGroups; import uk.gov.gchq.gaffer.commonutil.TestTypes; @@ -40,6 +43,7 @@ import uk.gov.gchq.gaffer.store.Context; import uk.gov.gchq.gaffer.store.Store; import uk.gov.gchq.gaffer.store.StoreProperties; +import uk.gov.gchq.gaffer.store.library.HashMapGraphLibrary; import uk.gov.gchq.gaffer.store.schema.Schema; import uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition; import uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition; @@ -82,6 +86,12 @@ public class FederatedOperationChainHandlerTest { .build() }; + @Before + @After + public void after() { + HashMapGraphLibrary.clear(); + CacheServiceLoader.shutdown(); + } @Test public void shouldHandleChainWithoutSpecialFederation() throws OperationException { From 261cfc518900343118e03bc63ab6ec48aaff63d7 Mon Sep 17 00:00:00 2001 From: m55624 Date: Wed, 1 Nov 2017 17:08:54 +0000 Subject: [PATCH 64/72] gh-1297 - AccumuloStore constructor fixes --- .../gov/gchq/gaffer/accumulostore/AccumuloProperties.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/store-implementation/accumulo-store/src/main/java/uk/gov/gchq/gaffer/accumulostore/AccumuloProperties.java b/store-implementation/accumulo-store/src/main/java/uk/gov/gchq/gaffer/accumulostore/AccumuloProperties.java index 1a690d0c614..b237923e10c 100644 --- a/store-implementation/accumulo-store/src/main/java/uk/gov/gchq/gaffer/accumulostore/AccumuloProperties.java +++ b/store-implementation/accumulo-store/src/main/java/uk/gov/gchq/gaffer/accumulostore/AccumuloProperties.java @@ -65,15 +65,11 @@ public class AccumuloProperties extends StoreProperties { public static final String ENABLE_VALIDATOR_ITERATOR_DEFAULT = "true"; public AccumuloProperties() { - super(); - setStoreClass(AccumuloStore.class); + super(AccumuloStore.class); } public AccumuloProperties(final Path propFileLocation) { - super(propFileLocation); - if (null == getStoreClass()) { - setStoreClass(AccumuloStore.class); - } + super(propFileLocation, AccumuloStore.class); } public static AccumuloProperties loadStoreProperties(final String pathStr) { From c2faf2b574ec8d28db6f35f82ca26f5f1efd44d5 Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Thu, 2 Nov 2017 17:35:43 +0000 Subject: [PATCH 65/72] gh-1297 Storage ignores adding duplicate graph and access and throws exceptions if either is different. --- core/cache/pom.xml | 5 + .../gchq/gaffer/cache/impl/HashMapCache.java | 49 +++++++++- .../cache/impl/HashMapCacheService.java | 15 ++- .../gaffer/cache/impl/HashMapCacheTest.java | 41 ++++++++ .../gaffer/operation/OperationException.java | 4 + .../federatedstore/FederatedAccess.java | 38 +++++-- .../federatedstore/FederatedGraphStorage.java | 98 ++++++++++++++----- .../exception/StorageException.java | 4 + .../FederatedOperationOutputHandler.java | 6 +- .../FederatedAccessAuthTest.java | 12 +++ .../FederatedGraphStorageTest.java | 33 ++++++- .../federatedstore/FederatedStoreTest.java | 8 +- .../FederatedOperationOutputHandlerTest.java | 29 ++++++ .../impl/FederatedAddGraphHandlerTest.java | 73 +++++++++++++- 14 files changed, 364 insertions(+), 51 deletions(-) diff --git a/core/cache/pom.xml b/core/cache/pom.xml index db569a5a0d8..43bd0a76960 100644 --- a/core/cache/pom.xml +++ b/core/cache/pom.xml @@ -15,6 +15,11 @@ exception ${project.parent.version} + + uk.gov.gchq.gaffer + serialisation + ${project.parent.version} + \ No newline at end of file diff --git a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCache.java b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCache.java index 2b3ce984c22..d764ff80b51 100644 --- a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCache.java +++ b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCache.java @@ -16,8 +16,13 @@ package uk.gov.gchq.gaffer.cache.impl; +import com.google.common.collect.Lists; + import uk.gov.gchq.gaffer.cache.ICache; +import uk.gov.gchq.gaffer.exception.SerialisationException; +import uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Set; @@ -30,17 +35,40 @@ * @param The value that is stored in the HashMap */ public class HashMapCache implements ICache { + private static final JavaSerialiser JAVA_SERIALISER = new JavaSerialiser(); + private boolean useJavaSerialisation; + private HashMap cache = new HashMap<>(); + + public HashMapCache(final boolean useJavaSerialisation) { + this.useJavaSerialisation = useJavaSerialisation; + } - private final HashMap cache = new HashMap<>(); + public HashMapCache() { + this(false); + } @Override public V get(final K key) { - return cache.get(key); + try { + return (V) (useJavaSerialisation + ? JAVA_SERIALISER.deserialise((byte[]) cache.get(key)) + : cache.get(key)); + } catch (final SerialisationException e) { + throw new RuntimeException(e); + } } @Override public void put(final K key, final V value) { - cache.put(key, value); + if (useJavaSerialisation) { + try { + cache.put(key, JAVA_SERIALISER.serialise(value)); + } catch (final SerialisationException e) { + throw new RuntimeException(e); + } + } else { + cache.put(key, value); + } } @Override @@ -50,7 +78,20 @@ public void remove(final K key) { @Override public Collection getAllValues() { - return cache.values(); + ArrayList rtn = Lists.newArrayList(); + if (useJavaSerialisation) { + cache.values() + .forEach((Object o) -> { + try { + rtn.add((V) JAVA_SERIALISER.deserialise((byte[]) o)); + } catch (final SerialisationException e) { + throw new RuntimeException(e); + } + }); + } else { + rtn.addAll((Collection) cache.values()); + } + return rtn; } @Override diff --git a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheService.java b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheService.java index 92c12968132..784c137ca75 100644 --- a/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheService.java +++ b/core/cache/src/main/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheService.java @@ -28,16 +28,21 @@ */ public class HashMapCacheService implements ICacheService { public static final String STATIC_CACHE = "gaffer.cache.hashmap.static"; - - private static final HashMap staticCaches = new HashMap<>(); + public static final String JAVA_SERIALISATION_CACHE = "gaffer.cache.hashmap.useJavaSerialisation"; + private static final HashMap STATIC_CACHES = new HashMap<>(); private final HashMap nonStaticCaches = new HashMap<>(); + private boolean useJavaSerialisation = false; private HashMap caches = nonStaticCaches; @Override public void initialise(final Properties properties) { - if (Boolean.parseBoolean(properties.getProperty(STATIC_CACHE))) { - caches = staticCaches; + if (properties != null) { + useJavaSerialisation = Boolean.parseBoolean(properties.getProperty(JAVA_SERIALISATION_CACHE)); + } + + if (properties != null && Boolean.parseBoolean(properties.getProperty(STATIC_CACHE))) { + caches = STATIC_CACHES; } else { caches = nonStaticCaches; } @@ -50,7 +55,7 @@ public void shutdown() { @Override public ICache getCache(final String cacheName) { - HashMapCache cache = caches.computeIfAbsent(cacheName, k -> new HashMapCache<>()); + HashMapCache cache = caches.computeIfAbsent(cacheName, k -> new HashMapCache<>(useJavaSerialisation)); return cache; } diff --git a/core/cache/src/test/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheTest.java b/core/cache/src/test/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheTest.java index b897092493d..d11c41a2a12 100644 --- a/core/cache/src/test/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheTest.java +++ b/core/cache/src/test/java/uk/gov/gchq/gaffer/cache/impl/HashMapCacheTest.java @@ -22,7 +22,11 @@ import org.junit.Assert; import org.junit.Test; +import uk.gov.gchq.gaffer.exception.SerialisationException; + import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class HashMapCacheTest { @@ -117,4 +121,41 @@ public void shouldGetAllValues() { Assert.assertThat(cache.getAllValues(), IsCollectionContaining.hasItems(1, 2, 3)); } + + + @Test + public void shouldThrowForNonJavaSerialisable() throws Exception { + + HashMapCache map = new HashMapCache<>(true); + + String s = "hello"; + map.put("test1", s); + + class TempClass { + } + + TempClass tempClass = new TempClass(); + + try { + map.put("test1", tempClass); + fail("Exception expected"); + } catch (final Exception e) { + assertTrue(e.getCause() instanceof SerialisationException); + } + } + + + @Test + public void shouldNotThrowForNonJavaSerialisable() throws Exception { + + HashMapCache map = new HashMapCache<>(false); + + map.put("test1", "hello"); + + class TempClass { + } + + TempClass tempClass = new TempClass(); + map.put("test1", tempClass); + } } diff --git a/core/operation/src/main/java/uk/gov/gchq/gaffer/operation/OperationException.java b/core/operation/src/main/java/uk/gov/gchq/gaffer/operation/OperationException.java index bd5322adda2..b3c85063883 100644 --- a/core/operation/src/main/java/uk/gov/gchq/gaffer/operation/OperationException.java +++ b/core/operation/src/main/java/uk/gov/gchq/gaffer/operation/OperationException.java @@ -27,6 +27,10 @@ public class OperationException extends GafferCheckedException { private static final long serialVersionUID = 3855512637690609379L; + public OperationException(final Throwable cause) { + super(cause); + } + public OperationException(final String message) { super(message, INTERNAL_SERVER_ERROR); } diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java index ef458530aae..8d6dc4512d7 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java @@ -16,7 +16,10 @@ package uk.gov.gchq.gaffer.federatedstore; +import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import uk.gov.gchq.gaffer.user.User; @@ -65,8 +68,8 @@ public class FederatedAccess implements Serializable { private String addingUserId; public FederatedAccess(final Set graphAuths, final String addingUserId) { - this.graphAuths = graphAuths; - this.addingUserId = addingUserId; + setGraphAuths(graphAuths); + setAddingUserId(addingUserId); } public FederatedAccess(final Set graphAuths, final String addingUser, final boolean isPublic) { @@ -121,6 +124,30 @@ public void setGraphAuths(final Set graphAuths) { this.graphAuths = graphAuths; } + @Override + public boolean equals(final Object o) { + if (this == o) return true; + + if (o == null || getClass() != o.getClass()) return false; + + final FederatedAccess that = (FederatedAccess) o; + + return new EqualsBuilder() + .append(isPublic, that.isPublic) + .append(graphAuths, that.graphAuths) + .append(addingUserId, that.addingUserId) + .isEquals(); + } + + @Override + public int hashCode() { + return new HashCodeBuilder(17, 37) + .append(isPublic) + .append(graphAuths) + .append(addingUserId) + .toHashCode(); + } + public static class Builder { private String addingUser; private Set graphAuths; @@ -141,9 +168,7 @@ public Builder graphAuths(final Collection graphAuths) { this.graphAuths = null; } else { final HashSet authSet = Sets.newHashSet(graphAuths); - authSet.remove(null); - authSet.remove(""); - + authSet.removeAll(Lists.newArrayList("", null)); this.graphAuths = authSet; } return self; @@ -152,8 +177,7 @@ public Builder graphAuths(final Collection graphAuths) { public Builder addGraphAuths(final Collection graphAuths) { if (null != graphAuths) { final HashSet authSet = Sets.newHashSet(graphAuths); - authSet.remove(null); - authSet.remove(""); + authSet.removeAll(Lists.newArrayList("", null)); if (null == this.graphAuths) { this.graphAuths = authSet; } else { diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java index b6ea002e916..6f1d014f2cc 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java @@ -23,10 +23,12 @@ import uk.gov.gchq.gaffer.commonutil.JsonUtil; import uk.gov.gchq.gaffer.commonutil.exception.OverwritingException; import uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException; +import uk.gov.gchq.gaffer.exception.SerialisationException; import uk.gov.gchq.gaffer.federatedstore.exception.StorageException; import uk.gov.gchq.gaffer.federatedstore.util.FederatedStoreUtil; import uk.gov.gchq.gaffer.graph.Graph; import uk.gov.gchq.gaffer.graph.GraphSerialisable; +import uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser; import uk.gov.gchq.gaffer.operation.OperationException; import uk.gov.gchq.gaffer.store.Context; import uk.gov.gchq.gaffer.store.library.GraphLibrary; @@ -38,6 +40,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -91,38 +94,39 @@ public void put(final Collection graphs, final FederatedAccess access) th * @param access access required to for the graph. */ public void put(final Graph graph, final FederatedAccess access) throws StorageException { + String graphId = graph.getGraphId(); try { - if (exists(graph.getGraphId())) { - throw new OverwritingException((String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, graph.getGraphId()))); - } else if (null == access) { + if (null == access) { throw new IllegalArgumentException(ACCESS_IS_NULL); } + if (!exists(graph, access)) { + if (isCacheEnabled()) { + addToCache(graph, access); + } - if (isCacheEnabled()) { - addToCache(graph, access); - } - - Set existingGraphs = storage.get(access); - if (null == existingGraphs) { - existingGraphs = Sets.newHashSet(graph); - storage.put(access, existingGraphs); - } else { - existingGraphs.add(graph); - } + Set existingGraphs = storage.get(access); + if (null == existingGraphs) { + existingGraphs = Sets.newHashSet(graph); + storage.put(access, existingGraphs); + } else { + existingGraphs.add(graph); + } - if (null != graphLibrary) { - try { - graphLibrary.add(graph.getGraphId(), graph.getSchema(), graph.getStoreProperties()); - } catch (final Exception e) { - remove(graph.getGraphId(), new User(access.getAddingUserId())); - throw e; + if (null != graphLibrary) { + try { + graphLibrary.add(graphId, graph.getSchema(), graph.getStoreProperties()); + } catch (final Exception e) { + remove(graphId, new User(access.getAddingUserId())); + throw e; + } } } } catch (final Exception e) { - throw new StorageException("Error adding graph " + graph.getGraphId() + " to storage due to: " + e.getMessage(), e); + throw new StorageException("Error adding graph " + graphId + " to storage due to: " + e.getMessage(), e); } } + /** * Returns all the graphIds that are visible for the given user. * @@ -165,13 +169,15 @@ public boolean remove(final String graphId, final User user) { if (isValidToView(user, entry.getKey())) { final Set graphs = entry.getValue(); if (null != graphs) { + HashSet remove = Sets.newHashSet(); for (final Graph graph : graphs) { if (graph.getGraphId().equals(graphId)) { - graphs.remove(graph); + remove.add(graph); deleteFromCache(graphId); isRemoved = true; } } + graphs.removeAll(remove); } } } @@ -284,16 +290,53 @@ private void validateAllGivenGraphIdsAreVisibleForUser(final User user, final Co } } - private boolean exists(final String graphId) { + private boolean exists(final Graph graph, final FederatedAccess access) throws StorageException { + Graph found = null; + String graphId = graph.getGraphId(); + outer: for (final Set graphs : storage.values()) { - for (final Graph graph : graphs) { - if (graph.getGraphId().equals(graphId)) { - return true; + for (final Graph g : graphs) { + if (g.getGraphId().equals(graphId)) { + found = g; + break outer; } } } - return false; + boolean rtn = false; + + if (null != found) { + byte[] graphJson; + byte[] existJson; + try { + graphJson = JSONSerialiser.serialise(graph); + existJson = JSONSerialiser.serialise(found); + } catch (final SerialisationException e) { + throw new StorageException(e); + } + if (JsonUtil.equals(graphJson, existJson)) { + rtn = true; + } else { + throw new OverwritingException((String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, graphId))); + } + + Set graphs = storage.get(access); + boolean foundAccess = false; + if (null != graphs) { + for (Graph g : graphs) { + if (g.getGraphId().equals(graphId)) { + foundAccess = true; + break; + } + } + if (!foundAccess) { + throw new OverwritingException((String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, graphId))); + } + } else { + throw new OverwritingException((String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, graphId))); + } + } + return rtn; } /** @@ -382,6 +425,7 @@ public enum GraphConfigEnum { public String toString() { return value; } + } private Boolean isCacheEnabled() { diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/exception/StorageException.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/exception/StorageException.java index a3e25dfb43a..2cdb2ee599d 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/exception/StorageException.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/exception/StorageException.java @@ -25,6 +25,10 @@ public class StorageException extends GafferCheckedException { private static final long serialVersionUID = -1391854996347271983L; + public StorageException(final Throwable cause) { + super(cause); + } + /** * Constructs a new storage exception with the specified detail message. * diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandler.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandler.java index 70f14be8444..1eb4b7edb27 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandler.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandler.java @@ -62,7 +62,11 @@ public O doOperation(final OP operation, final Context context, final Store stor } } } - return mergeResults(results, operation, context, store); + try { + return mergeResults(results, operation, context, store); + } catch (final Exception e) { + throw new OperationException(e); + } } protected abstract O mergeResults(final List results, final OP operation, final Context context, final Store store); diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessAuthTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessAuthTest.java index 140eff4992b..7ee7ca8425b 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessAuthTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccessAuthTest.java @@ -16,6 +16,7 @@ package uk.gov.gchq.gaffer.federatedstore; +import com.beust.jcommander.internal.Lists; import org.junit.Before; import org.junit.Test; @@ -90,4 +91,15 @@ public void shouldInValidateUserWithMismatchedAuth() throws Exception { assertFalse(access.isValidToExecute(testUser)); } + @Test + public void shouldValidateWithListOfAuths() throws Exception { + + final FederatedAccess access = new FederatedAccess.Builder() + .addGraphAuths(Lists.newArrayList(AUTH_1)) + .addGraphAuths(Lists.newArrayList("X")) + .build(); + + assertTrue(access.isValidToExecute(authUser())); + + } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorageTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorageTest.java index 23f2ed96c03..1362691f206 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorageTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorageTest.java @@ -20,14 +20,17 @@ import com.google.common.collect.Sets; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import uk.gov.gchq.gaffer.accumulostore.AccumuloProperties; import uk.gov.gchq.gaffer.accumulostore.SingleUseMockAccumuloStore; +import uk.gov.gchq.gaffer.federatedstore.exception.StorageException; import uk.gov.gchq.gaffer.graph.Graph; import uk.gov.gchq.gaffer.graph.Graph.Builder; import uk.gov.gchq.gaffer.graph.GraphConfig; import uk.gov.gchq.gaffer.mapstore.MapStoreProperties; import uk.gov.gchq.gaffer.store.Context; +import uk.gov.gchq.gaffer.store.library.GraphLibrary; import uk.gov.gchq.gaffer.store.schema.Schema; import uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition; import uk.gov.gchq.gaffer.user.User; @@ -299,8 +302,7 @@ public void shouldNotRemoveForBlankUser() throws Exception { @Test public void shouldGetGraphsInOrder() throws Exception { // Given - graphStorage.put(a, access); - graphStorage.put(b, access); + graphStorage.put(Lists.newArrayList(a, b), access); final List configAB = Arrays.asList(a.getGraphId(), b.getGraphId()); final List configBA = Arrays.asList(b.getGraphId(), a.getGraphId()); @@ -320,4 +322,31 @@ public void shouldGetGraphsInOrder() throws Exception { assertSame(a, itrBA.next()); assertFalse(itrBA.hasNext()); } + + @Test + public void shouldNotAddGraphWhenLibraryThrowsExceptionDuringAdd() throws Exception { + //given + GraphLibrary mock = Mockito.mock(GraphLibrary.class); + String testMockException = "testMockException"; + String graphId = a.getGraphId(); + Mockito.doThrow(new RuntimeException(testMockException)) + .when(mock) + .add(graphId, a.getSchema(), a.getStoreProperties()); + graphStorage.setGraphLibrary(mock); + try { + graphStorage.put(a, access); + fail("Exception expected"); + } catch (final Exception e) { + assertTrue(e instanceof StorageException); + assertEquals(testMockException, e.getCause().getMessage()); + } + try { + //when + graphStorage.get(testUser, Lists.newArrayList(graphId)); + fail("Exception exptected"); + } catch (final IllegalArgumentException e) { + //then + assertEquals(String.format(GRAPH_IDS_NOT_VISIBLE, Arrays.toString(new String[]{graphId})), e.getMessage()); + } + } } diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java index 3e12dcf58c9..befe4ca2877 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreTest.java @@ -632,8 +632,8 @@ public void shouldFederatedIfUserHasCorrectAuths() throws Exception { .opAuths("x") .build())); fail("expected exception"); - } catch (final IllegalArgumentException e) { - assertEquals(NO_RESULTS_TO_MERGE_ERROR, e.getMessage()); + } catch (final OperationException e) { + assertEquals(NO_RESULTS_TO_MERGE_ERROR, e.getCause().getMessage()); } } @@ -745,8 +745,8 @@ public void shouldAddGraphIdWithAuths() throws Exception { .opAuths("x") .build()); fail("expected exception"); - } catch (final IllegalArgumentException e) { - assertEquals(NO_RESULTS_TO_MERGE_ERROR, e.getMessage()); + } catch (final OperationException e) { + assertEquals(NO_RESULTS_TO_MERGE_ERROR, e.getCause().getMessage()); } // Then diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandlerTest.java index fad817fc25e..f1c56e048ba 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationOutputHandlerTest.java @@ -47,6 +47,7 @@ import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS; import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreConstants.KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE; import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreUser.testUser; +import static uk.gov.gchq.gaffer.federatedstore.operation.handler.FederatedOperationOutputHandler.NO_RESULTS_TO_MERGE_ERROR; public abstract class FederatedOperationOutputHandlerTest, O> { public static final String TEST_ENTITY = "TestEntity"; @@ -169,6 +170,34 @@ final public void shouldThrowException() throws Exception { } + @Test + final public void shouldThrowExceptionNoResults() throws Exception { + // Given + final OP op = getExampleOperation(); + op.addOption(KEY_OPERATION_OPTIONS_GRAPH_IDS, TEST_GRAPH_ID); + + Schema unusedSchema = new Schema.Builder().build(); + StoreProperties storeProperties = new StoreProperties(); + + Store mockStoreInner = Mockito.mock(Store.class); + given(mockStoreInner.getSchema()).willReturn(unusedSchema); + given(mockStoreInner.getProperties()).willReturn(storeProperties); + given(mockStoreInner.execute(any(OperationChain.class), eq(context))).willReturn(null); + given(mockStoreInner.createContext(any(User.class))).willReturn(context); + FederatedStore mockStore = Mockito.mock(FederatedStore.class); + HashSet filteredGraphs = Sets.newHashSet(getGraphWithMockStore(mockStoreInner)); + Mockito.when(mockStore.getGraphs(user, TEST_GRAPH_ID)).thenReturn(filteredGraphs); + + // When + try { + getFederatedHandler().doOperation(op, context, mockStore); + fail("Exception not thrown"); + } catch (OperationException e) { + assertEquals(NO_RESULTS_TO_MERGE_ERROR, e.getCause().getMessage()); + } + + } + @Test final public void shouldNotThrowException() throws Exception { // Given diff --git a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java index 27f5456e386..06ff75a83b3 100644 --- a/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java +++ b/store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/impl/FederatedAddGraphHandlerTest.java @@ -178,7 +178,7 @@ public void shouldAddGraphUsingLibrary() throws Exception { } @Test - public void shouldNotOverwriteGraph() throws Exception { + public void shouldThrowWhenOverwriteGraphIsDifferent() throws Exception { Schema expectedSchema = new Schema.Builder().build(); MapStoreProperties storeProperties = new MapStoreProperties(); @@ -203,6 +203,9 @@ public void shouldNotOverwriteGraph() throws Exception { new AddGraph.Builder() .graphId(EXPECTED_GRAPH_ID) .schema(expectedSchema) + .schema(new Schema.Builder() + .type("unusual", String.class) + .build()) .storeProperties(storeProperties) .build(), new Context(testUser), @@ -213,6 +216,74 @@ public void shouldNotOverwriteGraph() throws Exception { } } + @Test + public void shouldThrowWhenOverwriteGraphIsSameAndAccessIsDifferent() throws Exception { + Schema expectedSchema = new Schema.Builder().build(); + + MapStoreProperties storeProperties = new MapStoreProperties(); + + assertEquals(0, store.getGraphs(testUser, null).size()); + + store.initialise(FEDERATEDSTORE_GRAPH_ID, new Schema(), federatedStoreProperties); + + FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); + + federatedAddGraphHandler.doOperation( + new AddGraph.Builder() + .graphId(EXPECTED_GRAPH_ID) + .schema(expectedSchema) + .storeProperties(storeProperties) + .build(), + new Context(testUser), + store); + + try { + federatedAddGraphHandler.doOperation( + new AddGraph.Builder() + .graphId(EXPECTED_GRAPH_ID) + .schema(expectedSchema) + .graphAuths("X") + .storeProperties(storeProperties) + .build(), + new Context(testUser), + store); + fail(EXCEPTION_EXPECTED); + } catch (final Exception e) { + assertTrue(e.getMessage().contains(String.format(USER_IS_ATTEMPTING_TO_OVERWRITE, EXPECTED_GRAPH_ID))); + } + } + + @Test + public void shouldNotThrowWhenOverwriteGraphIsSame() throws Exception { + Schema expectedSchema = new Schema.Builder().build(); + + MapStoreProperties storeProperties = new MapStoreProperties(); + + assertEquals(0, store.getGraphs(testUser, null).size()); + + store.initialise(FEDERATEDSTORE_GRAPH_ID, new Schema(), federatedStoreProperties); + + FederatedAddGraphHandler federatedAddGraphHandler = new FederatedAddGraphHandler(); + + federatedAddGraphHandler.doOperation( + new AddGraph.Builder() + .graphId(EXPECTED_GRAPH_ID) + .schema(expectedSchema) + .storeProperties(storeProperties) + .build(), + new Context(testUser), + store); + + federatedAddGraphHandler.doOperation( + new AddGraph.Builder() + .graphId(EXPECTED_GRAPH_ID) + .schema(expectedSchema) + .storeProperties(storeProperties) + .build(), + new Context(testUser), + store); + } + @Test public void shouldAddGraphIDOnlyWithAuths() throws Exception { From b5d51e1e5079c061a17925613b11b88ae82b2a90 Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Mon, 6 Nov 2017 11:49:51 +0000 Subject: [PATCH 66/72] gh-1297 Test serialisation of GraphSerialisable. --- .../gaffer/graph/GraphSerialisableTest.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphSerialisableTest.java b/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphSerialisableTest.java index fc34a86ea97..17e321b0bf6 100644 --- a/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphSerialisableTest.java +++ b/core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphSerialisableTest.java @@ -19,7 +19,9 @@ import org.junit.Before; import org.junit.Test; +import uk.gov.gchq.gaffer.cache.impl.HashMapCache; import uk.gov.gchq.gaffer.data.elementdefinition.view.View; +import uk.gov.gchq.gaffer.graph.GraphSerialisable.Builder; import uk.gov.gchq.gaffer.integration.store.TestStore; import uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser; import uk.gov.gchq.gaffer.store.StoreProperties; @@ -74,6 +76,25 @@ public void shouldConsumeGraph() throws Exception { final Graph graph = new Graph.Builder().addSchema(schema).addStoreProperties(new StoreProperties(properties)).config(config).build(); final GraphSerialisable result = new GraphSerialisable.Builder().graph(graph).build(); assertEquals(expected, result); + } + @Test + public void shouldSerialiseWithJavaSerialiser() throws Exception { + HashMapCache cache = new HashMapCache<>(true); + String key = "key"; + GraphSerialisable expected = new Builder().config(config).schema(schema).properties(properties).build(); + cache.put(key, expected); + GraphSerialisable actual = cache.get(key); + assertEquals(expected, actual); + } + + @Test + public void shouldSerialiseWithJsonSerialiser() throws Exception { + HashMapCache cache = new HashMapCache<>(false); + String key = "key"; + GraphSerialisable expected = new Builder().config(config).schema(schema).properties(properties).build(); + cache.put(key, expected); + GraphSerialisable actual = cache.get(key); + assertEquals(expected, actual); } } \ No newline at end of file From f920702ffd75558edea40c486a0dc756a29b45ce Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Mon, 6 Nov 2017 12:02:53 +0000 Subject: [PATCH 67/72] gh-1297 checkstyle --- .../gov/gchq/gaffer/federatedstore/FederatedAccess.java | 8 ++++++-- .../gchq/gaffer/federatedstore/FederatedGraphStorage.java | 4 +++- .../uk/gov/gchq/gaffer/federatedstore/FederatedStore.java | 5 ++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java index 8d6dc4512d7..b62e4cbe7c6 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedAccess.java @@ -126,9 +126,13 @@ public void setGraphAuths(final Set graphAuths) { @Override public boolean equals(final Object o) { - if (this == o) return true; + if (this == o) { + return true; + } - if (o == null || getClass() != o.getClass()) return false; + if (o == null || getClass() != o.getClass()) { + return false; + } final FederatedAccess that = (FederatedAccess) o; diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java index 6f1d014f2cc..53bc59b1997 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedGraphStorage.java @@ -75,6 +75,7 @@ protected void startCacheServiceLoader() throws StorageException { * * @param graphs the graphs to add to the storage. * @param access access required to for the graphs, can't be null + * @throws StorageException if unable to put arguments into storage * @see #put(Graph, FederatedAccess) */ public void put(final Collection graphs, final FederatedAccess access) throws StorageException { @@ -92,6 +93,7 @@ public void put(final Collection graphs, final FederatedAccess access) th * * @param graph the graph to add to the storage. * @param access access required to for the graph. + * @throws StorageException if unable to put arguments into storage */ public void put(final Graph graph, final FederatedAccess access) throws StorageException { String graphId = graph.getGraphId(); @@ -323,7 +325,7 @@ private boolean exists(final Graph graph, final FederatedAccess access) throws S Set graphs = storage.get(access); boolean foundAccess = false; if (null != graphs) { - for (Graph g : graphs) { + for (final Graph g : graphs) { if (g.getGraphId().equals(graphId)) { foundAccess = true; break; diff --git a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java index 4700b2833c8..d91fafc89bc 100644 --- a/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java +++ b/store-implementation/federated-store/src/main/java/uk/gov/gchq/gaffer/federatedstore/FederatedStore.java @@ -79,8 +79,7 @@ *

* A Store that encapsulates a collection of sub-graphs and executes operations * against them and returns results as though it was a single graph. - *

- *

+ *

* To create a FederatedStore you need to initialise the store with a * graphId and (if graphId is not known by the {@link uk.gov.gchq.gaffer.store.library.GraphLibrary}) * the {@link Schema} and {@link StoreProperties}. @@ -167,8 +166,8 @@ public static OP updateOperationForGraph(final OP operati * @param graphs the graph to add * @param isPublic if this class should have public access. * @param graphAuths the access auths for the graph being added + * @throws StorageException if unable to put graph into storage */ - public void addGraphs(final Set graphAuths, final String addingUserId, final boolean isPublic, final Graph... graphs) throws StorageException { FederatedAccess access = new FederatedAccess(graphAuths, addingUserId, isPublicAccessAllowed && isPublic); From f360734af04305d47219c7f56def3cf1682a5187 Mon Sep 17 00:00:00 2001 From: GCHQDeveloper404 Date: Mon, 6 Nov 2017 12:58:38 +0000 Subject: [PATCH 68/72] gh-1297 checkstyle --- .../federated-demo/src/main/resources/federatedStore.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/federated-demo/src/main/resources/federatedStore.properties b/example/federated-demo/src/main/resources/federatedStore.properties index 72093a88617..d7b59308dc2 100644 --- a/example/federated-demo/src/main/resources/federatedStore.properties +++ b/example/federated-demo/src/main/resources/federatedStore.properties @@ -14,4 +14,4 @@ # limitations under the License. # gaffer.store.class=uk.gov.gchq.gaffer.federatedstore.FederatedStore -gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.JcsCacheService \ No newline at end of file +gaffer.cache.service.class=uk.gov.gchq.gaffer.cache.impl.JcsCacheService From d55599559b94a57d0890cf2996b24b1ae4f762bf Mon Sep 17 00:00:00 2001 From: p013570 Date: Mon, 6 Nov 2017 13:27:48 +0000 Subject: [PATCH 69/72] gh-1422 - added default exposed properties --- example/basic/basic-rest/pom.xml | 3 - example/federated-demo/pom.xml | 3 - .../road-traffic/road-traffic-demo/pom.xml | 3 - .../gchq/gaffer/rest/ServiceConstants.java | 6 +- .../gov/gchq/gaffer/rest/SystemProperty.java | 17 +++++ .../service/v1/SwaggerDefinitionConfig.java | 5 +- .../rest/service/v2/PropertiesServiceV2.java | 51 ++++++++++++- .../service/v2/SwaggerDefinitionConfigV2.java | 5 +- .../rest/service/v2/PropertyServiceV2IT.java | 74 +++++++++++++++++-- 9 files changed, 141 insertions(+), 26 deletions(-) diff --git a/example/basic/basic-rest/pom.xml b/example/basic/basic-rest/pom.xml index 0ae9f31b226..cfae8ff7cd0 100644 --- a/example/basic/basic-rest/pom.xml +++ b/example/basic/basic-rest/pom.xml @@ -112,9 +112,6 @@ /${standalone-path} ${standalone-port} - - gaffer.properties.app.description,gaffer.properties.app.title - Basic Gaffer Example diff --git a/example/federated-demo/pom.xml b/example/federated-demo/pom.xml index 8463ba615c6..4b5c3034674 100644 --- a/example/federated-demo/pom.xml +++ b/example/federated-demo/pom.xml @@ -127,9 +127,6 @@ /${standalone-path} ${standalone-port} - - gaffer.properties.app.description,gaffer.properties.app.title - Federated Gaffer Example diff --git a/example/road-traffic/road-traffic-demo/pom.xml b/example/road-traffic/road-traffic-demo/pom.xml index 41868ae9c96..2d3cef6fa67 100644 --- a/example/road-traffic/road-traffic-demo/pom.xml +++ b/example/road-traffic/road-traffic-demo/pom.xml @@ -70,9 +70,6 @@ - - gaffer.properties.app.description,gaffer.properties.app.title - Road Traffic Example diff --git a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/ServiceConstants.java b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/ServiceConstants.java index 8682a943978..6f047738d8d 100644 --- a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/ServiceConstants.java +++ b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/ServiceConstants.java @@ -42,10 +42,8 @@ public final class ServiceConstants { public static final String JOB_SERVICE_UNAVAILABLE = "The job service is not available"; // REST - Other - public static final String DESCRIPTION_DEFAULT = "The Gaffer REST service."; - public static final String CONTACT_DEFAULT = "Gaffer Developers"; - public static final String CONTACT_URL_DEFAULT = "https://github.com/gchq/Gaffer"; - public static final String GAFFER_DOCUMENTATION_URL = "https://gchq.github.io/gaffer-doc/"; + public static final String DESCRIPTION_DEFAULT = SystemProperty.APP_DESCRIPTION_DEFAULT; + public static final String GAFFER_DOCUMENTATION_URL = SystemProperty.APP_DOCUMENTATION_URL_DEFAULT; static { final String apiVersion = System.getProperty(SystemProperty.REST_API_VERSION, SystemProperty.CORE_VERSION); diff --git a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/SystemProperty.java b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/SystemProperty.java index e4e21f6b324..9ceb4ec3fc0 100644 --- a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/SystemProperty.java +++ b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/SystemProperty.java @@ -29,6 +29,7 @@ public abstract class SystemProperty { public static final String GRAPH_CONFIG_PATH = "gaffer.graph.config"; public static final String SCHEMA_PATHS = "gaffer.schemas"; public static final String STORE_PROPERTIES_PATH = "gaffer.storeProperties"; + public static final String BASE_PATH = "gaffer.rest-api.basePath"; public static final String REST_API_VERSION = "gaffer.rest-api.version"; public static final String GRAPH_FACTORY_CLASS = "gaffer.graph.factory.class"; @@ -39,6 +40,17 @@ public abstract class SystemProperty { public static final String JSON_SERIALISER_MODULES = JSONSerialiser.JSON_SERIALISER_MODULES; public static final String REST_DEBUG = DebugUtil.DEBUG; + // Exposed Property Keys + /** + * A CSV of properties to expose via the properties endpoint. + */ + public static final String EXPOSED_PROPERTIES = "gaffer.properties"; + public static final String APP_TITLE = "gaffer.properties.app.title"; + public static final String APP_DESCRIPTION = "gaffer.properties.app.description"; + public static final String APP_CONTACT = "gaffer.properties.app.contact"; + public static final String APP_CONTACT_URL = "gaffer.properties.app.contact.url"; + public static final String APP_DOCUMENTATION_URL = "gaffer.properties.app.doc.url"; + /** * @deprecated create a GraphConfig json file and use GRAPH_CONFIG_PATH instead */ @@ -74,6 +86,11 @@ public abstract class SystemProperty { public static final String GRAPH_FACTORY_CLASS_DEFAULT = DefaultGraphFactory.class.getName(); public static final String USER_FACTORY_CLASS_DEFAULT = UnknownUserFactory.class.getName(); public static final String REST_DEBUG_DEFAULT = DebugUtil.DEBUG_DEFAULT; + public static final String APP_TITLE_DEFAULT = "Gaffer REST"; + public static final String APP_DESCRIPTION_DEFAULT = "The Gaffer REST service."; + public static final String APP_CONTACT_DEFAULT = "Gaffer Developers"; + public static final String APP_CONTACT_URL_DEFAULT = "https://github.com/gchq/Gaffer"; + public static final String APP_DOCUMENTATION_URL_DEFAULT = "https://gchq.github.io/gaffer-doc/"; private SystemProperty() { // Private constructor to prevent instantiation. diff --git a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v1/SwaggerDefinitionConfig.java b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v1/SwaggerDefinitionConfig.java index 2d59b00f869..c56b21e186f 100644 --- a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v1/SwaggerDefinitionConfig.java +++ b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v1/SwaggerDefinitionConfig.java @@ -23,6 +23,7 @@ import io.swagger.annotations.SwaggerDefinition; import uk.gov.gchq.gaffer.rest.ServiceConstants; +import uk.gov.gchq.gaffer.rest.SystemProperty; import static javax.ws.rs.core.MediaType.APPLICATION_JSON; @@ -36,8 +37,8 @@ version = "v1", title = "", contact = @Contact( - name = ServiceConstants.CONTACT_DEFAULT, - url = ServiceConstants.CONTACT_URL_DEFAULT + name = SystemProperty.APP_CONTACT_DEFAULT, + url = SystemProperty.APP_CONTACT_URL_DEFAULT ), license = @License( name = "Apache 2.0", diff --git a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/PropertiesServiceV2.java b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/PropertiesServiceV2.java index 2b431cf02c5..6691c83e252 100644 --- a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/PropertiesServiceV2.java +++ b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/PropertiesServiceV2.java @@ -17,8 +17,10 @@ package uk.gov.gchq.gaffer.rest.service.v2; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.ArrayUtils; import uk.gov.gchq.gaffer.rest.ServiceConstants; +import uk.gov.gchq.gaffer.rest.SystemProperty; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @@ -27,22 +29,44 @@ import java.util.LinkedHashMap; import java.util.Map; +import static uk.gov.gchq.gaffer.rest.SystemProperty.APP_CONTACT; +import static uk.gov.gchq.gaffer.rest.SystemProperty.APP_CONTACT_DEFAULT; +import static uk.gov.gchq.gaffer.rest.SystemProperty.APP_CONTACT_URL; +import static uk.gov.gchq.gaffer.rest.SystemProperty.APP_CONTACT_URL_DEFAULT; +import static uk.gov.gchq.gaffer.rest.SystemProperty.APP_DESCRIPTION; +import static uk.gov.gchq.gaffer.rest.SystemProperty.APP_DESCRIPTION_DEFAULT; +import static uk.gov.gchq.gaffer.rest.SystemProperty.APP_DOCUMENTATION_URL; +import static uk.gov.gchq.gaffer.rest.SystemProperty.APP_DOCUMENTATION_URL_DEFAULT; +import static uk.gov.gchq.gaffer.rest.SystemProperty.APP_TITLE; +import static uk.gov.gchq.gaffer.rest.SystemProperty.APP_TITLE_DEFAULT; + /** * An implementation of {@link IPropertiesServiceV2} that gets the configured system properties */ public class PropertiesServiceV2 implements IPropertiesServiceV2 { - private static final String PROPERTIES_LIST = "gaffer.properties"; + public static final String EXPOSED_PROPERTIES = SystemProperty.EXPOSED_PROPERTIES; + public static final Map CORE_EXPOSED_PROPERTIES = createCoreExposedProperties(); + + private static Map createCoreExposedProperties() { + final Map map = new LinkedHashMap<>(); + map.put(APP_TITLE, APP_TITLE_DEFAULT); + map.put(APP_DESCRIPTION, APP_DESCRIPTION_DEFAULT); + map.put(APP_CONTACT, APP_CONTACT_DEFAULT); + map.put(APP_CONTACT_URL, APP_CONTACT_URL_DEFAULT); + map.put(APP_DOCUMENTATION_URL, APP_DOCUMENTATION_URL_DEFAULT); + return Collections.unmodifiableMap(map); + } @Override public Response getProperties() { final Map properties; - final String propertiesList = System.getProperty(PROPERTIES_LIST); + final String propertiesList = System.getProperty(EXPOSED_PROPERTIES); if (null == propertiesList) { properties = Collections.emptyMap(); } else { final String[] props = propertiesList.split(","); - properties = new LinkedHashMap<>(props.length); + properties = new LinkedHashMap<>(CORE_EXPOSED_PROPERTIES); for (final String prop : props) { if (StringUtils.isNotEmpty(prop)) { properties.put(prop, System.getProperty(prop)); @@ -57,7 +81,26 @@ public Response getProperties() { @Override public Response getProperty(final String propertyName) { - final String prop = System.getProperty(propertyName); + final boolean isCore = CORE_EXPOSED_PROPERTIES.containsKey(propertyName); + boolean isExposed = isCore; + if (!isExposed) { + final String propertiesList = System.getProperty(EXPOSED_PROPERTIES); + if (null != propertiesList) { + final String[] props = propertiesList.split(","); + isExposed = ArrayUtils.contains(props, propertyName); + } + } + + String prop; + if (isExposed) { + prop = System.getProperty(propertyName); + if (null == prop && isCore) { + prop = CORE_EXPOSED_PROPERTIES.get(propertyName); + } + } else { + prop = null; + } + final ResponseBuilder builder = null == prop ? Response.status(404) : Response.ok(prop); return builder.header(ServiceConstants.GAFFER_MEDIA_TYPE_HEADER, ServiceConstants.GAFFER_MEDIA_TYPE).build(); } diff --git a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/SwaggerDefinitionConfigV2.java b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/SwaggerDefinitionConfigV2.java index 57c349dafc6..8cd2378e072 100644 --- a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/SwaggerDefinitionConfigV2.java +++ b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/SwaggerDefinitionConfigV2.java @@ -23,6 +23,7 @@ import io.swagger.annotations.SwaggerDefinition; import uk.gov.gchq.gaffer.rest.ServiceConstants; +import uk.gov.gchq.gaffer.rest.SystemProperty; import static javax.ws.rs.core.MediaType.APPLICATION_JSON; @@ -36,8 +37,8 @@ version = "v2", title = "", contact = @Contact( - name = ServiceConstants.CONTACT_DEFAULT, - url = ServiceConstants.CONTACT_URL_DEFAULT + name = SystemProperty.APP_CONTACT_DEFAULT, + url = SystemProperty.APP_CONTACT_URL_DEFAULT ), license = @License( name = "Apache 2.0", diff --git a/rest-api/core-rest/src/test/java/uk/gov/gchq/gaffer/rest/service/v2/PropertyServiceV2IT.java b/rest-api/core-rest/src/test/java/uk/gov/gchq/gaffer/rest/service/v2/PropertyServiceV2IT.java index dab20be1f90..8156deeb9d0 100644 --- a/rest-api/core-rest/src/test/java/uk/gov/gchq/gaffer/rest/service/v2/PropertyServiceV2IT.java +++ b/rest-api/core-rest/src/test/java/uk/gov/gchq/gaffer/rest/service/v2/PropertyServiceV2IT.java @@ -15,9 +15,14 @@ * limitations under the License. */ +import org.junit.After; +import org.junit.Before; import org.junit.Test; +import uk.gov.gchq.gaffer.rest.SystemProperty; + import javax.ws.rs.core.Response; + import java.io.IOException; import java.util.Map; @@ -25,6 +30,16 @@ public class PropertyServiceV2IT extends AbstractRestApiV2IT { + @Before + @After + public void cleanUp() { + System.clearProperty("gaffer.properties"); + System.clearProperty("gaffer.test1"); + System.clearProperty("gaffer.test2"); + System.clearProperty("gaffer.test3"); + System.clearProperty(SystemProperty.APP_TITLE); + } + @Test public void shouldThrowErrorOnUnknownPropertyWhenNoneSet() throws IOException { //Given @@ -41,7 +56,7 @@ public void shouldThrowErrorOnUnknownProperty() throws IOException { //Given System.setProperty("gaffer.properties", "gaffer.test1,gaffer.test2"); System.setProperty("gaffer.test1", "1"); - System.setProperty("gaffer.test2", "3"); + System.setProperty("gaffer.test2", "2"); // When final Response response = getClient().getProperty("UNKNOWN"); @@ -50,21 +65,39 @@ public void shouldThrowErrorOnUnknownProperty() throws IOException { assertEquals(404, response.getStatus()); } + @Test + public void shouldThrowErrorOnPropertyThatIsNotExposed() throws IOException { + //Given + System.setProperty("gaffer.properties", "gaffer.test1,gaffer.test2"); + System.setProperty("gaffer.test1", "1"); + System.setProperty("gaffer.test2", "2"); + System.setProperty("gaffer.test3", "3"); + + // When + final Response response = getClient().getProperty("gaffer.test3"); + + //Then + assertEquals(404, response.getStatus()); + } + @Test public void shouldGetAllProperties() throws IOException { //Given System.setProperty("gaffer.properties", "gaffer.test1,gaffer.test2"); System.setProperty("gaffer.test1", "1"); - System.setProperty("gaffer.test2", "3"); + System.setProperty("gaffer.test2", "2"); + System.setProperty("gaffer.test3", "3"); // When final Response response = getClient().getProperties(); //Then assertEquals(200, response.getStatus()); Map properties = response.readEntity(Map.class); - assertEquals(2, properties.size()); + assertEquals(PropertiesServiceV2.CORE_EXPOSED_PROPERTIES.size() + 2, properties.size()); assertEquals("1", properties.get("gaffer.test1")); - assertEquals("3", properties.get("gaffer.test2")); + assertEquals("1", properties.get("gaffer.test1")); + assertEquals("1", properties.get("gaffer.test1")); + assertEquals("2", properties.get("gaffer.test2")); } @Test @@ -72,7 +105,7 @@ public void shouldGetKnownProperty() throws IOException { //Given System.setProperty("gaffer.properties", "gaffer.test1,gaffer.test2"); System.setProperty("gaffer.test1", "1"); - System.setProperty("gaffer.test2", "3"); + System.setProperty("gaffer.test2", "2"); // When final Response response = getClient().getProperty("gaffer.test1"); @@ -81,4 +114,35 @@ public void shouldGetKnownProperty() throws IOException { String property = response.readEntity(String.class); assertEquals("1", property); } + + @Test + public void shouldGetKnownCoreProperty() throws IOException { + //Given + System.setProperty("gaffer.properties", "gaffer.test1,gaffer.test2"); + System.setProperty("gaffer.test1", "1"); + System.setProperty("gaffer.test2", "2"); + // When + final Response response = getClient().getProperty(SystemProperty.APP_TITLE); + + //Then + assertEquals(200, response.getStatus()); + String property = response.readEntity(String.class); + assertEquals(SystemProperty.APP_TITLE_DEFAULT, property); + } + + @Test + public void shouldGetOverriddenKnownCoreProperty() throws IOException { + //Given + System.setProperty("gaffer.properties", "gaffer.test1,gaffer.test2"); + System.setProperty("gaffer.test1", "1"); + System.setProperty("gaffer.test2", "2"); + System.setProperty(SystemProperty.APP_TITLE, "newTitle"); + // When + final Response response = getClient().getProperty(SystemProperty.APP_TITLE); + + //Then + assertEquals(200, response.getStatus()); + String property = response.readEntity(String.class); + assertEquals("newTitle", property); + } } From 2a78fe0bfe47d2027f4f35c88abdcde6238ba59f Mon Sep 17 00:00:00 2001 From: p013570 Date: Mon, 6 Nov 2017 13:34:42 +0000 Subject: [PATCH 70/72] gh-1422 - fixed issue with properties end point --- .../rest/service/v2/PropertiesServiceV2.java | 15 ++++++++++----- .../rest/service/v2/PropertyServiceV2IT.java | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/PropertiesServiceV2.java b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/PropertiesServiceV2.java index 6691c83e252..abb5c3a94a8 100644 --- a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/PropertiesServiceV2.java +++ b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/PropertiesServiceV2.java @@ -25,9 +25,11 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; +import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; +import java.util.stream.Stream; import static uk.gov.gchq.gaffer.rest.SystemProperty.APP_CONTACT; import static uk.gov.gchq.gaffer.rest.SystemProperty.APP_CONTACT_DEFAULT; @@ -67,11 +69,14 @@ public Response getProperties() { } else { final String[] props = propertiesList.split(","); properties = new LinkedHashMap<>(CORE_EXPOSED_PROPERTIES); - for (final String prop : props) { - if (StringUtils.isNotEmpty(prop)) { - properties.put(prop, System.getProperty(prop)); - } - } + Stream.concat(CORE_EXPOSED_PROPERTIES.keySet().stream(), Arrays.stream(props)) + .filter(StringUtils::isNotEmpty) + .forEach(prop -> { + final String value = System.getProperty(prop); + if (null != value) { + properties.put(prop, value); + } + }); } return Response.ok(Collections.unmodifiableMap(properties)) diff --git a/rest-api/core-rest/src/test/java/uk/gov/gchq/gaffer/rest/service/v2/PropertyServiceV2IT.java b/rest-api/core-rest/src/test/java/uk/gov/gchq/gaffer/rest/service/v2/PropertyServiceV2IT.java index 8156deeb9d0..68e7bba5599 100644 --- a/rest-api/core-rest/src/test/java/uk/gov/gchq/gaffer/rest/service/v2/PropertyServiceV2IT.java +++ b/rest-api/core-rest/src/test/java/uk/gov/gchq/gaffer/rest/service/v2/PropertyServiceV2IT.java @@ -24,6 +24,7 @@ import javax.ws.rs.core.Response; import java.io.IOException; +import java.util.LinkedHashMap; import java.util.Map; import static org.junit.Assert.assertEquals; @@ -87,17 +88,22 @@ public void shouldGetAllProperties() throws IOException { System.setProperty("gaffer.test1", "1"); System.setProperty("gaffer.test2", "2"); System.setProperty("gaffer.test3", "3"); + System.setProperty(SystemProperty.APP_TITLE, "newTitle"); + // When final Response response = getClient().getProperties(); //Then assertEquals(200, response.getStatus()); Map properties = response.readEntity(Map.class); - assertEquals(PropertiesServiceV2.CORE_EXPOSED_PROPERTIES.size() + 2, properties.size()); - assertEquals("1", properties.get("gaffer.test1")); - assertEquals("1", properties.get("gaffer.test1")); + + final LinkedHashMap expectedProperties = new LinkedHashMap<>(PropertiesServiceV2.CORE_EXPOSED_PROPERTIES); + expectedProperties.put("gaffer.test1", "1"); + expectedProperties.put("gaffer.test2", "2"); + expectedProperties.put(SystemProperty.APP_TITLE, "newTitle"); assertEquals("1", properties.get("gaffer.test1")); assertEquals("2", properties.get("gaffer.test2")); + assertEquals(expectedProperties, properties); } @Test @@ -106,6 +112,7 @@ public void shouldGetKnownProperty() throws IOException { System.setProperty("gaffer.properties", "gaffer.test1,gaffer.test2"); System.setProperty("gaffer.test1", "1"); System.setProperty("gaffer.test2", "2"); + // When final Response response = getClient().getProperty("gaffer.test1"); @@ -121,6 +128,7 @@ public void shouldGetKnownCoreProperty() throws IOException { System.setProperty("gaffer.properties", "gaffer.test1,gaffer.test2"); System.setProperty("gaffer.test1", "1"); System.setProperty("gaffer.test2", "2"); + // When final Response response = getClient().getProperty(SystemProperty.APP_TITLE); @@ -137,6 +145,7 @@ public void shouldGetOverriddenKnownCoreProperty() throws IOException { System.setProperty("gaffer.test1", "1"); System.setProperty("gaffer.test2", "2"); System.setProperty(SystemProperty.APP_TITLE, "newTitle"); + // When final Response response = getClient().getProperty(SystemProperty.APP_TITLE); From 2615f14579c12fd307925f17b36d100c328981ec Mon Sep 17 00:00:00 2001 From: p013570 Date: Mon, 6 Nov 2017 13:54:04 +0000 Subject: [PATCH 71/72] gh-1422 - fixed bug with properties service --- .../rest/service/v2/PropertiesServiceV2.java | 29 ++++++++----------- .../rest/service/v2/PropertyServiceV2IT.java | 22 ++++++++++++-- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/PropertiesServiceV2.java b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/PropertiesServiceV2.java index abb5c3a94a8..4bde24144c3 100644 --- a/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/PropertiesServiceV2.java +++ b/rest-api/core-rest/src/main/java/uk/gov/gchq/gaffer/rest/service/v2/PropertiesServiceV2.java @@ -61,23 +61,18 @@ private static Map createCoreExposedProperties() { @Override public Response getProperties() { - final Map properties; - - final String propertiesList = System.getProperty(EXPOSED_PROPERTIES); - if (null == propertiesList) { - properties = Collections.emptyMap(); - } else { - final String[] props = propertiesList.split(","); - properties = new LinkedHashMap<>(CORE_EXPOSED_PROPERTIES); - Stream.concat(CORE_EXPOSED_PROPERTIES.keySet().stream(), Arrays.stream(props)) - .filter(StringUtils::isNotEmpty) - .forEach(prop -> { - final String value = System.getProperty(prop); - if (null != value) { - properties.put(prop, value); - } - }); - } + final Map properties = new LinkedHashMap<>(); + final String customPropNamesCsv = System.getProperty(EXPOSED_PROPERTIES); + final Stream customPropNames = null != customPropNamesCsv ? Arrays.stream(customPropNamesCsv.split(",")) : Stream.empty(); + Stream.concat(CORE_EXPOSED_PROPERTIES.keySet().stream(), customPropNames) + .filter(StringUtils::isNotEmpty) + .forEach(prop -> { + String value = System.getProperty(prop); + if (null == value) { + value = CORE_EXPOSED_PROPERTIES.get(prop); + } + properties.put(prop, value); + }); return Response.ok(Collections.unmodifiableMap(properties)) .header(ServiceConstants.GAFFER_MEDIA_TYPE_HEADER, ServiceConstants.GAFFER_MEDIA_TYPE) diff --git a/rest-api/core-rest/src/test/java/uk/gov/gchq/gaffer/rest/service/v2/PropertyServiceV2IT.java b/rest-api/core-rest/src/test/java/uk/gov/gchq/gaffer/rest/service/v2/PropertyServiceV2IT.java index 68e7bba5599..a11e8600f51 100644 --- a/rest-api/core-rest/src/test/java/uk/gov/gchq/gaffer/rest/service/v2/PropertyServiceV2IT.java +++ b/rest-api/core-rest/src/test/java/uk/gov/gchq/gaffer/rest/service/v2/PropertyServiceV2IT.java @@ -101,8 +101,26 @@ public void shouldGetAllProperties() throws IOException { expectedProperties.put("gaffer.test1", "1"); expectedProperties.put("gaffer.test2", "2"); expectedProperties.put(SystemProperty.APP_TITLE, "newTitle"); - assertEquals("1", properties.get("gaffer.test1")); - assertEquals("2", properties.get("gaffer.test2")); + assertEquals(expectedProperties, properties); + } + + @Test + public void shouldGetAllPropertiesWhenNoCustomPropertiesCsvDefined() throws IOException { + //Given + System.setProperty("gaffer.test1", "1"); + System.setProperty("gaffer.test2", "2"); + System.setProperty("gaffer.test3", "3"); + System.setProperty(SystemProperty.APP_TITLE, "newTitle"); + + // When + final Response response = getClient().getProperties(); + + //Then + assertEquals(200, response.getStatus()); + Map properties = response.readEntity(Map.class); + + final LinkedHashMap expectedProperties = new LinkedHashMap<>(PropertiesServiceV2.CORE_EXPOSED_PROPERTIES); + expectedProperties.put(SystemProperty.APP_TITLE, "newTitle"); assertEquals(expectedProperties, properties); } From a2e7d239cb4a292c8967659b4565f0588fef8cbf Mon Sep 17 00:00:00 2001 From: m55624 Date: Mon, 6 Nov 2017 14:38:34 +0000 Subject: [PATCH 72/72] gh-1422 - made methods more generic --- rest-api/core-rest/src/main/webapp/lib/gaffer.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/rest-api/core-rest/src/main/webapp/lib/gaffer.js b/rest-api/core-rest/src/main/webapp/lib/gaffer.js index 063137a58b6..ea7d6edfc73 100644 --- a/rest-api/core-rest/src/main/webapp/lib/gaffer.js +++ b/rest-api/core-rest/src/main/webapp/lib/gaffer.js @@ -163,24 +163,23 @@ function initFromProperties(onPropertiesLoad) { if(onPropertiesLoad) { onPropertiesLoad(properties); } - }; - + } $.get(getVersion() + '/properties', null, onSuccess); } function updateTitle(properties) { - updateElementText('gaffer.properties.app.title', properties, function(value, id) { + updateElement('gaffer.properties.app.title', properties, function(value, id) { + $('#' + id).text(value); document.title = value; - }) + }); } -function updateElementText(key, properties, onSuccess) { - updateElementTextWithId(key.split('.').pop(), key, properties, onSuccess); +function updateElement(key, properties, onSuccess) { + updateElementWithId(key.split('.').pop(), key, properties, onSuccess); } -function updateElementTextWithId(id, key, properties, onSuccess) { +function updateElementWithId(id, key, properties, onSuccess) { if(key in properties) { - $('#' + id).text(properties[key]); if(onSuccess) { onSuccess(properties[key], id); }