generated from devs-from-matrix/basic-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExampleStepDef.java
107 lines (94 loc) · 3.88 KB
/
ExampleStepDef.java
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
package packagename.cucumber;
import static org.assertj.core.api.Assertions.assertThat;
import io.cucumber.datatable.DataTable;
import io.cucumber.java8.En;
import io.cucumber.java8.HookNoArgsBody;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import packagename.domain.model.Example;
import packagename.domain.model.ExampleInfo;
import packagename.repository.dao.ExampleDao;
import packagename.repository.entity.ExampleEntity;
import packagename.rest.exception.ExampleExceptionResponse;
public class ExampleStepDef implements En {
private static final String LOCALHOST = "http://localhost:";
private static final String API_URI = "/api/v1/examples";
@LocalServerPort private int port;
private ResponseEntity responseEntity;
public ExampleStepDef(TestRestTemplate restTemplate, ExampleDao exampleDao) {
DataTableType(
(Map<String, String> row) ->
Example.builder()
.code(Long.parseLong(row.get("code")))
.description(row.get("description"))
.build());
DataTableType(
(Map<String, String> row) ->
ExampleEntity.builder()
.code(Long.parseLong(row.get("code")))
.description(row.get("description"))
.build());
Before((HookNoArgsBody) exampleDao::deleteAll);
After((HookNoArgsBody) exampleDao::deleteAll);
Given(
"the following examples exists in the library",
(DataTable dataTable) -> {
List<ExampleEntity> poems = dataTable.asList(ExampleEntity.class);
exampleDao.saveAll(poems);
});
When(
"user requests for all examples",
() -> {
String url = LOCALHOST + port + API_URI;
responseEntity = restTemplate.getForEntity(url, ExampleInfo.class);
});
When(
"user requests for examples by code {string}",
(String code) -> {
String url = LOCALHOST + port + API_URI + "/" + code;
responseEntity = restTemplate.getForEntity(url, Example.class);
});
When(
"user requests for examples by id {string} that does not exists",
(String code) -> {
String url = LOCALHOST + port + API_URI + "/" + code;
responseEntity = restTemplate.getForEntity(url, ExampleExceptionResponse.class);
});
Then(
"the user gets an exception {string}",
(String exception) -> {
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
Object body = responseEntity.getBody();
assertThat(body).isNotNull();
assertThat(body).isInstanceOf(ExampleExceptionResponse.class);
assertThat(((ExampleExceptionResponse) body).getMessage()).isEqualTo(exception);
});
Then(
"the user gets the following examples",
(DataTable dataTable) -> {
List<Example> expectedExamples = dataTable.asList(Example.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
Object body = responseEntity.getBody();
assertThat(body).isNotNull();
if (body instanceof ExampleInfo) {
assertThat(((ExampleInfo) body).getExamples())
.isNotEmpty()
.extracting("description")
.containsAll(
expectedExamples.stream()
.map(Example::getDescription)
.collect(Collectors.toList()));
} else if (body instanceof Example) {
assertThat(body)
.isNotNull()
.extracting("description")
.isEqualTo(expectedExamples.get(0).getDescription());
}
});
}
}