|
1 | 1 | package packagename.cucumber;
|
2 | 2 |
|
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.within; |
| 5 | + |
3 | 6 | import io.cucumber.datatable.DataTable;
|
4 | 7 | import io.cucumber.java8.En;
|
5 | 8 | import io.cucumber.java8.HookNoArgsBody;
|
| 9 | +import java.time.LocalDateTime; |
| 10 | +import java.time.temporal.ChronoUnit; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.stream.Collectors; |
6 | 14 | import org.springframework.boot.test.web.client.TestRestTemplate;
|
7 | 15 | import org.springframework.boot.test.web.server.LocalServerPort;
|
8 | 16 | import org.springframework.http.HttpStatus;
|
9 | 17 | import org.springframework.http.ResponseEntity;
|
10 | 18 | import packagename.domain.model.Example;
|
11 |
| -import packagename.domain.model.ExampleInfo; |
12 | 19 | import packagename.repository.dao.ExampleDao;
|
13 | 20 | import packagename.repository.entity.ExampleEntity;
|
14 |
| -import packagename.rest.exception.ExampleExceptionResponse; |
15 |
| - |
16 |
| -import java.util.List; |
17 |
| -import java.util.Map; |
18 |
| -import java.util.stream.Collectors; |
19 |
| - |
20 |
| -import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import packagename.rest.generated.model.ExampleInfo; |
| 22 | +import packagename.rest.generated.model.ProblemDetail; |
21 | 23 |
|
22 | 24 | public class ExampleStepDef implements En {
|
23 | 25 |
|
@@ -70,17 +72,27 @@ public ExampleStepDef(TestRestTemplate restTemplate, ExampleDao exampleDao) {
|
70 | 72 | "user requests for examples by id {string} that does not exists",
|
71 | 73 | (String code) -> {
|
72 | 74 | String url = LOCALHOST + port + API_URI + "/" + code;
|
73 |
| - responseEntity = restTemplate.getForEntity(url, ExampleExceptionResponse.class); |
| 75 | + responseEntity = restTemplate.getForEntity(url, ProblemDetail.class); |
74 | 76 | });
|
75 | 77 |
|
76 | 78 | Then(
|
77 | 79 | "the user gets an exception {string}",
|
78 | 80 | (String exception) -> {
|
79 | 81 | assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
80 |
| - Object body = responseEntity.getBody(); |
81 |
| - assertThat(body).isNotNull(); |
82 |
| - assertThat(body).isInstanceOf(ExampleExceptionResponse.class); |
83 |
| - assertThat(((ExampleExceptionResponse) body).getMessage()).isEqualTo(exception); |
| 82 | + var actualResponse = (ProblemDetail) responseEntity.getBody(); |
| 83 | + var expectedProblemDetail = ProblemDetail.builder() |
| 84 | + .type("https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404") |
| 85 | + .status(HttpStatus.NOT_FOUND.value()) |
| 86 | + .detail("Example with code 10000 does not exist") |
| 87 | + .instance("/api/v1/examples/10000") |
| 88 | + .title("Example not found") |
| 89 | + .build(); |
| 90 | + assertThat(actualResponse).isNotNull(); |
| 91 | + assertThat(actualResponse).usingRecursiveComparison() |
| 92 | + .ignoringFields("timestamp") |
| 93 | + .isEqualTo(expectedProblemDetail); |
| 94 | + assertThat(actualResponse.getTimestamp()).isCloseTo( |
| 95 | + LocalDateTime.now(), within(100L, ChronoUnit.SECONDS)); |
84 | 96 | });
|
85 | 97 |
|
86 | 98 | Then(
|
|
0 commit comments