fix(eventbridge): match numeric comparison patterns (#7695) (#7700)

Add numeric comparisons to the shared matcher used by TestEventPattern and PutEvents. Validate all numeric filters before testing events or mutating rules, including nested patterns and every $or branch. Match numeric and literal filters against event array elements without splitting range bounds across different values.

Add service and AWS SDK success/error regressions.

Validation: 293 focused tests pass; zero Checkstyle violations.

Fixes #7695

The validator accepted arbitrary operator/number pairs, allowing repeated bounds, equality mixed with bounds, and unordered ranges. Restrict filters to a single comparison or a lower bound followed by a strictly greater upper bound.

Add SDK rejection and rule-preservation regressions and valid range boundary controls. All 386 focused tests, Checkstyle, docs-check, and partition-check pass.

Keep the new range boundary cases aligned with the surrounding parameter tables. This also requests fresh CI checks on the verified PR history.

Use one testing fixture for valid comparisons and invalid patterns in both the service and SDK suites. Preserve the union of existing cases, including nested and matching alternative branches, so both layers exercise the same matrix.

Validation: 402 focused EventBridge tests pass with no skips and zero Checkstyle violations.

Expose PipesFilterMatcher.matchesNumeric and use it from EventBridge instead of maintaining a second comparison loop. Retain the null and numeric-form guards.

Use AwsTestClients.create directly in the EventBridge SDK tests and remove the custom SDK client builder and injected endpoint.

Validation: all 463 EventBridge service, EventBridge SDK, and Pipes matcher tests pass, with no skips and zero Checkstyle violations.
This commit is contained in:
jwoa95 authored and GitHub committed 2026-11-10 09:08:43 -04:00
1 parent 09ac552cf7
commit 9c989d7457
6 files changed
+348 -7

No files matched your search

@@ -989,6 +989,99 @@ class EventBridgeServiceTest {
assertFalse(service.matchesPattern(event, "{\"detail\":{\"status\":[\"PENDING\"]}}"));
}
@Test
void matchesPatternByNumericSeverity() {
Map<String, Object> event = Map.of("Detail", "{\"severity\":7}");
assertTrue(service.matchesPattern(event, "{\"detail\":{\"severity\":[7]}}"));
assertTrue(service.matchesPattern(event, "{\"detail\":{\"severity\":[{\"numeric\":[\">=\",7]}]}}"));
assertTrue(service.matchesPattern(event, "{\"detail\":{\"severity\":[{\"numeric\":[\">=\",6]}]}}"));
}
@ParameterizedTest
@MethodSource("io.floci.aws.testing.EventBridgeNumericPatterns#cases")
void matchesPatternByNumericComparison(String value, String comparisons, boolean expected) {
Map<String, Object> event = Map.of("Detail", "{\"value\":" + value + "}");
String pattern = "{\"detail\":{\"value\":[{\"numeric\":[" + comparisons + "]}]}}";
assertEquals(expected, service.matchesPattern(event, pattern));
assertEquals(expected, service.testEventPattern(pattern, "{\"detail\":{\"value\":" + value + "}}"));
service.putRule("valid-numeric", null, pattern, null, RuleState.ENABLED, null, null, null, REGION);
assertEquals(pattern, service.describeRule("valid-numeric", null, REGION).getEventPattern());
}
@ParameterizedTest
@MethodSource("io.floci.aws.testing.EventBridgeNumericPatterns#invalidPatterns")
void malformedNumericComparisonIsRejectedByPublicApis(String pattern) {
for (String detail : List.of("{\"severity\":7}", "{\"severity\":\"7\"}", "{}")) {
AwsException error = assertThrows(AwsException.class,
() -> service.testEventPattern(pattern, "{\"source\":\"example.test\",\"detail\":" + detail + "}"));
assertEquals("InvalidEventPatternException", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
}
AwsException error = assertThrows(AwsException.class, () -> service.putRule(
"invalid-numeric", null, pattern, null, RuleState.ENABLED, null, null, null, REGION));
assertEquals("InvalidEventPatternException", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
assertTrue(service.listRules(null, null, REGION).isEmpty());
}
@Test
void invalidNumericUpdateDoesNotMutateExistingRule() {
String original = "{\"detail\":{\"value\":[7]}}";
service.putRule("existing", null, original, null, RuleState.ENABLED, null, null, null, REGION);
assertThrows(AwsException.class, () -> service.putRule("existing", null,
"{\"detail\":{\"value\":[{\"numeric\":[]}]}}", null,
RuleState.ENABLED, null, null, null, REGION));
assertEquals(original, service.describeRule("existing", null, REGION).getEventPattern());
}
@Test
void numericFieldNameIsNotConfusedWithNumericOperator() {
assertTrue(service.testEventPattern("{\"detail\":{\"numeric\":[7]}}", "{\"detail\":{\"numeric\":7}}"));
}
@Test
void exactLiteralsMatchArrayElements() {
assertTrue(service.testEventPattern("{\"detail\":{\"value\":[7]}}", "{\"detail\":{\"value\":[3,7]}}"));
assertFalse(service.testEventPattern("{\"detail\":{\"value\":[7]}}", "{\"detail\":{\"value\":[3,6]}}"));
}
@Test
void numericComparisonDoesNotMatchMissingField() {
assertFalse(service.matchesPattern(Map.of("Detail", "{}"),
"{\"detail\":{\"value\":[{\"numeric\":[\">=\",0]}]}}"));
}
@Test
void numericComparisonsCanBeAlternativesToLiterals() {
String pattern = "{\"detail\":{\"value\":[{\"numeric\":[\"<\",0]},\"unknown\",{\"numeric\":[\">=\",7]}]}}";
assertTrue(service.matchesPattern(Map.of("Detail", "{\"value\":-1}"), pattern));
assertTrue(service.matchesPattern(Map.of("Detail", "{\"value\":\"unknown\"}"), pattern));
assertTrue(service.matchesPattern(Map.of("Detail", "{\"value\":7}"), pattern));
assertFalse(service.matchesPattern(Map.of("Detail", "{\"value\":3}"), pattern));
}
@ParameterizedTest
@ValueSource(ints = {6, 7, 8})
void putEventsDispatchesOnlyWhenNumericSeverityMatches(int severity) {
Rule rule = service.putRule("severity-rule", null,
"{\"detail\":{\"severity\":[{\"numeric\":[\">=\",7]}]}}", null,
RuleState.ENABLED, null, null, null, REGION);
Target target = new Target("t1", "arn:aws:sns:us-east-1:000000000000:alerts", null, null);
service.putTargets("severity-rule", null, List.of(target), REGION);
EventBridgeService.PutEventsResult result = service.putEvents(List.of(Map.of(
"Source", "example.test", "DetailType", "Example", "Detail", "{\"severity\":" + severity + "}")), REGION);
assertEquals(0, result.failedCount());
verify(dispatcherMock, Mockito.times(severity >= 7 ? 1 : 0))
.dispatch(eq(rule.getArn()), eq(target), anyString(), eq(REGION), any());
}
@Test
void matchesPatternByResources() {
Map<String, Object> event = Map.of(
@@ -1,12 +1,22 @@
package io.floci.aws.services.eventbridge;
import io.floci.aws.testing.AwsTestClients;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.eventbridge.EventBridgeClient;
import software.amazon.awssdk.services.eventbridge.model.InvalidEventPatternException;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
@QuarkusTest
class EventBridgeTestEventPatternIntegrationTest {
@@ -19,6 +29,71 @@ class EventBridgeTestEventPatternIntegrationTest {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@ParameterizedTest
@MethodSource("io.floci.aws.testing.EventBridgeNumericPatterns#cases")
void numericComparisonThroughSdk(String value, String comparisons, boolean expected) {
try (EventBridgeClient client = AwsTestClients.create(EventBridgeClient.builder(), "test", Region.EU_WEST_2)) {
String event = """
{"version":"0","id":"numeric-test","detail-type":"Example","source":"example.test",
"account":"000000000000","time":"2026-11-06T00:00:00Z","region":"eu-west-2",
"resources":[],"detail":{"severity":%s}}
""".formatted(value);
String pattern = "{\"detail\":{\"severity\":[{\"numeric\":[" + comparisons + "]}]}}";
assertEquals(expected, client.testEventPattern(request -> request
.eventPattern(pattern).event(event)).result());
String name = "valid-numeric-pattern";
client.putRule(request -> request.name(name).eventPattern(pattern));
try {
assertEquals(pattern, client.describeRule(request -> request.name(name)).eventPattern());
} finally {
client.deleteRule(request -> request.name(name));
}
}
}
@ParameterizedTest
@MethodSource("io.floci.aws.testing.EventBridgeNumericPatterns#invalidPatterns")
void invalidNumericPatternsRaiseSdkErrors(String pattern) {
try (EventBridgeClient client = AwsTestClients.create(EventBridgeClient.builder(), "test", Region.EU_WEST_2)) {
for (String detail : List.of("{\"severity\":7}", "{\"severity\":\"7\"}", "{}")) {
String event = """
{"version":"0","id":"numeric-test","detail-type":"Example","source":"example.test",
"account":"000000000000","time":"2026-11-06T00:00:00Z","region":"eu-west-2",
"resources":[],"detail":%s}
""".formatted(detail);
InvalidEventPatternException error = assertThrows(InvalidEventPatternException.class,
() -> client.testEventPattern(request -> request.eventPattern(pattern).event(event)));
assertEquals(400, error.statusCode());
assertEquals("InvalidEventPatternException", error.awsErrorDetails().errorCode());
}
InvalidEventPatternException error = assertThrows(InvalidEventPatternException.class,
() -> client.putRule(request -> request.name("invalid-numeric-pattern").eventPattern(pattern)));
assertEquals(400, error.statusCode());
assertEquals("InvalidEventPatternException", error.awsErrorDetails().errorCode());
}
}
@ParameterizedTest
@MethodSource("io.floci.aws.testing.EventBridgeNumericPatterns#invalidPatterns")
void invalidNumericUpdatePreservesExistingRuleThroughSdk(String pattern) {
try (EventBridgeClient client = AwsTestClients.create(EventBridgeClient.builder(), "test", Region.EU_WEST_2)) {
String name = "numeric-update-preservation";
String original = "{\"detail\":{\"severity\":[7]}}";
client.putRule(request -> request.name(name).eventPattern(original).description("original"));
try {
InvalidEventPatternException error = assertThrows(InvalidEventPatternException.class,
() -> client.putRule(request -> request.name(name).eventPattern(pattern).description("changed")));
assertEquals(400, error.statusCode());
assertEquals("InvalidEventPatternException", error.awsErrorDetails().errorCode());
assertEquals(original, client.describeRule(request -> request.name(name)).eventPattern());
assertEquals("original", client.describeRule(request -> request.name(name)).description());
} finally {
client.deleteRule(request -> request.name(name));
}
}
}
@Test
void sourceMatch() {
given()
@@ -0,0 +1,79 @@
package io.floci.aws.testing;
import org.junit.jupiter.params.provider.Arguments;
import java.util.stream.Stream;
/** Numeric pattern cases shared by the EventBridge service and SDK tests. */
public final class EventBridgeNumericPatterns {
private EventBridgeNumericPatterns() {
}
public static Stream<Arguments> cases() {
return Stream.of(
Arguments.of("7", "\">=\",7", true),
Arguments.of("7", "\">=\",6", true),
Arguments.of("6", "\">=\",7", false),
Arguments.of("7", "\">\",7", false),
Arguments.of("8", "\">\",7", true),
Arguments.of("6", "\"<\",7", true),
Arguments.of("7", "\"<\",7", false),
Arguments.of("7", "\"<=\",7", true),
Arguments.of("8", "\"<=\",7", false),
Arguments.of("7.0", "\"=\",7", true),
Arguments.of("3.018e2", "\"=\",301.8", true),
Arguments.of("7", "\"=\",8", false),
Arguments.of("-2.5", "\">\",-3,\"<=\",0", true),
Arguments.of("0", "\">\",-3,\"<=\",0", true),
Arguments.of("-3", "\">\",-3,\"<=\",0", false),
Arguments.of("1", "\">\",-3,\"<=\",0", false),
Arguments.of("-0.0", "\"=\",0", true),
Arguments.of("\"7\"", "\">=\",7", false),
Arguments.of("true", "\">=\",0", false),
Arguments.of("null", "\">=\",0", false),
Arguments.of("{}", "\">=\",0", false),
Arguments.of("[3,7]", "\">=\",7", true),
Arguments.of("[3,6]", "\">=\",7", false),
Arguments.of("[3,9]", "\">=\",7,\"<\",9", false),
Arguments.of("[3,8]", "\">=\",7,\"<\",9", true),
Arguments.of("[\"7\"]", "\">=\",7", false),
Arguments.of("[]", "\">=\",7", false),
Arguments.of("6", "\">\",6,\"<\",8", false),
Arguments.of("7", "\">\",6,\"<\",8", true),
Arguments.of("8", "\">\",6,\"<\",8", false),
Arguments.of("6", "\">\",6,\"<=\",8", false),
Arguments.of("7", "\">\",6,\"<=\",8", true),
Arguments.of("8", "\">\",6,\"<=\",8", true),
Arguments.of("6", "\">=\",6,\"<\",8", true),
Arguments.of("7", "\">=\",6,\"<\",8", true),
Arguments.of("8", "\">=\",6,\"<\",8", false),
Arguments.of("6", "\">=\",6,\"<=\",8", true),
Arguments.of("7", "\">=\",6,\"<=\",8", true),
Arguments.of("8", "\">=\",6,\"<=\",8", true),
Arguments.of("8", "\"<\",7", false),
Arguments.of("301.8", "\"=\",3.018e2", true),
Arguments.of("7", "\">\",6,\"<=\",7", true),
Arguments.of("8", "\">\",6,\"<=\",7", false));
}
public static Stream<String> invalidPatterns() {
Stream<String> invalidFilters = Stream.of("null", "7", "{}", "[]", "[\">=\"]", "[\">=\",7,\"<\"]",
"[\"!=\",7]", "[7,7]", "[\">=\",\"7\"]", "[\">=\",null]", "[\">=\",true]",
"[\">\",0,\">\",1]", "[\">=\",0,\">\",1]",
"[\">\",0,\">=\",1]", "[\">=\",0,\">=\",1]",
"[\"<\",9,\"<\",7]", "[\"<=\",9,\"<=\",7]",
"[\"=\",7,\">\",6]", "[\">\",6,\"=\",7]",
"[\"=\",7,\"=\",7]", "[\"<\",9,\">\",7]",
"[\"<=\",9,\">=\",7]", "[\">\",9,\"<\",7]",
"[\">=\",9,\"<=\",7]", "[\">\",7,\"<\",7]",
"[\">\",7,\"<=\",7]", "[\">=\",7,\"<\",7]",
"[\">=\",7,\"<=\",7]", "[\">\",0,\"<\",9,\"<\",8]")
.map(filter -> "{\"detail\":{\"severity\":[{\"numeric\":" + filter + "}]}}");
return Stream.concat(invalidFilters, Stream.of(
"{\"detail\":{\"nested\":{\"severity\":[{\"numeric\":[]}]}}}",
"{\"$or\":[{\"source\":[\"example.test\"]},{\"detail\":{\"severity\":[{\"numeric\":[]}]}}]}",
"{\"detail\":{\"$or\":[{\"severity\":[7]},{\"severity\":[{\"numeric\":[]}] }]}}",
"{\"detail\":{\"severity\":[7,{\"numeric\":[]}]}}"));
}
}