Skip to content

Commit 54d6329

Browse files
committed
#241 Extends AssertionError for verification to work with Awaitility polling
1 parent 29762e6 commit 54d6329

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ dependencies {
5454
testCompile 'com.github.stefanbirkner:system-rules:1.19.0'
5555
testCompile 'io.projectreactor.ipc:reactor-netty:0.7.12.RELEASE'
5656
testCompile 'io.projectreactor:reactor-test:3.2.3.RELEASE'
57+
testCompile 'org.awaitility:awaitility:4.0.3'
5758
}
5859

5960
allprojects { subproj ->

src/main/java/io/specto/hoverfly/junit/verification/HoverflyVerificationError.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.specto.hoverfly.junit.verification;
22

3-
public class HoverflyVerificationError extends Error {
3+
public class HoverflyVerificationError extends AssertionError {
44

55
public HoverflyVerificationError(String message) {
66
super(message);

src/test/java/io/specto/hoverfly/ruletest/HoverflyRuleVerificationTest.java

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
package io.specto.hoverfly.ruletest;
22

3+
import static io.specto.hoverfly.junit.core.SimulationSource.dsl;
4+
import static io.specto.hoverfly.junit.dsl.HoverflyDsl.service;
5+
import static io.specto.hoverfly.junit.dsl.HttpBodyConverter.json;
6+
import static io.specto.hoverfly.junit.dsl.ResponseCreators.success;
7+
import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.any;
8+
import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.contains;
9+
import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.equalsToJson;
10+
import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.matches;
11+
import static io.specto.hoverfly.junit.verification.HoverflyVerifications.never;
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
14+
import static org.awaitility.Awaitility.await;
15+
import static org.springframework.http.MediaType.APPLICATION_JSON;
16+
317
import com.fasterxml.jackson.core.JsonProcessingException;
418
import io.specto.hoverfly.junit.dsl.HttpBodyConverter;
519
import io.specto.hoverfly.junit.rule.HoverflyRule;
620
import io.specto.hoverfly.junit.verification.HoverflyVerificationError;
721
import io.specto.hoverfly.models.SimpleBooking;
22+
import java.io.IOException;
23+
import java.net.URI;
24+
import java.net.URISyntaxException;
25+
import java.time.Duration;
26+
import java.time.LocalDate;
27+
import org.apache.http.HttpHost;
28+
import org.apache.http.client.methods.HttpPost;
29+
import org.apache.http.impl.client.HttpClientBuilder;
30+
import org.junit.Assert;
831
import org.junit.Before;
932
import org.junit.ClassRule;
1033
import org.junit.Test;
@@ -14,26 +37,12 @@
1437
import org.springframework.web.client.RestTemplate;
1538
import org.springframework.web.util.UriComponentsBuilder;
1639

17-
import java.net.URI;
18-
import java.net.URISyntaxException;
19-
import java.time.LocalDate;
20-
21-
import static io.specto.hoverfly.junit.core.SimulationSource.dsl;
22-
import static io.specto.hoverfly.junit.dsl.HoverflyDsl.service;
23-
import static io.specto.hoverfly.junit.dsl.HttpBodyConverter.json;
24-
import static io.specto.hoverfly.junit.dsl.ResponseCreators.success;
25-
import static io.specto.hoverfly.junit.dsl.matchers.HoverflyMatchers.*;
26-
import static io.specto.hoverfly.junit.verification.HoverflyVerifications.never;
27-
import static org.assertj.core.api.Assertions.assertThat;
28-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
29-
import static org.springframework.http.MediaType.APPLICATION_JSON;
30-
3140
public class HoverflyRuleVerificationTest {
3241

3342
private static final String NL = System.lineSeparator();
34-
private RestTemplate restTemplate = new RestTemplate();
43+
private static final SimpleBooking BOOKING = new SimpleBooking(1, "London", "Hong Kong", LocalDate.of(2017, 6, 29));
44+
private final RestTemplate restTemplate = new RestTemplate();
3545

36-
private static SimpleBooking booking = new SimpleBooking(1, "London", "Hong Kong", LocalDate.of(2017, 6, 29));
3746

3847
@ClassRule
3948
public static HoverflyRule hoverflyRule = HoverflyRule.inSimulationMode(dsl(
@@ -42,10 +51,10 @@ public class HoverflyRuleVerificationTest {
4251
.get("/api/bookings")
4352
.queryParam("airline", contains("Pacific"))
4453
.queryParam("page", any())
45-
.willReturn(success(json(booking)))
54+
.willReturn(success(json(BOOKING)))
4655

4756
.put("/api/bookings/1")
48-
.body(equalsToJson(json(booking)))
57+
.body(equalsToJson(json(BOOKING)))
4958
.willReturn(success())
5059

5160
)).printSimulationData();
@@ -84,7 +93,7 @@ public void shouldVerifyRequestWithAJsonBody() throws Exception {
8493

8594
assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
8695

87-
hoverflyRule.verify(service("http://api-sandbox.flight.com").put("/api/bookings/1").header("Content-Type", any()).body(json(booking)));
96+
hoverflyRule.verify(service("http://api-sandbox.flight.com").put("/api/bookings/1").header("Content-Type", any()).body(json(BOOKING)));
8897
}
8998

9099
@Test
@@ -144,6 +153,32 @@ public void shouldThrowExceptionIfVerifyAllFailed() {
144153
"But actual number of requests is 0.");
145154
}
146155

156+
@Test
157+
public void shouldBeCompatibleWithAwaitility() {
158+
delayedRequest();
159+
160+
await().pollInterval(Duration.ofMillis(100))
161+
.untilAsserted(() -> hoverflyRule.verify(service("service")
162+
.post("/path")
163+
.anyBody()));
164+
}
165+
166+
private void delayedRequest() {
167+
new Thread(() -> {
168+
try {
169+
Thread.sleep(500);
170+
String host = String.format("localhost:%s", hoverflyRule.getProxyPort());
171+
HttpHost proxy = HttpHost.create(host);
172+
HttpClientBuilder.create()
173+
.setProxy(proxy)
174+
.build()
175+
.execute(new HttpPost("http://service/path"));
176+
} catch (IOException | InterruptedException e) {
177+
Assert.fail("Fail to make a delayed request: " + e.getMessage());
178+
}
179+
}).start();
180+
}
181+
147182
private ResponseEntity<SimpleBooking> getBookings() {
148183
URI uri = UriComponentsBuilder.fromHttpUrl("http://api-sandbox.flight.com")
149184
.path("/api/bookings")
@@ -160,7 +195,7 @@ private ResponseEntity<SimpleBooking> getBookings() {
160195
private ResponseEntity<String> putBooking() throws URISyntaxException, JsonProcessingException {
161196
RequestEntity<String> bookFlightRequest = RequestEntity.put(new URI("http://api-sandbox.flight.com/api/bookings/1"))
162197
.contentType(APPLICATION_JSON)
163-
.body(HttpBodyConverter.OBJECT_MAPPER.writeValueAsString(booking));
198+
.body(HttpBodyConverter.OBJECT_MAPPER.writeValueAsString(BOOKING));
164199

165200
return restTemplate.exchange(bookFlightRequest, String.class);
166201
}

0 commit comments

Comments
 (0)