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() + "'"));
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations