-
Notifications
You must be signed in to change notification settings - Fork 53.6k
Expand file tree
/
Copy pathRestClientLiveTest.java
More file actions
168 lines (140 loc) · 4.77 KB
/
RestClientLiveTest.java
File metadata and controls
168 lines (140 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.baeldung.restclient;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.List;
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.test.context.TestPropertySource;
import org.springframework.web.client.RestClient;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations="classpath:connectiondetails/application-r2dbc.properties")
public class RestClientLiveTest {
@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<String>() {});
}).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));
}
}