From 1af2964d2e515c00387166cf5665d9af81b7d8b5 Mon Sep 17 00:00:00 2001 From: Jakub Kubrynski Date: Sat, 21 Feb 2015 17:49:36 +0100 Subject: [PATCH] Genrating server tests from Groovy DSL --- .../io/coderate/accurest/TestGenerator.groovy | 2 +- .../accurest/builder/MethodBuilder.groovy | 8 +++-- .../builder/SpockMethodBodyBuilder.groovy | 30 ++++++++++--------- .../io/codearte/accurest/MainTest.groovy | 4 +-- .../accurest/TestGeneratorSpec.groovy | 5 ++-- .../different/{diff.json => diff.groovy} | 0 .../different/{diff.json => diff.groovy} | 0 .../stubs/other/{other.json => other.groovy} | 0 .../other/{sample.json => sample.groovy} | 0 .../test/resources/dsl/basic/sampleDsl.groovy | 27 +++++++++++++++++ 10 files changed, 54 insertions(+), 22 deletions(-) rename accurest-core/src/test/resources/directory/with/stubs/different/{diff.json => diff.groovy} (100%) rename accurest-core/src/test/resources/directory/with/stubs/other/different/{diff.json => diff.groovy} (100%) rename accurest-core/src/test/resources/directory/with/stubs/other/{other.json => other.groovy} (100%) rename accurest-core/src/test/resources/directory/with/stubs/other/{sample.json => sample.groovy} (100%) create mode 100644 accurest-core/src/test/resources/dsl/basic/sampleDsl.groovy diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/TestGenerator.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/TestGenerator.groovy index a8dca060f4..105d102446 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/TestGenerator.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/TestGenerator.groovy @@ -56,7 +56,7 @@ class TestGenerator { if (!includedDirectoryRelativePath.isEmpty()) { List filesToClass = directoryScanner.includedFiles. grep { String includedFile -> - return includedFile.matches(includedDirectoryRelativePath + File.separator + "[A-Za-z0-9]*\\.json") + return includedFile.matches(includedDirectoryRelativePath + File.separator + "[A-Za-z0-9]*\\.groovy") } .collect { return new File(configProperties.stubsBaseDirectory, it) diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/builder/MethodBuilder.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/builder/MethodBuilder.groovy index 951d1ebd5d..43d8046e1d 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/builder/MethodBuilder.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/builder/MethodBuilder.groovy @@ -1,6 +1,8 @@ package io.coderate.accurest.builder import io.coderate.accurest.config.TestFramework +import io.coderate.accurest.dsl.GroovyDsl +import io.coderate.accurest.dsl.WiremockStubStrategy import io.coderate.accurest.util.NamesUtil import io.coderate.accurest.util.StubMappingConverter @@ -10,17 +12,17 @@ import io.coderate.accurest.util.StubMappingConverter class MethodBuilder { private final String methodName - private final Map stubContent + private final GroovyDsl stubContent private final TestFramework lang - private MethodBuilder(String methodName, Map stubContent, TestFramework lang) { + private MethodBuilder(String methodName, GroovyDsl stubContent, TestFramework lang) { this.stubContent = stubContent this.methodName = methodName this.lang = lang } static MethodBuilder createTestMethod(File stubsFile, TestFramework lang) { - Map stubContent = StubMappingConverter.toStubMappingOnServerSide(stubsFile) + GroovyDsl stubContent = new GroovyShell(this.classLoader).evaluate(stubsFile) String methodName = NamesUtil.camelCase(NamesUtil.toLastDot(NamesUtil.afterLast(stubsFile.path, File.separator))) return new MethodBuilder(methodName, stubContent, lang) } diff --git a/accurest-core/src/main/groovy/io/coderate/accurest/builder/SpockMethodBodyBuilder.groovy b/accurest-core/src/main/groovy/io/coderate/accurest/builder/SpockMethodBodyBuilder.groovy index 93b2afe581..79cdf0998e 100644 --- a/accurest-core/src/main/groovy/io/coderate/accurest/builder/SpockMethodBodyBuilder.groovy +++ b/accurest-core/src/main/groovy/io/coderate/accurest/builder/SpockMethodBodyBuilder.groovy @@ -1,15 +1,18 @@ package io.coderate.accurest.builder +import groovy.json.JsonOutput +import groovy.json.JsonSlurper import groovy.transform.PackageScope +import io.coderate.accurest.dsl.GroovyDsl /** * @author Jakub Kubrynski */ @PackageScope class SpockMethodBodyBuilder { - private final Map stubDefinition + private final GroovyDsl stubDefinition - SpockMethodBodyBuilder(Map stubDefinition) { + SpockMethodBodyBuilder(GroovyDsl stubDefinition) { this.stubDefinition = stubDefinition } @@ -18,12 +21,11 @@ class SpockMethodBodyBuilder { blockBuilder.addLine('given:').startBlock() blockBuilder.addLine('def request = given()') blockBuilder.indent() - stubDefinition.request.headers.each { - blockBuilder.addLine(".header('$it.key', '$it.value.equalTo')") + stubDefinition.request.headers.valueHeaders().each { + blockBuilder.addLine(".header('$it.key', '$it.value')") } - if (stubDefinition.request.bodyPatterns) { - String matches = stubDefinition.request.bodyPatterns[0].matches - matches = matches.replaceAll('\\\\', ''); + if (stubDefinition.request.body) { + String matches = new JsonOutput().toJson(stubDefinition.request.body.serverValue) blockBuilder.addLine(".body('$matches')") } @@ -32,28 +34,28 @@ class SpockMethodBodyBuilder { blockBuilder.addLine('when:').startBlock() blockBuilder.addLine('def response = given().spec(request)') blockBuilder.indent() - blockBuilder.addLine(".${stubDefinition.request.method.toLowerCase()}(\"$stubDefinition.request.url\")") + blockBuilder.addLine(".${stubDefinition.request.method.serverValue.toLowerCase()}(\"$stubDefinition.request.urlPattern.serverValue\")") blockBuilder.unindent().endBlock().addEmptyLine() blockBuilder.addLine('then:').startBlock() - blockBuilder.addLine("response.statusCode == $stubDefinition.response.status") + blockBuilder.addLine("response.statusCode == $stubDefinition.response.status.serverValue") - stubDefinition.response.headers.each { + stubDefinition.response.headers?.valueHeaders().each { blockBuilder.addLine("response.header('$it.key') == '$it.value'") } if (stubDefinition.response.body) { blockBuilder.addLine('def responseBody = new JsonSlurper().parseText(response.body.asString())') - stubDefinition.response.body.each { + stubDefinition.response.body.serverValue.each { def value = it.value if (value instanceof String) { if (value.startsWith('$')) { - value = value.substring(1).replaceAll('\\$it', "responseBody.$it.key") + value = value.substring(1).replaceAll('\\$value', "responseBody.$it.key") blockBuilder.addLine(value) } else { - blockBuilder.addLine("responseBody.$it.key == \"$value\"") + blockBuilder.addLine("responseBody.$it.key == \"${value}\"") } } else { - blockBuilder.addLine("responseBody.$it.key == $value") + blockBuilder.addLine("responseBody.$it.key == ${value}") } } } diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/MainTest.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/MainTest.groovy index 3c32375e38..27b9ec796d 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/MainTest.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/MainTest.groovy @@ -8,8 +8,8 @@ import io.coderate.accurest.config.TestMode class MainTest { public static void main(String[] args) { AccurestConfigProperties properties = new AccurestConfigProperties( - stubsBaseDirectory: '/home/devel/projects/codearte/accurest/accurest-core/src/main/resources/stubs', - generatedTestSourcesDir: '/tmp/accurest', + stubsBaseDirectory: new File('/home/devel/projects/codearte/accurest/accurest-core/src/test/resources/dsl'), + generatedTestSourcesDir: new File('/tmp/accurest'), targetFramework: TestFramework.SPOCK, testMode: TestMode.MOCKMVC, basePackageForTests: 'io.test', staticImports: ['com.pupablada.Test.*'], imports: ['org.innapypa.Test'], ignoredFiles: ["**/other"]) println new TestGenerator(properties).generate() diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/TestGeneratorSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/TestGeneratorSpec.groovy index e952e74082..0fcb65fe50 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/TestGeneratorSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/TestGeneratorSpec.groovy @@ -4,6 +4,7 @@ import io.coderate.accurest.FileSaver import io.coderate.accurest.SingleTestGenerator import io.coderate.accurest.TestGenerator import io.coderate.accurest.config.AccurestConfigProperties +import spock.lang.Ignore import spock.lang.Specification class TestGeneratorSpec extends Specification { @@ -39,13 +40,13 @@ class TestGeneratorSpec extends Specification { given: File resource = new File(this.getClass().getResource("/directory/with/stubs/stubsRepositoryIndicator").toURI()) AccurestConfigProperties properties = new AccurestConfigProperties() - properties.ignoredFiles << "**/other.json" + properties.ignoredFiles << "**/other.groovy" properties.stubsBaseDirectory = resource.parentFile TestGenerator testGenerator = new TestGenerator(properties, classGenerator, Stub(FileSaver)) classGenerator.buildClass(_, _, _) >> "sample" when: testGenerator.generateTestClasses("com.ofg") then: - 1 * classGenerator.buildClass({ it.size() == 1 }, 'other', _) >> "sample.json" + 1 * classGenerator.buildClass({ it.size() == 1 }, 'other', _) >> "sample.groovy" } } diff --git a/accurest-core/src/test/resources/directory/with/stubs/different/diff.json b/accurest-core/src/test/resources/directory/with/stubs/different/diff.groovy similarity index 100% rename from accurest-core/src/test/resources/directory/with/stubs/different/diff.json rename to accurest-core/src/test/resources/directory/with/stubs/different/diff.groovy diff --git a/accurest-core/src/test/resources/directory/with/stubs/other/different/diff.json b/accurest-core/src/test/resources/directory/with/stubs/other/different/diff.groovy similarity index 100% rename from accurest-core/src/test/resources/directory/with/stubs/other/different/diff.json rename to accurest-core/src/test/resources/directory/with/stubs/other/different/diff.groovy diff --git a/accurest-core/src/test/resources/directory/with/stubs/other/other.json b/accurest-core/src/test/resources/directory/with/stubs/other/other.groovy similarity index 100% rename from accurest-core/src/test/resources/directory/with/stubs/other/other.json rename to accurest-core/src/test/resources/directory/with/stubs/other/other.groovy diff --git a/accurest-core/src/test/resources/directory/with/stubs/other/sample.json b/accurest-core/src/test/resources/directory/with/stubs/other/sample.groovy similarity index 100% rename from accurest-core/src/test/resources/directory/with/stubs/other/sample.json rename to accurest-core/src/test/resources/directory/with/stubs/other/sample.groovy diff --git a/accurest-core/src/test/resources/dsl/basic/sampleDsl.groovy b/accurest-core/src/test/resources/dsl/basic/sampleDsl.groovy new file mode 100644 index 0000000000..e0e69eef4a --- /dev/null +++ b/accurest-core/src/test/resources/dsl/basic/sampleDsl.groovy @@ -0,0 +1,27 @@ +io.coderate.accurest.dsl.GroovyDsl.make { + request { + method('PUT') + headers { + header 'Content-Type': 'application/json' + } + body("""\ + { + "name": "Jan" + } + """ + ) + urlPattern $(client('/[0-9]{2}'), server('/12')) + } + response { + status 200 + body("""\ + { + "name": "Jan" + } + """ + ) + headers { + header 'Content-Type': 'text/plain' + } + } +}