From 43d0bd6338753ebc61b53f7aedac2299f40ff997 Mon Sep 17 00:00:00 2001 From: Cesar Nicolas Cabo Date: Thu, 22 Aug 2019 23:16:46 -0300 Subject: [PATCH 1/5] Pojo models from Json objects added --- .../springframework/api/domain/Billing.java | 31 +++++ .../guru/springframework/api/domain/Card.java | 67 +++++++++++ .../api/domain/ExpirationDate.java | 49 ++++++++ .../guru/springframework/api/domain/Job.java | 40 +++++++ .../springframework/api/domain/Location.java | 58 +++++++++ .../springframework/api/domain/Login.java | 67 +++++++++++ .../guru/springframework/api/domain/Name.java | 49 ++++++++ .../guru/springframework/api/domain/User.java | 112 ++++++++++++++++++ 8 files changed, 473 insertions(+) create mode 100644 src/main/java/guru/springframework/api/domain/Billing.java create mode 100644 src/main/java/guru/springframework/api/domain/Card.java create mode 100644 src/main/java/guru/springframework/api/domain/ExpirationDate.java create mode 100644 src/main/java/guru/springframework/api/domain/Job.java create mode 100644 src/main/java/guru/springframework/api/domain/Location.java create mode 100644 src/main/java/guru/springframework/api/domain/Login.java create mode 100644 src/main/java/guru/springframework/api/domain/Name.java create mode 100644 src/main/java/guru/springframework/api/domain/User.java diff --git a/src/main/java/guru/springframework/api/domain/Billing.java b/src/main/java/guru/springframework/api/domain/Billing.java new file mode 100644 index 00000000..e90c9a5b --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/Billing.java @@ -0,0 +1,31 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class Billing implements Serializable +{ + + private Card card; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = 6577338081290507077L; + + public Card getCard() { + return card; + } + + public void setCard(Card card) { + this.card = card; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/Card.java b/src/main/java/guru/springframework/api/domain/Card.java new file mode 100644 index 00000000..f4f49ddc --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/Card.java @@ -0,0 +1,67 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class Card implements Serializable +{ + + private String type; + private String number; + private ExpirationDate expirationDate; + private String iban; + private String swift; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = 6203456183354582742L; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public ExpirationDate getExpirationDate() { + return expirationDate; + } + + public void setExpirationDate(ExpirationDate expirationDate) { + this.expirationDate = expirationDate; + } + + public String getIban() { + return iban; + } + + public void setIban(String iban) { + this.iban = iban; + } + + public String getSwift() { + return swift; + } + + public void setSwift(String swift) { + this.swift = swift; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/ExpirationDate.java b/src/main/java/guru/springframework/api/domain/ExpirationDate.java new file mode 100644 index 00000000..e3109274 --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/ExpirationDate.java @@ -0,0 +1,49 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class ExpirationDate implements Serializable +{ + + private String date; + private Integer timezoneType; + private String timezone; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = 4661228813349752965L; + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public Integer getTimezoneType() { + return timezoneType; + } + + public void setTimezoneType(Integer timezoneType) { + this.timezoneType = timezoneType; + } + + public String getTimezone() { + return timezone; + } + + public void setTimezone(String timezone) { + this.timezone = timezone; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/Job.java b/src/main/java/guru/springframework/api/domain/Job.java new file mode 100644 index 00000000..5ce2654b --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/Job.java @@ -0,0 +1,40 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class Job implements Serializable +{ + + private String title; + private String company; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = -4985150429002262656L; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getCompany() { + return company; + } + + public void setCompany(String company) { + this.company = company; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/Location.java b/src/main/java/guru/springframework/api/domain/Location.java new file mode 100644 index 00000000..569ae65e --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/Location.java @@ -0,0 +1,58 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class Location implements Serializable +{ + + private String street; + private String city; + private String state; + private String postcode; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = -3532048267747973846L; + + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getPostcode() { + return postcode; + } + + public void setPostcode(String postcode) { + this.postcode = postcode; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/Login.java b/src/main/java/guru/springframework/api/domain/Login.java new file mode 100644 index 00000000..2dcc8082 --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/Login.java @@ -0,0 +1,67 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class Login implements Serializable +{ + + private String username; + private String password; + private String md5; + private String sha1; + private String sha256; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = 1041720428871730372L; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getMd5() { + return md5; + } + + public void setMd5(String md5) { + this.md5 = md5; + } + + public String getSha1() { + return sha1; + } + + public void setSha1(String sha1) { + this.sha1 = sha1; + } + + public String getSha256() { + return sha256; + } + + public void setSha256(String sha256) { + this.sha256 = sha256; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/Name.java b/src/main/java/guru/springframework/api/domain/Name.java new file mode 100644 index 00000000..8a2028ed --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/Name.java @@ -0,0 +1,49 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class Name implements Serializable +{ + + private String title; + private String first; + private String last; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = 420620315591775395L; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getFirst() { + return first; + } + + public void setFirst(String first) { + this.first = first; + } + + public String getLast() { + return last; + } + + public void setLast(String last) { + this.last = last; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} diff --git a/src/main/java/guru/springframework/api/domain/User.java b/src/main/java/guru/springframework/api/domain/User.java new file mode 100644 index 00000000..d02cb12f --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/User.java @@ -0,0 +1,112 @@ + +package guru.springframework.api.domain; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class User implements Serializable +{ + + private String gender; + private Name name; + private Location location; + private String email; + private Login login; + private String phone; + private Job job; + private Billing billing; + private String language; + private String currency; + private Map additionalProperties = new HashMap(); + private final static long serialVersionUID = 270727596527329664L; + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + public Name getName() { + return name; + } + + public void setName(Name name) { + this.name = name; + } + + public Location getLocation() { + return location; + } + + public void setLocation(Location location) { + this.location = location; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public Login getLogin() { + return login; + } + + public void setLogin(Login login) { + this.login = login; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public Job getJob() { + return job; + } + + public void setJob(Job job) { + this.job = job; + } + + public Billing getBilling() { + return billing; + } + + public void setBilling(Billing billing) { + this.billing = billing; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } + + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + +} From 746bdc9be88a56b0723f5d1350e8dee2160ee985 Mon Sep 17 00:00:00 2001 From: Cesar Nicolas Cabo Date: Fri, 23 Aug 2019 23:18:52 -0300 Subject: [PATCH 2/5] Pom updated, index.html and userlist.html added, UserController created, ApiServiceImplTest created --- pom.xml | 25 ++++-- .../springframework/api/domain/UserData.java | 19 +++++ .../config/RestTemplateConfig.java | 19 +++++ .../controllers/UserController.java | 28 +++++++ .../services/ApiService.java | 15 ++++ .../services/ApiServiceImpl.java | 28 +++++++ src/main/resources/templates/index.html | 42 ++++++++++ src/main/resources/templates/userlist.html | 76 +++++++++++++++++++ .../services/ApiServiceImplTest.java | 36 +++++++++ 9 files changed, 282 insertions(+), 6 deletions(-) create mode 100644 src/main/java/guru/springframework/api/domain/UserData.java create mode 100644 src/main/java/guru/springframework/springrestclientexamples/config/RestTemplateConfig.java create mode 100644 src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java create mode 100644 src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java create mode 100644 src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java create mode 100644 src/main/resources/templates/index.html create mode 100644 src/main/resources/templates/userlist.html create mode 100644 src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java diff --git a/pom.xml b/pom.xml index 812708b4..c36b2312 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 guru.springframework @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.0.RELEASE + 2.0.0.RELEASE @@ -22,6 +22,10 @@ UTF-8 UTF-8 1.8 + + + 3.0.8-SNAPSHOT + 3.0.8-SNAPSHOT @@ -29,10 +33,6 @@ org.springframework.boot spring-boot-starter-thymeleaf - - org.springframework.boot - spring-boot-starter-web - org.springframework.boot spring-boot-starter-webflux @@ -48,6 +48,11 @@ lombok true + + org.webjars + bootstrap + 3.3.7-1 + org.springframework.boot spring-boot-starter-test @@ -86,6 +91,14 @@ false + + sonatype-nexus-snapshots + Sonatype Nexus Snapshots + https://oss.sonatype.org/content/repositories/snapshots + + true + + diff --git a/src/main/java/guru/springframework/api/domain/UserData.java b/src/main/java/guru/springframework/api/domain/UserData.java new file mode 100644 index 00000000..a1ff0f9f --- /dev/null +++ b/src/main/java/guru/springframework/api/domain/UserData.java @@ -0,0 +1,19 @@ +package guru.springframework.api.domain; + +import java.util.List; + +/** + * Created by ccabo 8/22/19 + */ +public class UserData { + + private List data; + + public List getData() { + return data; + } + + public void setData(List data) { + this.data = data; + } +} diff --git a/src/main/java/guru/springframework/springrestclientexamples/config/RestTemplateConfig.java b/src/main/java/guru/springframework/springrestclientexamples/config/RestTemplateConfig.java new file mode 100644 index 00000000..0e0b91f7 --- /dev/null +++ b/src/main/java/guru/springframework/springrestclientexamples/config/RestTemplateConfig.java @@ -0,0 +1,19 @@ +package guru.springframework.springrestclientexamples.config; + +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * Created by ccabo 8/22/19 + */ +@Configuration + +public class RestTemplateConfig { + + @Bean + public RestTemplate restTemplate(RestTemplateBuilder builder) { + return builder.build(); + } +} diff --git a/src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java b/src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java new file mode 100644 index 00000000..c3f80862 --- /dev/null +++ b/src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java @@ -0,0 +1,28 @@ +package guru.springframework.springrestclientexamples.controllers; + +import guru.springframework.springrestclientexamples.services.ApiService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * Created by ccabo 8/23/19 + */ +@Slf4j +@Controller +public class UserController { + public ApiService apiService; + + @Autowired + public UserController(ApiService apiService) { + this.apiService = apiService; + } + + @RequestMapping({"", "/", "/index"}) + public String index() { + return "index"; + } + + +} diff --git a/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java b/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java new file mode 100644 index 00000000..93d37476 --- /dev/null +++ b/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java @@ -0,0 +1,15 @@ +package guru.springframework.springrestclientexamples.services; + +import guru.springframework.api.domain.User; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * Created by ccabo 8/22/19 + */ +@Service +public interface ApiService { + + List getUser(Integer limit); +} diff --git a/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java b/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java new file mode 100644 index 00000000..7dafaf20 --- /dev/null +++ b/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java @@ -0,0 +1,28 @@ +package guru.springframework.springrestclientexamples.services; + +import guru.springframework.api.domain.User; +import guru.springframework.api.domain.UserData; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.util.List; + +/** + * Created by ccabo 8/22/19 + */ +@Service +public class ApiServiceImpl implements ApiService { + + private RestTemplate restTemplate; + + public ApiServiceImpl(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + @Override + public List getUser(Integer limit) { + UserData userData = restTemplate + .getForObject("http://apifaketory.com/api/user?limit=" + limit, UserData.class); + return userData.getData(); + } +} diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 00000000..e507ecdc --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,42 @@ + + + + + Get Users + + + + + + + + + +
+
+
+
+
+
+
+ + + +
+
+
+
+ +
+
+
+
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/userlist.html b/src/main/resources/templates/userlist.html new file mode 100644 index 00000000..1ae05ea5 --- /dev/null +++ b/src/main/resources/templates/userlist.html @@ -0,0 +1,76 @@ + + + + + Users + + + + + + + + + + + + +
+
+
+
+ +
+

Users from API

+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
First NameLast NameGenderEmailLanguagePhone
JoeBuckMalefoo@example.comEnglish132-123-1234
JoeBuckMalefoo@example.comEnglish132-123-1234
JoeBuckMalefoo@example.comEnglish132-123-1234
+
+
+
+
+
+
+ + + \ No newline at end of file diff --git a/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java b/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java new file mode 100644 index 00000000..221e6a83 --- /dev/null +++ b/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java @@ -0,0 +1,36 @@ +package guru.springframework.springrestclientexamples.services; + +import guru.springframework.api.domain.User; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; + +import static org.junit.Assert.assertEquals; + +/** + * Created by ccabo 8/22/19 + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class ApiServiceImplTest { + + @Autowired + ApiService apiService; + + @Before + public void setUp() throws Exception{ + } + + @Test + public void testGetUsers() throws Exception{ + + List users = apiService.getUser(4); + + assertEquals(4, users.size()); + } +} \ No newline at end of file From 6ac24a141a33342da0f671f1239c425a2317ad66 Mon Sep 17 00:00:00 2001 From: Cesar Nicolas Cabo Date: Sat, 24 Aug 2019 01:08:36 -0300 Subject: [PATCH 3/5] New test added, and apiURI added into ApiServiceImpl --- .../controllers/UserController.java | 2 +- .../services/ApiService.java | 4 +- .../services/ApiServiceImpl.java | 15 ++++++- src/main/resources/application.properties | 1 + .../services/ApiServiceImplTest.java | 43 ++++++++++++------- 5 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java b/src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java index c3f80862..977e89fe 100644 --- a/src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java +++ b/src/main/java/guru/springframework/springrestclientexamples/controllers/UserController.java @@ -24,5 +24,5 @@ public String index() { return "index"; } - + // TODO: 23/8/2019 Refactor comming up! } diff --git a/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java b/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java index 93d37476..3d2e383f 100644 --- a/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java +++ b/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java @@ -2,6 +2,8 @@ import guru.springframework.api.domain.User; import org.springframework.stereotype.Service; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; import java.util.List; @@ -11,5 +13,5 @@ @Service public interface ApiService { - List getUser(Integer limit); + List getUsers(Integer limit); } diff --git a/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java b/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java index 7dafaf20..21256393 100644 --- a/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java +++ b/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java @@ -2,8 +2,11 @@ import guru.springframework.api.domain.User; import guru.springframework.api.domain.UserData; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; import java.util.List; @@ -14,15 +17,23 @@ public class ApiServiceImpl implements ApiService { private RestTemplate restTemplate; + private final String api_url; - public ApiServiceImpl(RestTemplate restTemplate) { + @Autowired + public ApiServiceImpl(RestTemplate restTemplate, @Value("${api_url}") String api_url) { this.restTemplate = restTemplate; + this.api_url = api_url; } @Override public List getUser(Integer limit) { + + UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder + .fromUriString(api_url) + .queryParam("limit", limit); + UserData userData = restTemplate - .getForObject("http://apifaketory.com/api/user?limit=" + limit, UserData.class); + .getForObject(uriComponentsBuilder.toUriString(), UserData.class); return userData.getData(); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e69de29b..a1fa5d04 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +api_url=http://apifaketory.com/api/user \ No newline at end of file diff --git a/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java b/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java index 221e6a83..dba953d8 100644 --- a/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java +++ b/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java @@ -1,16 +1,14 @@ package guru.springframework.springrestclientexamples.services; -import guru.springframework.api.domain.User; -import org.junit.Before; +import com.fasterxml.jackson.databind.JsonNode; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestTemplate; -import java.util.List; - -import static org.junit.Assert.assertEquals; +import java.util.HashMap; +import java.util.Map; /** * Created by ccabo 8/22/19 @@ -19,18 +17,33 @@ @SpringBootTest public class ApiServiceImplTest { - @Autowired - ApiService apiService; - - @Before - public void setUp() throws Exception{ - } + public static final String API_ROOT = "https://virtserver.swaggerhub.com/CesarNicolasCabo/ApiTest/1.0.0"; @Test - public void testGetUsers() throws Exception{ + public void getInventory() throws Exception{ + String apiUrl = API_ROOT + "/inventory/"; - List users = apiService.getUser(4); + RestTemplate restTemplate = new RestTemplate(); - assertEquals(4, users.size()); + JsonNode jsonNode = restTemplate.getForObject(apiUrl, JsonNode.class); + + System.out.println("Response: "); + System.out.println(jsonNode.toString()); } + + // FIX THIS + /*@Test + public void createNewInventoryItem() { + String apiUrl = API_ROOT + "/inventory/"; + + RestTemplate restTemplate = new RestTemplate(); + + Map postMap = new HashMap<>(); + postMap.put("name", "Widget Adapter 2"); + + JsonNode jsonNode = restTemplate.postForObject(apiUrl, postMap, JsonNode.class); + + System.out.println("Response: "); + System.out.println(jsonNode.toString()); + }*/ } \ No newline at end of file From ebdff0880d31d42d82b4fa7433be92c104f66591 Mon Sep 17 00:00:00 2001 From: Cesar Nicolas Cabo Date: Sat, 24 Aug 2019 01:19:32 -0300 Subject: [PATCH 4/5] Flux added --- .../services/ApiService.java | 2 ++ .../services/ApiServiceImpl.java | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java b/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java index 3d2e383f..fbc760ab 100644 --- a/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java +++ b/src/main/java/guru/springframework/springrestclientexamples/services/ApiService.java @@ -14,4 +14,6 @@ public interface ApiService { List getUsers(Integer limit); + + Flux getUsers(Mono limit); } diff --git a/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java b/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java index 21256393..6e470037 100644 --- a/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java +++ b/src/main/java/guru/springframework/springrestclientexamples/services/ApiServiceImpl.java @@ -4,9 +4,13 @@ import guru.springframework.api.domain.UserData; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; +import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.util.UriComponentsBuilder; +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; import java.util.List; @@ -26,7 +30,7 @@ public ApiServiceImpl(RestTemplate restTemplate, @Value("${api_url}") String api } @Override - public List getUser(Integer limit) { + public List getUsers(Integer limit) { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder .fromUriString(api_url) @@ -36,4 +40,16 @@ public List getUser(Integer limit) { .getForObject(uriComponentsBuilder.toUriString(), UserData.class); return userData.getData(); } + + @Override + public Flux getUsers(Mono limit) { + + return WebClient.create(api_url) + .get() + .uri(uriBuilder -> uriBuilder.queryParam("limit", limit.block()).build()) + .accept(MediaType.APPLICATION_JSON) + .exchange() + .flatMap(resp -> resp.bodyToMono(UserData.class)) + .flatMapIterable(UserData::getData); + } } From f15b6f0b2bf079c4de4c4803e0d868907c321971 Mon Sep 17 00:00:00 2001 From: Cesar Nicolas Cabo Date: Sat, 24 Aug 2019 10:27:08 -0300 Subject: [PATCH 5/5] Deleted unused test --- .../services/ApiServiceImplTest.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java b/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java index dba953d8..03a0619a 100644 --- a/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java +++ b/src/test/java/guru/springframework/springrestclientexamples/services/ApiServiceImplTest.java @@ -30,20 +30,4 @@ public void getInventory() throws Exception{ System.out.println("Response: "); System.out.println(jsonNode.toString()); } - - // FIX THIS - /*@Test - public void createNewInventoryItem() { - String apiUrl = API_ROOT + "/inventory/"; - - RestTemplate restTemplate = new RestTemplate(); - - Map postMap = new HashMap<>(); - postMap.put("name", "Widget Adapter 2"); - - JsonNode jsonNode = restTemplate.postForObject(apiUrl, postMap, JsonNode.class); - - System.out.println("Response: "); - System.out.println(jsonNode.toString()); - }*/ } \ No newline at end of file