-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStepDefinition.java
More file actions
141 lines (119 loc) · 4.92 KB
/
StepDefinition.java
File metadata and controls
141 lines (119 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package feature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xpeho.spring_boot_java_random_user.domain.entities.UserRequest;
import feature.CucumberTypeConfig.FieldAssertion;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class StepDefinition extends SpringIntegrationTest {
private final ObjectMapper objectMapper = new ObjectMapper();
private UserRequest payload;
private Long createdUserId;
@Given("a valid user payload for creation")
public void aValidUserPayloadForCreation() {
payload = new UserRequest(
"female",
"Emma",
"Stone",
"Ms",
"emma@example.com",
"0644444444",
"emma.jpg",
"FR"
);
}
@When("the client call to POST \\/random-users")
public void theClientCallToPostRandomUser() {
executePost("/random-users", payload);
}
@Then("the response status should be {int}")
public void theResponseStatusShouldBe(int expectedStatus) {
assertEquals(expectedStatus, latestResponse.getStatusCode().value());
}
@And("the user profile")
public void theUserProfile(List<FieldAssertion> assertions) throws Exception {
JsonNode body = objectMapper.readTree(latestResponse.getBody());
for (FieldAssertion assertion : assertions) {
JsonNode valueNode = body.get(assertion.field());
assertNotNull(valueNode, "Field '%s' not found in response".formatted(assertion.field()));
switch (assertion.expected()) {
case "<generated_id>" -> {
createdUserId = valueNode.asLong();
assertTrue(createdUserId > 0);
}
case "<created_id>" -> {
assertNotNull(createdUserId);
assertEquals(createdUserId.toString(), valueNode.asText());
}
default -> assertEquals(assertion.expected(), valueNode.asText(),
"Mismatch on field '%s'".formatted(assertion.field()));
}
}
}
@When("the client call to GET \\/random-users\\/{int}")
public void theClientCallToGetRandomUser(int id) {
executeGet("/random-users/" + id);
}
@When("the client call to GET the created user")
public void theClientCallToGetTheCreatedUser() {
assertNotNull(createdUserId, "No user was created before this step");
executeGet("/random-users/" + createdUserId);
}
@When("the client call to GET \\/random-users")
public void theClientCallToGetRandomUsers() {
executeGet("/random-users");
}
@When("the client call to GET \\/random-users with page {int} and size {int}")
public void theClientCallToGetRandomUsersWithPageAndSize(int page, int size) {
executeGet("/random-users?page=" + page + "&size=" + size);
}
@When("the client call to DELETE \\/random-users\\/{int}")
public void theClientCallToDeleteRandomUser(int id) {
executeDelete("/random-users/" + id);
}
@When("the client call to DELETE the created user")
public void theClientCallToDeleteTheCreatedUser() {
assertNotNull(createdUserId, "No user was created before this step");
executeDelete("/random-users/" + createdUserId);
}
@Given("a valid user payload for update")
public void aValidUserPayloadForUpdate() {
payload = new UserRequest(
"male",
"John",
"Doe",
"Mr",
"john.doe@example.com",
"0611111111",
"john.jpg",
"US"
);
}
@When("the client call to PUT the created user")
public void theClientCallToPutTheCreatedUser() {
assertNotNull(createdUserId, "No user was created before this step");
executePut("/random-users/" + createdUserId, payload);
}
@When("the client call to PUT \\/random-users\\/{int}")
public void theClientCallToPutRandomUser(int id) {
executePut("/random-users/" + id, payload);
}
@And("the response contains a list of users")
public void theResponseContainsAListOfUsers() throws Exception {
JsonNode body = objectMapper.readTree(latestResponse.getBody());
assertNotNull(body.get("data"));
assertTrue(body.get("data").isArray());
}
@And("the response contains {int} users")
public void theResponseContainsUsers(int expectedSize) throws Exception {
JsonNode body = objectMapper.readTree(latestResponse.getBody());
assertNotNull(body.get("data"));
assertEquals(expectedSize, body.get("data").size());
}
}