Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(stepfunctions): let a mocked response with no attempts start the execution (#2823)

TATSUNO “Taz” Yasuhiro63a2a60a3001
diff --git a/docs/services/step-functions.md b/docs/services/step-functions.md
index df7a299ddc..295801d5a6 100644
--- a/docs/services/step-functions.md
+++ b/docs/services/step-functions.md
@@ -398,6 +398,11 @@ entry, `Return` and `Throw` together, `Throw` without `Error`) as a structured 4
at `StartExecution`. Step Functions Local instead returns a plain HTTP 500 for most of
these and starts the execution only to fail it with `States.Runtime` for the last two.
+A mocked response with no attempt entries (`{}`) is not rejected. As in Step Functions
+Local, the execution starts and fails with `States.Runtime` only if the state that names
+it is entered. This keeps a generated mock file usable when the collection it was built
+from is empty and the state is never reached.
+
## Configuration
| Variable | Default | Description |
diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/SfnMockLoader.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/SfnMockLoader.java
index 2035aa62b3..078348ad21 100644
--- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/SfnMockLoader.java
+++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/SfnMockLoader.java
@@ -121,14 +121,15 @@ public class SfnMockLoader {
return root;
}
+ /**
+ * An entry with no attempts stays empty instead of being rejected. Step Functions Local
+ * treats it as a lookup that finds nothing, so the execution starts and fails with
+ * {@code States.Runtime} only if the state that names it is entered.
+ */
private List<MockedResponseStep> parseSteps(String responseKey, JsonNode responseNode) {
var steps = new ArrayList<MockedResponseStep>();
responseNode.fields().forEachRemaining(
entry -> steps.add(parseStep(responseKey, entry.getKey(), entry.getValue())));
- if (steps.isEmpty()) {
- throw new AwsException("ValidationException",
- "Mocked response '" + responseKey + "' must define at least one attempt entry", 400);
- }
steps.sort(Comparator.comparingInt(MockedResponseStep::fromAttempt));
return List.copyOf(steps);
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/SfnMockLoaderTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/SfnMockLoaderTest.java
index 7db290b797..99842f69e2 100644
--- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/SfnMockLoaderTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/SfnMockLoaderTest.java
@@ -145,6 +145,24 @@ class SfnMockLoaderTest {
assertTrue(e.getMessage().contains("Error"));
}
+ @Test
+ void acceptsMockedResponseWithNoAttemptEntries() throws IOException {
+ var testCase = loader("""
+ {
+ "StateMachines": {
+ "Test": { "TestCases": { "Case": { "Skipped": "Empty", "Call API": "Ok" } } }
+ },
+ "MockedResponses": {
+ "Empty": {},
+ "Ok": { "0": { "Return": { "StatusCode": 200 } } }
+ }
+ }
+ """).requireTestCase("Test", "Case");
+
+ assertTrue(testCase.stateResponses().get("Skipped").isEmpty());
+ assertEquals(1, testCase.stateResponses().get("Call API").size());
+ }
+
@Test
void reportsMissingFile() {
var loader = new SfnMockLoader(
diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsMockedServiceIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsMockedServiceIntegrationTest.java
index f495070341..e533f021a6 100644
--- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsMockedServiceIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/StepFunctionsMockedServiceIntegrationTest.java
@@ -35,6 +35,7 @@ class StepFunctionsMockedServiceIntegrationTest {
private static String mockSmArn;
private static String ddbSmArn;
private static String expressSmArn;
+ private static String skipSmArn;
@BeforeAll
static void configureRestAssured() {
@@ -111,6 +112,29 @@ class StepFunctionsMockedServiceIntegrationTest {
}
""".formatted(UNSUPPORTED_RESOURCE));
+ skipSmArn = createStateMachine("sfn-mock-skip-test", null, """
+ {
+ "StartAt": "Decide",
+ "States": {
+ "Decide": {
+ "Type": "Choice",
+ "Choices": [{"Variable": "$.go", "StringEquals": "skip", "Next": "Skip"}],
+ "Default": "Run"
+ },
+ "Skip": {
+ "Type": "Task",
+ "Resource": "%s",
+ "End": true
+ },
+ "Run": {
+ "Type": "Task",
+ "Resource": "%s",
+ "End": true
+ }
+ }
+ }
+ """.formatted(UNSUPPORTED_RESOURCE, UNSUPPORTED_RESOURCE));
+
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DDB_CONTENT_TYPE)
@@ -261,6 +285,32 @@ class StepFunctionsMockedServiceIntegrationTest {
assertTrue(describe.jsonPath().getString("cause").contains("Unsupported resource"));
}
+ @Test
+ @Order(10)
+ void emptyMockedResponseIsIgnoredWhenItsStateIsNotEntered() throws Exception {
+ // Verified against Step Functions Local 2.0.0. A mocked response with no attempt entries
+ // does not stop the execution from starting when its state is never entered.
+ var execArn = startExecution(skipSmArn + "#EmptyUnusedResponse", "{\"go\": \"run\"}");
+
+ var describe = waitForTerminalState(execArn);
+ assertEquals("SUCCEEDED", describe.jsonPath().getString("status"));
+
+ var output = mapper.readTree(describe.jsonPath().getString("output"));
+ assertEquals(200, output.path("StatusCode").asInt());
+ }
+
+ @Test
+ @Order(11)
+ void emptyMockedResponseFailsTheExecutionWhenItsStateIsEntered() {
+ // Verified against Step Functions Local 2.0.0. Entering the state fails the execution
+ // with States.Runtime rather than rejecting StartExecution.
+ var execArn = startExecution(skipSmArn + "#EmptyUnusedResponse", "{\"go\": \"skip\"}");
+
+ var describe = waitForTerminalState(execArn);
+ assertEquals("FAILED", describe.jsonPath().getString("status"));
+ assertEquals("States.Runtime", describe.jsonPath().getString("error"));
+ }
+
private static String createStateMachine(String name, String type, String definition) {
var typeField = type != null ? "\"type\": \"%s\",".formatted(type) : "";
var resp = given()
diff --git a/src/test/resources/fixtures/sfn-mock-config.json b/src/test/resources/fixtures/sfn-mock-config.json
index 3c60ec2d3d..f691b20399 100644
--- a/src/test/resources/fixtures/sfn-mock-config.json
+++ b/src/test/resources/fixtures/sfn-mock-config.json
@@ -11,6 +11,11 @@
"TestCases": {
"MockedApiRealDdb": { "Call API": "Ok200" }
}
+ },
+ "sfn-mock-skip-test": {
+ "TestCases": {
+ "EmptyUnusedResponse": { "Skip": "Empty", "Run": "Ok200" }
+ }
}
},
"MockedResponses": {
@@ -23,6 +28,7 @@
"ThrowThenOk": {
"0": { "Throw": { "Error": "ApiGateway.429", "Cause": "Too many requests" } },
"1-2": { "Return": { "StatusCode": 200, "ResponseBody": { "retried": true } } }
- }
+ },
+ "Empty": {}
}
}