Search in sources :

Example 76 with Operation

use of uk.gov.gchq.gaffer.operation.Operation in project Gaffer by gchq.

the class ScoreOperationChainHandlerTest method shouldReAddDefaultScoreResolversWhenCallingSetMethod.

@Test
public void shouldReAddDefaultScoreResolversWhenCallingSetMethod() {
    // Given
    final ScoreOperationChainHandler handler = new ScoreOperationChainHandler();
    final Map<Class<? extends Operation>, ScoreResolver> defaultResolvers = ScoreOperationChainHandler.getDefaultScoreResolvers();
    final Map<Class<? extends Operation>, ScoreResolver> expectedMap = new HashMap<>();
    expectedMap.putAll(defaultResolvers);
    final Map<Class<? extends Operation>, ScoreResolver> inputMap = new HashMap<>();
    inputMap.put(GetElements.class, new DefaultScoreResolver(null));
    inputMap.put(GetAllElements.class, new DefaultScoreResolver(null));
    expectedMap.putAll(inputMap);
    // When
    handler.setScoreResolvers(inputMap);
    final Map<Class<? extends Operation>, ScoreResolver> results = handler.getScoreResolvers();
    // Then
    assertEquals(expectedMap.keySet(), results.keySet());
    assertTrue(results.get(NamedOperation.class) instanceof NamedOperationScoreResolver);
    assertTrue(results.get(If.class) instanceof IfScoreResolver);
    assertEquals(expectedMap.size(), results.size());
}
Also used : NamedOperationScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver) DefaultScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver) ScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver) IfScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.IfScoreResolver) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IfScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.IfScoreResolver) NamedOperationScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) DefaultScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver) Test(org.junit.jupiter.api.Test)

Example 77 with Operation

use of uk.gov.gchq.gaffer.operation.Operation in project Gaffer by gchq.

the class GraphTest method shouldCallAllGraphHooksOnExecuteFailureWhenRunningJob.

@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 GraphHook hook1 = mock(GraphHook.class);
    final GraphHook hook2 = mock(GraphHook.class);
    final Store store = mock(Store.class);
    final Schema schema = new Schema();
    final RuntimeException e = new RuntimeException("Store failed to execute operation chain");
    given(store.getSchema()).willReturn(schema);
    given(store.getProperties()).willReturn(new StoreProperties());
    given(hook1.onFailure(null, clonedOpChain, clonedContext, e)).willThrow(new RuntimeException("Hook1 failed in onFailure"));
    given(hook2.onFailure(null, clonedOpChain, clonedContext, e)).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<OperationChain> captor = ArgumentCaptor.forClass(OperationChain.class);
    given(store.executeJob(captor.capture(), eq(clonedContext))).willThrow(e);
    // When / Then
    try {
        graph.executeJob(opChain, context);
        fail("Exception expected");
    } catch (final RuntimeException runtimeE) {
        final InOrder inOrder = inOrder(context, hook1, hook2);
        inOrder.verify(context).setOriginalOpChain(opChain);
        inOrder.verify(hook1, never()).postExecute(any(), any(), any());
        inOrder.verify(hook2, never()).postExecute(any(), any(), any());
        inOrder.verify(hook1).onFailure(null, captor.getValue(), clonedContext, e);
        inOrder.verify(hook2).onFailure(null, captor.getValue(), clonedContext, e);
        final List<Operation> ops = captor.getValue().getOperations();
        assertThat(ops).hasSize(1);
        assertSame(operation, ops.get(0));
    }
}
Also used : InOrder(org.mockito.InOrder) GraphHook(uk.gov.gchq.gaffer.graph.hook.GraphHook) Schema(uk.gov.gchq.gaffer.store.schema.Schema) Store(uk.gov.gchq.gaffer.store.Store) TestStore(uk.gov.gchq.gaffer.integration.store.TestStore) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ArrayList(java.util.ArrayList) List(java.util.List) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Test(org.junit.jupiter.api.Test)

Example 78 with Operation

use of uk.gov.gchq.gaffer.operation.Operation in project Gaffer by gchq.

the class AddOperationsToChainTest method shouldAddIfOperation.

@Test
public void shouldAddIfOperation() throws SerialisationException {
    // Given
    final GetWalks getWalks = new GetWalks();
    final uk.gov.gchq.gaffer.operation.impl.Map map = new uk.gov.gchq.gaffer.operation.impl.Map();
    final ToVertices toVertices = new ToVertices();
    final ToSet toSet = new ToSet();
    final Exists exists = new Exists();
    final Limit limit = new Limit();
    final GetAllElements getAllElements = new GetAllElements();
    final GetElements getElements = new GetElements();
    final Conditional conditional = new Conditional();
    conditional.setPredicate(exists);
    final If ifOp = new If.Builder<>().conditional(conditional).then(getElements).otherwise(getAllElements).build();
    final AddOperationsToChain hook = new AddOperationsToChain();
    final Map<String, List<Operation>> after = new HashMap<>();
    final List<Operation> afterOps = new LinkedList<>();
    afterOps.add(ifOp);
    afterOps.add(limit);
    after.put("uk.gov.gchq.gaffer.operation.impl.output.ToSet", afterOps);
    hook.setAfter(after);
    final OperationChain opChain = new OperationChain.Builder().first(getWalks).then(map).then(toVertices).then(toSet).build();
    // When
    hook.preExecute(opChain, new Context());
    // Then
    final OperationChain expectedOpChain = new OperationChain.Builder().first(getWalks).then(map).then(toVertices).then(toSet).then(ifOp).then(limit).build();
    JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain));
}
Also used : HashMap(java.util.HashMap) GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Operation(uk.gov.gchq.gaffer.operation.Operation) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) LinkedList(java.util.LinkedList) List(java.util.List) Context(uk.gov.gchq.gaffer.store.Context) ToVertices(uk.gov.gchq.gaffer.operation.impl.output.ToVertices) LinkedList(java.util.LinkedList) Exists(uk.gov.gchq.koryphe.impl.predicate.Exists) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) HashMap(java.util.HashMap) Map(java.util.Map) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 79 with Operation

