From 4d2a99506a2f5aaf2086956fe52bc749ab3716e6 Mon Sep 17 00:00:00 2001 From: Yassir Idhbi Date: Thu, 14 May 2026 06:46:54 +0200 Subject: [PATCH] feat: add ListTagsForResource, TagResource, UntagResource for Step Functions (#767) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add ListTagsForResource, TagResource, UntagResource for Step Functions Implements the SharedTagsController TagHandler pattern for SFN state machines and activities. Adds tags field to StateMachine and Activity models, tag CRUD methods to StepFunctionsService, and a new StepFunctionsTagHandler that registers the 'states' service key. Closes #766 * feat: cover JSON 1.0 SDK wire path for SFN tag operations Add ListTagsForResource, TagResource, UntagResource cases to StepFunctionsJsonHandler for AWS SDK (awsJson1.0) compatibility. The REST /tags/{arn} path (SharedTagsController) already covers Terraform; this commit covers the SDK path. Expand integration test to verify both wire paths. Add SDK-based compatibility test using SfnClient. * chore: remove invented inline comment from tags integration test * refactor: remove dead REST TagHandler and clean up integration tests StepFunctionsTagHandler registered the 'states' service key with SharedTagsController (REST /tags/{arn}), but SFN uses JSON 1.0 exclusively. No real client calls GET/POST/DELETE /tags/arn:aws:states:... — the handler was unreachable dead code. Remove StepFunctionsTagHandler and strip the eight REST-path tests from StepFunctionsTagsIntegrationTest. The remaining JSON 1.0 tests are now self-contained (no longer depend on state set by the deleted REST tests). * fix: correct SDK API usage in tag tests and persist creation-time tags - Fix StepFunctionsTagsTest: tags() takes Tag... not Map, use tagsAsMap() helper to convert List before asserting with MapAssert methods - Fix StepFunctionsService: pass tags through createStateMachine/createActivity so tags set at creation time are persisted immediately - Fix StepFunctionsJsonHandler: extract parseTagsArray() helper, add 400 validation when tags/tagKeys are not arrays, wire tags into create calls - Expand StepFunctionsTagsIntegrationTest: cover creation-time tag persistence for state machines and activities, and 400 on malformed tag input --- .../com/floci/test/StepFunctionsTagsTest.java | 155 +++++++++ .../StepFunctionsJsonHandler.java | 60 +++- .../stepfunctions/StepFunctionsService.java | 60 +++- .../stepfunctions/model/Activity.java | 7 + .../stepfunctions/model/StateMachine.java | 7 + .../StepFunctionsTagsIntegrationTest.java | 329 ++++++++++++++++++ 6 files changed, 614 insertions(+), 4 deletions(-) create mode 100644 compatibility-tests/sdk-test-java/src/test/java/com/floci/test/StepFunctionsTagsTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsTagsIntegrationTest.java diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/StepFunctionsTagsTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/StepFunctionsTagsTest.java new file mode 100644 index 0000000000..b4694058e9 --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/StepFunctionsTagsTest.java @@ -0,0 +1,155 @@ +package com.floci.test; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import software.amazon.awssdk.services.sfn.SfnClient; +import software.amazon.awssdk.services.sfn.model.ListTagsForResourceResponse; +import software.amazon.awssdk.services.sfn.model.Tag; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.*; + +/** + * Compatibility tests for Step Functions tag operations: + * ListTagsForResource, TagResource, UntagResource on state machines and activities. + * + * Covers issue #766: Terraform AWS provider v5+ calls ListTagsForResource + * unconditionally on every read of aws_sfn_state_machine. + */ +@DisplayName("SFN Tag Operations") +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class StepFunctionsTagsTest { + + private static final String ROLE_ARN = System.getenv("SFN_ROLE_ARN") != null + ? System.getenv("SFN_ROLE_ARN") + : "arn:aws:iam::000000000000:role/service-role/test-role"; + + private static final String SIMPLE_DEFINITION = """ + {"Comment":"tag test","StartAt":"Done","States":{"Done":{"Type":"Pass","End":true}}} + """.strip(); + + private static SfnClient sfn; + private static String stateMachineArn; + private static String activityArn; + + @BeforeAll + static void setup() { + sfn = TestFixtures.sfnClient(); + String smName = TestFixtures.uniqueName("tags-sm"); + stateMachineArn = sfn.createStateMachine(b -> b + .name(smName) + .definition(SIMPLE_DEFINITION) + .roleArn(ROLE_ARN)).stateMachineArn(); + + String actName = TestFixtures.uniqueName("tags-activity"); + activityArn = sfn.createActivity(b -> b.name(actName)).activityArn(); + } + + @AfterAll + static void cleanup() { + if (sfn != null) { + if (stateMachineArn != null) { + try { sfn.deleteStateMachine(b -> b.stateMachineArn(stateMachineArn)); } catch (Exception ignored) {} + } + if (activityArn != null) { + try { sfn.deleteActivity(b -> b.activityArn(activityArn)); } catch (Exception ignored) {} + } + sfn.close(); + } + } + + // ──────────────── State Machine Tags ──────────────── + + @Test + @Order(1) + void listTags_newStateMachine_returnsEmptyList() { + ListTagsForResourceResponse resp = sfn.listTagsForResource(b -> b.resourceArn(stateMachineArn)); + assertThat(resp.tags()).isEmpty(); + } + + @Test + @Order(2) + void tagResource_stateMachine_addsTagsAndListReturnsAll() { + sfn.tagResource(b -> b + .resourceArn(stateMachineArn) + .tags(tag("env", "test"), tag("team", "platform"))); + + Map tags = tagsAsMap(sfn.listTagsForResource(b -> b.resourceArn(stateMachineArn)).tags()); + assertThat(tags) + .containsEntry("env", "test") + .containsEntry("team", "platform"); + } + + @Test + @Order(3) + void untagResource_stateMachine_removesOnlySpecifiedKeys() { + sfn.untagResource(b -> b + .resourceArn(stateMachineArn) + .tagKeys("team")); + + Map tags = tagsAsMap(sfn.listTagsForResource(b -> b.resourceArn(stateMachineArn)).tags()); + assertThat(tags) + .containsEntry("env", "test") + .doesNotContainKey("team"); + } + + @Test + @Order(4) + void tagResource_stateMachine_overwritesExistingKey() { + sfn.tagResource(b -> b + .resourceArn(stateMachineArn) + .tags(tag("env", "prod"))); + + Map tags = tagsAsMap(sfn.listTagsForResource(b -> b.resourceArn(stateMachineArn)).tags()); + assertThat(tags).containsEntry("env", "prod"); + } + + // ──────────────── Activity Tags ──────────────── + + @Test + @Order(5) + void listTags_newActivity_returnsEmptyList() { + ListTagsForResourceResponse resp = sfn.listTagsForResource(b -> b.resourceArn(activityArn)); + assertThat(resp.tags()).isEmpty(); + } + + @Test + @Order(6) + void tagResource_activity_addsTagsAndListReturnsAll() { + sfn.tagResource(b -> b + .resourceArn(activityArn) + .tags(tag("owner", "infra"))); + + Map tags = tagsAsMap(sfn.listTagsForResource(b -> b.resourceArn(activityArn)).tags()); + assertThat(tags).containsEntry("owner", "infra"); + } + + @Test + @Order(7) + void untagResource_activity_removesSpecifiedKey() { + sfn.untagResource(b -> b + .resourceArn(activityArn) + .tagKeys("owner")); + + Map tags = tagsAsMap(sfn.listTagsForResource(b -> b.resourceArn(activityArn)).tags()); + assertThat(tags).doesNotContainKey("owner"); + } + + // ──────────────── Helpers ──────────────── + + private static Tag tag(String key, String value) { + return Tag.builder().key(key).value(value).build(); + } + + private static Map tagsAsMap(List tags) { + return tags.stream().collect(Collectors.toMap(Tag::key, Tag::value)); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsJsonHandler.java index b904b7427d..74b213e74a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsJsonHandler.java @@ -14,7 +14,9 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.ws.rs.core.Response; +import java.util.HashMap; import java.util.List; +import java.util.Map; @ApplicationScoped public class StepFunctionsJsonHandler { @@ -48,6 +50,9 @@ public class StepFunctionsJsonHandler { case "DescribeActivity" -> handleDescribeActivity(request); case "ListActivities" -> handleListActivities(request, region); case "GetActivityTask" -> handleGetActivityTask(request); + case "ListTagsForResource" -> handleListTagsForResource(request); + case "TagResource" -> handleTagResource(request); + case "UntagResource" -> handleUntagResource(request); default -> Response.status(400) .entity(new AwsErrorResponse("UnsupportedOperation", "Operation " + action + " is not supported.")) .build(); @@ -60,7 +65,8 @@ public class StepFunctionsJsonHandler { request.path("definition").asText(), request.path("roleArn").asText(), request.path("type").asText(null), - region + region, + parseTagsArray(request.path("tags")) ); ObjectNode response = objectMapper.createObjectNode(); response.put("stateMachineArn", sm.getStateMachineArn()); @@ -214,7 +220,7 @@ public class StepFunctionsJsonHandler { } private Response handleCreateActivity(JsonNode request, String region) { - Activity activity = service.createActivity(request.path("name").asText(), region); + Activity activity = service.createActivity(request.path("name").asText(), region, parseTagsArray(request.path("tags"))); ObjectNode response = objectMapper.createObjectNode(); response.put("activityArn", activity.getActivityArn()); response.put("creationDate", activity.getCreationDate()); @@ -260,4 +266,54 @@ public class StepFunctionsJsonHandler { return Response.ok(response).build(); } + private Response handleListTagsForResource(JsonNode request) { + java.util.Map tags = service.listTags(request.path("resourceArn").asText()); + ObjectNode response = objectMapper.createObjectNode(); + ArrayNode array = response.putArray("tags"); + tags.forEach((k, v) -> { + ObjectNode entry = array.addObject(); + entry.put("key", k); + entry.put("value", v); + }); + return Response.ok(response).build(); + } + + private Response handleTagResource(JsonNode request) { + String arn = request.path("resourceArn").asText(); + JsonNode tagsNode = request.path("tags"); + if (!tagsNode.isArray()) { + return Response.status(400) + .entity(new AwsErrorResponse("ValidationException", "Parameter 'tags' must be a list")) + .build(); + } + service.tagResource(arn, parseTagsArray(tagsNode)); + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private Response handleUntagResource(JsonNode request) { + String arn = request.path("resourceArn").asText(); + JsonNode keysNode = request.path("tagKeys"); + if (!keysNode.isArray()) { + return Response.status(400) + .entity(new AwsErrorResponse("ValidationException", "Parameter 'tagKeys' must be a list")) + .build(); + } + java.util.List tagKeys = new java.util.ArrayList<>(); + for (JsonNode key : keysNode) { + tagKeys.add(key.asText()); + } + service.untagResource(arn, tagKeys); + return Response.ok(objectMapper.createObjectNode()).build(); + } + + private Map parseTagsArray(JsonNode tagsNode) { + Map tags = new HashMap<>(); + if (tagsNode != null && tagsNode.isArray()) { + for (JsonNode entry : tagsNode) { + tags.put(entry.path("key").asText(), entry.path("value").asText()); + } + } + return tags; + } + } diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java index a54ace8769..73b01a1044 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsService.java @@ -62,7 +62,7 @@ public class StepFunctionsService { // ──────────────────────────── State Machines ──────────────────────────── - public StateMachine createStateMachine(String name, String definition, String roleArn, String type, String region) { + public StateMachine createStateMachine(String name, String definition, String roleArn, String type, String region, Map tags) { String arn = regionResolver.buildArn("states", region, "stateMachine:" + name); if (stateMachineStore.get(arn).isPresent()) { throw new AwsException("StateMachineAlreadyExists", "State machine already exists: " + arn, 400); @@ -78,6 +78,9 @@ public class StepFunctionsService { if (type != null && !type.isEmpty()) { sm.setType(type); } + if (tags != null && !tags.isEmpty()) { + sm.getTags().putAll(tags); + } stateMachineStore.put(arn, sm); LOG.infov("Created State Machine: {0}", arn); @@ -213,7 +216,7 @@ public class StepFunctionsService { // ──────────────────────────── Activities ──────────────────────────── - public Activity createActivity(String name, String region) { + public Activity createActivity(String name, String region, Map tags) { String arn = regionResolver.buildArn("states", region, "activity:" + name); if (activityStore.get(arn).isPresent()) { throw new AwsException("ActivityAlreadyExists", "Activity already exists: " + arn, 400); @@ -221,6 +224,9 @@ public class StepFunctionsService { Activity activity = new Activity(); activity.setActivityArn(arn); activity.setName(name); + if (tags != null && !tags.isEmpty()) { + activity.getTags().putAll(tags); + } activityStore.put(arn, activity); LOG.infov("Created activity: {0}", arn); return activity; @@ -297,6 +303,56 @@ public class StepFunctionsService { LOG.debugv("Task heartbeat for token {0}", taskToken); } + // ──────────────────────────── Tags ──────────────────────────── + + public Map listTags(String arn) { + Optional sm = stateMachineStore.get(arn); + if (sm.isPresent()) { + return sm.get().getTags(); + } + Optional activity = activityStore.get(arn); + if (activity.isPresent()) { + return activity.get().getTags(); + } + throw new AwsException("ResourceNotFound", "Resource not found: " + arn, 400); + } + + public void tagResource(String arn, Map tags) { + Optional smOpt = stateMachineStore.get(arn); + if (smOpt.isPresent()) { + StateMachine sm = smOpt.get(); + sm.getTags().putAll(tags); + stateMachineStore.put(arn, sm); + return; + } + Optional actOpt = activityStore.get(arn); + if (actOpt.isPresent()) { + Activity activity = actOpt.get(); + activity.getTags().putAll(tags); + activityStore.put(arn, activity); + return; + } + throw new AwsException("ResourceNotFound", "Resource not found: " + arn, 400); + } + + public void untagResource(String arn, List tagKeys) { + Optional smOpt = stateMachineStore.get(arn); + if (smOpt.isPresent()) { + StateMachine sm = smOpt.get(); + tagKeys.forEach(sm.getTags()::remove); + stateMachineStore.put(arn, sm); + return; + } + Optional actOpt = activityStore.get(arn); + if (actOpt.isPresent()) { + Activity activity = actOpt.get(); + tagKeys.forEach(activity.getTags()::remove); + activityStore.put(arn, activity); + return; + } + throw new AwsException("ResourceNotFound", "Resource not found: " + arn, 400); + } + // ──────────────────────────── Validation ──────────────────────────── private void validateDefinition(String definition) { diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/model/Activity.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/model/Activity.java index cc114d7c91..8f8efd8646 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/model/Activity.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/model/Activity.java @@ -3,12 +3,16 @@ package io.github.hectorvent.floci.services.stepfunctions.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import io.quarkus.runtime.annotations.RegisterForReflection; +import java.util.HashMap; +import java.util.Map; + @RegisterForReflection @JsonIgnoreProperties(ignoreUnknown = true) public class Activity { private String activityArn; private String name; private double creationDate; + private Map tags = new HashMap<>(); public Activity() { this.creationDate = System.currentTimeMillis() / 1000.0; @@ -22,4 +26,7 @@ public class Activity { public double getCreationDate() { return creationDate; } public void setCreationDate(double creationDate) { this.creationDate = creationDate; } + + public Map getTags() { return tags; } + public void setTags(Map tags) { this.tags = tags; } } diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/model/StateMachine.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/model/StateMachine.java index e3c85fe5f6..e89f2c3559 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/model/StateMachine.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/model/StateMachine.java @@ -3,6 +3,9 @@ package io.github.hectorvent.floci.services.stepfunctions.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import io.quarkus.runtime.annotations.RegisterForReflection; +import java.util.HashMap; +import java.util.Map; + @RegisterForReflection @JsonIgnoreProperties(ignoreUnknown = true) public class StateMachine { @@ -13,6 +16,7 @@ public class StateMachine { private String type = "STANDARD"; private String status = "ACTIVE"; private double creationDate; + private Map tags = new HashMap<>(); public StateMachine() { this.creationDate = System.currentTimeMillis() / 1000.0; @@ -38,4 +42,7 @@ public class StateMachine { public double getCreationDate() { return creationDate; } public void setCreationDate(double creationDate) { this.creationDate = creationDate; } + + public Map getTags() { return tags; } + public void setTags(Map tags) { this.tags = tags; } } diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsTagsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsTagsIntegrationTest.java new file mode 100644 index 0000000000..5caa8f8d7b --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsTagsIntegrationTest.java @@ -0,0 +1,329 @@ +package io.github.hectorvent.floci.services.stepfunctions; + +import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.Response; +import org.junit.jupiter.api.*; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.*; + +/** + * Integration tests for SFN tag operations via the JSON 1.0 wire path + * (X-Amz-Target: AWSStepFunctions.*), used by the AWS SDK and Terraform. + */ +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class StepFunctionsTagsIntegrationTest { + + private static final String SFN_CONTENT_TYPE = "application/x-amz-json-1.0"; + private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/test-role"; + private static final String SIMPLE_DEFINITION = + "{\"Comment\":\"tag test\",\"StartAt\":\"Done\",\"States\":{\"Done\":{\"Type\":\"Pass\",\"End\":true}}}"; + + private static String stateMachineArn; + private static String activityArn; + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + @Order(1) + void createStateMachine() { + Response resp = given() + .header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine") + .contentType(SFN_CONTENT_TYPE) + .body("{\"name\":\"tag-test-sm\",\"definition\":\"" + SIMPLE_DEFINITION.replace("\"", "\\\"") + "\",\"roleArn\":\"" + ROLE_ARN + "\"}") + .when().post("/"); + resp.then().statusCode(200); + stateMachineArn = resp.jsonPath().getString("stateMachineArn"); + assertNotNull(stateMachineArn); + } + + @Test + @Order(2) + void createActivity() { + Response resp = given() + .header("X-Amz-Target", "AWSStepFunctions.CreateActivity") + .contentType(SFN_CONTENT_TYPE) + .body("{\"name\":\"tag-test-activity\"}") + .when().post("/"); + resp.then().statusCode(200); + activityArn = resp.jsonPath().getString("activityArn"); + assertNotNull(activityArn); + } + + // ──────────────── JSON 1.0 path (X-Amz-Target) — SDK / Terraform wire ──────────────── + + @Test + @Order(3) + void json_listTagsForResourceReturnsEmptyForNewStateMachine() { + given() + .header("X-Amz-Target", "AWSStepFunctions.ListTagsForResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + stateMachineArn + "\"}") + .post("/") + .then() + .statusCode(200) + .body("tags", hasSize(0)); + } + + @Test + @Order(4) + void json_tagResourceAddsTag() { + given() + .header("X-Amz-Target", "AWSStepFunctions.TagResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + stateMachineArn + "\",\"tags\":[{\"key\":\"env\",\"value\":\"test\"},{\"key\":\"owner\",\"value\":\"infra\"}]}") + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "AWSStepFunctions.ListTagsForResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + stateMachineArn + "\"}") + .post("/") + .then() + .statusCode(200) + .body("tags", hasSize(2)) + .body("tags.key", hasItems("env", "owner")) + .body("tags.value", hasItems("test", "infra")); + } + + @Test + @Order(5) + void json_untagResourceRemovesTag() { + given() + .header("X-Amz-Target", "AWSStepFunctions.UntagResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + stateMachineArn + "\",\"tagKeys\":[\"owner\"]}") + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "AWSStepFunctions.ListTagsForResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + stateMachineArn + "\"}") + .post("/") + .then() + .statusCode(200) + .body("tags", hasSize(1)) + .body("tags[0].key", equalTo("env")) + .body("tags[0].value", equalTo("test")); + } + + @Test + @Order(6) + void json_createStateMachine_withTags_tagsPersistedImmediately() { + Response resp = given() + .header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine") + .contentType(SFN_CONTENT_TYPE) + .body("{\"name\":\"tag-test-sm-with-tags\",\"definition\":\"" + SIMPLE_DEFINITION.replace("\"", "\\\"") + "\",\"roleArn\":\"" + ROLE_ARN + "\",\"tags\":[{\"key\":\"env\",\"value\":\"prod\"},{\"key\":\"team\",\"value\":\"platform\"}]}") + .when().post("/"); + resp.then().statusCode(200); + String arn = resp.jsonPath().getString("stateMachineArn"); + + given() + .header("X-Amz-Target", "AWSStepFunctions.ListTagsForResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + arn + "\"}") + .post("/") + .then() + .statusCode(200) + .body("tags", hasSize(2)) + .body("tags.key", hasItems("env", "team")) + .body("tags.value", hasItems("prod", "platform")); + + given() + .header("X-Amz-Target", "AWSStepFunctions.DeleteStateMachine") + .contentType(SFN_CONTENT_TYPE) + .body("{\"stateMachineArn\":\"" + arn + "\"}") + .post("/") + .then().statusCode(200); + } + + @Test + @Order(7) + void json_createActivity_withTags_tagsPersistedImmediately() { + Response resp = given() + .header("X-Amz-Target", "AWSStepFunctions.CreateActivity") + .contentType(SFN_CONTENT_TYPE) + .body("{\"name\":\"tag-test-activity-with-tags\",\"tags\":[{\"key\":\"owner\",\"value\":\"infra\"}]}") + .when().post("/"); + resp.then().statusCode(200); + String arn = resp.jsonPath().getString("activityArn"); + + given() + .header("X-Amz-Target", "AWSStepFunctions.ListTagsForResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + arn + "\"}") + .post("/") + .then() + .statusCode(200) + .body("tags", hasSize(1)) + .body("tags[0].key", equalTo("owner")) + .body("tags[0].value", equalTo("infra")); + + given() + .header("X-Amz-Target", "AWSStepFunctions.DeleteActivity") + .contentType(SFN_CONTENT_TYPE) + .body("{\"activityArn\":\"" + arn + "\"}") + .post("/") + .then().statusCode(200); + } + + @Test + @Order(8) + void json_tagResource_tagsNotArray_returns400() { + given() + .header("X-Amz-Target", "AWSStepFunctions.TagResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + stateMachineArn + "\",\"tags\":\"not-an-array\"}") + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")); + } + + @Test + @Order(9) + void json_untagResource_tagKeysNotArray_returns400() { + given() + .header("X-Amz-Target", "AWSStepFunctions.UntagResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + stateMachineArn + "\",\"tagKeys\":\"not-an-array\"}") + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ValidationException")); + } + + @Test + @Order(10) + void json_listTags_unknownArn_returns400() { + given() + .header("X-Amz-Target", "AWSStepFunctions.ListTagsForResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"arn:aws:states:us-east-1:000000000000:stateMachine:does-not-exist\"}") + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ResourceNotFound")); + } + + @Test + @Order(11) + void json_tagResource_unknownArn_returns400() { + given() + .header("X-Amz-Target", "AWSStepFunctions.TagResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"arn:aws:states:us-east-1:000000000000:stateMachine:does-not-exist\",\"tags\":[{\"key\":\"k\",\"value\":\"v\"}]}") + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ResourceNotFound")); + } + + @Test + @Order(12) + void json_untagResource_unknownArn_returns400() { + given() + .header("X-Amz-Target", "AWSStepFunctions.UntagResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"arn:aws:states:us-east-1:000000000000:stateMachine:does-not-exist\",\"tagKeys\":[\"k\"]}") + .post("/") + .then() + .statusCode(400) + .body("__type", equalTo("ResourceNotFound")); + } + + @Test + @Order(13) + void json_tagResource_activity_addsTag() { + given() + .header("X-Amz-Target", "AWSStepFunctions.TagResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + activityArn + "\",\"tags\":[{\"key\":\"env\",\"value\":\"test\"}]}") + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "AWSStepFunctions.ListTagsForResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + activityArn + "\"}") + .post("/") + .then() + .statusCode(200) + .body("tags", hasSize(1)) + .body("tags[0].key", equalTo("env")) + .body("tags[0].value", equalTo("test")); + } + + @Test + @Order(14) + void json_untagResource_activity_removesTag() { + given() + .header("X-Amz-Target", "AWSStepFunctions.UntagResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + activityArn + "\",\"tagKeys\":[\"env\"]}") + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "AWSStepFunctions.ListTagsForResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + activityArn + "\"}") + .post("/") + .then() + .statusCode(200) + .body("tags", hasSize(0)); + } + + @Test + @Order(15) + void json_tagResource_overwritesExistingKey() { + given() + .header("X-Amz-Target", "AWSStepFunctions.TagResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + stateMachineArn + "\",\"tags\":[{\"key\":\"env\",\"value\":\"updated\"}]}") + .post("/") + .then() + .statusCode(200); + + given() + .header("X-Amz-Target", "AWSStepFunctions.ListTagsForResource") + .contentType(SFN_CONTENT_TYPE) + .body("{\"resourceArn\":\"" + stateMachineArn + "\"}") + .post("/") + .then() + .statusCode(200) + .body("tags", hasSize(1)) + .body("tags[0].key", equalTo("env")) + .body("tags[0].value", equalTo("updated")); + } + + @Test + @Order(16) + void cleanup() { + given() + .header("X-Amz-Target", "AWSStepFunctions.DeleteStateMachine") + .contentType(SFN_CONTENT_TYPE) + .body("{\"stateMachineArn\":\"" + stateMachineArn + "\"}") + .post("/") + .then().statusCode(200); + + given() + .header("X-Amz-Target", "AWSStepFunctions.DeleteActivity") + .contentType(SFN_CONTENT_TYPE) + .body("{\"activityArn\":\"" + activityArn + "\"}") + .post("/") + .then().statusCode(200); + } +}