Skip to content

Commit

Permalink
gh-2887 Test fixed to user correct cache instance due to suffix/name …
Browse files Browse the repository at this point in the history
…change.
  • Loading branch information
GCHQDev404 committed Feb 23, 2023
1 parent dc2aaa7 commit d5d21f8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import uk.gov.gchq.gaffer.graph.Graph;
import uk.gov.gchq.gaffer.graph.GraphSerialisable;

import java.util.Locale;
import java.util.Set;

import static java.util.Objects.isNull;
Expand All @@ -33,17 +34,21 @@
*/
public class FederatedStoreCacheTransient extends Cache<String, Pair<GraphSerialisable, byte[]>> {
public static final String ERROR_ADDING_GRAPH_TO_CACHE_GRAPH_ID_S = "Error adding graph to cache. graphId: %s";
private static final String CACHE_SERVICE_NAME_PREFIX = "federatedStoreGraphs";
public static final String CACHE_SERVICE_NAME_PREFIX = "federatedStoreGraphs";

public FederatedStoreCacheTransient() {
this(null);
}

public FederatedStoreCacheTransient(final String cacheNameSuffix) {
super(String.format("%s%s", CACHE_SERVICE_NAME_PREFIX,
super(getCacheNameFrom(cacheNameSuffix));
}

public static String getCacheNameFrom(final String cacheNameSuffix) {
return String.format("%s%s", CACHE_SERVICE_NAME_PREFIX,
nonNull(cacheNameSuffix)
? "_" + cacheNameSuffix.toLowerCase()
: ""));
? "_" + cacheNameSuffix.toLowerCase(Locale.UK)
: "");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 Crown Copyright
* Copyright 2017-2023 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,6 +48,7 @@ public class FederatedStoreMultiCacheTest {
public static final User TEST_USER = testUser();
public static final User BLANK_USER = blankUser();
private static final AccumuloProperties ACCUMULO_PROPERTIES = loadAccumuloStoreProperties(ACCUMULO_STORE_SINGLE_USE_PROPERTIES);
public static final String USER_SAME_CACHE_SUFFIX = "UseSameCacheSuffix";
public FederatedStore federatedStore;
public FederatedStore federatedStore2WithSameCache;
public FederatedStoreProperties federatedStoreProperties;
Expand All @@ -59,6 +60,7 @@ public void setUp() throws Exception {
federatedStoreProperties = new FederatedStoreProperties();
federatedStoreProperties.setCacheServiceClass(CACHE_SERVICE_CLASS_STRING);
federatedStoreProperties.set(HashMapCacheService.STATIC_CACHE, String.valueOf(true));
federatedStoreProperties.setCacheServiceNameSuffix(USER_SAME_CACHE_SUFFIX);
federatedStore = new FederatedStore();
federatedStore.initialise(GRAPH_ID_TEST_FEDERATED_STORE, null, federatedStoreProperties);
federatedStore.execute(new AddGraph.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreCacheTransient.getCacheNameFrom;
import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreTestUtil.ACCUMULO_STORE_SINGLE_USE_PROPERTIES;
import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreTestUtil.ACCUMULO_STORE_SINGLE_USE_PROPERTIES_ALT;
import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreTestUtil.CACHE_SERVICE_CLASS_STRING;
Expand Down Expand Up @@ -128,7 +129,7 @@ public class FederatedStoreTest {
private static final String MAP_ID_1 = "miniMapGraphId1";
private static final String FED_ID_1 = "subFedGraphId1";
private static final String INVALID_CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.invalid";
private static final String CACHE_SERVICE_NAME = "federatedStoreGraphs";
public static final String CACHE_NAME = getCacheNameFrom(GRAPH_ID_TEST_FEDERATED_STORE);
private static AccumuloProperties properties1;
private static AccumuloProperties properties2;
private static AccumuloProperties propertiesAlt;
Expand Down Expand Up @@ -948,16 +949,16 @@ public void shouldReuseGraphsAlreadyInCache() throws Exception {

// check the store and the cache
assertThat(store.getAllGraphIds(blankUser)).hasSize(1);
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME))
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_NAME))
.contains(ACC_ID_2, ACC_ID_2);

// restart the store
store = new FederatedStore();
store.initialise(GRAPH_ID_TEST_FEDERATED_STORE, null, federatedProperties);

// check the graph is already in there from the cache
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME))
.withFailMessage(String.format("Keys: %s did not contain %s", CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME), ACC_ID_2)).contains(ACC_ID_2);
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_NAME))
.withFailMessage(String.format("Keys: %s did not contain %s", CacheServiceLoader.getService().getAllKeysFromCache(CACHE_NAME), ACC_ID_2)).contains(ACC_ID_2);
assertThat(store.getAllGraphIds(blankUser)).hasSize(1);
}

Expand Down Expand Up @@ -1026,14 +1027,14 @@ public void shouldAddGraphsToCache() throws Exception {
final Collection<GraphSerialisable> storeGraphs = store.getGraphs(blankUser, null, new GetAllGraphIds());

// Then
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME)).contains(ACC_ID_1);
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_NAME)).contains(ACC_ID_1);
assertThat(storeGraphs).contains(graphToAdd);

