Skip to content

Commit bc1954b

Browse files
Added test for RewindInstanceHttpAPI and addressed review comments
1 parent 54ec62d commit bc1954b

File tree

2 files changed

+77
-29
lines changed

2 files changed

+77
-29
lines changed

samples-azure-functions/src/main/java/com/functions/RewindInstance.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.functions;
22

3-
import com.microsoft.azure.functions.ExecutionContext;
4-
import com.microsoft.azure.functions.HttpMethod;
5-
import com.microsoft.azure.functions.HttpRequestMessage;
6-
import com.microsoft.azure.functions.HttpResponseMessage;
3+
import com.microsoft.azure.functions.*;
74
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
85
import com.microsoft.azure.functions.annotation.FunctionName;
96
import com.microsoft.azure.functions.annotation.HttpTrigger;
@@ -82,4 +79,17 @@ public String rewindInstance(
8279
client.rewindInstance(instanceId, reason);
8380
return "Failed orchestration instance is scheduled for rewind.";
8481
}
82+
83+
/**
84+
* This HTTP-triggered function resets the approvalFlag variable for testing purposes.
85+
*/
86+
@FunctionName("ResetApproval")
87+
public static HttpResponseMessage resetApproval(
88+
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS)
89+
HttpRequestMessage<Optional<String>> request,
90+
final ExecutionContext context) {
91+
context.getLogger().info("ResetApproval function invoked.");
92+
approvalFlag = 0;
93+
return request.createResponseBuilder(HttpStatus.OK).body(approvalFlag).build();
94+
}
8595
}

samples-azure-functions/src/test/java/com/functions/EndToEndTests.java

+63-25
Original file line numberDiff line numberDiff line change
@@ -12,63 +12,101 @@
1212

