Refactoring to GString and fixed StubMappingConverter

This commit is contained in:
Jakub Kubrynski
2015-01-27 14:48:25 +01:00
parent 566e2f98bb
commit a2763f7c1c
6 changed files with 51 additions and 35 deletions

View File

@@ -43,16 +43,13 @@ class TestGenerator {
this.lang = testFramework
}
public String generate() {
StringBuilder builder = new StringBuilder()
public void generate() {
List<File> 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()
}
}

View File

@@ -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
}

View File

@@ -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) {

View File

@@ -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()

View File

@@ -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)
}
}

View File

@@ -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"
}