From 4d02af97114b485d4b594e985f760831ab6fdefe Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 11 May 2015 19:03:08 +0200 Subject: [PATCH] [#58] Added method execution on server side --- .../builder/SpockMethodBodyBuilder.groovy | 4 ++++ .../accurest/dsl/internal/Common.groovy | 4 ++++ .../dsl/internal/ExecutionProperty.groovy | 19 +++++++++++++++++++ .../dsl/internal/ExecutionPropertySpec.groovy | 19 +++++++++++++++++++ .../pairId/moreComplexVersion.groovy | 3 ++- .../ofg/twitter/place/PairIdController.groovy | 3 ++- .../ofg/twitter/places/BaseMockMvcSpec.groovy | 4 ++++ 7 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/ExecutionProperty.groovy create mode 100644 accurest-core/src/test/groovy/io/codearte/accurest/dsl/internal/ExecutionPropertySpec.groovy diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy index babee4264c..532e429952 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/builder/SpockMethodBodyBuilder.groovy @@ -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}") } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy index f6eb65ab9e..91549f6409 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy @@ -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) } diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/ExecutionProperty.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/ExecutionProperty.groovy new file mode 100644 index 0000000000..ccc7037575 --- /dev/null +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/ExecutionProperty.groovy @@ -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) + } +} diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/internal/ExecutionPropertySpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/internal/ExecutionPropertySpec.groovy new file mode 100644 index 0000000000..d8660a8433 --- /dev/null +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/internal/ExecutionPropertySpec.groovy @@ -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 + } + +} diff --git a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/repository/mappings/com/ofg/twitter-places-analyzer/pairId/moreComplexVersion.groovy b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/repository/mappings/com/ofg/twitter-places-analyzer/pairId/moreComplexVersion.groovy index 9937b43721..fdeb658215 100644 --- a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/repository/mappings/com/ofg/twitter-places-analyzer/pairId/moreComplexVersion.groovy +++ b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/repository/mappings/com/ofg/twitter-places-analyzer/pairId/moreComplexVersion.groovy @@ -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 } diff --git a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy index ee02469454..4f49f1ed3b 100644 --- a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy +++ b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/main/groovy/com/ofg/twitter/place/PairIdController.groovy @@ -28,7 +28,8 @@ class PairIdController { } return """ { - "path" : "/api/$pairId" + "path" : "/api/$pairId", + "correlationId" : 123456 } """ } diff --git a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/test/groovy/com/ofg/twitter/places/BaseMockMvcSpec.groovy b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/test/groovy/com/ofg/twitter/places/BaseMockMvcSpec.groovy index b88c863fcc..cade991eaa 100644 --- a/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/test/groovy/com/ofg/twitter/places/BaseMockMvcSpec.groovy +++ b/accurest-gradle-plugin/src/test/resources/functionalTest/bootSimple/src/test/groovy/com/ofg/twitter/places/BaseMockMvcSpec.groovy @@ -9,4 +9,8 @@ abstract class BaseMockMvcSpec extends Specification { def setup() { RestAssuredMockMvc.standaloneSetup(new PairIdController()) } + + void isProperCorrelationId(Integer correlationId) { + assert correlationId == 123456 + } }