Introduced configuration
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
package eu.coderate.accurest
|
||||
|
||||
import static eu.coderate.accurest.builder.ClassBuilder.createClass
|
||||
import static eu.coderate.accurest.builder.MethodBuilder.createVoidMethod
|
||||
|
||||
class Generator {
|
||||
|
||||
public static void main(String[] args) {
|
||||
print createClass('MyTest', 'io.test', "com.test.BaseClass")
|
||||
.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()
|
||||
}
|
||||
}
|
||||
49
src/main/groovy/eu/coderate/accurest/TestGenerator.groovy
Normal file
49
src/main/groovy/eu/coderate/accurest/TestGenerator.groovy
Normal file
@@ -0,0 +1,49 @@
|
||||
package eu.coderate.accurest
|
||||
|
||||
import eu.coderate.accurest.util.NamesUtil
|
||||
|
||||
import static eu.coderate.accurest.builder.ClassBuilder.createClass
|
||||
import static eu.coderate.accurest.builder.MethodBuilder.createVoidMethod
|
||||
|
||||
/**
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
class TestGenerator {
|
||||
|
||||
private final String mocksBaseDirectory
|
||||
private final String basePackageForTests
|
||||
private final String baseClassForTests
|
||||
private final String ruleClassForTests
|
||||
|
||||
TestGenerator(String mocksBaseDirectory, String basePackageForTests, String baseClassForTests, String ruleClassForTests) {
|
||||
this.mocksBaseDirectory = getClass().getClassLoader().getResource(mocksBaseDirectory).path
|
||||
this.basePackageForTests = basePackageForTests
|
||||
this.baseClassForTests = baseClassForTests
|
||||
this.ruleClassForTests = ruleClassForTests
|
||||
}
|
||||
|
||||
public String generate() {
|
||||
StringBuilder builder = new StringBuilder()
|
||||
List<String> files = new File(mocksBaseDirectory).list()
|
||||
files.each {
|
||||
builder << addClass(NamesUtil.capitalize(NamesUtil.toLastDot(it)))
|
||||
}
|
||||
return builder
|
||||
}
|
||||
|
||||
private String addClass(String className) {
|
||||
createClass(className, 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()
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
print new TestGenerator("wiremocks", 'io.test', 'com.test.BaseClass', "").generate()
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ class ClassBuilder {
|
||||
if (baseClass) {
|
||||
imports << baseClass
|
||||
}
|
||||
this.baseClass = NamesUtil.getName(baseClass)
|
||||
this.baseClass = NamesUtil.afterLastDot(baseClass)
|
||||
this.packageName = packageName
|
||||
this.className = className
|
||||
}
|
||||
@@ -49,7 +49,7 @@ class ClassBuilder {
|
||||
|
||||
ClassBuilder addRule(String ruleClass) {
|
||||
imports << ruleClass
|
||||
rules << NamesUtil.getName(ruleClass)
|
||||
rules << NamesUtil.afterLastDot(ruleClass)
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ class ClassBuilder {
|
||||
clazz.startBlock()
|
||||
rules.sort().each {
|
||||
clazz.addLine("@Rule")
|
||||
clazz.addLine("public " + it + " " + NamesUtil.fieldName(it) + " = new $it();")
|
||||
clazz.addLine("public " + it + " " + NamesUtil.uncapitalize(it) + " = new $it();")
|
||||
}
|
||||
clazz.endBlock()
|
||||
clazz.addEmptyLine()
|
||||
|
||||
@@ -4,19 +4,33 @@ package eu.coderate.accurest.util
|
||||
* @author Jakub Kubrynski
|
||||
*/
|
||||
class NamesUtil {
|
||||
static String getName(String qualifiedName) {
|
||||
if (qualifiedName?.indexOf('.') > -1) {
|
||||
return qualifiedName.substring(qualifiedName.lastIndexOf('.')+1)
|
||||
static String afterLastDot(String string) {
|
||||
if (string?.indexOf('.') > -1) {
|
||||
return string.substring(string.lastIndexOf('.')+1)
|
||||
}
|
||||
return qualifiedName
|
||||
return string
|
||||
}
|
||||
|
||||
static String fieldName(String className) {
|
||||
if (className) {
|
||||
static String uncapitalize(String className) {
|
||||
if (!className) {
|
||||
return className
|
||||
}
|
||||
String name = getName(className)
|
||||
String firstChar = name.charAt(0).toLowerCase() as String
|
||||
return firstChar + name.substring(1)
|
||||
String firstChar = className.charAt(0).toLowerCase() as String
|
||||
return firstChar + className.substring(1)
|
||||
}
|
||||
|
||||
static String capitalize(String className) {
|
||||
if (!className) {
|
||||
return className
|
||||
}
|
||||
String firstChar = className.charAt(0).toUpperCase() as String
|
||||
return firstChar + className.substring(1)
|
||||
}
|
||||
|
||||
static String toLastDot(String string) {
|
||||
if (string?.indexOf('.') > -1) {
|
||||
return string.substring(0, string.lastIndexOf('.'))
|
||||
}
|
||||
return string
|
||||
}
|
||||
}
|
||||
|
||||
13
src/main/resources/wiremocks/sample.json
Normal file
13
src/main/resources/wiremocks/sample.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"url": "/some/thing"
|
||||
},
|
||||
"response": {
|
||||
"status": 200,
|
||||
"body": "Hello world!",
|
||||
"headers": {
|
||||
"Content-Type": "text/plain"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/main/resources/wiremocks/shouldFail.json
Normal file
9
src/main/resources/wiremocks/shouldFail.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"url": "/some/thing2"
|
||||
},
|
||||
"response": {
|
||||
"status": 400
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user