From ff6d3e73935c9ad1243ff4e7c49a087d84ca9a28 Mon Sep 17 00:00:00 2001 From: mdabrowski-eu <57441874+mdabrowski-eu@users.noreply.github.com> Date: Mon, 9 Oct 2023 13:33:39 +0200 Subject: [PATCH] BAEL-6911 fix dependencies issues, add missing snipets (#14935) --- spring-boot-modules/spring-boot-3/pom.xml | 8 ++- .../java/com/baeldung/restclient/Article.java | 10 +++ .../restclient/ArticleController.java | 19 ++++-- .../restclient/ArticleNotFoundException.java | 6 ++ .../InvalidArticleResponseException.java | 6 ++ .../restclient/RestClientIntegrationTest.java | 66 +++++++++++++++++-- 6 files changed, 101 insertions(+), 14 deletions(-) create mode 100644 spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleNotFoundException.java create mode 100644 spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/InvalidArticleResponseException.java diff --git a/spring-boot-modules/spring-boot-3/pom.xml b/spring-boot-modules/spring-boot-3/pom.xml index 7dd44c89dc..bb8c5dd53c 100644 --- a/spring-boot-modules/spring-boot-3/pom.xml +++ b/spring-boot-modules/spring-boot-3/pom.xml @@ -60,7 +60,11 @@ org.junit.jupiter junit-jupiter-params ${jupiter.version} - test + + + org.junit.platform + junit-platform-commons + 1.10.0 org.mock-server @@ -204,7 +208,7 @@ 19 1.5.2.Final - 2.0.0 + 2.2.0 3.0.0-M7 com.baeldung.sample.TodoApplication 5.14.0 diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/Article.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/Article.java index a69d5989af..892cf3b5ae 100644 --- a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/Article.java +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/Article.java @@ -6,6 +6,8 @@ public class Article { Integer id; String title; + public Article() {} + public Article(Integer id, String title) { this.id = id; this.title = title; @@ -19,6 +21,14 @@ public class Article { return title; } + public void setId(Integer id) { + this.id = id; + } + + public void setTitle(String title) { + this.title = title; + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleController.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleController.java index 62922bdcee..5e1dff6fd7 100644 --- a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleController.java +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleController.java @@ -1,5 +1,6 @@ package com.baeldung.restclient; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.Collection; @@ -13,13 +14,21 @@ public class ArticleController { Map database = new HashMap<>(); @GetMapping - public Collection
getArticles() { - return database.values(); + public ResponseEntity> getArticles() { + Collection
values = database.values(); + if (values.isEmpty()) { + return ResponseEntity.noContent().build(); + } + return ResponseEntity.ok(values); } @GetMapping("/{id}") - public Article getArticle(@PathVariable Integer id) { - return database.get(id); + public ResponseEntity
getArticle(@PathVariable("id") Integer id) { + Article article = database.get(id); + if (article == null) { + return ResponseEntity.notFound().build(); + } + return ResponseEntity.ok(article); } @PostMapping @@ -28,7 +37,7 @@ public class ArticleController { } @PutMapping("/{id}") - public void updateArticle(@PathVariable Integer id, @RequestBody Article article) { + public void updateArticle(@PathVariable("id") Integer id, @RequestBody Article article) { assert Objects.equals(id, article.getId()); database.remove(id); database.put(id, article); diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleNotFoundException.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleNotFoundException.java new file mode 100644 index 0000000000..cdd13b8330 --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/ArticleNotFoundException.java @@ -0,0 +1,6 @@ +package com.baeldung.restclient; + +public class ArticleNotFoundException extends RuntimeException { + public ArticleNotFoundException() { + } +} diff --git a/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/InvalidArticleResponseException.java b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/InvalidArticleResponseException.java new file mode 100644 index 0000000000..26ca75036e --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/main/java/com/baeldung/restclient/InvalidArticleResponseException.java @@ -0,0 +1,6 @@ +package com.baeldung.restclient; + +public class InvalidArticleResponseException extends RuntimeException { + public InvalidArticleResponseException() { + } +} diff --git a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/restclient/RestClientIntegrationTest.java b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/restclient/RestClientIntegrationTest.java index 92474c88f0..1a615faf4e 100644 --- a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/restclient/RestClientIntegrationTest.java +++ b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/restclient/RestClientIntegrationTest.java @@ -1,17 +1,23 @@ package com.baeldung.restclient; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpStatusCode; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestClient; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @@ -22,7 +28,10 @@ public class RestClientIntegrationTest { private String uriBase; RestClient restClient = RestClient.create(); - @BeforeAll + @Autowired + ObjectMapper objectMapper; + + @BeforeEach public void setup() { uriBase = "http://localhost:" + port; } @@ -42,7 +51,7 @@ public class RestClientIntegrationTest { .retrieve() .body(String.class); - assertThat(articlesAsString).isEqualTo("[]"); + assertThat(articlesAsString).isEqualTo(""); } @Test @@ -63,6 +72,48 @@ public class RestClientIntegrationTest { assertThat(articles).isEqualTo(List.of(article)); } + @Test + void shouldPostAndGetArticlesWithExchange() { + assertThatThrownBy(this::getArticlesWithExchange).isInstanceOf(ArticleNotFoundException.class); + + Article article = new Article(1, "How to use RestClient"); + restClient.post() + .uri(uriBase + "/articles") + .contentType(MediaType.APPLICATION_JSON) + .body(article) + .retrieve() + .toBodilessEntity(); + + List
articles = getArticlesWithExchange(); + + assertThat(articles).isEqualTo(List.of(article)); + } + + private List
getArticlesWithExchange() { + return restClient.get() + .uri(uriBase + "/articles") + .exchange((request, response) -> { + if (response.getStatusCode().isSameCodeAs(HttpStatusCode.valueOf(204))) { + throw new ArticleNotFoundException(); + } else if (response.getStatusCode().isSameCodeAs(HttpStatusCode.valueOf(200))) { + return objectMapper.readValue(response.getBody(), new TypeReference<>() {}); + } else { + throw new InvalidArticleResponseException(); + } + }); + } + + @Test + void shouldPostAndGetArticlesWithErrorHandling() { + assertThatThrownBy(() -> { + restClient.get() + .uri(uriBase + "/articles/1234") + .retrieve() + .onStatus(status -> status.value() == 404, (request, response) -> { throw new ArticleNotFoundException(); }) + .body(new ParameterizedTypeReference<>() {}); + }).isInstanceOf(ArticleNotFoundException.class); + } + @Test void shouldPostAndPutAndGetArticles() { Article article = new Article(1, "How to use RestClient"); @@ -79,7 +130,7 @@ public class RestClientIntegrationTest { .contentType(MediaType.APPLICATION_JSON) .body(articleChanged) .retrieve() - .toBodilessEntity(); + .toBodilessEntity(); List
articles = restClient.get() .uri(uriBase + "/articles") @@ -104,11 +155,12 @@ public class RestClientIntegrationTest { .retrieve() .toBodilessEntity(); - List
articles = restClient.get() + ResponseEntity entity = restClient.get() .uri(uriBase + "/articles") + .accept(MediaType.APPLICATION_JSON) .retrieve() - .body(new ParameterizedTypeReference<>() {}); + .toBodilessEntity(); - assertThat(articles).isEqualTo(List.of()); + assertThat(entity.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(204)); } }