Search in sources :

Example 16 with Conditional

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

the class IfIT method shouldDoOtherwiseWhenConditionIsFalseAndNoThenOperation.

@Test
public void shouldDoOtherwiseWhenConditionIsFalseAndNoThenOperation() throws OperationException {
    // Given
    final If<Object, Object> ifOperation = new If<>();
    ifOperation.setInput(INPUT_CAMEL_CASE);
    ifOperation.setConditional(new Conditional(new IsA("java.lang.Integer")));
    ifOperation.setOtherwise(new Map<>(Lists.newArrayList(new ToLowerCase(), new ToList())));
    // When
    final Object output = graph.execute(ifOperation, getUser());
    // Then
    assertThat(output).isEqualTo(Lists.newArrayList(INPUT_CAMEL_CASE.toLowerCase())).isInstanceOf(List.class);
}
Also used : IsA(uk.gov.gchq.koryphe.impl.predicate.IsA) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) If(uk.gov.gchq.gaffer.operation.impl.If) ToLowerCase(uk.gov.gchq.koryphe.impl.function.ToLowerCase) ToList(uk.gov.gchq.koryphe.impl.function.ToList) Test(org.junit.Test)

Example 17 with Conditional

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

the class IfHandlerTest method shouldExecuteThenOperationWhenConditionMet.

@Test
public void shouldExecuteThenOperationWhenConditionMet() 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(true);
    // When
    handler.doOperation(filter, context, store);
    // Then
    verify(predicate).test(input);
    verify(store).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 18 with Conditional

use of uk.gov.gchq.gaffer.operation.util.Conditional 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 19 with Conditional

use of uk.gov.gchq.gaffer.operation.util.Conditional 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 20 with Conditional

use of uk.gov.gchq.gaffer.operation.util.Conditional 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)

Aggregations

Conditional (uk.gov.gchq.gaffer.operation.util.Conditional)30 Test (org.junit.jupiter.api.Test)18 If (uk.gov.gchq.gaffer.operation.impl.If)16 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)14 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)10 Test (org.junit.Test)9 Operation (uk.gov.gchq.gaffer.operation.Operation)9 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)9 GetWalks (uk.gov.gchq.gaffer.operation.impl.GetWalks)7 Map (uk.gov.gchq.gaffer.operation.impl.Map)6 While (uk.gov.gchq.gaffer.operation.impl.While)6 LinkedHashMap (java.util.LinkedHashMap)5 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)5 Context (uk.gov.gchq.gaffer.store.Context)5 ToList (uk.gov.gchq.koryphe.impl.function.ToList)5 IsA (uk.gov.gchq.koryphe.impl.predicate.IsA)5 HashMap (java.util.HashMap)4 Walk (uk.gov.gchq.gaffer.data.graph.Walk)4 LinkedList (java.util.LinkedList)3 Predicate (java.util.function.Predicate)3