mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(apigateway): honor limit and position on GetRequestValidators (#9060)
This commit is contained in:
1 parent
d37698ea71
commit
41aa3ba2eb
5 files changed
+129
-45
No files matched your search
+4
-18
@@ -12,13 +12,11 @@ import software.amazon.awssdk.services.apigateway.model.BadRequestException;
|
||||
import software.amazon.awssdk.services.apigateway.model.GetAuthorizersResponse;
|
||||
import software.amazon.awssdk.services.apigateway.model.NotFoundException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
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;
|
||||
@@ -56,22 +54,10 @@ class ApiGatewayAuthorizerPagingIntegrationTest {
|
||||
assertEquals(1, first.items().size());
|
||||
assertNotNull(first.position());
|
||||
|
||||
List<String> seen = new ArrayList<>();
|
||||
List<String> cursors = new ArrayList<>();
|
||||
seen.add(first.items().get(0).id());
|
||||
String position = first.position();
|
||||
for (int page = 0; position != null && page < created.size(); page++) {
|
||||
assertFalse(cursors.contains(position), "position repeated: " + position);
|
||||
cursors.add(position);
|
||||
String next = position;
|
||||
GetAuthorizersResponse response = client.getAuthorizers(request -> request.restApiId(apiId)
|
||||
.limit(1).position(next));
|
||||
assertEquals(1, response.items().size());
|
||||
assertFalse(seen.contains(response.items().get(0).id()), "authorizer repeated");
|
||||
seen.add(response.items().get(0).id());
|
||||
position = response.position();
|
||||
}
|
||||
assertNull(position);
|
||||
List<String> seen = ApiGatewayPagingWalk.oneItemPerPageIds(
|
||||
position -> client.getAuthorizers(
|
||||
request -> request.restApiId(apiId).limit(1).position(position)),
|
||||
GetAuthorizersResponse::items, GetAuthorizersResponse::position, Authorizer::id);
|
||||
assertEquals(3, seen.size());
|
||||
assertEquals(created, new HashSet<>(seen));
|
||||
} finally {
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package io.floci.aws.services.apigateway;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
final class ApiGatewayPagingWalk {
|
||||
|
||||
private ApiGatewayPagingWalk() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Follows {@code position} from the first page to the last, one item per page, and returns the ids
|
||||
* seen in order. Fails if an id or a position repeats.
|
||||
*/
|
||||
static <P, I> List<String> oneItemPerPageIds(Function<String, P> fetchPage, Function<P, List<I>> itemsOf,
|
||||
Function<P, String> positionOf, Function<I, String> idOf) {
|
||||
List<String> seen = new ArrayList<>();
|
||||
List<String> positions = new ArrayList<>();
|
||||
String position = null;
|
||||
do {
|
||||
P page = fetchPage.apply(position);
|
||||
List<I> items = itemsOf.apply(page);
|
||||
assertEquals(1, items.size());
|
||||
String id = idOf.apply(items.get(0));
|
||||
assertFalse(seen.contains(id), "item repeated: " + id);
|
||||
seen.add(id);
|
||||
position = positionOf.apply(page);
|
||||
if (position != null) {
|
||||
assertFalse(positions.contains(position), "position repeated: " + position);
|
||||
positions.add(position);
|
||||
}
|
||||
} while (position != null);
|
||||
return seen;
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package io.floci.aws.services.apigateway;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.http.apache5.Apache5HttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.apigateway.ApiGatewayClient;
|
||||
import software.amazon.awssdk.services.apigateway.model.BadRequestException;
|
||||
import software.amazon.awssdk.services.apigateway.model.GetRequestValidatorsResponse;
|
||||
import software.amazon.awssdk.services.apigateway.model.RequestValidator;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
class ApiGatewayRequestValidatorPagingIntegrationTest {
|
||||
|
||||
private ApiGatewayClient client() {
|
||||
return AwsTestClients.create(ApiGatewayClient.builder(), "test", Region.US_EAST_1,
|
||||
Apache5HttpClient.builder());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRequestValidatorsWithLimitFollowsPositionToTerminalPage() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
String apiId = client.createRestApi(request -> request.name("validator-paging-sdk")).id();
|
||||
try {
|
||||
Set<String> created = new HashSet<>();
|
||||
for (int i = 0; i < 3; i++) {
|
||||
String name = "Validator" + i;
|
||||
boolean body = i != 0;
|
||||
created.add(client.createRequestValidator(request -> request.restApiId(apiId).name(name)
|
||||
.validateRequestBody(body).validateRequestParameters(!body)).id());
|
||||
}
|
||||
|
||||
List<String> seen = ApiGatewayPagingWalk.oneItemPerPageIds(
|
||||
position -> client.getRequestValidators(
|
||||
request -> request.restApiId(apiId).limit(1).position(position)),
|
||||
GetRequestValidatorsResponse::items, GetRequestValidatorsResponse::position,
|
||||
RequestValidator::id);
|
||||
assertEquals(created, new HashSet<>(seen));
|
||||
|
||||
GetRequestValidatorsResponse all = client.getRequestValidators(request -> request.restApiId(apiId));
|
||||
assertEquals(3, all.items().size());
|
||||
assertNull(all.position());
|
||||
|
||||
assertThrows(BadRequestException.class, () -> client.getRequestValidators(
|
||||
request -> request.restApiId(apiId).limit(1).position("missing")));
|
||||
} finally {
|
||||
client.deleteRestApi(request -> request.restApiId(apiId));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user