use of uk.gov.gchq.gaffer.operation.impl.If 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);
}
use of uk.gov.gchq.gaffer.operation.impl.If 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);
}
use of uk.gov.gchq.gaffer.operation.impl.If 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);
}
use of uk.gov.gchq.gaffer.operation.impl.If in project Gaffer by gchq.
the class AddOperationsToChainTest method shouldAddIfOperation.
@Test
public void shouldAddIfOperation() throws SerialisationException {
// Given
final GetWalks getWalks = new GetWalks();
final uk.gov.gchq.gaffer.operation.impl.Map map = new uk.gov.gchq.gaffer.operation.impl.Map();
final ToVertices toVertices = new ToVertices();
final ToSet toSet = new ToSet();
final Exists exists = new Exists();
final Limit limit = new Limit();
final GetAllElements getAllElements = new GetAllElements();
final GetElements getElements = new GetElements();
final Conditional conditional = new Conditional();
conditional.setPredicate(exists);
final If ifOp = new If.Builder<>().conditional(conditional).then(getElements).otherwise(getAllElements).build();
final AddOperationsToChain hook = new AddOperationsToChain();
final Map<String, List<Operation>> after = new HashMap<>();
final List<Operation> afterOps = new LinkedList<>();
afterOps.add(ifOp);
afterOps.add(limit);
after.put("uk.gov.gchq.gaffer.operation.impl.output.ToSet", afterOps);
hook.setAfter(after);
final OperationChain opChain = new OperationChain.Builder().first(getWalks).then(map).then(toVertices).then(toSet).build();
// When
hook.preExecute(opChain, new Context());
// Then
final OperationChain expectedOpChain = new OperationChain.Builder().first(getWalks).then(map).then(toVertices).then(toSet).then(ifOp).then(limit).build();
JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain));
}
use of uk.gov.gchq.gaffer.operation.impl.If in project Gaffer by gchq.
the class AddOperationsToChainTest method shouldHandleIfOperationWithNoConditionalOrOtherwise.
@Test
public void shouldHandleIfOperationWithNoConditionalOrOtherwise() throws SerialisationException {
// Given
AddOperationsToChain hook = fromJson(ADD_OPERATIONS_TO_CHAIN_RESOURCE_PATH);
Operation discardOutput = new DiscardOutput();
Operation splitStore = new SplitStoreFromFile();
If ifOp = new If.Builder<>().then(new GetElements()).build();
final OperationChain opChain = new OperationChain.Builder().first(ifOp).build();
// When
hook.preExecute(opChain, new Context(new User()));
// Then
final OperationChain expectedOpChain = new OperationChain.Builder().first(discardOutput).then(splitStore).then(new If.Builder<>().then(new OperationChain<>(new CountGroups(), new GetElements())).build()).then(new Count()).build();
JsonAssert.assertEquals(JSONSerialiser.serialise(expectedOpChain), JSONSerialiser.serialise(opChain));
}
Aggregations