Skip to content

Commit

Permalink
gh-2823 FederatedStore unsupported operations are forwarded to subgra… (
Browse files Browse the repository at this point in the history
#2824)

* gh-2823 FederatedStore unsupported operations are forwarded to subgraphs.

* gh-2823 FederatedStore unsupported operations are forwarded to subgraphs. Tests

* gh-2823 FederatedStore unsupported operations are forwarded to subgraphs. improvements

Co-authored-by: GCHQDev404 <GCHQDev404@users.noreply.github.com>
  • Loading branch information
GCHQDev404 and GCHQDev404 committed Nov 16, 2022
1 parent cb9a1c9 commit 6ff8f69
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds;
import uk.gov.gchq.gaffer.operation.impl.get.GetAllElements;
import uk.gov.gchq.gaffer.operation.impl.get.GetElements;
import uk.gov.gchq.gaffer.operation.io.Output;
import uk.gov.gchq.gaffer.serialisation.Serialiser;
import uk.gov.gchq.gaffer.store.Context;
import uk.gov.gchq.gaffer.store.Store;
Expand Down Expand Up @@ -613,4 +614,23 @@ public Map<String, BiFunction> getStoreConfiguredMergeFunctions() {
return Collections.unmodifiableMap(storeConfiguredMergeFunctions);
}

protected Object doUnhandledOperation(final Operation operation, final Context context) {
try {
if (operation instanceof Output) {
if (Iterable.class.isAssignableFrom(((Output<?>) operation).getOutputClass())) {
return new FederatedOutputIterableHandler<>()
.doOperation((Output<Iterable<?>>) operation, context, this);
} else {
return new FederatedOutputHandler<>()
.doOperation((Output<Object>) operation, context, this);
}
} else {
return new FederatedNoOutputHandler()
.doOperation(operation, context, this);
}
} catch (final Exception e) {
throw new UnsupportedOperationException(String.format("Operation class %s is not supported by the FederatedStore. Error occurred forwarding unhandled operation to sub-graphs due to: %s", operation.getClass().getName(), e.getMessage()), e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package uk.gov.gchq.gaffer.federatedstore;

import com.google.common.collect.Sets;
import org.apache.commons.lang3.exception.CloneFailedException;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -50,7 +51,6 @@
import uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser;
import uk.gov.gchq.gaffer.operation.Operation;
import uk.gov.gchq.gaffer.operation.OperationException;
import uk.gov.gchq.gaffer.operation.impl.OperationImpl;
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;
Expand All @@ -69,6 +69,7 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -270,13 +271,28 @@ public void shouldNotAllowOverwritingOfGraphWithinFederatedScope() throws Except
@Test
public void shouldThrowAppropriateExceptionWhenHandlingAnUnsupportedOperation() {
// Given
final Operation op = new OperationImpl();
final Operation unknownOperation = new Operation() {
@Override
public Operation shallowClone() throws CloneFailedException {
return this;
}

@Override
public Map<String, String> getOptions() {
return null;
}

@Override
public void setOptions(final Map<String, String> options) {

}
};
// When
// Expected an UnsupportedOperationException rather than an OperationException

// Then
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> store.handleOperation(op, new Context()))
.withMessage("Operation class uk.gov.gchq.gaffer.operation.impl.OperationImpl is not supported by the FederatedStore.");
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> store.handleOperation(unknownOperation, new Context()))
.withMessageContaining("Operation class uk.gov.gchq.gaffer.federatedstore.FederatedStoreTest$1 is not supported by the FederatedStore.");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ 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";
static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService";
static final Set<String> GRAPH_AUTHS_ALL_USERS = ImmutableSet.of(ALL_USERS);
public static final String CACHE_SERVICE_CLASS_STRING = "uk.gov.gchq.gaffer.cache.impl.HashMapCacheService";
public static final Set<String> GRAPH_AUTHS_ALL_USERS = ImmutableSet.of(ALL_USERS);

private FederatedStoreTestUtil() {
//no instance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright 2022 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.operation.handler.impl;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import uk.gov.gchq.gaffer.accumulostore.AccumuloProperties;
import uk.gov.gchq.gaffer.federatedstore.FederatedStore;
import uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties;
import uk.gov.gchq.gaffer.federatedstore.FederatedStoreTestUtil;
import uk.gov.gchq.gaffer.federatedstore.operation.AddGraph;
import uk.gov.gchq.gaffer.graph.Graph;
import uk.gov.gchq.gaffer.operation.Operation;
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;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreTestUtil.ACCUMULO_STORE_SINGLE_USE_PROPERTIES;
import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreTestUtil.CACHE_SERVICE_CLASS_STRING;
import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreTestUtil.GRAPH_ID_ACCUMULO;
import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreTestUtil.GRAPH_ID_TEST_FEDERATED_STORE;
import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreTestUtil.SCHEMA_ENTITY_BASIC_JSON;
import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreTestUtil.contextTestUser;
import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreTestUtil.entityBasic;
import static uk.gov.gchq.gaffer.federatedstore.FederatedStoreTestUtil.loadSchemaFromJson;

public class FederatedUnhandledOperationTest {
private FederatedStoreProperties federatedStoreProperties;

private static final AccumuloProperties PROPERTIES = FederatedStoreTestUtil.loadAccumuloStoreProperties(ACCUMULO_STORE_SINGLE_USE_PROPERTIES);

@BeforeEach
public void setUp() throws Exception {
FederatedStoreTestUtil.resetForFederatedTests();
federatedStoreProperties = new FederatedStoreProperties();
federatedStoreProperties.setCacheServiceClass(CACHE_SERVICE_CLASS_STRING);
}

@AfterEach
void afterEach() {
FederatedStoreTestUtil.resetForFederatedTests();
}


@Test
public void shouldFailWithUnsupportedOperation() throws Exception {
final String testExceptionMessage = "TestExceptionMessage";

//make custom store with no GetAllElements
FederatedStore federatedStoreWithNoGetAllElements = new FederatedStore() {
@Override
protected void addAdditionalOperationHandlers() {
super.addAdditionalOperationHandlers();
//No support for GetAllElements
addOperationHandler(GetAllElements.class, null);
//No support for adding Generic Handlers to Federation.
addOperationHandler(AddGraph.class, new FederatedAddGraphHandler() {
@Override
protected void addGenericHandler(final FederatedStore store, final Graph graph) {
//nothing
}
});
}

@Override
protected Object doUnhandledOperation(final Operation operation, final Context context) {
throw new IllegalStateException(testExceptionMessage);
}
};
federatedStoreWithNoGetAllElements.initialise(GRAPH_ID_TEST_FEDERATED_STORE, null, federatedStoreProperties);

//Add graph.
federatedStoreWithNoGetAllElements.execute(
new AddGraph.Builder()
.graphId(GRAPH_ID_ACCUMULO)
.schema(loadSchemaFromJson(SCHEMA_ENTITY_BASIC_JSON))
.storeProperties(PROPERTIES)
.build(), contextTestUser());

//Add elements
federatedStoreWithNoGetAllElements.execute(new AddElements.Builder().input(entityBasic()).build(), contextTestUser());


// check handler is missing
assertThatException()
.isThrownBy(() -> federatedStoreWithNoGetAllElements.execute(new GetAllElements(), contextTestUser()))
.withMessageContaining(testExceptionMessage);
}

@Test
public void shouldDelegateUnknownOperationToSubGraphs() throws Exception {
//make custom store with no GetAllElements
FederatedStore federatedStoreWithNoGetAllElements = new FederatedStore() {
@Override
protected void addAdditionalOperationHandlers() {
super.addAdditionalOperationHandlers();
//No support for GetAllElements
addOperationHandler(GetAllElements.class, null);
//No support for adding Generic Handlers to Federation.
addOperationHandler(AddGraph.class, new FederatedAddGraphHandler() {
@Override
protected void addGenericHandler(final FederatedStore store, final Graph graph) {
//nothing
}
});
}
};
federatedStoreWithNoGetAllElements.initialise(GRAPH_ID_TEST_FEDERATED_STORE, null, federatedStoreProperties);

//Add graph
federatedStoreWithNoGetAllElements.execute(
new AddGraph.Builder()
.graphId(GRAPH_ID_ACCUMULO)
.schema(loadSchemaFromJson(SCHEMA_ENTITY_BASIC_JSON))
.storeProperties(PROPERTIES)
.build(), contextTestUser());

//Add elements
federatedStoreWithNoGetAllElements.execute(new AddElements.Builder().input(entityBasic()).build(), contextTestUser());

//Get Elements with no handler.
assertThat(federatedStoreWithNoGetAllElements.execute(new GetAllElements(), contextTestUser()))
.containsExactly(entityBasic());
}
}

0 comments on commit 6ff8f69

Please sign in to comment.