diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/webclient/json/ReaderConsumerService.java b/spring-5-reactive-client/src/main/java/com/baeldung/webclient/json/ReaderConsumerService.java index b0bc71d2ed..17676b3f33 100644 --- a/spring-5-reactive-client/src/main/java/com/baeldung/webclient/json/ReaderConsumerService.java +++ b/spring-5-reactive-client/src/main/java/com/baeldung/webclient/json/ReaderConsumerService.java @@ -1,14 +1,16 @@ package com.baeldung.webclient.json; +import com.baeldung.webclient.json.model.Book; + import java.util.List; public interface ReaderConsumerService { - List processReaderDataFromObjectArray(); + List processReaderDataFromObjectArray(); - List processReaderDataFromReaderArray(); + List processReaderDataFromReaderArray(); - List processReaderDataFromReaderList(); + List processReaderDataFromReaderList(); List processNestedReaderDataFromReaderArray(); diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/webclient/json/ReaderConsumerServiceImpl.java b/spring-5-reactive-client/src/main/java/com/baeldung/webclient/json/ReaderConsumerServiceImpl.java index d1800a7f65..8f1a4c019a 100644 --- a/spring-5-reactive-client/src/main/java/com/baeldung/webclient/json/ReaderConsumerServiceImpl.java +++ b/spring-5-reactive-client/src/main/java/com/baeldung/webclient/json/ReaderConsumerServiceImpl.java @@ -21,7 +21,7 @@ public class ReaderConsumerServiceImpl implements ReaderConsumerService { this.webClient = webClient; } @Override - public List processReaderDataFromObjectArray() { + public List processReaderDataFromObjectArray() { Mono response = webClient.get() .accept(MediaType.APPLICATION_JSON) .retrieve() @@ -29,12 +29,12 @@ public class ReaderConsumerServiceImpl implements ReaderConsumerService { Object[] objects = response.block(); return Arrays.stream(objects) .map(object -> mapper.convertValue(object, Reader.class)) - .map(Reader::getName) + .map(Reader::getFavouriteBook) .collect(Collectors.toList()); } @Override - public List processReaderDataFromReaderArray() { + public List processReaderDataFromReaderArray() { Mono response = webClient.get() .accept(MediaType.APPLICATION_JSON) @@ -43,12 +43,12 @@ public class ReaderConsumerServiceImpl implements ReaderConsumerService { Reader[] readers = response.block(); return Arrays.stream(readers) - .map(Reader::getName) + .map(Reader::getFavouriteBook) .collect(Collectors.toList()); } @Override - public List processReaderDataFromReaderList() { + public List processReaderDataFromReaderList() { Mono> response = webClient.get() .accept(MediaType.APPLICATION_JSON) .retrieve() @@ -56,7 +56,7 @@ public class ReaderConsumerServiceImpl implements ReaderConsumerService { List readers = response.block(); return readers.stream() - .map(Reader::getName) + .map(Reader::getFavouriteBook) .collect(Collectors.toList()); } @@ -69,7 +69,7 @@ public class ReaderConsumerServiceImpl implements ReaderConsumerService { Reader[] readers = response.block(); return Arrays.stream(readers) - .flatMap(reader -> reader.getFavouriteBooks().stream()) + .flatMap(reader -> reader.getBooksRead().stream()) .map(Book::getAuthor) .collect(Collectors.toList()); } @@ -83,7 +83,7 @@ public class ReaderConsumerServiceImpl implements ReaderConsumerService { List readers = response.block(); return readers.stream() - .flatMap(reader -> reader.getFavouriteBooks().stream()) + .flatMap(reader -> reader.getBooksRead().stream()) .map(Book::getAuthor) .collect(Collectors.toList()); } diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/webclient/json/model/Reader.java b/spring-5-reactive-client/src/main/java/com/baeldung/webclient/json/model/Reader.java index 9fc480c3df..7d02853ea1 100644 --- a/spring-5-reactive-client/src/main/java/com/baeldung/webclient/json/model/Reader.java +++ b/spring-5-reactive-client/src/main/java/com/baeldung/webclient/json/model/Reader.java @@ -10,21 +10,24 @@ import java.util.List; public class Reader { private final int id; private final String name; - private final List favouriteBooks; + private final Book favouriteBook; + private final List booksRead; @JsonCreator public Reader( @JsonProperty("id") int id, @JsonProperty("name") String name, - @JsonProperty("favouriteBooks") List favoriteBooks) { + @JsonProperty("favouriteBook") Book favouriteBook, + @JsonProperty("booksRead") List booksRead) { this.id = id; this.name = name; - this.favouriteBooks = favoriteBooks; + this.favouriteBook = favouriteBook; + this.booksRead =booksRead; } - public String getName() { - return name; + public Book getFavouriteBook() { + return favouriteBook; } - public List getFavouriteBooks() { return favouriteBooks; } + public List getBooksRead() { return booksRead; } } diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/webclient/json/ReaderConsumerServiceImplUnitTest.java b/spring-5-reactive-client/src/test/java/com/baeldung/webclient/json/ReaderConsumerServiceImplUnitTest.java index 065f1dc11a..51f0a5c1bb 100644 --- a/spring-5-reactive-client/src/test/java/com/baeldung/webclient/json/ReaderConsumerServiceImplUnitTest.java +++ b/spring-5-reactive-client/src/test/java/com/baeldung/webclient/json/ReaderConsumerServiceImplUnitTest.java @@ -1,5 +1,6 @@ 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; @@ -10,13 +11,16 @@ import java.util.Arrays; import java.util.List; 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 { - private static String READER_JSON = "[{\"id\":1,\"name\":\"reader1\",\"favouriteBooks\":[{\"author\":\"Milan Kundera\",\"title\":\"The Unbearable Lightness of Being\"}," + - "{\"author\":\"Charles Dickens\",\"title\":\"Oliver Twist\"}]}," + - "{\"id\":2,\"name\":\"reader2\",\"favouriteBooks\":[{\"author\":\"J.R.R. Tolkien\",\"title\":\"Lord of the Rings\"}, " + + 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"; @@ -32,23 +36,30 @@ public class ReaderConsumerServiceImplUnitTest { @Test void when_processReaderDataFromObjectArray_then_OK() { - List expected = Arrays.asList("reader1", "reader2"); - List actual = tested.processReaderDataFromObjectArray(); - assertThat(actual, contains(expected.get(0), expected.get(1))); + String expectedAuthor1 = "Milan Kundera"; + String expectedAuthor2 = "Douglas Adams"; + List actual = tested.processReaderDataFromObjectArray(); + assertThat(actual, hasItems(hasProperty("author", is(expectedAuthor1)), + hasProperty("author", is(expectedAuthor2)))); } @Test void when_processReaderDataFromReaderArray_then_OK() { - List expected = Arrays.asList("reader1", "reader2"); - List actual = tested.processReaderDataFromReaderArray(); - assertThat(actual, contains(expected.get(0), expected.get(1))); + String expectedAuthor1 = "Milan Kundera"; + String expectedAuthor2 = "Douglas Adams"; + List actual = tested.processReaderDataFromReaderArray(); + assertThat(actual, hasItems(hasProperty("author", is(expectedAuthor1)), + hasProperty("author", is(expectedAuthor2)))); } @Test void when_processReaderDataFromReaderList_then_OK() { - List expected = Arrays.asList("reader1", "reader2"); - List actual = tested.processReaderDataFromReaderList(); - assertThat(actual, contains(expected.get(0), expected.get(1))); + String expectedAuthor1 = "Milan Kundera"; + String expectedAuthor2 = "Douglas Adams"; + List actual = tested.processReaderDataFromReaderList(); + assertThat(actual, hasItems(hasProperty("author", is(expectedAuthor1)), + hasProperty("author", is(expectedAuthor2)))); + } @Test @@ -60,7 +71,7 @@ public class ReaderConsumerServiceImplUnitTest { "Douglas Adams"); List 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 @@ -72,6 +83,6 @@ public class ReaderConsumerServiceImplUnitTest { "Douglas Adams"); List 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))); } } \ No newline at end of file