Skip to content

Commit 77b05b6

Browse files
authored
Fixes gh-1876 (#1877)
1 parent 622aef7 commit 77b05b6

File tree

4 files changed

+133
-5
lines changed

4 files changed

+133
-5
lines changed

spring-cloud-contract-tools/spring-cloud-contract-converters/src/test/groovy/org/springframework/cloud/contract/verifier/converter/RecursiveFilesConverterSpec.groovy

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ class RecursiveFilesConverterSpec extends Specification {
4747
@Rule
4848
public TemporaryFolder tmpFolder = new TemporaryFolder()
4949

50+
def "should recursively convert all matching files with stubs"() {
51+
given:
52+
File originalSourceRootDirectory = new File(this.getClass()
53+
.getResource("/converter/dir3").toURI())
54+
File contractsDslDir = tmpFolder.newFolder("source")
55+
File stubsOutputDir = tmpFolder.newFolder("target")
56+
FileSystemUtils
57+
.copyRecursively(originalSourceRootDirectory, contractsDslDir)
58+
and:
59+
RecursiveFilesConverter recursiveFilesConverter = new RecursiveFilesConverter(stubsOutputDir, contractsDslDir, new ArrayList<>(), ".*", false)
60+
when:
61+
recursiveFilesConverter.processFiles()
62+
then:
63+
Collection<File> createdFiles = [] as List
64+
stubsOutputDir.
65+
eachFileRecurse(FileType.FILES) { createdFiles << it }
66+
Set<String> relativizedCreatedFiles =
67+
getRelativePathsForFilesInDirectory(createdFiles, stubsOutputDir)
68+
relativizedCreatedFiles == [Paths.get("Account creating.json"),
69+
Paths.get("Test route.json")] as Set<Path>
70+
}
71+
5072
def "should recursively convert all matching files"() {
5173
given:
5274
File originalSourceRootDirectory = new File(this.getClass()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
request:
3+
method: "POST"
4+
url: null
5+
urlPath: "/post-route"
6+
queryParameters: {}
7+
headers:
8+
Content-Type: "application/json"
9+
cookies: null
10+
body:
11+
age: 35
12+
name: "Jonh"
13+
bodyFromFile: null
14+
bodyFromFileAsBytes: null
15+
matchers:
16+
url: null
17+
body: []
18+
headers:
19+
- key: "Content-Type"
20+
regex: "application/json.*"
21+
predefined: null
22+
command: null
23+
regexType: "as_string"
24+
queryParameters: []
25+
cookies: []
26+
multipart: null
27+
multipart: null
28+
response:
29+
status: 200
30+
headers: null
31+
cookies: null
32+
body:
33+
age: 35
34+
name: "Jonh"
35+
bodyFromFile: null
36+
bodyFromFileAsBytes: null
37+
matchers:
38+
body: []
39+
headers: []
40+
cookies: []
41+
async: false
42+
fixedDelayMilliseconds: null
43+
input: null
44+
outputMessage: null
45+
description: null
46+
label: null
47+
name: "Account creating"
48+
priority: 1
49+
ignored: false
50+
inProgress: false
51+
metadata: {}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
request:
3+
method: "GET"
4+
url: null
5+
urlPath: "/my-first-route"
6+
queryParameters: {}
7+
headers:
8+
Content-Type: "application/json"
9+
cookies: null
10+
body: null
11+
bodyFromFile: null
12+
bodyFromFileAsBytes: null
13+
matchers:
14+
url: null
15+
body: []
16+
headers:
17+
- key: "Content-Type"
18+
regex: "application/json.*"
19+
predefined: null
20+
command: null
21+
regexType: "as_string"
22+
queryParameters: []
23+
cookies: []
24+
multipart: null
25+
multipart: null
26+
response:
27+
status: 200
28+
headers: null
29+
cookies: null
30+
body:
31+
test: "MVZWIKTLUUXDCDGKXMDC"
32+
bodyFromFile: null
33+
bodyFromFileAsBytes: null
34+
matchers:
35+
body:
36+
- path: "$.['test']"
37+
type: "by_regex"
38+
value: "^\\s*\\S[\\S\\s]*"
39+
minOccurrence: null
40+
maxOccurrence: null
41+
predefined: null
42+
regexType: "as_string"
43+
headers: []
44+
cookies: []
45+
async: false
46+
fixedDelayMilliseconds: null
47+
input: null
48+
outputMessage: null
49+
description: null
50+
label: null
51+
name: "Test route"
52+
priority: 1
53+
ignored: false
54+
inProgress: false
55+
metadata: {}

spring-cloud-contract-verifier/src/main/java/org/springframework/cloud/contract/verifier/converter/YamlToContracts.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Collection;
3131
import java.util.Collections;
3232
import java.util.HashMap;
33+
import java.util.HashSet;
3334
import java.util.List;
3435
import java.util.Map;
3536
import java.util.Optional;
@@ -723,9 +724,7 @@ private void mapInputBodyMatchers(YamlContract.Input yamlContractInput, Input ds
723724
.ifPresent(yamlContractBodyStubMatchers -> yamlContractBodyStubMatchers
724725
.forEach(yamlContractBodyStubMatcher -> {
725726
ContentType contentType = evaluateClientSideContentType(
726-
yamlHeadersToContractHeaders(
727-
Optional.ofNullable(yamlContractInput.messageHeaders)
728-
.orElse(new HashMap<>())),
727+
yamlHeadersToContractHeaders(yamlContractInput.messageHeaders),
729728
Optional.ofNullable(yamlContractInput.messageBody).orElse(null));
730729
MatchingTypeValue value;
731730
switch (yamlContractBodyStubMatcher.type) {
@@ -764,8 +763,9 @@ private void mapInputBodyMatchers(YamlContract.Input yamlContractInput, Input ds
764763
}
765764

766765
private Headers yamlHeadersToContractHeaders(Map<String, Object> headers) {
767-
Set<Header> convertedHeaders = headers.keySet().stream()
768-
.map(header -> Header.build(header, headers.get(header))).collect(toSet());
766+
Set<Header> convertedHeaders = headers != null
767+
? headers.keySet().stream().map(header -> Header.build(header, headers.get(header))).collect(toSet())
768+
: new HashSet<>();
769769
Headers contractHeaders = new Headers();
770770
contractHeaders.headers(convertedHeaders);
771771
return contractHeaders;

0 commit comments

Comments
 (0)