BAEL-4748 change user to reader and address to favouriteBooks
This commit is contained in:
parent
df31e606c3
commit
70a0e0de96
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.webclient.json;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ReaderConsumerService {
|
||||
|
||||
List<String> processReaderDataFromObjectArray();
|
||||
|
||||
List<String> processReaderDataFromReaderArray();
|
||||
|
||||
List<String> 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<String> 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::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> 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::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> 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::getName)
|
||||
.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.getFavouriteBooks().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.getFavouriteBooks().stream())
|
||||
.map(Book::getAuthor)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
package com.baeldung.webclient.json;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface UserConsumerService {
|
||||
|
||||
List<String> processUserDataFromObjectArray();
|
||||
|
||||
List<String> processUserDataFromUserArray();
|
||||
|
||||
List<String> processUserDataFromUserList();
|
||||
|
||||
List<String> processNestedUserDataFromUserArray();
|
||||
|
||||
List<String> processNestedUserDataFromUserList();
|
||||
}
|
|
@ -1,90 +0,0 @@
|
|||
package com.baeldung.webclient.json;
|
||||
|
||||
import com.baeldung.webclient.json.model.Address;
|
||||
import com.baeldung.webclient.json.model.User;
|
||||
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 UserConsumerServiceImpl implements UserConsumerService {
|
||||
|
||||
private final WebClient webClient;
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
public UserConsumerServiceImpl(WebClient webClient) {
|
||||
this.webClient = webClient;
|
||||
}
|
||||
@Override
|
||||
public List<String> processUserDataFromObjectArray() {
|
||||
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, User.class))
|
||||
.map(User::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> processUserDataFromUserArray() {
|
||||
Mono<User[]> response =
|
||||
webClient.get()
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.bodyToMono(User[].class).log();
|
||||
|
||||
User[] userArray = response.block();
|
||||
return Arrays.stream(userArray)
|
||||
.map(User::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> processUserDataFromUserList() {
|
||||
Mono<List<User>> response = webClient.get()
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.bodyToMono(new ParameterizedTypeReference<List<User>>() {});
|
||||
List<User> userList = response.block();
|
||||
|
||||
return userList.stream()
|
||||
.map(User::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> processNestedUserDataFromUserArray() {
|
||||
Mono<User[]> response = webClient.get()
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.bodyToMono(User[].class).log();
|
||||
User[] userArray = response.block();
|
||||
|
||||
return Arrays.stream(userArray)
|
||||
.flatMap(user -> user.getAddressList().stream())
|
||||
.map(Address::getPostCode)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> processNestedUserDataFromUserList() {
|
||||
Mono<List<User>> response = webClient.get()
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.bodyToMono(new ParameterizedTypeReference<List<User>>() {});
|
||||
|
||||
List<User> userList = response.block();
|
||||
return userList.stream()
|
||||
.flatMap(user -> user.getAddressList().stream())
|
||||
.map(Address::getPostCode)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
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 Address {
|
||||
private final String addressLine1;
|
||||
private final String addressLine2;
|
||||
private final String town;
|
||||
private final String postCode;
|
||||
|
||||
@JsonCreator
|
||||
public Address(
|
||||
@JsonProperty("addressLine1") String addressLine1,
|
||||
@JsonProperty("addressLine2") String addressLine2,
|
||||
@JsonProperty("town") String town,
|
||||
@JsonProperty("postCode") String postCode) {
|
||||
this.addressLine1 = addressLine1;
|
||||
this.addressLine2 = addressLine2;
|
||||
this.town = town;
|
||||
this.postCode = postCode;
|
||||
}
|
||||
public String getPostCode() {
|
||||
return postCode;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
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 User {
|
||||
private final int id;
|
||||
private final String name;
|
||||
private final List<Address> addressList;
|
||||
|
||||
@JsonCreator
|
||||
public User(
|
||||
@JsonProperty("id") int id,
|
||||
@JsonProperty("name") String name,
|
||||
@JsonProperty("addressList") List<Address> addressList) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.addressList = addressList;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public List<Address> getAddressList() { return addressList; }
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package com.baeldung.webclient.json;
|
||||
|
||||
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.Matchers.contains;
|
||||
|
||||
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\"}, " +
|
||||
"{\"author\":\"Douglas Adams\",\"title\":\"The Hitchhiker\'s Guide to the Galaxy\"}]}]";
|
||||
|
||||
private static String BASE_URL = "http://localhost:8080";
|
||||
|
||||
WebClient webClientMock = WebClient.builder()
|
||||
.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() {
|
||||
List<String> expected = Arrays.asList("reader1", "reader2");
|
||||
List<String> actual = tested.processReaderDataFromObjectArray();
|
||||
assertThat(actual, contains(expected.get(0), expected.get(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void when_processReaderDataFromReaderArray_then_OK() {
|
||||
List<String> expected = Arrays.asList("reader1", "reader2");
|
||||
List<String> actual = tested.processReaderDataFromReaderArray();
|
||||
assertThat(actual, contains(expected.get(0), expected.get(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void when_processReaderDataFromReaderList_then_OK() {
|
||||
List<String> expected = Arrays.asList("reader1", "reader2");
|
||||
List<String> actual = tested.processReaderDataFromReaderList();
|
||||
assertThat(actual, contains(expected.get(0), expected.get(1)));
|
||||
}
|
||||
|
||||
@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, contains(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, contains(expected.get(0), expected.get(1), expected.get(2), expected.get(3)));
|
||||
}
|
||||
}
|
|
@ -1,74 +0,0 @@
|
|||
package com.baeldung.webclient.json;
|
||||
|
||||
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.Matchers.contains;
|
||||
|
||||
public class UserConsumerServiceImplUnitTest {
|
||||
|
||||
private static String USER_JSON = "[{\"id\":1,\"name\":\"user1\",\"addressList\":[{\"addressLine1\":\"address1_addressLine1\",\"addressLine2\":\"address1_addressLine2\",\"town\":\"address1_town\",\"postCode\":\"user1_address1_postCode\"}," +
|
||||
"{\"addressLine1\":\"address2_addressLine1\",\"addressLine2\":\"address2_addressLine2\",\"town\":\"address2_town\",\"postCode\":\"user1_address2_postCode\"}]}," +
|
||||
"{\"id\":2,\"name\":\"user2\",\"addressList\":[{\"addressLine1\":\"address1_addressLine1\",\"addressLine2\":\"address1_addressLine2\",\"town\":\"address1_town\",\"postCode\":\"user2_address1_postCode\"}]}]";
|
||||
|
||||
private static String BASE_URL = "http://localhost:8080";
|
||||
|
||||
WebClient webClientMock = WebClient.builder()
|
||||
.exchangeFunction(clientRequest -> Mono.just(ClientResponse.create(HttpStatus.OK)
|
||||
.header("content-type", "application/json")
|
||||
.body(USER_JSON)
|
||||
.build()))
|
||||
.build();
|
||||
|
||||
private final UserConsumerService tested = new UserConsumerServiceImpl(webClientMock);
|
||||
|
||||
@Test
|
||||
void when_processUserDataFromObjectArray_then_OK() {
|
||||
List<String> expected = Arrays.asList("user1", "user2");
|
||||
List<String> actual = tested.processUserDataFromObjectArray();
|
||||
assertThat(actual, contains(expected.get(0), expected.get(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void when_processUserDataFromUserArray_then_OK() {
|
||||
List<String> expected = Arrays.asList("user1", "user2");
|
||||
List<String> actual = tested.processUserDataFromUserArray();
|
||||
assertThat(actual, contains(expected.get(0), expected.get(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void when_processUserDataFromUserList_then_OK() {
|
||||
List<String> expected = Arrays.asList("user1", "user2");
|
||||
List<String> actual = tested.processUserDataFromUserList();
|
||||
assertThat(actual, contains(expected.get(0), expected.get(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void when_processNestedUserDataFromUserArray_then_OK() {
|
||||
List<String> expected = Arrays.asList(
|
||||
"user1_address1_postCode",
|
||||
"user1_address2_postCode",
|
||||
"user2_address1_postCode");
|
||||
|
||||
List<String> actual = tested.processNestedUserDataFromUserArray();
|
||||
assertThat(actual, contains(expected.get(0), expected.get(1), expected.get(2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void when_processNestedUserDataFromUserList_then_OK() {
|
||||
List<String> expected = Arrays.asList(
|
||||
"user1_address1_postCode",
|
||||
"user1_address2_postCode",
|
||||
"user2_address1_postCode");
|
||||
|
||||
List<String> actual = tested.processNestedUserDataFromUserList();
|
||||
assertThat(actual, contains(expected.get(0), expected.get(1), expected.get(2)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue