BAEL-4748 implement UserConsumerService with webClient and write UnitTests

This commit is contained in:
Trixi Turny 2021-01-09 15:55:24 +00:00
parent f5ef0bb500
commit df31e606c3
5 changed files with 239 additions and 0 deletions

View File

@ -0,0 +1,16 @@
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();
}

View File

@ -0,0 +1,90 @@
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());
}
}

View File

@ -0,0 +1,29 @@
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;
}
}

View File

@ -0,0 +1,30 @@
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; }
}

View File

@ -0,0 +1,74 @@
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)));
}
}