Skip to content

Commit 02b3fc0

Browse files
italomgItalo Garcia
authored andcommitted
Implementing POST request for appending simulation (#258)
Co-authored-by: Italo Garcia <[email protected]>
1 parent f3de9ae commit 02b3fc0

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

src/main/java/io/specto/hoverfly/junit/api/HoverflyClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,20 @@
1919
public interface HoverflyClient {
2020

2121

22+
/**
23+
* Set the given simulation data, overwriting any existing one.
24+
* @param simulation Hoverfly simulation data to set
25+
*/
2226
void setSimulation(Simulation simulation);
2327

2428
void setSimulation(String simulation);
2529

30+
/**
31+
* Append the given simulation to the existing one in Hoverfly, duplicated pair will not be added
32+
* @param simulation Hoverfly simulation data to append
33+
*/
34+
void addSimulation(Simulation simulation);
35+
2636
Simulation getSimulation();
2737

2838
/**

src/main/java/io/specto/hoverfly/junit/api/OkHttpHoverflyClient.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ class OkHttpHoverflyClient implements HoverflyClient {
4242
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
4343
private static final MediaType JSON = MediaType.parse("application/json");
4444

45-
private OkHttpClient client;
45+
private final OkHttpClient client;
4646

47-
private HttpUrl baseUrl;
47+
private final HttpUrl baseUrl;
4848

4949
OkHttpHoverflyClient(String scheme, String host, int port, String authToken) {
5050
OBJECT_MAPPER.registerModule(new JavaTimeModule());
5151
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
52-
if (authToken != null ) {
52+
if (authToken != null) {
5353
clientBuilder.addInterceptor(new AuthHeaderInterceptor(authToken));
5454
}
5555
this.client = clientBuilder.build();
@@ -86,6 +86,19 @@ public void setSimulation(String simulation) {
8686
}
8787
}
8888

89+
@Override
90+
public void addSimulation(Simulation simulation) {
91+
try {
92+
final Request.Builder builder = createRequestBuilderWithUrl(SIMULATION_PATH);
93+
final RequestBody body = createRequestBody(simulation);
94+
final Request request = builder.post(body).build();
95+
exchange(request);
96+
} catch (Exception e) {
97+
LOGGER.warn("Failed to add simulation: {}", e.getMessage());
98+
throw new HoverflyClientException("Failed to add simulation: " + e.getMessage());
99+
}
100+
}
101+
89102
@Override
90103
public Simulation getSimulation() {
91104
try {
@@ -184,7 +197,7 @@ public StateView getState() {
184197
throw new HoverflyClientException("Failed to get state: " + e.getMessage());
185198
}
186199
}
187-
200+
188201
@Override
189202
public DiffView getDiffs() {
190203
try {

src/main/java/io/specto/hoverfly/junit/core/model/Response.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.apache.commons.lang3.builder.EqualsBuilder;
2020
import org.apache.commons.lang3.builder.HashCodeBuilder;
2121
import org.apache.commons.lang3.builder.ToStringBuilder;
22-
import sun.rmi.runtime.Log;
2322

2423
import java.util.List;
2524
import java.util.Map;

src/test/java/io/specto/hoverfly/junit/api/OkHttpHoverflyClientTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ public void shouldBeAbleToDeleteAllSimulation() throws Exception {
179179
assertThat(result.getHoverflyData().getPairs()).isEmpty();
180180
}
181181

182+
@Test
183+
public void shouldBeAbleToAppendSimulation() throws Exception {
184+
URL resource = Resources.getResource("simulations/v5-simulation.json");
185+
Simulation simulation = objectMapper.readValue(resource, Simulation.class);
186+
client.setSimulation(simulation);
187+
188+
resource = Resources.getResource("simulations/v5-simulation-without-response-body.json");
189+
simulation = objectMapper.readValue(resource, Simulation.class);
190+
client.addSimulation(simulation);
191+
192+
Simulation result = client.getSimulation();
193+
assertThat(result.getHoverflyData().getPairs()).hasSize(2);
194+
}
195+
182196
@Test
183197
public void shouldBeAbleToDeleteJournal() {
184198

0 commit comments

Comments
 (0)