Body object now extends DslProperty

This commit is contained in:
Jakub Kubrynski
2015-02-21 16:51:18 +01:00
parent e7d1b4501f
commit 71789a6031
4 changed files with 21 additions and 42 deletions

View File

@@ -219,7 +219,6 @@ class WiremockToDslConverterSpec extends Specification {
])
headers {
header 'Content-Type': 'application/json'
}
}
}

View File

@@ -32,7 +32,7 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy {
urlPattern: request?.urlPattern?.clientValue,
urlPath : request?.urlPath?.clientValue,
headers : buildClientHeadersSection(request.headers),
body : request?.body?.forClientSide()
body : request?.body?.clientValue
].findAll { it.value }
}
@@ -42,7 +42,7 @@ class WiremockRequestStubStrategy extends BaseWiremockStubStrategy {
urlPattern: request?.urlPattern?.serverValue,
urlPath : request?.urlPath?.serverValue,
headers : buildServerHeadersSection(request.headers),
body : request?.body?.forServerSide()
body : request?.body?.serverValue
].findAll { it.value }
}
}

View File

@@ -28,13 +28,13 @@ class WiremockResponseStubStrategy extends BaseWiremockStubStrategy {
private Map<String, Object> buildResponseContent(ClientResponse response) {
return [status : response?.status?.clientValue,
body : response?.body?.forClientSide(),
body : response?.body?.clientValue,
headers: buildClientHeadersSection(response.headers)].findAll { it.value }
}
private Map<String, Object> buildResponseContent(ServerResponse response) {
return [status : response?.status?.serverValue,
body : response?.body?.forServerSide(),
body : response?.body?.serverValue,
headers: buildServerHeadersSection(response.headers)].findAll { it.value }
}

View File

@@ -7,22 +7,23 @@ import org.codehaus.groovy.runtime.GStringImpl
@ToString(includePackage = false, includeFields = true)
@EqualsAndHashCode(includeFields = true)
class Body {
private Map<String, DslProperty> body
private DslProperty bodyAsValue
private List<DslProperty> bodyAsList
class Body extends DslProperty {
Body() {
this.body = [:]
super(null)
}
Body(Map<String, DslProperty> body) {
this.body = body
super(body.collectEntries { Map.Entry<String, DslProperty> entry ->
[(entry.key): entry.value.clientValue]
} as Map<String, Object>,
body.collectEntries { Map.Entry<String, DslProperty> entry ->
[(entry.key): entry.value.serverValue]
} as Map<String, Object>)
}
Body(List bodyAsList) {
this.bodyAsList = bodyAsList
super(bodyAsList.collect { it.clientValue }, bodyAsList.collect { it.serverValue })
}
Body(Object bodyAsValue) {
@@ -30,44 +31,23 @@ class Body {
}
Body(GString bodyAsValue) {
this.bodyAsValue = new DslProperty(getClientValue(bodyAsValue), getServerValue(bodyAsValue))
super(extractClientValue(bodyAsValue), extractServerValue(bodyAsValue))
}
private def getClientValue(GString bodyAsValue) {
Body(DslProperty bodyAsValue) {
super(bodyAsValue.clientValue, bodyAsValue.serverValue)
}
private static def extractClientValue(GString bodyAsValue) {
GString clientGString = new GStringImpl(bodyAsValue.values.clone(), bodyAsValue.strings.clone())
Object[] clientValues = bodyAsValue.values.collect { it instanceof DslProperty ? it.clientValue : it } as Object[]
return new JsonSlurper().parseText(new GStringImpl(clientValues, clientGString.strings).toString())
}
private def getServerValue(GString bodyAsValue) {
private static def extractServerValue(GString bodyAsValue) {
GString clientGString = new GStringImpl(bodyAsValue.values.clone(), bodyAsValue.strings.clone())
Object[] serverValues = bodyAsValue.values.collect { it instanceof DslProperty ? it.serverValue : it } as Object[]
return new JsonSlurper().parseText(new GStringImpl(serverValues, clientGString.strings).toString())
}
Body(DslProperty bodyAsValue) {
this.bodyAsValue = bodyAsValue
}
Object forClientSide() {
if (bodyAsValue) {
return bodyAsValue.clientValue
} else if (bodyAsList) {
bodyAsList.collect { it.clientValue }
}
return body.collectEntries { Map.Entry<String, DslProperty> entry ->
[(entry.key): entry.value.clientValue]
} as Map<String, Object>
}
Object forServerSide() {
if (bodyAsValue) {
return bodyAsValue.serverValue
} else if (bodyAsList) {
bodyAsList.collect { it.serverValue }
}
return body.collectEntries { Map.Entry<String, DslProperty> entry ->
[(entry.key): entry.value.serverValue]
} as Map<String, Object>
}
}