IfHandler implements OutputOperationHandler<If<Object, Object>, Object> { @Override public Object doOperation(final If operation, final Context context, final Store store) throws OperationException { final Object input = operation.getInput(); boolean computedCondition; if (null == operation.getCondition()) { if (null == operation.getConditional() || null == operation.getConditional().getPredicate()) { computedCondition = false; } else { final Object intermediate; if (null == operation.getConditional().getTransform()) { intermediate = input; } else { final Operation transform = operation.getConditional().getTransform(); updateOperationInput(transform, input); intermediate = getResultsOrNull(transform, context, store); } try { computedCondition = operation.getConditional().getPredicate().test(intermediate); } catch (final ClassCastException e) { final String inputType = null != intermediate ? intermediate.getClass().getSimpleName() : "null"; throw new OperationException("The predicate '" + operation.getConditional().getPredicate().getClass().getSimpleName() + "' cannot accept an input of type '" + inputType + "'"); } } } else { computedCondition = operation.getCondition(); } final Operation nextOp = computedCondition ? operation.getThen() : operation.getOtherwise(); if (null == nextOp) { return input; } else { updateOperationInput(nextOp, input); return getResultsOrNull(nextOp, context, store); } }  @Override Object doOperation(final If operation, final Context context, final Store store);  }
@Test public void shouldExecuteThenOperationWhenConditionMet() throws OperationException { 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); handler.doOperation(filter, context, store); verify(predicate).test(input); verify(store).execute(then, context); verify(store, never()).execute(otherwise, context); }
@Test public void shouldExecuteOtherwiseOperationWhenConditionNotMet() throws OperationException { 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); handler.doOperation(filter, context, store); verify(predicate).test(input); verify(store, never()).execute(then, context); verify(store).execute(otherwise, context); }
@Test public void shouldReturnInitialInputForNullOperations() throws OperationException { 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(); final Object result = handler.doOperation(filter, context, store); assertEquals(result, input); verify(predicate).test(input); verify(store, never()).execute(then, context); verify(store, never()).execute(otherwise, context); }
@Test public void shouldExecuteThenWithBooleanCondition() throws OperationException { 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(); handler.doOperation(filter, context, store); verify(store).execute(then, context); verify(store, never()).execute(otherwise, context); }
@Test public void shouldExecuteCorrectlyWithOperationChainAsThen() throws OperationException { 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); final Object result = handler.doOperation(filter, context, store); verify(store).execute(then, context); verify(store, never()).execute(otherwise, context); }
@Test public void shouldCorrectlyExecutePrePredicateTransformUsingConditional() throws OperationException { 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); final Object result = handler.doOperation(filter, context, store); 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); }