mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(stepfunctions): ResultSelector, Pass Parameters, ArrayContains, wildcard projection; fix IsPresent for absent paths (#1558)
* feat(stepfunctions): apply ResultSelector, add States.ArrayContains and wildcard path projection Three JSONPath/intrinsic features that AslExecutor was missing: - ResultSelector is now applied to a Task's result (before ResultPath), the same template resolution used for Parameters. Previously it was accepted but silently ignored, so the raw result flowed through unchanged. - $.X.*.Y wildcard projection in path resolution: a * segment projects the remainder of the path over each array element and collects the results (e.g. $.Regions.*.RegionName). - States.ArrayContains(array, value) intrinsic. These are used together by orchestration that lists results and validates an input against them. Unit-tested for projection, ArrayContains, and ResultSelector evaluation; existing Step Functions suites unchanged. * fix(stepfunctions): apply Parameters in Pass states A Pass state's Parameters (with intrinsics) were accepted but never applied — the effective input flowed through unchanged. Apply Parameters to produce the Pass result (a static Result still overrides), so transforms like States.ArrayContains in a Pass state take effect. Integration-tested. * fix(stepfunctions): IsPresent reports an absent path as not present resolvePath collapses an absent path to a null node, so the Choice IsPresent comparator (which only checked isMissingNode) reported absent fields as present. Treat null and missing alike. Integration-tested for absent vs present fields. * feat(stepfunctions): bracket [*] wildcard, ResultSelector on Map/Parallel, present-null Address review feedback on #1558: - Support the AWS bracket wildcard form ($.Regions[*].RegionName) and root-level brackets ($[*][*]) by normalizing bracket notation into path segments; multi-level wildcards now flatten, so $[*][*] flattens an array of arrays. The dot form ($.Regions.*.RegionName) keeps working. - Apply ResultSelector to Map and Parallel states (not just Task), before ResultPath merges the result in. - IsPresent now treats an explicit null value as present: resolvePathNode preserves the missing-vs-null distinction that resolvePath collapses. * fix(stepfunctions): keep nulls in projections, validate ArrayContains arg Address greptile review on #1558: - Wildcard projection now keeps explicit null values and omits only missing matches, so $[*].field over [{"field":null},{"field":"x"}] yields [null,"x"] (consistent with treating null as a present value). - States.ArrayContains throws States.Runtime when its first argument is not an array, matching AWS and States.ArrayLength, instead of silently returning false. - Documented the dotted-field-key limitation of path segment splitting. * test(stepfunctions): update AslExecutor ctor for new main params Current main added CloudFormationQueryHandler, Ec2Service and S3Service to the AslExecutor constructor; pass mocks for them in AslExecutorPathIntrinsicsTest so it compiles against main. * fix(stepfunctions): resolve the $.[0] dot-bracket root array-index form splitPathSegments turned "$.[0]" into "..0" (the [0]->.0 rewrite stacked on the existing leading dot), leaving a spurious empty segment after the split, so the path resolved to null. "$[0]" already worked. Collapse consecutive dots so both the dot-bracket ($.[0]) and no-dot ($[0]) root array-index forms resolve. EventBridge Pipes batch unwrapping (InputPath "$.[0]") relies on this. Covered by AslExecutorPathIntrinsicsTest root-array-index cases.
This commit is contained in:
1 parent
b87955b630
commit
e0a1d027b9
5 files changed
+593
-23
No files matched your search
+160
@@ -0,0 +1,160 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler;
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaExecutorService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaFunctionStore;
|
||||
import io.github.hectorvent.floci.services.sqs.SqsJsonHandler;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Unit coverage for the JSONPath/intrinsic features DPS's provisioning state machine relies on:
|
||||
* the {@code $.X.*.Y} wildcard projection, {@code States.ArrayContains}, and {@code ResultSelector}
|
||||
* evaluation (which reuses the Parameters resolver).
|
||||
*/
|
||||
class AslExecutorPathIntrinsicsTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private AslExecutor executor;
|
||||
|
||||
@BeforeEach
|
||||
@SuppressWarnings("unchecked")
|
||||
void setUp() {
|
||||
executor = new AslExecutor(
|
||||
mock(LambdaExecutorService.class),
|
||||
mock(LambdaFunctionStore.class),
|
||||
mock(DynamoDbService.class),
|
||||
mock(DynamoDbJsonHandler.class),
|
||||
mock(SqsJsonHandler.class),
|
||||
mock(io.github.hectorvent.floci.services.cloudformation.CloudFormationQueryHandler.class),
|
||||
mock(io.github.hectorvent.floci.services.ec2.Ec2Service.class),
|
||||
mock(io.github.hectorvent.floci.services.s3.S3Service.class),
|
||||
mapper,
|
||||
new JsonataEvaluator(mapper),
|
||||
mock(Instance.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardProjectionCollectsFieldFromEachArrayElement() throws Exception {
|
||||
JsonNode root = mapper.readTree(
|
||||
"{\"Regions\":[{\"RegionName\":\"us-east-1\"},{\"RegionName\":\"eu-west-1\"}]}");
|
||||
JsonNode result = executor.resolvePath("$.Regions.*.RegionName", root);
|
||||
assertTrue(result.isArray());
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("us-east-1", result.get(0).asText());
|
||||
assertEquals("eu-west-1", result.get(1).asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardProjectionOnNonArrayIsNull() throws Exception {
|
||||
JsonNode root = mapper.readTree("{\"Regions\":\"not-an-array\"}");
|
||||
assertTrue(executor.resolvePath("$.Regions.*.RegionName", root).isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void bracketWildcardProjectionMatchesDotForm() throws Exception {
|
||||
JsonNode root = mapper.readTree(
|
||||
"{\"Regions\":[{\"RegionName\":\"us-east-1\"},{\"RegionName\":\"eu-west-1\"}]}");
|
||||
// The AWS bracket form must behave exactly like the dot form.
|
||||
JsonNode result = executor.resolvePath("$.Regions[*].RegionName", root);
|
||||
assertTrue(result.isArray());
|
||||
assertEquals(2, result.size());
|
||||
assertEquals("us-east-1", result.get(0).asText());
|
||||
assertEquals("eu-west-1", result.get(1).asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rootBracketDoubleWildcardFlattensArrayOfArrays() throws Exception {
|
||||
JsonNode root = mapper.readTree("[[1,2],[3,4]]");
|
||||
JsonNode result = executor.resolvePath("$[*][*]", root);
|
||||
assertTrue(result.isArray());
|
||||
assertEquals(4, result.size());
|
||||
assertEquals(1, result.get(0).asInt());
|
||||
assertEquals(4, result.get(3).asInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void numericBracketSegmentIndexesIntoArray() throws Exception {
|
||||
JsonNode root = mapper.readTree("{\"items\":[\"a\",\"b\",\"c\"]}");
|
||||
assertEquals("b", executor.resolvePath("$.items[1]", root).asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvePathNodeDistinguishesExplicitNullFromMissing() throws Exception {
|
||||
JsonNode root = mapper.readTree("{\"x\":null}");
|
||||
// An explicit null exists, so it is present (not a MissingNode)...
|
||||
assertFalse(executor.resolvePathNode("$.x", root).isMissingNode());
|
||||
assertTrue(executor.resolvePathNode("$.x", root).isNull());
|
||||
// ...while an absent field resolves to a MissingNode.
|
||||
assertTrue(executor.resolvePathNode("$.y", root).isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void arrayContainsTrueAndFalse() throws Exception {
|
||||
JsonNode root = mapper.readTree("{\"list\":[\"a\",\"b\",\"c\"],\"hit\":\"b\",\"miss\":\"z\"}");
|
||||
assertTrue(executor.resolvePath("States.ArrayContains($.list, $.hit)", root).asBoolean());
|
||||
assertFalse(executor.resolvePath("States.ArrayContains($.list, $.miss)", root).asBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
void arrayContainsThrowsWhenFirstArgIsNotAnArray() throws Exception {
|
||||
JsonNode root = mapper.readTree("{\"notList\":\"oops\",\"hit\":\"b\"}");
|
||||
// AWS raises States.Runtime instead of silently returning false on a non-array argument.
|
||||
assertThrows(AslExecutor.FailStateException.class,
|
||||
() -> executor.resolvePath("States.ArrayContains($.notList, $.hit)", root));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wildcardProjectionKeepsExplicitNullsButOmitsMissing() throws Exception {
|
||||
JsonNode root = mapper.readTree("[{\"field\":null},{\"field\":\"x\"},{\"other\":1}]");
|
||||
JsonNode result = executor.resolvePath("$[*].field", root);
|
||||
assertTrue(result.isArray());
|
||||
// The explicit null is kept; the element missing "field" is omitted.
|
||||
assertEquals(2, result.size());
|
||||
assertTrue(result.get(0).isNull());
|
||||
assertEquals("x", result.get(1).asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resultSelectorAppliesProjectionAndIntrinsics() throws Exception {
|
||||
JsonNode result = mapper.readTree(
|
||||
"{\"Regions\":[{\"RegionName\":\"us-east-1\"},{\"RegionName\":\"us-west-2\"}]}");
|
||||
JsonNode selector = mapper.readTree("{\"list.$\":\"$.Regions.*.RegionName\"}");
|
||||
JsonNode out = executor.resolveParameters(selector, result, mapper.createObjectNode());
|
||||
assertTrue(out.path("list").isArray());
|
||||
assertEquals(2, out.path("list").size());
|
||||
assertEquals("us-east-1", out.path("list").get(0).asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rootArrayIndexDotBracketIsResolved() throws Exception {
|
||||
// EventBridge Pipes delivers a batch as a JSON array; an SFN unwraps it via InputPath "$.[0]".
|
||||
JsonNode root = mapper.readTree("[{\"systemId\":\"DFSLOCAL\",\"solutions\":[1,2]}]");
|
||||
JsonNode out = executor.resolvePath("$.[0]", root);
|
||||
assertTrue(out.isObject());
|
||||
assertEquals("DFSLOCAL", out.path("systemId").asText());
|
||||
assertEquals(2, out.path("solutions").size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rootArrayIndexNoDotIsResolved() throws Exception {
|
||||
JsonNode root = mapper.readTree("[{\"k\":\"v0\"},{\"k\":\"v1\"}]");
|
||||
assertEquals("v1", executor.resolvePath("$[1]", root).path("k").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rootArrayIndexThenFieldIsResolved() throws Exception {
|
||||
JsonNode root = mapper.readTree("[{\"systemId\":\"S1\"}]");
|
||||
assertEquals("S1", executor.resolvePath("$.[0].systemId", root).asText());
|
||||
}
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
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.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* A Choice rule {@code Not { IsPresent: $.absentField }} must take the "absent" branch when the
|
||||
* field is missing — previously an absent path resolved to a non-missing null node, so IsPresent
|
||||
* wrongly reported the field as present.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class StepFunctionsChoiceIsPresentIntegrationTest {
|
||||
|
||||
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 DEFINITION = """
|
||||
{"StartAt":"Check","States":{
|
||||
"Check":{"Type":"Choice",
|
||||
"Choices":[{"Not":{"Variable":"$.maybe","IsPresent":true},"Next":"Absent"}],
|
||||
"Default":"Present"},
|
||||
"Absent":{"Type":"Pass","Result":"absent","End":true},
|
||||
"Present":{"Type":"Pass","Result":"present","End":true}}}
|
||||
""";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void absentFieldTakesNotIsPresentBranch() throws Exception {
|
||||
assertEquals("\"absent\"", run("{}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void presentFieldTakesDefaultBranch() throws Exception {
|
||||
assertEquals("\"present\"", run("{\"maybe\":\"here\"}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitNullCountsAsPresent() throws Exception {
|
||||
// A field that exists with a null value is present in AWS, so it must NOT take the absent branch.
|
||||
assertEquals("\"present\"", run("{\"maybe\":null}"));
|
||||
}
|
||||
|
||||
private String run(String input) throws InterruptedException {
|
||||
String name = "ispresent-" + System.currentTimeMillis() + "-" + input.length();
|
||||
Response create = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("{\"name\":\"" + name + "\",\"definition\":" + quote(DEFINITION) + ",\"roleArn\":\"" + ROLE_ARN + "\"}")
|
||||
.when().post("/");
|
||||
create.then().statusCode(200);
|
||||
String smArn = create.jsonPath().getString("stateMachineArn");
|
||||
|
||||
Response start = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.StartExecution")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("{\"stateMachineArn\":\"" + smArn + "\",\"input\":" + quote(input) + "}")
|
||||
.when().post("/");
|
||||
start.then().statusCode(200);
|
||||
String execArn = start.jsonPath().getString("executionArn");
|
||||
|
||||
for (int i = 0; i < 50; i++) {
|
||||
Response d = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.DescribeExecution")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("{\"executionArn\":\"" + execArn + "\"}")
|
||||
.when().post("/");
|
||||
String status = d.jsonPath().getString("status");
|
||||
if ("SUCCEEDED".equals(status)) {
|
||||
return d.jsonPath().getString("output");
|
||||
}
|
||||
if ("FAILED".equals(status)) {
|
||||
fail("Execution failed: " + d.body().asString());
|
||||
}
|
||||
Thread.sleep(100);
|
||||
}
|
||||
fail("Execution did not complete");
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String quote(String raw) {
|
||||
return "\"" + raw.replace("\\", "\\\\").replace("\"", "\\\"")
|
||||
.replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t") + "\"";
|
||||
}
|
||||
}
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
package io.github.hectorvent.floci.services.stepfunctions;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Verifies a Pass state applies its {@code Parameters} (with intrinsics) — previously they were
|
||||
* accepted but ignored, so the input flowed through unchanged.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class StepFunctionsPassParametersIntegrationTest {
|
||||
|
||||
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 ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
private static final String DEFINITION = """
|
||||
{"StartAt":"Validate","States":{
|
||||
"Validate":{"Type":"Pass",
|
||||
"Parameters":{"result.$":"States.ArrayContains($.list, $.target)"},
|
||||
"ResultPath":"$.validation","End":true}}}
|
||||
""";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void passStateAppliesParametersWithArrayContains() throws Exception {
|
||||
JsonNode hit = mapper.readTree(run("{\"list\":[\"a\",\"b\",\"c\"],\"target\":\"b\"}"));
|
||||
assertTrue(hit.path("validation").path("result").asBoolean());
|
||||
|
||||
JsonNode miss = mapper.readTree(run("{\"list\":[\"a\",\"b\",\"c\"],\"target\":\"z\"}"));
|
||||
assertFalse(miss.path("validation").path("result").asBoolean());
|
||||
}
|
||||
|
||||
private String run(String input) throws InterruptedException {
|
||||
String smArn = create("pass-params-" + System.currentTimeMillis());
|
||||
String execArn = start(smArn, input);
|
||||
for (int i = 0; i < 50; i++) {
|
||||
Response resp = describe(execArn);
|
||||
String status = resp.jsonPath().getString("status");
|
||||
if ("SUCCEEDED".equals(status)) {
|
||||
return resp.jsonPath().getString("output");
|
||||
}
|
||||
if ("FAILED".equals(status) || "ABORTED".equals(status)) {
|
||||
fail("Execution " + status + ": " + resp.body().asString());
|
||||
}
|
||||
Thread.sleep(100);
|
||||
}
|
||||
fail("Execution did not complete");
|
||||
return null;
|
||||
}
|
||||
|
||||
private String create(String name) {
|
||||
Response resp = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("{\"name\":\"" + name + "\",\"definition\":" + quote(DEFINITION) + ",\"roleArn\":\"" + ROLE_ARN + "\"}")
|
||||
.when().post("/");
|
||||
resp.then().statusCode(200);
|
||||
return resp.jsonPath().getString("stateMachineArn");
|
||||
}
|
||||
|
||||
private String start(String smArn, String input) {
|
||||
Response resp = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.StartExecution")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("{\"stateMachineArn\":\"" + smArn + "\",\"input\":" + quote(input) + "}")
|
||||
.when().post("/");
|
||||
resp.then().statusCode(200);
|
||||
return resp.jsonPath().getString("executionArn");
|
||||
}
|
||||
|
||||
private Response describe(String execArn) {
|
||||
return given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.DescribeExecution")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("{\"executionArn\":\"" + execArn + "\"}")
|
||||
.when().post("/");
|
||||
}
|
||||
|
||||
private static String quote(String raw) {
|
||||
return "\"" + raw.replace("\\", "\\\\").replace("\"", "\\\"")
|
||||
.replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t") + "\"";
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
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.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* ResultSelector is not Task-only in AWS: it also transforms the raw result of Map and Parallel
|
||||
* states before ResultPath merges it back into the state input. These tests also exercise the
|
||||
* AWS bracket wildcard form ({@code $[*].field}) inside the selector.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class StepFunctionsResultSelectorMapParallelIntegrationTest {
|
||||
|
||||
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 MAP_DEFINITION = """
|
||||
{"StartAt":"M","States":{
|
||||
"M":{"Type":"Map","ItemsPath":"$.people",
|
||||
"ItemProcessor":{"StartAt":"P","States":{"P":{"Type":"Pass","End":true}}},
|
||||
"ResultSelector":{"names.$":"$[*].name"},
|
||||
"End":true}}}
|
||||
""";
|
||||
|
||||
private static final String PARALLEL_DEFINITION = """
|
||||
{"StartAt":"P","States":{
|
||||
"P":{"Type":"Parallel",
|
||||
"Branches":[
|
||||
{"StartAt":"A","States":{"A":{"Type":"Pass","Result":{"v":1},"End":true}}},
|
||||
{"StartAt":"B","States":{"B":{"Type":"Pass","Result":{"v":2},"End":true}}}],
|
||||
"ResultSelector":{"values.$":"$[*].v"},
|
||||
"End":true}}}
|
||||
""";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
void mapResultSelectorCollectsFieldFromEachIteration() throws Exception {
|
||||
assertEquals("{\"names\":[\"a\",\"b\"]}",
|
||||
run(MAP_DEFINITION, "{\"people\":[{\"name\":\"a\"},{\"name\":\"b\"}]}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parallelResultSelectorCollectsFieldFromEachBranch() throws Exception {
|
||||
assertEquals("{\"values\":[1,2]}", run(PARALLEL_DEFINITION, "{}"));
|
||||
}
|
||||
|
||||
private String run(String definition, String input) throws InterruptedException {
|
||||
String name = "resultselector-" + System.nanoTime();
|
||||
Response create = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("{\"name\":\"" + name + "\",\"definition\":" + quote(definition) + ",\"roleArn\":\"" + ROLE_ARN + "\"}")
|
||||
.when().post("/");
|
||||
create.then().statusCode(200);
|
||||
String smArn = create.jsonPath().getString("stateMachineArn");
|
||||
|
||||
Response start = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.StartExecution")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("{\"stateMachineArn\":\"" + smArn + "\",\"input\":" + quote(input) + "}")
|
||||
.when().post("/");
|
||||
start.then().statusCode(200);
|
||||
String execArn = start.jsonPath().getString("executionArn");
|
||||
|
||||
for (int i = 0; i < 50; i++) {
|
||||
Response d = given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.DescribeExecution")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body("{\"executionArn\":\"" + execArn + "\"}")
|
||||
.when().post("/");
|
||||
String status = d.jsonPath().getString("status");
|
||||
if ("SUCCEEDED".equals(status)) {
|
||||
return d.jsonPath().getString("output");
|
||||
}
|
||||
if ("FAILED".equals(status)) {
|
||||
fail("Execution failed: " + d.body().asString());
|
||||
}
|
||||
Thread.sleep(100);
|
||||
}
|
||||
fail("Execution did not complete");
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String quote(String raw) {
|
||||
return "\"" + raw.replace("\\", "\\\\").replace("\"", "\\\"")
|
||||
.replace("\n", "\\n").replace("\r", "\\r").replace("\t", "\\t") + "\"";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user