Refactored the conversion from String / File to Groovy DSL
This commit is contained in:
@@ -2,7 +2,7 @@ package io.codearte.accurest.wiremock
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import io.codearte.accurest.dsl.GroovyDsl
|
||||
import org.codehaus.groovy.control.CompilerConfiguration
|
||||
import io.codearte.accurest.util.AccurestDslConverter
|
||||
|
||||
@CompileStatic
|
||||
abstract class DslToWireMockConverter implements SingleFileConverter {
|
||||
@@ -18,6 +18,6 @@ abstract class DslToWireMockConverter implements SingleFileConverter {
|
||||
}
|
||||
|
||||
protected GroovyDsl createGroovyDSLfromStringContent(String groovyDslAsString) {
|
||||
return (GroovyDsl) new GroovyShell(this.class.classLoader, new Binding(), new CompilerConfiguration(sourceEncoding:'UTF-8')).evaluate("$groovyDslAsString")
|
||||
return AccurestDslConverter.convert(groovyDslAsString)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.codearte.accurest.wiremock
|
||||
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping
|
||||
import io.codearte.accurest.dsl.GroovyDsl
|
||||
import io.codearte.accurest.util.AccurestDslConverter
|
||||
import spock.lang.Specification
|
||||
|
||||
class WireMockToDslConverterSpec extends Specification {
|
||||
@@ -67,7 +68,7 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
when:
|
||||
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
|
||||
then:
|
||||
new GroovyShell(this.class.classLoader).evaluate(
|
||||
AccurestDslConverter.convert(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""") == expectedGroovyDsl
|
||||
@@ -121,7 +122,7 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
when:
|
||||
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
|
||||
then:
|
||||
new GroovyShell(this.class.classLoader).evaluate(
|
||||
AccurestDslConverter.convert(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""") == expectedGroovyDsl
|
||||
@@ -172,7 +173,7 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
when:
|
||||
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
|
||||
then:
|
||||
new GroovyShell(this.class.classLoader).evaluate(
|
||||
AccurestDslConverter.convert(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""") == expectedGroovyDsl
|
||||
@@ -226,7 +227,7 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
when:
|
||||
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
|
||||
then:
|
||||
new GroovyShell(this.class.classLoader).evaluate(
|
||||
AccurestDslConverter.convert(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""") == expectedGroovyDsl
|
||||
@@ -291,7 +292,7 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
when:
|
||||
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
|
||||
then:
|
||||
new GroovyShell(this.class.classLoader).evaluate(
|
||||
AccurestDslConverter.convert(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""") == expectedGroovyDsl
|
||||
@@ -329,7 +330,7 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
when:
|
||||
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
|
||||
then:
|
||||
GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate(
|
||||
GroovyDsl evaluatedGroovyDsl = AccurestDslConverter.convert(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""")
|
||||
@@ -369,7 +370,7 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
when:
|
||||
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
|
||||
then:
|
||||
GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate(
|
||||
GroovyDsl evaluatedGroovyDsl = AccurestDslConverter.convert(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""")
|
||||
@@ -410,7 +411,7 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
when:
|
||||
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
|
||||
then:
|
||||
GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate(
|
||||
GroovyDsl evaluatedGroovyDsl = AccurestDslConverter.convert(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""")
|
||||
@@ -450,7 +451,7 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
when:
|
||||
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
|
||||
then:
|
||||
GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate(
|
||||
GroovyDsl evaluatedGroovyDsl = AccurestDslConverter.convert(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""")
|
||||
@@ -490,7 +491,7 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
when:
|
||||
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
|
||||
then:
|
||||
GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate(
|
||||
GroovyDsl evaluatedGroovyDsl = AccurestDslConverter.convert(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""")
|
||||
@@ -528,7 +529,7 @@ class WireMockToDslConverterSpec extends Specification {
|
||||
when:
|
||||
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
|
||||
then:
|
||||
GroovyDsl evaluatedGroovyDsl = new GroovyShell(this.class.classLoader).evaluate(
|
||||
GroovyDsl evaluatedGroovyDsl = AccurestDslConverter.convert(
|
||||
""" io.codearte.accurest.dsl.GroovyDsl.make {
|
||||
$groovyDsl
|
||||
}""")
|
||||
|
||||
@@ -10,7 +10,7 @@ import io.codearte.accurest.config.TestFramework
|
||||
import io.codearte.accurest.config.TestMode
|
||||
import io.codearte.accurest.dsl.GroovyDsl
|
||||
import io.codearte.accurest.file.Contract
|
||||
import org.codehaus.groovy.control.CompilerConfiguration
|
||||
import io.codearte.accurest.util.AccurestDslConverter
|
||||
|
||||
import static io.codearte.accurest.builder.ClassBuilder.createClass
|
||||
import static io.codearte.accurest.builder.MethodBuilder.createTestMethod
|
||||
@@ -58,7 +58,7 @@ class SingleTestGenerator {
|
||||
Map<ParsedDsl, TestType> contracts = listOfFiles.collectEntries {
|
||||
File stubsFile = it.path.toFile()
|
||||
log.debug("Stub content from file [${stubsFile.text}]")
|
||||
GroovyDsl stubContent = new GroovyShell(delegate.class.classLoader, new Binding(), new CompilerConfiguration(sourceEncoding:'UTF-8')).evaluate(stubsFile)
|
||||
GroovyDsl stubContent = AccurestDslConverter.convert(stubsFile)
|
||||
TestType testType = (stubContent.input || stubContent.outputMessage) ? TestType.MESSAGING : TestType.HTTP
|
||||
return [(new ParsedDsl(it, stubContent, stubsFile)) : testType]
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ class ContractFileScanner {
|
||||
}
|
||||
|
||||
private boolean hasScenarioFilenamePattern(Path path) {
|
||||
SCENARIO_STEP_FILENAME_PATTERN.matcher(path.fileName.toString()).matches()
|
||||
return SCENARIO_STEP_FILENAME_PATTERN.matcher(path.fileName.toString()).matches()
|
||||
}
|
||||
|
||||
boolean matchesPattern(File file, Set<PathMatcher> excludeMatchers) {
|
||||
@@ -81,6 +81,6 @@ class ContractFileScanner {
|
||||
}
|
||||
|
||||
private boolean isContractFile(File file) {
|
||||
file.isFile() && FilenameUtils.getExtension(file.toString()).equalsIgnoreCase("groovy")
|
||||
return file.isFile() && FilenameUtils.getExtension(file.toString()).equalsIgnoreCase("groovy")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package io.codearte.accurest.util
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import io.codearte.accurest.dsl.GroovyDsl
|
||||
import org.codehaus.groovy.control.CompilerConfiguration
|
||||
|
||||
/**
|
||||
* Converts to Groovy DSL
|
||||
*
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
@CompileStatic
|
||||
class AccurestDslConverter {
|
||||
|
||||
static GroovyDsl convert(String dsl) {
|
||||
return groovyShell().evaluate(dsl) as GroovyDsl
|
||||
}
|
||||
|
||||
static GroovyDsl convert(File dsl) {
|
||||
return groovyShell().evaluate(dsl) as GroovyDsl
|
||||
}
|
||||
|
||||
private static GroovyShell groovyShell() {
|
||||
return new GroovyShell(AccurestDslConverter.classLoader, new Binding(), new CompilerConfiguration(sourceEncoding: 'UTF-8'))
|
||||
}
|
||||
}
|
||||
@@ -4,8 +4,7 @@ import groovy.transform.CompileStatic
|
||||
import groovy.transform.PackageScope
|
||||
import groovy.util.logging.Slf4j
|
||||
import io.codearte.accurest.dsl.GroovyDsl
|
||||
import org.codehaus.groovy.control.CompilerConfiguration
|
||||
|
||||
import io.codearte.accurest.util.AccurestDslConverter
|
||||
/**
|
||||
* Wraps the folder with WireMock mappings.
|
||||
*/
|
||||
@@ -68,8 +67,7 @@ class StubRepository {
|
||||
descriptorsDirectory.eachFileRecurse { File file ->
|
||||
if (isAccurestDescriptor(file)) {
|
||||
try {
|
||||
mappingDescriptors <<
|
||||
((new GroovyShell(delegate.class.classLoader, new Binding(), new CompilerConfiguration(sourceEncoding:'UTF-8')).evaluate(file)) as GroovyDsl)
|
||||
mappingDescriptors << AccurestDslConverter.convert(file)
|
||||
} catch (Exception e) {
|
||||
log.warn("Exception occurred while trying to parse file [$file]", e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user