Skip to content

Commit d76307c

Browse files
Added test for RewindInstanceHttpAPI and addressed review comments
1 parent f2e6fc2 commit d76307c

File tree

2 files changed

+78
-21
lines changed

2 files changed

+78
-21
lines changed

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

Lines changed: 14 additions & 4 deletions
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

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121

2222
@Tag("e2e")
2323
public class EndToEndTests {
24-
private static final String hostHealthPingPath = "/admin/host/ping";
25-
private static final String startOrchestrationPath = "/api/StartOrchestration";
26-
private static final String approvalWorkFlow = "/api/ApprovalWorkflowOrchestration";
27-
private static final String rewindInstance = "/api/RewindInstance";
24+
private static final String hostHealthPingUrl = "/admin/host/ping";
25+
private static final String startOrchestrationUrl = "/api/StartOrchestration";
26+
private static final String startApprovalWorkflowUrl = "/api/ApprovalWorkflowOrchestration";
27+
private static final String rewindInstanceFunctionUrl = "/api/RewindInstance";
28+
private static final String resetApprovalUrl = "/api/ResetApprovalFlag";
2829

2930
@Order(1)
3031
@Test
@@ -80,6 +81,15 @@ public void retryTestSuccess() throws InterruptedException {
8081
String statusQueryGetUri = jsonPath.get("statusQueryGetUri");
8182
boolean pass = pollingCheck(statusQueryGetUri, "Completed", null, Duration.ofSeconds(10));
8283
assertTrue(pass);
84+
String runtimeStatus = null;
85+
for (int i = 0; i < 15; i++) {
86+
Response statusResponse = get(statusQueryGetUri);
87+
runtimeStatus = statusResponse.jsonPath().get("runtimeStatus");
88+
if (!"Completed".equals(runtimeStatus)) {
89+
Thread.sleep(1000);
90+
} else break;
91+
}
92+
assertEquals("Completed", runtimeStatus);
8393
}
8494

8595
@Test
@@ -209,35 +219,72 @@ private boolean pollingCheck(String statusQueryGetUri,
209219

210220
@Order(2)
211221
@Test
212-
public void testRewindInstanceAPI() throws InterruptedException {
213-
Response response = post(approvalWorkFlow);
214-
JsonPath rewindTestJsonPath = response.jsonPath();
222+
public void testRewindInstanceJavaAPI() throws InterruptedException {
223+
Response response = post(startApprovalWorkflowUrl);
224+
JsonPath startOrchestrationResponseJson = response.jsonPath();
215225

216226
// Wait for the ApprovalWorkflowOrchestration to fail
217227
Thread.sleep(3000);
218228

219-
String instanceId = rewindTestJsonPath.get("id");
220-
String statusQueryGetUri = rewindTestJsonPath.get("statusQueryGetUri");
229+
String instanceId = startOrchestrationResponseJson.get("id");
230+
String statusQueryGetUri = startOrchestrationResponseJson.get("statusQueryGetUri");
221231
Response statusResponse = get(statusQueryGetUri);
222-
String runTimeStatus = statusResponse.jsonPath().get("runtimeStatus");
223-
assertEquals("Failed", runTimeStatus);
232+
String runtimeStatus = statusResponse.jsonPath().get("runtimeStatus");
233+
assertEquals("Failed", runtimeStatus);
224234

225-
// Rewind the instance
226-
String rewindPostUri = rewindInstance + "?instanceId=" + instanceId;
227-
response = post(rewindPostUri);
235+
// Rewind the instance using Java API
236+
String rewindInstanceUrl = rewindInstanceFunctionUrl + "?instanceId=" + instanceId;
237+
response = post(rewindInstanceUrl);
228238
assertEquals("Failed orchestration instance is scheduled for rewind.", response.toString());
229239

230240
// Wait for orchestration to rewind and complete
231241
Thread.sleep(3000);
232242

233243
for (int i = 0; i < 5; i++) {
234244
statusResponse = get(statusQueryGetUri);
235-
runTimeStatus = statusResponse.jsonPath().get("runtimeStatus");
236-
if (!"Completed".equals(runTimeStatus)) {
245+
runtimeStatus = statusResponse.jsonPath().get("runtimeStatus");
246+
if (!"Completed".equals(runtimeStatus)) {
237247
Thread.sleep(1000);
238248
} else break;
239249
}
240-
assertEquals("Completed", runTimeStatus);
250+
assertEquals("Completed", runtimeStatus);
251+
252+
// Reset approval for other test cases
253+
post(resetApprovalUrl);
254+
}
255+
256+
@Order(3)
257+
@Test
258+
public void testRewindInstanceHttpAPI() throws InterruptedException {
259+
Response response = post(startApprovalWorkflowUrl);
260+
JsonPath startOrchestrationResponseJson = response.jsonPath();
261+
262+
// Wait for the ApprovalWorkflowOrchestration to fail
263+
Thread.sleep(3000);
264+
265+
String statusQueryGetUri = startOrchestrationResponseJson.get("statusQueryGetUri");
266+
Response statusResponse = get(statusQueryGetUri);
267+
String runtimeStatus = statusResponse.jsonPath().get("runtimeStatus");
268+
assertEquals("Failed", runtimeStatus);
269+
270+
// Rewind the instance using Http API
271+
String rewindPostUri = startOrchestrationResponseJson.get("rewindPostUri");
272+
post(rewindPostUri);
273+
274+
// Wait for orchestration to rewind and complete
275+
Thread.sleep(3000);
276+
277+
for (int i = 0; i < 5; i++) {
278+
statusResponse = get(statusQueryGetUri);
279+
runtimeStatus = statusResponse.jsonPath().get("runtimeStatus");
280+
if (!"Completed".equals(runtimeStatus)) {
281+
Thread.sleep(1000);
282+
} else break;
283+
}
284+
assertEquals("Completed", runtimeStatus);
285+
286+
// Reset approval for other test cases
287+
post(resetApprovalUrl);
241288
}
242289
}
243290

0 commit comments

Comments
 (0)