Skip to content

Commit 262e5f7

Browse files
committed
Polish
1 parent 2515134 commit 262e5f7

File tree

8 files changed

+114
-143
lines changed

8 files changed

+114
-143
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,8 @@ public <T> FluxExchangeResult<T> decodeBody(ResolvableType elementType) {
300300
}
301301

302302
public EntityExchangeResult<Void> consumeEmpty() {
303-
assertWithDiagnostics(() -> {
304-
DataBuffer buffer = this.response.body(toDataBuffers()).blockFirst(getTimeout());
305-
assertTrue("Expected empty body", buffer == null);
306-
});
303+
DataBuffer buffer = this.response.body(toDataBuffers()).blockFirst(getTimeout());
304+
assertWithDiagnostics(() -> assertTrue("Expected empty body", buffer == null));
307305
return new EntityExchangeResult<>(this, null);
308306
}
309307
}
@@ -344,10 +342,8 @@ public BodySpec expectBody() {
344342

345343
@Override
346344
public ResponseSpec consumeWith(Consumer<ExchangeResult> consumer) {
347-
return this.result.assertWithDiagnosticsAndReturn(() -> {
348-
consumer.accept(this.result);
349-
return this;
350-
});
345+
this.result.assertWithDiagnostics(() -> consumer.accept(this.result));
346+
return this;
351347
}
352348

353349
@Override
@@ -402,10 +398,9 @@ public DefaultSingleValueBodySpec(EntityExchangeResult<?> result) {
402398

403399
@Override
404400
public <T> EntityExchangeResult<T> isEqualTo(T expected) {
405-
return this.result.assertWithDiagnosticsAndReturn(() -> {
406-
assertEquals("Response body", expected, this.result.getResponseBody());
407-
return returnResult();
408-
});
401+
Object actual = this.result.getResponseBody();
402+
this.result.assertWithDiagnostics(() -> assertEquals("Response body", expected, actual));
403+
return returnResult();
409404
}
410405

411406
@SuppressWarnings("unchecked")
@@ -427,10 +422,9 @@ public DefaultListBodySpec(EntityExchangeResult<List<?>> result) {
427422

428423
@Override
429424
public <T> EntityExchangeResult<List<T>> isEqualTo(List<T> expected) {
430-
return this.result.assertWithDiagnosticsAndReturn(() -> {
431-
assertEquals("Response body", expected, this.result.getResponseBody());
432-
return returnResult();
433-
});
425+
List<?> actual = this.result.getResponseBody();
426+
this.result.assertWithDiagnostics(() -> assertEquals("Response body", expected, actual));
427+
return returnResult();
434428
}
435429

436430
@Override
@@ -440,21 +434,19 @@ public ListBodySpec hasSize(int size) {
440434

441435
@Override
442436
public ListBodySpec contains(Object... elements) {
443-
this.result.assertWithDiagnostics(() -> {
444-
List<Object> elementList = Arrays.asList(elements);
445-
String message = "Response body does not contain " + elementList;
446-
assertTrue(message, this.result.getResponseBody().containsAll(elementList));
447-
});
437+
List<?> expected = Arrays.asList(elements);
438+
List<?> actual = this.result.getResponseBody();
439+
String message = "Response body does not contain " + expected;
440+
this.result.assertWithDiagnostics(() -> assertTrue(message, actual.containsAll(expected)));
448441
return this;
449442
}
450443

451444
@Override
452445
public ListBodySpec doesNotContain(Object... elements) {
453-
this.result.assertWithDiagnostics(() -> {
454-
List<Object> elementList = Arrays.asList(elements);
455-
String message = "Response body should have contained " + elementList;
456-
assertTrue(message, !this.result.getResponseBody().containsAll(Arrays.asList(elements)));
457-
});
446+
List<?> expected = Arrays.asList(elements);
447+
List<?> actual = this.result.getResponseBody();
448+
String message = "Response body should have contained " + expected;
449+
this.result.assertWithDiagnostics(() -> assertTrue(message, !actual.containsAll(expected)));
458450
return this;
459451
}
460452

@@ -507,43 +499,38 @@ public DefaultMapBodySpec(EntityExchangeResult<Map<?, ?>> result) {
507499

508500
@Override
509501
public <K, V> EntityExchangeResult<Map<K, V>> isEqualTo(Map<K, V> expected) {
510-
return this.result.assertWithDiagnosticsAndReturn(() -> {
511-
assertEquals("Response body map", expected, getBody());
512-
return returnResult();
513-
});
502+
String message = "Response body map";
503+
this.result.assertWithDiagnostics(() -> assertEquals(message, expected, getBody()));
504+
return returnResult();
514505
}
515506

516507
@Override
517508
public MapBodySpec hasSize(int size) {
518-
return this.result.assertWithDiagnosticsAndReturn(() -> {
519-
assertEquals("Response body map size", size, getBody().size());
520-
return this;
521-
});
509+
String message = "Response body map size";
510+
this.result.assertWithDiagnostics(() -> assertEquals(message, size, getBody().size()));
511+
return this;
522512
}
523513

524514
@Override
525515
public MapBodySpec contains(Object key, Object value) {
526-
return this.result.assertWithDiagnosticsAndReturn(() -> {
527-
assertEquals("Response body map value for key " + key, value, getBody().get(key));
528-
return this;
529-
});
516+
String message = "Response body map value for key " + key;
517+
this.result.assertWithDiagnostics(() -> assertEquals(message, value, getBody().get(key)));
518+
return this;
530519
}
531520

532521
@Override
533522
public MapBodySpec containsKeys(Object... keys) {
534-
return this.result.assertWithDiagnosticsAndReturn(() -> {
535-
List<?> missing = Arrays.stream(keys).filter(k -> !getBody().containsKey(k)).collect(toList());
536-
assertTrue("Response body map does not contain keys " + missing, missing.isEmpty());
537-
return this;
538-
});
523+
List<?> missing = Arrays.stream(keys).filter(k -> !getBody().containsKey(k)).collect(toList());
524+
String message = "Response body map does not contain keys " + missing;
525+
this.result.assertWithDiagnostics(() -> assertTrue(message, missing.isEmpty()));
526+
return this;
539527
}
540528

541529
@Override
542530
public MapBodySpec containsValues(Object... values) {
543-
this.result.assertWithDiagnostics(() -> {
544-
List<?> missing = Arrays.stream(values).filter(v -> !getBody().containsValue(v)).collect(toList());
545-
assertTrue("Response body map does not contain values " + missing, missing.isEmpty());
546-
});
531+
List<?> missing = Arrays.stream(values).filter(v -> !getBody().containsValue(v)).collect(toList());
532+
String message = "Response body map does not contain values " + missing;
533+
this.result.assertWithDiagnostics(() -> assertTrue(message, missing.isEmpty()));
547534
return this;
548535
}
549536

spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.nio.charset.StandardCharsets;
2121
import java.util.Arrays;
2222
import java.util.List;
23-
import java.util.function.Supplier;
2423
import java.util.stream.Collectors;
2524

2625
import reactor.core.publisher.MonoProcessor;
@@ -33,16 +32,17 @@
3332
import org.springframework.util.MultiValueMap;
3433

3534
/**
36-
* Simple container for request and response details from an exchange performed
35+
* Provides access to request and response details from an exchange performed
3736
* through the {@link WebTestClient}.
3837
*
39-
* <p>When an {@code ExchangeResult} is first created it has only the status and
40-
* headers of the response available. When the response body is extracted, the
41-
* {@code ExchangeResult} is re-created as either {@link EntityExchangeResult}
42-
* or {@link FluxExchangeResult} that further expose extracted entities.
38+
* <p>When an {@code ExchangeResult} is first created it has the status and the
39+
* headers of the response ready. Later when the response body is extracted,
40+
* the {@code ExchangeResult} is re-created as {@link EntityExchangeResult} or
41+
* {@link FluxExchangeResult} also exposing the extracted entities.
4342
*
44-
* <p>Raw request and response content may also be accessed once complete via
45-
* {@link #getRequestContent()} or {@link #getResponseContent()}.
43+
* <p>Serialized request and response content may also be accessed through the
44+
* methods {@link #getRequestContent()} and {@link #getResponseContent()} after
45+
* that content has been fully read or written.
4646
*
4747
* @author Rossen Stoyanchev
4848
* @since 5.0
@@ -137,8 +137,9 @@ public MonoProcessor<byte[]> getResponseContent() {
137137

138138

139139
/**
140-
* Execute the given Runnable in the context of "this" instance and decorate
141-
* any {@link AssertionError}s raised with request and response details.
140+
* Execute the given Runnable, catch any {@link AssertionError}, decorate
141+
* with {@code AssertionError} containing diagnostic information about the
142+
* request and response, and then re-throw.
142143
*/
143144
public void assertWithDiagnostics(Runnable assertion) {
144145
try {
@@ -149,32 +150,19 @@ public void assertWithDiagnostics(Runnable assertion) {
149150
}
150151
}
151152

152-
/**
153-
* Variant of {@link #assertWithDiagnostics(Runnable)} that passes through
154-
* a return value from the assertion code.
155-
*/
156-
public <T> T assertWithDiagnosticsAndReturn(Supplier<T> assertion) {
157-
try {
158-
return assertion.get();
159-
}
160-
catch (AssertionError ex) {
161-
throw new AssertionError("Assertion failed on the following exchange:" + this, ex);
162-
}
163-
}
164-
165153

166154
@Override
167155
public String toString() {
168156
return "\n" +
169157
"> " + getMethod() + " " + getUrl() + "\n" +
170-
"> " + formatHeaders("> ", getRequestHeaders()) + "\n" +
158+
"> " + formatHeaders(getRequestHeaders(), "\n> ") + "\n" +
171159
"\n" +
172-
formatContent(getRequestHeaders().getContentType(), getRequestContent()) + "\n" +
160+
formatBody(getRequestHeaders().getContentType(), getRequestContent()) + "\n" +
173161
"\n" +
174162
"< " + getStatus() + " " + getStatusReason() + "\n" +
175-
"< " + formatHeaders("< ", getResponseHeaders()) + "\n" +
163+
"< " + formatHeaders(getResponseHeaders(), "\n< ") + "\n" +
176164
"\n" +
177-
formatContent(getResponseHeaders().getContentType(), getResponseContent()) + "\n\n";
165+
formatBody(getResponseHeaders().getContentType(), getResponseContent()) + "\n\n";
178166
}
179167

180168
private String getStatusReason() {
@@ -185,13 +173,13 @@ private String getStatusReason() {
185173
return reason;
186174
}
187175

188-
private String formatHeaders(String linePrefix, HttpHeaders headers) {
176+
private String formatHeaders(HttpHeaders headers, String delimiter) {
189177
return headers.entrySet().stream()
190178
.map(entry -> entry.getKey() + ": " + entry.getValue())
191-
.collect(Collectors.joining("\n" + linePrefix));
179+
.collect(Collectors.joining(delimiter));
192180
}
193181

194-
private String formatContent(MediaType contentType, MonoProcessor<byte[]> body) {
182+
private String formatBody(MediaType contentType, MonoProcessor<byte[]> body) {
195183
if (body.isSuccess()) {
196184
byte[] bytes = body.blockMillis(0);
197185
if (bytes.length == 0) {

spring-test/src/main/java/org/springframework/test/web/reactive/server/HeaderAssertions.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ public WebTestClient.ResponseSpec valueEquals(String headerName, String... value
6060
* @param pattern String pattern to pass to {@link Pattern#compile(String)}
6161
*/
6262
public WebTestClient.ResponseSpec valueMatches(String name, String pattern) {
63-
return this.exchangeResult.assertWithDiagnosticsAndReturn(() -> {
64-
String value = getHeaders().getFirst(name);
65-
assertTrue(getMessage(name) + " not found", value != null);
66-
boolean match = Pattern.compile(pattern).matcher(value).matches();
67-
assertTrue(getMessage(name) + "=\'" + value + "\' does not match \'" + pattern + "\'", match);
68-
return this.responseSpec;
69-
});
63+
String value = getHeaders().getFirst(name);
64+
assertTrue(getMessage(name) + " not found", value != null);
65+
boolean match = Pattern.compile(pattern).matcher(value).matches();
66+
String message = getMessage(name) + "=\'" + value + "\' does not match \'" + pattern + "\'";
67+
this.exchangeResult.assertWithDiagnostics(() -> assertTrue(message, match));
68+
return this.responseSpec;
7069
}
7170

7271
/**
@@ -123,10 +122,8 @@ private String getMessage(String headerName) {
123122
}
124123

125124
private WebTestClient.ResponseSpec assertHeader(String name, Object expected, Object actual) {
126-
return this.exchangeResult.assertWithDiagnosticsAndReturn(() -> {
127-
assertEquals(getMessage(name), expected, actual);
128-
return this.responseSpec;
129-
});
125+
this.exchangeResult.assertWithDiagnostics(() -> assertEquals(getMessage(name), expected, actual));
126+
return this.responseSpec;
130127
}
131128

132129
}

spring-test/src/main/java/org/springframework/test/web/reactive/server/StatusAssertions.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ public WebTestClient.ResponseSpec isEqualTo(HttpStatus status) {
5151
* Assert the response status as an integer.
5252
*/
5353
public WebTestClient.ResponseSpec isEqualTo(int status) {
54-
return this.exchangeResult.assertWithDiagnosticsAndReturn(() -> {
55-
assertEquals("Status", status, this.exchangeResult.getStatus().value());
56-
return this.responseSpec;
57-
});
54+
int actual = this.exchangeResult.getStatus().value();
55+
this.exchangeResult.assertWithDiagnostics(() -> assertEquals("Status", status, actual));
56+
return this.responseSpec;
5857
}
5958

6059
/**
@@ -146,11 +145,10 @@ public WebTestClient.ResponseSpec isNotFound() {
146145
* Assert the response error message.
147146
*/
148147
public WebTestClient.ResponseSpec reasonEquals(String reason) {
149-
return this.exchangeResult.assertWithDiagnosticsAndReturn(() -> {
150-
HttpStatus status = this.exchangeResult.getStatus();
151-
assertEquals("Response status reason", reason, status.getReasonPhrase());
152-
return this.responseSpec;
153-
});
148+
String actual = this.exchangeResult.getStatus().getReasonPhrase();
149+
String message = "Response status reason";
150+
this.exchangeResult.assertWithDiagnostics(() -> assertEquals(message, reason, actual));
151+
return this.responseSpec;
154152
}
155153

156154
/**
@@ -192,19 +190,16 @@ public WebTestClient.ResponseSpec is5xxServerError() {
192190
// Private methods
193191

194192
private WebTestClient.ResponseSpec assertStatusAndReturn(HttpStatus expected) {
195-
return this.exchangeResult.assertWithDiagnosticsAndReturn(() -> {
196-
assertEquals("Status", expected, this.exchangeResult.getStatus());
197-
return this.responseSpec;
198-
});
193+
HttpStatus actual = this.exchangeResult.getStatus();
194+
this.exchangeResult.assertWithDiagnostics(() -> assertEquals("Status", expected, actual));
195+
return this.responseSpec;
199196
}
200197

201198
private WebTestClient.ResponseSpec assertSeriesAndReturn(HttpStatus.Series expected) {
202-
return this.exchangeResult.assertWithDiagnosticsAndReturn(() -> {
203-
HttpStatus status = this.exchangeResult.getStatus();
204-
String message = "Range for response status value " + status;
205-
assertEquals(message, expected, status.series());
206-
return this.responseSpec;
207-
});
199+
HttpStatus status = this.exchangeResult.getStatus();
200+
String message = "Range for response status value " + status;
201+
this.exchangeResult.assertWithDiagnostics(() -> assertEquals(message, expected, status.series()));
202+
return this.responseSpec;
208203
}
209204

210205
}

spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,10 @@ interface TypeBodySpec {
512512
ListBodySpec list(int elementCount);
513513

514514
/**
515-
* Return request and response details from the exchange including the
516-
* response body as a {@code Flux<T>} and available for example for use
517-
* with a {@code StepVerifier} from Project Reactor.
515+
* Return request and response details for the exchange incluidng the
516+
* response body decoded as {@code Flux<T>} where {@code <T>} is the
517+
* expected element type. The returned {@code Flux} may for example be
518+
* verified with the Reactor {@code StepVerifier}.
518519
*/
519520
<T> FluxExchangeResult<T> returnResult();
520521
}
@@ -530,7 +531,7 @@ interface SingleValueBodySpec {
530531
<T> EntityExchangeResult<T> isEqualTo(T expected);
531532

532533
/**
533-
* Return request and response details from the exchange including the
534+
* Return request and response details for the exchange including the
534535
* extracted response body.
535536
*/
536537
<T> EntityExchangeResult<T> returnResult();
@@ -565,7 +566,7 @@ interface ListBodySpec {
565566
ListBodySpec doesNotContain(Object... elements);
566567

567568
/**
568-
* Return request and response details from the exchange including the
569+
* Return request and response details for the exchange including the
569570
* extracted response body.
570571
*/
571572
<T> EntityExchangeResult<List<T>> returnResult();
@@ -630,7 +631,7 @@ interface MapBodySpec {
630631
MapBodySpec containsValues(Object... values);
631632

632633
/**
633-
* Return request and response details from the exchange including the
634+
* Return request and response details for the exchange including the
634635
* extracted response body.
635636
*/
636637
<K, V> EntityExchangeResult<Map<K, V>> returnResult();

0 commit comments

Comments
 (0)