mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(codebuild): paginate ListReportGroups responses (#6524)
* fix(codebuild): paginate ListReportGroups responses * fix(codebuild): snapshot report group pagination keys * refactor(codebuild): share listing pagination and snapshot project keys
This commit is contained in:
1 parent
3389621dc6
commit
28d934fd8e
5 files changed
+430
-86
No files matched your search
+49
@@ -0,0 +1,49 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.codebuild.CodeBuildClient;
|
||||
import software.amazon.awssdk.services.codebuild.model.ListReportGroupsResponse;
|
||||
import software.amazon.awssdk.services.codebuild.model.ReportExportConfigType;
|
||||
import software.amazon.awssdk.services.codebuild.model.ReportGroupSortByType;
|
||||
import software.amazon.awssdk.services.codebuild.model.ReportType;
|
||||
import software.amazon.awssdk.services.codebuild.model.SortOrderType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class CodeBuildReportGroupPaginationTest {
|
||||
|
||||
@Test
|
||||
void paginatorReturnsEveryReportGroupOnce() {
|
||||
String prefix = "sdk-page-" + UUID.randomUUID() + "-";
|
||||
List<String> created = new ArrayList<>();
|
||||
try (CodeBuildClient client = TestFixtures.codeBuildClient()) {
|
||||
try {
|
||||
for (String suffix : List.of("a", "b", "c")) {
|
||||
created.add(client.createReportGroup(request -> request.name(prefix + suffix)
|
||||
.type(ReportType.TEST)
|
||||
.exportConfig(config -> config.exportConfigType(ReportExportConfigType.NO_EXPORT)))
|
||||
.reportGroup().arn());
|
||||
}
|
||||
List<String> seen = new ArrayList<>();
|
||||
int pages = 0;
|
||||
for (ListReportGroupsResponse page : client.listReportGroupsPaginator(request -> request
|
||||
.maxResults(1).sortBy(ReportGroupSortByType.NAME).sortOrder(SortOrderType.ASCENDING))) {
|
||||
assertThat(page.reportGroups()).hasSizeLessThanOrEqualTo(1);
|
||||
seen.addAll(page.reportGroups());
|
||||
assertThat(++pages).as("pagination must end").isLessThan(1000);
|
||||
}
|
||||
assertThat(seen).doesNotHaveDuplicates();
|
||||
assertThat(seen.stream().filter(arn -> arn.contains(prefix)).toList())
|
||||
.containsExactlyElementsOf(created);
|
||||
} finally {
|
||||
for (String arn : created) {
|
||||
client.deleteReportGroup(request -> request.arn(arn));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+20
-3
@@ -4,6 +4,7 @@ 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.ProjectPage;
|
||||
import io.github.hectorvent.floci.services.codebuild.CodeBuildService.ReportGroupPage;
|
||||
import io.github.hectorvent.floci.services.codebuild.model.Build;
|
||||
import io.github.hectorvent.floci.services.codebuild.model.Project;
|
||||
import io.github.hectorvent.floci.services.codebuild.model.ProjectArtifacts;
|
||||
@@ -44,7 +45,7 @@ public class CodeBuildJsonHandler {
|
||||
case "UpdateReportGroup" -> updateReportGroup(request, region);
|
||||
case "DeleteReportGroup" -> deleteReportGroup(request, region);
|
||||
case "BatchGetReportGroups" -> batchGetReportGroups(request, region);
|
||||
case "ListReportGroups" -> listReportGroups(region);
|
||||
case "ListReportGroups" -> listReportGroups(request, region);
|
||||
case "ImportSourceCredentials" -> importSourceCredentials(request, region, account);
|
||||
case "ListSourceCredentials" -> listSourceCredentials(region);
|
||||
case "DeleteSourceCredentials" -> deleteSourceCredentials(request, region);
|
||||
@@ -179,8 +180,24 @@ public class CodeBuildJsonHandler {
|
||||
return Response.ok(Map.of("reportGroups", found, "reportGroupsNotFound", notFound)).build();
|
||||
}
|
||||
|
||||
private Response listReportGroups(String region) {
|
||||
return Response.ok(Map.of("reportGroups", service.listReportGroups(region))).build();
|
||||
private Response listReportGroups(JsonNode req, String region) {
|
||||
JsonNode sizeNode = req.get("maxResults");
|
||||
if (sizeNode != null && (!sizeNode.isIntegralNumber() || !sizeNode.canConvertToInt())) {
|
||||
throw new AwsException("InvalidInputException", "Invalid maxResults", 400);
|
||||
}
|
||||
JsonNode tokenNode = req.get("nextToken");
|
||||
if (tokenNode != null && !tokenNode.isTextual()) {
|
||||
throw new AwsException("InvalidInputException", "Invalid nextToken", 400);
|
||||
}
|
||||
ReportGroupPage page = service.listReportGroupsPage(region,
|
||||
req.path("sortBy").asText(null), req.path("sortOrder").asText(null),
|
||||
sizeNode == null ? 100 : sizeNode.intValue(), tokenNode == null ? null : tokenNode.asText());
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("reportGroups", page.reportGroups());
|
||||
if (page.nextToken() != null) {
|
||||
result.put("nextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(result).build();
|
||||
}
|
||||
|
||||
private Response importSourceCredentials(JsonNode req, String region, String account) {
|
||||
|
||||
+120
-83
@@ -264,8 +264,9 @@ public class CodeBuildService {
|
||||
if (sortBy == null && sortOrder == null) {
|
||||
return listProjects(region);
|
||||
}
|
||||
return listProjectRecords(region, sortBy, sortOrder).stream()
|
||||
.map(Project::getName)
|
||||
return projectSortKeys(region).stream()
|
||||
.sorted(listComparator(sortBy, sortOrder))
|
||||
.map(ListSortKey::name)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@@ -277,96 +278,21 @@ public class CodeBuildService {
|
||||
}
|
||||
}
|
||||
|
||||
List<Project> projects = listProjectRecords(region, sortBy, sortOrder);
|
||||
int start = 0;
|
||||
if (nextToken != null) {
|
||||
Project anchor = decodeListProjectsToken(nextToken, region, sortBy, sortOrder);
|
||||
Comparator<Project> comparator = projectComparator(sortBy, sortOrder);
|
||||
while (start < projects.size() && comparator.compare(projects.get(start), anchor) <= 0) {
|
||||
start++;
|
||||
}
|
||||
}
|
||||
|
||||
int end = Math.min(start + LIST_PROJECTS_PAGE_SIZE, projects.size());
|
||||
List<String> names = projects.subList(start, end).stream().map(Project::getName).toList();
|
||||
String resultToken = end < projects.size()
|
||||
? encodeListProjectsToken(projects.get(end - 1), region, sortBy, sortOrder)
|
||||
: null;
|
||||
return new ProjectPage(names, resultToken);
|
||||
}
|
||||
|
||||
private Project decodeListProjectsToken(String token, String region, String sortBy, String sortOrder) {
|
||||
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 lastName = cursor == null ? null : cursor.path("lastName");
|
||||
JsonNode sortValue = cursor == null ? null : cursor.path("sortValue");
|
||||
if (cursor == null || !"ListProjects".equals(cursor.path("action").asText(null))
|
||||
|| !region.equals(cursor.path("region").asText(null))
|
||||
|| !cursor.has("sortBy") || !Objects.equals(sortBy, cursor.path("sortBy").asText(null))
|
||||
|| !cursor.has("sortOrder") || !Objects.equals(sortOrder, cursor.path("sortOrder").asText(null))
|
||||
|| !lastName.isTextual() || lastName.asText().isEmpty()
|
||||
|| (sortBy == null || "NAME".equals(sortBy)) && !sortValue.isNull()
|
||||
|| !sortValue.isNull() && !sortValue.isNumber()) {
|
||||
throw new AwsException("InvalidInputException", "Invalid nextToken", 400);
|
||||
}
|
||||
Double value = sortValue.isNull() ? null : sortValue.asDouble();
|
||||
if (value != null && !Double.isFinite(value)) {
|
||||
throw new AwsException("InvalidInputException", "Invalid nextToken", 400);
|
||||
}
|
||||
Project anchor = new Project();
|
||||
anchor.setName(lastName.asText());
|
||||
anchor.setCreated(value);
|
||||
anchor.setLastModified(value);
|
||||
return anchor;
|
||||
}
|
||||
|
||||
private String encodeListProjectsToken(Project last, String region, String sortBy, String sortOrder) {
|
||||
Double value = switch (sortBy == null ? "NAME" : sortBy) {
|
||||
case "CREATED_TIME" -> last.getCreated();
|
||||
case "LAST_MODIFIED_TIME" -> last.getLastModified();
|
||||
default -> null;
|
||||
};
|
||||
ObjectNode cursor = PAGE_TOKEN_MAPPER.createObjectNode();
|
||||
cursor.put("action", "ListProjects");
|
||||
cursor.put("region", region);
|
||||
cursor.put("sortBy", sortBy);
|
||||
cursor.put("sortOrder", sortOrder);
|
||||
cursor.put("lastName", last.getName());
|
||||
cursor.put("sortValue", value);
|
||||
return Base64Url.encode(cursor.toString());
|
||||
SortedListPage 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());
|
||||
}
|
||||
|
||||
public record ProjectPage(List<String> projects, String nextToken) {
|
||||
}
|
||||
|
||||
private List<Project> listProjectRecords(String region, String sortBy, String sortOrder) {
|
||||
private List<ListSortKey> projectSortKeys(String region) {
|
||||
return projectsFor(region).values().stream()
|
||||
.sorted(projectComparator(sortBy, sortOrder))
|
||||
.map(project -> new ListSortKey(project.getCreated(), project.getLastModified(),
|
||||
project.getName(), project.getName()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private Comparator<Project> projectComparator(String sortBy, String sortOrder) {
|
||||
Comparator<Project> comparator = switch (sortBy == null ? "NAME" : sortBy) {
|
||||
case "NAME" -> Comparator.comparing(Project::getName);
|
||||
case "CREATED_TIME" -> Comparator.comparing(Project::getCreated,
|
||||
Comparator.nullsFirst(Comparator.naturalOrder()));
|
||||
case "LAST_MODIFIED_TIME" -> Comparator.comparing(Project::getLastModified,
|
||||
Comparator.nullsFirst(Comparator.naturalOrder()));
|
||||
default -> throw new AwsException("InvalidInputException", "Invalid sortBy: " + sortBy, 400);
|
||||
};
|
||||
if (isDescendingSortOrder(sortOrder, false)) {
|
||||
comparator = comparator.reversed();
|
||||
}
|
||||
return comparator.thenComparing(Project::getName);
|
||||
}
|
||||
|
||||
// ---- Report Groups ----
|
||||
|
||||
public ReportGroup createReportGroup(String region, String account,
|
||||
@@ -437,6 +363,117 @@ public class CodeBuildService {
|
||||
return new ArrayList<>(reportGroupsFor(region).keySet());
|
||||
}
|
||||
|
||||
public ReportGroupPage listReportGroupsPage(String region, String sortBy, String sortOrder,
|
||||
int maxResults, String nextToken) {
|
||||
if (maxResults < 1 || maxResults > 100) {
|
||||
throw new AwsException("InvalidInputException", "maxResults must be between 1 and 100", 400);
|
||||
}
|
||||
String criterion = sortBy == null ? "NAME" : sortBy;
|
||||
String order = sortOrder == null ? "ASCENDING" : sortOrder;
|
||||
List<ListSortKey> keys = reportGroupsFor(region).values().stream()
|
||||
.map(group -> new ListSortKey(group.getCreated(), group.getLastModified(),
|
||||
group.getName(), group.getArn()))
|
||||
.toList();
|
||||
SortedListPage page = paginateList(keys, region, criterion, order, maxResults, nextToken,
|
||||
"ListReportGroups", "lastArn");
|
||||
return new ReportGroupPage(page.keys().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) {
|
||||
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);
|
||||
}
|
||||
|
||||
private Comparator<ListSortKey> listComparator(String sortBy, String sortOrder) {
|
||||
Comparator<ListSortKey> comparator = switch (sortBy == null ? "NAME" : sortBy) {
|
||||
case "NAME" -> Comparator.comparing(ListSortKey::name);
|
||||
case "CREATED_TIME" -> Comparator.comparing(ListSortKey::created,
|
||||
Comparator.nullsFirst(Comparator.naturalOrder()));
|
||||
case "LAST_MODIFIED_TIME" -> Comparator.comparing(ListSortKey::lastModified,
|
||||
Comparator.nullsFirst(Comparator.naturalOrder()));
|
||||
default -> throw new AwsException("InvalidInputException", "Invalid sortBy: " + sortBy, 400);
|
||||
};
|
||||
if (isDescendingSortOrder(sortOrder, false)) {
|
||||
comparator = comparator.reversed();
|
||||
}
|
||||
return comparator.thenComparing(ListSortKey::name).thenComparing(ListSortKey::id);
|
||||
}
|
||||
|
||||
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 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);
|
||||
if (cursor == null || !action.equals(cursor.path("action").asText(null))
|
||||
|| !region.equals(cursor.path("region").asText(null))
|
||||
|| !cursor.has("sortBy") || !Objects.equals(sortBy, cursor.path("sortBy").asText(null))
|
||||
|| !cursor.has("sortOrder") || !Objects.equals(sortOrder, cursor.path("sortOrder").asText(null))
|
||||
|| !lastName.isTextual() || lastName.asText().isEmpty()
|
||||
|| idField != null && (!lastId.isTextual() || lastId.asText().isEmpty())
|
||||
|| (sortBy == null || "NAME".equals(sortBy)) && !sortValue.isNull()
|
||||
|| !sortValue.isNull() && !sortValue.isNumber()) {
|
||||
throw new AwsException("InvalidInputException", "Invalid nextToken", 400);
|
||||
}
|
||||
Double value = sortValue.isNull() ? null : sortValue.asDouble();
|
||||
if (value != null && !Double.isFinite(value)) {
|
||||
throw new AwsException("InvalidInputException", "Invalid nextToken", 400);
|
||||
}
|
||||
String name = lastName.asText();
|
||||
return new ListSortKey(value, value, name, idField == null ? name : lastId.asText());
|
||||
}
|
||||
|
||||
private String encodeListToken(ListSortKey last, String region, String sortBy,
|
||||
String sortOrder, String action, String idField) {
|
||||
Double value = switch (sortBy == null ? "NAME" : sortBy) {
|
||||
case "CREATED_TIME" -> last.created();
|
||||
case "LAST_MODIFIED_TIME" -> last.lastModified();
|
||||
default -> null;
|
||||
};
|
||||
ObjectNode cursor = PAGE_TOKEN_MAPPER.createObjectNode();
|
||||
cursor.put("action", action);
|
||||
cursor.put("region", region);
|
||||
cursor.put("sortBy", sortBy);
|
||||
cursor.put("sortOrder", sortOrder);
|
||||
if (idField != null) {
|
||||
cursor.put(idField, last.id());
|
||||
}
|
||||
cursor.put("lastName", last.name());
|
||||
cursor.put("sortValue", value);
|
||||
return Base64Url.encode(cursor.toString());
|
||||
}
|
||||
|
||||
public record ReportGroupPage(List<String> reportGroups, String nextToken) {
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
+37
@@ -15,6 +15,7 @@ import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
@@ -27,6 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class CodeBuildListProjectsTest {
|
||||
@@ -200,6 +202,41 @@ class CodeBuildListProjectsTest {
|
||||
assertFalse(nextPage.containsKey("nextToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void keepsCursorSortValueWhenProjectChangesDuringPageConstruction() throws Exception {
|
||||
IntStream.range(0, 101).forEach(index -> createProject(REGION,
|
||||
"project-%03d".formatted(index), (double) index, (double) index));
|
||||
Project changing = new Project() {
|
||||
private boolean changed;
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
if (!changed) {
|
||||
setLastModified(1000.0);
|
||||
changed = true;
|
||||
}
|
||||
return super.getName();
|
||||
}
|
||||
};
|
||||
changing.setName("project-099");
|
||||
changing.setCreated(99.0);
|
||||
changing.setLastModified(99.0);
|
||||
Field field = CodeBuildService.class.getDeclaredField("projects");
|
||||
field.setAccessible(true);
|
||||
Map<String, Map<String, Project>> regions =
|
||||
(Map<String, Map<String, Project>>) field.get(service);
|
||||
regions.get(REGION).put("project-099", changing);
|
||||
|
||||
Map<String, Object> first = listPage(Map.of("sortBy", "LAST_MODIFIED_TIME"));
|
||||
String token = (String) first.get("nextToken");
|
||||
assertNotNull(token);
|
||||
assertEquals(99.0, mapper.readTree(Base64.getUrlDecoder().decode(token))
|
||||
.path("sortValue").asDouble());
|
||||
Map<String, Object> second = listPage(Map.of("sortBy", "LAST_MODIFIED_TIME", "nextToken", token));
|
||||
assertTrue(((List<String>) second.get("projects")).contains("project-100"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void assertPagesMatchServiceOrder(Map<String, String> options, List<String> expected) throws Exception {
|
||||
Map<String, Object> first = listPage(options);
|
||||
|
||||
+204
@@ -0,0 +1,204 @@
|
||||
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.core.common.Base64Url;
|
||||
import io.github.hectorvent.floci.services.codebuild.model.ReportGroup;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
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 CodeBuildListReportGroupsServiceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
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);
|
||||
|
||||
@Test
|
||||
void returnsRequestedPageAndContinuationToken() throws Exception {
|
||||
ReportGroup first = createGroup("alpha");
|
||||
ReportGroup second = createGroup("beta");
|
||||
|
||||
JsonNode page = list(Map.of("maxResults", 1));
|
||||
assertEquals(List.of(first.getArn()), arns(page));
|
||||
assertTrue(page.path("nextToken").isTextual());
|
||||
|
||||
JsonNode last = list(Map.of("maxResults", 1, "nextToken", page.path("nextToken").asText()));
|
||||
assertEquals(List.of(second.getArn()), arns(last));
|
||||
assertFalse(last.has("nextToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultsToOneHundredAndAllowsChangingPageSize() throws Exception {
|
||||
List<String> expected = IntStream.range(0, 103)
|
||||
.mapToObj(index -> createGroup("group-%03d".formatted(index)).getArn()).toList();
|
||||
JsonNode first = list(Map.of());
|
||||
assertEquals(expected.subList(0, 100), arns(first));
|
||||
JsonNode second = list(Map.of("maxResults", 2, "nextToken", first.path("nextToken").asText()));
|
||||
assertEquals(expected.subList(100, 102), arns(second));
|
||||
JsonNode last = list(Map.of("maxResults", 100, "nextToken", second.path("nextToken").asText()));
|
||||
assertEquals(expected.subList(102, 103), arns(last));
|
||||
assertFalse(last.has("nextToken"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"NAME, ASCENDING, alpha:mu:zeta",
|
||||
"NAME, DESCENDING, zeta:mu:alpha",
|
||||
"CREATED_TIME, ASCENDING, zeta:alpha:mu",
|
||||
"CREATED_TIME, DESCENDING, mu:alpha:zeta",
|
||||
"LAST_MODIFIED_TIME, ASCENDING, mu:zeta:alpha",
|
||||
"LAST_MODIFIED_TIME, DESCENDING, alpha:zeta:mu"
|
||||
})
|
||||
void continuesInRequestedSortOrder(String sortBy, String sortOrder, String expected) throws Exception {
|
||||
ReportGroup zeta = createGroup("zeta");
|
||||
zeta.setCreated(1.0);
|
||||
zeta.setLastModified(2.0);
|
||||
ReportGroup alpha = createGroup("alpha");
|
||||
alpha.setCreated(2.0);
|
||||
alpha.setLastModified(3.0);
|
||||
ReportGroup mu = createGroup("mu");
|
||||
mu.setCreated(3.0);
|
||||
mu.setLastModified(1.0);
|
||||
service.createReportGroup("eu-west-1", ACCOUNT, "elsewhere", "TEST", Map.of(), null);
|
||||
|
||||
JsonNode first = list(Map.of("maxResults", 2, "sortBy", sortBy, "sortOrder", sortOrder));
|
||||
JsonNode last = list(Map.of("maxResults", 2, "sortBy", sortBy, "sortOrder", sortOrder,
|
||||
"nextToken", first.path("nextToken").asText()));
|
||||
List<String> seen = new ArrayList<>(arns(first));
|
||||
seen.addAll(arns(last));
|
||||
assertEquals(List.of(expected.split(":")), seen.stream()
|
||||
.map(arn -> arn.substring(arn.lastIndexOf('/') + 1)).toList());
|
||||
assertFalse(last.has("nextToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void continuesPastDeletedAnchorAndTiedTimestamps() throws Exception {
|
||||
ReportGroup alpha = createGroup("alpha");
|
||||
ReportGroup beta = createGroup("beta");
|
||||
ReportGroup gamma = createGroup("gamma");
|
||||
for (ReportGroup group : List.of(alpha, beta, gamma)) {
|
||||
group.setCreated(1.0);
|
||||
}
|
||||
JsonNode first = list(Map.of("maxResults", 1, "sortBy", "CREATED_TIME"));
|
||||
assertEquals(List.of(alpha.getArn()), arns(first));
|
||||
service.deleteReportGroup(REGION, alpha.getArn());
|
||||
JsonNode last = list(Map.of("maxResults", 2, "sortBy", "CREATED_TIME",
|
||||
"nextToken", first.path("nextToken").asText()));
|
||||
assertEquals(List.of(beta.getArn(), gamma.getArn()), arns(last));
|
||||
assertFalse(last.has("nextToken"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void keepsCursorSortValueWhenReportGroupChangesDuringPageConstruction() throws Exception {
|
||||
ReportGroup alpha = createGroup("alpha");
|
||||
ReportGroup beta = createGroup("beta");
|
||||
beta.setLastModified(2.0);
|
||||
ReportGroup changing = new ReportGroup() {
|
||||
private boolean changed;
|
||||
|
||||
@Override
|
||||
public String getArn() {
|
||||
if (!changed) {
|
||||
setLastModified(99.0);
|
||||
changed = true;
|
||||
}
|
||||
return super.getArn();
|
||||
}
|
||||
};
|
||||
changing.setArn(alpha.getArn());
|
||||
changing.setName(alpha.getName());
|
||||
changing.setLastModified(1.0);
|
||||
Field field = CodeBuildService.class.getDeclaredField("reportGroups");
|
||||
field.setAccessible(true);
|
||||
Map<String, Map<String, ReportGroup>> regions =
|
||||
(Map<String, Map<String, ReportGroup>>) field.get(service);
|
||||
regions.get(REGION).put(alpha.getArn(), changing);
|
||||
|
||||
JsonNode first = list(Map.of("maxResults", 1, "sortBy", "LAST_MODIFIED_TIME"));
|
||||
assertEquals(List.of(alpha.getArn()), arns(first));
|
||||
JsonNode second = list(Map.of("maxResults", 1, "sortBy", "LAST_MODIFIED_TIME",
|
||||
"nextToken", first.path("nextToken").asText()));
|
||||
assertEquals(List.of(beta.getArn()), arns(second));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyRegionHasNoContinuationToken() throws Exception {
|
||||
JsonNode response = list(Map.of());
|
||||
assertEquals(List.of(), arns(response));
|
||||
assertFalse(response.has("nextToken"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"0", "-1", "101", "1.5", "2147483648", "true", "\"1\"", "null"})
|
||||
void rejectsInvalidPageSizes(String value) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> handler.handle("ListReportGroups",
|
||||
mapper.readTree("{\"maxResults\":" + value + "}"), REGION, ACCOUNT));
|
||||
assertEquals("InvalidInputException", error.getErrorCode());
|
||||
assertEquals(400, error.getHttpStatus());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"sortBy, BAD", "sortOrder, BAD"})
|
||||
void rejectsUnknownSortOptionsOnEmptyRegion(String key, String value) {
|
||||
assertThrows(AwsException.class, () -> list(Map.of(key, value)));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", "not-a-token!", "e30", "bnVsbA", "MQ"})
|
||||
void rejectsMalformedTokens(String token) {
|
||||
AwsException error = assertThrows(AwsException.class, () -> list(Map.of("nextToken", token)));
|
||||
assertEquals("InvalidInputException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsTokensForOtherRegionsOperationsAndSorts() throws Exception {
|
||||
createGroup("alpha");
|
||||
createGroup("beta");
|
||||
String token = list(Map.of("maxResults", 1)).path("nextToken").asText();
|
||||
assertThrows(AwsException.class, () -> handler.handle("ListReportGroups",
|
||||
mapper.valueToTree(Map.of("nextToken", token)), "eu-west-1", ACCOUNT));
|
||||
assertThrows(AwsException.class, () -> handler.handle("ListProjects",
|
||||
mapper.valueToTree(Map.of("nextToken", token)), REGION, ACCOUNT));
|
||||
assertThrows(AwsException.class, () -> list(Map.of("nextToken", token, "sortBy", "CREATED_TIME")));
|
||||
assertThrows(AwsException.class, () -> list(Map.of("nextToken", token, "sortOrder", "DESCENDING")));
|
||||
assertThrows(AwsException.class, () -> list(Map.of("nextToken", 42)));
|
||||
assertThrows(AwsException.class, () -> list(Map.of("nextToken", Base64Url.encode("[]"))));
|
||||
}
|
||||
|
||||
private ReportGroup createGroup(String name) {
|
||||
return service.createReportGroup(REGION, ACCOUNT, name, "TEST",
|
||||
Map.of("exportConfigType", "NO_EXPORT"), null);
|
||||
}
|
||||
|
||||
private JsonNode list(Map<String, ?> request) throws Exception {
|
||||
try (Response response = handler.handle("ListReportGroups", mapper.valueToTree(request), REGION, ACCOUNT)) {
|
||||
return mapper.valueToTree(response.getEntity());
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> arns(JsonNode response) {
|
||||
return mapper.convertValue(response.path("reportGroups"),
|
||||
mapper.getTypeFactory().constructCollectionType(List.class, String.class));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user