use of uk.gov.gchq.gaffer.graph.hook.UpdateViewHook in project Gaffer by gchq.
the class GraphTest method shouldCorrectlyAddExtraGroupsFromSchemaViewWithUpdateViewHookWhenNotInBlacklist.
@Test
public void shouldCorrectlyAddExtraGroupsFromSchemaViewWithUpdateViewHookWhenNotInBlacklist() throws OperationException {
// Given
operation = new GetElements.Builder().build();
final UpdateViewHook updateViewHook = new UpdateViewHook.Builder().addExtraGroups(true).blackListElementGroups(Collections.singleton(TestGroups.EDGE)).build();
given(opChain.getOperations()).willReturn(Lists.newArrayList(operation));
given(opChain.shallowClone()).willReturn(clonedOpChain);
given(clonedOpChain.getOperations()).willReturn(Lists.newArrayList(operation));
given(clonedOpChain.flatten()).willReturn(Arrays.asList(operation));
final Store store = mock(Store.class);
given(store.getSchema()).willReturn(new Schema.Builder().edge(TestGroups.EDGE_4, new SchemaEdgeDefinition()).edge(TestGroups.EDGE_5, new SchemaEdgeDefinition()).edge(TestGroups.EDGE, new SchemaEdgeDefinition()).build());
given(store.getProperties()).willReturn(new StoreProperties());
final Graph graph = new Graph.Builder().config(new GraphConfig.Builder().graphId(GRAPH_ID).addHook(updateViewHook).build()).storeProperties(StreamUtil.storeProps(getClass())).store(store).build();
final ArgumentCaptor<OperationChain> captor = ArgumentCaptor.forClass(OperationChain.class);
final ArgumentCaptor<Context> contextCaptor1 = ArgumentCaptor.forClass(Context.class);
given(store.execute(captor.capture(), contextCaptor1.capture())).willReturn(new ArrayList<>());
// When / Then
graph.execute(opChain, user);
final List<Operation> ops = captor.getValue().getOperations();
JsonAssert.assertEquals(new View.Builder().edge(TestGroups.EDGE_5).edge(TestGroups.EDGE_4).build().toCompactJson(), ((GetElements) ops.get(0)).getView().toCompactJson());
}
use of uk.gov.gchq.gaffer.graph.hook.UpdateViewHook in project Gaffer by gchq.
the class GraphTest method shouldFillSchemaViewAndManipulateViewRemovingBlacklistedEdgeLeavingEmptyViewUsingUpdateViewHook.
@Test
public void shouldFillSchemaViewAndManipulateViewRemovingBlacklistedEdgeLeavingEmptyViewUsingUpdateViewHook() throws OperationException {
// Given
operation = new GetElements.Builder().build();
final UpdateViewHook updateViewHook = new UpdateViewHook.Builder().blackListElementGroups(Collections.singleton(TestGroups.EDGE)).build();
given(opChain.getOperations()).willReturn(Lists.newArrayList(operation));
given(opChain.shallowClone()).willReturn(clonedOpChain);
given(clonedOpChain.getOperations()).willReturn(Lists.newArrayList(operation));
given(clonedOpChain.flatten()).willReturn(Arrays.asList(operation));
final Store store = mock(Store.class);
given(store.getSchema()).willReturn(new Schema.Builder().edge(TestGroups.EDGE_5, new SchemaEdgeDefinition()).edge(TestGroups.EDGE, new SchemaEdgeDefinition()).build());
given(store.getProperties()).willReturn(new StoreProperties());
final Graph graph = new Graph.Builder().config(new GraphConfig.Builder().graphId(GRAPH_ID).addHook(updateViewHook).build()).storeProperties(StreamUtil.storeProps(getClass())).store(store).build();
final ArgumentCaptor<OperationChain> captor = ArgumentCaptor.forClass(OperationChain.class);
final ArgumentCaptor<Context> contextCaptor1 = ArgumentCaptor.forClass(Context.class);
given(store.execute(captor.capture(), contextCaptor1.capture())).willReturn(new ArrayList<>());
// When / Then
graph.execute(opChain, user);
final List<Operation> ops = captor.getValue().getOperations();
JsonAssert.assertEquals(new View.Builder().edge(TestGroups.EDGE_5).build().toCompactJson(), ((GetElements) ops.get(0)).getView().toCompactJson());
}
use of uk.gov.gchq.gaffer.graph.hook.UpdateViewHook in project Gaffer by gchq.
the class Graph method _execute.
private <O> GraphResult<O> _execute(final StoreExecuter<O> storeExecuter, final GraphRequest<?> request) throws OperationException {
if (null == request) {
throw new IllegalArgumentException("A request is required");
}
if (null == request.getContext()) {
throw new IllegalArgumentException("A context is required");
}
request.getContext().setOriginalOpChain(request.getOperationChain());
final Context clonedContext = request.getContext().shallowClone();
final OperationChain clonedOpChain = request.getOperationChain().shallowClone();
O result = null;
try {
updateOperationChainView(clonedOpChain);
for (final GraphHook graphHook : config.getHooks()) {
graphHook.preExecute(clonedOpChain, clonedContext);
}
// TODO - remove in V2
// This updates the view, used for empty or null views, for
// example if there is a NamedOperation that has been resolved
// that contains an empty view
updateOperationChainView(clonedOpChain);
// Runs the updateGraphHook instance (if set) or if not runs a
// new instance
ArrayList<UpdateViewHook> hookInstances = new ArrayList<>();
for (final GraphHook graphHook : config.getHooks()) {
if (UpdateViewHook.class.isAssignableFrom(graphHook.getClass())) {
hookInstances.add((UpdateViewHook) graphHook);
}
}
if (hookInstances.size() == 0) {
hookInstances.add(new UpdateViewHook());
}
for (final UpdateViewHook hook : hookInstances) {
hook.preExecute(clonedOpChain, clonedContext);
}
result = (O) storeExecuter.execute(clonedOpChain, clonedContext);
for (final GraphHook graphHook : config.getHooks()) {
result = graphHook.postExecute(result, clonedOpChain, clonedContext);
}
} catch (final Exception e) {
for (final GraphHook graphHook : config.getHooks()) {
try {
result = graphHook.onFailure(result, clonedOpChain, clonedContext, e);
} catch (final Exception graphHookE) {
LOGGER.warn("Error in graphHook " + graphHook.getClass().getSimpleName() + ": " + graphHookE.getMessage(), graphHookE);
}
}
CloseableUtil.close(clonedOpChain);
CloseableUtil.close(result);
throw e;
}
return new GraphResult<>(result, clonedContext);
}
Aggregations