Skip to content

Commit

Permalink
Add labels to simulation pojo
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Jul 12, 2024
1 parent 5a474f8 commit 1395cb8
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,36 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_EMPTY)
public class RequestResponsePair {
private final Request request;
private final Response response;
private final List<String> labels;

@JsonCreator
public RequestResponsePair(@JsonProperty("request") Request request,
@JsonProperty("response") Response response) {
@JsonProperty("response") Response response,
@JsonProperty("labels") List<String> labels) {
this.request = request;
this.response = response;
this.labels = labels == null ? Collections.emptyList() : labels;
}

public RequestResponsePair(Request request,
Response response) {
this.request = request;
this.response = response;
this.labels = Collections.emptyList();
}

public Request getRequest() {
Expand All @@ -39,6 +54,10 @@ public Response getResponse() {
return response;
}

public List<String> getLabels() {
return labels;
}

@Override
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
Expand All @@ -53,4 +72,4 @@ public int hashCode() {
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class SimulationTest {
private final URL v5ResourceWithoutGlobalActions = Resources.getResource("simulations/v5-simulation-without-global-actions.json");
private final URL v5ResourceWithUnknownFields = Resources.getResource("simulations/v5-simulation-with-unknown-fields.json");
private final URL v5ResourceWithMixedCaseMatcherType = Resources.getResource("simulations/v5-simulation-with-mixed-case-matcher-type.json");
private final URL v5ResourceWithLabels = Resources.getResource("simulations/v5-simulation-with-labels.json");
private final URL latestResource = Resources.getResource("simulations/latest-simulation.json");


Expand Down Expand Up @@ -107,6 +108,28 @@ public void deserializingMatcherTypeShouldBeCaseInsensitive() throws Exception {
assertThat(actual).isEqualTo(expected);
}

@Test
public void deserializingSimulationWithLabels() throws Exception {
// Given
Request.Builder requestBuilder = getTestRequestBuilder()
.requiresState(ImmutableMap.of("requiresStateKey", "requiresStateValue"));
Response.Builder responseBuilder = getTestResponseBuilder()
.transitionsState(ImmutableMap.of("transitionsStateKey", "transitionsStateValue"))
.removesState(ImmutableList.of("removesStateKey"))
.fixedDelay(3000);
HoverflyData data = new HoverflyData(
Sets.newHashSet(new RequestResponsePair(requestBuilder.build(), responseBuilder.build(), ImmutableList.of("create", "bookings"))),
new GlobalActions(Collections.emptyList()));
HoverflyMetaData meta = new HoverflyMetaData();
Simulation expected = new Simulation(data, meta);

// When
Simulation actual = objectMapper.readValue(v5ResourceWithLabels, Simulation.class);

// Then
assertThat(actual).isEqualTo(expected);
}

@Test
public void shouldNotIncludeNullGlobalActionsFieldWhenSerialize() throws Exception{
String expected = Resources.toString(v5ResourceWithoutGlobalActions, StandardCharsets.UTF_8);
Expand Down
84 changes: 84 additions & 0 deletions src/test/resources/simulations/v5-simulation-with-labels.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"data": {
"pairs": [
{
"labels": ["create", "bookings"],
"request": {
"path": [
{
"matcher": "exact",
"value": "/api/bookings/1"
}
],
"method": [
{
"matcher": "exact",
"value": "GET"
}
],
"destination": [
{
"matcher": "exact",
"value": "www.my-test.com"
}
],
"scheme": [
{
"matcher": "exact",
"value": "http"
}
],
"body": [
{
"matcher": "exact",
"value": ""
}
],
"headers": {
"Content-Type": [
{
"matcher": "exact",
"value": "text/plain; charset=utf-8"
}
]
},
"query": {
"key": [
{
"matcher": "exact",
"value": "value"
}
]
},
"requiresState": {
"requiresStateKey": "requiresStateValue"
}
},
"response": {
"status": 200,
"body": "{\"bookingId\":\"1\"}",
"encodedBody": false,
"headers": {
"Content-Type": [
"application/json"
]
},
"templated": false,
"transitionsState": {
"transitionsStateKey": "transitionsStateValue"
},
"removesState": [
"removesStateKey"
],
"fixedDelay": 3000
}
}
],
"globalActions": {
"delays": []
}
},
"meta": {
"schemaVersion": "v5.3"
}
}

0 comments on commit 1395cb8

Please sign in to comment.