Merge pull request #59 from 4finance/issues/58-added-method-execution-on-server-side

[#58] Added method execution on server side
This commit is contained in:
Jakub Kubryński
2015-05-11 21:07:49 +02:00
7 changed files with 54 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package io.codearte.accurest.builder
import groovy.json.JsonOutput
import groovy.transform.PackageScope
import io.codearte.accurest.dsl.GroovyDsl
import io.codearte.accurest.dsl.internal.ExecutionProperty
import io.codearte.accurest.dsl.internal.Header
import java.util.regex.Pattern
@@ -77,6 +78,9 @@ class SpockMethodBodyBuilder {
processArrayElements(value, property, blockBuilder)
} else if (value instanceof Pattern) {
blockBuilder.addLine("responseBody$property ==~ java.util.regex.Pattern.compile('${value}')")
} else if (value instanceof ExecutionProperty) {
ExecutionProperty exec = (ExecutionProperty) value
blockBuilder.addLine("${exec.insertValue("responseBody$property")}")
} else {
blockBuilder.addLine("responseBody$property == ${value}")
}

View File

@@ -66,6 +66,10 @@ class Common {
return Pattern.compile(regex)
}
ExecutionProperty execute(String commandToExecute) {
return new ExecutionProperty(commandToExecute)
}
ClientDslProperty client(Object clientValue) {
return new ClientDslProperty(clientValue)
}

View File

@@ -0,0 +1,19 @@
package io.codearte.accurest.dsl.internal
import groovy.transform.CompileStatic
@CompileStatic
class ExecutionProperty {
private static final String PLACEHOLDER_VALUE = '\\$it'
final String executionCommand
ExecutionProperty(String executionCommand) {
this.executionCommand = executionCommand
}
String insertValue(String valueToInsert) {
return executionCommand.replaceAll(PLACEHOLDER_VALUE, valueToInsert)
}
}

View File

@@ -0,0 +1,19 @@
package io.codearte.accurest.dsl.internal
import spock.lang.Specification
class ExecutionPropertySpec extends Specification {
def 'should insert passed value in place of $it placeholder'() {
given:
String commandToExecute = 'commandToExecute($it)'
ExecutionProperty executionProperty = new ExecutionProperty(commandToExecute)
and:
String valueToInsert = 'someObject.itsValue'
when:
String commandWithInsertedValue = executionProperty.insertValue(valueToInsert)
then:
'commandToExecute(someObject.itsValue)' == commandWithInsertedValue
}
}

View File

@@ -13,7 +13,8 @@ io.codearte.accurest.dsl.GroovyDsl.make {
}
response {
body (
path: $(client('/api/12'), server(regex('^/api/[0-9]{2}$')))
path: $(client('/api/12'), server(regex('^/api/[0-9]{2}$'))),
correlationId: $(client('1223456'), server(execute('isProperCorrelationId($it)')))
)
status 200
}

View File

@@ -28,7 +28,8 @@ class PairIdController {
}
return """
{
"path" : "/api/$pairId"
"path" : "/api/$pairId",
"correlationId" : 123456
}
"""
}

View File

@@ -9,4 +9,8 @@ abstract class BaseMockMvcSpec extends Specification {
def setup() {
RestAssuredMockMvc.standaloneSetup(new PairIdController())
}
void isProperCorrelationId(Integer correlationId) {
assert correlationId == 123456
}
}