@@ -72,11 +72,11 @@ public class WireMockToDslConverter {
7272 private static final JsonPointer RESPONSE_HEADERS_POINTER = JsonPointer .compile ("/response/headers" );
7373
7474 private static final JsonMapper OBJECT_MAPPER = JsonMapper .builder ()
75- .configure (ALLOW_JAVA_COMMENTS , true )
76- .configure (ALLOW_YAML_COMMENTS , true )
77- .configure (ALLOW_UNQUOTED_PROPERTY_NAMES , true )
78- .configure (ALLOW_SINGLE_QUOTES , true )
79- .build ();
75+ .configure (ALLOW_JAVA_COMMENTS , true )
76+ .configure (ALLOW_YAML_COMMENTS , true )
77+ .configure (ALLOW_UNQUOTED_PROPERTY_NAMES , true )
78+ .configure (ALLOW_SINGLE_QUOTES , true )
79+ .build ();
8080
8181 /**
8282 * Returns the string content of the contract.
@@ -108,7 +108,7 @@ private JsonNode parseStubDefinition(String wireMockStringStub) {
108108 private String buildPriority (JsonNode wireMockStub ) {
109109 String priority = "" ;
110110 JsonNode priorityNode = wireMockStub .at (PRIORITY_POINTER );
111- if (priorityNode != null && priorityNode .asInt () > 0 ) {
111+ if (! priorityNode . isMissingNode () && priorityNode .asInt () > 0 ) {
112112 priority = "priority " + priorityNode .asInt () + "\n " ;
113113 }
114114 return priority ;
@@ -174,10 +174,12 @@ private String buildRequestHeaders(JsonNode wireMockStub) {
174174 fields .forEach (c -> {
175175 requestHeadersBuilder .append ("header('" ).append (c .getKey ()).append ("'," );
176176 ObjectNode headersNode = (ObjectNode ) c .getValue ().deepCopy ();
177- Iterator <Map .Entry <String , JsonNode >> headersNodeIterator = headersNode .properties ().iterator ();
177+ Iterator <Map .Entry <String , JsonNode >> headersNodeIterator = headersNode .properties ()
178+ .iterator ();
178179 if (headersNodeIterator .hasNext ()) {
179180 Map .Entry <String , JsonNode > headerValue = headersNodeIterator .next ();
180- String header = buildHeader (headerValue .getKey (), headerValue .getValue ().asString ());
181+ String header = buildHeader (headerValue .getKey (), headerValue .getValue ()
182+ .asString ());
181183 requestHeadersBuilder .append (header ).append (")" ).append ("\n " );
182184 }
183185 });
@@ -188,13 +190,13 @@ private String buildRequestHeaders(JsonNode wireMockStub) {
188190
189191 private String buildHeader (String method , String value ) {
190192 switch (method ) {
191- case "equalTo" :
192- return "'" + value + "'" ;
193- case "contains" :
194- String regex = "^.*" + value + ".*$" ;
195- return "c(regex('" + escapeJava (regex ) + "'))" ;
196- default :
197- return "c(regex('" + escapeJava (value ) + "'))" ;
193+ case "equalTo" :
194+ return "'" + value + "'" ;
195+ case "contains" :
196+ String regex = "^.*" + value + ".*$" ;
197+ return "c(regex('" + escapeJava (regex ) + "'))" ;
198+ default :
199+ return "c(regex('" + escapeJava (value ) + "'))" ;
198200 }
199201 }
200202
@@ -207,26 +209,29 @@ private String buildRequestBody(JsonNode wireMockStub) {
207209 List <Map .Entry <String , JsonNode >> requestBodyObjectNodes = new ArrayList <>();
208210
209211 elements .stream ()
210- .filter (f -> f instanceof ObjectNode )
211- .map (f -> (ObjectNode ) f )
212- .map (ObjectNode ::properties )
213- .forEachOrdered (requestBodyObjectNodes ::addAll );
212+ .filter (f -> f instanceof ObjectNode )
213+ .map (f -> (ObjectNode ) f )
214+ .map (ObjectNode ::properties )
215+ .forEachOrdered (requestBodyObjectNodes ::addAll );
214216 requestBodyObjectNodes .stream ()
215- .filter (b -> b .getKey ().equals ("equalTo" ))
216- .findFirst ()
217- .ifPresent (b -> requestBody .append ("body ('" ).append (b .getValue ().asString ()).append ("')" ));
217+ .filter (b -> b .getKey ().equals ("equalTo" ))
218+ .findFirst ()
219+ .ifPresent (b -> requestBody .append ("body ('" )
220+ .append (b .getValue ().asString ()).append ("')" ));
218221 requestBodyObjectNodes .stream ()
219- .filter (b -> b .getKey ().equals ("equalToJson" ))
220- .findFirst ()
221- .ifPresent (b -> requestBody .append ("body ('" ).append (b .getValue ().asString ()).append ("')" ));
222+ .filter (b -> b .getKey ().equals ("equalToJson" ))
223+ .findFirst ()
224+ .ifPresent (b -> requestBody .append ("body ('" )
225+ .append (b .getValue ().asString ()).append ("')" ));
222226 requestBodyObjectNodes .stream ()
223- .filter (b -> b .getKey ().equals ("matches" ))
224- .findFirst ()
225- .ifPresent (b -> requestBody .append ("body $(consumer(regex('" )
226- .append (escapeJava (b .getValue ().asString ()))
227- .append ("')), producer('" )
228- .append (new Xeger (escapeJava (b .getValue ().asString ())).generate ())
229- .append ("'))" ));
227+ .filter (b -> b .getKey ().equals ("matches" ))
228+ .findFirst ()
229+ .ifPresent (b -> requestBody .append ("body $(consumer(regex('" )
230+ .append (escapeJava (b .getValue ().asString ()))
231+ .append ("')), producer('" )
232+ .append (new Xeger (escapeJava (b .getValue ()
233+ .asString ())).generate ())
234+ .append ("'))" ));
230235 }
231236 return requestBody .toString ();
232237 }
@@ -261,12 +266,14 @@ private String buildPrettyPrintResponseBody(IntNode node) {
261266 private String buildPrettyPrintResponseBody (StringNode node ) {
262267 try {
263268 String stringNode = node .asString ();
264- Object intermediateObjectForPrettyPrinting = OBJECT_MAPPER .readerFor (Object .class ).readValue (stringNode );
269+ Object intermediateObjectForPrettyPrinting = OBJECT_MAPPER .readerFor (Object .class )
270+ .readValue (stringNode );
265271
266272 DefaultIndenter customIndenter = new DefaultIndenter (" " , "\n " );
267273 return OBJECT_MAPPER .writer ()
268- .with (new DefaultPrettyPrinter ().withArrayIndenter (customIndenter ).withObjectIndenter (customIndenter ))
269- .writeValueAsString (intermediateObjectForPrettyPrinting );
274+ .with (new DefaultPrettyPrinter ().withArrayIndenter (customIndenter )
275+ .withObjectIndenter (customIndenter ))
276+ .writeValueAsString (intermediateObjectForPrettyPrinting );
270277 }
271278 catch (Exception e ) {
272279 throw new RuntimeException ("WireMock response body could not be pretty printed" , e );
@@ -282,11 +289,11 @@ private String buildResponseHeaders(JsonNode wireMockStub) {
282289 JsonNode responseHeadersObjectNode = requestHeadersNode .deepCopy ();
283290 Set <Map .Entry <String , JsonNode >> fields = responseHeadersObjectNode .properties ();
284291 fields .forEach (c -> responseHeadersBuilder .append ("header('" )
285- .append (c .getKey ())
286- .append ("'," )
287- .append ("'" )
288- .append (c .getValue ().asString ())
289- .append ("')\n " ));
292+ .append (c .getKey ())
293+ .append ("'," )
294+ .append ("'" )
295+ .append (c .getValue ().asString ())
296+ .append ("')\n " ));
290297 responseHeadersBuilder .append ("}" );
291298 }
292299 return responseHeadersBuilder .toString ();
0 commit comments