Provide support for scenarios. Fix #182

This commit is contained in:
Jakub Kubrynski
2016-02-07 21:35:25 +01:00
parent cf01657ec9
commit d42dc2ade4
24 changed files with 292 additions and 104 deletions

View File

@@ -2,12 +2,16 @@ package io.codearte.accurest.wiremock
import groovy.transform.CompileStatic
import io.codearte.accurest.dsl.WireMockStubStrategy
import io.codearte.accurest.file.Contract
import java.nio.charset.StandardCharsets
@CompileStatic
class DslToWireMockClientConverter extends DslToWireMockConverter {
@Override
String convertContent(String dslBody) {
return new WireMockStubStrategy(createGroovyDSLfromStringContent(dslBody)).toWireMockClientStub()
String convertContent(String rootName, Contract contract) {
String dslContent = contract.path.getText(StandardCharsets.UTF_8.toString())
return new WireMockStubStrategy(rootName, contract, createGroovyDSLfromStringContent(dslContent)).toWireMockClientStub()
}
}

View File

@@ -1,7 +1,6 @@
package io.codearte.accurest.wiremock
import com.google.common.collect.Multimap
import groovy.io.FileType
import com.google.common.collect.ListMultimap
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.codearte.accurest.config.AccurestConfigProperties
@@ -27,19 +26,21 @@ class RecursiveFilesConverter {
void processFiles() {
ContractFileScanner scanner = new ContractFileScanner(properties.contractsDslDir, properties.excludedFiles as Set, [] as Set)
Multimap<Path, Contract> contracts = scanner.findContracts()
contracts.values().each { Contract contract ->
File sourceFile = contract.path.toFile()
try {
if (!singleFileConverter.canHandleFileName(sourceFile.name)) {
return
ListMultimap<Path, Contract> contracts = scanner.findContracts()
contracts.asMap().entrySet().each { entry ->
entry.value.each { Contract contract ->
File sourceFile = contract.path.toFile()
try {
if (!singleFileConverter.canHandleFileName(sourceFile.name)) {
return
}
String convertedContent = singleFileConverter.convertContent(entry.key.last().toString(), contract)
Path absoluteTargetPath = createAndReturnTargetDirectory(sourceFile)
File newGroovyFile = createTargetFileWithProperName(absoluteTargetPath, sourceFile)
newGroovyFile.setText(convertedContent, StandardCharsets.UTF_8.toString())
} catch (Exception e) {
throw new ConversionAccurestException("Unable to make convertion of ${sourceFile.name}", e)
}
String convertedContent = singleFileConverter.convertContent(sourceFile.getText(StandardCharsets.UTF_8.toString()))
Path absoluteTargetPath = createAndReturnTargetDirectory(sourceFile)
File newGroovyFile = createTargetFileWithProperName(absoluteTargetPath, sourceFile)
newGroovyFile.setText(convertedContent, StandardCharsets.UTF_8.toString())
} catch (Exception e) {
throw new ConversionAccurestException("Unable to make convertion of ${sourceFile.name}", e)
}
}
}

View File

@@ -1,13 +1,14 @@
package io.codearte.accurest.wiremock
import groovy.transform.CompileStatic
import io.codearte.accurest.file.Contract
@CompileStatic
interface SingleFileConverter {
boolean canHandleFileName(String fileName)
String convertContent(String content)
String convertContent(String rootName, Contract content)
String generateOutputFileNameForInput(String inputFileName)
}

View File

@@ -1,15 +1,22 @@
package io.codearte.accurest.wiremock
import io.codearte.accurest.file.Contract
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import org.skyscreamer.jsonassert.JSONAssert
import spock.lang.Specification
class DslToWireMockClientConverterSpec extends Specification {
@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();
def "should convert DSL file to WireMock JSON"() {
given:
def converter = new DslToWireMockClientConverter()
and:
String dslBody = """
File file = tmpFolder.newFile("dsl1.groovy")
file.write("""
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method('PUT')
@@ -19,9 +26,9 @@ class DslToWireMockClientConverterSpec extends Specification {
status 200
}
}
"""
""")
when:
String json = converter.convertContent(dslBody)
String json = converter.convertContent("Test", new Contract(file.toPath(), false, 0, null))
then:
JSONAssert.assertEquals('''
{"request":{"method":"PUT","urlPattern":"/[0-9]{2}"},"response":{"status":200}}
@@ -33,7 +40,8 @@ class DslToWireMockClientConverterSpec extends Specification {
given:
def converter = new DslToWireMockClientConverter()
and:
String dslBody = """
File file = tmpFolder.newFile("dsl2.groovy")
file.write("""
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method 'PUT'
@@ -77,9 +85,9 @@ class DslToWireMockClientConverterSpec extends Specification {
status 200
}
}
"""
""")
when:
String json = converter.convertContent(dslBody)
String json = converter.convertContent("Test", new Contract(file.toPath(), false, 0, null))
then:
JSONAssert.assertEquals('''
{

View File

@@ -73,7 +73,7 @@ class RecursiveFilesConverterSpec extends Specification {
and:
def singleFileConverterStub = Stub(SingleFileConverter)
singleFileConverterStub.canHandleFileName(_) >> { true }
singleFileConverterStub.convertContent(_) >> { throw new NullPointerException("Test conversion error") }
singleFileConverterStub.convertContent(_, _) >> { throw new NullPointerException("Test conversion error") }
singleFileConverterStub.generateOutputFileNameForInput(_) >> { String inputFileName -> "${inputFileName}2" }
AccurestConfigProperties properties = new AccurestConfigProperties()
properties.contractsDslDir = tmpFolder.root

View File

@@ -0,0 +1,46 @@
package io.codearte.accurest.wiremock
import io.codearte.accurest.file.Contract
import spock.lang.Specification
import java.nio.file.Path
import java.nio.file.Paths
class WiremockScenarioConverterSpec extends Specification {
def "should generate first scenario step"() {
given:
DslToWireMockClientConverter converter = new DslToWireMockClientConverter()
Path dsl = Paths.get(this.getClass().getResource("/converter/scenario/main_scenario/01_login.groovy").toURI())
when:
String content = converter.convertContent("Test", new Contract(dsl, false, 3, 0))
then:
content.contains('"requiredScenarioState" : "Started"')
content.contains('"newScenarioState" : "Step1"')
content.contains('"scenarioName" : "Scenario_Test"')
}
def "should generate mid scenario step"() {
given:
DslToWireMockClientConverter converter = new DslToWireMockClientConverter()
Path dsl = Paths.get(this.getClass().getResource("/converter/scenario/main_scenario/02_showCart.groovy").toURI())
when:
String content = converter.convertContent("Test", new Contract(dsl, false, 3, 1))
then:
content.contains('"requiredScenarioState" : "Step1"')
content.contains('"newScenarioState" : "Step2"')
content.contains('"scenarioName" : "Scenario_Test"')
}
def "should generate last scenario step"() {
given:
DslToWireMockClientConverter converter = new DslToWireMockClientConverter()
Path dsl = Paths.get(this.getClass().getResource("/converter/scenario/main_scenario/03_logout.groovy").toURI())
when:
String content = converter.convertContent("Test", new Contract(dsl, false, 3, 2))
then:
content.contains('"requiredScenarioState" : "Step2"')
!content.contains('"newScenarioState"')
content.contains('"scenarioName" : "Scenario_Test"')
}
}

View File

@@ -0,0 +1,9 @@
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method('POST')
url '/login'
}
response {
status 200
}
}

View File

@@ -0,0 +1,9 @@
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method('GET')
url '/cart'
}
response {
status 200
}
}

View File

@@ -0,0 +1,9 @@
io.codearte.accurest.dsl.GroovyDsl.make {
request {
method('POST')
url '/logout'
}
response {
status 200
}
}

View File

@@ -21,8 +21,7 @@ class SingleTestGenerator {
@PackageScope
String buildClass(Collection<Contract> listOfFiles, String className, String classPackage) {
ClassBuilder clazz = createClass(capitalize(className), classPackage,
configProperties)
ClassBuilder clazz = createClass(capitalize(className), classPackage, configProperties)
if (configProperties.imports) {
configProperties.imports.each {
@@ -30,8 +29,8 @@ class SingleTestGenerator {
}
}
if (listOfFiles.ignored.find {it}) {
clazz.addImport("org.junit.Ignore")
if (listOfFiles.ignored.find { it }) {
clazz.addImport(configProperties.targetFramework.getIgnoreClass())
}
if (configProperties.staticImports) {
@@ -40,6 +39,11 @@ class SingleTestGenerator {
}
}
if (isScenarioClass(listOfFiles)) {
clazz.addImport(configProperties.targetFramework.getOrderClass())
clazz.addClassLevelAnnotation(configProperties.targetFramework.getOrderAnnotation())
}
if (configProperties.testMode == TestMode.JAXRSCLIENT) {
clazz.addStaticImport('javax.ws.rs.client.Entity.*')
} else if (configProperties.testMode == TestMode.MOCKMVC) {
@@ -66,10 +70,14 @@ class SingleTestGenerator {
return clazz.build()
}
private boolean isScenarioClass(Collection<Contract> listOfFiles) {
listOfFiles.find({ it.order != null }) != null
}
private ClassBuilder addJsonPathRelatedImports(ClassBuilder clazz) {
clazz.addImport(['com.jayway.jsonpath.DocumentContext',
'com.jayway.jsonpath.JsonPath',
'net.minidev.json.JSONArray'])
'com.jayway.jsonpath.JsonPath',
'net.minidev.json.JSONArray'])
}
}

View File

@@ -1,6 +1,6 @@
package io.codearte.accurest
import com.google.common.collect.Multimap
import com.google.common.collect.ListMultimap
import groovy.transform.PackageScope
import io.codearte.accurest.config.AccurestConfigProperties
import io.codearte.accurest.file.Contract
@@ -53,7 +53,7 @@ class TestGenerator {
@PackageScope
void generateTestClasses(final String basePackageName) {
Multimap<Path, Contract> contracts = contractFileScanner.findContracts()
ListMultimap<Path, Contract> contracts = contractFileScanner.findContracts()
contracts.asMap().entrySet().each {
Map.Entry<Path, Collection<Contract>> entry -> processIncludedDirectory(relativizeContractPath(entry), entry.getValue(), basePackageName)
}

View File

@@ -16,6 +16,7 @@ class ClassBuilder {
private final List<String> staticImports = []
private final List<String> rules = []
private final List<MethodBuilder> methods = []
private final List<String> classLevelAnnotations = []
private final TestFramework lang
private ClassBuilder(String className, String packageName, String baseClass, TestFramework lang) {
@@ -83,6 +84,10 @@ class ClassBuilder {
clazz.addEmptyLine()
}
classLevelAnnotations.sort().each {
clazz.addLine(it)
}
def classLine = "${lang.classModifier}class $className"
if (baseClass) {
classLine += " extends $baseClass"
@@ -107,4 +112,8 @@ class ClassBuilder {
clazz.addLine('}')
clazz.toString()
}
void addClassLevelAnnotation(String annotation) {
classLevelAnnotations << annotation
}
}

View File

@@ -4,21 +4,28 @@ package io.codearte.accurest.config
* @author Jakub Kubrynski
*/
enum TestFramework {
JUNIT("public ", "public void ", ";", ".java", "Test"),
SPOCK("", "def ", "", ".groovy", "Spec")
JUNIT("public ", "public void ", ";", ".java", "Test", "org.junit.Ignore", "org.junit.FixMethodOrder", "@FixMethodOrder(MethodSorters.NAME_ASCENDING)"),
SPOCK("", "def ", "", ".groovy", "Spec", "spock.lang.Ignore", "spock.lang.Stepwise", "@Stepwise")
private final String classModifier
private final String methodModifier
private final String lineSuffix
private final String classExtension;
private final String classNameSuffix;
private final String ignoreClass
private final String orderAnnotationImport
private final String orderAnnotation
TestFramework(String classModifier, String methodModifier, String lineSuffix, String classExtension, String classNameSuffix) {
TestFramework(String classModifier, String methodModifier, String lineSuffix, String classExtension, String classNameSuffix,
String ignoreClass, String orderAnnotationImport, String orderAnnotation) {
this.classModifier = classModifier
this.lineSuffix = lineSuffix
this.methodModifier = methodModifier
this.classExtension = classExtension
this.classNameSuffix = classNameSuffix
this.ignoreClass = ignoreClass
this.orderAnnotationImport = orderAnnotationImport
this.orderAnnotation = orderAnnotation
}
String getClassModifier() {
@@ -40,4 +47,16 @@ enum TestFramework {
String getClassNameSuffix() {
return classNameSuffix
}
String getIgnoreClass() {
return ignoreClass
}
String getOrderClass() {
return orderClass
}
String getOrderAnnotation() {
return orderAnnotation
}
}

View File

@@ -5,15 +5,22 @@ import com.github.tomakehurst.wiremock.matching.RequestPattern
import com.github.tomakehurst.wiremock.stubbing.StubMapping
import groovy.transform.CompileDynamic
import groovy.transform.CompileStatic
import io.codearte.accurest.file.Contract
@CompileStatic
class WireMockStubStrategy {
private static final String STEP_START = "Started"
private static final String STEP_PREFIX = "Step"
private final WireMockRequestStubStrategy wireMockRequestStubStrategy
private final WireMockResponseStubStrategy wireMockResponseStubStrategy
private final Integer priority
private final Contract contract
private final String rootName
WireMockStubStrategy(GroovyDsl groovyDsl) {
WireMockStubStrategy(String rootName, Contract contract, GroovyDsl groovyDsl) {
this.rootName = rootName
this.contract = contract
this.wireMockRequestStubStrategy = new WireMockRequestStubStrategy(groovyDsl)
this.wireMockResponseStubStrategy = new WireMockResponseStubStrategy(groovyDsl)
this.priority = groovyDsl.priority
@@ -29,6 +36,15 @@ class WireMockStubStrategy {
}
stubMapping.request = request
stubMapping.response = response
if (contract.order != null) {
stubMapping.scenarioName = "Scenario_" + rootName
stubMapping.requiredScenarioState = contract.order == 0 ? STEP_START : STEP_PREFIX + contract.order
if (contract.order < contract.groupSize - 1) {
stubMapping.newScenarioState = STEP_PREFIX + (contract.order + 1)
}
}
return StubMapping.buildJsonStringFor(stubMapping)
}
}

View File

@@ -8,10 +8,23 @@ import java.nio.file.Path
class Contract {
final Path path;
final boolean ignored;
final int groupSize
final Integer order;
Contract(Path path, boolean ignored) {
Contract(Path path, boolean ignored, int groupSize, Integer order) {
this.groupSize = groupSize
this.path = path
this.ignored = ignored
this.order = order
}
@Override
public String toString() {
return "Contract{" +
"fileName=" + path.fileName +
", ignored=" + ignored +
", groupSize=" + groupSize +
", order=" + order +
'}';
}
}

View File

@@ -1,6 +1,7 @@
package io.codearte.accurest.file
import com.google.common.collect.ArrayListMultimap
import com.google.common.collect.ListMultimap
import com.google.common.collect.Multimap
import org.apache.commons.io.FilenameUtils
@@ -8,13 +9,15 @@ import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Path
import java.nio.file.PathMatcher
import java.util.regex.Pattern
/**
* @author Jakub Kubrynski
*/
class ContractFileScanner {
private final String MATCH_PREFIX = "glob:"
private static final String MATCH_PREFIX = "glob:"
private static final Pattern SCENARIO_STEP_FILENAME_PATTERN = Pattern.compile("[0-9]+_.*")
private final File baseDir
private final Set<PathMatcher> excludeMatchers
private final Set<PathMatcher> ignoreMatchers
@@ -32,26 +35,37 @@ class ContractFileScanner {
}) as Set
}
Multimap<Path, Contract> findContracts() {
Multimap<Path, Contract> result = ArrayListMultimap.create()
ListMultimap<Path, Contract> findContracts() {
ListMultimap<Path, Contract> result = ArrayListMultimap.create()
appendRecursively(baseDir, result)
return result
}
private void appendRecursively(File baseDir, Multimap<Path, Contract> result) {
for (File file : baseDir.listFiles()) {
if (matchesPattern(file, excludeMatchers)) {
break;
}
if (isContractFile(file)) {
Path path = file.toPath()
result.put(file.parentFile.toPath(), new Contract(path, matchesPattern(file, ignoreMatchers)))
} else {
appendRecursively(file, result)
private void appendRecursively(File baseDir, ListMultimap<Path, Contract> result) {
File[] files = baseDir.listFiles()
if (!files) {
return;
}
files.sort().eachWithIndex { File file, int index ->
if (!matchesPattern(file, excludeMatchers)) {
if (isContractFile(file)) {
Path path = file.toPath()
Integer order = null
if (hasScenarioFilenamePattern(path)) {
order = index
}
result.put(file.parentFile.toPath(), new Contract(path, matchesPattern(file, ignoreMatchers), files.size(), order))
} else {
appendRecursively(file, result)
}
}
}
}
private boolean hasScenarioFilenamePattern(Path path) {
SCENARIO_STEP_FILENAME_PATTERN.matcher(path.fileName.toString()).matches()
}
boolean matchesPattern(File file, Set<PathMatcher> excludeMatchers) {
for (PathMatcher matcher : excludeMatchers) {
if (matcher.matches(file.toPath())) {

View File

@@ -3,6 +3,7 @@ package io.codearte.accurest.builder
import io.codearte.accurest.dsl.GroovyDsl
import io.codearte.accurest.dsl.WireMockStubStrategy
import io.codearte.accurest.dsl.WireMockStubVerifier
import io.codearte.accurest.file.Contract
import spock.lang.Issue
import spock.lang.Specification
@@ -31,7 +32,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
blockBuilder.toString().contains("\$[?(@.property1 == 'a')]")
blockBuilder.toString().contains("\$[?(@.property2 == 'b')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue("#187")
@@ -60,7 +61,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
blockBuilder.toString().contains("\$[?(@.property2 == null)]")
blockBuilder.toString().contains("\$[?(@.property3 == false)]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue("#79")
@@ -91,7 +92,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
blockBuilder.toString().contains("\$.property2[*][?(@.a == 'sth')]")
blockBuilder.toString().contains("\$.property2[*][?(@.b == 'sthElse')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue("#82")
@@ -116,7 +117,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
then:
blockBuilder.toString().contains("entity('{\"items\":[\"HOP\"]}', 'application/json')")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue("#88")
@@ -141,7 +142,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
then:
blockBuilder.toString().contains("entity('property1=VAL1', 'application/octet-stream')")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate assertions for array in response body"() {
@@ -170,7 +171,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
blockBuilder.toString().contains("\$[*][?(@.property1 == 'a')]")
blockBuilder.toString().contains("\$[*][?(@.property2 == 'b')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate assertions for array inside response body element"() {
@@ -198,7 +199,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
blockBuilder.toString().contains("\$.property1[*][?(@.property3 == 'test2')]")
blockBuilder.toString().contains("\$.property1[*][?(@.property2 == 'test1')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate assertions for nested objects in response body"() {
@@ -226,7 +227,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
blockBuilder.toString().contains("\$.property2[?(@.property3 == 'b')]")
blockBuilder.toString().contains("\$[?(@.property1 == 'a')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate regex assertions for map objects in response body"() {
@@ -260,7 +261,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
blockBuilder.toString().contains("\$[?(@.property2 =~ /[0-9]{3}/)]")
blockBuilder.toString().contains("\$[?(@.property1 == 'a')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate regex assertions for string objects in response body"() {
@@ -288,7 +289,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
blockBuilder.toString().contains("\$[?(@.property2 =~ /[0-9]{3}/)]")
blockBuilder.toString().contains("\$[?(@.property1 == 'a')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should ignore 'Accept' header and use 'request' method"() {
@@ -312,7 +313,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
then:
blockBuilder.toString().contains("request('text/plain')")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should ignore 'Content-Type' header and use 'entity' method"() {
@@ -340,7 +341,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
blockBuilder.toString().contains("header('Timer', '123')")
!blockBuilder.toString().contains("header('Content-Type'")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate a call with an url path and query parameters"() {
@@ -390,7 +391,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
spockTest.contains('$[?(@.property2 == \'b\')]')
spockTest.contains('$[?(@.property1 == \'a\')]')
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue('#169')
@@ -441,7 +442,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
spockTest.contains('$[?(@.property2 == \'b\')]')
spockTest.contains('$[?(@.property1 == \'a\')]')
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate test for empty body"() {
@@ -464,7 +465,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
then:
spockTest.contains("entity('', 'application/octet-stream')")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate test for String in response body"() {
@@ -488,7 +489,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
spockTest.contains('String responseAsString = response.readEntity(String)')
spockTest.contains('responseBody == "test"')
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue('#171')
@@ -517,7 +518,7 @@ class JaxRsClientSpockMethodBuilderSpec extends Specification implements WireMoc
then:
spockTest.contains(".method('GET')")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
}

View File

@@ -3,6 +3,7 @@ package io.codearte.accurest.builder
import io.codearte.accurest.dsl.GroovyDsl
import io.codearte.accurest.dsl.WireMockStubStrategy
import io.codearte.accurest.dsl.WireMockStubVerifier
import io.codearte.accurest.file.Contract
import spock.lang.Issue
import spock.lang.Specification
import spock.lang.Unroll
@@ -37,7 +38,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
blockBuilder.toString().contains("\$[?(@.property1 == 'a')]")
blockBuilder.toString().contains("\$[?(@.property2 == 'b')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue("#187")
@@ -66,7 +67,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
blockBuilder.toString().contains("\$[?(@.property2 == null)]")
blockBuilder.toString().contains("\$[?(@.property3 == false)]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue("#79")
@@ -97,7 +98,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
blockBuilder.toString().contains("\$.property2[*][?(@.a == 'sth')]")
blockBuilder.toString().contains("\$.property2[*][?(@.b == 'sthElse')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue("#82")
@@ -122,7 +123,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
then:
blockBuilder.toString().contains(".body('''{\"items\":[\"HOP\"]}''')")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue("#88")
@@ -147,7 +148,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
then:
blockBuilder.toString().contains(".body('''property1=VAL1''')")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue("185")
@@ -176,7 +177,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
blockBuilder.toString().contains("\$.property[?(@.7 == 0.0)]")
blockBuilder.toString().contains("\$.property[?(@.14 == 0.0)]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate assertions for array in response body"() {
@@ -205,7 +206,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
blockBuilder.toString().contains("\$[*][?(@.property1 == 'a')]")
blockBuilder.toString().contains("\$[*][?(@.property2 == 'b')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate assertions for array inside response body element"() {
@@ -233,7 +234,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
blockBuilder.toString().contains("\$.property1[*][?(@.property3 == 'test2')]")
blockBuilder.toString().contains("\$.property1[*][?(@.property2 == 'test1')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate assertions for nested objects in response body"() {
@@ -261,7 +262,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
blockBuilder.toString().contains("\$.property2[?(@.property3 == 'b')]")
blockBuilder.toString().contains("\$[?(@.property1 == 'a')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate regex assertions for map objects in response body"() {
@@ -295,7 +296,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
blockBuilder.toString().contains("\$[?(@.property2 =~ /[0-9]{3}/)]")
blockBuilder.toString().contains("\$[?(@.property1 == 'a')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate regex assertions for string objects in response body"() {
@@ -323,7 +324,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
blockBuilder.toString().contains("\$[?(@.property2 =~ /[0-9]{3}/)]")
blockBuilder.toString().contains("\$[?(@.property1 == 'a')]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue(["#126", "#143"])
@@ -349,7 +350,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
then:
blockBuilder.toString().contains("\$[?(@.property =~ /\\d+/)]")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate a call with an url path and query parameters"() {
@@ -392,7 +393,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
spockTest.contains('$[?(@.property2 == \'b\')]')
spockTest.contains('$[?(@.property1 == \'a\')]')
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue('#169')
@@ -436,7 +437,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
spockTest.contains('$[?(@.property2 == \'b\')]')
spockTest.contains('$[?(@.property1 == \'a\')]')
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate test for empty body"() {
@@ -459,7 +460,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
then:
spockTest.contains(".body('''''')")
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should generate test for String in response body"() {
@@ -483,7 +484,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
spockTest.contains('def responseBody = (response.body.asString())')
spockTest.contains('responseBody == "test"')
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue('113')
@@ -518,7 +519,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
then:
spockTest.contains('''response.header('Location') ==~ java.util.regex.Pattern.compile('http://localhost/partners/[0-9]+/users/[0-9]+')''')
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
@Issue('115')
@@ -553,7 +554,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
then:
spockTest.contains('''response.header('Location') ==~ java.util.regex.Pattern.compile('^((http[s]?|ftp):\\/)\\/?([^:\\/\\s]+)(:[0-9]{1,5})?/partners/[0-9]+/users/[0-9]+')''')
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should work with more complex stuff and jsonpaths"() {
@@ -590,7 +591,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
spockTest.contains('''$.errors[*][?(@.property == 'bank_account_number')]''')
spockTest.contains('''$.errors[*][?(@.message == 'incorrect_format')]''')
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should work properly with GString url"() {
@@ -619,7 +620,7 @@ class MockMvcSpockMethodBuilderSpec extends Specification implements WireMockStu
then:
spockTest.contains('''/partners/11/agents/11/customers/09665703Z''')
and:
stubMappingIsValidWireMockStub(new WireMockStubStrategy(contractDsl).toWireMockClientStub())
stubMappingIsValidWireMockStub(new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub())
}
def "should resolve properties in GString with regular expression"() {

View File

@@ -2,6 +2,7 @@ package io.codearte.accurest.dsl
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import io.codearte.accurest.file.Contract
import io.codearte.accurest.util.AssertionUtil
import spock.lang.Issue
import spock.lang.Specification
@@ -36,7 +37,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual('''
{
@@ -81,7 +82,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual('''
{
@@ -129,7 +130,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual('''
{
@@ -181,7 +182,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
@@ -228,7 +229,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual('''
{
@@ -276,7 +277,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
@@ -560,7 +561,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
@@ -615,7 +616,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
@@ -963,7 +964,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
@@ -1136,7 +1137,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
@@ -1170,7 +1171,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
@@ -1218,7 +1219,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
@@ -1269,7 +1270,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
@@ -1298,7 +1299,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
@Unroll
def 'should generate stub without optional parameters'() {
when:
String wireMockStub = new WireMockStubStrategy(contractDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub()
then:
AssertionUtil.assertThatJsonsAreEqual(('''
{
@@ -1396,7 +1397,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
String toWireMockClientJsonStub(groovyDsl) {
new WireMockStubStrategy(groovyDsl).toWireMockClientStub()
new WireMockStubStrategy("Test", new Contract(null, false, 0, null), groovyDsl).toWireMockClientStub()
}
@Issue('180')
@@ -1420,7 +1421,7 @@ class WireMockGroovyDslSpec extends Specification implements WireMockStubVerifie
}
}
when:
String wireMockStub = new WireMockStubStrategy(contractDsl).toWireMockClientStub()
String wireMockStub = new WireMockStubStrategy("Test", new Contract(null, false, 0, null), contractDsl).toWireMockClientStub()
then:
println wireMockStub
AssertionUtil.assertThatJsonsAreEqual(('''

View File

@@ -1,4 +1,5 @@
package io.codearte.accurest.dsl
import com.github.tomakehurst.wiremock.stubbing.StubMapping
import java.util.regex.Pattern

View File

@@ -1,5 +1,6 @@
package io.codearte.accurest.file
import com.google.common.collect.ListMultimap
import com.google.common.collect.Multimap
import spock.lang.Specification
@@ -17,7 +18,7 @@ class ContractFileScannerSpec extends Specification {
Set<String> ignored = ["other/different/**"] as Set
ContractFileScanner scanner = new ContractFileScanner(baseDir, excluded, ignored)
when:
Multimap<Path, Contract> result = scanner.findContracts()
ListMultimap<Path, Contract> result = scanner.findContracts()
then:
result.keySet().size() == 3
result.get(baseDir.toPath().resolve("different")).size() == 1
@@ -27,4 +28,18 @@ class ContractFileScannerSpec extends Specification {
ignoredSet.size() == 1
ignoredSet.ignored == [true]
}
def "should find contracts group in scenario"() {
given:
File baseDir = new File(this.getClass().getResource("/directory/with/scenario").toURI())
ContractFileScanner scanner = new ContractFileScanner(baseDir, [] as Set, [] as Set)
when:
ListMultimap<Path, Contract> contracts = scanner.findContracts()
then:
contracts.values().size() == 3
contracts.values().find { it.path.fileName.toString().startsWith('01') }.groupSize == 3
contracts.values().find { it.path.fileName.toString().startsWith('01') }.order == 0
contracts.values().find { it.path.fileName.toString().startsWith('02') }.order == 1
contracts.values().find { it.path.fileName.toString().startsWith('03') }.order == 2
}
}

View File

@@ -0,0 +1 @@
package directory.with.scenario

View File

@@ -0,0 +1 @@
package directory.with.scenario

View File

@@ -0,0 +1 @@
package directory.with.scenario