Skip to content

Commit

Permalink
MAINT: merge current main and resolve conflicts
Browse files Browse the repository at this point in the history
Signed-off-by: Chen <[email protected]>
  • Loading branch information
chenqi0805 committed Mar 10, 2022
2 parents ce1121d + 6489e69 commit 5779a86
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 23 deletions.
3 changes: 2 additions & 1 deletion build-resources.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ext.coreProjects = [
project(':data-prepper-api'),
project(':data-prepper-core'),
project('data-prepper-plugins:common'),
project('data-prepper-expression')
project('data-prepper-expression'),
project(':data-prepper-logstash-configuration')
]
ext.mavenArtifactProjects = [project(':data-prepper-api')]
1 change: 0 additions & 1 deletion data-prepper-expression/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,3 @@ jacocoTestCoverageVerification {
}))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ setOperator
unaryOperatorExpression
: primary
| setInitializer
| regexPattern
| parenthesesExpression
| unaryNotOperatorExpression
| unaryOperator (primary | unaryOperatorExpression)
;

parenthesesExpression
Expand All @@ -87,13 +86,10 @@ setInitializer
: LBRACE primary (SET_DELIMITER primary)* RBRACE
;

unaryNotOperatorExpression
: unaryOperator parenthesesExpression
| unaryOperator primary
;

unaryOperator
: NOT
| SUBTRACT
;

