BAEL-4748 update examples with favouriteBook
This commit is contained in:
parent
a0425afd1b
commit
c3741487d8
|
@ -1,14 +1,16 @@
|
||||||
package com.baeldung.webclient.json;
|
package com.baeldung.webclient.json;
|
||||||
|
|
||||||
|
import com.baeldung.webclient.json.model.Book;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface ReaderConsumerService {
|
public interface ReaderConsumerService {
|
||||||
|
|
||||||
List<String> processReaderDataFromObjectArray();
|
List<Book> processReaderDataFromObjectArray();
|
||||||
|
|
||||||
List<String> processReaderDataFromReaderArray();
|
List<Book> processReaderDataFromReaderArray();
|
||||||
|
|
||||||
List<String> processReaderDataFromReaderList();
|
List<Book> processReaderDataFromReaderList();
|
||||||
|
|
||||||
List<String> processNestedReaderDataFromReaderArray();
|
List<String> processNestedReaderDataFromReaderArray();
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ public class ReaderConsumerServiceImpl implements ReaderConsumerService {
|
||||||
this.webClient = webClient;
|
this.webClient = webClient;
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public List<String> processReaderDataFromObjectArray() {
|
public List<Book> processReaderDataFromObjectArray() {
|
||||||
Mono<Object[]> response = webClient.get()
|
Mono<Object[]> response = webClient.get()
|
||||||
.accept(MediaType.APPLICATION_JSON)
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
@ -29,12 +29,12 @@ public class ReaderConsumerServiceImpl implements ReaderConsumerService {
|
||||||
Object[] objects = response.block();
|
Object[] objects = response.block();
|
||||||
return Arrays.stream(objects)
|
return Arrays.stream(objects)
|
||||||
.map(object -> mapper.convertValue(object, Reader.class))
|
.map(object -> mapper.convertValue(object, Reader.class))
|
||||||
.map(Reader::getName)
|
.map(Reader::getFavouriteBook)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> processReaderDataFromReaderArray() {
|
public List<Book> processReaderDataFromReaderArray() {
|
||||||
Mono<Reader[]> response =
|
Mono<Reader[]> response =
|
||||||
webClient.get()
|
webClient.get()
|
||||||
.accept(MediaType.APPLICATION_JSON)
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
|
@ -43,12 +43,12 @@ public class ReaderConsumerServiceImpl implements ReaderConsumerService {
|
||||||
|
|
||||||
Reader[] readers = response.block();
|
Reader[] readers = response.block();
|
||||||
return Arrays.stream(readers)
|
return Arrays.stream(readers)
|
||||||
.map(Reader::getName)
|
.map(Reader::getFavouriteBook)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> processReaderDataFromReaderList() {
|
public List<Book> processReaderDataFromReaderList() {
|
||||||
Mono<List<Reader>> response = webClient.get()
|
Mono<List<Reader>> response = webClient.get()
|
||||||
.accept(MediaType.APPLICATION_JSON)
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
.retrieve()
|
.retrieve()
|
||||||
|
@ -56,7 +56,7 @@ public class ReaderConsumerServiceImpl implements ReaderConsumerService {
|
||||||
List<Reader> readers = response.block();
|
List<Reader> readers = response.block();
|
||||||
|
|
||||||
return readers.stream()
|
return readers.stream()
|
||||||
.map(Reader::getName)
|
.map(Reader::getFavouriteBook)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ public class ReaderConsumerServiceImpl implements ReaderConsumerService {
|
||||||
Reader[] readers = response.block();
|
Reader[] readers = response.block();
|
||||||
|
|
||||||
return Arrays.stream(readers)
|
return Arrays.stream(readers)
|
||||||
.flatMap(reader -> reader.getFavouriteBooks().stream())
|
.flatMap(reader -> reader.getBooksRead().stream())
|
||||||
.map(Book::getAuthor)
|
.map(Book::getAuthor)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,7 @@ public class ReaderConsumerServiceImpl implements ReaderConsumerService {
|
||||||
|
|
||||||
List<Reader> readers = response.block();
|
List<Reader> readers = response.block();
|
||||||
return readers.stream()
|
return readers.stream()
|
||||||
.flatMap(reader -> reader.getFavouriteBooks().stream())
|
.flatMap(reader -> reader.getBooksRead().stream())
|
||||||
.map(Book::getAuthor)
|
.map(Book::getAuthor)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,21 +10,24 @@ import java.util.List;
|
||||||
public class Reader {
|
public class Reader {
|
||||||
private final int id;
|
private final int id;
|
||||||
private final String name;
|
private final String name;
|
||||||
private final List<Book> favouriteBooks;
|
private final Book favouriteBook;
|
||||||
|
private final List<Book> booksRead;
|
||||||
|
|
||||||
@JsonCreator
|
@JsonCreator
|
||||||
public Reader(
|
public Reader(
|
||||||
@JsonProperty("id") int id,
|
@JsonProperty("id") int id,
|
||||||
@JsonProperty("name") String name,
|
@JsonProperty("name") String name,
|
||||||
@JsonProperty("favouriteBooks") List<Book> favoriteBooks) {
|
@JsonProperty("favouriteBook") Book favouriteBook,
|
||||||
|
@JsonProperty("booksRead") List<Book> booksRead) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.favouriteBooks = favoriteBooks;
|
this.favouriteBook = favouriteBook;
|
||||||
|
this.booksRead =booksRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public Book getFavouriteBook() {
|
||||||
return name;
|
return favouriteBook;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Book> getFavouriteBooks() { return favouriteBooks; }
|
public List<Book> getBooksRead() { return booksRead; }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.baeldung.webclient.json;
|
package com.baeldung.webclient.json;
|
||||||
|
|
||||||
|
import com.baeldung.webclient.json.model.Book;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||||
|
@ -10,13 +11,16 @@ import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.contains;
|
import static org.hamcrest.CoreMatchers.hasItems;
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.Matchers.hasProperty;
|
||||||
|
|
||||||
public class ReaderConsumerServiceImplUnitTest {
|
public class ReaderConsumerServiceImplUnitTest {
|
||||||
|
|
||||||
private static String READER_JSON = "[{\"id\":1,\"name\":\"reader1\",\"favouriteBooks\":[{\"author\":\"Milan Kundera\",\"title\":\"The Unbearable Lightness of Being\"}," +
|
private static String READER_JSON = "[{\"id\":1,\"name\":\"reader1\",\"favouriteBook\":{\"author\":\"Milan Kundera\",\"title\":\"The Unbearable Lightness of Being\"}," +
|
||||||
"{\"author\":\"Charles Dickens\",\"title\":\"Oliver Twist\"}]}," +
|
"\"booksRead\":[{\"author\":\"Charles Dickens\",\"title\":\"Oliver Twist\"},{\"author\":\"Milan Kundera\",\"title\":\"The Unbearable Lightness of Being\"}]}," +
|
||||||
"{\"id\":2,\"name\":\"reader2\",\"favouriteBooks\":[{\"author\":\"J.R.R. Tolkien\",\"title\":\"Lord of the Rings\"}, " +
|
"{\"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\"}]}]";
|
"{\"author\":\"Douglas Adams\",\"title\":\"The Hitchhiker\'s Guide to the Galaxy\"}]}]";
|
||||||
|
|
||||||
private static String BASE_URL = "http://localhost:8080/readers";
|
private static String BASE_URL = "http://localhost:8080/readers";
|
||||||
|
@ -32,23 +36,30 @@ public class ReaderConsumerServiceImplUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void when_processReaderDataFromObjectArray_then_OK() {
|
void when_processReaderDataFromObjectArray_then_OK() {
|
||||||
List<String> expected = Arrays.asList("reader1", "reader2");
|
String expectedAuthor1 = "Milan Kundera";
|
||||||
List<String> actual = tested.processReaderDataFromObjectArray();
|
String expectedAuthor2 = "Douglas Adams";
|
||||||
assertThat(actual, contains(expected.get(0), expected.get(1)));
|
List<Book> actual = tested.processReaderDataFromObjectArray();
|
||||||
|
assertThat(actual, hasItems(hasProperty("author", is(expectedAuthor1)),
|
||||||
|
hasProperty("author", is(expectedAuthor2))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void when_processReaderDataFromReaderArray_then_OK() {
|
void when_processReaderDataFromReaderArray_then_OK() {
|
||||||
List<String> expected = Arrays.asList("reader1", "reader2");
|
String expectedAuthor1 = "Milan Kundera";
|
||||||
List<String> actual = tested.processReaderDataFromReaderArray();
|
String expectedAuthor2 = "Douglas Adams";
|
||||||
assertThat(actual, contains(expected.get(0), expected.get(1)));
|
List<Book> actual = tested.processReaderDataFromReaderArray();
|
||||||
|
assertThat(actual, hasItems(hasProperty("author", is(expectedAuthor1)),
|
||||||
|
hasProperty("author", is(expectedAuthor2))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void when_processReaderDataFromReaderList_then_OK() {
|
void when_processReaderDataFromReaderList_then_OK() {
|
||||||
List<String> expected = Arrays.asList("reader1", "reader2");
|
String expectedAuthor1 = "Milan Kundera";
|
||||||
List<String> actual = tested.processReaderDataFromReaderList();
|
String expectedAuthor2 = "Douglas Adams";
|
||||||
assertThat(actual, contains(expected.get(0), expected.get(1)));
|
List<Book> actual = tested.processReaderDataFromReaderList();
|
||||||
|
assertThat(actual, hasItems(hasProperty("author", is(expectedAuthor1)),
|
||||||
|
hasProperty("author", is(expectedAuthor2))));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -60,7 +71,7 @@ public class ReaderConsumerServiceImplUnitTest {
|
||||||
"Douglas Adams");
|
"Douglas Adams");
|
||||||
|
|
||||||
List<String> actual = tested.processNestedReaderDataFromReaderArray();
|
List<String> actual = tested.processNestedReaderDataFromReaderArray();
|
||||||
assertThat(actual, contains(expected.get(0), expected.get(1), expected.get(2), expected.get(3)));
|
assertThat(actual, hasItems(expected.get(0), expected.get(1), expected.get(2), expected.get(3)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -72,6 +83,6 @@ public class ReaderConsumerServiceImplUnitTest {
|
||||||
"Douglas Adams");
|
"Douglas Adams");
|
||||||
|
|
||||||
List<String> actual = tested.processNestedReaderDataFromReaderList();
|
List<String> actual = tested.processNestedReaderDataFromReaderList();
|
||||||
assertThat(actual, contains(expected.get(0), expected.get(1), expected.get(2), expected.get(3)));
|
assertThat(actual, hasItems(expected.get(0), expected.get(1), expected.get(2), expected.get(3)));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue