11package 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+
317import com .fasterxml .jackson .core .JsonProcessingException ;
418import io .specto .hoverfly .junit .dsl .HttpBodyConverter ;
519import io .specto .hoverfly .junit .rule .HoverflyRule ;
620import io .specto .hoverfly .junit .verification .HoverflyVerificationError ;
721import 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 ;
831import org .junit .Before ;
932import org .junit .ClassRule ;
1033import org .junit .Test ;
1437import org .springframework .web .client .RestTemplate ;
1538import 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-
3140public 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