// When
store = new FederatedStore();

// Then
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME)).contains(ACC_ID_1);
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_NAME)).contains(ACC_ID_1);
}

@Test
Expand All @@ -1056,15 +1057,15 @@ public void shouldAddMultipleGraphsToCache() throws Exception {

// Then
for (int i = 0; i < 10; i++) {
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME)).contains(ACC_ID_1 + i);
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_NAME)).contains(ACC_ID_1 + i);
}

// When
store = new FederatedStore();

// Then
for (int i = 0; i < 10; i++) {
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME)).contains(ACC_ID_1 + i);
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_NAME)).contains(ACC_ID_1 + i);
}
}

Expand Down Expand Up @@ -1107,7 +1108,7 @@ public void shouldNotAddGraphToLibraryWhenReinitialisingFederatedStoreWithGraphF
// check is in the store
assertThat(store.getAllGraphIds(blankUser)).hasSize(1);
// check is in the cache
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME)).contains(ACC_ID_1);
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_NAME)).contains(ACC_ID_1);
// check isn't in the LIBRARY
assertThat(store.getGraphLibrary().get(ACC_ID_1)).isNull();

Expand All @@ -1119,8 +1120,8 @@ public void shouldNotAddGraphToLibraryWhenReinitialisingFederatedStoreWithGraphF
store.setGraphLibrary(library);

// check is in the cache still
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME))
.withFailMessage(String.format("Keys: %s did not contain %s", CacheServiceLoader.getService().getAllKeysFromCache(CACHE_SERVICE_NAME), ACC_ID_1)).contains(ACC_ID_1);
assertThat(CacheServiceLoader.getService().getAllKeysFromCache(CACHE_NAME))
.withFailMessage(String.format("Keys: %s did not contain %s", CacheServiceLoader.getService().getAllKeysFromCache(CACHE_NAME), ACC_ID_1)).contains(ACC_ID_1);
// check is in the store from the cache
assertThat(store.getAllGraphIds(blankUser)).hasSize(1);
// check the graph isn't in the GraphLibrary
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Crown Copyright
* Copyright 2022-2023 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@

import uk.gov.gchq.gaffer.accumulostore.AccumuloProperties;
import uk.gov.gchq.gaffer.cache.CacheServiceLoader;
import uk.gov.gchq.gaffer.cache.impl.HashMapCacheService;
import uk.gov.gchq.gaffer.commonutil.ExecutorService;
import uk.gov.gchq.gaffer.commonutil.StreamUtil;
import uk.gov.gchq.gaffer.data.element.Edge;
Expand Down Expand Up @@ -93,7 +94,7 @@ public final class FederatedStoreTestUtil {
public static final String VALUE_1 = value(1);
public static final String VALUE_2 = value(2);
public static final String INTEGER = "integer";
public static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService";
public static final String CACHE_SERVICE_CLASS_STRING = HashMapCacheService.class.getCanonicalName();
public static final Set<String> GRAPH_AUTHS_ALL_USERS = ImmutableSet.of(ALL_USERS);

private FederatedStoreTestUtil() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2022 Crown Copyright
* Copyright 2020-2023 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -97,7 +97,7 @@ public void shouldRemoveGraphFromStorage() throws Exception {
@Test
public void shouldRemoveGraphFromCache() throws Exception {
//given
FederatedStoreCache federatedStoreCache = new FederatedStoreCache();
FederatedStoreCache federatedStoreCache = new FederatedStoreCache(graph.getGraphId());
graph.execute(new AddGraph.Builder()
.graphId(GRAPH_ID_A)
.schema(new Schema())
Expand Down Expand Up @@ -532,7 +532,7 @@ public void shouldNotChangeGraphIdForNonOwnedGraphAsNonAdminWhenRequestingAdminA
@Test
public void shouldStartWithEmptyCache() throws Exception {
//given
FederatedStoreCache federatedStoreCache = new FederatedStoreCache();
FederatedStoreCache federatedStoreCache = new FederatedStoreCache(graph.getGraphId());

//then
assertThat(federatedStoreCache.getAllGraphIds()).isEmpty();
Expand Down Expand Up @@ -567,7 +567,7 @@ public void shouldChangeGraphIdInStorage() throws Exception {
public void shouldChangeGraphIdInCache() throws Exception {
//given
String newName = "newName" + 23452335;
FederatedStoreCache federatedStoreCache = new FederatedStoreCache();
FederatedStoreCache federatedStoreCache = new FederatedStoreCache(graph.getGraphId());

graph.execute(new AddGraph.Builder()
.graphId(GRAPH_ID_A)
Expand Down Expand Up @@ -621,7 +621,7 @@ public void shouldChangeGraphAccessIdInStorage() throws Exception {
@Test
public void shouldChangeGraphAccessIdInCache() throws Exception {
//given
FederatedStoreCache federatedStoreCache = new FederatedStoreCache();
FederatedStoreCache federatedStoreCache = new FederatedStoreCache(graph.getGraphId());
graph.execute(new AddGraph.Builder()
.graphId(GRAPH_ID_A)
.schema(new Schema())
Expand Down

0 comments on commit d5d21f8

Please sign in to comment.