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:
@@ -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}")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -28,7 +28,8 @@ class PairIdController {
|
||||
}
|
||||
return """
|
||||
{
|
||||
"path" : "/api/$pairId"
|
||||
"path" : "/api/$pairId",
|
||||
"correlationId" : 123456
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
@@ -9,4 +9,8 @@ abstract class BaseMockMvcSpec extends Specification {
|
||||
def setup() {
|
||||
RestAssuredMockMvc.standaloneSetup(new PairIdController())
|
||||
}
|
||||
|
||||
void isProperCorrelationId(Integer correlationId) {
|
||||
assert correlationId == 123456
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user