[#22] Dsl to Wiremock converters
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package io.codearte.accurest.wiremock
|
||||
|
||||
import io.coderate.accurest.dsl.WiremockStubStrategy
|
||||
|
||||
class DslToWiremockClientConverter extends DslToWiremockConverter {
|
||||
|
||||
@Override
|
||||
String convertContent(String dslBody) {
|
||||
return new WiremockStubStrategy(createGroovyDSLfromStringContent(dslBody)).toWiremockClientStub()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package io.codearte.accurest.wiremock
|
||||
|
||||
import io.coderate.accurest.dsl.GroovyDsl
|
||||
|
||||
abstract class DslToWiremockConverter implements SingleFileConverter {
|
||||
|
||||
@Override
|
||||
boolean canHandleFileName(String fileName) {
|
||||
return fileName.endsWith('.groovy')
|
||||
}
|
||||
|
||||
@Override
|
||||
String getOutputFileNameForInputFileName(String inputFileName) {
|
||||
return inputFileName.replaceAll('.groovy', '.json')
|
||||
}
|
||||
|
||||
protected GroovyDsl createGroovyDSLfromStringContent(String groovyDslAsString) {
|
||||
return (GroovyDsl)new GroovyShell(this.class.classLoader).evaluate("$groovyDslAsString")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package io.codearte.accurest.wiremock
|
||||
|
||||
import io.coderate.accurest.dsl.WiremockStubStrategy
|
||||
|
||||
class DslToWiremockServerConverter extends DslToWiremockConverter {
|
||||
|
||||
@Override
|
||||
String convertContent(String dslBody) {
|
||||
return new WiremockStubStrategy(createGroovyDSLfromStringContent(dslBody)).toWiremockServerStub()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package io.codearte.accurest.wiremock
|
||||
|
||||
import groovy.io.FileType
|
||||
import groovy.util.logging.Slf4j
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
|
||||
@Slf4j
|
||||
class RecursiveFilesConverter {
|
||||
|
||||
private final SingleFileConverter singleFileConverter
|
||||
|
||||
RecursiveFilesConverter(SingleFileConverter singleFileConverter) {
|
||||
this.singleFileConverter = singleFileConverter
|
||||
}
|
||||
|
||||
void processDirectory(File sourceRootDirectory, File targetRootDirectory) {
|
||||
sourceRootDirectory.eachFileRecurse(FileType.FILES) { File sourceFile ->
|
||||
try {
|
||||
if (!singleFileConverter.canHandleFileName(sourceFile.name)) {
|
||||
return
|
||||
}
|
||||
|
||||
String convertedContent = singleFileConverter.convertContent(sourceFile.text)
|
||||
|
||||
Path relativePath = Paths.get(sourceRootDirectory.toURI()).relativize(sourceFile.parentFile.toPath())
|
||||
Path absoluteTargetPath = targetRootDirectory.toPath().resolve(relativePath)
|
||||
Files.createDirectories(absoluteTargetPath)
|
||||
|
||||
File newGroovyFile = new File(absoluteTargetPath.toFile(), singleFileConverter.getOutputFileNameForInputFileName(sourceFile.name))
|
||||
log.info("Creating new json [$newGroovyFile.path]")
|
||||
newGroovyFile.text = convertedContent
|
||||
} catch (Exception e) {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package io.codearte.accurest.wiremock
|
||||
|
||||
interface SingleFileConverter {
|
||||
|
||||
boolean canHandleFileName(String fileName)
|
||||
|
||||
String convertContent(String content)
|
||||
|
||||
String getOutputFileNameForInputFileName(String inputFileName)
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
package io.codearte.accurest.wiremock
|
||||
|
||||
import groovy.io.FileType
|
||||
import groovy.json.JsonOutput
|
||||
import groovy.json.JsonSlurper
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.codearte.accurest.wiremock
|
||||
|
||||
import groovy.json.JsonSlurper
|
||||
import spock.lang.Specification
|
||||
|
||||
class DslToWiremockClientConverterSpec extends Specification {
|
||||
|
||||
def "should convert DSL file to Wiremock JSON"() {
|
||||
given:
|
||||
def converter = new DslToWiremockClientConverter()
|
||||
and:
|
||||
String dslBody = """
|
||||
io.coderate.accurest.dsl.GroovyDsl.make {
|
||||
request {
|
||||
method('PUT')
|
||||
urlPattern \$(client('/[0-9]{2}'), server('/12'))
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
"""
|
||||
when:
|
||||
String json = converter.convertContent(dslBody)
|
||||
then:
|
||||
new JsonSlurper().parseText(json) == new JsonSlurper().parseText("""
|
||||
{"request":{"method":"PUT","urlPattern":"/[0-9]{2}"},"response":{"status":200}}""")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package io.codearte.accurest.wiremock
|
||||
|
||||
import groovy.json.JsonSlurper
|
||||
import spock.lang.Specification
|
||||
|
||||
class DslToWiremockServerConverterSpec extends Specification {
|
||||
|
||||
def "should convert DSL file to Wiremock JSON"() {
|
||||
given:
|
||||
def converter = new DslToWiremockServerConverter()
|
||||
and:
|
||||
String dslBody = """
|
||||
io.coderate.accurest.dsl.GroovyDsl.make {
|
||||
request {
|
||||
method('PUT')
|
||||
urlPattern \$(client('/[0-9]{2}'), server('/12'))
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
"""
|
||||
when:
|
||||
String json = converter.convertContent(dslBody)
|
||||
then:
|
||||
new JsonSlurper().parseText(json) == new JsonSlurper().parseText("""
|
||||
{"request":{"method":"PUT","urlPattern":"/12"},"response":{"status":200}}""")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package io.codearte.accurest.wiremock
|
||||
|
||||
import org.apache.commons.io.FileUtils
|
||||
import org.apache.commons.io.filefilter.TrueFileFilter
|
||||
import org.junit.Rule
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import spock.lang.Specification
|
||||
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
|
||||
class RecursiveFilesConverterSpec extends Specification {
|
||||
|
||||
private static final Set<String> EXPECTED_TARGET_FILES = ["dslRoot.json", "dir1/dsl1.json", "dir1/dsl1b.json", "dir2/dsl2.json"]
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder tmpFolder = new TemporaryFolder();
|
||||
|
||||
def "should recursively convert all matching files"() {
|
||||
given:
|
||||
def singleFileConverterStub = Stub(SingleFileConverter)
|
||||
singleFileConverterStub.canHandleFileName(_) >> { String fileName -> fileName.endsWith(".groovy") }
|
||||
singleFileConverterStub.convertContent(_) >> { "converted" }
|
||||
singleFileConverterStub.getOutputFileNameForInputFileName(_) >> { String inputFileName -> inputFileName.replaceAll('.groovy', '.json') }
|
||||
RecursiveFilesConverter recursiveFilesConverter = new RecursiveFilesConverter(singleFileConverterStub)
|
||||
and:
|
||||
File originalSourceRootDirectory = new File(this.getClass().getResource("/converter/source").toURI())
|
||||
File sourceRootDirectory = tmpFolder.newFolder("source")
|
||||
File targetRootDirectory = tmpFolder.newFolder("target")
|
||||
FileUtils.copyDirectory(originalSourceRootDirectory, sourceRootDirectory)
|
||||
when:
|
||||
recursiveFilesConverter.processDirectory(sourceRootDirectory, targetRootDirectory)
|
||||
then:
|
||||
Collection<File> createdFiles = FileUtils.listFiles(targetRootDirectory, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE)
|
||||
Set<String> relativizedCreatedFiles = getRelativePathsForFilesInDirectory(createdFiles, targetRootDirectory)
|
||||
EXPECTED_TARGET_FILES == relativizedCreatedFiles
|
||||
and:
|
||||
createdFiles.each { it.text == "converted" }
|
||||
}
|
||||
|
||||
private static Set<String> getRelativePathsForFilesInDirectory(Collection<File> createdFiles, File targetRootDirectory) {
|
||||
Path rootSourcePath = Paths.get(targetRootDirectory.toURI())
|
||||
Set<String> relativizedCreatedFiles = createdFiles.collect { File file ->
|
||||
rootSourcePath.relativize(Paths.get(file.toURI())).toString()
|
||||
}
|
||||
relativizedCreatedFiles
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
io.coderate.accurest.dsl.GroovyDsl.make {
|
||||
request {
|
||||
method('PUT')
|
||||
headers {
|
||||
header 'Content-Type': 'application/json'
|
||||
}
|
||||
urlPattern $(client('/[0-9]{2}'), server('/12'))
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
io.coderate.accurest.dsl.GroovyDsl.make {
|
||||
request {
|
||||
method('PUT')
|
||||
headers {
|
||||
header 'Content-Type': 'application/json'
|
||||
}
|
||||
urlPattern $(client('/[0-9]{2}'), server('/12'))
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
io.coderate.accurest.dsl.GroovyDsl.make {
|
||||
request {
|
||||
method('PUT')
|
||||
headers {
|
||||
header 'Content-Type': 'application/json'
|
||||
}
|
||||
urlPattern $(client('/[0-9]{2}'), server('/12'))
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
io.coderate.accurest.dsl.GroovyDsl.make {
|
||||
request {
|
||||
method('PUT')
|
||||
headers {
|
||||
header 'Content-Type': 'application/json'
|
||||
}
|
||||
urlPattern $(client('/[0-9]{2}'), server('/12'))
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
@@ -47,10 +47,10 @@ subprojects {
|
||||
|
||||
project(':accurest-core') {
|
||||
dependencies {
|
||||
compile 'org.slf4j:slf4j-api:[1.6.0,)'
|
||||
compile 'org.codehaus.plexus:plexus-utils:3.0.21'
|
||||
testCompile 'cglib:cglib-nodep:2.2'
|
||||
testCompile 'org.objenesis:objenesis:2.1'
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,7 @@ project(':accurest-converters') {
|
||||
dependencies {
|
||||
compile project(':accurest-core')
|
||||
compile 'org.apache.commons:commons-lang3:3.3.2'
|
||||
compile 'commons-io:commons-io:[2.4,)'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user