[JAVA-27620] (#15396)

* [JAVA-27620]

* [JAVA-27620]

* [JAVA-27620] Comment spring-boot-3-2 module
This commit is contained in:
panos-kakos
2023-12-13 22:04:35 +02:00
committed by GitHub
parent 3e8f48d06e
commit cd85eb8b0b
10 changed files with 23 additions and 12 deletions
@@ -1,44 +0,0 @@
package com.baeldung.restclient;
import java.util.Objects;
public class Article {
Integer id;
String title;
public Article() {}
public Article(Integer id, String title) {
this.id = id;
this.title = title;
}
public Integer getId() {
return id;
}
public String getTitle() {
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;
if (o == null || getClass() != o.getClass()) return false;
Article article = (Article) o;
return Objects.equals(id, article.id) && Objects.equals(title, article.title);
}
@Override
public int hashCode() {
return Objects.hash(id, title);
}
}
@@ -1,54 +0,0 @@
package com.baeldung.restclient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@RestController
@RequestMapping("/articles")
public class ArticleController {
Map<Integer, Article> database = new HashMap<>();
@GetMapping
public ResponseEntity<Collection<Article>> getArticles() {
Collection<Article> values = database.values();
if (values.isEmpty()) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(values);
}
@GetMapping("/{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
public void createArticle(@RequestBody Article article) {
database.put(article.getId(), article);
}
@PutMapping("/{id}")
public void updateArticle(@PathVariable("id") Integer id, @RequestBody Article article) {
assert Objects.equals(id, article.getId());
database.remove(id);
database.put(id, article);
}
@DeleteMapping("/{id}")
public void deleteArticle(@PathVariable Integer id) {
database.remove(id);
}
@DeleteMapping()
public void deleteArticles() {
database.clear();
}
}
@@ -1,6 +0,0 @@
package com.baeldung.restclient;
public class ArticleNotFoundException extends RuntimeException {
public ArticleNotFoundException() {
}
}
@@ -1,6 +0,0 @@
package com.baeldung.restclient;
public class InvalidArticleResponseException extends RuntimeException {
public InvalidArticleResponseException() {
}
}
@@ -1,13 +0,0 @@
package com.baeldung.restclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestClientApplication {
public static void main(String[] args) {
SpringApplication.run(RestClientApplication.class, args);
}
}
@@ -1,166 +0,0 @@
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.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)
public class RestClientIntegrationTest {
@LocalServerPort
private int port;
private String uriBase;
RestClient restClient = RestClient.create();
@Autowired
ObjectMapper objectMapper;
@BeforeEach
public void setup() {
uriBase = "http://localhost:" + port;
}
@AfterEach
public void teardown() {
restClient.delete()
.uri(uriBase + "/articles")
.retrieve()
.toBodilessEntity();
}
@Test
void shouldGetArticlesAndReturnString() {
String articlesAsString = restClient.get()
.uri(uriBase + "/articles")
.retrieve()
.body(String.class);
assertThat(articlesAsString).isEqualTo("");
}
@Test
void shouldPostAndGetArticles() {
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 = restClient.get()
.uri(uriBase + "/articles")
.retrieve()
.body(new ParameterizedTypeReference<>() {});
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");
restClient.post()
.uri(uriBase + "/articles")
.contentType(MediaType.APPLICATION_JSON)
.body(article)
.retrieve()
.toBodilessEntity();
Article articleChanged = new Article(1, "How to use RestClient even better");
restClient.put()
.uri(uriBase + "/articles/1")
.contentType(MediaType.APPLICATION_JSON)
.body(articleChanged)
.retrieve()
.toBodilessEntity();
List<Article> articles = restClient.get()
.uri(uriBase + "/articles")
.retrieve()
.body(new ParameterizedTypeReference<>() {});
assertThat(articles).isEqualTo(List.of(articleChanged));
}
@Test
void shouldPostAndDeleteArticles() {
Article article = new Article(1, "How to use RestClient");
restClient.post()
.uri(uriBase + "/articles")
.contentType(MediaType.APPLICATION_JSON)
.body(article)
.retrieve()
.toBodilessEntity();
restClient.delete()
.uri(uriBase + "/articles")
.retrieve()
.toBodilessEntity();
ResponseEntity<Void> entity = restClient.get()
.uri(uriBase + "/articles")
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.toBodilessEntity();
assertThat(entity.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(204));
}
}