Skip to content

Commit 40f1cf6

Browse files
committed
Polish DefaultClientResponseTests and suppress "unchecked" warnings
1 parent 6da9aed commit 40f1cf6

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

spring-webflux/src/test/java/org/springframework/web/reactive/function/client/DefaultClientResponseTests.java

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.net.InetSocketAddress;
2020
import java.nio.charset.StandardCharsets;
21-
import java.util.Collections;
2221
import java.util.List;
2322
import java.util.Map;
2423
import java.util.OptionalLong;
@@ -94,8 +93,7 @@ void header() {
9493
httpHeaders.setContentType(contentType);
9594
InetSocketAddress host = InetSocketAddress.createUnresolved("localhost", 80);
9695
httpHeaders.setHost(host);
97-
List<HttpRange> range = Collections.singletonList(HttpRange.createByteRange(0, 42));
98-
httpHeaders.setRange(range);
96+
httpHeaders.setRange(List.of(HttpRange.createByteRange(0, 42)));
9997

10098
given(mockResponse.getHeaders()).willReturn(httpHeaders);
10199

@@ -124,7 +122,7 @@ void body() {
124122
mockTextPlainResponse(Flux.just(dataBuffer));
125123

126124
given(mockExchangeStrategies.messageReaders()).willReturn(
127-
Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
125+
List.of(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
128126

129127
Mono<String> resultMono = defaultClientResponse.body(toMono(String.class));
130128
assertThat(resultMono.block()).isEqualTo("foo");
@@ -137,7 +135,7 @@ void bodyToMono() {
137135
mockTextPlainResponse(Flux.just(dataBuffer));
138136

139137
given(mockExchangeStrategies.messageReaders()).willReturn(
140-
Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
138+
List.of(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
141139

142140
Mono<String> resultMono = defaultClientResponse.bodyToMono(String.class);
143141
assertThat(resultMono.block()).isEqualTo("foo");
@@ -150,7 +148,7 @@ void bodyToMonoTypeReference() {
150148
mockTextPlainResponse(Flux.just(dataBuffer));
151149

152150
given(mockExchangeStrategies.messageReaders()).willReturn(
153-
Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
151+
List.of(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
154152

155153
Mono<String> resultMono = defaultClientResponse.bodyToMono(STRING_TYPE);
156154
assertThat(resultMono.block()).isEqualTo("foo");
@@ -163,11 +161,11 @@ void bodyToFlux() {
163161
mockTextPlainResponse(Flux.just(dataBuffer));
164162

165163
given(mockExchangeStrategies.messageReaders()).willReturn(
166-
Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
164+
List.of(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
167165

168166
Flux<String> resultFlux = defaultClientResponse.bodyToFlux(String.class);
169167
Mono<List<String>> result = resultFlux.collectList();
170-
assertThat(result.block()).isEqualTo(Collections.singletonList("foo"));
168+
assertThat(result.block()).containsExactly("foo");
171169
}
172170

173171
@Test
@@ -177,11 +175,11 @@ void bodyToFluxTypeReference() {
177175
mockTextPlainResponse(Flux.just(dataBuffer));
178176

179177
given(mockExchangeStrategies.messageReaders()).willReturn(
180-
Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
178+
List.of(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
181179

182180
Flux<String> resultFlux = defaultClientResponse.bodyToFlux(STRING_TYPE);
183181
Mono<List<String>> result = resultFlux.collectList();
184-
assertThat(result.block()).isEqualTo(Collections.singletonList("foo"));
182+
assertThat(result.block()).containsExactly("foo");
185183
}
186184

187185
@Test
@@ -192,7 +190,7 @@ void toEntity() {
192190
mockTextPlainResponse(Flux.just(dataBuffer));
193191

194192
given(mockExchangeStrategies.messageReaders()).willReturn(
195-
Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
193+
List.of(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
196194

197195
ResponseEntity<String> result = defaultClientResponse.toEntity(String.class).block();
198196
assertThat(result.getBody()).isEqualTo("foo");
@@ -213,7 +211,7 @@ void toEntityWithUnknownStatusCode() {
213211
given(mockResponse.getBody()).willReturn(Flux.just(dataBuffer));
214212

215213
given(mockExchangeStrategies.messageReaders()).willReturn(
216-
Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
214+
List.of(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
217215

218216
ResponseEntity<String> result = defaultClientResponse.toEntity(String.class).block();
219217
assertThat(result.getBody()).isEqualTo("foo");
@@ -230,7 +228,7 @@ void toEntityTypeReference() {
230228
mockTextPlainResponse(Flux.just(dataBuffer));
231229

232230
given(mockExchangeStrategies.messageReaders()).willReturn(
233-
Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
231+
List.of(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
234232

235233
ResponseEntity<String> result = defaultClientResponse.toEntity(STRING_TYPE).block();
236234
assertThat(result.getBody()).isEqualTo("foo");
@@ -247,10 +245,10 @@ void toEntityList() {
247245
mockTextPlainResponse(Flux.just(dataBuffer));
248246

249247
given(mockExchangeStrategies.messageReaders()).willReturn(
250-
Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
248+
List.of(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
251249

252250
ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block();
253-
assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
251+
assertThat(result.getBody()).containsExactly("foo");
254252
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
255253
assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
256254
assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
@@ -268,10 +266,10 @@ void toEntityListWithUnknownStatusCode() {
268266
given(mockResponse.getBody()).willReturn(Flux.just(dataBuffer));
269267

270268
given(mockExchangeStrategies.messageReaders()).willReturn(
271-
Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
269+
List.of(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
272270

273271
ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(String.class).block();
274-
assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
272+
assertThat(result.getBody()).containsExactly("foo");
275273
assertThat(result.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(999));
276274
assertThat(result.getStatusCodeValue()).isEqualTo(999);
277275
assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
@@ -286,10 +284,10 @@ void toEntityListTypeReference() {
286284
mockTextPlainResponse(Flux.just(dataBuffer));
287285

288286
given(mockExchangeStrategies.messageReaders()).willReturn(
289-
Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
287+
List.of(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes())));
290288

291289
ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(STRING_TYPE).block();
292-
assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
290+
assertThat(result.getBody()).containsExactly("foo");
293291
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
294292
assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
295293
assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
@@ -305,18 +303,18 @@ void createException() {
305303
given(mockResponse.getBody()).willReturn(Flux.just(dataBuffer));
306304

307305
given(mockExchangeStrategies.messageReaders()).willReturn(
308-
Collections.singletonList(new DecoderHttpMessageReader<>(new ByteArrayDecoder())));
306+
List.of(new DecoderHttpMessageReader<>(new ByteArrayDecoder())));
309307

310308
Mono<WebClientResponseException> resultMono = defaultClientResponse.createException();
311309
WebClientResponseException exception = resultMono.block();
312310
assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
313311
assertThat(exception.getMessage()).isEqualTo("404 Not Found");
314-
assertThat(exception.getHeaders()).containsExactly(entry("Content-Type",
315-
Collections.singletonList("text/plain")));
312+
assertThat(exception.getHeaders()).containsExactly(entry("Content-Type", List.of("text/plain")));
316313
assertThat(exception.getResponseBodyAsByteArray()).isEqualTo(bytes);
317314
}
318315

319316
@Test
317+
@SuppressWarnings("unchecked")
320318
void createExceptionAndDecodeContent() {
321319
byte[] bytes = "{\"name\":\"Jason\"}".getBytes(StandardCharsets.UTF_8);
322320
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
@@ -330,10 +328,11 @@ void createExceptionAndDecodeContent() {
330328
new DecoderHttpMessageReader<>(new Jackson2JsonDecoder())));
331329

332330
WebClientResponseException ex = defaultClientResponse.createException().block();
333-
assertThat(ex.getResponseBodyAs(Map.class)).hasSize(1).containsEntry("name", "Jason");
331+
assertThat(ex.getResponseBodyAs(Map.class)).containsExactly(entry("name", "Jason"));
334332
}
335333

336334
@Test
335+
@SuppressWarnings("unchecked")
337336
void createExceptionAndDecodeWithoutContent() {
338337
byte[] bytes = new byte[0];
339338
DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
@@ -360,7 +359,7 @@ void createError() {
360359
given(mockResponse.getBody()).willReturn(Flux.just(dataBuffer));
361360

362361
given(mockExchangeStrategies.messageReaders()).willReturn(
363-
Collections.singletonList(new DecoderHttpMessageReader<>(new ByteArrayDecoder())));
362+
List.of(new DecoderHttpMessageReader<>(new ByteArrayDecoder())));
364363

365364
Mono<String> resultMono = defaultClientResponse.createError();
366365
StepVerifier.create(resultMono)
@@ -369,10 +368,8 @@ void createError() {
369368
WebClientResponseException exception = (WebClientResponseException) t;
370369
assertThat(exception.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
371370
assertThat(exception.getMessage()).isEqualTo("404 Not Found");
372-
assertThat(exception.getHeaders()).containsExactly(entry("Content-Type",
373-
Collections.singletonList("text/plain")));
371+
assertThat(exception.getHeaders()).containsExactly(entry("Content-Type",List.of("text/plain")));
374372
assertThat(exception.getResponseBodyAsByteArray()).isEqualTo(bytes);
375-
376373
})
377374
.verify();
378375
}

0 commit comments

Comments
 (0)