|
| 1 | +package {{cookiecutter.package_name}}.rest; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.within; |
| 5 | +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; |
| 6 | + |
| 7 | +import java.time.LocalDateTime; |
| 8 | +import java.time.temporal.ChronoUnit; |
| 9 | +import java.util.List; |
| 10 | +import org.junit.jupiter.api.DisplayName; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 13 | +import org.mockito.Mockito; |
| 14 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 17 | +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
| 18 | +import org.springframework.boot.test.context.SpringBootTest; |
| 19 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 20 | +import org.springframework.boot.test.web.server.LocalServerPort; |
| 21 | +import org.springframework.http.HttpStatus; |
| 22 | +import {{cookiecutter.package_name}}.domain.exception.{{cookiecutter.domain_capitalized}}NotFoundException; |
| 23 | +import {{cookiecutter.package_name}}.domain.model.{{cookiecutter.domain_capitalized}}; |
| 24 | +import {{cookiecutter.package_name}}.domain.port.Request{{cookiecutter.domain_capitalized}}; |
| 25 | +import {{cookiecutter.package_name}}.rest.generated.model.{{cookiecutter.domain_capitalized}}Info; |
| 26 | +import {{cookiecutter.package_name}}.rest.generated.model.ProblemDetail; |
| 27 | + |
| 28 | +@ExtendWith(MockitoExtension.class) |
| 29 | +@SpringBootTest(classes = {{cookiecutter.domain_capitalized}}RestAdapterApplication.class, webEnvironment = RANDOM_PORT) |
| 30 | +@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) |
| 31 | +public class {{cookiecutter.domain_capitalized}}ResourceTest { |
| 32 | + |
| 33 | + private static final String LOCALHOST = "http://localhost:"; |
| 34 | + private static final String API_URI = "/api/v1/{{cookiecutter.domain_plural}}"; |
| 35 | + @LocalServerPort private int port; |
| 36 | + @Autowired private TestRestTemplate restTemplate; |
| 37 | + @Autowired private Request{{cookiecutter.domain_capitalized}} request{{cookiecutter.domain_capitalized}}; |
| 38 | + |
| 39 | + @Test |
| 40 | + @DisplayName("should start the rest adapter application") |
| 41 | + public void startup() { |
| 42 | + assertThat(Boolean.TRUE).isTrue(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + @DisplayName("should give {{cookiecutter.domain_plural}} when asked for {{cookiecutter.domain_plural}} with the support of domain stub") |
| 47 | + public void obtain{{cookiecutter.domain_plural_capitalized}}FromDomainStub() { |
| 48 | + // Given |
| 49 | + var {{cookiecutter.domain}} = {{cookiecutter.domain_capitalized}}.builder().code(1L).description("Johnny Johnny Yes Papa !!").build(); |
| 50 | + Mockito.lenient().when(request{{cookiecutter.domain_capitalized}}.get{{cookiecutter.domain_plural_capitalized}}()).thenReturn(List.of({{cookiecutter.domain}})); |
| 51 | + // When |
| 52 | + var url = LOCALHOST + port + API_URI; |
| 53 | + var responseEntity = restTemplate.getForEntity(url, {{cookiecutter.domain_capitalized}}Info.class); |
| 54 | + // Then |
| 55 | + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 56 | + assertThat(responseEntity.getBody()).isNotNull(); |
| 57 | + assertThat(responseEntity.getBody().get{{cookiecutter.domain_plural_capitalized}}()) |
| 58 | + .isNotEmpty() |
| 59 | + .extracting("description") |
| 60 | + .contains("Johnny Johnny Yes Papa !!"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName( |
| 65 | + "should give the {{cookiecutter.domain}} when asked for an {{cookiecutter.domain}} by code with the support of domain stub") |
| 66 | + public void obtain{{cookiecutter.domain_capitalized}}ByCodeFromDomainStub() { |
| 67 | + // Given |
| 68 | + var code = 1L; |
| 69 | + var description = "Johnny Johnny Yes Papa !!"; |
| 70 | + var {{cookiecutter.domain}} = {{cookiecutter.domain_capitalized}}.builder().code(code).description(description).build(); |
| 71 | + Mockito.lenient().when(request{{cookiecutter.domain_capitalized}}.get{{cookiecutter.domain_capitalized}}ByCode(code)).thenReturn({{cookiecutter.domain}}); |
| 72 | + // When |
| 73 | + var url = LOCALHOST + port + API_URI + "/" + code; |
| 74 | + var responseEntity = restTemplate.getForEntity(url, {{cookiecutter.domain_capitalized}}.class); |
| 75 | + // Then |
| 76 | + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 77 | + assertThat(responseEntity.getBody()).isNotNull(); |
| 78 | + assertThat(responseEntity.getBody()).isEqualTo({{cookiecutter.domain}}); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @DisplayName( |
| 83 | + "should give exception when asked for an {{cookiecutter.domain}} by code that does not exists with the support of domain stub") |
| 84 | + public void shouldGiveExceptionWhenAskedForAn{{cookiecutter.domain_capitalized}}ByCodeFromDomainStub() { |
| 85 | + // Given |
| 86 | + var code = -1000L; |
| 87 | + Mockito.lenient() |
| 88 | + .when(request{{cookiecutter.domain_capitalized}}.get{{cookiecutter.domain_capitalized}}ByCode(code)) |
| 89 | + .thenThrow(new {{cookiecutter.domain_capitalized}}NotFoundException(code)); |
| 90 | + // When |
| 91 | + var url = LOCALHOST + port + API_URI + "/" + code; |
| 92 | + var responseEntity = restTemplate.getForEntity(url, ProblemDetail.class); |
| 93 | + // Then |
| 94 | + var expectedProblemDetail = |
| 95 | + ProblemDetail.builder() |
| 96 | + .type("https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404") |
| 97 | + .status(HttpStatus.NOT_FOUND.value()) |
| 98 | + .detail("{{cookiecutter.domain_capitalized}} with code -1000 does not exist") |
| 99 | + .instance("/api/v1/{{cookiecutter.domain_plural}}/-1000") |
| 100 | + .title("{{cookiecutter.domain_capitalized}} not found") |
| 101 | + .build(); |
| 102 | + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); |
| 103 | + assertThat(responseEntity.getBody()).isNotNull(); |
| 104 | + assertThat(responseEntity.getBody()) |
| 105 | + .usingRecursiveComparison() |
| 106 | + .ignoringFields("timestamp") |
| 107 | + .isEqualTo(expectedProblemDetail); |
| 108 | + assertThat(responseEntity.getBody().getTimestamp()) |
| 109 | + .isCloseTo(LocalDateTime.now(), within(100L, ChronoUnit.SECONDS)); |
| 110 | + } |
| 111 | +} |
0 commit comments