Merge branch 'master' into feature/kotlin-contract-support

This commit is contained in:
Tim Ysewyn
2019-08-13 15:45:51 +02:00
5 changed files with 103 additions and 9 deletions

View File

@@ -34,12 +34,12 @@ interface QueryParamsResolver {
return resolveParamValue(((OptionalProperty) value).optionalPattern());
}
else if (value instanceof MatchingStrategy) {
return ((MatchingStrategy) value).getServerValue().toString();
return resolveParamValue(((MatchingStrategy) value).getServerValue());
}
else if (value instanceof DslProperty) {
return resolveParamValue(((DslProperty) value).getServerValue());
}
return value.toString();
return value == null ? "null" : value.toString();
}
}

View File

@@ -128,16 +128,19 @@ class ContractVerifierDslConverter implements ContractConverter<Collection<Contr
private static Object toObject(ClassLoader cl, File rootFolder, File dsl) {
if (isJava(dsl)) {
try {
return parseJavaFile(dsl)
return parseJavaFile(cl, dsl)
}
catch (Exception ex) {
throw new DslParseException("Exception occurred while trying to parse the file [" + dsl + "] as a contract. Will not parse it.", ex)
if (log.isWarnEnabled()) {
log.warn("Exception occurred while trying to parse the file [" + dsl + "] as a contract. Will not parse it.", ex)
}
return null
}
}
return groovyShell(cl, rootFolder).evaluate(dsl)
}
private static Object parseJavaFile(File dsl) {
private static Object parseJavaFile(ClassLoader cl, File dsl) {
Constructor<?> constructor = classConstructor(dsl)
Object newInstance = constructor.newInstance()
if (!newInstance instanceof Supplier) {
@@ -158,7 +161,7 @@ class ContractVerifierDslConverter implements ContractConverter<Collection<Contr
if (!compilationResult.wasSuccessful()) {
throw new IllegalStateException("Exceptions occurred while trying to compile the file " + compilationResult.compilationMessages)
}
Class<?> clazz = compilationResult.compiledClasses.find { it.name == fqn }
Class<?> clazz = compilationResult.compiledClasses.find { it.name == fqn}
if (clazz == null) {
throw new IllegalStateException("Class with name [" + fqn + "] not found")
}

View File

@@ -0,0 +1,84 @@
package org.springframework.cloud.contract.verifier.builder
import org.springframework.cloud.contract.spec.internal.DslProperty
import org.springframework.cloud.contract.spec.internal.MatchingStrategy
import org.springframework.cloud.contract.spec.internal.OptionalProperty
import org.springframework.cloud.contract.spec.internal.QueryParameter
import org.springframework.cloud.contract.spec.internal.RegexProperty
import spock.lang.Specification
import java.util.regex.Pattern
class QueryParamsResolverSpec extends Specification {
def 'should return serverValue for QueryParameter'() {
given:
Object parameter = new QueryParameter("some_param", dslProperty)
when:
String resolved = new QueryParamsResolver() {}.resolveParamValue(parameter)
then:
resolved == expected
where:
dslProperty | expected
new DslProperty<String>("client", "server") | "server"
new DslProperty<String>(null, "server") | "server"
new DslProperty<String>("client", null) | "null"
new DslProperty<String>(null, null) | "null"
}
def 'should return optionalPattern for OptionalProperty'() {
given:
Object parameter = new OptionalProperty(value)
when:
String resolved = new QueryParamsResolver() {}.resolveParamValue(parameter)
then:
resolved == expected
where:
value | expected
"server" | "(server)?"
null | "()?"
new RegexProperty(Pattern.compile(".*")) | "(.*)?"
new RegexProperty(null, Pattern.compile(".*")) | "(.*)?"
new RegexProperty(Pattern.compile(".*"), null) | "(.*)?"
new RegexProperty("", Pattern.compile(".*")) | "(.*)?"
new RegexProperty(Pattern.compile(".*"), "") | "(.*)?"
}
def 'should return serverValue for MatchingStrategy'() {
given:
Object parameter = new MatchingStrategy(value, MatchingStrategy.Type.EQUAL_TO)
when:
String resolved = new QueryParamsResolver() {}.resolveParamValue(parameter)
then:
resolved == expected
where:
value | expected
"server" | "server"
null | "null"
new DslProperty<String>("client", "server") | "server"
new DslProperty<String>(null, "server") | "server"
new DslProperty<String>("client", null) | "null"
new DslProperty<String>(null, null) | "null"
}
def 'should return serverValue for DslProperty'() {
when:
String resolved = new QueryParamsResolver() {}.resolveParamValue(parameter)
then:
resolved == expected
where:
parameter | expected
new DslProperty<String>("client", "server") | "server"
new DslProperty<String>(null, "server") | "server"
new DslProperty<String>("client", null) | "null"
new DslProperty<String>(null, null) | "null"
}
def 'should return \"null\" for null'() {
when:
String resolved = new QueryParamsResolver() {}.resolveParamValue(null)
then:
resolved == "null"
}
}