Fixed equals and hashcode
This commit is contained in:
@@ -113,16 +113,17 @@ class JavaTestGenerator implements SingleTestGenerator {
|
||||
log.debug("Stub content from file [${stubsFile.text}]")
|
||||
}
|
||||
List<Contract> stubContents = metadata.convertedContract
|
||||
dsls << stubContents.collectEntries { Contract stubContent ->
|
||||
Map<ParsedDsl, TestType> entries = stubContents.collectEntries { Contract stubContent ->
|
||||
TestType testType = (stubContent.input || stubContent.outputMessage) ? TestType.MESSAGING : TestType.HTTP
|
||||
return [(new ParsedDsl(metadata, stubContent, stubsFile)): testType]
|
||||
}
|
||||
dsls.putAll(entries)
|
||||
}
|
||||
return dsls
|
||||
}
|
||||
|
||||
@Canonical
|
||||
@EqualsAndHashCode
|
||||
@EqualsAndHashCode(includeFields = true)
|
||||
private static class ParsedDsl {
|
||||
ContractMetadata contract
|
||||
Contract groovyDsl
|
||||
|
||||
@@ -57,10 +57,24 @@ class MethodBuilder {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Stub content Groovy DSL [$stubContent]")
|
||||
}
|
||||
String methodName = NamesUtil.camelCase(NamesUtil.toLastDot(NamesUtil.afterLast(stubsFile.path, File.separator)))
|
||||
String methodName = methodName(contract, stubsFile, stubContent)
|
||||
return new MethodBuilder(methodName, stubContent, configProperties, contract.ignored || stubContent.ignored)
|
||||
}
|
||||
|
||||
private static String methodName(ContractMetadata contract, File stubsFile, Contract stubContent) {
|
||||
if (stubContent.name) {
|
||||
return NamesUtil.camelCase(stubContent.name)
|
||||
} else if (contract.convertedContract.size() > 1) {
|
||||
int index = contract.convertedContract.findIndexOf { it == stubContent}
|
||||
return "${camelCasedMethodFromFileName(stubsFile)}_${index}"
|
||||
}
|
||||
return camelCasedMethodFromFileName(stubsFile)
|
||||
}
|
||||
|
||||
private static String camelCasedMethodFromFileName(File stubsFile) {
|
||||
return NamesUtil.camelCase(NamesUtil.toLastDot(NamesUtil.afterLast(stubsFile.path, File.separator)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends to the {@link BlockBuilder} the contents of the test
|
||||
*/
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.cloud.contract.verifier.file
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
import groovy.transform.EqualsAndHashCode
|
||||
import groovy.transform.ToString
|
||||
import org.springframework.cloud.contract.spec.Contract
|
||||
import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter
|
||||
@@ -30,6 +31,7 @@ import java.nio.file.Path
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@CompileStatic
|
||||
@EqualsAndHashCode
|
||||
@ToString
|
||||
class ContractMetadata {
|
||||
/**
|
||||
|
||||
@@ -80,7 +80,7 @@ class ContractVerifierDslConverter {
|
||||
return new GroovyShell(ContractVerifierDslConverter.classLoader, new Binding(), new CompilerConfiguration(sourceEncoding: 'UTF-8'))
|
||||
}
|
||||
|
||||
private static List<Contract> listOfContracts(object) {
|
||||
private static Collection<Contract> listOfContracts(object) {
|
||||
if (object instanceof Collection) {
|
||||
return object as Collection<Contract>
|
||||
} else if (!object instanceof Contract) {
|
||||
|
||||
@@ -106,6 +106,6 @@ class NamesUtil {
|
||||
* Converts illegal package characters to underscores
|
||||
*/
|
||||
static String convertIllegalPackageChars(String packageName) {
|
||||
return packageName.replace('-', '_')
|
||||
return packageName.replace('-', '_').replace(" ", "_")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ class MockMvcMethodBodyBuilderSpec extends Specification implements WireMockStub
|
||||
Contract dslWithOptionalsInString = Contract.make {
|
||||
priority 1
|
||||
request {
|
||||
method 'POST'
|
||||
method POST()
|
||||
url '/users/password'
|
||||
headers {
|
||||
contentType(applicationJson())
|
||||
@@ -65,7 +65,7 @@ class MockMvcMethodBodyBuilderSpec extends Specification implements WireMockStub
|
||||
Contract dslWithOptionals = Contract.make {
|
||||
priority 1
|
||||
request {
|
||||
method 'POST'
|
||||
method POST()
|
||||
url '/users/password'
|
||||
headers {
|
||||
contentType(applicationJson())
|
||||
|
||||
@@ -26,7 +26,7 @@ import spock.lang.Specification
|
||||
|
||||
import static org.springframework.cloud.contract.verifier.config.TestFramework.JUNIT
|
||||
import static org.springframework.cloud.contract.verifier.config.TestFramework.SPOCK
|
||||
import static org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter.convert
|
||||
import static org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter.convertAsCollection
|
||||
|
||||
class SingleTestGeneratorSpec extends Specification {
|
||||
|
||||
@@ -64,7 +64,7 @@ class SingleTestGeneratorSpec extends Specification {
|
||||
given:
|
||||
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties();
|
||||
properties.targetFramework = testFramework
|
||||
ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2, convert(file))
|
||||
ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2, convertAsCollection(file))
|
||||
contract.ignored >> true
|
||||
contract.order >> 2
|
||||
JavaTestGenerator testGenerator = new JavaTestGenerator()
|
||||
@@ -86,7 +86,7 @@ class SingleTestGeneratorSpec extends Specification {
|
||||
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties();
|
||||
properties.testMode = TestMode.JAXRSCLIENT
|
||||
properties.targetFramework = testFramework
|
||||
ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2, convert(file))
|
||||
ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2, convertAsCollection(file))
|
||||
contract.ignored >> true
|
||||
contract.order >> 2
|
||||
JavaTestGenerator testGenerator = new JavaTestGenerator()
|
||||
@@ -124,11 +124,11 @@ class SingleTestGeneratorSpec extends Specification {
|
||||
and:
|
||||
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties();
|
||||
properties.targetFramework = testFramework
|
||||
ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2, convert(file))
|
||||
ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2, convertAsCollection(file))
|
||||
contract.ignored >> true
|
||||
contract.order >> 2
|
||||
and:
|
||||
ContractMetadata contract2 = new ContractMetadata(secondFile.toPath(), true, 1, 2, convert(secondFile))
|
||||
ContractMetadata contract2 = new ContractMetadata(secondFile.toPath(), true, 1, 2, convertAsCollection(secondFile))
|
||||
contract2.ignored >> true
|
||||
contract2.order >> 2
|
||||
and:
|
||||
@@ -167,7 +167,7 @@ class SingleTestGeneratorSpec extends Specification {
|
||||
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties();
|
||||
properties.targetFramework = testFramework
|
||||
and:
|
||||
ContractMetadata contract2 = new ContractMetadata(secondFile.toPath(), true, 1, 2, convert(file))
|
||||
ContractMetadata contract2 = new ContractMetadata(secondFile.toPath(), true, 1, 2, convertAsCollection(file))
|
||||
contract2.ignored >> false
|
||||
contract2.order >> 2
|
||||
and:
|
||||
@@ -186,5 +186,98 @@ class SingleTestGeneratorSpec extends Specification {
|
||||
SPOCK | spockClassStrings
|
||||
}
|
||||
|
||||
def "should pick the contract's name as the test method"() {
|
||||
given:
|
||||
File secondFile = tmpFolder.newFile()
|
||||
secondFile.write("""
|
||||
org.springframework.cloud.contract.spec.Contract.make {
|
||||
name("MySuperMethod")
|
||||
request {
|
||||
method 'PUT'
|
||||
url 'url'
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
""")
|
||||
and:
|
||||
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties();
|
||||
properties.targetFramework = testFramework
|
||||
ContractMetadata contract = new ContractMetadata(secondFile.toPath(), false, 1, null, convertAsCollection(secondFile))
|
||||
JavaTestGenerator testGenerator = new JavaTestGenerator()
|
||||
when:
|
||||
String clazz = testGenerator.buildClass(properties, [contract], "test", "test", 'com/foo')
|
||||
then:
|
||||
clazz.contains("validate_mySuperMethod()")
|
||||
where:
|
||||
testFramework << [JUNIT, SPOCK]
|
||||
}
|
||||
|
||||
def "should pick the contract's name as the test method when there are multiple contracts"() {
|
||||
given:
|
||||
File secondFile = tmpFolder.newFile()
|
||||
secondFile.write('''
|
||||
(1..2).collect { int index ->
|
||||
org.springframework.cloud.contract.spec.Contract.make {
|
||||
name("shouldHaveIndex${index}")
|
||||
request {
|
||||
method(PUT())
|
||||
headers {
|
||||
contentType(applicationJson())
|
||||
}
|
||||
url "/${index}"
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
}''')
|
||||
and:
|
||||
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties();
|
||||
properties.targetFramework = testFramework
|
||||
ContractMetadata contract = new ContractMetadata(secondFile.toPath(), false, 1, null, convertAsCollection(secondFile))
|
||||
JavaTestGenerator testGenerator = new JavaTestGenerator()
|
||||
when:
|
||||
String clazz = testGenerator.buildClass(properties, [contract], "test", "test", 'com/foo')
|
||||
then:
|
||||
clazz.contains("validate_shouldHaveIndex1()")
|
||||
clazz.contains("validate_shouldHaveIndex2()")
|
||||
where:
|
||||
testFramework << [JUNIT, SPOCK]
|
||||
}
|
||||
|
||||
def "should generate the test method when there are multiple contracts without name field"() {
|
||||
given:
|
||||
File secondFile = tmpFolder.newFile()
|
||||
secondFile.write('''
|
||||
(1..2).collect { int index ->
|
||||
org.springframework.cloud.contract.spec.Contract.make {
|
||||
request {
|
||||
method(PUT())
|
||||
headers {
|
||||
contentType(applicationJson())
|
||||
}
|
||||
url "/${index}"
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
}
|
||||
}
|
||||
}''')
|
||||
and:
|
||||
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties();
|
||||
properties.targetFramework = testFramework
|
||||
ContractMetadata contract = new ContractMetadata(secondFile.toPath(), false, 1, null, convertAsCollection(secondFile))
|
||||
JavaTestGenerator testGenerator = new JavaTestGenerator()
|
||||
when:
|
||||
String clazz = testGenerator.buildClass(properties, [contract], "test", "test", 'com/foo')
|
||||
then:
|
||||
clazz.contains("_0() throws Exception")
|
||||
clazz.contains("_1() throws Exception")
|
||||
where:
|
||||
testFramework << [JUNIT, SPOCK]
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package org.springframework.cloud.contract.verifier.util
|
||||
|
||||
import spock.lang.Specification
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
class NamesUtilSpec extends Specification {
|
||||
def "should return the whole string before the last one"() {
|
||||
given:
|
||||
String string = "a.b.c.d.e"
|
||||
expect:
|
||||
"a.b.c.d" == NamesUtil.beforeLast(string, ".")
|
||||
}
|
||||
|
||||
def "should return empty string when no token was found for before last"() {
|
||||
given:
|
||||
String string = "a.b.c.d.e"
|
||||
expect:
|
||||
"" == NamesUtil.beforeLast(string, "/")
|
||||
}
|
||||
|
||||
def "should return first token after the last one"() {
|
||||
given:
|
||||
String string = "a.b.c.d.e"
|
||||
expect:
|
||||
"e" == NamesUtil.afterLast(string, ".")
|
||||
}
|
||||
|
||||
def "should return the input string when no token was found for after last"() {
|
||||
given:
|
||||
String string = "a.b.c.d.e"
|
||||
expect:
|
||||
string == NamesUtil.afterLast(string, "/")
|
||||
}
|
||||
|
||||
def "should return first token after the last dot"() {
|
||||
given:
|
||||
String string = "a.b.c.d.e"
|
||||
expect:
|
||||
"e" == NamesUtil.afterLastDot(string)
|
||||
}
|
||||
|
||||
def "should return the input string when no token was found for after last dot"() {
|
||||
given:
|
||||
String string = "abcde"
|
||||
expect:
|
||||
string == NamesUtil.afterLastDot(string)
|
||||
}
|
||||
|
||||
def "should return camel case version of a string"() {
|
||||
given:
|
||||
String string = "BlaBlaBla"
|
||||
expect:
|
||||
"blaBlaBla" == NamesUtil.camelCase(string)
|
||||
}
|
||||
|
||||
def "should return capitalized version of a string"() {
|
||||
given:
|
||||
String string = "blaBlaBla"
|
||||
expect:
|
||||
"BlaBlaBla" == NamesUtil.capitalize(string)
|
||||
}
|
||||
|
||||
def "should return all text to last dot"() {
|
||||
given:
|
||||
String string = "a.b.c.d.e"
|
||||
expect:
|
||||
"a.b.c.d" == NamesUtil.toLastDot(string)
|
||||
}
|
||||
|
||||
def "should return the input string when no token was found for to last dot"() {
|
||||
given:
|
||||
String string = "abcde"
|
||||
expect:
|
||||
string == NamesUtil.toLastDot(string)
|
||||
}
|
||||
|
||||
def "should convert a package notation to directory"() {
|
||||
given:
|
||||
String string = "a.b.c.d.e"
|
||||
expect:
|
||||
"a/b/c/d/e".replace("/", File.separator) == NamesUtil.packageToDirectory(string)
|
||||
}
|
||||
|
||||
def "should convert a directory notation to package"() {
|
||||
given:
|
||||
String string = "a/b/c/d/e".replace("/", File.separator)
|
||||
expect:
|
||||
"a.b.c.d.e" == NamesUtil.directoryToPackage(string)
|
||||
}
|
||||
|
||||
def "should convert all illegal package chars to legal ones"() {
|
||||
given:
|
||||
String string = "a-b c"
|
||||
expect:
|
||||
"a_b_c" == NamesUtil.convertIllegalPackageChars(string)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user