primary
Expand Down Expand Up @@ -240,6 +236,7 @@ NOT_IN_SET : SPACE 'not in' SPACE;
AND : SPACE 'and' SPACE;
OR : SPACE 'or' SPACE;
NOT : 'not' SPACE;
SUBTRACT : '-';
LPAREN : '(';
RPAREN : ')';
LBRACE : '{';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public int getNumberOfOperands() {

@Override
public boolean shouldEvaluate(final RuleContext ctx) {
return ctx.getRuleIndex() == DataPrepperExpressionParser.RULE_unaryNotOperatorExpression;
return ctx.getRuleIndex() == DataPrepperExpressionParser.RULE_unaryOperatorExpression;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ void testTokenFORWARDSLASH() {
assertToken("/", DataPrepperExpressionLexer.FORWARDSLASH);
}

@Test
void testTokenSUBTRACT() {
assertToken("-", DataPrepperExpressionLexer.SUBTRACT);
}

@Test
void testSpaceInsignificant() {
final String statement = " ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void testGetNumberOfOperands() {

@Test
void testShouldEvaluate() {
when(ctx.getRuleIndex()).thenReturn(DataPrepperExpressionParser.RULE_unaryNotOperatorExpression);
when(ctx.getRuleIndex()).thenReturn(DataPrepperExpressionParser.RULE_unaryOperatorExpression);
assertThat(objectUnderTest.shouldEvaluate(ctx), is(true));
when(ctx.getRuleIndex()).thenReturn(-1);
assertThat(objectUnderTest.shouldEvaluate(ctx), is(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ void testCoerceTerminalNodeFloatType() {
assertThat(result, equalTo(testFloat));
}

@Test
void testCoerceTerminalNodeBooleanType() {
when(token.getType()).thenReturn(DataPrepperExpressionParser.Boolean);
final Boolean testBoolean = new Random().nextBoolean();
when(terminalNode.getSymbol()).thenReturn(token);
when(terminalNode.getText()).thenReturn(String.valueOf(testBoolean));
final Event testEvent = createTestEvent(new HashMap<>());
final Object result = objectUnderTest.coercePrimaryTerminalNode(terminalNode, testEvent);
assertThat(result, instanceOf(Boolean.class));
assertThat(result, equalTo(testBoolean));
}

@Test
void testCoerceTerminalNodeJsonPointerType() {
final String testKey1 = "key1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void testEmptyListThrows() {
@Test
void testGivenSingleExceptionThenExceptionIsCause() {
final RuntimeException mock = mock(RuntimeException.class);
final ParseTreeCompositeException parseTreeCompositeException = new ParseTreeCompositeException(Arrays.asList(mock));
final ParseTreeCompositeException parseTreeCompositeException = new ParseTreeCompositeException(Collections.singletonList(mock));

assertThat(parseTreeCompositeException.getCause(), is(mock));
}
Expand All @@ -54,4 +54,19 @@ void testMultipleExceptionsPrinted() throws ParseTreeCompositeException {
assertThat(message, containsString("|-- java.lang.RuntimeException: Error"));
assertThat(message, containsString("|-- java.lang.NullPointerException: Throwable was null!"));
}

@Test
void testExceptionWithoutStackTrace() {
final RuntimeException error1 = new RuntimeException("Error1");
error1.setStackTrace(new StackTraceElement[0]);
final RuntimeException error2 = new RuntimeException("Error2");
error2.setStackTrace(new StackTraceElement[0]);
final ParseTreeCompositeException parseTreeCompositeException = new ParseTreeCompositeException(Arrays.asList(error1, error2));

assertThat(parseTreeCompositeException.getCause() instanceof ExceptionOverview, is(true));
final String message = parseTreeCompositeException.getCause().getMessage();
assertThat(message, containsString("Multiple exceptions (2)"));
assertThat(message, containsString("|-- java.lang.RuntimeException: Error2"));
assertThat(message, containsString("|-- java.lang.RuntimeException: Error1"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,7 @@ void testNotOperationOrder() {
REGEX_OPERATOR_EXPRESSION,
RELATIONAL_OPERATOR_EXPRESSION,
SET_OPERATOR_EXPRESSION,
UNARY_OPERATOR_EXPRESSION,
UNARY_NOT_OPERATOR_EXPRESSION
UNARY_OPERATOR_EXPRESSION
).withChildrenMatching(
isOperator(UNARY_OPERATOR),
isUnaryTree()
Expand Down Expand Up @@ -370,8 +369,7 @@ void testNotPriorityOperationOrder() {
REGEX_OPERATOR_EXPRESSION,
RELATIONAL_OPERATOR_EXPRESSION,
SET_OPERATOR_EXPRESSION,
UNARY_OPERATOR_EXPRESSION,
UNARY_NOT_OPERATOR_EXPRESSION
UNARY_OPERATOR_EXPRESSION
).withChildrenMatching(isOperator(UNARY_OPERATOR), parenthesesExpression);

assertThat(expression, isExpression(not));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.junit.jupiter.api.Test;
import org.opensearch.dataprepper.expression.util.GrammarTest;

import java.util.function.Function;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.opensearch.dataprepper.expression.util.ContextMatcher.hasContext;
Expand Down Expand Up @@ -199,4 +201,35 @@ void testValidJsonPointerCharacterSet() {
assertThat(expression, isExpression(isJsonPointerUnaryTree()));
assertThat(errorListener.isErrorFound(), is(false));
}

@Test
void testSubtractOperator() {
final ParserRuleContext expression = parseExpression("/status_code == -200");

final DiagnosingMatcher<ParseTree> lhs = isJsonPointerUnaryTree();
final Function<DiagnosingMatcher<ParseTree>, DiagnosingMatcher<ParseTree>> isNegative =
rhs -> hasContext(UNARY_OPERATOR_EXPRESSION, isOperator(UNARY_OPERATOR), rhs);
final DiagnosingMatcher<ParseTree> doubleNegative200 = isNegative.apply(isUnaryTree());

final DiagnosingMatcher<ParseTree> rhs = isParseTree(
REGEX_OPERATOR_EXPRESSION,
RELATIONAL_OPERATOR_EXPRESSION,
SET_OPERATOR_EXPRESSION
).withChildrenMatching(
doubleNegative200
);

final DiagnosingMatcher<ParseTree> equalsExpression = isParseTree(
CONDITIONAL_EXPRESSION,
EQUALITY_OPERATOR_EXPRESSION
).withChildrenMatching(
lhs,
isOperator(EQUALITY_OPERATOR),
rhs
);


assertThat(expression, isExpression(equalsExpression));
assertThat(errorListener.isErrorFound(), is(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ void testValidOptionalSpaceOperators(final String expression) {
assertThatIsValid(expression);
}

@ParameterizedTest
@ValueSource(strings = {
"not false",
"not not false",
"not /status_code",
"not (true)",
"-5",
"-3.14",
"-(5)"
})
void testValidUnaryOperators(final String expression) {
assertThatIsValid(expression);
}

@ParameterizedTest
@ValueSource(strings = {
"falkse and true",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,25 @@

public abstract class GrammarTest {
//region ParseTree child classes
protected static final Class<? extends ParseTree> EXPRESSION = DataPrepperExpressionParser.ExpressionContext.class;
protected static final Class<? extends ParseTree> CONDITIONAL_EXPRESSION = DataPrepperExpressionParser.ConditionalExpressionContext.class;
protected static final Class<? extends ParseTree> CONDITIONAL_EXPRESSION =
DataPrepperExpressionParser.ConditionalExpressionContext.class;
protected static final Class<? extends ParseTree> CONDITIONAL_OPERATOR = DataPrepperExpressionParser.ConditionalOperatorContext.class;
protected static final Class<? extends ParseTree> EQUALITY_OPERATOR_EXPRESSION =
DataPrepperExpressionParser.EqualityOperatorExpressionContext.class;
protected static final Class<? extends ParseTree> EQUALITY_OPERATOR = DataPrepperExpressionParser.EqualityOperatorContext.class;
protected static final Class<? extends ParseTree> REGEX_OPERATOR_EXPRESSION =
DataPrepperExpressionParser.RegexOperatorExpressionContext.class;
protected static final Class<? extends ParseTree> REGEX_EQUALITY_OPERATOR =
DataPrepperExpressionParser.RegexEqualityOperatorContext.class;
protected static final Class<? extends ParseTree> RELATIONAL_OPERATOR_EXPRESSION =
DataPrepperExpressionParser.RelationalOperatorExpressionContext.class;
protected static final Class<? extends ParseTree> RELATIONAL_OPERATOR = DataPrepperExpressionParser.RelationalOperatorContext.class;
protected static final Class<? extends ParseTree> SET_OPERATOR_EXPRESSION =
DataPrepperExpressionParser.SetOperatorExpressionContext.class;
protected static final Class<? extends ParseTree> UNARY_OPERATOR_EXPRESSION =
DataPrepperExpressionParser.UnaryOperatorExpressionContext.class;
protected static final Class<? extends ParseTree> UNARY_NOT_OPERATOR_EXPRESSION =
DataPrepperExpressionParser.UnaryNotOperatorExpressionContext.class;
protected static final Class<? extends ParseTree> UNARY_OPERATOR =
DataPrepperExpressionParser.UnaryOperatorContext.class;
protected static final Class<? extends ParseTree> PARENTHESES_EXPRESSION = DataPrepperExpressionParser.ParenthesesExpressionContext.class;
protected static final Class<? extends ParseTree> PARENTHESES_EXPRESSION =
DataPrepperExpressionParser.ParenthesesExpressionContext.class;
//endregion

protected ErrorListener errorListener;
Expand Down
22 changes: 21 additions & 1 deletion data-prepper-logstash-configuration/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ plugins {
id 'idea'
}

ext {
antlrGeneratedPackageDirectory = "org/opensearch/dataprepper/logstash/"
}

dependencies {
antlr 'org.antlr:antlr4:4.9.2'
implementation project(':data-prepper-api')
Expand All @@ -23,7 +27,23 @@ dependencies {
generateGrammarSource {
maxHeapSize = '128m'
arguments += ['-listener', '-visitor']
outputDirectory = new File('build/generated-src/antlr/main/org/opensearch/dataprepper/logstash/')
outputDirectory = new File("build/generated-src/antlr/main/${antlrGeneratedPackageDirectory}")
}

compileJava.dependsOn generateGrammarSource

jacocoTestCoverageVerification {
dependsOn jacocoTestReport
violationRules {
rule { //in addition to core projects rule - this one checks for 100% code coverage for this project
limit {
minimum = 1.0 //keep this at 100%
}
}
}
afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it, exclude: ["${antlrGeneratedPackageDirectory}/**"])
}))
}
}

0 comments on commit 5779a86

Please sign in to comment.