Light, fluffy, and always free - The AWS Local Emulator alternative
feat(redshift): implement zero-ETL integrations (#3559)
Ovidiu (Ovi)d3bfe2609c08
diff --git a/docs/services/redshift.md b/docs/services/redshift.mdindex 7416e14803..9e7b94c999 100644--- a/docs/services/redshift.md+++ b/docs/services/redshift.md@@ -33,6 +33,9 @@ For running SQL without a PostgreSQL wire connection (the way Lambda and Step Fu| `DeleteTags` | Remove tags by key from a resource || `DescribeTags` | List tagged resources and their tags || `CreateClusterSubnetGroup` | Register a cluster subnet group (metadata only) |+| `CreateIntegration` | Register a zero-ETL integration (metadata only; no data is replicated). Accepts `Description`, `KMSKeyId`, `AdditionalEncryptionContext` and `TagList` |+| `DescribeIntegrations` | List integrations with `Filters`, `MaxRecords` and `Marker` pagination, or the one an `IntegrationArn` names |+| `DeleteIntegration` | Remove a zero-ETL integration || `DescribeClusterSubnetGroups` | List subnet groups, optionally filtered by name || `ModifyClusterSubnetGroup` | Update a subnet group's description or subnet list || `DeleteClusterSubnetGroup` | Remove a subnet group |diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.javaindex 3e236a8fdd..91a2f6f5e9 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftQueryHandler.java@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.core.common.XmlBuilder;import io.github.hectorvent.floci.services.redshift.model.Cluster;import io.github.hectorvent.floci.services.redshift.model.ClusterParameterGroup;import io.github.hectorvent.floci.services.redshift.model.ClusterSubnetGroup;+import io.github.hectorvent.floci.services.redshift.model.Integration;import io.github.hectorvent.floci.services.redshift.model.Parameter;import io.github.hectorvent.floci.services.redshift.model.Snapshot;import jakarta.enterprise.context.ApplicationScoped;@@ -29,6 +30,9 @@ public class RedshiftQueryHandler {private static final Logger LOG = Logger.getLogger(RedshiftQueryHandler.class);+ private static final Pattern FILTER_NAME =+ Pattern.compile("Filters\\.DescribeIntegrationsFilter\\.(\\d+)\\.Name");+// GetClusterCredentials DurationSeconds bounds, inclusive (AWS: 900 to 3600).private static final int MIN_CREDENTIAL_DURATION_SECONDS = 900;private static final int MAX_CREDENTIAL_DURATION_SECONDS = 3600;@@ -348,6 +352,70 @@ public class RedshiftQueryHandler {.build();return Response.ok(xml).type(MediaType.APPLICATION_XML).build();}+ case "CreateIntegration" -> {+ Integration integration = service.createIntegration(+ params.getFirst("IntegrationName"),+ params.getFirst("SourceArn"),+ params.getFirst("TargetArn"),+ params.getFirst("KMSKeyId"),+ params.getFirst("Description"),+ encryptionContextMap(params),+ tagMap(params),+ regionResolver.resolveRegionFromAuth(authorizationHeader));+ String xml = new XmlBuilder()+ .start("CreateIntegrationResponse")+ .start("CreateIntegrationResult")+ .raw(buildIntegrationXml(integration, false))+ .end("CreateIntegrationResult")+ .start("ResponseMetadata")+ .elem("RequestId", "test-req-id")+ .end("ResponseMetadata")+ .end("CreateIntegrationResponse")+ .build();+ return Response.ok(xml).type(MediaType.APPLICATION_XML).build();+ }+ case "DescribeIntegrations" -> {+ RedshiftService.IntegrationPage found = service.describeIntegrations(+ params.getFirst("IntegrationArn"),+ intParam(params, "MaxRecords"),+ params.getFirst("Marker"),+ integrationFilters(params));+ XmlBuilder xmlBuilder = new XmlBuilder()+ .start("DescribeIntegrationsResponse")+ .start("DescribeIntegrationsResult")+ .start("Integrations");+ for (Integration integration : found.integrations()) {+ xmlBuilder.raw(buildIntegrationXml(integration, true));+ }+ xmlBuilder.end("Integrations");+ // Marker only when a further page exists: real Redshift omits it on the terminal page,+ // and an absent marker is what stops a caller's pagination loop.+ if (found.marker() != null) {+ xmlBuilder.elem("Marker", found.marker());+ }+ String xml = xmlBuilder+ .end("DescribeIntegrationsResult")+ .start("ResponseMetadata")+ .elem("RequestId", "test-req-id")+ .end("ResponseMetadata")+ .end("DescribeIntegrationsResponse")+ .build();+ return Response.ok(xml).type(MediaType.APPLICATION_XML).build();+ }+ case "DeleteIntegration" -> {+ Integration integration = service.deleteIntegration(params.getFirst("IntegrationArn"));+ String xml = new XmlBuilder()+ .start("DeleteIntegrationResponse")+ .start("DeleteIntegrationResult")+ .raw(buildIntegrationXml(integration, false))+ .end("DeleteIntegrationResult")+ .start("ResponseMetadata")+ .elem("RequestId", "test-req-id")+ .end("ResponseMetadata")+ .end("DeleteIntegrationResponse")+ .build();+ return Response.ok(xml).type(MediaType.APPLICATION_XML).build();+ }case "DescribeClusterSubnetGroups" -> {String name = params.getFirst("ClusterSubnetGroupName");List<ClusterSubnetGroup> groups = service.describeClusterSubnetGroups(name);@@ -649,6 +717,121 @@ public class RedshiftQueryHandler {return builder.end("Parameter").build();}++ /**+ * Renders one integration. {@code Errors} is emitted even when empty, which is what a live+ * integration returns, and {@code Status} stays lower case for the same reason.+ */+ private String buildIntegrationXml(Integration integration, boolean includeErrors) {+ XmlBuilder builder = new XmlBuilder()+ .start("Integration")+ .elem("IntegrationArn", integration.getIntegrationArn())+ .elem("IntegrationName", integration.getIntegrationName())+ .elem("SourceArn", integration.getSourceArn())+ .elem("TargetArn", integration.getTargetArn())+ .elem("Status", integration.getStatus())+ .elem("CreateTime", integration.getCreateTime());+ if (integration.getDescription() != null) {+ builder.elem("Description", integration.getDescription());+ }+ if (integration.getKmsKeyId() != null) {+ builder.elem("KMSKeyId", integration.getKmsKeyId());+ }+ if (integration.getAdditionalEncryptionContext() != null+ && !integration.getAdditionalEncryptionContext().isEmpty()) {+ builder.start("AdditionalEncryptionContext");+ for (Map.Entry<String, String> entry : integration.getAdditionalEncryptionContext().entrySet()) {+ builder.start("entry")+ .elem("key", entry.getKey())+ .elem("value", entry.getValue())+ .end("entry");+ }+ builder.end("AdditionalEncryptionContext");+ }+ if (includeErrors) {+ builder.start("Errors").end("Errors");+ }+ if (integration.getTags() != null && !integration.getTags().isEmpty()) {+ builder.start("Tags");+ for (Map.Entry<String, String> tag : integration.getTags().entrySet()) {+ builder.start("Tag")+ .elem("Key", tag.getKey())+ .elem("Value", tag.getValue())+ .end("Tag");+ }+ builder.end("Tags");+ }+ return builder.end("Integration").build();+ }++ /**+ * Reads the {@code TagList.Tag.N.Key} / {@code .Value} pairs of a Query request.+ *+ * <p>The member is {@code TagList}, not {@code Tags}: an SDK serialises the list under its own+ * member name, so reading {@code Tags.Tag.N} silently drops every tag a real client sends.+ */+ private static Map<String, String> tagMap(MultivaluedMap<String, String> params) {+ Map<String, String> tags = new LinkedHashMap<>();+ for (String key : params.keySet()) {+ if (key.matches("TagList\\.Tag\\.\\d+\\.Key")) {+ String value = params.getFirst(key.replaceAll("\\.Key$", ".Value"));+ String name = params.getFirst(key);+ if (name != null && !name.isBlank()) {+ tags.put(name, value == null ? "" : value);+ }+ }+ }+ return tags;+ }++ /** Reads an {@code AdditionalEncryptionContext.entry.N.key} / {@code .value} map. */+ private static Map<String, String> encryptionContextMap(MultivaluedMap<String, String> params) {+ Map<String, String> context = new LinkedHashMap<>();+ for (String key : params.keySet()) {+ if (key.matches("AdditionalEncryptionContext\\.entry\\.\\d+\\.key")) {+ String value = params.getFirst(key.replaceAll("\\.key$", ".value"));+ String name = params.getFirst(key);+ if (name != null && !name.isBlank()) {+ context.put(name, value == null ? "" : value);+ }+ }+ }+ return context;+ }++ /** Reads {@code Filters.DescribeIntegrationsFilter.N.Name} and its {@code Values.Value.M} list. */+ private static List<RedshiftService.IntegrationFilter> integrationFilters(MultivaluedMap<String, String> params) {+ Map<String, RedshiftService.IntegrationFilter> byIndex = new LinkedHashMap<>();+ for (String key : params.keySet()) {+ java.util.regex.Matcher matcher = FILTER_NAME.matcher(key);+ if (!matcher.matches()) {+ continue;+ }+ String index = matcher.group(1);+ String name = params.getFirst(key);+ List<String> values = params.keySet().stream()+ .filter(candidate -> candidate.matches(+ "Filters\\.DescribeIntegrationsFilter\\." + index + "\\.Values\\.Value\\.\\d+"))+ .sorted(Comparator.comparingInt(RedshiftQueryHandler::numericSuffix))+ .map(params::getFirst)+ .toList();+ byIndex.put(index, new RedshiftService.IntegrationFilter(name, values));+ }+ return List.copyOf(byIndex.values());+ }++ private static Integer intParam(MultivaluedMap<String, String> params, String name) {+ String raw = params.getFirst(name);+ if (raw == null || raw.isBlank()) {+ return null;+ }+ try {+ return Integer.valueOf(raw);+ } catch (NumberFormatException e) {+ throw new AwsException("InvalidParameterValue", name + " must be an integer.", 400);+ }+ }+private static List<String> memberList(MultivaluedMap<String, String> params, String baseName) {return params.keySet().stream().filter(key -> key.matches(memberKeyRegex(baseName)))diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.javaindex 570d101d80..5fdf85377f 100644--- a/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/RedshiftService.java@@ -11,6 +11,11 @@ import io.github.hectorvent.floci.services.redshift.proxy.RedshiftProxyManager;import io.github.hectorvent.floci.core.storage.StorageFactory;import io.github.hectorvent.floci.services.redshift.container.RedshiftContainerHandle;import io.github.hectorvent.floci.services.redshift.container.RedshiftContainerManager;+import io.github.hectorvent.floci.services.redshift.model.Integration;+import java.time.Instant;+import java.time.format.DateTimeFormatter;+import java.util.UUID;+import java.util.regex.Pattern;import io.github.hectorvent.floci.services.redshift.model.Cluster;import io.github.hectorvent.floci.services.redshift.model.ClusterParameterGroup;import io.github.hectorvent.floci.services.redshift.model.ClusterSubnetGroup;@@ -29,6 +34,7 @@ import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.util.ArrayList;+import java.util.Comparator;import java.util.Collection;import java.util.LinkedHashMap;import java.util.List;@@ -46,6 +52,16 @@ public class RedshiftService {private final AccountAwareStorageBackend<Snapshot> snapshots;private final AccountAwareStorageBackend<ClusterParameterGroup> parameterGroups;private final AccountAwareStorageBackend<ClusterSubnetGroup> subnetGroups;+ private static final int MIN_INTEGRATION_RECORDS = 20;+ private static final int MAX_INTEGRATION_RECORDS = 100;+ private static final int MAX_INTEGRATION_DESCRIPTION = 1000;+ private static final int MAX_INTEGRATION_NAME = 63;+ // Modelled constraint on CreateIntegration.IntegrationName: a letter first, then alphanumeric+ // groups joined by single hyphens. No leading digit, underscore, or trailing hyphen.+ private static final String INTEGRATION_NAME_PATTERN = "^[a-zA-Z][a-zA-Z0-9]*(-[a-zA-Z0-9]+)*$";+ private static final Pattern INTEGRATION_NAME = Pattern.compile(INTEGRATION_NAME_PATTERN);++ private final AccountAwareStorageBackend<Integration> integrations;private final RedshiftContainerManager containerManager;private final EmulatorConfig config;private final RegionResolver regionResolver;@@ -64,6 +80,7 @@ public class RedshiftService {this.snapshots = storageFactory.create("redshift", "redshift-snapshots.json", new TypeReference<Map<String, Snapshot>>() {});this.parameterGroups = storageFactory.create("redshift", "redshift-parameter-groups.json", new TypeReference<Map<String, ClusterParameterGroup>>() {});this.subnetGroups = storageFactory.create("redshift", "redshift-subnet-groups.json", new TypeReference<Map<String, ClusterSubnetGroup>>() {});+ this.integrations = storageFactory.create("redshift", "redshift-integrations.json", new TypeReference<Map<String, Integration>>() {});this.containerManager = containerManager;this.config = config;this.regionResolver = regionResolver;@@ -200,6 +217,173 @@ public class RedshiftService {return cluster;}+ // ── Zero-ETL integrations ────────────────────────────────────+ //+ // Metadata only: no data is replicated from the source. The shape and the lower case status+ // were captured from a live integration in us-west-2.++ public synchronized Integration createIntegration(String integrationName, String sourceArn, String targetArn,+ String kmsKeyId, String description,+ Map<String, String> additionalEncryptionContext,+ Map<String, String> tags, String region) {+ if (integrationName == null || integrationName.isBlank()) {+ throw new AwsException("InvalidParameterValue", "IntegrationName is required.", 400);+ }+ if (integrationName.length() > MAX_INTEGRATION_NAME+ || !INTEGRATION_NAME.matcher(integrationName).matches()) {+ throw new AwsException("InvalidParameterValue",+ "IntegrationName must match " + INTEGRATION_NAME_PATTERN+ + " and be at most " + MAX_INTEGRATION_NAME + " characters.", 400);+ }+ if (sourceArn == null || sourceArn.isBlank()) {+ throw new AwsException("InvalidParameterValue", "SourceArn is required.", 400);+ }+ if (targetArn == null || targetArn.isBlank()) {+ throw new AwsException("InvalidParameterValue", "TargetArn is required.", 400);+ }+ if (description != null && description.length() > MAX_INTEGRATION_DESCRIPTION) {+ throw new AwsException("InvalidParameterValue",+ "Description must be at most " + MAX_INTEGRATION_DESCRIPTION + " characters.", 400);+ }+ // AdditionalEncryptionContext only means anything alongside a customer managed key.+ if (additionalEncryptionContext != null && !additionalEncryptionContext.isEmpty()+ && (kmsKeyId == null || kmsKeyId.isBlank())) {+ throw new AwsException("InvalidParameterValue",+ "AdditionalEncryptionContext is only valid when KMSKeyId is supplied.", 400);+ }++ boolean nameTaken = integrations.scan(k -> true).stream()+ .anyMatch(existing -> integrationName.equals(existing.getIntegrationName()));+ if (nameTaken) {+ throw new AwsException("IntegrationAlreadyExistsFault",+ "The integration " + integrationName + " already exists.", 400);+ }++ String integrationId = UUID.randomUUID().toString();+ Integration integration = new Integration();+ integration.setIntegrationArn("arn:aws:redshift:" + region + ":" + regionResolver.getAccountId()+ + ":integration:" + integrationId);+ integration.setIntegrationName(integrationName);+ integration.setSourceArn(sourceArn);+ integration.setTargetArn(targetArn);+ // Real integrations pass through creating before settling; nothing here has work to do.+ integration.setStatus("active");+ integration.setKmsKeyId(kmsKeyId);+ integration.setCreateTime(DateTimeFormatter.ISO_INSTANT.format(Instant.now()));+ integration.setDescription(description);+ integration.setAdditionalEncryptionContext(additionalEncryptionContext);+ integration.setTags(tags);+ integrations.put(integrationId, integration);+ LOG.infov("Created Redshift zero-ETL integration: {0}", integration.getIntegrationArn());+ return integration;+ }++ /**+ * Lists integrations with the documented filters and marker pagination.+ *+ * <p>An unknown {@code IntegrationArn} is {@code IntegrationNotFoundFault}, measured against+ * real Redshift. An account with no integrations at all is an empty list rather than an error,+ * and no {@code Marker} is emitted on the terminal page.+ */+ public IntegrationPage describeIntegrations(String integrationArn, Integer maxRecords, String marker,+ List<IntegrationFilter> filters) {+ List<Integration> all = integrations.scan(k -> true).stream()+ .sorted(Comparator.comparing(Integration::getIntegrationArn))+ .toList();++ if (integrationArn != null && !integrationArn.isBlank()) {+ Integration match = all.stream()+ .filter(integration -> integrationArn.equals(integration.getIntegrationArn()))+ .findFirst()+ .orElseThrow(() -> new AwsException("IntegrationNotFoundFault",+ "The requested integration doesn't exist.", 404));+ all = List.of(match);+ }++ for (IntegrationFilter filter : filters == null ? List.<IntegrationFilter>of() : filters) {+ all = all.stream().filter(integration -> matchesFilter(integration, filter)).toList();+ }++ int pageSize = resolveMaxRecords(maxRecords);+ int from = 0;+ if (marker != null && !marker.isBlank()) {+ int previous = -1;+ for (int i = 0; i < all.size(); i++) {+ if (marker.equals(all.get(i).getIntegrationArn())) {+ previous = i;+ break;+ }+ }+ if (previous < 0) {+ throw new AwsException("InvalidParameterValue", "Invalid Marker specified.", 400);+ }+ from = previous + 1;+ }++ List<Integration> page = all.subList(Math.min(from, all.size()), Math.min(from + pageSize, all.size()));+ boolean more = from + pageSize < all.size();+ // The marker is the last ARN already returned, so a page resumes after a known record+ // rather than at an offset a concurrent create could shift.+ String next = more && !page.isEmpty() ? page.get(page.size() - 1).getIntegrationArn() : null;+ return new IntegrationPage(List.copyOf(page), next);+ }++ /** One page of integrations plus the marker to continue with, or {@code null} at the end. */+ public record IntegrationPage(List<Integration> integrations, String marker) {}++ /** One {@code Filters.DescribeIntegrationsFilter.N} entry. */+ public record IntegrationFilter(String name, List<String> values) {}++ private static boolean matchesFilter(Integration integration, IntegrationFilter filter) {+ String name = filter.name() == null ? "" : filter.name();+ List<String> values = filter.values() == null ? List.of() : filter.values();+ String actual = switch (name) {+ case "integration-arn" -> integration.getIntegrationArn();+ case "source-arn" -> integration.getSourceArn();+ case "status" -> integration.getStatus();+ // source-types filters on the source's AWS service, which the ARN's third field names.+ case "source-types" -> sourceType(integration.getSourceArn());+ default -> throw new AwsException("InvalidParameterValue",+ "Unrecognized filter name: " + name, 400);+ };+ return actual != null && values.contains(actual);+ }++ private static String sourceType(String sourceArn) {+ if (sourceArn == null) {+ return null;+ }+ String[] parts = sourceArn.split(":");+ return parts.length > 2 ? parts[2] : null;+ }++ private static int resolveMaxRecords(Integer maxRecords) {+ if (maxRecords == null) {+ return MAX_INTEGRATION_RECORDS;+ }+ if (maxRecords < MIN_INTEGRATION_RECORDS || maxRecords > MAX_INTEGRATION_RECORDS) {+ throw new AwsException("InvalidParameterValue",+ "MaxRecords must be between " + MIN_INTEGRATION_RECORDS + " and "+ + MAX_INTEGRATION_RECORDS + ".", 400);+ }+ return maxRecords;+ }++ public synchronized Integration deleteIntegration(String integrationArn) {+ if (integrationArn == null || integrationArn.isBlank()) {+ throw new AwsException("InvalidParameterValue", "IntegrationArn is required.", 400);+ }+ for (String key : integrations.keys()) {+ Optional<Integration> stored = integrations.get(key);+ if (stored.isPresent() && integrationArn.equals(stored.get().getIntegrationArn())) {+ integrations.delete(key);+ LOG.infov("Deleted Redshift zero-ETL integration: {0}", integrationArn);+ return stored.get();+ }+ }+ throw new AwsException("IntegrationNotFoundFault", "The requested integration doesn't exist.", 404);+ }+public List<Cluster> describeClusters(String identifier) {if (identifier != null) {Optional<Cluster> cluster = clusters.get(identifier);diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/model/Integration.java b/src/main/java/io/github/hectorvent/floci/services/redshift/model/Integration.javanew file mode 100644index 0000000000..6edf08a41c--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/redshift/model/Integration.java@@ -0,0 +1,58 @@+package io.github.hectorvent.floci.services.redshift.model;++import io.quarkus.runtime.annotations.RegisterForReflection;++import java.util.ArrayList;+import java.util.LinkedHashMap;+import java.util.List;+import java.util.Map;++/**+ * A zero-ETL integration between a source (for example a DynamoDB table) and a Redshift target.+ *+ * <p>Field set captured from a live integration in us-west-2. {@code status} is lower case there+ * ({@code active}), and {@code errors} is present but empty on a healthy integration.+ */+@RegisterForReflection+public class Integration {+ private String integrationArn;+ private String integrationName;+ private String sourceArn;+ private String targetArn;+ private String description;+ private String status;+ private String kmsKeyId;+ private String createTime;+ private Map<String, String> tags = new LinkedHashMap<>();+ private Map<String, String> additionalEncryptionContext = new LinkedHashMap<>();+ private List<String> errors = new ArrayList<>();++ public Integration() {}++ public String getIntegrationArn() { return integrationArn; }+ public void setIntegrationArn(String integrationArn) { this.integrationArn = integrationArn; }+ public String getIntegrationName() { return integrationName; }+ public void setIntegrationName(String integrationName) { this.integrationName = integrationName; }+ public String getSourceArn() { return sourceArn; }+ public void setSourceArn(String sourceArn) { this.sourceArn = sourceArn; }+ public String getTargetArn() { return targetArn; }+ public void setTargetArn(String targetArn) { this.targetArn = targetArn; }+ public String getDescription() { return description; }+ public void setDescription(String description) { this.description = description; }+ public String getStatus() { return status; }+ public void setStatus(String status) { this.status = status; }+ public String getKmsKeyId() { return kmsKeyId; }+ public void setKmsKeyId(String kmsKeyId) { this.kmsKeyId = kmsKeyId; }+ public String getCreateTime() { return createTime; }+ public void setCreateTime(String createTime) { this.createTime = createTime; }+ public Map<String, String> getTags() { return tags == null ? null : new LinkedHashMap<>(tags); }+ public void setTags(Map<String, String> tags) { this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags); }+ public Map<String, String> getAdditionalEncryptionContext() {+ return additionalEncryptionContext == null ? null : new LinkedHashMap<>(additionalEncryptionContext);+ }+ public void setAdditionalEncryptionContext(Map<String, String> context) {+ this.additionalEncryptionContext = context == null ? new LinkedHashMap<>() : new LinkedHashMap<>(context);+ }+ public List<String> getErrors() { return errors == null ? null : new ArrayList<>(errors); }+ public void setErrors(List<String> errors) { this.errors = errors == null ? new ArrayList<>() : new ArrayList<>(errors); }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftIntegrationsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftIntegrationsIntegrationTest.javanew file mode 100644index 0000000000..f8e58675c3--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/redshift/RedshiftIntegrationsIntegrationTest.java@@ -0,0 +1,314 @@+package io.github.hectorvent.floci.services.redshift;++import java.util.ArrayList;+import java.util.List;+import io.quarkus.test.junit.QuarkusTest;+import io.restassured.specification.RequestSpecification;+import io.restassured.response.Response;+import org.junit.jupiter.api.Test;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.not;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.junit.jupiter.api.Assertions.assertEquals;++/**+ * Redshift zero-ETL integrations.+ *+ * <p>The response shape was captured from a live integration in us-west-2: {@code Status} is lower+ * case ({@code active}), {@code Errors} is present but empty on a healthy integration, and an+ * unknown {@code IntegrationArn} is {@code IntegrationNotFoundFault}. An account with no+ * integrations returns an empty list rather than an error.+ */+@QuarkusTest+class RedshiftIntegrationsIntegrationTest {++ private static final String SOURCE = "arn:aws:dynamodb:us-east-1:000000000000:table/keystone-main";+ private static final String TARGET =+ "arn:aws:redshift-serverless:us-east-1:000000000000:namespace/8445f0c7-d2b1-4c1c-916c-0eeaa68fd487";++ /**+ * The Authorization header is what routes a Query request to a service; without it the+ * emulator cannot tell which service the action belongs to.+ */+ private static final String AUTH_HEADER =+ "AWS4-HMAC-SHA256 Credential=test/20260822/us-east-1/redshift/aws4_request";++ private static Response query(String... formParams) {+ RequestSpecification spec = given()+ .contentType("application/x-www-form-urlencoded")+ .header("Authorization", AUTH_HEADER)+ .formParam("Version", "2012-12-01");+ for (int i = 0; i < formParams.length; i += 2) {+ spec = spec.formParam(formParams[i], formParams[i + 1]);+ }+ return spec.when().post("/");+ }++ private static String createIntegration(String name) {+ return query("Action", "CreateIntegration", "IntegrationName", name,+ "SourceArn", SOURCE, "TargetArn", TARGET)+ .then().statusCode(200)+ .extract().body().asString();+ }++ private static String arnOf(String createResponseXml) {+ int start = createResponseXml.indexOf("<IntegrationArn>") + "<IntegrationArn>".length();+ return createResponseXml.substring(start, createResponseXml.indexOf("</IntegrationArn>"));+ }++ @Test+ void aCreatedIntegrationIsDescribed() {+ createIntegration("zetl-described");++ query("Action", "DescribeIntegrations")+ .then().statusCode(200)+ .body(containsString("zetl-described"))+ .body(containsString(SOURCE))+ .body(containsString(TARGET))+ // Lower case on real Redshift, not ACTIVE.+ .body(containsString("<Status>active</Status>"))+ .body(containsString("<Errors></Errors>"));+ }++ @Test+ void describingByArnReturnsOnlyThatIntegration() {+ String arn = arnOf(createIntegration("zetl-byarn"));+ createIntegration("zetl-other");++ query("Action", "DescribeIntegrations", "IntegrationArn", arn)+ .then().statusCode(200)+ .body(containsString("zetl-byarn"))+ .body(not(containsString("zetl-other")));+ }++ @Test+ void anUnknownArnIsNotFound() {+ query("Action", "DescribeIntegrations", "IntegrationArn",+ "arn:aws:redshift:us-east-1:000000000000:integration:00000000-0000-0000-0000-000000000000")+ .then().statusCode(404)+ .body(containsString("IntegrationNotFoundFault"));+ }++ @Test+ void aDeletedIntegrationNoLongerAppears() {+ String arn = arnOf(createIntegration("zetl-deleted"));++ query("Action", "DeleteIntegration", "IntegrationArn", arn)+ .then().statusCode(200)+ .body(containsString("zetl-deleted"));++ query("Action", "DescribeIntegrations")+ .then().statusCode(200)+ .body(not(containsString("zetl-deleted")));+ }++ @Test+ void deletingAnUnknownIntegrationIsNotFound() {+ query("Action", "DeleteIntegration", "IntegrationArn",+ "arn:aws:redshift:us-east-1:000000000000:integration:00000000-0000-0000-0000-000000000000")+ .then().statusCode(404)+ .body(containsString("IntegrationNotFoundFault"));+ }++ @Test+ void aDuplicateIntegrationNameIsRejected() {+ createIntegration("zetl-duplicate");++ query("Action", "CreateIntegration", "IntegrationName", "zetl-duplicate",+ "SourceArn", SOURCE, "TargetArn", TARGET)+ .then().statusCode(400)+ .body(containsString("IntegrationAlreadyExistsFault"));+ }++ @Test+ void anIntegrationNameOutsideTheModelledPatternIsRejected() {+ // CreateIntegration.IntegrationName is modelled as+ // ^[a-zA-Z][a-zA-Z0-9]*(-[a-zA-Z0-9]+)*$: a letter first, then alphanumeric groups joined+ // by single hyphens. A leading digit or hyphen, an underscore, a doubled hyphen and a+ // trailing hyphen are each outside it, so AWS refuses names floci used to accept.+ for (String name : new String[] {"1zetl", "-zetl", "zetl_name", "zetl--name", "zetl-"}) {+ query("Action", "CreateIntegration", "IntegrationName", name,+ "SourceArn", SOURCE, "TargetArn", TARGET)+ .then().statusCode(400)+ .body(containsString("InvalidParameterValue"))+ .body(containsString("IntegrationName"));+ }+ }++ @Test+ void anIntegrationNameOverTheModelledLengthIsRejected() {+ query("Action", "CreateIntegration", "IntegrationName", "z".repeat(64),+ "SourceArn", SOURCE, "TargetArn", TARGET)+ .then().statusCode(400)+ .body(containsString("InvalidParameterValue"));+ }++ @Test+ void anIntegrationNameAtTheModelledLengthIsAccepted() {+ // 63 characters is the documented maximum, so the boundary itself must still create.+ String atLimit = "zetl" + "a".repeat(59);+ assertEquals(63, atLimit.length());+ createIntegration(atLimit);+ }++ @Test+ void createRequiresSourceAndTarget() {+ query("Action", "CreateIntegration", "IntegrationName", "zetl-missing")+ .then().statusCode(400)+ .body(containsString("SourceArn"));+ }++ @Test+ void tagsSurviveTheRoundTrip() {+ // The Query member is TagList, not Tags: an SDK serialises the list under its own name.+ query("Action", "CreateIntegration", "IntegrationName", "zetl-tagged",+ "SourceArn", SOURCE, "TargetArn", TARGET,+ "TagList.Tag.1.Key", "Environment", "TagList.Tag.1.Value", "dev")+ .then().statusCode(200);++ query("Action", "DescribeIntegrations")+ .then().statusCode(200)+ .body(containsString("<Key>Environment</Key>"))+ .body(containsString("<Value>dev</Value>"));+ }++ @Test+ void anAccountWithNoIntegrationsGetsAnEmptyList() {+ // Not an error: real Redshift answers an account with none with an empty Integrations list.+ query("Action", "DescribeIntegrations")+ .then().statusCode(200)+ .body(containsString("<DescribeIntegrationsResult>"));+ }++ @Test+ void descriptionAndEncryptionContextAreStoredAndReturned() {+ query("Action", "CreateIntegration", "IntegrationName", "zetl-full",+ "SourceArn", SOURCE, "TargetArn", TARGET,+ "Description", "nightly replica",+ "KMSKeyId", "arn:aws:kms:us-east-1:000000000000:key/abc",+ "AdditionalEncryptionContext.entry.1.key", "team",+ "AdditionalEncryptionContext.entry.1.value", "data")+ .then().statusCode(200);++ query("Action", "DescribeIntegrations")+ .then().statusCode(200)+ .body(containsString("<Description>nightly replica</Description>"))+ .body(containsString("<key>team</key>"))+ .body(containsString("<value>data</value>"));+ }++ @Test+ void encryptionContextWithoutAKmsKeyIsRejected() {+ query("Action", "CreateIntegration", "IntegrationName", "zetl-nokms",+ "SourceArn", SOURCE, "TargetArn", TARGET,+ "AdditionalEncryptionContext.entry.1.key", "team",+ "AdditionalEncryptionContext.entry.1.value", "data")+ .then().statusCode(400)+ .body(containsString("KMSKeyId"));+ }++ @Test+ void describeCrossesAPageBoundaryAndResumesFromTheMarker() {+ // 21 records against the smallest legal page size guarantees more than one page. Other+ // tests in this class share the store, so the assertions below are about the traversal+ // rather than absolute counts.+ for (int i = 0; i < 21; i++) {+ query("Action", "CreateIntegration", "IntegrationName", String.format("zetl-page-%02d", i),+ "SourceArn", SOURCE + "-" + i, "TargetArn", TARGET)+ .then().statusCode(200);+ }++ String firstPage = query("Action", "DescribeIntegrations", "MaxRecords", "20")+ .then().statusCode(200).extract().body().asString();+ assertEquals(20, arnsIn(firstPage).size(), "a full page must carry exactly MaxRecords records");+ assertTrue(firstPage.contains("<Marker>"), "a non-terminal page must carry a Marker");++ // Walk every page, proving the marker resumes correctly rather than repeating or skipping.+ List<String> seen = new ArrayList<>(arnsIn(firstPage));+ String marker = between(firstPage, "<Marker>", "</Marker>");+ String page = firstPage;+ int guard = 0;+ while (page.contains("<Marker>") && guard++ < 20) {+ marker = between(page, "<Marker>", "</Marker>");+ page = query("Action", "DescribeIntegrations", "MaxRecords", "20", "Marker", marker)+ .then().statusCode(200).extract().body().asString();+ List<String> arns = arnsIn(page);+ assertFalse(arns.isEmpty(), "a page reached through a Marker must not be empty");+ for (String arn : arns) {+ assertFalse(seen.contains(arn), "no record may appear on two pages: " + arn);+ }+ seen.addAll(arns);+ }+ assertFalse(page.contains("<Marker>"), "the final page must omit Marker");++ // The traversal must have seen every record exactly once.+ List<String> everything = arnsIn(+ query("Action", "DescribeIntegrations", "MaxRecords", "100")+ .then().statusCode(200).extract().body().asString());+ assertEquals(everything.size(), seen.size(), "paging must omit nothing");+ assertTrue(seen.containsAll(everything), "paging must return the same records as one page");+ assertTrue(seen.size() >= 21, "the 21 records created here must all be reachable");+ }++ private static List<String> arnsIn(String xml) {+ List<String> arns = new ArrayList<>();+ int from = 0;+ while ((from = xml.indexOf("<IntegrationArn>", from)) >= 0) {+ int start = from + "<IntegrationArn>".length();+ arns.add(xml.substring(start, xml.indexOf("</IntegrationArn>", start)));+ from = start;+ }+ return arns;+ }++ private static String between(String xml, String open, String close) {+ int start = xml.indexOf(open) + open.length();+ return xml.substring(start, xml.indexOf(close, start));+ }++ @Test+ void maxRecordsOutsideTheDocumentedRangeIsRejected() {+ query("Action", "DescribeIntegrations", "MaxRecords", "5")+ .then().statusCode(400)+ .body(containsString("MaxRecords"));+ query("Action", "DescribeIntegrations", "MaxRecords", "101")+ .then().statusCode(400)+ .body(containsString("MaxRecords"));+ }++ @Test+ void filteringBySourceArnNarrowsTheResult() {+ query("Action", "CreateIntegration", "IntegrationName", "zetl-filtered",+ "SourceArn", SOURCE + "-filtered", "TargetArn", TARGET)+ .then().statusCode(200);+ query("Action", "CreateIntegration", "IntegrationName", "zetl-unfiltered",+ "SourceArn", SOURCE + "-other", "TargetArn", TARGET)+ .then().statusCode(200);++ query("Action", "DescribeIntegrations",+ "Filters.DescribeIntegrationsFilter.1.Name", "source-arn",+ "Filters.DescribeIntegrationsFilter.1.Values.Value.1", SOURCE + "-filtered")+ .then().statusCode(200)+ .body(containsString("zetl-filtered"))+ .body(not(containsString("zetl-unfiltered")));+ }++ @Test+ void anUnrecognisedFilterNameIsRejected() {+ query("Action", "DescribeIntegrations",+ "Filters.DescribeIntegrationsFilter.1.Name", "not-a-filter",+ "Filters.DescribeIntegrationsFilter.1.Values.Value.1", "x")+ .then().statusCode(400);+ }++ @Test+ void anInvalidMarkerIsRejected() {+ query("Action", "DescribeIntegrations", "Marker", "nonsense")+ .then().statusCode(400)+ .body(containsString("Marker"));+ }+}