Genrating server tests from Groovy DSL
This commit is contained in:
@@ -56,7 +56,7 @@ class TestGenerator {
|
||||
if (!includedDirectoryRelativePath.isEmpty()) {
|
||||
List<File> 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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
27
accurest-core/src/test/resources/dsl/basic/sampleDsl.groovy
Normal file
27
accurest-core/src/test/resources/dsl/basic/sampleDsl.groovy
Normal file
@@ -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'
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user