| | |
| | | package de.gedoplan.seminar.sbt.sbtrestexercise; |
| | | |
| | | import de.gedoplan.seminar.sbt.sbtrestexercise.domain.Person; |
| | | import org.junit.jupiter.api.BeforeEach; |
| | | import org.junit.jupiter.api.Test; |
| | | import org.springframework.boot.test.context.SpringBootTest; |
| | | import org.springframework.boot.test.web.server.LocalServerPort; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.web.reactive.function.client.WebClient; |
| | | |
| | | import java.util.List; |
| | | |
| | | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| | | public class PersonenResourceTest { |
| | |
| | | |
| | | @BeforeEach |
| | | public void init() { |
| | | client = WebClient.create("http://localhost:"+port+"/personen"); |
| | | } |
| | | |
| | | @Test |
| | | public void testGetPersonen() { |
| | | List<Person> personen = client.get() |
| | | .retrieve().bodyToFlux(Person.class) |
| | | .collectList().block(); |
| | | System.out.printf("Found %d talks:\n", personen.size()); |
| | | personen.forEach(talk -> System.out.println(" "+talk)); |
| | | } |
| | | |
| | | @Test |
| | | public void testGetPersonById() { |
| | | Person person = client.get() |
| | | .uri(ub -> ub.path("/{id}").build(1)) |
| | | .retrieve().bodyToMono(Person.class) |
| | | .block(); |
| | | System.out.println(person); |
| | | } |
| | | |
| | | @Test |
| | | public void testCreateTalk() { |
| | | Person person = new Person("Duck","Donald"); |
| | | |
| | | ResponseEntity<String> response = client.post() |
| | | .contentType(MediaType.APPLICATION_JSON) |
| | | .bodyValue(person) |
| | | .retrieve() |
| | | .toEntity(String.class) |
| | | .block(); |
| | | |
| | | System.out.printf("Response status: %03d %s\n", response.getStatusCode().value(), response.getStatusCode().getReasonPhrase()); |
| | | String message = switch (response.getStatusCode()) { |
| | | case CREATED -> "URI: %s\n".formatted(response.getHeaders().getLocation()); |
| | | case INTERNAL_SERVER_ERROR -> response.getBody(); |
| | | default -> ""; |
| | | }; |
| | | System.out.println(message); |
| | | |
| | | } |
| | | |