diff --git a/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/AndOperator.java b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/AndOperator.java new file mode 100644 index 0000000000..20e552c688 --- /dev/null +++ b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/AndOperator.java @@ -0,0 +1,32 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import javax.inject.Named; + +import static com.google.common.base.Preconditions.checkArgument; + +@Named +class AndOperator implements Operator { + private static final Integer SYMBOL = DataPrepperExpressionParser.AND; + private static final String DISPLAY_NAME = DataPrepperExpressionParser.VOCABULARY + .getDisplayName(DataPrepperExpressionParser.AND); + + @Override + public Integer getSymbol() { + return SYMBOL; + } + + @Override + public Boolean evaluate(final Object... args) { + checkArgument(args.length == 2, DISPLAY_NAME + " requires operands length to be 2."); + checkArgument(args[0] instanceof Boolean, DISPLAY_NAME + " requires left operand to be Boolean."); + checkArgument(args[1] instanceof Boolean, DISPLAY_NAME + " requires right operand to be Boolean."); + return ((Boolean) args[0]) && ((Boolean) args[1]); + } +} diff --git a/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/GenericEqualOperator.java b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/GenericEqualOperator.java new file mode 100644 index 0000000000..7ef277b2bd --- /dev/null +++ b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/GenericEqualOperator.java @@ -0,0 +1,37 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import java.util.function.BiPredicate; + +import static com.google.common.base.Preconditions.checkArgument; + +public class GenericEqualOperator implements Operator { + private final Integer symbol; + private final String displayName; + private final BiPredicate operation; + + public GenericEqualOperator(final Integer symbol, BiPredicate operation) { + this.symbol = symbol; + displayName = DataPrepperExpressionParser.VOCABULARY.getDisplayName(symbol); + this.operation = operation; + } + + @Override + public Integer getSymbol() { + return symbol; + } + + @Override + public Boolean evaluate(final Object... args) { + checkArgument(args.length == 2, displayName + " requires operands length to be 2."); + final Object leftOperand = args[0]; + final Object rightOperand = args[1]; + return operation.test(leftOperand, rightOperand); + } +} diff --git a/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/GenericInSetOperator.java b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/GenericInSetOperator.java new file mode 100644 index 0000000000..4e8810f66d --- /dev/null +++ b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/GenericInSetOperator.java @@ -0,0 +1,39 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import java.util.Set; +import java.util.function.BiPredicate; + +import static com.google.common.base.Preconditions.checkArgument; + +public class GenericInSetOperator implements Operator { + private final Integer symbol; + private final String displayName; + private final BiPredicate operation; + + public GenericInSetOperator(final Integer symbol, BiPredicate operation) { + this.symbol = symbol; + displayName = DataPrepperExpressionParser.VOCABULARY.getDisplayName(symbol); + this.operation = operation; + } + + @Override + public Integer getSymbol() { + return symbol; + } + + @Override + public Boolean evaluate(final Object... args) { + checkArgument(args.length == 2, displayName + " requires operands length to be 2."); + if (!(args[1] instanceof Set)) { + throw new IllegalArgumentException(displayName + " requires right operand to be Set."); + } + return operation.test(args[0], args[1]); + } +} diff --git a/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/GenericRegexMatchOperator.java b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/GenericRegexMatchOperator.java new file mode 100644 index 0000000000..6003bd331d --- /dev/null +++ b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/GenericRegexMatchOperator.java @@ -0,0 +1,42 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import java.util.function.BiPredicate; +import java.util.regex.PatternSyntaxException; + +import static com.google.common.base.Preconditions.checkArgument; + +public class GenericRegexMatchOperator implements Operator { + private final Integer symbol; + private final String displayName; + private final BiPredicate operation; + + public GenericRegexMatchOperator(final Integer symbol, BiPredicate operation) { + this.symbol = symbol; + displayName = DataPrepperExpressionParser.VOCABULARY.getDisplayName(symbol); + this.operation = operation; + } + + @Override + public Integer getSymbol() { + return symbol; + } + + @Override + public Boolean evaluate(final Object... args) { + checkArgument(args.length == 2, displayName + " requires operands length needs to be 2."); + checkArgument(args[0] instanceof String, displayName + " requires left operand to be String."); + checkArgument(args[1] instanceof String, displayName + " requires right operand to be String."); + try { + return operation.test(args[0], args[1]); + } catch (final PatternSyntaxException e) { + throw new IllegalArgumentException(e); + } + } +} diff --git a/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/NotOperator.java b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/NotOperator.java new file mode 100644 index 0000000000..549a036c0c --- /dev/null +++ b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/NotOperator.java @@ -0,0 +1,31 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import javax.inject.Named; + +import static com.google.common.base.Preconditions.checkArgument; + +@Named +class NotOperator implements Operator { + private static final Integer SYMBOL = DataPrepperExpressionParser.NOT; + private static final String DISPLAY_NAME = DataPrepperExpressionParser.VOCABULARY + .getDisplayName(DataPrepperExpressionParser.NOT); + + @Override + public Integer getSymbol() { + return SYMBOL; + } + + @Override + public Boolean evaluate(final Object... args) { + checkArgument(args.length == 1, DISPLAY_NAME + " requires operands length to be 1."); + checkArgument(args[0] instanceof Boolean, DISPLAY_NAME + " requires operand to be Boolean."); + return !((Boolean) args[0]); + } +} diff --git a/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/NumericCompareOperator.java b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/NumericCompareOperator.java new file mode 100644 index 0000000000..160c979904 --- /dev/null +++ b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/NumericCompareOperator.java @@ -0,0 +1,51 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import java.util.Map; +import java.util.function.BiFunction; + +import static com.google.common.base.Preconditions.checkArgument; + +public class NumericCompareOperator implements Operator { + private final Integer symbol; + private final String displayName; + private final Map, Map, BiFunction>> operandsToOperationMap; + + public NumericCompareOperator( + final Integer symbol, + final Map, Map, BiFunction>> operandsToOperationMap) { + this.symbol = symbol; + displayName = DataPrepperExpressionParser.VOCABULARY.getDisplayName(symbol); + this.operandsToOperationMap = operandsToOperationMap; + } + + @Override + public Integer getSymbol() { + return symbol; + } + + @Override + public Boolean evaluate(final Object... args) { + checkArgument(args.length == 2, displayName + " requires operands length needs to be 2."); + final Object leftValue = args[0]; + final Object rightValue = args[1]; + final Class leftValueClass = leftValue.getClass(); + final Class rightValueClass = rightValue.getClass(); + if (!operandsToOperationMap.containsKey(leftValueClass)) { + throw new IllegalArgumentException(displayName + " requires left operand to be either Float or Integer."); + } + Map, BiFunction> rightOperandToOperation = + operandsToOperationMap.get(leftValueClass); + if (!rightOperandToOperation.containsKey(rightValueClass)) { + throw new IllegalArgumentException(displayName + " requires right operand to be either Float or Integer."); + } + final BiFunction operation = rightOperandToOperation.get(rightValueClass); + return operation.apply(leftValue, rightValue); + } +} diff --git a/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/Operator.java b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/Operator.java new file mode 100644 index 0000000000..cf1804240c --- /dev/null +++ b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/Operator.java @@ -0,0 +1,19 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +interface Operator { + Integer getSymbol(); + + /** + * @since 1.3 + * Placeholder interface for implementing Data-Prepper supported binary/unary operations on operands that + * returns custom type T. + * @param args operands + * @return T + */ + T evaluate(final Object... args); +} diff --git a/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/OperatorFactory.java b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/OperatorFactory.java new file mode 100644 index 0000000000..50d8c7cb1d --- /dev/null +++ b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/OperatorFactory.java @@ -0,0 +1,130 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; +import org.springframework.context.annotation.Bean; + +import javax.inject.Named; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.BiFunction; +import java.util.function.BiPredicate; + +@Named +public class OperatorFactory { + public final BiPredicate regexEquals = (x, y) -> ((String) x).matches((String) y); + public final BiPredicate equals = Objects::equals; + public final BiPredicate inSet = (x, y) -> ((Set) y).contains(x); + + @Bean + public NumericCompareOperator greaterThanOperator() { + final Map, Map, BiFunction>> + operandsToOperationMap = new HashMap<>(); + final Map, BiFunction> intOperations = + new HashMap, BiFunction>() {{ + put(Integer.class, (lhs, rhs) -> (Integer) lhs > (Integer) rhs); + put(Float.class, (lhs, rhs) -> (Integer) lhs > (Float) rhs);}}; + final Map, BiFunction> floatOperations = + new HashMap, BiFunction>() {{ + put(Integer.class, (lhs, rhs) -> (Float) lhs > (Integer) rhs); + put(Float.class, (lhs, rhs) -> (Float) lhs > (Float) rhs);}}; + + operandsToOperationMap.put(Integer.class, intOperations); + operandsToOperationMap.put(Float.class, floatOperations); + + return new NumericCompareOperator(DataPrepperExpressionParser.GT, operandsToOperationMap); + } + + @Bean + public NumericCompareOperator greaterThanOrEqualOperator() { + final Map, Map, BiFunction>> + operandsToOperationMap = new HashMap<>(); + final Map, BiFunction> intOperations = + new HashMap, BiFunction>() {{ + put(Integer.class, (lhs, rhs) -> (Integer) lhs >= (Integer) rhs); + put(Float.class, (lhs, rhs) -> (Integer) lhs >= (Float) rhs);}}; + final Map, BiFunction> floatOperations = + new HashMap, BiFunction>() {{ + put(Integer.class, (lhs, rhs) -> (Float) lhs >= (Integer) rhs); + put(Float.class, (lhs, rhs) -> (Float) lhs >= (Float) rhs);}}; + + operandsToOperationMap.put(Integer.class, intOperations); + operandsToOperationMap.put(Float.class, floatOperations); + + return new NumericCompareOperator(DataPrepperExpressionParser.GTE, operandsToOperationMap); + } + + @Bean + public NumericCompareOperator lessThanOperator() { + final Map, Map, BiFunction>> + operandsToOperationMap = new HashMap<>(); + final Map, BiFunction> intOperations = + new HashMap, BiFunction>() {{ + put(Integer.class, (lhs, rhs) -> (Integer) lhs < (Integer) rhs); + put(Float.class, (lhs, rhs) -> (Integer) lhs < (Float) rhs);}}; + final Map, BiFunction> floatOperations = + new HashMap, BiFunction>() {{ + put(Integer.class, (lhs, rhs) -> (Float) lhs < (Integer) rhs); + put(Float.class, (lhs, rhs) -> (Float) lhs < (Float) rhs);}}; + + operandsToOperationMap.put(Integer.class, intOperations); + operandsToOperationMap.put(Float.class, floatOperations); + + return new NumericCompareOperator(DataPrepperExpressionParser.LT, operandsToOperationMap); + } + + @Bean + public NumericCompareOperator lessThanOrEqualOperator() { + final Map, Map, BiFunction>> + operandsToOperationMap = new HashMap<>(); + final Map, BiFunction> intOperations = + new HashMap, BiFunction>() {{ + put(Integer.class, (lhs, rhs) -> (Integer) lhs <= (Integer) rhs); + put(Float.class, (lhs, rhs) -> (Integer) lhs <= (Float) rhs);}}; + final Map, BiFunction> floatOperations = + new HashMap, BiFunction>() {{ + put(Integer.class, (lhs, rhs) -> (Float) lhs <= (Integer) rhs); + put(Float.class, (lhs, rhs) -> (Float) lhs <= (Float) rhs);}}; + + operandsToOperationMap.put(Integer.class, intOperations); + operandsToOperationMap.put(Float.class, floatOperations); + + return new NumericCompareOperator(DataPrepperExpressionParser.LTE, operandsToOperationMap); + } + + @Bean + public GenericRegexMatchOperator regexEqualOperator() { + return new GenericRegexMatchOperator(DataPrepperExpressionParser.MATCH_REGEX_PATTERN, regexEquals); + } + + @Bean + public GenericRegexMatchOperator regexNotEqualOperator() { + return new GenericRegexMatchOperator(DataPrepperExpressionParser.NOT_MATCH_REGEX_PATTERN, regexEquals.negate()); + } + + @Bean + public GenericEqualOperator equalOperator() { + return new GenericEqualOperator(DataPrepperExpressionParser.EQUAL, equals); + } + + @Bean + public GenericEqualOperator notEqualOperator() { + return new GenericEqualOperator(DataPrepperExpressionParser.NOT_EQUAL, equals.negate()); + } + + @Bean + public GenericInSetOperator inSetOperator() { + return new GenericInSetOperator(DataPrepperExpressionParser.IN_SET, inSet); + } + + @Bean + public GenericInSetOperator notInSetOperator() { + return new GenericInSetOperator(DataPrepperExpressionParser.NOT_IN_SET, inSet.negate()); + } +} diff --git a/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/OrOperator.java b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/OrOperator.java new file mode 100644 index 0000000000..a76f6792b4 --- /dev/null +++ b/data-prepper-expression/src/main/java/org/opensearch/dataprepper/expression/OrOperator.java @@ -0,0 +1,32 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import javax.inject.Named; + +import static com.google.common.base.Preconditions.checkArgument; + +@Named +class OrOperator implements Operator { + private static final Integer SYMBOL = DataPrepperExpressionParser.OR; + private static final String DISPLAY_NAME = DataPrepperExpressionParser.VOCABULARY + .getDisplayName(DataPrepperExpressionParser.OR); + + @Override + public Integer getSymbol() { + return SYMBOL; + } + + @Override + public Boolean evaluate(final Object... args) { + checkArgument(args.length == 2, DISPLAY_NAME + " requires operands length to be 2."); + checkArgument(args[0] instanceof Boolean, DISPLAY_NAME + " requires left operand to be Boolean."); + checkArgument(args[1] instanceof Boolean, DISPLAY_NAME + " requires right Operand to be Boolean."); + return ((Boolean) args[0]) || ((Boolean) args[1]); + } +} diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/AndOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/AndOperatorTest.java new file mode 100644 index 0000000000..e8b6d6a4c1 --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/AndOperatorTest.java @@ -0,0 +1,42 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class AndOperatorTest { + final AndOperator objectUnderTest = new AndOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.AND)); + } + + @Test + void testEvalValidArgs() { + assertThat(objectUnderTest.evaluate(true, true), is(true)); + assertThat(objectUnderTest.evaluate(true, false), is(false)); + assertThat(objectUnderTest.evaluate(false, true), is(false)); + assertThat(objectUnderTest.evaluate(false, false), is(false)); + } + + @Test + void testEvalInValidArgLength() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(true)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(true, true, false)); + } + + @Test + void testEvalInValidArgType() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(true, 1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, true)); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/EqualOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/EqualOperatorTest.java new file mode 100644 index 0000000000..f186353a63 --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/EqualOperatorTest.java @@ -0,0 +1,44 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; +import org.opensearch.dataprepper.expression.util.TestObject; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class EqualOperatorTest { + final GenericEqualOperator objectUnderTest = new OperatorFactory().equalOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.EQUAL)); + } + + @Test + void testEvalValidArgs() { + final TestObject testObject1 = new TestObject("1"); + final TestObject testObject2 = new TestObject("1"); + final TestObject testObject3 = new TestObject("2"); + assertThat(objectUnderTest.evaluate(testObject1, testObject2), is(true)); + assertThat(objectUnderTest.evaluate(testObject1, testObject3), is(false)); + assertThat(objectUnderTest.evaluate(null, testObject1), is(false)); + assertThat(objectUnderTest.evaluate(testObject1, null), is(false)); + assertThat(objectUnderTest.evaluate(null, null), is(true)); + } + + @Test + void testEvalInValidArgLength() { + final TestObject testObject1 = new TestObject("1"); + final TestObject testObject2 = new TestObject("1"); + final TestObject testObject3 = new TestObject("2"); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(testObject1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(testObject1, testObject2, testObject3)); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/GreaterThanOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/GreaterThanOperatorTest.java new file mode 100644 index 0000000000..237329559b --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/GreaterThanOperatorTest.java @@ -0,0 +1,52 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class GreaterThanOperatorTest { + final Operator objectUnderTest = new OperatorFactory().greaterThanOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.GT)); + } + + @Test + void testEvalValidArgs() { + assertThat(objectUnderTest.evaluate(2, 1), is(true)); + assertThat(objectUnderTest.evaluate(1, 2), is(false)); + assertThat(objectUnderTest.evaluate(1, 1), is(false)); + assertThat(objectUnderTest.evaluate(2f, 1), is(true)); + assertThat(objectUnderTest.evaluate(1f, 2), is(false)); + assertThat(objectUnderTest.evaluate(1f, 1), is(false)); + assertThat(objectUnderTest.evaluate(2, 1f), is(true)); + assertThat(objectUnderTest.evaluate(1, 2f), is(false)); + assertThat(objectUnderTest.evaluate(1, 1f), is(false)); + assertThat(objectUnderTest.evaluate(2f, 1f), is(true)); + assertThat(objectUnderTest.evaluate(1f, 2f), is(false)); + assertThat(objectUnderTest.evaluate(1f, 1f), is(false)); + } + + @Test + void testEvalInValidArgLength() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 2, 3)); + } + + @Test + void testEvalInValidArgType() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1L, 1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1.0, 1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 1L)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 1.0)); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/GreaterThanOrEqualOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/GreaterThanOrEqualOperatorTest.java new file mode 100644 index 0000000000..aa557ee005 --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/GreaterThanOrEqualOperatorTest.java @@ -0,0 +1,52 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class GreaterThanOrEqualOperatorTest { + final Operator objectUnderTest = new OperatorFactory().greaterThanOrEqualOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.GTE)); + } + + @Test + void testEvalValidArgs() { + assertThat(objectUnderTest.evaluate(2, 1), is(true)); + assertThat(objectUnderTest.evaluate(1, 2), is(false)); + assertThat(objectUnderTest.evaluate(1, 1), is(true)); + assertThat(objectUnderTest.evaluate(2f, 1), is(true)); + assertThat(objectUnderTest.evaluate(1f, 2), is(false)); + assertThat(objectUnderTest.evaluate(1f, 1), is(true)); + assertThat(objectUnderTest.evaluate(2, 1f), is(true)); + assertThat(objectUnderTest.evaluate(1, 2f), is(false)); + assertThat(objectUnderTest.evaluate(1, 1f), is(true)); + assertThat(objectUnderTest.evaluate(2f, 1f), is(true)); + assertThat(objectUnderTest.evaluate(1f, 2f), is(false)); + assertThat(objectUnderTest.evaluate(1f, 1f), is(true)); + } + + @Test + void testEvalInValidArgLength() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 2, 3)); + } + + @Test + void testEvalInValidArgType() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1L, 1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1.0, 1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 1L)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 1.0)); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/InSetOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/InSetOperatorTest.java new file mode 100644 index 0000000000..c923824b82 --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/InSetOperatorTest.java @@ -0,0 +1,41 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import java.util.Collections; +import java.util.Set; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class InSetOperatorTest { + final GenericInSetOperator objectUnderTest = new OperatorFactory().inSetOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.IN_SET)); + } + + @Test + void testEvalValidArgs() { + assertThat(objectUnderTest.evaluate(1, Set.of(1)), is(true)); + assertThat(objectUnderTest.evaluate(1, Collections.emptySet()), is(false)); + } + + @Test + void testEvalInValidArgLength() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 2, Set.of(1, 2))); + } + + @Test + void testEvalInValidArgType() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 1)); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/LessThanOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/LessThanOperatorTest.java new file mode 100644 index 0000000000..31da67bd9e --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/LessThanOperatorTest.java @@ -0,0 +1,52 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class LessThanOperatorTest { + final Operator objectUnderTest = new OperatorFactory().lessThanOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.LT)); + } + + @Test + void testEvalValidArgs() { + assertThat(objectUnderTest.evaluate(2, 1), is(false)); + assertThat(objectUnderTest.evaluate(1, 2), is(true)); + assertThat(objectUnderTest.evaluate(1, 1), is(false)); + assertThat(objectUnderTest.evaluate(2f, 1), is(false)); + assertThat(objectUnderTest.evaluate(1f, 2), is(true)); + assertThat(objectUnderTest.evaluate(1f, 1), is(false)); + assertThat(objectUnderTest.evaluate(2, 1f), is(false)); + assertThat(objectUnderTest.evaluate(1, 2f), is(true)); + assertThat(objectUnderTest.evaluate(1, 1f), is(false)); + assertThat(objectUnderTest.evaluate(2f, 1f), is(false)); + assertThat(objectUnderTest.evaluate(1f, 2f), is(true)); + assertThat(objectUnderTest.evaluate(1f, 1f), is(false)); + } + + @Test + void testEvalInValidArgLength() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 2, 3)); + } + + @Test + void testEvalInValidArgType() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1L, 1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1.0, 1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 1L)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 1.0)); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/LessThanOrEqualOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/LessThanOrEqualOperatorTest.java new file mode 100644 index 0000000000..601be2bb6e --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/LessThanOrEqualOperatorTest.java @@ -0,0 +1,52 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class LessThanOrEqualOperatorTest { + final Operator objectUnderTest = new OperatorFactory().lessThanOrEqualOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.LTE)); + } + + @Test + void testEvalValidArgs() { + assertThat(objectUnderTest.evaluate(2, 1), is(false)); + assertThat(objectUnderTest.evaluate(1, 2), is(true)); + assertThat(objectUnderTest.evaluate(1, 1), is(true)); + assertThat(objectUnderTest.evaluate(2f, 1), is(false)); + assertThat(objectUnderTest.evaluate(1f, 2), is(true)); + assertThat(objectUnderTest.evaluate(1f, 1), is(true)); + assertThat(objectUnderTest.evaluate(2, 1f), is(false)); + assertThat(objectUnderTest.evaluate(1, 2f), is(true)); + assertThat(objectUnderTest.evaluate(1, 1f), is(true)); + assertThat(objectUnderTest.evaluate(2f, 1f), is(false)); + assertThat(objectUnderTest.evaluate(1f, 2f), is(true)); + assertThat(objectUnderTest.evaluate(1f, 1f), is(true)); + } + + @Test + void testEvalInValidArgLength() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 2, 3)); + } + + @Test + void testEvalInValidArgType() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1L, 1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1.0, 1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 1L)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 1.0)); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/NotEqualOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/NotEqualOperatorTest.java new file mode 100644 index 0000000000..99aaa984bd --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/NotEqualOperatorTest.java @@ -0,0 +1,44 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; +import org.opensearch.dataprepper.expression.util.TestObject; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class NotEqualOperatorTest { + final GenericEqualOperator objectUnderTest = new OperatorFactory().notEqualOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.NOT_EQUAL)); + } + + @Test + void testEvalValidArgs() { + final TestObject testObject1 = new TestObject("1"); + final TestObject testObject2 = new TestObject("1"); + final TestObject testObject3 = new TestObject("2"); + assertThat(objectUnderTest.evaluate(testObject1, testObject2), is(false)); + assertThat(objectUnderTest.evaluate(testObject1, testObject3), is(true)); + assertThat(objectUnderTest.evaluate(null, testObject1), is(true)); + assertThat(objectUnderTest.evaluate(testObject1, null), is(true)); + assertThat(objectUnderTest.evaluate(null, null), is(false)); + } + + @Test + void testEvalInValidArgLength() { + final TestObject testObject1 = new TestObject("1"); + final TestObject testObject2 = new TestObject("1"); + final TestObject testObject3 = new TestObject("2"); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(testObject1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(testObject1, testObject2, testObject3)); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/NotInSetOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/NotInSetOperatorTest.java new file mode 100644 index 0000000000..17e3e9f1e9 --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/NotInSetOperatorTest.java @@ -0,0 +1,41 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import java.util.Collections; +import java.util.Set; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class NotInSetOperatorTest { + final GenericInSetOperator objectUnderTest = new OperatorFactory().notInSetOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.NOT_IN_SET)); + } + + @Test + void testEvalValidArgs() { + assertThat(objectUnderTest.evaluate(1, Set.of(1)), is(false)); + assertThat(objectUnderTest.evaluate(1, Collections.emptySet()), is(true)); + } + + @Test + void testEvalInValidArgLength() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 2, Set.of(1, 2))); + } + + @Test + void testEvalInValidArgType() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, 1)); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/NotOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/NotOperatorTest.java new file mode 100644 index 0000000000..14915688f2 --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/NotOperatorTest.java @@ -0,0 +1,38 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class NotOperatorTest { + final NotOperator objectUnderTest = new NotOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.NOT)); + } + + @Test + void testEvalValidArgs() { + assertThat(objectUnderTest.evaluate(true), is(false)); + assertThat(objectUnderTest.evaluate(false), is(true)); + } + + @Test + void testEvalInValidArgLength() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(true, true)); + } + + @Test + void testEvalInValidArgType() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1)); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/OrOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/OrOperatorTest.java new file mode 100644 index 0000000000..d4db705def --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/OrOperatorTest.java @@ -0,0 +1,42 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class OrOperatorTest { + final OrOperator objectUnderTest = new OrOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.OR)); + } + + @Test + void testEvalValidArgs() { + assertThat(objectUnderTest.evaluate(true, true), is(true)); + assertThat(objectUnderTest.evaluate(true, false), is(true)); + assertThat(objectUnderTest.evaluate(false, true), is(true)); + assertThat(objectUnderTest.evaluate(false, false), is(false)); + } + + @Test + void testEvalInValidArgLength() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(true)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(true, true, false)); + } + + @Test + void testEvalInValidArgType() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(true, 1)); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, true)); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/RegexEqualOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/RegexEqualOperatorTest.java new file mode 100644 index 0000000000..615715789a --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/RegexEqualOperatorTest.java @@ -0,0 +1,45 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class RegexEqualOperatorTest { + final GenericRegexMatchOperator objectUnderTest = new OperatorFactory().regexEqualOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.MATCH_REGEX_PATTERN)); + } + + @Test + void testEvalValidArgs() { + assertThat(objectUnderTest.evaluate("a", "a*"), is(true)); + assertThat(objectUnderTest.evaluate("a", "b*"), is(false)); + } + + @Test + void testEvalInValidArgLength() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate("a")); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate("a", "a", "a*")); + } + + @Test + void testEvalInValidArgType() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, "a*")); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate("a", 1)); + } + + @Test + void testEvalInValidPattern() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate("a", "*")); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/RegexNotEqualOperatorTest.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/RegexNotEqualOperatorTest.java new file mode 100644 index 0000000000..bf61c36901 --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/RegexNotEqualOperatorTest.java @@ -0,0 +1,45 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression; + +import org.junit.jupiter.api.Test; +import org.opensearch.dataprepper.expression.antlr.DataPrepperExpressionParser; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class RegexNotEqualOperatorTest { + final GenericRegexMatchOperator objectUnderTest = new OperatorFactory().regexNotEqualOperator(); + + @Test + void testGetSymbol() { + assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.NOT_MATCH_REGEX_PATTERN)); + } + + @Test + void testEvalValidArgs() { + assertThat(objectUnderTest.evaluate("a", "a*"), is(false)); + assertThat(objectUnderTest.evaluate("a", "b*"), is(true)); + } + + @Test + void testEvalInValidArgLength() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate("a")); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate("a", "a", "a*")); + } + + @Test + void testEvalInValidArgType() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate(1, "a*")); + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate("a", 1)); + } + + @Test + void testEvalInValidPattern() { + assertThrows(IllegalArgumentException.class, () -> objectUnderTest.evaluate("a", "*")); + } +} \ No newline at end of file diff --git a/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/util/TestObject.java b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/util/TestObject.java new file mode 100644 index 0000000000..6ef7303302 --- /dev/null +++ b/data-prepper-expression/src/test/java/org/opensearch/dataprepper/expression/util/TestObject.java @@ -0,0 +1,29 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.opensearch.dataprepper.expression.util; + +import java.util.Objects; + +public class TestObject { + String field; + + public TestObject(String field) { + this.field = field; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + TestObject that = (TestObject) o; + return Objects.equals(field, that.field); + } + + @Override + public int hashCode() { + return Objects.hash(field); + } +}