mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(eventbridge): page ListRules with Limit and NextToken (#6223)
ListRules ignored Limit and NextToken and always returned every matching rule. It now pages through the bus's rules in name order using the shared Pagination cursor, keeping NamePrefix and EventBusName filtering intact. - Limit defaults to 100, the documented per-page maximum; an integral value outside 1 to 100, including one wider than an int, is a ValidationException. - A string, boolean or fractional Limit is a SerializationException rather than being coerced into a page size. - NextToken is returned only when more rules remain. - A token Floci did not mint is rejected with HTTP 400 InvalidToken, as the API reference documents for a bad pagination token. Fixes #6220
This commit is contained in:
1 parent
71cc704319
commit
007ac9268e
6 files changed
+330
-3
No files matched your search
+145
@@ -0,0 +1,145 @@
|
||||
package io.github.hectorvent.floci.services.eventbridge;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.json.JsonPath;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@QuarkusTest
|
||||
class EventBridgeListRulesPaginationIntegrationTest {
|
||||
|
||||
private static final String CT = "application/x-amz-json-1.1";
|
||||
|
||||
@BeforeAll
|
||||
static void configure() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
private static void call(String action, String body) {
|
||||
given().contentType(CT).header("X-Amz-Target", "AWSEvents." + action)
|
||||
.body(body)
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
private static void putRule(String name, String bus) {
|
||||
String busField = bus == null ? "" : ",\"EventBusName\":\"" + bus + "\"";
|
||||
call("PutRule", "{\"Name\":\"" + name + "\",\"EventPattern\":\"{\\\"source\\\":[\\\"paging\\\"]}\""
|
||||
+ busField + "}");
|
||||
}
|
||||
|
||||
private static JsonPath listRules(String body) {
|
||||
return given().contentType(CT).header("X-Amz-Target", "AWSEvents.ListRules")
|
||||
.body(body)
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.extract().jsonPath();
|
||||
}
|
||||
|
||||
@Test
|
||||
void limitOnePagesThroughEveryRuleExactlyOnce() {
|
||||
putRule("lr-page-c", null);
|
||||
putRule("lr-page-a", null);
|
||||
putRule("lr-page-b", null);
|
||||
putRule("lr-other", null);
|
||||
|
||||
List<String> seen = new ArrayList<>();
|
||||
String token = null;
|
||||
do {
|
||||
String tokenField = token == null ? "" : ",\"NextToken\":\"" + token + "\"";
|
||||
JsonPath page = listRules("{\"NamePrefix\":\"lr-page-\",\"Limit\":1" + tokenField + "}");
|
||||
List<String> names = page.getList("Rules.Name");
|
||||
assertEquals(1, names.size());
|
||||
seen.add(names.get(0));
|
||||
token = page.getString("NextToken");
|
||||
} while (token != null);
|
||||
|
||||
assertEquals(List.of("lr-page-a", "lr-page-b", "lr-page-c"), seen);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customBusAndPrefixPageTogether() {
|
||||
call("CreateEventBus", "{\"Name\":\"lr-paging-bus\"}");
|
||||
putRule("lr-bus-1", "lr-paging-bus");
|
||||
putRule("lr-bus-2", "lr-paging-bus");
|
||||
putRule("lr-bus-skip", "lr-paging-bus");
|
||||
putRule("lr-bus-3", null);
|
||||
|
||||
String token = given().contentType(CT).header("X-Amz-Target", "AWSEvents.ListRules")
|
||||
.body("{\"EventBusName\":\"lr-paging-bus\",\"NamePrefix\":\"lr-bus-\",\"Limit\":2}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("Rules", hasSize(2))
|
||||
.body("Rules[0].Name", equalTo("lr-bus-1"))
|
||||
.body("Rules[1].Name", equalTo("lr-bus-2"))
|
||||
.body("NextToken", notNullValue())
|
||||
.extract().jsonPath().getString("NextToken");
|
||||
|
||||
given().contentType(CT).header("X-Amz-Target", "AWSEvents.ListRules")
|
||||
.body("{\"EventBusName\":\"lr-paging-bus\",\"NamePrefix\":\"lr-bus-\",\"Limit\":2,"
|
||||
+ "\"NextToken\":\"" + token + "\"}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("Rules", hasSize(1))
|
||||
.body("Rules[0].Name", equalTo("lr-bus-skip"))
|
||||
.body("NextToken", nullValue());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"0", "101", "4294967297", "-4294967297"})
|
||||
void outOfRangeLimitIsAValidationException(String limit) {
|
||||
given().contentType(CT).header("X-Amz-Target", "AWSEvents.ListRules")
|
||||
.body("{\"Limit\":" + limit + "}")
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("ValidationException"))
|
||||
.body("message", containsString("Value '" + limit + "' at 'limit' failed to satisfy constraint"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"100.9", "true", "\"5\""})
|
||||
void nonIntegerLimitIsASerializationException(String limit) {
|
||||
given().contentType(CT).header("X-Amz-Target", "AWSEvents.ListRules")
|
||||
.body("{\"Limit\":" + limit + "}")
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("SerializationException"))
|
||||
.body("message", containsString("can not be converted to Integer"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {1, 100})
|
||||
void boundaryLimitsAreAccepted(int limit) {
|
||||
putRule("lr-bound-a", null);
|
||||
putRule("lr-bound-b", null);
|
||||
|
||||
given().contentType(CT).header("X-Amz-Target", "AWSEvents.ListRules")
|
||||
.body("{\"NamePrefix\":\"lr-bound-\",\"Limit\":" + limit + "}")
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("Rules", hasSize(Math.min(limit, 2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownNextTokenIsInvalidToken() {
|
||||
given().contentType(CT).header("X-Amz-Target", "AWSEvents.ListRules")
|
||||
.body("{\"Limit\":1,\"NextToken\":\"not-a-token!\"}")
|
||||
.when().post("/")
|
||||
.then().statusCode(400)
|
||||
.body("__type", equalTo("InvalidToken"));
|
||||
}
|
||||
}
|
||||
+75
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
@@ -29,6 +30,7 @@ import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
@@ -445,6 +447,79 @@ class EventBridgeServiceTest {
|
||||
assertEquals(2, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listRulesPagesEachRuleExactlyOnceInNameOrder() {
|
||||
for (String name : List.of("page-c", "page-a", "page-d", "page-b", "other")) {
|
||||
service.putRule(name, null, null, "rate(1 minute)", RuleState.ENABLED,
|
||||
null, null, null, REGION);
|
||||
}
|
||||
|
||||
List<String> seen = new ArrayList<>();
|
||||
String token = null;
|
||||
int pages = 0;
|
||||
do {
|
||||
PaginatedResult<Rule> page = service.listRules(null, "page-", 1, token, REGION);
|
||||
assertEquals(1, page.items().size());
|
||||
seen.add(page.items().get(0).getName());
|
||||
token = page.nextToken();
|
||||
pages++;
|
||||
} while (token != null);
|
||||
|
||||
assertEquals(List.of("page-a", "page-b", "page-c", "page-d"), seen);
|
||||
assertEquals(4, pages);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listRulesOmitsNextTokenWhenEverythingFits() {
|
||||
service.putRule("fit-a", null, null, "rate(1 minute)", RuleState.ENABLED, null, null, null, REGION);
|
||||
service.putRule("fit-b", null, null, "rate(1 minute)", RuleState.ENABLED, null, null, null, REGION);
|
||||
|
||||
PaginatedResult<Rule> exact = service.listRules(null, null, 2, null, REGION);
|
||||
assertEquals(2, exact.items().size());
|
||||
assertNull(exact.nextToken());
|
||||
|
||||
PaginatedResult<Rule> unbounded = service.listRules(null, null, null, null, REGION);
|
||||
assertEquals(2, unbounded.items().size());
|
||||
assertNull(unbounded.nextToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listRulesPagesOnACustomBusWithoutLeakingDefaultBusRules() {
|
||||
service.createEventBus("paging-bus", null, null, REGION);
|
||||
service.putRule("custom-1", "paging-bus", null, "rate(1 minute)", RuleState.ENABLED,
|
||||
null, null, null, REGION);
|
||||
service.putRule("custom-2", "paging-bus", null, "rate(1 minute)", RuleState.ENABLED,
|
||||
null, null, null, REGION);
|
||||
service.putRule("custom-3", null, null, "rate(1 minute)", RuleState.ENABLED,
|
||||
null, null, null, REGION);
|
||||
|
||||
PaginatedResult<Rule> first = service.listRules("paging-bus", "custom-", 1, null, REGION);
|
||||
assertEquals("custom-1", first.items().get(0).getName());
|
||||
assertNotNull(first.nextToken());
|
||||
|
||||
PaginatedResult<Rule> second = service.listRules("paging-bus", "custom-", 1, first.nextToken(), REGION);
|
||||
assertEquals("custom-2", second.items().get(0).getName());
|
||||
assertNull(second.nextToken());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, -1, 101})
|
||||
void listRulesRejectsOutOfRangeLimit(int limit) {
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> service.listRules(null, null, limit, null, REGION));
|
||||
assertEquals("ValidationException", ex.getErrorCode());
|
||||
assertTrue(ex.getMessage().contains("'" + limit + "' at 'limit'"), ex.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"not base64 !", "Zm9vOmJhcg"})
|
||||
void listRulesRejectsATokenItDidNotMint(String token) {
|
||||
AwsException ex = assertThrows(AwsException.class,
|
||||
() -> service.listRules(null, null, 1, token, REGION));
|
||||
assertEquals("InvalidToken", ex.getErrorCode());
|
||||
assertEquals(400, ex.getHttpStatus());
|
||||
}
|
||||
|
||||
// ──────────────────────────── Targets ────────────────────────────
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user