Reading stub definitions basics
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package eu.coderate.accurest
|
||||
|
||||
import eu.coderate.accurest.builder.ClassBuilder
|
||||
import eu.coderate.accurest.util.NamesUtil
|
||||
|
||||
import static eu.coderate.accurest.builder.ClassBuilder.createClass
|
||||
@@ -24,26 +25,37 @@ class TestGenerator {
|
||||
|
||||
public String generate() {
|
||||
StringBuilder builder = new StringBuilder()
|
||||
List<String> files = new File(mocksBaseDirectory).list()
|
||||
files.each {
|
||||
builder << addClass(NamesUtil.capitalize(NamesUtil.toLastDot(it)))
|
||||
List<File> files = new File(mocksBaseDirectory).listFiles()
|
||||
files.grep({ File file -> file.isDirectory() && containsStubs(file) }).each {
|
||||
builder << addClass(it)
|
||||
}
|
||||
return builder
|
||||
}
|
||||
|
||||
private String addClass(String className) {
|
||||
createClass(className, basePackageForTests, baseClassForTests)
|
||||
boolean containsStubs(File file) {
|
||||
return file.list(new FilenameFilter() {
|
||||
@Override
|
||||
boolean accept(File dir, String name) {
|
||||
return "json".equalsIgnoreCase(NamesUtil.afterLastDot(name))
|
||||
}
|
||||
}).size() > 0
|
||||
}
|
||||
|
||||
private String addClass(File directory) {
|
||||
ClassBuilder clazz = createClass(NamesUtil.capitalize(NamesUtil.afterLast(directory.path, '/')), basePackageForTests, baseClassForTests)
|
||||
.addImport('org.junit.Test')
|
||||
.addImport('org.junit.Rule')
|
||||
.addStaticImport('org.hamcrest.Matchers.*')
|
||||
.addStaticImport('com.jayway.restassured.RestAssured.*')
|
||||
.addStaticImport('com.jayway.restassured.matcher.RestAssuredMatchers.*')
|
||||
.addRule('com.test.MyRule')
|
||||
.addMethod(createVoidMethod('shouldInvokeService'))
|
||||
.build()
|
||||
directory.listFiles().each {
|
||||
clazz.addMethod(createVoidMethod(it))
|
||||
}
|
||||
return clazz.build()
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
print new TestGenerator("wiremocks", 'io.test', 'com.test.BaseClass', "").generate()
|
||||
print new TestGenerator("", 'io.test', 'com.test.BaseClass', "").generate()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package eu.coderate.accurest.builder
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
class MethodBodyBuilder {
|
||||
private final Map stubDefinition
|
||||
|
||||
MethodBodyBuilder(Map stubDefinition) {
|
||||
this.stubDefinition = stubDefinition
|
||||
}
|
||||
|
||||
void appendTo(BlockBuilder blockBuilder) {
|
||||
blockBuilder.startBlock()
|
||||
blockBuilder.addLine("//" + stubDefinition.request.method)
|
||||
blockBuilder.addLine("//" + stubDefinition.response.status)
|
||||
blockBuilder.endBlock()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
package eu.coderate.accurest.builder
|
||||
|
||||
import eu.coderate.accurest.util.NamesUtil
|
||||
import groovy.json.JsonSlurper
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
@@ -7,19 +10,24 @@ class MethodBuilder {
|
||||
|
||||
private final String methodName
|
||||
private final String returnType
|
||||
private final Map stubContent
|
||||
|
||||
private MethodBuilder(String returnType, String methodName) {
|
||||
private MethodBuilder(String returnType, String methodName, Map stubContent) {
|
||||
this.stubContent = stubContent
|
||||
this.returnType = returnType
|
||||
this.methodName = methodName
|
||||
}
|
||||
|
||||
static MethodBuilder createVoidMethod(String methodName) {
|
||||
return new MethodBuilder("void", methodName)
|
||||
static MethodBuilder createVoidMethod(File stubsFile) {
|
||||
Map stubContent = new JsonSlurper().parse(stubsFile)
|
||||
String methodName = NamesUtil.uncapitalize(NamesUtil.toLastDot(NamesUtil.afterLast(stubsFile.path, File.separator)))
|
||||
return new MethodBuilder("void", methodName, stubContent)
|
||||
}
|
||||
|
||||
void appendTo(BlockBuilder blockBuilder) {
|
||||
blockBuilder.addLine('@Test')
|
||||
blockBuilder.addLine("public $returnType $methodName {")
|
||||
.addLine('}')
|
||||
blockBuilder.addLine("public $returnType $methodName() {")
|
||||
new MethodBodyBuilder(stubContent).appendTo(blockBuilder)
|
||||
blockBuilder.addLine('}')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,18 @@ package eu.coderate.accurest.util
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
class NamesUtil {
|
||||
static String afterLastDot(String string) {
|
||||
if (string?.indexOf('.') > -1) {
|
||||
return string.substring(string.lastIndexOf('.')+1)
|
||||
|
||||
static String afterLast(String string, String separator) {
|
||||
if (string?.indexOf(separator) > -1) {
|
||||
return string.substring(string.lastIndexOf(separator)+1)
|
||||
}
|
||||
return string
|
||||
}
|
||||
|
||||
static String afterLastDot(String string) {
|
||||
return afterLast(string, '.')
|
||||
}
|
||||
|
||||
static String uncapitalize(String className) {
|
||||
if (!className) {
|
||||
return className
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"url": "/some/thing2"
|
||||
"method": "POST",
|
||||
"url": "/some/thing"
|
||||
},
|
||||
"response": {
|
||||
"status": 400
|
||||
|
||||
Reference in New Issue
Block a user