Search in sources :

Example 11 with If

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

the class IfHandlerTest method shouldExecuteOtherwiseOperationWhenConditionNotMet.

@Test
public void shouldExecuteOtherwiseOperationWhenConditionNotMet() throws OperationException {
    // Given
    final Object input = Arrays.asList(new EntitySeed("1"), new EntitySeed("2"));
    final Conditional conditional = mock(Conditional.class);
    final Predicate<Object> predicate = mock(Predicate.class);
    final GetWalks then = mock(GetWalks.class);
    final GetElements otherwise = mock(GetElements.class);
    final If filter = new If.Builder<>().input(input).conditional(conditional).then(then).otherwise(otherwise).build();
    final IfHandler handler = new IfHandler();
    given(conditional.getPredicate()).willReturn(predicate);
    given(predicate.test(input)).willReturn(false);
    // When
    handler.doOperation(filter, context, store);
    // Then
    verify(predicate).test(input);
    verify(store, never()).execute(then, context);
    verify(store).execute(otherwise, context);
}
Also used : GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 12 with If

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

the class IfHandlerTest method shouldReturnInitialInputForNullOperations.

@Test
public void shouldReturnInitialInputForNullOperations() throws OperationException {
    // Given
    final Object input = Arrays.asList(new EntitySeed("1"), new EntitySeed("2"));
    final Conditional conditional = mock(Conditional.class);
    final Predicate<Object> predicate = mock(Predicate.class);
    final GetWalks then = null;
    final GetElements otherwise = null;
    final If filter = new If.Builder<>().input(input).conditional(conditional).then(then).otherwise(otherwise).build();
    given(conditional.getPredicate()).willReturn(predicate);
    given(predicate.test(input)).willReturn(true);
    final IfHandler handler = new IfHandler();
    // When
    final Object result = handler.doOperation(filter, context, store);
    // Then
    assertEquals(result, input);
    verify(predicate).test(input);
    verify(store, never()).execute(then, context);
    verify(store, never()).execute(otherwise, context);
}
Also used : GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 13 with If

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

the class IfHandlerTest method shouldCorrectlyExecutePrePredicateTransformUsingConditional.

@Test
public void shouldCorrectlyExecutePrePredicateTransformUsingConditional() throws OperationException {
    // Given
    final Object input = Arrays.asList(new EntitySeed("A"), new EntitySeed("B"));
    final Object intermediate = Arrays.asList(new EntitySeed("1"), new EntitySeed("2"));
    final Conditional conditional = mock(Conditional.class);
    final Predicate<Object> predicate = mock(Predicate.class);
    final OperationChain<Object> transform = mock(OperationChain.class);
    final GetElements then = mock(GetElements.class);
    final GetAllElements otherwise = mock(GetAllElements.class);
    final If filter = new If.Builder<>().input(input).conditional(conditional).then(then).build();
    final IfHandler handler = new IfHandler();
    given(conditional.getPredicate()).willReturn(predicate);
    given(conditional.getTransform()).willReturn(transform);
    given(store.execute(transform, context)).willReturn(intermediate);
    given(predicate.test(intermediate)).willReturn(true);
    // When
    final Object result = handler.doOperation(filter, context, store);
    // Then
    verify(predicate).test(intermediate);
    verify(predicate, never()).test(input);
    verify(store).execute(transform, context);
    verify(store).execute(then, context);
    verify(store, never()).execute(otherwise, context);
}
Also used : EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 14 with If

use of uk.gov.gchq.gaffer.operation.impl.If 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 15 with If

use of uk.gov.gchq.gaffer.operation.impl.If 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

If (uk.gov.gchq.gaffer.operation.impl.If)20 Conditional (uk.gov.gchq.gaffer.operation.util.Conditional)16 Test (org.junit.jupiter.api.Test)14 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)10 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)10 Operation (uk.gov.gchq.gaffer.operation.Operation)7 GetWalks (uk.gov.gchq.gaffer.operation.impl.GetWalks)7 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)6 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)6 Test (org.junit.Test)5 ToList (uk.gov.gchq.koryphe.impl.function.ToList)5 IsA (uk.gov.gchq.koryphe.impl.predicate.IsA)5 LinkedHashMap (java.util.LinkedHashMap)4 Count (uk.gov.gchq.gaffer.operation.impl.Count)4 CountGroups (uk.gov.gchq.gaffer.operation.impl.CountGroups)3 DiscardOutput (uk.gov.gchq.gaffer.operation.impl.DiscardOutput)3 Limit (uk.gov.gchq.gaffer.operation.impl.Limit)3 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)3 Context (uk.gov.gchq.gaffer.store.Context)3 User (uk.gov.gchq.gaffer.user.User)3