Skip to content

Commit

Permalink
Start unit tests for the OpenSearch sink testing the document_version…
Browse files Browse the repository at this point in the history
… error cases (opensearch-project#3599)

Signed-off-by: Taylor Gray <[email protected]>
  • Loading branch information
graytaylor0 authored Nov 10, 2023
1 parent 7df58f4 commit d2ad8ae
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ default Boolean evaluateConditional(final String statement, final Event context)

Boolean isValidExpressionStatement(final String statement);

Boolean isValidFormatExpressions(final String format);
Boolean isValidFormatExpression(final String format);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Boolean isValidExpressionStatement(final String statement) {
}

@Override
public Boolean isValidFormatExpressions(String format) {
public Boolean isValidFormatExpression(String format) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Boolean isValidExpressionStatement(final String statement) {
}

@Override
public Boolean isValidFormatExpressions(final String format) {
public Boolean isValidFormatExpression(final String format) {
int fromIndex = 0;
int position = 0;
while ((position = format.indexOf("${", fromIndex)) != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ void isValidExpressionStatement_returns_false_when_parse_throws() {
"abc-${invalid, false"
})
void isValidFormatExpressionsReturnsCorrectResult(final String format, final Boolean expectedResult) {
assertThat(statementEvaluator.isValidFormatExpressions(format), equalTo(expectedResult));
assertThat(statementEvaluator.isValidFormatExpression(format), equalTo(expectedResult));
}

@ParameterizedTest
@ValueSource(strings = {"abc-${anyS(=tring}"})
void isValidFormatExpressionsReturnsFalseWhenIsValidKeyAndValidExpressionIsFalse(final String format) {
doThrow(RuntimeException.class).when(parser).parse(anyString());
assertThat(statementEvaluator.isValidFormatExpressions(format), equalTo(false));
assertThat(statementEvaluator.isValidFormatExpression(format), equalTo(false));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ public void testOpenSearchBulkActionsCreateWithExpression() throws IOException,
Event event = (Event) testRecords.get(0).getData();
event.getMetadata().setAttribute("action", "create");
final String actionFormatExpression = "${getMetadata(\"action\")}";
when(expressionEvaluator.isValidFormatExpressions(actionFormatExpression)).thenReturn(true);
when(expressionEvaluator.isValidFormatExpression(actionFormatExpression)).thenReturn(true);
when(expressionEvaluator.isValidExpressionStatement("getMetadata(\"action\")")).thenReturn(true);
when(expressionEvaluator.evaluate("getMetadata(\"action\")", event)).thenReturn(event.getMetadata().getAttribute("action"));
pluginSetting.getSettings().put(IndexConfiguration.ACTION, actionFormatExpression);
Expand Down Expand Up @@ -715,7 +715,7 @@ public void testOpenSearchBulkActionsCreateWithInvalidExpression() throws IOExce
Event event = (Event) testRecords.get(0).getData();
event.getMetadata().setAttribute("action", "unknown");
final String actionFormatExpression = "${getMetadata(\"action\")}";
when(expressionEvaluator.isValidFormatExpressions(actionFormatExpression)).thenReturn(true);
when(expressionEvaluator.isValidFormatExpression(actionFormatExpression)).thenReturn(true);
when(expressionEvaluator.isValidExpressionStatement("getMetadata(\"action\")")).thenReturn(true);
when(expressionEvaluator.evaluate("getMetadata(\"action\")", event)).thenReturn(event.getMetadata().getAttribute("action"));
pluginSetting.getSettings().put(IndexConfiguration.ACTION, actionFormatExpression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,17 @@ public void doOutput(final Collection<Record<Event>> records) {
versionExpressionEvaluationResult = event.formatString(versionExpression, expressionEvaluator);
version = Long.valueOf(event.formatString(versionExpression, expressionEvaluator));
} catch (final NumberFormatException e) {
LOG.warn("Unable to convert the result of evaluating document_version '{}' to Long for an Event. The evaluation result '{}' must be a valid Long type", versionExpression, versionExpressionEvaluationResult);
logFailureForDlqObjects(List.of(createDlqObjectFromEvent(event, indexName, e.getMessage())), e);
final String errorMessage = String.format(
"Unable to convert the result of evaluating document_version '%s' to Long for an Event. The evaluation result '%s' must be a valid Long type", versionExpression, versionExpressionEvaluationResult
);
LOG.error(errorMessage);
logFailureForDlqObjects(List.of(createDlqObjectFromEvent(event, indexName, errorMessage)), e);
dynamicDocumentVersionDroppedEvents.increment();
} catch (final RuntimeException e) {
LOG.error("There was an exception when evaluating the document_version '{}'. Check the dlq if configured to see details about the affected Event: {}", versionExpression, e.getMessage());
logFailureForDlqObjects(List.of(createDlqObjectFromEvent(event, indexName, e.getMessage())), e);
final String errorMessage = String.format(
"There was an exception when evaluating the document_version '%s': %s", versionExpression, e.getMessage());
LOG.error(errorMessage + " Check the dlq if configured to see more details about the affected Event");
logFailureForDlqObjects(List.of(createDlqObjectFromEvent(event, indexName, errorMessage)), e);
dynamicDocumentVersionDroppedEvents.increment();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public static IndexConfiguration readIndexConfig(final PluginSetting pluginSetti
final String versionType = pluginSetting.getStringOrDefault(DOCUMENT_VERSION_TYPE, null);

builder = builder.withVersionExpression(versionExpression);
if (versionExpression != null && (!expressionEvaluator.isValidFormatExpressions(versionExpression))) {
if (versionExpression != null && (!expressionEvaluator.isValidFormatExpression(versionExpression))) {
throw new InvalidPluginConfigurationException("document_version {} is not a valid format expression.");
}

Expand Down Expand Up @@ -546,7 +546,7 @@ public Builder withIsmPolicyFile(final String ismPolicyFile) {

public Builder withAction(final String action, final ExpressionEvaluator expressionEvaluator) {
checkArgument((EnumUtils.isValidEnumIgnoreCase(OpenSearchBulkActions.class, action) ||
(action.contains("${") && expressionEvaluator.isValidFormatExpressions(action))), "action \"" + action + "\" is invalid. action must be one of the following: " + Arrays.stream(OpenSearchBulkActions.values()).collect(Collectors.toList()));
(action.contains("${") && expressionEvaluator.isValidFormatExpression(action))), "action \"" + action + "\" is invalid. action must be one of the following: " + Arrays.stream(OpenSearchBulkActions.values()).collect(Collectors.toList()));
this.action = action;
return this;
}
Expand All @@ -556,7 +556,7 @@ public Builder withActions(final List<Map<String, Object>> actions, final Expres
String action = (String)actionMap.get("type");
if (action != null) {
checkArgument((EnumUtils.isValidEnumIgnoreCase(OpenSearchBulkActions.class, action) ||
(action.contains("${") && expressionEvaluator.isValidFormatExpressions(action))), "action \"" + action + "\". action must be one of the following: " + Arrays.stream(OpenSearchBulkActions.values()).collect(Collectors.toList()));
(action.contains("${") && expressionEvaluator.isValidFormatExpression(action))), "action \"" + action + "\". action must be one of the following: " + Arrays.stream(OpenSearchBulkActions.values()).collect(Collectors.toList()));
}
}
this.actions = actions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void testReadESConfigWithBulkActionCreateExpression() {
pluginSetting.setPipelineName(PIPELINE_NAME);

expressionEvaluator = mock(ExpressionEvaluator.class);
when(expressionEvaluator.isValidFormatExpressions(actionFormatExpression)).thenReturn(true);
when(expressionEvaluator.isValidFormatExpression(actionFormatExpression)).thenReturn(true);
final OpenSearchSinkConfiguration openSearchSinkConfiguration =
OpenSearchSinkConfiguration.readESConfig(pluginSetting, expressionEvaluator);

Expand Down
Loading

0 comments on commit d2ad8ae

Please sign in to comment.