Merge pull request #10411 from Trixi-Turny/BAEL-4748
BAEL-4748 implement UserConsumerService with webClient and write Unit…
This commit is contained in:
commit
29d798d54f
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.webclient.json;
|
||||||
|
|
||||||
|
import com.baeldung.webclient.json.model.Book;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface ReaderConsumerService {
|
||||||
|
|
||||||
|
List<Book> processReaderDataFromObjectArray();
|
||||||
|
|
||||||
|
List<Book> processReaderDataFromReaderArray();
|
||||||
|
|
||||||
|
List<Book> processReaderDataFromReaderList();
|
||||||
|
|
||||||
|
List<String> processNestedReaderDataFromReaderArray();
|
||||||
|
|
||||||
|
List<String> processNestedReaderDataFromReaderList();
|
||||||
|
}
|
|
@ -0,0 +1,90 @@
|
||||||
|
package com.baeldung.webclient.json;
|
||||||
|
|
||||||
|
import com.baeldung.webclient.json.model.Book;
|
||||||
|
import com.baeldung.webclient.json.model.Reader;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class ReaderConsumerServiceImpl implements ReaderConsumerService {
|
||||||
|
|
||||||
|
private final WebClient webClient;
|
||||||
|
private static final ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
public ReaderConsumerServiceImpl(WebClient webClient) {
|
||||||
|
this.webClient = webClient;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public List<Book> processReaderDataFromObjectArray() {
|
||||||
|
Mono<Object[]> response = webClient.get()
|
||||||
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(Object[].class).log();
|
||||||
|
Object[] objects = response.block();
|
||||||
|
return Arrays.stream(objects)
|
||||||
|
.map(object -> mapper.convertValue(object, Reader.class))
|
||||||
|
.map(Reader::getFavouriteBook)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Book> processReaderDataFromReaderArray() {
|
||||||
|
Mono<Reader[]> response =
|
||||||
|
webClient.get()
|
||||||
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(Reader[].class).log();
|
||||||
|
|
||||||
|
Reader[] readers = response.block();
|
||||||
|
return Arrays.stream(readers)
|
||||||
|
.map(Reader::getFavouriteBook)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Book> processReaderDataFromReaderList() {
|
||||||
|
Mono<List<Reader>> response = webClient.get()
|
||||||
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(new ParameterizedTypeReference<List<Reader>>() {});
|
||||||
|
List<Reader> readers = response.block();
|
||||||
|
|
||||||
|
return readers.stream()
|
||||||
|
.map(Reader::getFavouriteBook)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> processNestedReaderDataFromReaderArray() {
|
||||||
|
Mono<Reader[]> response = webClient.get()
|
||||||
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(Reader[].class).log();
|
||||||
|
Reader[] readers = response.block();
|
||||||
|
|
||||||
|
return Arrays.stream(readers)
|
||||||
|
.flatMap(reader -> reader.getBooksRead().stream())
|
||||||
|
.map(Book::getAuthor)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<String> processNestedReaderDataFromReaderList() {
|
||||||
|
Mono<List<Reader>> response = webClient.get()
|
||||||
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
|
.retrieve()
|
||||||
|
.bodyToMono(new ParameterizedTypeReference<List<Reader>>() {});
|
||||||
|
|
||||||
|
List<Reader> readers = response.block();
|
||||||
|
return readers.stream()
|
||||||
|
.flatMap(reader -> reader.getBooksRead().stream())
|
||||||
|
.map(Book::getAuthor)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.webclient.json.model;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
public class Book {
|
||||||
|
private final String author;
|
||||||
|
private final String title;
|
||||||
|
|
||||||
|
@JsonCreator
|
||||||
|
public Book(
|
||||||
|
@JsonProperty("author") String author,
|
||||||
|
@JsonProperty("title") String title) {
|
||||||
|
this.author = author;
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
public String getAuthor() {
|
||||||
|
return this.author;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.baeldung.webclient.json.model;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
public class Reader {
|
||||||
|
private final int id;
|
||||||
|
private final String name;
|
||||||
|
private final Book favouriteBook;
|
||||||
|
private final List<Book> booksRead;
|
||||||
|
|
||||||
|
@JsonCreator
|
||||||
|
public Reader(
|
||||||
|
@JsonProperty("id") int id,
|
||||||
|
@JsonProperty("name") String name,
|
||||||
|
@JsonProperty("favouriteBook") Book favouriteBook,
|
||||||
|
@JsonProperty("booksRead") List<Book> booksRead) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.favouriteBook = favouriteBook;
|
||||||
|
this.booksRead =booksRead;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book getFavouriteBook() {
|
||||||
|
return favouriteBook;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Book> getBooksRead() { return booksRead; }
|
||||||
|
}
|
|
@ -0,0 +1,88 @@
|
||||||
|
package com.baeldung.webclient.json;
|
||||||
|
|
||||||
|
import com.baeldung.webclient.json.model.Book;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.CoreMatchers.hasItems;
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.Matchers.hasProperty;
|
||||||
|
|
||||||
|
public class ReaderConsumerServiceImplUnitTest {
|
||||||
|
|
||||||
|
private static String READER_JSON = "[{\"id\":1,\"name\":\"reader1\",\"favouriteBook\":{\"author\":\"Milan Kundera\",\"title\":\"The Unbearable Lightness of Being\"}," +
|
||||||
|
"\"booksRead\":[{\"author\":\"Charles Dickens\",\"title\":\"Oliver Twist\"},{\"author\":\"Milan Kundera\",\"title\":\"The Unbearable Lightness of Being\"}]}," +
|
||||||
|
"{\"id\":2,\"name\":\"reader2\",\"favouriteBook\":{\"author\":\"Douglas Adams\",\"title\":\"The Hitchhiker\'s Guide to the Galaxy\"}," +
|
||||||
|
"\"booksRead\":[{\"author\":\"J.R.R. Tolkien\",\"title\":\"Lord of the Rings\"}, " +
|
||||||
|
"{\"author\":\"Douglas Adams\",\"title\":\"The Hitchhiker\'s Guide to the Galaxy\"}]}]";
|
||||||
|
|
||||||
|
private static String BASE_URL = "http://localhost:8080/readers";
|
||||||
|
|
||||||
|
WebClient webClientMock = WebClient.builder().baseUrl(BASE_URL)
|
||||||
|
.exchangeFunction(clientRequest -> Mono.just(ClientResponse.create(HttpStatus.OK)
|
||||||
|
.header("content-type", "application/json")
|
||||||
|
.body(READER_JSON)
|
||||||
|
.build()))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
private final ReaderConsumerService tested = new ReaderConsumerServiceImpl(webClientMock);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void when_processReaderDataFromObjectArray_then_OK() {
|
||||||
|
String expectedAuthor1 = "Milan Kundera";
|
||||||
|
String expectedAuthor2 = "Douglas Adams";
|
||||||
|
List<Book> actual = tested.processReaderDataFromObjectArray();
|
||||||
|
assertThat(actual, hasItems(hasProperty("author", is(expectedAuthor1)),
|
||||||
|
hasProperty("author", is(expectedAuthor2))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void when_processReaderDataFromReaderArray_then_OK() {
|
||||||
|
String expectedAuthor1 = "Milan Kundera";
|
||||||
|
String expectedAuthor2 = "Douglas Adams";
|
||||||
|
List<Book> actual = tested.processReaderDataFromReaderArray();
|
||||||
|
assertThat(actual, hasItems(hasProperty("author", is(expectedAuthor1)),
|
||||||
|
hasProperty("author", is(expectedAuthor2))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void when_processReaderDataFromReaderList_then_OK() {
|
||||||
|
String expectedAuthor1 = "Milan Kundera";
|
||||||
|
String expectedAuthor2 = "Douglas Adams";
|
||||||
|
List<Book> actual = tested.processReaderDataFromReaderList();
|
||||||
|
assertThat(actual, hasItems(hasProperty("author", is(expectedAuthor1)),
|
||||||
|
hasProperty("author", is(expectedAuthor2))));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void when_processNestedReaderDataFromReaderArray_then_OK() {
|
||||||
|
List<String> expected = Arrays.asList(
|
||||||
|
"Milan Kundera",
|
||||||
|
"Charles Dickens",
|
||||||
|
"J.R.R. Tolkien",
|
||||||
|
"Douglas Adams");
|
||||||
|
|
||||||
|
List<String> actual = tested.processNestedReaderDataFromReaderArray();
|
||||||
|
assertThat(actual, hasItems(expected.get(0), expected.get(1), expected.get(2), expected.get(3)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void when_processNestedReaderDataFromReaderList_then_OK() {
|
||||||
|
List<String> expected = Arrays.asList(
|
||||||
|
"Milan Kundera",
|
||||||
|
"Charles Dickens",
|
||||||
|
"J.R.R. Tolkien",
|
||||||
|
"Douglas Adams");
|
||||||
|
|
||||||
|
List<String> actual = tested.processNestedReaderDataFromReaderList();
|
||||||
|
assertThat(actual, hasItems(expected.get(0), expected.get(1), expected.get(2), expected.get(3)));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue