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