Generating test cases for simple stubs
This commit is contained in:
13
build.gradle
13
build.gradle
@@ -1,9 +1,14 @@
|
||||
ext {
|
||||
projectVersion = '0.1.0-SNAPSHOT'
|
||||
projectGroup = 'io.codearte.accurest'
|
||||
}
|
||||
|
||||
subprojects {
|
||||
apply plugin: 'groovy'
|
||||
apply plugin: 'maven-publish'
|
||||
|
||||
group = 'io.codearte.accurest'
|
||||
version = '1.0.0-SNAPSHOT'
|
||||
group = projectGroup
|
||||
version = projectVersion
|
||||
|
||||
sourceCompatibility = 1.7
|
||||
targetCompatibility = 1.7
|
||||
@@ -21,8 +26,8 @@ subprojects {
|
||||
publishing {
|
||||
publications {
|
||||
maven(MavenPublication) {
|
||||
groupId 'io.codearte.accurest'
|
||||
version '1.0.0-SNAPSHOT'
|
||||
groupId projectGroup
|
||||
version projectVersion
|
||||
|
||||
from components.java
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package io.coderate.accurest
|
||||
|
||||
import io.coderate.accurest.builder.ClassBuilder
|
||||
import io.coderate.accurest.builder.TestFramework
|
||||
import io.coderate.accurest.config.TestFramework
|
||||
import io.coderate.accurest.config.TestMode
|
||||
import io.coderate.accurest.util.NamesUtil
|
||||
|
||||
import java.nio.file.Files
|
||||
@@ -21,10 +22,11 @@ class TestGenerator {
|
||||
private final String ruleClassForTests
|
||||
private final TestFramework lang
|
||||
private final String targetDirectory
|
||||
private final TestMode testMode
|
||||
|
||||
TestGenerator(String stubsBaseDirectory, String basePackageForTests, String baseClassForTests,
|
||||
String ruleClassForTests, String testFramework, String targetDirectory) {
|
||||
// URL stubsResource = getClass().getClassLoader().getResource(stubsBaseDirectory)
|
||||
String ruleClassForTests, TestFramework testFramework, TestMode testMode, String targetDirectory) {
|
||||
this.testMode = testMode
|
||||
this.targetDirectory = targetDirectory
|
||||
File stubsResource = new File(stubsBaseDirectory)
|
||||
if (stubsResource == null) {
|
||||
@@ -38,7 +40,7 @@ class TestGenerator {
|
||||
this.baseClassForTests = baseClassForTests
|
||||
}
|
||||
this.ruleClassForTests = ruleClassForTests
|
||||
this.lang = testFramework == 'Spock' ? TestFramework.SPOCK : TestFramework.JUNIT
|
||||
this.lang = testFramework
|
||||
}
|
||||
|
||||
public String generate() {
|
||||
@@ -49,7 +51,6 @@ class TestGenerator {
|
||||
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)
|
||||
println testBaseDir
|
||||
}
|
||||
return builder
|
||||
}
|
||||
@@ -69,11 +70,17 @@ class TestGenerator {
|
||||
|
||||
private String addClass(File directory) {
|
||||
ClassBuilder clazz = createClass(NamesUtil.capitalize(NamesUtil.afterLast(directory.path, '/')), basePackageForTests, baseClassForTests, lang)
|
||||
.addStaticImport('com.jayway.restassured.RestAssured.*')
|
||||
.addImport('java.util.regex.Pattern')
|
||||
|
||||
if (testMode == TestMode.MOCKMVC) {
|
||||
clazz.addStaticImport('com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*')
|
||||
} else {
|
||||
clazz.addStaticImport('com.jayway.restassured.RestAssured.*')
|
||||
}
|
||||
|
||||
if (lang == TestFramework.JUNIT) {
|
||||
clazz.addImport('org.junit.Test')
|
||||
} else {
|
||||
clazz.addImport('groovy.json.JsonSlurper')
|
||||
}
|
||||
|
||||
if (ruleClassForTests) {
|
||||
@@ -88,6 +95,6 @@ class TestGenerator {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
print new TestGenerator('stubs', 'io.test', '', '', 'Spock').generate()
|
||||
print new TestGenerator('/home/devel/projects/codearte/accurest/core/src/main/resources/stubs', 'io.test', '', '', TestFramework.SPOCK, TestMode.MOCKMVC, "").generate()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.coderate.accurest.builder
|
||||
|
||||
import io.coderate.accurest.config.TestFramework
|
||||
import io.coderate.accurest.util.NamesUtil
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.coderate.accurest.builder
|
||||
|
||||
import io.coderate.accurest.config.TestFramework
|
||||
import io.coderate.accurest.util.NamesUtil
|
||||
import io.coderate.accurest.util.StubMappingConverter
|
||||
|
||||
|
||||
@@ -40,7 +40,14 @@ class SpockMethodBodyBuilder {
|
||||
blockBuilder.addLine("response.header('$it.key') == '$it.value'")
|
||||
}
|
||||
if (stubDefinition.response.body) {
|
||||
blockBuilder.addLine("Pattern.compile('$stubDefinition.response.body', Pattern.DOTALL).matcher(response.body.asString()).matches()")
|
||||
blockBuilder.addLine('def responseBody = new JsonSlurper().parseText(response.body.asString())')
|
||||
new groovy.json.JsonSlurper().parseText(stubDefinition.response.body).each {
|
||||
def value = it.value
|
||||
if (value instanceof String) {
|
||||
value = '"' + value + '"'
|
||||
}
|
||||
blockBuilder.addLine('responseBody.' + it.key + ' == ' + value)
|
||||
}
|
||||
}
|
||||
blockBuilder.endBlock()
|
||||
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package io.coderate.accurest.builder
|
||||
package io.coderate.accurest.config
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
enum TestFramework {
|
||||
|
||||
JUNIT, SPOCK
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.coderate.accurest.config
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
enum TestMode {
|
||||
MOCKMVC, EXPLICIT
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"body": "Hello world!",
|
||||
"body": "{\"loanApplicationStatus\":\"LOAN_APPLIED\",\"loanApplicationId\":123123123}",
|
||||
"headers": {
|
||||
"Content-Type": "text/plain"
|
||||
}
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
{
|
||||
"request": {
|
||||
"method": "POST",
|
||||
"url": "/some/thing",
|
||||
"headers": {
|
||||
"Accept": {
|
||||
"equalTo": "text/plain"
|
||||
}
|
||||
},
|
||||
"bodyPatterns": [
|
||||
{
|
||||
"matches": "{\"client\":\"1234\"}"
|
||||
}
|
||||
]
|
||||
},
|
||||
"response": {
|
||||
"status": 400
|
||||
}
|
||||
}
|
||||
@@ -27,9 +27,11 @@ class AccurestGradlePlugin implements Plugin<Project> {
|
||||
}
|
||||
|
||||
try {
|
||||
TestGenerator generator = new TestGenerator(project.projectDir.path + '/src/test/resources/' + extension.stubsBaseDirectory, extension.basePackageForTests,
|
||||
extension.baseClassForTests, extension.ruleClassForTests,
|
||||
extension.targetFramework, extension.generatedTestSourcesDir)
|
||||
String resourceDirectory = project.projectDir.path + '/src/test/resources/' + extension.stubsBaseDirectory
|
||||
TestGenerator generator = new TestGenerator(resourceDirectory,
|
||||
extension.basePackageForTests, extension.baseClassForTests, extension.ruleClassForTests,
|
||||
extension.targetFramework, extension.testMode,
|
||||
extension.generatedTestSourcesDir)
|
||||
generator.generate()
|
||||
} catch (IllegalStateException e) {
|
||||
project.logger.error("Accurest Plugin: {}", e.getMessage())
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package io.codearte.accurest.plugin
|
||||
|
||||
import io.coderate.accurest.config.TestFramework
|
||||
import io.coderate.accurest.config.TestMode
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
class AccurestPluginExtension {
|
||||
String targetFramework = 'Spock'
|
||||
TestFramework targetFramework = TestFramework.SPOCK
|
||||
TestMode testMode = TestMode.EXPLICIT
|
||||
String stubsBaseDirectory = 'mappings'
|
||||
String basePackageForTests = 'io.codearte.accurest.tests'
|
||||
String baseClassForTests
|
||||
|
||||
Reference in New Issue
Block a user