1313
@Tag("e2e")
1414
public class EndToEndTests {
15-
private static final String hostHealthPingPath = "/admin/host/ping";
16-
private static final String startOrchestrationPath = "/api/StartOrchestration";
17-
private static final String approvalWorkFlow = "/api/ApprovalWorkflowOrchestration";
18-
private static final String rewindInstance = "/api/RewindInstance";
15+
private static final String hostHealthPingUrl = "/admin/host/ping";
16+
private static final String startOrchestrationUrl = "/api/StartOrchestration";
17+
private static final String startApprovalWorkflowUrl = "/api/ApprovalWorkflowOrchestration";
18+
private static final String rewindInstanceFunctionUrl = "/api/RewindInstance";
19+
private static final String resetApprovalUrl = "/api/ResetApprovalFlag";
1920

2021
@Order(1)
2122
@Test
2223
public void setupHost() {
23-
post(hostHealthPingPath).then().statusCode(200);
24+
post(hostHealthPingUrl).then().statusCode(200);
2425
}
2526

2627
@Test
2728
public void basicChain() throws InterruptedException {
28-
Response response = post(startOrchestrationPath);
29-
JsonPath jsonPath = response.jsonPath();
30-
String statusQueryGetUri = jsonPath.get("statusQueryGetUri");
31-
String runTimeStatus = null;
29+
Response response = post(startOrchestrationUrl);
30+
JsonPath startOrchestrationResponseJson = response.jsonPath();
31+
String statusQueryGetUri = startOrchestrationResponseJson.get("statusQueryGetUri");
32+
String runtimeStatus = null;
3233
for (int i = 0; i < 15; i++) {
3334
Response statusResponse = get(statusQueryGetUri);
34-
runTimeStatus = statusResponse.jsonPath().get("runtimeStatus");
35-
if (!"Completed".equals(runTimeStatus)) {
35+
runtimeStatus = statusResponse.jsonPath().get("runtimeStatus");
36+
if (!"Completed".equals(runtimeStatus)) {
3637
Thread.sleep(1000);
3738
} else break;
3839
}
39-
assertEquals("Completed", runTimeStatus);
40+
assertEquals("Completed", runtimeStatus);
4041
}
4142

4243
@Order(2)
4344
@Test
44-
public void testRewindInstanceAPI() throws InterruptedException {
45-
Response response = post(approvalWorkFlow);
46-
JsonPath rewindTestJsonPath = response.jsonPath();
45+
public void testRewindInstanceJavaAPI() throws InterruptedException {
46+
Response response = post(startApprovalWorkflowUrl);
47+
JsonPath startOrchestrationResponseJson = response.jsonPath();
4748

4849
// Wait for the ApprovalWorkflowOrchestration to fail
4950
Thread.sleep(3000);
5051

51-
String instanceId = rewindTestJsonPath.get("id");
52-
String statusQueryGetUri = rewindTestJsonPath.get("statusQueryGetUri");
52+
String instanceId = startOrchestrationResponseJson.get("id");
53+
String statusQueryGetUri = startOrchestrationResponseJson.get("statusQueryGetUri");
5354
Response statusResponse = get(statusQueryGetUri);
54-
String runTimeStatus = statusResponse.jsonPath().get("runtimeStatus");
55-
assertEquals("Failed", runTimeStatus);
55+
String runtimeStatus = statusResponse.jsonPath().get("runtimeStatus");
56+
assertEquals("Failed", runtimeStatus);
5657

57-
// Rewind the instance
58-
String rewindPostUri = rewindInstance + "?instanceId=" + instanceId;
59-
response = post(rewindPostUri);
58+
// Rewind the instance using Java API
59+
String rewindInstanceUrl = rewindInstanceFunctionUrl + "?instanceId=" + instanceId;
60+
response = post(rewindInstanceUrl);
6061
assertEquals("Failed orchestration instance is scheduled for rewind.", response.toString());
6162

6263
// Wait for orchestration to rewind and complete
6364
Thread.sleep(3000);
6465

6566
for (int i = 0; i < 5; i++) {
6667
statusResponse = get(statusQueryGetUri);
67-
runTimeStatus = statusResponse.jsonPath().get("runtimeStatus");
68-
if (!"Completed".equals(runTimeStatus)) {
68+
runtimeStatus = statusResponse.jsonPath().get("runtimeStatus");
69+
if (!"Completed".equals(runtimeStatus)) {
6970
Thread.sleep(1000);
7071
} else break;
7172
}
72-
assertEquals("Completed", runTimeStatus);
73+
assertEquals("Completed", runtimeStatus);
74+
75+
// Reset approval for other test cases
76+
post(resetApprovalUrl);
77+
}
78+
79+
@Order(3)
80+
@Test
81+
public void testRewindInstanceHttpAPI() throws InterruptedException {
82+
Response response = post(startApprovalWorkflowUrl);
83+
JsonPath startOrchestrationResponseJson = response.jsonPath();
84+
85+
// Wait for the ApprovalWorkflowOrchestration to fail
86+
Thread.sleep(3000);
87+
88+
String statusQueryGetUri = startOrchestrationResponseJson.get("statusQueryGetUri");
89+
Response statusResponse = get(statusQueryGetUri);
90+
String runtimeStatus = statusResponse.jsonPath().get("runtimeStatus");
91+
assertEquals("Failed", runtimeStatus);
92+
93+
// Rewind the instance using Http API
94+
String rewindPostUri = startOrchestrationResponseJson.get("rewindPostUri");
95+
post(rewindPostUri);
96+
97+
// Wait for orchestration to rewind and complete
98+
Thread.sleep(3000);
99+
100+
for (int i = 0; i < 5; i++) {
101+
statusResponse = get(statusQueryGetUri);
102+
runtimeStatus = statusResponse.jsonPath().get("runtimeStatus");
103+
if (!"Completed".equals(runtimeStatus)) {
104+
Thread.sleep(1000);
105+
} else break;
106+
}
107+
assertEquals("Completed", runtimeStatus);
108+
109+
// Reset approval for other test cases
110+
post(resetApprovalUrl);
73111
}
74112
}

0 commit comments

Comments
 (0)