Fixed equals and hashcode

This commit is contained in:
Marcin Grzejszczak
2016-12-06 17:22:52 +01:00
parent a328130418
commit 39bef2ce08
35 changed files with 567 additions and 111 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.contract.verifier.converter
import groovy.transform.CompileStatic
import org.springframework.cloud.contract.spec.Contract
import org.springframework.cloud.contract.verifier.file.ContractMetadata
/**
@@ -46,7 +47,9 @@ interface SingleFileConverter {
/**
* Returns the name of the converted stub file. If you have multiple contracts
* in a single file then a prefix will be added to the generated file.
* in a single file then a prefix will be added to the generated file. If you
* provide the {@link Contract#name} field then that field will override the
* generated file name.
*
* Example: name of file with 2 contracts is {@code foo.groovy}, it will be
* converted by the implementation to {@code foo.json}. The recursive file

View File

@@ -20,6 +20,7 @@ import groovy.transform.CompileStatic
import org.springframework.cloud.contract.spec.Contract
import org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockStubStrategy
import org.springframework.cloud.contract.verifier.file.ContractMetadata
import org.springframework.cloud.contract.verifier.util.NamesUtil
import java.nio.charset.StandardCharsets
@@ -49,7 +50,7 @@ class DslToWireMockClientConverter extends DslToWireMockConverter {
}
List<String> convertedContracts = []
contract.convertedContract.eachWithIndex { Contract dsl, int index ->
String name = "${rootName}_${index}"
String name = dsl.name ? NamesUtil.convertIllegalPackageChars(dsl.name) : "${rootName}_${index}"
convertedContracts << convertASingleContract(name, contract, dsl)
}
return convertedContracts

View File

@@ -47,7 +47,7 @@ abstract class DslToWireMockConverter implements SingleFileConverter {
return ""
}
protected List<Contract> createGroovyDSLFromStringContent(String groovyDslAsString) {
protected Collection<Contract> createGroovyDSLFromStringContent(String groovyDslAsString) {
return ContractVerifierDslConverter.convertAsCollection(groovyDslAsString)
}
}

View File

@@ -25,6 +25,7 @@ import org.springframework.cloud.contract.verifier.converter.SingleFileConverter
import org.springframework.cloud.contract.verifier.converter.SingleFileConvertersHolder
import org.springframework.cloud.contract.verifier.file.ContractFileScanner
import org.springframework.cloud.contract.verifier.file.ContractMetadata
import org.springframework.cloud.contract.verifier.util.NamesUtil
import java.nio.charset.StandardCharsets
import java.nio.file.Files
@@ -94,7 +95,7 @@ class RecursiveFilesConverter {
}
Path absoluteTargetPath = createAndReturnTargetDirectory(sourceFile)
File newJsonFile = createTargetFileWithProperName(singleFileConverter, absoluteTargetPath,
sourceFile, contractsSize, index)
sourceFile, contractsSize, index, dsl)
newJsonFile.setText(convertedContent, StandardCharsets.UTF_8.toString())
}
} catch (Exception e) {
@@ -112,11 +113,22 @@ class RecursiveFilesConverter {
}
private File createTargetFileWithProperName(SingleFileConverter singleFileConverter, Path absoluteTargetPath,
File sourceFile, int contractsSize, int index) {
String generatedName = singleFileConverter.generateOutputFileNameForInput(sourceFile.name)
String name = contractsSize == 1 ? generatedName : "${index}_${generatedName}"
File sourceFile, int contractsSize, int index, Contract dsl) {
String name = generateName(dsl, contractsSize, singleFileConverter, sourceFile, index)
File newJsonFile = new File(absoluteTargetPath.toFile(), name)
log.info("Creating new json [$newJsonFile.path]")
log.info("Creating new stub [$newJsonFile.path]")
return newJsonFile
}
private String generateName(Contract dsl, int contractsSize, SingleFileConverter converter,
File sourceFile, int index) {
String generatedName = converter.generateOutputFileNameForInput(sourceFile.name)
String extension = NamesUtil.afterLastDot(generatedName)
if (dsl.name) {
return "${dsl.name}.${extension}"
} else if (contractsSize == 1) {
return generatedName
}
return "${index}_${generatedName}"
}
}

View File

@@ -35,7 +35,8 @@ class RecursiveFilesConverterSpec extends Specification {
private static
final Set<Path> EXPECTED_TARGET_FILES = [Paths.get("dslRoot.json"), Paths.get("dir1/dsl1.json"),
Paths.get("dir1/dsl1b.json"), Paths.get("dir2/dsl2.json"),
Paths.get("dir1/0_dsl1_list.json"), Paths.get("dir1/1_dsl1_list.json")]
Paths.get("dir1/0_dsl1_list.json"), Paths.get("dir1/1_dsl1_list.json"),
Paths.get("dir1/shouldHaveIndex1.json"), Paths.get("dir1/shouldHaveIndex2.json")]
@Rule
public TemporaryFolder tmpFolder = new TemporaryFolder();

View File

@@ -17,10 +17,12 @@
package org.springframework.cloud.contract.verifier.wiremock
import com.github.tomakehurst.wiremock.stubbing.StubMapping
import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter
import org.springframework.cloud.contract.spec.Contract
import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter
import spock.lang.Specification
import java.util.regex.Pattern
class WireMockToDslConverterSpec extends Specification {
def 'should produce a Groovy DSL from WireMock stub'() {
@@ -69,12 +71,14 @@ class WireMockToDslConverterSpec extends Specification {
}
response {
status 200
body(
id: [value: '132'],
surname: 'Kowalsky',
name: 'Jan',
created: '2014-02-02 12:23:43'
)
body("""{
"id": {
"value": "132"
},
"surname": "Kowalsky",
"name": "Jan",
"created": "2014-02-02 12:23:43"
}""")
headers {
header 'Content-Type': 'text/plain'
@@ -84,10 +88,12 @@ class WireMockToDslConverterSpec extends Specification {
when:
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
then:
ContractVerifierDslConverter.convert(
def a = ContractVerifierDslConverter.convert(
"""org.springframework.cloud.contract.spec.Contract.make {
$groovyDsl
}""") == expectedGroovyDsl
}""")
def b = expectedGroovyDsl
a == b
}
@@ -97,7 +103,7 @@ class WireMockToDslConverterSpec extends Specification {
{
"request": {
"method": "DELETE",
"urlPattern": "/credit-card-verification-data/[0-9]+",
"urlPattern": "1",
"headers": {
"Content-Type": {
"equalTo": "application/vnd.mymoid-adapter.v2+json; charset=UTF-8"
@@ -119,29 +125,30 @@ class WireMockToDslConverterSpec extends Specification {
Contract expectedGroovyDsl = Contract.make {
request {
method 'DELETE'
url $(consumer(~/\/credit-card-verification-data\/[0-9]+/), producer('/credit-card-verification-data/1'))
url $(consumer(~/1/), producer('1'))
headers {
header('Content-Type': 'application/vnd.mymoid-adapter.v2+json; charset=UTF-8')
}
}
response {
status 200
body("""{
"status": "OK"
body( """{
"status": "OK"
}""")
headers {
header 'Content-Type': 'application/json'
}
}
}
when:
String groovyDsl = WireMockToDslConverter.fromWireMockStub(wireMockStub)
then:
ContractVerifierDslConverter.convert(
def a = ContractVerifierDslConverter.convert(
"""org.springframework.cloud.contract.spec.Contract.make {
$groovyDsl
}""") == expectedGroovyDsl
$groovyDsl
}""")
def b = expectedGroovyDsl
(a.request.url.clientValue as Pattern).pattern() == (b.request.url.clientValue as Pattern).pattern()
}
def 'should convert WireMock stub with response body containing integer'() {
@@ -230,11 +237,14 @@ class WireMockToDslConverterSpec extends Specification {
}
response {
status 200
body([
[a: 1, c: '3'],
'b',
'a'
])
body( """[
{
"a": 1,
"c": "3"
},
"b",
"a"
]""")
headers {
header 'Content-Type': 'application/json'
}
@@ -282,26 +292,26 @@ class WireMockToDslConverterSpec extends Specification {
response {
status 200
body("""[
{
"amount": 1.01,
"name": "Name",
"info": {
"title": "title1",
"payload": null
},
"booleanvalue": true,
"user": null
},
{
"amount": 2.01,
"name": "Name2",
"info": {
"title": "title2",
"payload": null
},
"booleanvalue": true,
"user": null
}
{
"amount": 1.01,
"name": "Name",
"info": {
"title": "title1",
"payload": null
},
"booleanvalue": true,
"user": null
},
{
"amount": 2.01,
"name": "Name2",
"info": {
"title": "title2",
"payload": null
},
"booleanvalue": true,
"user": null
}
]""")
}
}
@@ -337,7 +347,7 @@ class WireMockToDslConverterSpec extends Specification {
request {
method 'POST'
url '/test'
body ('''{"property1":"abc","property2":"2017-01","property3":"666","property4":1428566412}''')
body ('''{"property1":"abc", "property2":"2017-01", "property3":"666", "property4":1428566412}''')
}
response {
status 200
@@ -359,15 +369,15 @@ class WireMockToDslConverterSpec extends Specification {
String wireMockStub = '''\
{
"request": {
"method": "POST",
"url": "/test",
"bodyPatterns": [{
"matches": "[0-9]{5}"
}]
"method": "POST",
"url": "/test",
"bodyPatterns": [{
"matches": "1"
}]
},
"response": {
"status": 200
}
"status": 200
}
}
'''
and:
@@ -377,7 +387,7 @@ class WireMockToDslConverterSpec extends Specification {
request {
method 'POST'
url '/test'
body $(consumer(~/[0-9]{5}/), producer('12345'))
body $(consumer(~/1/), producer('1'))
}
response {
status 200
@@ -391,7 +401,7 @@ class WireMockToDslConverterSpec extends Specification {
$groovyDsl
}""")
and:
evaluatedGroovyDsl == expectedGroovyDsl
(evaluatedGroovyDsl.request.body.clientValue as Pattern).pattern() == (expectedGroovyDsl.request.body.clientValue as Pattern).pattern()
}
def 'should convert WireMock stub with request body with equalToJson'() {
@@ -418,7 +428,7 @@ class WireMockToDslConverterSpec extends Specification {
request {
method 'POST'
url '/test'
body '''{"pan":"4855141150107894","expirationDate":"2017-01","dcvx":"178"}'''
body '''{"pan":"4855141150107894", "expirationDate":"2017-01", "dcvx":"178"}'''
}
response {
status 200
@@ -458,7 +468,7 @@ class WireMockToDslConverterSpec extends Specification {
request {
method 'POST'
url '/test'
body '''{"pan":"4855141150107894","expirationDate":"2017-01","dcvx":"178"}'''
body '''{"pan":"4855141150107894", "expirationDate":"2017-01", "dcvx":"178"}'''
}
response {
status 200
@@ -483,7 +493,7 @@ class WireMockToDslConverterSpec extends Specification {
"url" : "/test",
"method" : "POST",
"bodyPatterns" : [ {
"matches" : "[0-9]{2}"
"matches" : "1"
} ]
},
"response" : {
@@ -498,7 +508,7 @@ class WireMockToDslConverterSpec extends Specification {
request {
method 'POST'
url '/test'
body $(consumer(~/[0-9]{2}/), producer('12'))
body $(consumer(~/1/), producer('1'))
}
response {
status 200
@@ -512,7 +522,7 @@ class WireMockToDslConverterSpec extends Specification {
$groovyDsl
}""")
and:
evaluatedGroovyDsl == expectedGroovyDsl
(evaluatedGroovyDsl.request.body.clientValue as Pattern).pattern() == (expectedGroovyDsl.request.body.clientValue as Pattern).pattern()
}
def 'should convert WireMock stub with priorities'() {

View File

@@ -0,0 +1,33 @@
import org.springframework.cloud.contract.spec.Contract
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(1..2).collect { int index ->
Contract.make {
name("shouldHaveIndex${index}")
request {
method(PUT())
headers {
contentType(applicationJson())
}
url "/${index}"
}
response {
status 200
}
}
}