Search in sources :

Example 6 with If

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

the class IfHandlerTest method shouldExecuteCorrectlyWithOperationChainAsThen.

@Test
public void shouldExecuteCorrectlyWithOperationChainAsThen() 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 OperationChain<Object> then = mock(OperationChain.class);
    final GetAllElements otherwise = mock(GetAllElements.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
    final Object result = handler.doOperation(filter, context, store);
    // Then
    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) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 7 with If

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

the class IfHandlerTest method shouldExecuteThenWithBooleanCondition.

@Test
public void shouldExecuteThenWithBooleanCondition() throws OperationException {
    // Given
    final Object input = Arrays.asList(new EntitySeed("1"), new EntitySeed("2"));
    final GetElements then = mock(GetElements.class);
    final GetAllElements otherwise = mock(GetAllElements.class);
    final If filter = new If.Builder<>().input(input).condition(true).then(then).otherwise(otherwise).build();
    final IfHandler handler = new IfHandler();
    // When
    handler.doOperation(filter, context, store);
    // Then
    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) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 8 with If

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

the class IfIT method shouldRunOtherwiseOperationsWhenConditionIsFalse.

@Test
public void shouldRunOtherwiseOperationsWhenConditionIsFalse() 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.setThen(new Map<>(Lists.newArrayList(new ToUpperCase(), new ToList())));
    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) ToUpperCase(uk.gov.gchq.koryphe.impl.function.ToUpperCase) Test(org.junit.Test)

Example 9 with If

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

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

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