Search in sources :

Example 6 with UpdateViewHook

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());
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Store(uk.gov.gchq.gaffer.store.Store) TestStore(uk.gov.gchq.gaffer.integration.store.TestStore) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) OperationView(uk.gov.gchq.gaffer.operation.graph.OperationView) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) UpdateViewHook(uk.gov.gchq.gaffer.graph.hook.UpdateViewHook) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Example 7 with UpdateViewHook

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());
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Store(uk.gov.gchq.gaffer.store.Store) TestStore(uk.gov.gchq.gaffer.integration.store.TestStore) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) UpdateViewHook(uk.gov.gchq.gaffer.graph.hook.UpdateViewHook) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Example 8 with UpdateViewHook

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);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) GraphHook(uk.gov.gchq.gaffer.graph.hook.GraphHook) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) UpdateViewHook(uk.gov.gchq.gaffer.graph.hook.UpdateViewHook) ArrayList(java.util.ArrayList) SchemaException(uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException) OperationException(uk.gov.gchq.gaffer.operation.OperationException) StoreException(uk.gov.gchq.gaffer.store.StoreException) IOException(java.io.IOException)

Aggregations

UpdateViewHook (uk.gov.gchq.gaffer.graph.hook.UpdateViewHook)8 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)8 Context (uk.gov.gchq.gaffer.store.Context)8 Test (org.junit.jupiter.api.Test)7 TestStore (uk.gov.gchq.gaffer.integration.store.TestStore)7 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)7 Operation (uk.gov.gchq.gaffer.operation.Operation)7 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)7 Store (uk.gov.gchq.gaffer.store.Store)7 StoreProperties (uk.gov.gchq.gaffer.store.StoreProperties)7 SchemaEdgeDefinition (uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition)5 Schema (uk.gov.gchq.gaffer.store.schema.Schema)4 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)3 OperationView (uk.gov.gchq.gaffer.operation.graph.OperationView)3 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 SchemaException (uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException)1 GraphHook (uk.gov.gchq.gaffer.graph.hook.GraphHook)1 OperationException (uk.gov.gchq.gaffer.operation.OperationException)1 StoreException (uk.gov.gchq.gaffer.store.StoreException)1