|
|
|
|
@@ -49,25 +49,35 @@ public class WireMockToDslConverter {
|
|
|
|
|
|
|
|
|
|
private static final JsonPointer PRIORITY_POINTER = JsonPointer.compile("/priority");
|
|
|
|
|
|
|
|
|
|
private static final JsonPointer REQUEST_METHOD_POINTER = JsonPointer.compile("/request/method");
|
|
|
|
|
private static final JsonPointer REQUEST_METHOD_POINTER = JsonPointer
|
|
|
|
|
.compile("/request/method");
|
|
|
|
|
|
|
|
|
|
private static final JsonPointer REQUEST_URL_POINTER = JsonPointer.compile("/request/url");
|
|
|
|
|
private static final JsonPointer REQUEST_URL_POINTER = JsonPointer
|
|
|
|
|
.compile("/request/url");
|
|
|
|
|
|
|
|
|
|
private static final JsonPointer REQUEST_URL_PATH_POINTER = JsonPointer.compile("/request/urlPath");
|
|
|
|
|
private static final JsonPointer REQUEST_URL_PATH_POINTER = JsonPointer
|
|
|
|
|
.compile("/request/urlPath");
|
|
|
|
|
|
|
|
|
|
private static final JsonPointer REQUEST_URL_PATTERN_POINTER = JsonPointer.compile("/request/urlPattern");
|
|
|
|
|
private static final JsonPointer REQUEST_URL_PATTERN_POINTER = JsonPointer
|
|
|
|
|
.compile("/request/urlPattern");
|
|
|
|
|
|
|
|
|
|
private static final JsonPointer REQUEST_URL_PATH_PATTERN_POINTER = JsonPointer.compile("/request/urlPathPattern");
|
|
|
|
|
private static final JsonPointer REQUEST_URL_PATH_PATTERN_POINTER = JsonPointer
|
|
|
|
|
.compile("/request/urlPathPattern");
|
|
|
|
|
|
|
|
|
|
private static final JsonPointer REQUEST_HEADERS_POINTER = JsonPointer.compile("/request/headers");
|
|
|
|
|
private static final JsonPointer REQUEST_HEADERS_POINTER = JsonPointer
|
|
|
|
|
.compile("/request/headers");
|
|
|
|
|
|
|
|
|
|
private static final JsonPointer REQUEST_BODY_POINTER = JsonPointer.compile("/request/bodyPatterns");
|
|
|
|
|
private static final JsonPointer REQUEST_BODY_POINTER = JsonPointer
|
|
|
|
|
.compile("/request/bodyPatterns");
|
|
|
|
|
|
|
|
|
|
private static final JsonPointer RESPONSE_STATUS_POINTER = JsonPointer.compile("/response/status");
|
|
|
|
|
private static final JsonPointer RESPONSE_STATUS_POINTER = JsonPointer
|
|
|
|
|
.compile("/response/status");
|
|
|
|
|
|
|
|
|
|
private static final JsonPointer RESPONSE_BODY_POINTER = JsonPointer.compile("/response/body");
|
|
|
|
|
private static final JsonPointer RESPONSE_BODY_POINTER = JsonPointer
|
|
|
|
|
.compile("/response/body");
|
|
|
|
|
|
|
|
|
|
private static final JsonPointer RESPONSE_HEADERS_POINTER = JsonPointer.compile("/response/headers");
|
|
|
|
|
private static final JsonPointer RESPONSE_HEADERS_POINTER = JsonPointer
|
|
|
|
|
.compile("/response/headers");
|
|
|
|
|
|
|
|
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
|
|
|
|
|
|
|
|
|
@@ -80,7 +90,6 @@ public class WireMockToDslConverter {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the string content of the contract.
|
|
|
|
|
*
|
|
|
|
|
* @param wireMockStringStub - string content of the WireMock JSON stub
|
|
|
|
|
*/
|
|
|
|
|
public static String fromWireMockStub(String wireMockStringStub) {
|
|
|
|
|
@@ -89,22 +98,14 @@ public class WireMockToDslConverter {
|
|
|
|
|
|
|
|
|
|
private String convertFromWireMockStub(String wireMockStringStub) {
|
|
|
|
|
JsonNode wireMockStub = parseStubDefinition(wireMockStringStub);
|
|
|
|
|
return buildPriority(wireMockStub)
|
|
|
|
|
+ "request {\n"
|
|
|
|
|
+ buildRequestMethod(wireMockStub)
|
|
|
|
|
+ buildRequestUrl(wireMockStub)
|
|
|
|
|
return buildPriority(wireMockStub) + "request {\n"
|
|
|
|
|
+ buildRequestMethod(wireMockStub) + buildRequestUrl(wireMockStub)
|
|
|
|
|
+ buildRequestUrlPattern(wireMockStub)
|
|
|
|
|
+ buildRequestUrlPathPattern(wireMockStub)
|
|
|
|
|
+ buildRequestUrlPath(wireMockStub)
|
|
|
|
|
+ buildRequestHeaders(wireMockStub)
|
|
|
|
|
+ buildRequestBody(wireMockStub)
|
|
|
|
|
+ "}\n"
|
|
|
|
|
+ "response {\n"
|
|
|
|
|
+ buildResponseStatus(wireMockStub)
|
|
|
|
|
+ buildResponseBody(wireMockStub)
|
|
|
|
|
+ buildResponseHeaders(wireMockStub)
|
|
|
|
|
+ "}"
|
|
|
|
|
+ "\n";
|
|
|
|
|
+ buildRequestUrlPath(wireMockStub) + buildRequestHeaders(wireMockStub)
|
|
|
|
|
+ buildRequestBody(wireMockStub) + "}\n" + "response {\n"
|
|
|
|
|
+ buildResponseStatus(wireMockStub) + buildResponseBody(wireMockStub)
|
|
|
|
|
+ buildResponseHeaders(wireMockStub) + "}" + "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private JsonNode parseStubDefinition(String wireMockStringStub) {
|
|
|
|
|
@@ -156,7 +157,8 @@ public class WireMockToDslConverter {
|
|
|
|
|
String requestUrlPattern = "";
|
|
|
|
|
JsonNode requestUrlPatternNode = wireMockStub.at(REQUEST_URL_PATTERN_POINTER);
|
|
|
|
|
if (!requestUrlPatternNode.isMissingNode()) {
|
|
|
|
|
String escapedRequestUrlPatternValue = escapeJava(requestUrlPatternNode.asText());
|
|
|
|
|
String escapedRequestUrlPatternValue = escapeJava(
|
|
|
|
|
requestUrlPatternNode.asText());
|
|
|
|
|
requestUrlPattern = "url $(consumer(regex('" + escapedRequestUrlPatternValue
|
|
|
|
|
+ "')), producer('"
|
|
|
|
|
+ new Xeger(escapedRequestUrlPatternValue).generate() + "'))\n";
|
|
|
|
|
@@ -166,9 +168,11 @@ public class WireMockToDslConverter {
|
|
|
|
|
|
|
|
|
|
private String buildRequestUrlPathPattern(JsonNode wireMockStub) {
|
|
|
|
|
String requestUrlPathPattern = "";
|
|
|
|
|
JsonNode requestUrlPathPatternNode = wireMockStub.at(REQUEST_URL_PATH_PATTERN_POINTER);
|
|
|
|
|
JsonNode requestUrlPathPatternNode = wireMockStub
|
|
|
|
|
.at(REQUEST_URL_PATH_PATTERN_POINTER);
|
|
|
|
|
if (!requestUrlPathPatternNode.isMissingNode()) {
|
|
|
|
|
String escapedRequestUrlPathPatternValue = escapeJava(requestUrlPathPatternNode.asText());
|
|
|
|
|
String escapedRequestUrlPathPatternValue = escapeJava(
|
|
|
|
|
requestUrlPathPatternNode.asText());
|
|
|
|
|
requestUrlPathPattern = "urlPath $(consumer(regex('"
|
|
|
|
|
+ escapedRequestUrlPathPatternValue + "')), producer('"
|
|
|
|
|
+ new Xeger(escapedRequestUrlPathPatternValue).generate() + "'))'\n";
|
|
|
|
|
@@ -183,11 +187,14 @@ public class WireMockToDslConverter {
|
|
|
|
|
if (requestHeadersNode.isObject()) {
|
|
|
|
|
requestHeadersBuilder.append("headers {\n");
|
|
|
|
|
ObjectNode requestHeadersObjectNode = requestHeadersNode.deepCopy();
|
|
|
|
|
Iterator<Map.Entry<String, JsonNode>> fields = requestHeadersObjectNode.fields();
|
|
|
|
|
Iterator<Map.Entry<String, JsonNode>> fields = requestHeadersObjectNode
|
|
|
|
|
.fields();
|
|
|
|
|
fields.forEachRemaining(c -> {
|
|
|
|
|
requestHeadersBuilder.append("header('").append(c.getKey()).append("',");
|
|
|
|
|
Map.Entry<String, JsonNode> headerValue = c.getValue().deepCopy().fields().next();
|
|
|
|
|
String header = buildHeader(headerValue.getKey(), headerValue.getValue().asText());
|
|
|
|
|
Map.Entry<String, JsonNode> headerValue = c.getValue().deepCopy().fields()
|
|
|
|
|
.next();
|
|
|
|
|
String header = buildHeader(headerValue.getKey(),
|
|
|
|
|
headerValue.getValue().asText());
|
|
|
|
|
requestHeadersBuilder.append(header).append(")").append("\n");
|
|
|
|
|
});
|
|
|
|
|
requestHeadersBuilder.append("}");
|
|
|
|
|
@@ -216,24 +223,22 @@ public class WireMockToDslConverter {
|
|
|
|
|
Iterable<JsonNode> iterableFields = () -> elements;
|
|
|
|
|
List<Map.Entry<String, JsonNode>> requestBodyObjectNodes = new ArrayList<>();
|
|
|
|
|
StreamSupport.stream(iterableFields.spliterator(), false)
|
|
|
|
|
.filter(f -> f instanceof ObjectNode)
|
|
|
|
|
.map(f -> (ObjectNode) f)
|
|
|
|
|
.filter(f -> f instanceof ObjectNode).map(f -> (ObjectNode) f)
|
|
|
|
|
.map(ObjectNode::fields)
|
|
|
|
|
.forEachOrdered(i -> i.forEachRemaining(requestBodyObjectNodes::add));
|
|
|
|
|
requestBodyObjectNodes.stream()
|
|
|
|
|
.filter(b -> b.getKey().equals("equalTo"))
|
|
|
|
|
.findFirst()
|
|
|
|
|
.ifPresent(b -> requestBody.append("body ('").append(b.getValue().asText()).append("')"));
|
|
|
|
|
requestBodyObjectNodes.stream()
|
|
|
|
|
.filter(b -> b.getKey().equals("equalToJson"))
|
|
|
|
|
.findFirst()
|
|
|
|
|
.ifPresent(b -> requestBody.append("body ('").append(b.getValue().asText()).append("')"));
|
|
|
|
|
requestBodyObjectNodes.stream().filter(b -> b.getKey().equals("equalTo"))
|
|
|
|
|
.findFirst().ifPresent(b -> requestBody.append("body ('")
|
|
|
|
|
.append(b.getValue().asText()).append("')"));
|
|
|
|
|
requestBodyObjectNodes.stream().filter(b -> b.getKey().equals("equalToJson"))
|
|
|
|
|
.findFirst().ifPresent(b -> requestBody.append("body ('")
|
|
|
|
|
.append(b.getValue().asText()).append("')"));
|
|
|
|
|
requestBodyObjectNodes.stream().filter(b -> b.getKey().equals("matches"))
|
|
|
|
|
.findFirst()
|
|
|
|
|
.ifPresent(b -> requestBody.append("body $(consumer(regex('")
|
|
|
|
|
.append(escapeJava(b.getValue().asText()))
|
|
|
|
|
.append("')), producer('")
|
|
|
|
|
.append(new Xeger(escapeJava(b.getValue().asText())).generate())
|
|
|
|
|
.append(new Xeger(escapeJava(b.getValue().asText()))
|
|
|
|
|
.generate())
|
|
|
|
|
.append("'))"));
|
|
|
|
|
}
|
|
|
|
|
return requestBody.toString();
|
|
|
|
|
@@ -253,10 +258,15 @@ public class WireMockToDslConverter {
|
|
|
|
|
String responseBody = "";
|
|
|
|
|
JsonNode responseBodyNode = wireMockStub.at(RESPONSE_BODY_POINTER);
|
|
|
|
|
if (responseBodyNode.isInt()) {
|
|
|
|
|
responseBody += "body( " + escapeJava(buildPrettyPrintResponseBody((IntNode) responseBodyNode)) + ")\n";
|
|
|
|
|
responseBody += "body( "
|
|
|
|
|
+ escapeJava(buildPrettyPrintResponseBody((IntNode) responseBodyNode))
|
|
|
|
|
+ ")\n";
|
|
|
|
|
}
|
|
|
|
|
if (responseBodyNode.isTextual()) {
|
|
|
|
|
responseBody += "body( \"" + escapeJava(buildPrettyPrintResponseBody((TextNode) responseBodyNode)) + "\")\n";
|
|
|
|
|
responseBody += "body( \""
|
|
|
|
|
+ escapeJava(
|
|
|
|
|
buildPrettyPrintResponseBody((TextNode) responseBodyNode))
|
|
|
|
|
+ "\")\n";
|
|
|
|
|
}
|
|
|
|
|
return responseBody;
|
|
|
|
|
}
|
|
|
|
|
@@ -272,13 +282,13 @@ public class WireMockToDslConverter {
|
|
|
|
|
.readValue(textNode, Object.class);
|
|
|
|
|
DefaultIndenter customIndenter = new DefaultIndenter(" ", "\n");
|
|
|
|
|
return OBJECT_MAPPER
|
|
|
|
|
.writer(new PrivatePrettyPrinter()
|
|
|
|
|
.withArrayIndenter(customIndenter)
|
|
|
|
|
.writer(new PrivatePrettyPrinter().withArrayIndenter(customIndenter)
|
|
|
|
|
.withObjectIndenter(customIndenter))
|
|
|
|
|
.writeValueAsString(intermediateObjectForPrettyPrinting);
|
|
|
|
|
}
|
|
|
|
|
catch (IOException e) {
|
|
|
|
|
throw new RuntimeException("WireMock response body could not be pretty printed");
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
"WireMock response body could not be pretty printed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -289,14 +299,11 @@ public class WireMockToDslConverter {
|
|
|
|
|
if (requestHeadersNode.isObject()) {
|
|
|
|
|
responseHeadersBuilder.append("headers {\n");
|
|
|
|
|
ObjectNode responseHeadersObjectNode = requestHeadersNode.deepCopy();
|
|
|
|
|
Iterator<Map.Entry<String, JsonNode>> fields = responseHeadersObjectNode.fields();
|
|
|
|
|
fields.forEachRemaining(c -> responseHeadersBuilder
|
|
|
|
|
.append("header('")
|
|
|
|
|
.append(c.getKey())
|
|
|
|
|
.append("',")
|
|
|
|
|
.append("'")
|
|
|
|
|
.append(c.getValue().asText())
|
|
|
|
|
.append("')\n"));
|
|
|
|
|
Iterator<Map.Entry<String, JsonNode>> fields = responseHeadersObjectNode
|
|
|
|
|
.fields();
|
|
|
|
|
fields.forEachRemaining(c -> responseHeadersBuilder.append("header('")
|
|
|
|
|
.append(c.getKey()).append("',").append("'")
|
|
|
|
|
.append(c.getValue().asText()).append("')\n"));
|
|
|
|
|
responseHeadersBuilder.append("}");
|
|
|
|
|
}
|
|
|
|
|
return responseHeadersBuilder.toString();
|
|
|
|
|
@@ -312,7 +319,8 @@ public class WireMockToDslConverter {
|
|
|
|
|
@Override
|
|
|
|
|
public DefaultPrettyPrinter withSeparators(Separators separators) {
|
|
|
|
|
_separators = separators;
|
|
|
|
|
_objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSeparator() + " ";
|
|
|
|
|
_objectFieldValueSeparatorWithSpaces = separators
|
|
|
|
|
.getObjectFieldValueSeparator() + " ";
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|