Support for stub priority
This commit is contained in:
committed by
Mariusz Smykuła
parent
9639e7395a
commit
9ccc2a2b8a
@@ -17,11 +17,13 @@ class WireMockToDslConverter {
|
||||
|
||||
private String convertFromWireMockStub(String wireMockStringStub) {
|
||||
Object wireMockStub = parseStubDefinition(wireMockStringStub)
|
||||
Integer priority = wireMockStub.priority
|
||||
def request = wireMockStub.request
|
||||
def response = wireMockStub.response
|
||||
def bodyPatterns = request.bodyPatterns
|
||||
String urlPattern = request.urlPattern
|
||||
return """\
|
||||
${priority ? "priority ${priority}" : ''}
|
||||
request {
|
||||
${request.method ? "method \"\"\"$request.method\"\"\"" : ""}
|
||||
${request.url ? "url \"\"\"$request.url\"\"\"" : ""}
|
||||
|
||||
@@ -498,6 +498,44 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
evaluatedGroovyDsl == expectedGroovyDsl
|
||||
}
|
||||
|
||||
def 'should convert WireMock stub with priorities'() {
|
||||
given:
|
||||
String wireMockStub = '''\
|
||||
{
|
||||
"priority" : 2,
|
||||
"request" : {
|
||||
"url" : "/test",
|
||||
"method" : "POST"
|
||||
},
|
||||
"response" : {
|
||||
"status" : 200
|
||||
}
|
||||
}
|
||||
'''
|
||||
and:
|
||||
stubMappingIsValidWireMockStub(wireMockStub)
|
||||
and:
|
||||
GroovyDsl expectedGroovyDsl = GroovyDsl.make {
|
||||
priority 2
|
||||
request {
|
||||
method 'POST'
|
||||
url '/test'
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
when:
|
||||
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
|
||||
then:
|
||||
GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""")
|
||||
and:
|
||||
evaluatedGroovyDsl == expectedGroovyDsl
|
||||
}
|
||||
|
||||
void stubMappingIsValidWireMockStub(String mappingDefinition) {
|
||||
StubMapping.buildFrom(mappingDefinition)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.codearte.accurest.dsl.internal.Response
|
||||
@ToString(includeFields = true, includePackage = false, includeNames = true)
|
||||
class GroovyDsl {
|
||||
|
||||
Integer priority
|
||||
Request request
|
||||
Response response
|
||||
|
||||
@@ -21,6 +22,10 @@ class GroovyDsl {
|
||||
return dsl
|
||||
}
|
||||
|
||||
void priority(int priority) {
|
||||
this.priority = new Integer(priority)
|
||||
}
|
||||
|
||||
void request(@DelegatesTo(Request) Closure closure) {
|
||||
this.request = new Request()
|
||||
closure.delegate = request
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.codearte.accurest.dsl
|
||||
|
||||
import groovy.json.JsonOutput
|
||||
import groovy.transform.CompileDynamic
|
||||
import groovy.transform.CompileStatic
|
||||
|
||||
@CompileStatic
|
||||
@@ -8,14 +9,21 @@ class WireMockStubStrategy {
|
||||
|
||||
private final WireMockRequestStubStrategy wireMockRequestStubStrategy
|
||||
private final WireMockResponseStubStrategy wireMockResponseStubStrategy
|
||||
private final Integer priority
|
||||
|
||||
WireMockStubStrategy(GroovyDsl groovyDsl) {
|
||||
this.wireMockRequestStubStrategy = new WireMockRequestStubStrategy(groovyDsl)
|
||||
this.wireMockResponseStubStrategy = new WireMockResponseStubStrategy(groovyDsl)
|
||||
this.priority = groovyDsl.priority
|
||||
}
|
||||
|
||||
@CompileDynamic
|
||||
String toWireMockClientStub() {
|
||||
return JsonOutput.prettyPrint(JsonOutput.toJson([request : wireMockRequestStubStrategy.buildClientRequestContent(),
|
||||
response: wireMockResponseStubStrategy.buildClientResponseContent()]))
|
||||
def wiremockStubDefinition = [request : wireMockRequestStubStrategy.buildClientRequestContent(),
|
||||
response: wireMockResponseStubStrategy.buildClientResponseContent()]
|
||||
if (priority) {
|
||||
wiremockStubDefinition.priority = priority
|
||||
}
|
||||
return JsonOutput.prettyPrint(JsonOutput.toJson(wiremockStubDefinition))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1152,6 +1152,35 @@ class WireMockGroovyDslSpec extends WireMockSpec {
|
||||
''')
|
||||
}
|
||||
|
||||
def "should generate stub with priority"() {
|
||||
given:
|
||||
GroovyDsl groovyDsl = GroovyDsl.make {
|
||||
priority 9
|
||||
request {
|
||||
method('POST')
|
||||
url("test")
|
||||
}
|
||||
response {
|
||||
status 406
|
||||
}
|
||||
}
|
||||
when:
|
||||
def json = toWireMockClientJsonStub(groovyDsl)
|
||||
then:
|
||||
parseJson(json) == parseJson('''
|
||||
{
|
||||
"priority": 9,
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"url": "test"
|
||||
},
|
||||
"response": {
|
||||
"status": 406
|
||||
}
|
||||
}
|
||||
''')
|
||||
}
|
||||
|
||||
String toJsonString(value) {
|
||||
new JsonBuilder(value).toPrettyString()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user