ConditionValueCompareOperatorGenerator implements ConditionValueGenerator<BinaryOperationExpression> { @Override public Optional<RouteValue> generate(final BinaryOperationExpression predicate, final Column column, final List<Object> parameters) { String operator = predicate.getOperator(); if (!isSupportedOperator(operator)) { return Optional.empty(); } Optional<Comparable<?>> routeValue = new ConditionValue(predicate.getRight(), parameters).getValue(); if (routeValue.isPresent()) { return generate(routeValue.get(), column, operator); } if (ExpressionConditionUtils.isNowExpression(predicate.getRight())) { return generate(new SPITimeService().getTime(), column, operator); } return Optional.empty(); }  @Override Optional<RouteValue> generate(final BinaryOperationExpression predicate, final Column column, final List<Object> parameters);  }
@SuppressWarnings("unchecked") @Test public void assertGenerateConditionValue() { int value = 1; BinaryOperationExpression rightValue = new BinaryOperationExpression(0, 0, null, new LiteralExpressionSegment(0, 0, value), "=", null); Optional<RouteValue> routeValue = generator.generate(rightValue, column, new LinkedList<>()); assertTrue(routeValue.isPresent()); assertTrue(((ListRouteValue<Integer>) routeValue.get()).getValues().contains(value)); }
@SuppressWarnings("unchecked") @Test public void assertGenerateConditionValueWithLessThanOperator() { BinaryOperationExpression rightValue = new BinaryOperationExpression(0, 0, null, new LiteralExpressionSegment(0, 0, 1), "<", null); Optional<RouteValue> routeValue = generator.generate(rightValue, column, new LinkedList<>()); assertTrue(routeValue.isPresent()); assertTrue(Range.lessThan(1).encloses(((RangeRouteValue<Integer>) routeValue.get()).getValueRange())); }
@SuppressWarnings("unchecked") @Test public void assertGenerateConditionValueWithGreaterThanOperator() { BinaryOperationExpression rightValue = new BinaryOperationExpression(0, 0, null, new LiteralExpressionSegment(0, 0, 1), ">", null); Optional<RouteValue> routeValue = generator.generate(rightValue, column, new LinkedList<>()); assertTrue(routeValue.isPresent()); assertTrue(Range.greaterThan(1).encloses(((RangeRouteValue<Integer>) routeValue.get()).getValueRange())); }
@SuppressWarnings("unchecked") @Test public void assertGenerateConditionValueWithAtMostOperator() { BinaryOperationExpression rightValue = new BinaryOperationExpression(0, 0, null, new LiteralExpressionSegment(0, 0, 1), "<=", null); Optional<RouteValue> routeValue = generator.generate(rightValue, column, new LinkedList<>()); assertTrue(routeValue.isPresent()); assertTrue(Range.atMost(1).encloses(((RangeRouteValue<Integer>) routeValue.get()).getValueRange())); }
@SuppressWarnings("unchecked") @Test public void assertGenerateConditionValueWithAtLeastOperator() { BinaryOperationExpression rightValue = new BinaryOperationExpression(0, 0, null, new LiteralExpressionSegment(0, 0, 1), ">=", null); Optional<RouteValue> routeValue = generator.generate(rightValue, column, new LinkedList<>()); assertTrue(routeValue.isPresent()); assertTrue(Range.atLeast(1).encloses(((RangeRouteValue<Integer>) routeValue.get()).getValueRange())); }
@Test public void assertGenerateConditionValueWithErrorOperator() { BinaryOperationExpression rightValue = new BinaryOperationExpression(0, 0, null, new LiteralExpressionSegment(0, 0, 1), "!=", null); assertFalse(generator.generate(rightValue, column, new LinkedList<>()).isPresent()); }
@Test public void assertGenerateConditionValueWithoutNowExpression() { BinaryOperationExpression rightValue = new BinaryOperationExpression(0, 0, null, new CommonExpressionSegment(0, 0, "value"), "=", null); assertFalse(generator.generate(rightValue, column, new LinkedList<>()).isPresent()); }
@SuppressWarnings("unchecked") @Test public void assertGenerateConditionValueWithNowExpression() { BinaryOperationExpression rightValue = new BinaryOperationExpression(0, 0, null, new LiteralExpressionSegment(0, 0, "now()"), "=", null); Optional<RouteValue> routeValue = generator.generate(rightValue, column, new LinkedList<>()); assertTrue(routeValue.isPresent()); assertFalse(((ListRouteValue<Integer>) routeValue.get()).getValues().isEmpty()); }