Added compatibility with WireMock helpers
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.springframework.cloud.contract.verifier.builder.handlebars
|
||||
|
||||
import com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.WireMockHelpers
|
||||
import wiremock.com.github.jknack.handlebars.Helper
|
||||
import wiremock.com.github.jknack.handlebars.Options
|
||||
import com.github.tomakehurst.wiremock.extension.responsetemplating.RequestTemplateModel
|
||||
@@ -22,14 +23,34 @@ class HandlebarsJsonPathHelper implements Helper<Map<String, Object>> {
|
||||
|
||||
@Override
|
||||
Object apply(Map<String, Object> context, Options options) throws IOException {
|
||||
String jsonPath = options.param(0)
|
||||
Object model = context.get(REQUEST_MODEL_NAME)
|
||||
if (model instanceof TestSideRequestTemplateModel) {
|
||||
return returnObjectForTest(model, jsonPath)
|
||||
} else if (model instanceof RequestTemplateModel) {
|
||||
return returnObjectForStub(model, jsonPath)
|
||||
if (context instanceof Map<String, Object>) {
|
||||
// legacy
|
||||
Map<String, Object> oldContext = (Map<String, Object>) context
|
||||
String jsonPath = options.param(0)
|
||||
Object model = oldContext.get(REQUEST_MODEL_NAME)
|
||||
if (model instanceof TestSideRequestTemplateModel) {
|
||||
return returnObjectForTest(model, jsonPath)
|
||||
} else if (model instanceof RequestTemplateModel) {
|
||||
return returnObjectForStub(model, jsonPath)
|
||||
}
|
||||
throw new IllegalArgumentException("Unsupported model")
|
||||
} else if (context instanceof String) {
|
||||
Object value = WireMockHelpers.jsonPath.apply(context, options)
|
||||
if (testSideModel(options)) {
|
||||
return processTestResponseValue(value)
|
||||
}
|
||||
return value
|
||||
}
|
||||
throw new IllegalArgumentException("Unsupported model")
|
||||
throw new IllegalArgumentException("Unsupported context")
|
||||
}
|
||||
|
||||
private boolean testSideModel(Options options) {
|
||||
Object model = options.context.model()
|
||||
if (!(model instanceof Map)) {
|
||||
return false
|
||||
}
|
||||
Map map = (Map) model
|
||||
return map.values().any { it instanceof TestSideRequestTemplateModel }
|
||||
}
|
||||
|
||||
private Object returnObjectForStub(Object model, String jsonPath) {
|
||||
@@ -37,10 +58,14 @@ class HandlebarsJsonPathHelper implements Helper<Map<String, Object>> {
|
||||
return documentContext.read(jsonPath)
|
||||
}
|
||||
|
||||
private Object returnObjectForTest(Object model, String jsonPath) {
|
||||
String body = removeSurroundingQuotes(((TestSideRequestTemplateModel) model).rawBody).replace('\\"', '"')
|
||||
private Object returnObjectForTest(TestSideRequestTemplateModel model, String jsonPath) {
|
||||
String body = removeSurroundingQuotes(model.rawBody).replace('\\"', '"')
|
||||
DocumentContext documentContext = JsonPath.parse(body)
|
||||
Object value = documentContext.read(jsonPath)
|
||||
return processTestResponseValue(value)
|
||||
}
|
||||
|
||||
private Object processTestResponseValue(Object value) {
|
||||
if (value instanceof Long) {
|
||||
return String.valueOf(value) + "L"
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.springframework.cloud.contract.verifier.template
|
||||
|
||||
import com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.WireMockHelpers
|
||||
import wiremock.com.github.jknack.handlebars.Handlebars
|
||||
import wiremock.com.github.jknack.handlebars.Template
|
||||
import groovy.transform.CompileStatic
|
||||
@@ -71,6 +72,12 @@ class HandlebarsTemplateProcessor implements TemplateProcessor, ContractTemplate
|
||||
try {
|
||||
Handlebars handlebars = new Handlebars()
|
||||
handlebars.registerHelper(HandlebarsJsonPathHelper.NAME, new HandlebarsJsonPathHelper())
|
||||
handlebars.registerHelper(WireMockHelpers.jsonPath.name(), new HandlebarsJsonPathHelper())
|
||||
WireMockHelpers.values()
|
||||
.findAll { it != WireMockHelpers.jsonPath}
|
||||
.each { WireMockHelpers helper ->
|
||||
handlebars.registerHelper(helper.name(), helper)
|
||||
}
|
||||
return handlebars.compileInline(content)
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e)
|
||||
|
||||
Reference in New Issue
Block a user