Light, fluffy, and always free - The AWS Local Emulator alternative
fix(codebuild): paginate ListBuilds and ListBuildsForProject (#6707)
Jimmy Liu Jiafc5598c579bb
diff --git a/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildJsonHandler.javaindex e7c3b5ae5e..c7ac5d8dc0 100644--- a/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildJsonHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildJsonHandler.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.codebuild;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.services.codebuild.CodeBuildService.BuildPage;import io.github.hectorvent.floci.services.codebuild.CodeBuildService.ProjectPage;import io.github.hectorvent.floci.services.codebuild.CodeBuildService.ReportGroupPage;import io.github.hectorvent.floci.services.codebuild.model.Build;@@ -279,14 +280,33 @@ public class CodeBuildJsonHandler {}private Response listBuilds(JsonNode req, String region, String account) {- return Response.ok(Map.of("ids", service.listBuilds(region, account,- req.path("sortOrder").asText(null)))).build();+ BuildPage page = service.listBuildsPage(region, account,+ req.path("sortOrder").asText(null), buildListToken(req));+ return buildListResponse(page);}private Response listBuildsForProject(JsonNode req, String region, String account) {String projectName = req.path("projectName").asText(null);- return Response.ok(Map.of("ids", service.listBuildsForProject(region, account, projectName,- req.path("sortOrder").asText(null)))).build();+ BuildPage page = service.listBuildsForProjectPage(region, account, projectName,+ req.path("sortOrder").asText(null), buildListToken(req));+ return buildListResponse(page);+ }++ private String buildListToken(JsonNode req) {+ JsonNode token = req.get("nextToken");+ if (token != null && !token.isTextual()) {+ throw new AwsException("InvalidInputException", "Invalid nextToken", 400);+ }+ return token == null ? null : token.asText();+ }++ private Response buildListResponse(BuildPage page) {+ Map<String, Object> result = new HashMap<>();+ result.put("ids", page.ids());+ if (page.nextToken() != null) {+ result.put("nextToken", page.nextToken());+ }+ return Response.ok(result).build();}private Response stopBuild(JsonNode req, String region, String account) {diff --git a/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildPagination.java b/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildPagination.javanew file mode 100644index 0000000000..97c7283046--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildPagination.java@@ -0,0 +1,58 @@+package io.github.hectorvent.floci.services.codebuild;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import com.fasterxml.jackson.databind.node.ObjectNode;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.core.common.Base64Url;++import java.io.IOException;+import java.util.Base64;+import java.util.Comparator;+import java.util.List;+import java.util.function.Function;++final class CodeBuildPagination {++ private static final ObjectMapper TOKEN_MAPPER = new ObjectMapper();++ private CodeBuildPagination() {+ }++ static <T> Page<T> page(List<T> sorted, Comparator<T> comparator, int pageSize, String nextToken,+ Function<String, T> readCursor, Function<T, String> writeCursor) {+ int start = 0;+ if (nextToken != null) {+ T anchor = readCursor.apply(nextToken);+ while (start < sorted.size() && comparator.compare(sorted.get(start), anchor) <= 0) {+ start++;+ }+ }+ int end = Math.min(start + pageSize, sorted.size());+ List<T> items = sorted.subList(start, end);+ String continuation = end < sorted.size() ? writeCursor.apply(items.getLast()) : null;+ return new Page<>(items, continuation);+ }++ static JsonNode readToken(String token) {+ if (token.isEmpty()) {+ throw new AwsException("InvalidInputException", "Invalid nextToken", 400);+ }+ try {+ return TOKEN_MAPPER.readTree(Base64.getUrlDecoder().decode(token));+ } catch (IllegalArgumentException | IOException e) {+ throw new AwsException("InvalidInputException", "Invalid nextToken", 400);+ }+ }++ static ObjectNode newToken() {+ return TOKEN_MAPPER.createObjectNode();+ }++ static String writeToken(ObjectNode token) {+ return Base64Url.encode(token.toString());+ }++ record Page<T>(List<T> items, String nextToken) {+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildService.java b/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildService.javaindex 381890bd8e..95d910989c 100644--- a/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildService.java+++ b/src/main/java/io/github/hectorvent/floci/services/codebuild/CodeBuildService.java@@ -2,12 +2,10 @@ package io.github.hectorvent.floci.services.codebuild;import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.JsonNode;-import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.node.ObjectNode;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.Base64Url;import io.github.hectorvent.floci.core.storage.StorageBackedMap;import io.github.hectorvent.floci.core.storage.StorageFactory;import io.github.hectorvent.floci.services.codebuild.model.Build;@@ -22,10 +20,8 @@ import jakarta.annotation.PostConstruct;import jakarta.enterprise.context.ApplicationScoped;import jakarta.inject.Inject;-import java.io.IOException;import java.time.Instant;import java.util.ArrayList;-import java.util.Base64;import java.util.Comparator;import java.util.List;import java.util.Map;@@ -40,7 +36,7 @@ import java.util.stream.Collectors;public class CodeBuildService {private static final int LIST_PROJECTS_PAGE_SIZE = 100;- private static final ObjectMapper PAGE_TOKEN_MAPPER = new ObjectMapper();+ private static final int LIST_BUILDS_PAGE_SIZE = 100;// key: region -> name -> projectprivate Map<String, Map<String, Project>> projects = new ConcurrentHashMap<>();@@ -278,9 +274,9 @@ public class CodeBuildService {}}- SortedListPage page = paginateList(projectSortKeys(region), region, sortBy, sortOrder,+ CodeBuildPagination.Page<ListSortKey> page = paginateList(projectSortKeys(region), region, sortBy, sortOrder,LIST_PROJECTS_PAGE_SIZE, nextToken, "ListProjects", null);- return new ProjectPage(page.keys().stream().map(ListSortKey::name).toList(), page.nextToken());+ return new ProjectPage(page.items().stream().map(ListSortKey::name).toList(), page.nextToken());}public record ProjectPage(List<String> projects, String nextToken) {@@ -374,28 +370,19 @@ public class CodeBuildService {.map(group -> new ListSortKey(group.getCreated(), group.getLastModified(),group.getName(), group.getArn())).toList();- SortedListPage page = paginateList(keys, region, criterion, order, maxResults, nextToken,+ CodeBuildPagination.Page<ListSortKey> page = paginateList(keys, region, criterion, order, maxResults, nextToken,"ListReportGroups", "lastArn");- return new ReportGroupPage(page.keys().stream().map(ListSortKey::id).toList(), page.nextToken());+ return new ReportGroupPage(page.items().stream().map(ListSortKey::id).toList(), page.nextToken());}- private SortedListPage paginateList(List<ListSortKey> keys, String region, String sortBy,- String sortOrder, int pageSize, String nextToken,- String action, String idField) {+ private CodeBuildPagination.Page<ListSortKey> paginateList(List<ListSortKey> keys, String region, String sortBy,+ String sortOrder, int pageSize, String nextToken,+ String action, String idField) {Comparator<ListSortKey> comparator = listComparator(sortBy, sortOrder);List<ListSortKey> sorted = keys.stream().sorted(comparator).toList();- int start = 0;- if (nextToken != null) {- ListSortKey anchor = decodeListToken(nextToken, region, sortBy, sortOrder, action, idField);- while (start < sorted.size() && comparator.compare(sorted.get(start), anchor) <= 0) {- start++;- }- }- int end = Math.min(start + pageSize, sorted.size());- List<ListSortKey> page = sorted.subList(start, end);- String token = end < sorted.size()- ? encodeListToken(page.getLast(), region, sortBy, sortOrder, action, idField) : null;- return new SortedListPage(page, token);+ return CodeBuildPagination.page(sorted, comparator, pageSize, nextToken,+ token -> decodeListToken(token, region, sortBy, sortOrder, action, idField),+ last -> encodeListToken(last, region, sortBy, sortOrder, action, idField));}private Comparator<ListSortKey> listComparator(String sortBy, String sortOrder) {@@ -415,15 +402,7 @@ public class CodeBuildService {private ListSortKey decodeListToken(String token, String region, String sortBy,String sortOrder, String action, String idField) {- if (token.isEmpty()) {- throw new AwsException("InvalidInputException", "Invalid nextToken", 400);- }- JsonNode cursor;- try {- cursor = PAGE_TOKEN_MAPPER.readTree(Base64.getUrlDecoder().decode(token));- } catch (IllegalArgumentException | IOException e) {- throw new AwsException("InvalidInputException", "Invalid nextToken", 400);- }+ JsonNode cursor = CodeBuildPagination.readToken(token);JsonNode lastName = cursor == null ? null : cursor.path("lastName");JsonNode sortValue = cursor == null ? null : cursor.path("sortValue");JsonNode lastId = cursor == null || idField == null ? null : cursor.path(idField);@@ -452,7 +431,7 @@ public class CodeBuildService {case "LAST_MODIFIED_TIME" -> last.lastModified();default -> null;};- ObjectNode cursor = PAGE_TOKEN_MAPPER.createObjectNode();+ ObjectNode cursor = CodeBuildPagination.newToken();cursor.put("action", action);cursor.put("region", region);cursor.put("sortBy", sortBy);@@ -462,7 +441,7 @@ public class CodeBuildService {}cursor.put("lastName", last.name());cursor.put("sortValue", value);- return Base64Url.encode(cursor.toString());+ return CodeBuildPagination.writeToken(cursor);}public record ReportGroupPage(List<String> reportGroups, String nextToken) {@@ -471,9 +450,6 @@ public class CodeBuildService {private record ListSortKey(Double created, Double lastModified, String name, String id) {}- private record SortedListPage(List<ListSortKey> keys, String nextToken) {- }-// ---- Source Credentials ----public SourceCredential importSourceCredentials(String region, String account,@@ -676,6 +652,85 @@ public class CodeBuildService {.toList();}+ public BuildPage listBuildsPage(String region, String account, String sortOrder, String nextToken) {+ return paginateBuilds(region, account, null, sortOrder, nextToken, false);+ }++ public BuildPage listBuildsForProjectPage(String region, String account, String projectName,+ String sortOrder, String nextToken) {+ if (projectName == null || projectName.isEmpty()) {+ throw new AwsException("InvalidInputException", "projectName is required", 400);+ }+ return paginateBuilds(region, account, projectName, sortOrder, nextToken, true);+ }++ private BuildPage paginateBuilds(String region, String account, String projectName,+ String sortOrder, String nextToken, boolean forProject) {+ Comparator<Build> comparator = forProject+ ? Comparator.comparing(Build::getBuildNumber).thenComparing(Build::getId)+ : Comparator.comparing(Build::getId);+ if (isDescendingSortOrder(sortOrder, true)) {+ comparator = comparator.reversed();+ }+ List<Build> sorted = buildsFor(account, region).values().stream()+ .filter(build -> !forProject || projectName.equals(build.getProjectName()))+ .sorted(comparator)+ .toList();+ if (forProject && sortOrder != null && sorted.size() > LIST_BUILDS_PAGE_SIZE) {+ throw new AwsException("InvalidInputException",+ "sortOrder cannot be set when a project has more than 100 builds", 400);+ }++ String action = forProject ? "ListBuildsForProject" : "ListBuilds";+ CodeBuildPagination.Page<Build> page = CodeBuildPagination.page(sorted, comparator, LIST_BUILDS_PAGE_SIZE,+ nextToken,+ token -> decodeBuildToken(token, action, region, account, projectName, sortOrder, forProject),+ last -> encodeBuildToken(last, action, region, account, projectName, sortOrder, forProject));+ return new BuildPage(page.items().stream().map(Build::getId).toList(), page.nextToken());+ }++ private Build decodeBuildToken(String token, String action, String region, String account,+ String projectName, String sortOrder, boolean forProject) {+ JsonNode cursor = CodeBuildPagination.readToken(token);+ if (cursor == null || !cursor.isObject()+ || !action.equals(cursor.path("action").asText(null))+ || !region.equals(cursor.path("region").asText(null))+ || !account.equals(cursor.path("account").asText(null))+ || !cursor.has("projectName")+ || !Objects.equals(projectName, cursor.path("projectName").asText(null))+ || !cursor.has("sortOrder")+ || !Objects.equals(sortOrder, cursor.path("sortOrder").asText(null))+ || !cursor.path("lastId").isTextual() || cursor.path("lastId").asText().isEmpty()+ || forProject && (!cursor.path("buildNumber").isIntegralNumber()+ || !cursor.path("buildNumber").canConvertToLong())) {+ throw new AwsException("InvalidInputException", "Invalid nextToken", 400);+ }+ Build anchor = new Build();+ anchor.setId(cursor.path("lastId").asText());+ if (forProject) {+ anchor.setBuildNumber(cursor.path("buildNumber").longValue());+ }+ return anchor;+ }++ private String encodeBuildToken(Build last, String action, String region, String account,+ String projectName, String sortOrder, boolean forProject) {+ ObjectNode cursor = CodeBuildPagination.newToken();+ cursor.put("action", action);+ cursor.put("region", region);+ cursor.put("account", account);+ cursor.put("projectName", projectName);+ cursor.put("sortOrder", sortOrder);+ cursor.put("lastId", last.getId());+ if (forProject) {+ cursor.put("buildNumber", last.getBuildNumber());+ }+ return CodeBuildPagination.writeToken(cursor);+ }++ public record BuildPage(List<String> ids, String nextToken) {+ }+private boolean isDescendingSortOrder(String sortOrder, boolean defaultDescending) {if (sortOrder != null && !"ASCENDING".equals(sortOrder) && !"DESCENDING".equals(sortOrder)) {throw new AwsException("InvalidInputException", "Invalid sortOrder: " + sortOrder, 400);diff --git a/src/test/java/io/github/hectorvent/floci/services/codebuild/CodeBuildBuildListPaginationTest.java b/src/test/java/io/github/hectorvent/floci/services/codebuild/CodeBuildBuildListPaginationTest.javanew file mode 100644index 0000000000..4a80030254--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/codebuild/CodeBuildBuildListPaginationTest.java@@ -0,0 +1,200 @@+package io.github.hectorvent.floci.services.codebuild;++import com.fasterxml.jackson.databind.JsonNode;+import com.fasterxml.jackson.databind.ObjectMapper;+import io.github.hectorvent.floci.config.EmulatorConfig;+import io.github.hectorvent.floci.core.common.AwsException;+import io.github.hectorvent.floci.services.codebuild.model.ProjectArtifacts;+import io.github.hectorvent.floci.services.codebuild.model.ProjectEnvironment;+import io.github.hectorvent.floci.services.codebuild.model.ProjectSource;+import jakarta.ws.rs.core.Response;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++import java.util.ArrayList;+import java.util.Comparator;+import java.util.List;+import java.util.Map;+import java.util.stream.IntStream;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertThrows;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.Mockito.mock;++class CodeBuildBuildListPaginationTest {++ private static final String REGION = "us-east-1";+ private static final String ACCOUNT = "000000000000";+ private static final String PROJECT = "pagination-builds";++ private final ObjectMapper mapper = new ObjectMapper();+ private final CodeBuildService service = new CodeBuildService(+ mock(CodeBuildRunner.class), mock(EmulatorConfig.class), null);+ private final CodeBuildJsonHandler handler = new CodeBuildJsonHandler(service, mapper);++ @BeforeEach+ void createProject() {+ ProjectSource source = new ProjectSource();+ source.setType("NO_SOURCE");+ ProjectArtifacts artifacts = new ProjectArtifacts();+ artifacts.setType("NO_ARTIFACTS");+ service.createProject(REGION, ACCOUNT, PROJECT, null, source, null, null,+ artifacts, null, new ProjectEnvironment(), "arn:aws:iam::000000000000:role/codebuild",+ null, null, null, null, null, null, null);+ }++ @Test+ void listBuildsReturnsOneHundredIdsAndContinuation() throws Exception {+ createBuilds(101);+ JsonNode first = list("ListBuilds", Map.of());+ assertEquals(100, ids(first).size());+ assertTrue(first.path("nextToken").isTextual());++ JsonNode last = list("ListBuilds", Map.of("nextToken", first.path("nextToken").asText()));+ assertEquals(1, ids(last).size());+ assertFalse(last.has("nextToken"));+ List<String> seen = new ArrayList<>(ids(first));+ seen.addAll(ids(last));+ assertEquals(IntStream.rangeClosed(1, 101).mapToObj(number -> PROJECT + ":" + number)+ .sorted(Comparator.reverseOrder()).toList(), seen);+ }++ @Test+ void listBuildsForProjectReturnsOneHundredIdsAndContinuation() throws Exception {+ createBuilds(101);+ JsonNode first = list("ListBuildsForProject", Map.of("projectName", PROJECT));+ assertEquals(100, ids(first).size());+ assertTrue(first.path("nextToken").isTextual());++ JsonNode last = list("ListBuildsForProject", Map.of(+ "projectName", PROJECT, "nextToken", first.path("nextToken").asText()));+ assertEquals(List.of(PROJECT + ":1"), ids(last));+ assertFalse(last.has("nextToken"));+ List<String> seen = new ArrayList<>(ids(first));+ seen.addAll(ids(last));+ assertEquals(IntStream.rangeClosed(1, 101).mapToObj(number -> PROJECT + ":" + number)+ .toList().reversed(), seen);+ }++ @Test+ void exactPageOmitsContinuationAndAllowsProjectSortOrder() throws Exception {+ createBuilds(100);+ JsonNode allBuilds = list("ListBuilds", Map.of());+ assertEquals(100, ids(allBuilds).size());+ assertFalse(allBuilds.has("nextToken"));++ JsonNode projectBuilds = list("ListBuildsForProject",+ Map.of("projectName", PROJECT, "sortOrder", "ASCENDING"));+ assertEquals(IntStream.rangeClosed(1, 100).mapToObj(number -> PROJECT + ":" + number).toList(),+ ids(projectBuilds));+ assertFalse(projectBuilds.has("nextToken"));+ }++ @Test+ void listBuildsContinuesInAscendingBuildIdOrder() throws Exception {+ createBuilds(101);+ JsonNode first = list("ListBuilds", Map.of("sortOrder", "ASCENDING"));+ JsonNode last = list("ListBuilds", Map.of(+ "sortOrder", "ASCENDING", "nextToken", first.path("nextToken").asText()));+ List<String> seen = new ArrayList<>(ids(first));+ seen.addAll(ids(last));+ assertEquals(IntStream.rangeClosed(1, 101).mapToObj(number -> PROJECT + ":" + number)+ .sorted().toList(), seen);+ assertFalse(last.has("nextToken"));+ }++ @Test+ void projectBuildsContinueThroughThreePages() throws Exception {+ createBuilds(201);+ List<String> seen = new ArrayList<>();+ String token = null;+ for (int pageIndex = 0; pageIndex < 3; pageIndex++) {+ Map<String, String> request = token == null+ ? Map.of("projectName", PROJECT)+ : Map.of("projectName", PROJECT, "nextToken", token);+ JsonNode page = list("ListBuildsForProject", request);+ assertEquals(pageIndex < 2 ? 100 : 1, ids(page).size());+ seen.addAll(ids(page));+ token = page.path("nextToken").isTextual() ? page.path("nextToken").asText() : null;+ }+ assertEquals(IntStream.rangeClosed(1, 201).mapToObj(number -> PROJECT + ":" + number)+ .toList().reversed(), seen);+ }++ @Test+ void projectContinuationKeepsItsPositionWhenANewBuildStarts() throws Exception {+ createBuilds(101);+ JsonNode first = list("ListBuildsForProject", Map.of("projectName", PROJECT));+ String token = first.path("nextToken").asText();++ createBuilds(1);++ JsonNode last = list("ListBuildsForProject", Map.of("projectName", PROJECT, "nextToken", token));+ assertEquals(List.of(PROJECT + ":1"), ids(last));+ assertFalse(last.has("nextToken"));+ }++ @Test+ void projectSortOrderIsRejectedAboveOneHundredBuilds() {+ createBuilds(101);+ AwsException error = assertThrows(AwsException.class, () -> list("ListBuildsForProject",+ Map.of("projectName", PROJECT, "sortOrder", "ASCENDING")));+ assertEquals("InvalidInputException", error.getErrorCode());+ assertEquals(400, error.getHttpStatus());+ }++ @Test+ void projectNameIsRequiredForProjectBuildListing() {+ AwsException error = assertThrows(AwsException.class,+ () -> list("ListBuildsForProject", Map.of()));+ assertEquals("InvalidInputException", error.getErrorCode());+ assertEquals(400, error.getHttpStatus());+ }++ @Test+ void continuationRejectsOtherRequestScopesAndMalformedTokens() throws Exception {+ createBuilds(101);+ String globalToken = list("ListBuilds", Map.of()).path("nextToken").asText();+ String projectToken = list("ListBuildsForProject", Map.of("projectName", PROJECT))+ .path("nextToken").asText();+ assertInvalidToken("ListBuilds", Map.of("nextToken", globalToken, "sortOrder", "ASCENDING"),+ REGION, ACCOUNT);+ assertInvalidToken("ListBuilds", Map.of("nextToken", globalToken), "eu-west-1", ACCOUNT);+ assertInvalidToken("ListBuilds", Map.of("nextToken", globalToken), REGION, "111111111111");+ assertInvalidToken("ListBuilds", Map.of("nextToken", "not-a-token!"), REGION, ACCOUNT);+ assertInvalidToken("ListBuilds", Map.of("nextToken", 42), REGION, ACCOUNT);+ assertInvalidToken("ListBuildsForProject", Map.of("projectName", PROJECT, "nextToken", globalToken),+ REGION, ACCOUNT);+ assertInvalidToken("ListBuildsForProject", Map.of("projectName", "another", "nextToken", projectToken),+ REGION, ACCOUNT);+ }++ private JsonNode list(String action, Map<String, ?> request) throws Exception {+ return list(action, request, REGION, ACCOUNT);+ }++ private JsonNode list(String action, Map<String, ?> request, String region, String account) throws Exception {+ try (Response response = handler.handle(action, mapper.valueToTree(request), region, account)) {+ return mapper.valueToTree(response.getEntity());+ }+ }++ private void assertInvalidToken(String action, Map<String, ?> request, String region, String account) {+ AwsException error = assertThrows(AwsException.class, () -> list(action, request, region, account));+ assertEquals("InvalidInputException", error.getErrorCode());+ assertEquals(400, error.getHttpStatus());+ }++ private void createBuilds(int count) {+ for (int number = 0; number < count; number++) {+ service.startBuild(REGION, ACCOUNT, PROJECT, null, null, null, null, null, null, null);+ }+ }++ private List<String> ids(JsonNode page) {+ return mapper.convertValue(page.path("ids"),+ mapper.getTypeFactory().constructCollectionType(List.class, String.class));+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/codebuild/CodeBuildBuildListSortingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codebuild/CodeBuildBuildListSortingIntegrationTest.javaindex 22060d5a22..94dbd2f8a0 100644--- a/src/test/java/io/github/hectorvent/floci/services/codebuild/CodeBuildBuildListSortingIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/codebuild/CodeBuildBuildListSortingIntegrationTest.java@@ -7,20 +7,28 @@ import io.github.hectorvent.floci.services.codebuild.model.ProjectSource;import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;import io.quarkus.test.InjectMock;import io.quarkus.test.junit.QuarkusTest;+import io.restassured.path.json.JsonPath;import jakarta.inject.Inject;import org.junit.jupiter.api.AfterEach;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;+import java.util.ArrayList;+import java.util.HashMap;import java.util.List;+import java.util.Locale;+import java.util.Map;import java.util.UUID;+import java.util.concurrent.ThreadLocalRandom;import java.util.stream.IntStream;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.containsString;import static org.hamcrest.Matchers.equalTo;import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertNotNull;+import static org.junit.jupiter.api.Assertions.assertNull;@QuarkusTestclass CodeBuildBuildListSortingIntegrationTest {@@ -84,6 +92,47 @@ class CodeBuildBuildListSortingIntegrationTest {assertEquals(expected.reversed(), listBuildsForProject(null));}+ @Test+ void buildListContinuationUsesJsonWireShape() {+ String region = "eu-west-3";+ String account = String.format(Locale.ROOT, "78%010d",+ ThreadLocalRandom.current().nextLong(10_000_000_000L));+ String auth = "AWS4-HMAC-SHA256 Credential=" + account+ + "/20261027/" + region + "/codebuild/aws4_request";+ String isolatedProject = "page-builds-" + UUID.randomUUID();+ ProjectSource source = new ProjectSource();+ source.setType("NO_SOURCE");+ ProjectArtifacts artifacts = new ProjectArtifacts();+ artifacts.setType("NO_ARTIFACTS");+ service.createProject(region, account, isolatedProject, null, source, null, null,+ artifacts, null, new ProjectEnvironment(), "arn:aws:iam::" + account + ":role/codebuild",+ null, null, null, null, null, null, null);+ try {+ for (int number = 0; number < 101; number++) {+ service.startBuild(region, account, isolatedProject, null,+ null, null, null, null, null, null);+ }++ JsonPath first = given().header("Authorization", auth)+ .header("X-Amz-Target", "CodeBuild_20161006.ListBuildsForProject")+ .contentType(CONTENT_TYPE).body(Map.of("projectName", isolatedProject))+ .post("/").then().statusCode(200).extract().jsonPath();+ assertEquals(100, first.getList("ids", String.class).size());+ String token = first.getString("nextToken");+ assertNotNull(token);++ JsonPath last = given().header("Authorization", auth)+ .header("X-Amz-Target", "CodeBuild_20161006.ListBuildsForProject")+ .contentType(CONTENT_TYPE)+ .body(Map.of("projectName", isolatedProject, "nextToken", token))+ .post("/").then().statusCode(200).extract().jsonPath();+ assertEquals(List.of(isolatedProject + ":1"), last.getList("ids", String.class));+ assertNull(last.getString("nextToken"));+ } finally {+ service.deleteProject(region, isolatedProject);+ }+ }+@Testvoid invalidSortOrderIsRejectedWithoutChangingBuilds() {List<String> allBuilds = listBuilds("DESCENDING");@@ -108,11 +157,22 @@ class CodeBuildBuildListSortingIntegrationTest {}private List<String> listBuilds(String sortOrder) {- List<String> ids = given().header("X-Amz-Target", "CodeBuild_20161006.ListBuilds")- .contentType(CONTENT_TYPE)- .body(sortOrder == null ? "{}" : "{\"sortOrder\":\"" + sortOrder + "\"}")- .post("/").then().statusCode(200)- .extract().jsonPath().getList("ids", String.class);+ List<String> ids = new ArrayList<>();+ String token = null;+ do {+ Map<String, String> request = new HashMap<>();+ if (sortOrder != null) {+ request.put("sortOrder", sortOrder);+ }+ if (token != null) {+ request.put("nextToken", token);+ }+ JsonPath page = given().header("X-Amz-Target", "CodeBuild_20161006.ListBuilds")+ .contentType(CONTENT_TYPE).body(request)+ .post("/").then().statusCode(200).extract().jsonPath();+ ids.addAll(page.getList("ids", String.class));+ token = page.getString("nextToken");+ } while (token != null);return ids.stream().filter(id -> id.startsWith(projectName + ":")).toList();}diff --git a/src/test/java/io/github/hectorvent/floci/services/codebuild/CodeBuildServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/codebuild/CodeBuildServiceTest.javaindex f869a93bb9..9f655103bc 100644--- a/src/test/java/io/github/hectorvent/floci/services/codebuild/CodeBuildServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/codebuild/CodeBuildServiceTest.java@@ -9,10 +9,12 @@ import io.github.hectorvent.floci.services.codebuild.model.ProjectSource;import org.junit.jupiter.api.Test;import org.mockito.ArgumentCaptor;+import java.util.Comparator;import java.util.List;import static org.junit.jupiter.api.Assertions.assertEquals;import static org.junit.jupiter.api.Assertions.assertNotEquals;+import static org.junit.jupiter.api.Assertions.assertNull;import static org.mockito.ArgumentMatchers.any;import static org.mockito.ArgumentMatchers.eq;import static org.mockito.Mockito.mock;@@ -24,6 +26,21 @@ class CodeBuildServiceTest {private static final String REGION = "us-east-1";private static final String ACCOUNT = "000000000000";+ @Test+ void sharedPaginationResumesAfterDeletingTheDescendingAnchor() {+ CodeBuildPagination.Page<Integer> first = CodeBuildPagination.page(+ List.of(5, 4, 3, 2), Comparator.reverseOrder(), 2, null,+ Integer::valueOf, String::valueOf);+ assertEquals(List.of(5, 4), first.items());+ assertEquals("4", first.nextToken());++ CodeBuildPagination.Page<Integer> second = CodeBuildPagination.page(+ List.of(5, 3, 2), Comparator.reverseOrder(), 2, first.nextToken(),+ Integer::valueOf, String::valueOf);+ assertEquals(List.of(3, 2), second.items());+ assertNull(second.nextToken());+ }+@Testvoid retryBuildRetainsOriginalBuildspecOverride() {CodeBuildRunner runner = mock(CodeBuildRunner.class);