Search in sources :

Example 11 with Conditional

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

the class WhileHandlerTest method shouldThrowExceptionWhenPredicateCannotAcceptInputType.

@Test
public void shouldThrowExceptionWhenPredicateCannotAcceptInputType() throws OperationException {
    // Given
    final Predicate predicate = new IsFalse();
    final Object input = new EntitySeed();
    final Conditional conditional = new Conditional(predicate);
    final Context context = mock(Context.class);
    final Store store = mock(Store.class);
    final While operation = new While.Builder<>().input(input).conditional(conditional).operation(new GetElements()).build();
    final WhileHandler handler = new WhileHandler();
    // When / Then
    try {
        handler.doOperation(operation, context, store);
        fail("Exception expected");
    } catch (final OperationException e) {
        assertTrue(e.getMessage().contains("The predicate '" + predicate.getClass().getSimpleName() + "' cannot accept an input of type '" + input.getClass().getSimpleName() + "'"));
    }
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) IsFalse(uk.gov.gchq.koryphe.impl.predicate.IsFalse) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Store(uk.gov.gchq.gaffer.store.Store) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) While(uk.gov.gchq.gaffer.operation.impl.While) OperationException(uk.gov.gchq.gaffer.operation.OperationException) Predicate(java.util.function.Predicate) Test(org.junit.jupiter.api.Test)

Example 12 with Conditional

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

the class WhileHandlerTest method shouldFailPredicateTestAndNotExecuteDelegateOperation.

@Test
public void shouldFailPredicateTestAndNotExecuteDelegateOperation() throws OperationException {
    // Given
    final Edge input = new Edge.Builder().group("testEdge").source("src").dest("dest").directed(true).property("count", 3).build();
    final Map<Element, Object> transform = new Map.Builder<Element>().first(new ExtractProperty("count")).build();
    final Predicate predicate = new IsMoreThan(5);
    final Conditional conditional = new Conditional(predicate, transform);
    final Context context = mock(Context.class);
    final Store store = mock(Store.class);
    final GetElements getElements = mock(GetElements.class);
    final While operation = new While.Builder<>().input(input).maxRepeats(1).conditional(conditional).operation(getElements).build();
    final WhileHandler handler = new WhileHandler();
    // When
    handler.doOperation(operation, context, store);
    // Then
    verify(store, never()).execute((Output) getElements, context);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Element(uk.gov.gchq.gaffer.data.element.Element) Store(uk.gov.gchq.gaffer.store.Store) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) While(uk.gov.gchq.gaffer.operation.impl.While) Predicate(java.util.function.Predicate) ExtractProperty(uk.gov.gchq.gaffer.data.element.function.ExtractProperty) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.jupiter.api.Test)

Example 13 with Conditional

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

the class GetWalksIT method shouldReturnAllWalksWhenConditionalIsUnconfigured.

@Test
public void shouldReturnAllWalksWhenConditionalIsUnconfigured() throws Exception {
    final Iterable<Walk> walks = executeGetWalksApplyingConditional(new Conditional());
    assertThat(getPaths(walks)).isEqualTo("AED,ABC");
}
Also used : Walk(uk.gov.gchq.gaffer.data.graph.Walk) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Test(org.junit.Test)

Example 14 with Conditional

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

the class GetWalksIT method shouldGetPathsWithWhile.

@Test
public void shouldGetPathsWithWhile() throws Exception {
    // Given
    final GetElements operation = new GetElements.Builder().directedType(DirectedType.DIRECTED).view(new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().properties(TestPropertyNames.COUNT).build()).build()).inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING).build();
    final GetWalks op = new Builder().input(seedA).operations(new While.Builder<>().conditional(new Conditional(// This will always be true
    new Exists(), new Map.Builder<>().first(new AssertEntityIdsUnwrapped()).build())).operation(operation).maxRepeats(2).build()).build();
    // When
    final Iterable<Walk> results = graph.execute(op, getUser());
    // Then
    assertThat(getPaths(results)).isEqualTo("AED,ABC");
}
Also used : Walk(uk.gov.gchq.gaffer.data.graph.Walk) GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) Builder(uk.gov.gchq.gaffer.operation.impl.GetWalks.Builder) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Exists(uk.gov.gchq.koryphe.impl.predicate.Exists) HashMap(java.util.HashMap) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.Test)

Example 15 with Conditional

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

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