This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InventoryClientTest { | |
private InventoryClient cut; | |
public static MockWebServer mockBackEnd; | |
@BeforeAll | |
public static void setUp() throws IOException { | |
mockBackEnd = new MockWebServer(); | |
mockBackEnd.start(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@GetExchange | |
List<CharacterResponse> getByName(@RequestParam String lastName); | |
// vs. | |
public List<CharacterResponse> getByName(String lastName) { | |
return webClient | |
.get() | |
.uri(uriBuilder -> uriBuilder | |
.path("/characters") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CharacterResponse brandon = characterClient.addCharacter(new AddCharacterRequest("Brandon", "Stark")); | |
List<CharacterResponse> starks = characterClient.getByName("Stark"); | |
Optional<CharacterResponse> eddardStark = characterClient.getById(1); | |
Optional<CharacterResponse> unknown = characterClient.getById(1337L); // empty | |
characterClient.deleteById(brandon.id()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class CharacterClientConfig { | |
@Bean | |
public CharacterClient characterClient(CharacterClientProperties properties) { | |
WebClient webClient = WebClient.builder() | |
.baseUrl(properties.getUrl()) | |
.defaultStatusHandler( | |
httpStatusCode -> HttpStatus.NOT_FOUND == httpStatusCode, | |
response -> Mono.empty()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@HttpExchange( | |
url = "/characters", | |
accept = MediaType.APPLICATION_JSON_VALUE) | |
public interface CharacterClient { | |
@GetExchange | |
List<CharacterResponse> getByName(@RequestParam String lastName); | |
@GetExchange("/{id}") | |
Optional<CharacterResponse> getById(@PathVariable long id); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-webflux</artifactId> | |
</dependency> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mono<InventoryEntry> result = cut.getByName(expectedEntry.name()); | |
StepVerifier | |
.withVirtualTime(() -> result) | |
// ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
retrySpec = Retry.backoff( | |
inventoryClientProperties.getRetry().getMaxAttempts(), | |
inventoryClientProperties.getRetry().getBackoffDuration() | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void verifyGetByNameRecoversFromServerFailuresWithVirtualTime() { | |
InventoryEntry expectedEntry = new InventoryEntry(42, "Der lange Weg zu einem kleinen zornigen Planeten", 5); | |
mockBackEnd.enqueue(assembleResponseWithStatusCode(500)); | |
mockBackEnd.enqueue(assembleResponse(expectedEntry)); | |
StepVerifier | |
.withVirtualTime(() -> cut.getByName(expectedEntry.name())) | |
.expectSubscription() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void verifyGetByNameRecoversFromServerFailuresWithBlock() { | |
InventoryEntry expectedEntry = new InventoryEntry(42, "Der lange Weg zu einem kleinen zornigen Planeten", 5); | |
mockBackEnd.enqueue(assembleResponseWithStatusCode(500)); | |
mockBackEnd.enqueue(assembleResponse(expectedEntry)); | |
InventoryEntry actualEntry = cut | |
.getByName(expectedEntry.name()) | |
.block(); |
NewerOlder