Skip to content

Commit 786d09e

Browse files
committed
updating tests to validate output from APIs
1 parent 4a9a829 commit 786d09e

File tree

5 files changed

+61
-92
lines changed

5 files changed

+61
-92
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@
665665
<id>integration-tests</id>
666666
<modules>
667667
<module>sdk-tests</module>
668+
<module>spring-boot-examples</module>
668669
</modules>
669670
<build>
670671
<plugins>

spring-boot-examples/workflows/multi-app/orchestrator/src/main/java/io/dapr/springboot/examples/orchestrator/CustomersRestController.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ public String customerNotification(@RequestBody Customer customer) {
7474
}
7575
}
7676

77+
/**
78+
* Request customer workflow instance status.
79+
* @param customer associated with a workflow instance
80+
* @return the workflow instance status for a given customer
81+
*/
82+
@PostMapping("/customers/status")
83+
public String getCustomerStatus(@RequestBody Customer customer) {
84+
logger.info("Customer status requested: {}", customer.getCustomerName());
85+
String workflowIdForCustomer = customersWorkflows.get(customer.getCustomerName());
86+
if (workflowIdForCustomer == null || workflowIdForCustomer.isEmpty()) {
87+
return "N/A";
88+
} else {
89+
WorkflowInstanceStatus instanceState = daprWorkflowClient.getInstanceState(workflowIdForCustomer, true);
90+
assert instanceState != null;
91+
return "Workflow for Customer: " + customer.getCustomerName() + " is " + instanceState.getRuntimeStatus().name();
92+
}
93+
}
94+
7795
/**
7896
* Request customer output.
7997
* @param customer associated with a workflow instance

spring-boot-examples/workflows/multi-app/orchestrator/src/test/java/io/dapr/springboot/examples/orchestrator/OrchestratorAppIT.java

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import org.springframework.boot.test.context.SpringBootTest;
2121

2222
import java.io.IOException;
23+
import java.time.Duration;
2324

2425
import static io.restassured.RestAssured.given;
2526
import static org.awaitility.Awaitility.await;
2627
import static org.hamcrest.CoreMatchers.is;
2728
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
2830

2931
@SpringBootTest(classes = {TestOrchestratorApplication.class, DaprTestContainersConfig.class, CustomersRestController.class},
3032
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
@@ -42,44 +44,60 @@ void setUp() {
4244
@Test
4345
void testCustomersWorkflows() throws InterruptedException, IOException {
4446

47+
// Create a new workflow instance for a given customer
4548
given().contentType(ContentType.JSON)
4649
.body("{\"customerName\": \"salaboy\"}")
4750
.when()
4851
.post("/customers")
4952
.then()
5053
.statusCode(200);
5154

52-
53-
// await().atMost(Duration.ofSeconds(15))
54-
// .until(customerStore.getCustomers()::size, equalTo(1));
55-
// io.dapr.springboot.examples.orchestrator.Customer customer = customerStore.getCustomer("salaboy");
56-
// assertEquals(true, customer.isInCustomerDB());
57-
58-
// String workflowId = customer.getWorkflowId();
59-
60-
Thread.sleep(1000);
61-
55+
// Wait for the workflow instance to be running by checking the status
56+
await().atMost(Duration.ofSeconds(5)).until(() ->
57+
{
58+
String workflowStatus = given().contentType(ContentType.JSON)
59+
.body("{\"customerName\": \"salaboy\" }")
60+
.when()
61+
.post("/customers/status")
62+
.then()
63+
.statusCode(200)
64+
.extract().asString();
65+
return workflowStatus.equals("Workflow for Customer: salaboy is RUNNING");
66+
}
67+
);
68+
69+
// Raise an external event to move the workflow forward
6270
given().contentType(ContentType.JSON)
6371
.body("{\"customerName\": \"salaboy\" }")
6472
.when()
6573
.post("/customers/followup")
6674
.then()
6775
.statusCode(200);
6876

69-
Thread.sleep(1000);
70-
71-
given().contentType(ContentType.JSON)
77+
// Wait for the workflow instance to be completed by checking the status
78+
await().atMost(Duration.ofSeconds(5)).until(() ->
79+
{
80+
String workflowStatus = given().contentType(ContentType.JSON)
81+
.body("{\"customerName\": \"salaboy\" }")
82+
.when()
83+
.post("/customers/status")
84+
.then()
85+
.statusCode(200).extract().asString();
86+
return workflowStatus.equals("Workflow for Customer: salaboy is COMPLETED");
87+
}
88+
);
89+
90+
// Get the customer after running all the workflow activities
91+
Customer customer = given().contentType(ContentType.JSON)
7292
.body("{\"customerName\": \"salaboy\" }")
7393
.when()
7494
.post("/customers/output")
7595
.then()
76-
.statusCode(200);
77-
// }
78-
//
79-
// assertEquals(1, customerStore.getCustomers().size());
80-
//
81-
// await().atMost(Duration.ofSeconds(10))
82-
// .until(customerStore.getCustomer("salaboy")::isFollowUp, equalTo(true));
96+
.statusCode(200).extract().as(Customer.class);
97+
98+
assertTrue(customer.isInCustomerDB());
99+
assertTrue(customer.isFollowUp());
100+
83101

84102
}
85103

spring-boot-examples/workflows/multi-app/worker-one/src/test/java/io/dapr/springboot/examples/workerone/WorkerOneAppTests.java

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,50 +33,16 @@
3333
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
3434
class WorkerOneAppTests {
3535

36-
37-
@Autowired
38-
private DaprClient daprClient;
39-
40-
@Autowired
41-
private DaprContainer daprContainer;
42-
4336
@BeforeEach
4437
void setUp() {
4538
RestAssured.baseURI = "http://localhost:" + 8081;
4639
org.testcontainers.Testcontainers.exposeHostPorts(8081);
4740

4841
}
4942

50-
51-
5243
@Test
53-
void testCustomersWorkflows() throws InterruptedException, IOException {
54-
55-
given().contentType(ContentType.JSON)
56-
.body("{\"customerName\": \"salaboy\"}")
57-
.when()
58-
.post("/customers")
59-
.then()
60-
.statusCode(200);
61-
62-
63-
// await().atMost(Duration.ofSeconds(15))
64-
// .until(customerStore.getCustomers()::size, equalTo(1));
65-
// io.dapr.springboot.examples.orchestrator.Customer customer = customerStore.getCustomer("salaboy");
66-
// assertEquals(true, customer.isInCustomerDB());
67-
// String workflowId = customer.getWorkflowId();
68-
// given().contentType(ContentType.JSON)
69-
// .body("{ \"workflowId\": \"" + workflowId + "\",\"customerName\": \"salaboy\" }")
70-
// .when()
71-
// .post("/customers/followup")
72-
// .then()
73-
// .statusCode(200);
74-
//
75-
// assertEquals(1, customerStore.getCustomers().size());
76-
//
77-
// await().atMost(Duration.ofSeconds(10))
78-
// .until(customerStore.getCustomer("salaboy")::isFollowUp, equalTo(true));
79-
44+
void testWorkerOne() {
45+
//Test the logic of the worker one
8046
}
8147

8248
}

spring-boot-examples/workflows/multi-app/worker-two/src/test/java/io/dapr/springboot/examples/workertwo/WorkerTwoAppTests.java

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,50 +33,16 @@
3333
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
3434
class WorkerTwoAppTests {
3535

36-
37-
@Autowired
38-
private DaprClient daprClient;
39-
40-
@Autowired
41-
private DaprContainer daprContainer;
42-
4336
@BeforeEach
4437
void setUp() {
4538
RestAssured.baseURI = "http://localhost:" + 8082;
4639
org.testcontainers.Testcontainers.exposeHostPorts(8082);
4740

4841
}
4942

50-
51-
5243
@Test
53-
void testCustomersWorkflows() throws InterruptedException, IOException {
54-
55-
given().contentType(ContentType.JSON)
56-
.body("{\"customerName\": \"salaboy\"}")
57-
.when()
58-
.post("/customers")
59-
.then()
60-
.statusCode(200);
61-
62-
63-
// await().atMost(Duration.ofSeconds(15))
64-
// .until(customerStore.getCustomers()::size, equalTo(1));
65-
// io.dapr.springboot.examples.orchestrator.Customer customer = customerStore.getCustomer("salaboy");
66-
// assertEquals(true, customer.isInCustomerDB());
67-
// String workflowId = customer.getWorkflowId();
68-
// given().contentType(ContentType.JSON)
69-
// .body("{ \"workflowId\": \"" + workflowId + "\",\"customerName\": \"salaboy\" }")
70-
// .when()
71-
// .post("/customers/followup")
72-
// .then()
73-
// .statusCode(200);
74-
//
75-
// assertEquals(1, customerStore.getCustomers().size());
76-
//
77-
// await().atMost(Duration.ofSeconds(10))
78-
// .until(customerStore.getCustomer("salaboy")::isFollowUp, equalTo(true));
79-
44+
void testWorkerTwo() {
45+
//Test the logic of the worker two
8046
}
8147

8248
}

0 commit comments

Comments
 (0)