|
3 | 3 | import com.fasterxml.jackson.databind.JsonNode; |
4 | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
5 | 5 | import com.xpeho.spring_boot_java_random_user.domain.entities.UserRequest; |
6 | | -import io.cucumber.datatable.DataTable; |
| 6 | +import feature.CucumberTypeConfig.FieldAssertion; |
7 | 7 | import io.cucumber.java.en.And; |
8 | 8 | import io.cucumber.java.en.Given; |
9 | 9 | import io.cucumber.java.en.Then; |
@@ -46,31 +46,30 @@ public void theResponseStatusShouldBe(int expectedStatus) { |
46 | 46 | } |
47 | 47 |
|
48 | 48 | @And("the user profile") |
49 | | - public void theUserProfile(DataTable table) throws Exception{ |
| 49 | + public void theUserProfile(List<FieldAssertion> assertions) throws Exception { |
50 | 50 | JsonNode body = objectMapper.readTree(latestResponse.getBody()); |
51 | | - List<List<String>> rows = table.asLists(String.class); |
52 | | - for (List<String> row : rows) { |
53 | | - String field = row.get(0).trim(); |
54 | | - String expected = row.get(1).trim(); |
55 | | - JsonNode valueNode = body.get(field); |
56 | | - assertNotNull(valueNode); |
57 | 51 |
|
58 | | - if ("<generated_id>".equals(expected)) { |
59 | | - createdUserId = valueNode.asLong(); |
60 | | - assertTrue(createdUserId > 0); |
61 | | - continue; |
62 | | - } |
63 | | - if ("<created_id>".equals(expected)) { |
64 | | - assertNotNull(createdUserId); |
65 | | - assertEquals(createdUserId.toString(), valueNode.asText()); |
66 | | - continue; |
| 52 | + for (FieldAssertion assertion : assertions) { |
| 53 | + JsonNode valueNode = body.get(assertion.field()); |
| 54 | + assertNotNull(valueNode, "Field '%s' not found in response".formatted(assertion.field())); |
| 55 | + |
| 56 | + switch (assertion.expected()) { |
| 57 | + case "<generated_id>" -> { |
| 58 | + createdUserId = valueNode.asLong(); |
| 59 | + assertTrue(createdUserId > 0); |
| 60 | + } |
| 61 | + case "<created_id>" -> { |
| 62 | + assertNotNull(createdUserId); |
| 63 | + assertEquals(createdUserId.toString(), valueNode.asText()); |
| 64 | + } |
| 65 | + default -> assertEquals(assertion.expected(), valueNode.asText(), |
| 66 | + "Mismatch on field '%s'".formatted(assertion.field())); |
67 | 67 | } |
68 | | - assertEquals(expected, valueNode.asText()); |
69 | 68 | } |
70 | 69 | } |
71 | 70 |
|
72 | | - @When("the client call to GET \\/random-users\\/{int}") |
73 | | - public void theClientCallToGetRandomUser(int id) { |
| 71 | + @When("the client call to GET \\/random-users\\/{userId}") |
| 72 | + public void theClientCallToGetRandomUser(Long id) { |
74 | 73 | executeGet("/random-users/" + id); |
75 | 74 | } |
76 | 75 |
|
|
0 commit comments