diff --git a/core/src/main/groovy/io/coderate/accurest/TestGenerator.groovy b/core/src/main/groovy/io/coderate/accurest/TestGenerator.groovy index be40bc05ba..bca414595f 100644 --- a/core/src/main/groovy/io/coderate/accurest/TestGenerator.groovy +++ b/core/src/main/groovy/io/coderate/accurest/TestGenerator.groovy @@ -43,16 +43,13 @@ class TestGenerator { this.lang = testFramework } - public String generate() { - StringBuilder builder = new StringBuilder() + public void generate() { List files = new File(stubsBaseDirectory).listFiles() files.grep({ File file -> file.isDirectory() && containsStubs(file) }).each { - builder << addClass(it) def testBaseDir = Paths.get(targetDirectory, NamesUtil.packageToDirectory(basePackageForTests)) Files.createDirectories(testBaseDir) Files.write(Paths.get(testBaseDir.toString(), NamesUtil.capitalize(it.name) + getTestClassExtension()), addClass(it).bytes) } - return builder } private String getTestClassExtension() { @@ -95,6 +92,6 @@ class TestGenerator { } public static void main(String[] args) { - print new TestGenerator('/home/devel/projects/codearte/accurest/core/src/main/resources/stubs', 'io.test', '', '', TestFramework.SPOCK, TestMode.MOCKMVC, "").generate() + new TestGenerator('/home/devel/projects/codearte/accurest/core/src/main/resources/stubs', 'io.test', '', '', TestFramework.SPOCK, TestMode.MOCKMVC, "").generate() } } \ No newline at end of file diff --git a/core/src/main/groovy/io/coderate/accurest/builder/BlockBuilder.groovy b/core/src/main/groovy/io/coderate/accurest/builder/BlockBuilder.groovy index 22f5247a5d..31e8a05aac 100644 --- a/core/src/main/groovy/io/coderate/accurest/builder/BlockBuilder.groovy +++ b/core/src/main/groovy/io/coderate/accurest/builder/BlockBuilder.groovy @@ -27,6 +27,16 @@ class BlockBuilder { return this } + BlockBuilder indent() { + startBlock().startBlock() + return this + } + + BlockBuilder unindent() { + endBlock().endBlock() + return this + } + BlockBuilder addLine(String line) { addIndentation() builder << "$line\n" @@ -38,7 +48,7 @@ class BlockBuilder { return this } - void addIndentation() { + private void addIndentation() { indents.times { builder << spacer } diff --git a/core/src/main/groovy/io/coderate/accurest/builder/ClassBuilder.groovy b/core/src/main/groovy/io/coderate/accurest/builder/ClassBuilder.groovy index 7a07910bff..7e368f8490 100644 --- a/core/src/main/groovy/io/coderate/accurest/builder/ClassBuilder.groovy +++ b/core/src/main/groovy/io/coderate/accurest/builder/ClassBuilder.groovy @@ -67,24 +67,24 @@ class ClassBuilder { String build() { BlockBuilder clazz = new BlockBuilder("\t") - .addLine("package $packageName" + suffix) + .addLine("package $packageName$suffix") .addEmptyLine() imports.sort().each { - clazz.addLine("import $it" + suffix) + clazz.addLine("import $it$suffix") } if (!imports.empty) { clazz.addEmptyLine() } staticImports.sort().each { - clazz.addLine("import static $it" + suffix) + clazz.addLine("import static $it$suffix") } if (!staticImports.empty) { clazz.addEmptyLine() } - def classLine = modifier + "class $className" + def classLine = "${modifier}class $className" if (baseClass) { classLine += " extends $baseClass" } @@ -94,7 +94,7 @@ class ClassBuilder { clazz.startBlock() rules.sort().each { clazz.addLine("@Rule") - clazz.addLine("public " + it + " " + NamesUtil.camelCase(it) + " = new $it()" + suffix) + clazz.addLine("public $it ${NamesUtil.camelCase(it)} = new $it()$suffix") } clazz.endBlock() if (!rules.empty) { diff --git a/core/src/main/groovy/io/coderate/accurest/builder/SpockMethodBodyBuilder.groovy b/core/src/main/groovy/io/coderate/accurest/builder/SpockMethodBodyBuilder.groovy index 528202f41d..85316e747a 100644 --- a/core/src/main/groovy/io/coderate/accurest/builder/SpockMethodBodyBuilder.groovy +++ b/core/src/main/groovy/io/coderate/accurest/builder/SpockMethodBodyBuilder.groovy @@ -17,36 +17,42 @@ class SpockMethodBodyBuilder { blockBuilder.startBlock() blockBuilder.addLine('given:').startBlock() blockBuilder.addLine('def request = given()') - blockBuilder.startBlock().startBlock() + blockBuilder.indent() stubDefinition.request.headers.each { blockBuilder.addLine(".header('$it.key', '$it.value.equalTo')") } if (stubDefinition.request.bodyPatterns) { - blockBuilder.addLine('.body(\'' + stubDefinition.request.bodyPatterns[0].matches + '\')') + blockBuilder.addLine(".body('${stubDefinition.request.bodyPatterns[0].matches}')") } - blockBuilder.endBlock().endBlock().endBlock().addEmptyLine() + blockBuilder.unindent().endBlock().addEmptyLine() blockBuilder.addLine('when:').startBlock() blockBuilder.addLine('def response = given().spec(request)') - blockBuilder.startBlock().startBlock() - blockBuilder.addLine('.' + stubDefinition.request.method.toLowerCase() + '("' + stubDefinition.request.url + '")') - blockBuilder.endBlock().endBlock().endBlock().addEmptyLine() + blockBuilder.indent() + blockBuilder.addLine(".${stubDefinition.request.method.toLowerCase()}(\"$stubDefinition.request.url\")") + blockBuilder.unindent().endBlock().addEmptyLine() blockBuilder.addLine('then:').startBlock() - blockBuilder.addLine('response.statusCode == ' + stubDefinition.response.status) + blockBuilder.addLine("response.statusCode == $stubDefinition.response.status") stubDefinition.response.headers.each { blockBuilder.addLine("response.header('$it.key') == '$it.value'") } if (stubDefinition.response.body) { blockBuilder.addLine('def responseBody = new JsonSlurper().parseText(response.body.asString())') - new groovy.json.JsonSlurper().parseText(stubDefinition.response.body).each { + stubDefinition.response.body.each { def value = it.value if (value instanceof String) { - value = '"' + value + '"' + if (value.startsWith('$')) { + value = value.substring(1).replaceAll('\\$it', "responseBody.$it.key") + blockBuilder.addLine(value) + } else { + blockBuilder.addLine("responseBody.$it.key == \"$value\"") + } + } else { + blockBuilder.addLine("responseBody.$it.key == $value") } - blockBuilder.addLine('responseBody.' + it.key + ' == ' + value) } } blockBuilder.endBlock() diff --git a/core/src/main/groovy/io/coderate/accurest/util/StubMappingConverter.groovy b/core/src/main/groovy/io/coderate/accurest/util/StubMappingConverter.groovy index 2315d5bc18..198c5ca99c 100644 --- a/core/src/main/groovy/io/coderate/accurest/util/StubMappingConverter.groovy +++ b/core/src/main/groovy/io/coderate/accurest/util/StubMappingConverter.groovy @@ -1,5 +1,6 @@ package io.coderate.accurest.util +import groovy.json.JsonException import groovy.json.JsonSlurper import java.util.regex.Pattern @@ -9,28 +10,30 @@ import java.util.regex.Pattern */ class StubMappingConverter { - private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile(/^\$\{(.*)\}:\$\{(.*)\}$/) + private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile(/^\$\{(.*):(.*)\}$/) public static final int SERVER_SIDE_GROUP = 2 static Map toStubMappingOnServerSide(File stubMapping) { def json = new JsonSlurper().parse(stubMapping) - convertPlaceholders(json as Map, { String value -> + return convertPlaceholders(json as Map, { String value -> getGroupFromMatchingPattern(value) }) - return json } - private static void convertPlaceholders(Map map, Closure closure) { - map.each { - if (it instanceof Map.Entry) { - Map.Entry entry = it as Map.Entry - if (entry.value instanceof String) { - String value = entry.value as String - entry.value = closure(value) - } else if (entry.value instanceof Map) { - convertPlaceholders(entry.value as Map, closure) - } + private static Map convertPlaceholders(Map map, Closure closure) { + return map.collectEntries { key, value -> [ key, transformValue(value, closure) ] } + } + + static def transformValue(def value, Closure closure) { + if (value instanceof String) { + try { + def json = new JsonSlurper().parseText(value as String) as Map + return convertPlaceholders(json, closure) + } catch (JsonException ignore) { + return closure(value) } + } else if (value instanceof Map) { + return convertPlaceholders(value as Map, closure) } } diff --git a/core/src/main/resources/stubs/wiremocks/sample.json b/core/src/main/resources/stubs/wiremocks/sample.json index 7ad5ef1a63..b6d6af2f50 100644 --- a/core/src/main/resources/stubs/wiremocks/sample.json +++ b/core/src/main/resources/stubs/wiremocks/sample.json @@ -5,7 +5,7 @@ }, "response": { "status": 200, - "body": "{\"loanApplicationStatus\":\"LOAN_APPLIED\",\"loanApplicationId\":123123123}", + "body": "{\"loanApplicationStatus\":\"LOAN_APPLIED\",\"loanApplicationId\":\"${123123123:$anyInt($it)}\"}", "headers": { "Content-Type": "text/plain" }