|
| 1 | +package feature; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.xpeho.spring_boot_java_random_user.domain.entities.UserRequest; |
| 6 | +import io.cucumber.datatable.DataTable; |
| 7 | +import io.cucumber.java.en.And; |
| 8 | +import io.cucumber.java.en.Given; |
| 9 | +import io.cucumber.java.en.Then; |
| 10 | +import io.cucumber.java.en.When; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | + |
| 18 | +public class StepDefinition extends SpringIntegrationTest { |
| 19 | + |
| 20 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 21 | + private UserRequest payload; |
| 22 | + private Long createdUserId; |
| 23 | + |
| 24 | + @Given("a valid user payload for creation") |
| 25 | + public void aValidUserPayloadForCreation() { |
| 26 | + payload = new UserRequest( |
| 27 | + "female", |
| 28 | + "Emma", |
| 29 | + "Stone", |
| 30 | + "Ms", |
| 31 | + "emma@example.com", |
| 32 | + "0644444444", |
| 33 | + "emma.jpg", |
| 34 | + "FR" |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + @When("the client call to POST \\/random-users") |
| 39 | + public void theClientCallToPostRandomUser() { |
| 40 | + executePost("/random-users", payload); |
| 41 | + } |
| 42 | + |
| 43 | + @Then("the response status should be {int}") |
| 44 | + public void theResponseStatusShouldBe(int expectedStatus) { |
| 45 | + assertEquals(expectedStatus, latestResponse.getStatusCode().value()); |
| 46 | + } |
| 47 | + |
| 48 | + @And("the user profile") |
| 49 | + public void theUserProfile(DataTable table) throws Exception{ |
| 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 | + |
| 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; |
| 67 | + } |
| 68 | + assertEquals(expected, valueNode.asText()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @When("the client call to GET \\/random-users\\/{int}") |
| 73 | + public void theClientCallToGetRandomUser(int id) { |
| 74 | + executeGet("/random-users/" + id); |
| 75 | + } |
| 76 | + |
| 77 | + @When("the client call to GET the created user") |
| 78 | + public void theClientCallToGetTheCreatedUser() { |
| 79 | + assertNotNull(createdUserId, "No user was created before this step"); |
| 80 | + executeGet("/random-users/" + createdUserId); |
| 81 | + } |
| 82 | +} |
0 commit comments