Added an option to use Rest Assured version 3.0

without this change it's impossible to use Rest Assured 3.0 with Spring Cloud Contract. That's because we are adding Rest Assured 2.0 imports.
with this change we detect Rest Assured version. If 3.0 is available on the classpath we change the imports

fixes #266
This commit is contained in:
Marcin Grzejszczak
2017-04-14 15:31:22 +02:00
parent 28885c1459
commit 2ce4b2e67c
12 changed files with 285 additions and 496 deletions

View File

@@ -63,6 +63,9 @@ class TestGenerator {
}
int generate() {
if (!configProperties.basePackageForTests) {
}
generateTestClasses(configProperties.basePackageForTests)
return counter.get()
}

View File

@@ -20,6 +20,8 @@ import groovy.transform.Canonical
import groovy.transform.EqualsAndHashCode
import groovy.transform.PackageScope
import groovy.util.logging.Slf4j
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.cloud.contract.spec.Contract
import org.springframework.cloud.contract.verifier.config.ContractVerifierConfigProperties
import org.springframework.cloud.contract.verifier.config.TestFramework
@@ -27,6 +29,8 @@ import org.springframework.cloud.contract.verifier.config.TestMode
import org.springframework.cloud.contract.verifier.file.ContractMetadata
import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter
import java.lang.invoke.MethodHandles
import static org.springframework.cloud.contract.verifier.util.NamesUtil.capitalize
/**
* Builds a single test for the given {@link ContractVerifierConfigProperties properties}
@@ -38,11 +42,19 @@ class SingleTestGenerator {
private static final String JSON_ASSERT_STATIC_IMPORT = 'com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson'
private static final String JSON_ASSERT_CLASS = 'com.toomuchcoding.jsonassert.JsonAssertion'
private static final String REST_ASSURED_3_0_CLASS = 'io.restassured.RestAssured'
private final ContractVerifierConfigProperties configProperties
private final ClassPresenceChecker checker
SingleTestGenerator(ContractVerifierConfigProperties configProperties) {
this.configProperties = configProperties
this.checker = new ClassPresenceChecker()
}
protected SingleTestGenerator(ContractVerifierConfigProperties configProperties, ClassPresenceChecker checker) {
this.configProperties = configProperties
this.checker = checker
}
/**
@@ -73,7 +85,8 @@ class SingleTestGenerator {
addJsonPathRelatedImports(clazz)
Map<ParsedDsl, TestType> contracts = mapContractsToTheirTestTypes(listOfFiles)
boolean restAssured3Present = this.checker.isClassPresent(REST_ASSURED_3_0_CLASS)
String restAssuredPackage = restAssured3Present ? 'io.restassured' : 'com.jayway.restassured'
boolean conditionalImportsAdded = false
boolean toIgnore = listOfFiles.ignored.find { it }
contracts.each { ParsedDsl key, TestType value ->
@@ -85,18 +98,18 @@ class SingleTestGenerator {
clazz.addImport('javax.ws.rs.core.Response')
}
} else if (configProperties.testMode == TestMode.MOCKMVC) {
clazz.addStaticImport('com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*')
clazz.addStaticImport("${restAssuredPackage}.module.mockmvc.RestAssuredMockMvc.*")
} else {
clazz.addStaticImport('com.jayway.restassured.RestAssured.*')
clazz.addStaticImport("${restAssuredPackage}.RestAssured.*")
}
}
if (configProperties.targetFramework == TestFramework.JUNIT) {
if (contracts.values().contains(TestType.HTTP) && configProperties.testMode == TestMode.MOCKMVC) {
clazz.addImport('com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification')
clazz.addImport('com.jayway.restassured.response.ResponseOptions')
clazz.addImport("${restAssuredPackage}.module.mockmvc.specification.MockMvcRequestSpecification")
clazz.addImport("${restAssuredPackage}.response.ResponseOptions")
} else if (contracts.values().contains(TestType.HTTP) && configProperties.testMode == TestMode.EXPLICIT) {
clazz.addImport('com.jayway.restassured.specification.RequestSpecification')
clazz.addImport('com.jayway.restassured.response.Response')
clazz.addImport("${restAssuredPackage}.specification.RequestSpecification")
clazz.addImport("${restAssuredPackage}.response.Response")
}
clazz.addImport('org.junit.Test')
clazz.addStaticImport('org.assertj.core.api.Assertions.assertThat')
@@ -152,7 +165,7 @@ class SingleTestGenerator {
clazz.addImport(['com.jayway.jsonpath.DocumentContext',
'com.jayway.jsonpath.JsonPath',
])
if (jsonAssertPresent()) {
if (this.checker.isClassPresent(JSON_ASSERT_CLASS)) {
clazz.addStaticImport(JSON_ASSERT_STATIC_IMPORT)
}
}
@@ -169,16 +182,21 @@ class SingleTestGenerator {
clazz.addStaticImport('org.springframework.cloud.contract.verifier.messaging.util.ContractVerifierMessagingUtil.headers')
}
private static boolean jsonAssertPresent() {
}
class ClassPresenceChecker {
private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass())
boolean isClassPresent(String className) {
try {
Class.forName(JSON_ASSERT_CLASS)
Class.forName(className)
return true
} catch (ClassNotFoundException e) {
if (log.isDebugEnabled()) {
log.debug("JsonAssert is not present on classpath. Will not add a static import.")
log.debug("[${className}] is not present on classpath. Will not add a static import.")
}
return false
}
}
}
}

View File

@@ -25,12 +25,12 @@ package org.springframework.cloud.contract.verifier.config
*/
enum TestMode {
/**
* Uses Spring's MockMvc
* Uses Spring's MockMvc with Rest Assured 2.x
*/
MOCKMVC,
/**
* Uses direct HTTP invocations
* Uses direct HTTP invocations with Rest Assured 2.x
*/
EXPLICIT,

View File

@@ -35,29 +35,51 @@ class SingleTestGeneratorSpec extends Specification {
TemporaryFolder tmpFolder = new TemporaryFolder()
File file
private static final List<String> mockMvcJUnitClassStrings = ['package test; ', 'import com.jayway.jsonpath.DocumentContext; ', 'import com.jayway.jsonpath.JsonPath; ',
'import org.junit.FixMethodOrder; ', 'import org.junit.Ignore; ', 'import org.junit.Test; ', 'import org.junit.runners.MethodSorters; ',
'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson; ', 'import static com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*; ',
'@FixMethodOrder(MethodSorters.NAME_ASCENDING); ', '@Test; ', '@Ignore; ', 'import com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification; ',
'import com.jayway.restassured.response.ResponseOptions; ', 'import static org.assertj.core.api.Assertions.assertThat']
private static final List<String> mockMvcJUnitClassStrings = ['import com.jayway.jsonpath.DocumentContext;', 'import com.jayway.jsonpath.JsonPath;',
'import org.junit.FixMethodOrder;', 'import org.junit.Ignore;', 'import org.junit.Test;', 'import org.junit.runners.MethodSorters;',
'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;', 'import static com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*;',
'@FixMethodOrder(MethodSorters.NAME_ASCENDING)', '@Test', '@Ignore', 'import com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification;',
'import com.jayway.restassured.response.ResponseOptions;', 'import static org.assertj.core.api.Assertions.assertThat']
private static final List<String> mockMvcJUnitRestAssured3ClassStrings = ['import com.jayway.jsonpath.DocumentContext;', 'import com.jayway.jsonpath.JsonPath;',
'import org.junit.FixMethodOrder;', 'import org.junit.Ignore;', 'import org.junit.Test;', 'import org.junit.runners.MethodSorters;',
'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;', 'import static io.restassured.module.mockmvc.RestAssuredMockMvc.*;',
'@FixMethodOrder(MethodSorters.NAME_ASCENDING)', '@Test', '@Ignore', 'import io.restassured.module.mockmvc.specification.MockMvcRequestSpecification;',
'import io.restassured.response.ResponseOptions;', 'import static org.assertj.core.api.Assertions.assertThat']
private static final List<String> explicitJUnitClassStrings = ['package test; ', 'import com.jayway.jsonpath.DocumentContext; ', 'import com.jayway.jsonpath.JsonPath; ',
'import org.junit.FixMethodOrder; ', 'import org.junit.Ignore; ', 'import org.junit.Test; ', 'import org.junit.runners.MethodSorters; ',
'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson; ', 'import static com.jayway.restassured.RestAssured.*; ',
'@FixMethodOrder(MethodSorters.NAME_ASCENDING); ', '@Test; ', '@Ignore; ', 'import com.jayway.restassured.specification.RequestSpecification; ',
'import com.jayway.restassured.response.Response; ', 'import static org.assertj.core.api.Assertions.assertThat']
private static final List<String> explicitJUnitClassStrings = ['import com.jayway.jsonpath.DocumentContext;', 'import com.jayway.jsonpath.JsonPath;',
'import org.junit.FixMethodOrder;', 'import org.junit.Ignore;', 'import org.junit.Test;', 'import org.junit.runners.MethodSorters;',
'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;', 'import static com.jayway.restassured.RestAssured.*;',
'@FixMethodOrder(MethodSorters.NAME_ASCENDING)', '@Test', '@Ignore', 'import com.jayway.restassured.specification.RequestSpecification;',
'import com.jayway.restassured.response.Response;', 'import static org.assertj.core.api.Assertions.assertThat']
private static final List<String> spockClassStrings = ['package test', 'import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath',
private static final List<String> explicitJUnitRestAssured3ClassStrings = ['import com.jayway.jsonpath.DocumentContext;', 'import com.jayway.jsonpath.JsonPath;',
'import org.junit.FixMethodOrder;', 'import org.junit.Ignore;', 'import org.junit.Test;', 'import org.junit.runners.MethodSorters;',
'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;', 'import static io.restassured.RestAssured.*;',
'@FixMethodOrder(MethodSorters.NAME_ASCENDING)', '@Test', '@Ignore', 'import io.restassured.specification.RequestSpecification;',
'import io.restassured.response.Response;', 'import static org.assertj.core.api.Assertions.assertThat']
private static final List<String> spockClassStrings = ['import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath',
'import spock.lang.Ignore', 'import spock.lang.Specification', 'import spock.lang.Stepwise',
'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson', 'import static com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*',
'@Stepwise', '@Ignore']
private static final List<String> explicitSpockClassStrings = ['package test', 'import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath',
private static final List<String> spockClassRestAssured3Strings = ['import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath',
'import spock.lang.Ignore', 'import spock.lang.Specification', 'import spock.lang.Stepwise',
'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson', 'import static io.restassured.module.mockmvc.RestAssuredMockMvc.*',
'@Stepwise', '@Ignore']
private static final List<String> explicitSpockClassStrings = ['import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath',
'import spock.lang.Ignore', 'import spock.lang.Specification', 'import spock.lang.Stepwise',
'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson', 'import static com.jayway.restassured.RestAssured.*',
'@Stepwise', '@Ignore']
private static final List<String> explicitSpockRestAssured3ClassStrings = ['import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath',
'import spock.lang.Ignore', 'import spock.lang.Specification', 'import spock.lang.Stepwise',
'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson', 'import static io.restassured.RestAssured.*',
'@Stepwise', '@Ignore']
public static final Closure JAVA_ASSERTER = { String classToTest ->
String name = Math.abs(new Random().nextInt())
String changedTest = classToTest.replace("public class Test", "public class Test${name}")
@@ -93,6 +115,7 @@ class SingleTestGeneratorSpec extends Specification {
given:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties()
properties.targetFramework = testFramework
properties.testMode = mode
ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2)
contract.ignored >> true
contract.order >> 2
@@ -102,7 +125,7 @@ class SingleTestGeneratorSpec extends Specification {
String clazz = testGenerator.buildClass([contract], "test", "test", 'com/foo')
then:
classStrings.each { clazz.contains(it) }
classStrings.each { assert clazz.contains(it) }
and:
asserter(clazz)
where:
@@ -113,6 +136,36 @@ class SingleTestGeneratorSpec extends Specification {
SPOCK | TestMode.EXPLICIT | explicitSpockClassStrings | GROOVY_ASSERTER
}
def "should build test class for #testFramework with Rest Assured 3.0"() {
given:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties()
properties.targetFramework = testFramework
properties.testMode = mode
ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2)
contract.ignored >> true
contract.order >> 2
SingleTestGenerator testGenerator = new SingleTestGenerator(properties, new ClassPresenceChecker() {
@Override
boolean isClassPresent(String className) {
return true
}
})
when:
String clazz = testGenerator.buildClass([contract], "test", "test", 'com/foo')
then:
classStrings.each { assert clazz.contains(it) }
!clazz.contains("com.jayway.restassured")
where:
testFramework | mode | classStrings
JUNIT | TestMode.MOCKMVC | mockMvcJUnitRestAssured3ClassStrings
JUNIT | TestMode.EXPLICIT | explicitJUnitRestAssured3ClassStrings
SPOCK | TestMode.MOCKMVC | spockClassRestAssured3Strings
SPOCK | TestMode.EXPLICIT | explicitSpockRestAssured3ClassStrings
}
def "should build test class for #testFramework and mode #mode with two files"() {
given:
File file = tmpFolder.newFile()