BAEL-6911 fix dependencies issues, add missing snipets (#14935)
This commit is contained in:
parent
bb29bb9dcd
commit
ff6d3e7393
|
@ -60,7 +60,11 @@
|
|||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<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>
|
||||
<groupId>org.mock-server</groupId>
|
||||
|
@ -204,7 +208,7 @@
|
|||
<properties>
|
||||
<java.version>19</java.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>
|
||||
<start-class>com.baeldung.sample.TodoApplication</start-class>
|
||||
<mockserver.version>5.14.0</mockserver.version>
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<Integer, Article> database = new HashMap<>();
|
||||
|
||||
@GetMapping
|
||||
public Collection<Article> getArticles() {
|
||||
return database.values();
|
||||
public ResponseEntity<Collection<Article>> getArticles() {
|
||||
Collection<Article> 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<Article> 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);
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.restclient;
|
||||
|
||||
public class ArticleNotFoundException extends RuntimeException {
|
||||
public ArticleNotFoundException() {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.restclient;
|
||||
|
||||
public class InvalidArticleResponseException extends RuntimeException {
|
||||
public InvalidArticleResponseException() {
|
||||
}
|
||||
}
|
|
@ -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<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
|
||||
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<Article> articles = restClient.get()
|
||||
.uri(uriBase + "/articles")
|
||||
|
@ -104,11 +155,12 @@ public class RestClientIntegrationTest {
|
|||
.retrieve()
|
||||
.toBodilessEntity();
|
||||
|
||||
List<Article> articles = restClient.get()
|
||||
ResponseEntity<Void> 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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue