BAEL-6911 fix dependencies issues, add missing snipets (#14935)

This commit is contained in:
mdabrowski-eu 2023-10-09 13:33:39 +02:00 committed by GitHub
parent bb29bb9dcd
commit ff6d3e7393
6 changed files with 101 additions and 14 deletions

View File

@ -60,7 +60,11 @@
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId> <artifactId>junit-jupiter-params</artifactId>
<version>${jupiter.version}</version> <version>${jupiter.version}</version>
<scope>test</scope> </dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.10.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.mock-server</groupId> <groupId>org.mock-server</groupId>
@ -204,7 +208,7 @@
<properties> <properties>
<java.version>19</java.version> <java.version>19</java.version>
<mapstruct.version>1.5.2.Final</mapstruct.version> <mapstruct.version>1.5.2.Final</mapstruct.version>
<springdoc.version>2.0.0</springdoc.version> <springdoc.version>2.2.0</springdoc.version>
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version> <maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
<start-class>com.baeldung.sample.TodoApplication</start-class> <start-class>com.baeldung.sample.TodoApplication</start-class>
<mockserver.version>5.14.0</mockserver.version> <mockserver.version>5.14.0</mockserver.version>

View File

@ -6,6 +6,8 @@ public class Article {
Integer id; Integer id;
String title; String title;
public Article() {}
public Article(Integer id, String title) { public Article(Integer id, String title) {
this.id = id; this.id = id;
this.title = title; this.title = title;
@ -19,6 +21,14 @@ public class Article {
return title; return title;
} }
public void setId(Integer id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -1,5 +1,6 @@
package com.baeldung.restclient; package com.baeldung.restclient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Collection; import java.util.Collection;
@ -13,13 +14,21 @@ public class ArticleController {
Map<Integer, Article> database = new HashMap<>(); Map<Integer, Article> database = new HashMap<>();
@GetMapping @GetMapping
public Collection<Article> getArticles() { public ResponseEntity<Collection<Article>> getArticles() {
return database.values(); Collection<Article> values = database.values();
if (values.isEmpty()) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(values);
} }
@GetMapping("/{id}") @GetMapping("/{id}")
public Article getArticle(@PathVariable Integer id) { public ResponseEntity<Article> getArticle(@PathVariable("id") Integer id) {
return database.get(id); Article article = database.get(id);
if (article == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(article);
} }
@PostMapping @PostMapping
@ -28,7 +37,7 @@ public class ArticleController {
} }
@PutMapping("/{id}") @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()); assert Objects.equals(id, article.getId());
database.remove(id); database.remove(id);
database.put(id, article); database.put(id, article);

View File

@ -0,0 +1,6 @@
package com.baeldung.restclient;
public class ArticleNotFoundException extends RuntimeException {
public ArticleNotFoundException() {
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.restclient;
public class InvalidArticleResponseException extends RuntimeException {
public InvalidArticleResponseException() {
}
}

View File

@ -1,17 +1,23 @@
package com.baeldung.restclient; 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.AfterEach;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClient; import org.springframework.web.client.RestClient;
import java.util.List; import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ -22,7 +28,10 @@ public class RestClientIntegrationTest {
private String uriBase; private String uriBase;
RestClient restClient = RestClient.create(); RestClient restClient = RestClient.create();
@BeforeAll @Autowired
ObjectMapper objectMapper;
@BeforeEach
public void setup() { public void setup() {
uriBase = "http://localhost:" + port; uriBase = "http://localhost:" + port;
} }
@ -42,7 +51,7 @@ public class RestClientIntegrationTest {
.retrieve() .retrieve()
.body(String.class); .body(String.class);
assertThat(articlesAsString).isEqualTo("[]"); assertThat(articlesAsString).isEqualTo("");
} }
@Test @Test
@ -63,6 +72,48 @@ public class RestClientIntegrationTest {
assertThat(articles).isEqualTo(List.of(article)); 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<Article> articles = getArticlesWithExchange();
assertThat(articles).isEqualTo(List.of(article));
}
private List<Article> 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 @Test
void shouldPostAndPutAndGetArticles() { void shouldPostAndPutAndGetArticles() {
Article article = new Article(1, "How to use RestClient"); Article article = new Article(1, "How to use RestClient");
@ -79,7 +130,7 @@ public class RestClientIntegrationTest {
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.body(articleChanged) .body(articleChanged)
.retrieve() .retrieve()
.toBodilessEntity(); .toBodilessEntity();
List<Article> articles = restClient.get() List<Article> articles = restClient.get()
.uri(uriBase + "/articles") .uri(uriBase + "/articles")
@ -104,11 +155,12 @@ public class RestClientIntegrationTest {
.retrieve() .retrieve()
.toBodilessEntity(); .toBodilessEntity();
List<Article> articles = restClient.get() ResponseEntity<Void> entity = restClient.get()
.uri(uriBase + "/articles") .uri(uriBase + "/articles")
.accept(MediaType.APPLICATION_JSON)
.retrieve() .retrieve()
.body(new ParameterizedTypeReference<>() {}); .toBodilessEntity();
assertThat(articles).isEqualTo(List.of()); assertThat(entity.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(204));
} }
} }