Skip to content

Commit

Permalink
Fixed conversion to and from YAML; fixes spring-cloudgh-1921
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Sep 5, 2023
1 parent a15db49 commit b063b3a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,19 @@ else if (value instanceof RegexProperty || value instanceof Pattern) {
}

private void mapRequestMatchersUrl(YamlContract.Request yamlContractRequest, Request request) {
Object url = Optional.ofNullable(request.getUrl()).map(Url::getClientValue).orElse(null);
Object url = Optional.ofNullable(request.getUrl()).map(Url::getClientValue)
.orElse(Optional.ofNullable(request.getUrlPath()).map(Url::getClientValue).orElse(null));
YamlContract.KeyValueMatcher keyValueMatcher = new YamlContract.KeyValueMatcher();
if (url instanceof RegexProperty) {
keyValueMatcher.regex = ((RegexProperty) url).pattern();
yamlContractRequest.matchers.url = keyValueMatcher;
}
else if (url instanceof ExecutionProperty) {
keyValueMatcher.command = url.toString();
else if (url instanceof Pattern) {
keyValueMatcher.regex = ((Pattern) url).pattern();
yamlContractRequest.matchers.url = keyValueMatcher;
}
else {
yamlContractRequest.matchers.url = null;
}

Object urlPath = Optional.ofNullable(request.getUrlPath()).map(Url::getClientValue).orElse(null);
if (urlPath instanceof RegexProperty) {
keyValueMatcher.regex = ((RegexProperty) urlPath).pattern();
yamlContractRequest.matchers.url = keyValueMatcher;
}
else if (urlPath instanceof ExecutionProperty) {
keyValueMatcher.command = urlPath.toString();
else if (url instanceof ExecutionProperty) {
keyValueMatcher.command = url.toString();
yamlContractRequest.matchers.url = keyValueMatcher;
}
else {
Expand Down Expand Up @@ -578,7 +570,7 @@ protected YamlContract.TestMatcherType testMatcherType(MatchingType matchingType

protected YamlContract.StubMatcherType stubMatcherType(MatchingType matchingType) {
if (matchingType == MatchingType.COMMAND || matchingType == MatchingType.TYPE) {
throw new UnsupportedOperationException("No type or command for client side");
return null;
}
return STUB_MATCHER_TYPE.getOrDefault(matchingType, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,41 @@ class YamlContractConverterSpec extends Specification {
urlPropertyName << ['url', 'urlPath']
}

@Issue('#1921')
def 'should convert YAML to Contract and back to YAML without losing url information'() {
given:
File ymlMatchers = File.createTempFile('contract_matchers_url', '.yml').with {
write YamlContractConverterSpec.getResource('/yml/contract_matchers_url.yml')
.text
return it
}
expect:
converter.isAccepted(ymlMatchers)
when:
Collection<Contract> contracts = converter.convertFrom(ymlMatchers)
List<YamlContract> convertedBack = converter.convertTo(contracts)
then:
convertedBack.size() == 1
convertedBack[0].request.matchers.url.regex == "/get/[0-9]"
}

@Issue('#1921')
def 'should convert YAML to Contract and back to YAML without exceptions'() {
given:
File ymlMatchers = File.createTempFile('contract_matchers', '.yml').with {
write YamlContractConverterSpec.getResource('/yml/contract_matchers.yml')
.text
return it
}
expect:
converter.isAccepted(ymlMatchers)
when:
Collection<Contract> contracts = converter.convertFrom(ymlMatchers)
List<YamlContract> convertedBack = converter.convertTo(contracts)
then:
convertedBack.size() == 1
}

protected Object assertQueryParam(QueryParameters queryParameters, String queryParamName, Object serverValue,
MatchingStrategy.Type clientType, Object clientValue) {
if (clientType == MatchingStrategy.Type.ABSENT) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
request:
method: GET
urlPath: /get/1
matchers:
url:
regex: /get/[0-9]
response:
status: 200

0 comments on commit b063b3a

Please sign in to comment.