use of uk.gov.gchq.gaffer.operation.Operation in project Gaffer by gchq.

the class AddOperationsToChainTest method shouldFailQuietlyIfNestedOperationsCannotBeModified.

@Test
public void shouldFailQuietlyIfNestedOperationsCannotBeModified() throws SerialisationException {
    // Given
    AddOperationsToChain hook = fromJson(ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH);
    Operation discardOutput = new DiscardOutput();
    Operation splitStore = new SplitStoreFromFile();
    Operation validate = new Validate();
    Operation getAdjacentIds = new GetAdjacentIds();
    Operation count = new Count<>();
    Operation getElements = new GetElements();
    Operation getAllElements = new GetAllElements();
    TestUnmodifiableOperationsImpl nestedUnmodifiableOps = new TestUnmodifiableOperationsImpl(Arrays.asList(getAllElements, getElements));
    final OperationChain opChain = new OperationChain.Builder().first(getAdjacentIds).then(nestedUnmodifiableOps).build();
    // When
    hook.preExecute(opChain, new Context(new User()));
    // Then
    final OperationChain expectedOpChain = new OperationChain.Builder().first(discardOutput).then(splitStore).then(validate).then(getAdjacentIds).then(count).then(discardOutput).then(nestedUnmodifiableOps).then(count).build();
    JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain));
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) User(uk.gov.gchq.gaffer.user.User) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Operation(uk.gov.gchq.gaffer.operation.Operation) Count(uk.gov.gchq.gaffer.operation.impl.Count) SplitStoreFromFile(uk.gov.gchq.gaffer.operation.impl.SplitStoreFromFile) Validate(uk.gov.gchq.gaffer.operation.impl.Validate) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) DiscardOutput(uk.gov.gchq.gaffer.operation.impl.DiscardOutput) TestUnmodifiableOperationsImpl(uk.gov.gchq.gaffer.operation.TestUnmodifiableOperationsImpl) Test(org.junit.jupiter.api.Test)

Example 80 with Operation

use of uk.gov.gchq.gaffer.operation.Operation in project Gaffer by gchq.

the class AddOperationsToChainTest method shouldHandleIfOperationWithNoConditionalOrOtherwise.

@Test
public void shouldHandleIfOperationWithNoConditionalOrOtherwise() throws SerialisationException {
    // Given
    AddOperationsToChain hook = fromJson(ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH);
    Operation discardOutput = new DiscardOutput();
    Operation splitStore = new SplitStoreFromFile();
    If ifOp = new If.Builder<>().then(new GetElements()).build();
    final OperationChain opChain = new OperationChain.Builder().first(ifOp).build();
    // When
    hook.preExecute(opChain, new Context(new User()));
    // Then
    final OperationChain expectedOpChain = new OperationChain.Builder().first(discardOutput).then(splitStore).then(new If.Builder<>().then(new OperationChain<>(new CountGroups(), new GetElements())).build()).then(new Count()).build();
    JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain));
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) User(uk.gov.gchq.gaffer.user.User) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Operation(uk.gov.gchq.gaffer.operation.Operation) Count(uk.gov.gchq.gaffer.operation.impl.Count) SplitStoreFromFile(uk.gov.gchq.gaffer.operation.impl.SplitStoreFromFile) CountGroups(uk.gov.gchq.gaffer.operation.impl.CountGroups) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) DiscardOutput(uk.gov.gchq.gaffer.operation.impl.DiscardOutput) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Aggregations

Operation (uk.gov.gchq.gaffer.operation.Operation)136 Test (org.junit.jupiter.api.Test)88 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)49 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)44 Schema (uk.gov.gchq.gaffer.store.schema.Schema)41 Context (uk.gov.gchq.gaffer.store.Context)35 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)34 Store (uk.gov.gchq.gaffer.store.Store)28 StoreProperties (uk.gov.gchq.gaffer.store.StoreProperties)26 LinkedHashMap (java.util.LinkedHashMap)21 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)21 User (uk.gov.gchq.gaffer.user.User)21 ArrayList (java.util.ArrayList)18 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)18 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)17 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)16 HashSet (java.util.HashSet)15 HashMap (java.util.HashMap)13 TestStore (uk.gov.gchq.gaffer.integration.store.TestStore)13 OperationException (uk.gov.gchq.gaffer.operation.OperationException)13