Search in sources :

Example 6 with While

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

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

the class WhileHandlerTest method shouldExecuteNonOutputOperation.

@Test
public void shouldExecuteNonOutputOperation() throws OperationException {
    // Given
    final AddElements addElements = new AddElements.Builder().input(new Edge.Builder().build()).build();
    final Context context = mock(Context.class);
    final Store store = mock(Store.class);
    final While operation = new While.Builder<>().operation(addElements).condition(true).maxRepeats(3).build();
    final WhileHandler handler = new WhileHandler();
    // When
    handler.doOperation(operation, context, store);
    // Then
    verify(store, times(3)).execute(addElements, context);
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Context(uk.gov.gchq.gaffer.store.Context) Store(uk.gov.gchq.gaffer.store.Store) While(uk.gov.gchq.gaffer.operation.impl.While) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.jupiter.api.Test)

Example 8 with While

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

the class WhileHandlerTest method shouldNotRepeatWhileConditionIsFalse.

@Test
public void shouldNotRepeatWhileConditionIsFalse() throws OperationException {
    // Given
    final EntitySeed input = mock(EntitySeed.class);
    final int maxRepeats = 3;
    final boolean condition = false;
    final Operation delegate = mock(GetElements.class);
    final Context context = mock(Context.class);
    final Store store = mock(Store.class);
    final While operation = new While.Builder<>().input(input).maxRepeats(maxRepeats).condition(condition).operation(delegate).build();
    final WhileHandler handler = new WhileHandler();
    // When
    handler.doOperation(operation, context, store);
    // Then
    verify(store, never()).execute((Output) delegate, context);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Store(uk.gov.gchq.gaffer.store.Store) Operation(uk.gov.gchq.gaffer.operation.Operation) While(uk.gov.gchq.gaffer.operation.impl.While) Test(org.junit.jupiter.api.Test)

Example 9 with While

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

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

the class WhileHandlerTest method shouldRepeatDelegateOperationUntilMaxRepeatsReached.

@Test
public void shouldRepeatDelegateOperationUntilMaxRepeatsReached() throws OperationException {
    // Given
    final List<EntitySeed> input = Collections.singletonList(mock(EntitySeed.class));
    final int maxRepeats = 3;
    final GetAdjacentIds delegate = mock(GetAdjacentIds.class);
    final GetAdjacentIds delegateClone1 = mock(GetAdjacentIds.class);
    final GetAdjacentIds delegateClone2 = mock(GetAdjacentIds.class);
    final GetAdjacentIds delegateClone3 = mock(GetAdjacentIds.class);
    final Context context = mock(Context.class);
    final Store store = mock(Store.class);
    given(delegate.shallowClone()).willReturn(delegateClone1, delegateClone2, delegateClone3);
    final CloseableIterable result1 = mock(CloseableIterable.class);
    final CloseableIterable result2 = mock(CloseableIterable.class);
    final CloseableIterable result3 = mock(CloseableIterable.class);
    given(store.execute(delegateClone1, context)).willReturn(result1);
    given(store.execute(delegateClone2, context)).willReturn(result2);
    given(store.execute(delegateClone3, context)).willReturn(result3);
    final While operation = new While.Builder<>().input(input).maxRepeats(maxRepeats).operation(delegate).build();
    final WhileHandler handler = new WhileHandler();
    // When
    final Object result = handler.doOperation(operation, context, store);
    // Then
    verify(delegateClone1, times(1)).getInput();
    verify(delegateClone2, times(1)).getInput();
    verify(delegateClone3, times(1)).getInput();
    verify(store).execute((Output) delegateClone1, context);
    verify(store).execute((Output) delegateClone2, context);
    verify(store).execute((Output) delegateClone3, context);
    assertSame(result3, result);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Store(uk.gov.gchq.gaffer.store.Store) While(uk.gov.gchq.gaffer.operation.impl.While) Test(org.junit.jupiter.api.Test)

Aggregations

While (uk.gov.gchq.gaffer.operation.impl.While)15 Test (org.junit.jupiter.api.Test)12 Context (uk.gov.gchq.gaffer.store.Context)8 Store (uk.gov.gchq.gaffer.store.Store)8 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)7 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)7 Conditional (uk.gov.gchq.gaffer.operation.util.Conditional)6 Operation (uk.gov.gchq.gaffer.operation.Operation)4 Predicate (java.util.function.Predicate)3 Edge (uk.gov.gchq.gaffer.data.element.Edge)3 LinkedHashMap (java.util.LinkedHashMap)2 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)2 Element (uk.gov.gchq.gaffer.data.element.Element)2 OperationException (uk.gov.gchq.gaffer.operation.OperationException)2 IsMoreThan (uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)2 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1 ExtractProperty (uk.gov.gchq.gaffer.data.element.function.ExtractProperty)1 UnwrapEntityId (uk.gov.gchq.gaffer.data.element.function.UnwrapEntityId)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1