Light, fluffy, and always free - The AWS Local Emulator alternative
fix(kinesisanalytics): paginate ListApplications (#6813)
Jimmy Liu Jia3d4fdfe7ca26
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java b/src/main/java/io/github/hectorvent/floci/core/common/Pagination.javaindex ae2b534ea2..7f4e9c95bc 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/Pagination.java@@ -128,14 +128,38 @@ public final class Pagination {Integer maxResults, String nextToken,int defaultPageSize, int maxResultsLimit, int maxTokenLength,String errorCode, String namespace) {+ validatePageSizeAndToken(maxResults, nextToken, maxResultsLimit, maxTokenLength, errorCode,+ false, "Pagination token must not exceed " + maxTokenLength + " characters.");+ return paginate(all, cursorOf, maxResults == null ? defaultPageSize : maxResults, nextToken, namespace,+ token -> new AwsException(errorCode, "Invalid pagination token.", 400));+ }++ /**+ * Paginates a list whose cursor is itself a valid API token. Unlike the namespaced overloads,+ * this leaves the sorted cursor unencoded in the response, so an API whose resource name is+ * also its token can resume after that resource has been removed.+ */+ public static <T> PaginatedResult<T> paginateUnencoded(List<T> all, Function<T, String> cursorOf,+ Integer maxResults, String nextToken,+ int defaultPageSize, int maxResultsLimit,+ int maxTokenLength, String errorCode) {+ validatePageSizeAndToken(maxResults, nextToken, maxResultsLimit, maxTokenLength, errorCode,+ true, "Pagination token must be between 1 and " + maxTokenLength + " characters.");+ int limit = maxResults == null ? defaultPageSize : maxResults;+ return slice(all, cursorOf, limit, nextToken, "", Function.identity());+ }++ private static void validatePageSizeAndToken(Integer maxResults, String nextToken,+ int maxResultsLimit, int maxTokenLength,+ String errorCode, boolean tokenMustBeNonEmpty,+ String tokenErrorMessage) {if (maxResults != null && (maxResults < 1 || maxResults > maxResultsLimit)) {throw new AwsException(errorCode, "Page size must be between 1 and " + maxResultsLimit + ".", 400);}- if (nextToken != null && nextToken.length() > maxTokenLength) {- throw new AwsException(errorCode, "Pagination token must not exceed " + maxTokenLength + " characters.", 400);+ if (nextToken != null+ && ((tokenMustBeNonEmpty && nextToken.isEmpty()) || nextToken.length() > maxTokenLength)) {+ throw new AwsException(errorCode, tokenErrorMessage, 400);}- return paginate(all, cursorOf, maxResults == null ? defaultPageSize : maxResults, nextToken, namespace,- token -> new AwsException(errorCode, "Invalid pagination token.", 400));}/** Reads an optional JSON page size without coercing strings, fractions, or overflowing integers. */diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandler.javaindex fd2024b0e3..c11fd80917 100644--- a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandler.java@@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.node.ArrayNode;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsErrorResponse;+import io.github.hectorvent.floci.core.common.PaginatedResult;+import io.github.hectorvent.floci.core.common.Pagination;import io.github.hectorvent.floci.services.kinesisanalytics.model.FlinkApplication;import io.github.hectorvent.floci.services.kinesisanalytics.model.Snapshot;import jakarta.enterprise.context.ApplicationScoped;@@ -114,9 +116,12 @@ public class KinesisAnalyticsV2JsonHandler {}private Response handleListApplications(JsonNode request, String region) {+ Integer limit = Pagination.parseMaxResults(request, "Limit", "InvalidRequestException");+ String nextToken = Pagination.parseNextToken(request, "NextToken", "InvalidRequestException");+ PaginatedResult<FlinkApplication> page = service.listApplications(region, limit, nextToken);ObjectNode response = objectMapper.createObjectNode();ArrayNode summaries = response.putArray("ApplicationSummaries");- for (FlinkApplication app : service.listApplications(region)) {+ for (FlinkApplication app : page.items()) {ObjectNode summary = summaries.addObject();summary.put("ApplicationName", app.getApplicationName());summary.put("ApplicationARN", app.getApplicationArn());@@ -127,6 +132,9 @@ public class KinesisAnalyticsV2JsonHandler {summary.put("ApplicationMode", app.getApplicationMode());}}+ if (page.nextToken() != null) {+ response.put("NextToken", page.nextToken());+ }return Response.ok(response).build();}diff --git a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2Service.java b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2Service.javaindex 8e9aec27ad..229e3213de 100644--- a/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2Service.java+++ b/src/main/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2Service.java@@ -4,6 +4,8 @@ import com.fasterxml.jackson.core.type.TypeReference;import io.github.hectorvent.floci.config.EmulatorConfig;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.PaginatedResult;+import io.github.hectorvent.floci.core.common.Pagination;import io.github.hectorvent.floci.core.common.RegionResolver;import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;@@ -245,6 +247,14 @@ public class KinesisAnalyticsV2Service {return storage.scan(key -> key.startsWith(prefix));}+ public PaginatedResult<FlinkApplication> listApplications(String region, Integer limit, String nextToken) {+ if (nextToken != null && !nextToken.isEmpty() && !APPLICATION_NAME.matcher(nextToken).matches()) {+ throw new AwsException("InvalidRequestException", "Invalid NextToken.", 400);+ }+ return Pagination.paginateUnencoded(listApplications(region), FlinkApplication::getApplicationName,+ limit, nextToken, 50, 50, 128, "InvalidRequestException");+ }+public FlinkApplication startApplication(String applicationName) {return startApplication(applicationName, config.defaultRegion());}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.javaindex 230f82f366..4398ee7aae 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/PaginationTest.java@@ -175,7 +175,8 @@ class PaginationTest {@Testvoid boundedNamespacedPaging_refusesOutOfRangeSizes() {for (int size : new int[] {-1, 0, 4}) {- assertInvalid(() -> bounded(size, null));+ AwsException error = assertInvalid(() -> bounded(size, null));+ assertEquals("Page size must be between 1 and 3.", error.getMessage());}}@@ -187,7 +188,13 @@ class PaginationTest {assertTrue(bounded(1, boundary).items().isEmpty());String token = Base64.getUrlEncoder().withoutPadding().encodeToString(("ns:" + "a".repeat(100)).getBytes(StandardCharsets.UTF_8));- assertInvalid(() -> bounded(1, token));+ AwsException error = assertInvalid(() -> bounded(1, token));+ assertEquals("Pagination token must not exceed 32 characters.", error.getMessage());+ }++ @Test+ void boundedNamespacedPaging_treatsAnEmptyTokenAsTheFirstPage() {+ assertThat(bounded(1, "").items(), contains("a"));}@Test@@ -196,6 +203,31 @@ class PaginationTest {assertInvalid(() -> bounded(1, page(1, null, "other").nextToken()));}+ @Test+ void unencodedPagingReturnsTheRawCursorAndResumesAfterItIsDeleted() {+ PaginatedResult<String> first = Pagination.paginateUnencoded(ITEMS, Function.identity(),+ 1, null, 2, 3, 128, "InvalidInput");+ assertThat(first.items(), contains("a"));+ assertEquals("a", first.nextToken());++ PaginatedResult<String> rest = Pagination.paginateUnencoded(List.of("b", "c"),+ Function.identity(), 1, first.nextToken(), 2, 3, 128, "InvalidInput");+ assertThat(rest.items(), contains("b"));+ assertEquals("b", rest.nextToken());+ }++ @Test+ void unencodedPagingValidatesBoundsAndTokenLength() {+ AwsException pageSizeError = assertInvalid(() -> Pagination.paginateUnencoded(ITEMS,+ Function.identity(), 0, null, 2, 3, 128, "InvalidInput"));+ assertEquals("Page size must be between 1 and 3.", pageSizeError.getMessage());+ for (String token : List.of("", "x".repeat(129))) {+ AwsException tokenError = assertInvalid(() -> Pagination.paginateUnencoded(ITEMS,+ Function.identity(), 1, token, 2, 3, 128, "InvalidInput"));+ assertEquals("Pagination token must be between 1 and 128 characters.", tokenError.getMessage());+ }+ }+@Testvoid jsonPageSize_preservesOmittedNullZeroAndIntegerValues() throws Exception {for (String field : List.of("MaxResults", "PageSize")) {@@ -232,10 +264,11 @@ class PaginationTest {return Pagination.paginate(ITEMS, Function.identity(), size, token, 2, 3, 32, "InvalidInput", "ns");}- private static void assertInvalid(Executable call) {+ private static AwsException assertInvalid(Executable call) {AwsException error = assertThrows(AwsException.class, call);assertEquals("InvalidInput", error.getErrorCode());assertEquals(400, error.getHttpStatus());+ return error;}private static PaginatedResult<String> page(int limit, String token, String namespace) {diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2IntegrationTest.javaindex 15116bf740..5eb5491a92 100644--- a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2IntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2IntegrationTest.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.kinesisanalytics;import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;import io.quarkus.test.junit.QuarkusTest;import io.restassured.path.json.config.JsonPathConfig;+import io.restassured.response.Response;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.Test;@@ -14,6 +15,8 @@ import static org.hamcrest.Matchers.equalTo;import static org.hamcrest.Matchers.hasItem;import static org.hamcrest.Matchers.not;import static org.hamcrest.Matchers.startsWith;+import static org.junit.jupiter.api.Assertions.assertNotEquals;+import static org.junit.jupiter.api.Assertions.assertNotNull;/*** Drives the full JSON-1.1 dispatch path (AwsJson11Controller → ResolvedServiceCatalog →@@ -136,6 +139,37 @@ class KinesisAnalyticsV2IntegrationTest {.body("ApplicationSummaries.ApplicationName", hasItem("it-list"));}+ @Test+ void listApplicationsPaginatesThroughJsonProtocol() {+ createApplication("it-page-a-" + System.nanoTime());+ createApplication("it-page-b-" + System.nanoTime());++ Response firstPage = given()+ .header("X-Amz-Target", "KinesisAnalytics_20180523.ListApplications")+ .contentType(CONTENT_TYPE)+ .body("{\"Limit\":1}")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("ApplicationSummaries.size()", equalTo(1))+ .extract().response();+ String token = firstPage.path("NextToken");+ assertNotNull(token);++ String secondName = given()+ .header("X-Amz-Target", "KinesisAnalytics_20180523.ListApplications")+ .contentType(CONTENT_TYPE)+ .body("{\"Limit\":1,\"NextToken\":\"" + token + "\"}")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("ApplicationSummaries.size()", equalTo(1))+ .extract().path("ApplicationSummaries[0].ApplicationName");+ assertNotEquals(firstPage.path("ApplicationSummaries[0].ApplicationName"), secondName);+ }+@Testvoid deleteApplicationSucceeds() {createApplication("it-delete");diff --git a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandlerTest.javaindex e1f48a524a..1046eded1c 100644--- a/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandlerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/kinesisanalytics/KinesisAnalyticsV2JsonHandlerTest.java@@ -101,6 +101,86 @@ class KinesisAnalyticsV2JsonHandlerTest {assertEquals(2, summaries.size());}+ @Test+ void listApplicationsHonorsLimitAndReturnsNextToken() {+ String longName = "a".repeat(128);+ createApplication(longName);+ createApplication("demo-b");++ ObjectNode request = MAPPER.createObjectNode().put("Limit", 1);+ ObjectNode firstPage = entity(handler.handle("ListApplications", request, REGION));++ assertEquals(1, firstPage.get("ApplicationSummaries").size());+ assertEquals(longName, firstPage.get("ApplicationSummaries").get(0).get("ApplicationName").asText());+ assertThat(firstPage.hasNonNull("NextToken"), is(true));+ String token = firstPage.get("NextToken").asText();+ assertEquals(longName, token);++ createApplication("a0");+ ObjectNode secondRequest = MAPPER.createObjectNode().put("Limit", 1).put("NextToken", token);+ ObjectNode secondPage = entity(handler.handle("ListApplications", secondRequest, REGION));+ assertEquals("demo-b", secondPage.get("ApplicationSummaries").get(0).get("ApplicationName").asText());+ assertThat(secondPage.has("NextToken"), is(false));+ }++ @Test+ void listApplicationsContinuesAfterTheLastReturnedApplicationIsDeleted() {+ createApplication("demo-a");+ createApplication("demo-b");++ ObjectNode firstPage = entity(handler.handle("ListApplications",+ MAPPER.createObjectNode().put("Limit", 1), REGION));+ String firstName = firstPage.get("ApplicationSummaries").get(0).get("ApplicationName").asText();+ String token = firstPage.get("NextToken").asText();+ assertEquals(firstName, token);++ ObjectNode describe = MAPPER.createObjectNode().put("ApplicationName", firstName);+ double createTimestamp = entity(handler.handle("DescribeApplication", describe, REGION))+ .get("ApplicationDetail").get("CreateTimestamp").asDouble();+ ObjectNode delete = MAPPER.createObjectNode()+ .put("ApplicationName", firstName)+ .put("CreateTimestamp", createTimestamp);+ assertThat(handler.handle("DeleteApplication", delete, REGION).getStatus(), is(200));++ ObjectNode secondPage = entity(handler.handle("ListApplications",+ MAPPER.createObjectNode().put("Limit", 1).put("NextToken", token), REGION));+ assertEquals("demo-b", secondPage.get("ApplicationSummaries").get(0).get("ApplicationName").asText());+ }++ @Test+ void listApplicationsUsesDefaultPageSizeAndEndsWithoutToken() {+ for (int index = 0; index < 51; index++) {+ createApplication(String.format("default-page-%02d", index));+ }++ ObjectNode firstPage = entity(handler.handle("ListApplications", MAPPER.createObjectNode(), REGION));++ assertEquals(50, firstPage.get("ApplicationSummaries").size());+ assertThat(firstPage.hasNonNull("NextToken"), is(true));++ ObjectNode secondRequest = MAPPER.createObjectNode().put("NextToken",+ firstPage.get("NextToken").asText());+ ObjectNode secondPage = entity(handler.handle("ListApplications", secondRequest, REGION));++ assertEquals(1, secondPage.get("ApplicationSummaries").size());+ assertThat(secondPage.has("NextToken"), is(false));+ }++ @Test+ void listApplicationsRejectsInvalidLimitAndToken() {+ createApplication("demo-a");+ for (int limit : new int[]{0, 51}) {+ AwsException error = assertThrows(AwsException.class, () -> handler.handle("ListApplications",+ MAPPER.createObjectNode().put("Limit", limit), REGION));+ assertEquals("InvalidRequestException", error.getErrorCode());+ }+ for (String token : new String[]{"", "!", "x".repeat(129)}) {+ AwsException error = assertThrows(AwsException.class, () -> handler.handle("ListApplications",+ MAPPER.createObjectNode().put("NextToken", token), REGION));+ assertEquals("InvalidRequestException", error.getErrorCode());+ }+ }+@Testvoid deleteApplicationRemovesIt() {createApplication("demo");