-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathHoverflyDslTest.java
129 lines (101 loc) · 5.57 KB
/
HoverflyDslTest.java
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
package io.specto.hoverfly.ruletest;
import static io.specto.hoverfly.junit.core.SimulationSource.dsl;
import static io.specto.hoverfly.junit.dsl.HoverflyDsl.service;
import static io.specto.hoverfly.junit.dsl.HttpBodyConverter.jsonWithSingleQuotes;
import static io.specto.hoverfly.junit.dsl.ResponseCreators.created;
import static io.specto.hoverfly.junit.dsl.ResponseCreators.noContent;
import static io.specto.hoverfly.junit.dsl.ResponseCreators.success;
import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import io.specto.hoverfly.junit.rule.HoverflyRule;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.ClassRule;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
public class HoverflyDslTest {
@ClassRule
public static HoverflyRule hoverflyRule = HoverflyRule.inSimulationMode(dsl(
service("www.my-test.com")
.post("/api/bookings").body("{\"flightId\": \"1\"}")
.willReturn(created("http://localhost/api/bookings/1"))
.get("/api/bookings/1")
.willReturn(success().body(jsonWithSingleQuotes(
"{'bookingId':'1','origin':'London','destination':'Singapore','time':'2011-09-01T12:30','_links':{'self':{'href':'http://localhost/api/bookings/1'}}}"
))),
service("www.other-anotherservice.com")
.put("/api/bookings/1").body("{\"flightId\": \"1\", \"class\": \"PREMIUM\"}")
.willReturn(success())
.delete("/api/bookings/1")
.willReturn(noContent())
.get("/api/bookings")
.queryParam("class", "business", "premium")
.queryParam("destination", "new york")
.willReturn(success("{\"bookingId\":\"2\",\"origin\":\"London\",\"destination\":\"New York\",\"class\":\"BUSINESS\",\"time\":\"2011-09-01T12:30\",\"_links\":{\"self\":{\"href\":\"http://localhost/api/bookings/2\"}}}", "application/json"))
.patch("/api/bookings/1").body("{\"class\": \"BUSINESS\"}")
.willReturn(noContent())
)).printSimulationData();
private final RestTemplate restTemplate = new RestTemplate();
@Test
public void shouldBeAbleToAmendABookingUsingHoverfly() throws URISyntaxException {
// Given
final RequestEntity<String> bookFlightRequest = RequestEntity.put(new URI("http://www.other-anotherservice.com/api/bookings/1"))
.contentType(APPLICATION_JSON)
.body("{\"flightId\": \"1\", \"class\": \"PREMIUM\"}");
// When
final ResponseEntity<String> bookFlightResponse = restTemplate.exchange(bookFlightRequest, String.class);
// Then
assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
}
@Test
public void shouldBeAbleToDeleteBookingUsingHoverfly() throws Exception {
// Given
final RequestEntity<Void> bookFlightRequest = RequestEntity.delete(new URI("http://www.other-anotherservice.com/api/bookings/1")).build();
// When
final ResponseEntity<Void> bookFlightResponse = restTemplate.exchange(bookFlightRequest, Void.class);
// Then
assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
}
@Test
public void shouldBeAbleToQueryBookingsUsingHoverfly() {
// When
URI uri = UriComponentsBuilder.fromHttpUrl("http://www.other-anotherservice.com")
.path("/api/bookings")
.queryParam("class", "business", "premium")
.queryParam("destination", "new york")
.build()
.toUri();
final ResponseEntity<String> getBookingResponse = restTemplate.getForEntity(uri, String.class);
// Then
assertThat(getBookingResponse.getStatusCode()).isEqualTo(OK);
assertThatJson(getBookingResponse.getBody()).isEqualTo("{" +
"\"bookingId\":\"2\"," +
"\"origin\":\"London\"," +
"\"destination\":\"New York\"," +
"\"class\":\"BUSINESS\"," +
"\"time\":\"2011-09-01T12:30\"," +
"\"_links\":{\"self\":{\"href\":\"http://localhost/api/bookings/2\"}}" +
"}");
}
@Test
public void shouldBeAbleToPatchBookingsUsingHoverfly() throws Exception {
// Given
final RequestEntity<String> bookFlightRequest = RequestEntity.patch(new URI("http://www.other-anotherservice.com/api/bookings/1"))
.contentType(APPLICATION_JSON)
.body("{\"class\": \"BUSINESS\"}");
// When
// Apache HttpClient is required for making PATCH request using RestTemplate
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
restTemplate.setRequestFactory(requestFactory);
final ResponseEntity<Void> bookFlightResponse = restTemplate.exchange(bookFlightRequest, Void.class);
// Then
assertThat(bookFlightResponse.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
}
}