use of org.springframework.cloud.contract.spec.internal.RegexProperty in project spring-cloud-contract by spring-cloud.
the class ContractsToYaml method mapRequestMatchersMultipart.
private void mapRequestMatchersMultipart(YamlContract.Request yamlContractRequest, Request request) {
Multipart multipart = request.getMultipart();
if (multipart != null) {
yamlContractRequest.matchers.multipart = new YamlContract.MultipartStubMatcher();
Map<String, Object> map = (Map<String, Object>) MapConverter.getStubSideValues(multipart);
map.forEach((key, value) -> {
if (value instanceof NamedProperty) {
Object fileName = Optional.ofNullable(((NamedProperty) value).getName()).map(DslProperty::getClientValue).orElse(null);
Object fileContent = Optional.ofNullable(((NamedProperty) value).getValue()).map(DslProperty::getClientValue).orElse(null);
Object contentType = Optional.ofNullable(((NamedProperty) value).getContentType()).map(DslProperty::getClientValue).orElse(null);
if (fileName instanceof RegexProperty || fileContent instanceof RegexProperty || contentType instanceof RegexProperty) {
YamlContract.MultipartNamedStubMatcher multipartNamedStubMatcher = new YamlContract.MultipartNamedStubMatcher();
multipartNamedStubMatcher.paramName = key;
multipartNamedStubMatcher.fileName = valueMatcher(fileName);
multipartNamedStubMatcher.fileContent = valueMatcher(fileContent);
multipartNamedStubMatcher.contentType = valueMatcher(contentType);
yamlContractRequest.matchers.multipart.named.add(multipartNamedStubMatcher);
}
} else if (value instanceof RegexProperty || value instanceof Pattern) {
RegexProperty property = new RegexProperty(value);
YamlContract.KeyValueMatcher keyValueMatcher = new YamlContract.KeyValueMatcher();
keyValueMatcher.key = key;
keyValueMatcher.regex = property.pattern();
keyValueMatcher.regexType = regexType(property.clazz());
yamlContractRequest.matchers.multipart.params.add(keyValueMatcher);
}
});
}
}
use of org.springframework.cloud.contract.spec.internal.RegexProperty in project spring-cloud-contract by spring-cloud.
the class ContractsToYaml method setOutputHeadersMatchers.
protected void setOutputHeadersMatchers(Headers headers, List<YamlContract.TestHeaderMatcher> headerMatchers) {
Optional.ofNullable(headers).map(Headers::asTestSideMap).ifPresent(testSideMap -> testSideMap.forEach((key, value) -> {
if (value instanceof RegexProperty || value instanceof Pattern) {
RegexProperty property = new RegexProperty(value);
YamlContract.TestHeaderMatcher testHeaderMatcher = new YamlContract.TestHeaderMatcher();
testHeaderMatcher.key = key;
testHeaderMatcher.regex = property.pattern();
testHeaderMatcher.regexType = regexType(property.clazz());
headerMatchers.add(testHeaderMatcher);
} else if (value instanceof ExecutionProperty) {
YamlContract.TestHeaderMatcher testHeaderMatcher = new YamlContract.TestHeaderMatcher();
testHeaderMatcher.key = key;
testHeaderMatcher.command = ((ExecutionProperty) value).getExecutionCommand();
headerMatchers.add(testHeaderMatcher);
} else if (value instanceof NotToEscapePattern) {
YamlContract.TestHeaderMatcher testHeaderMatcher = new YamlContract.TestHeaderMatcher();
testHeaderMatcher.key = key;
testHeaderMatcher.regex = (((NotToEscapePattern) value).getServerValue()).pattern();
headerMatchers.add(testHeaderMatcher);
}
}));
}
use of org.springframework.cloud.contract.spec.internal.RegexProperty in project spring-cloud-contract by spring-cloud.
the class StubRunnerJmsMessageSelector method headersMatch.
private List<String> headersMatch(Message message, Contract groovyDsl) {
List<String> unmatchedHeaders = new ArrayList<>();
Map<String, Object> headers = StubRunnerJmsAccessor.getHeaders(message);
for (Header it : groovyDsl.getInput().getMessageHeaders().getEntries()) {
String name = it.getName();
Object value = it.getClientValue();
Object valueInHeader = headers.get(name);
boolean matches;
if (value instanceof RegexProperty) {
Pattern pattern = ((RegexProperty) value).getPattern();
matches = pattern.matcher(valueInHeader.toString()).matches();
} else {
matches = valueInHeader != null && valueInHeader.toString().equals(value.toString());
}
if (!matches) {
unmatchedHeaders.add("Header with name [" + name + "] was supposed to " + unmatchedText(value) + " but the value is [" + (valueInHeader != null ? valueInHeader.toString() : "null") + "]");
}
}
return unmatchedHeaders;
}
use of org.springframework.cloud.contract.spec.internal.RegexProperty in project spring-cloud-contract by spring-cloud.
the class StubRunnerCamelPredicate method matchViaContent.
private boolean matchViaContent(Contract groovyDsl, Object inputMessage, Object dslBody) {
boolean matches;
ContentType type = ContentUtils.getClientContentType(inputMessage, groovyDsl.getInput().getMessageHeaders());
if (type == ContentType.JSON) {
BodyMatchers matchers = groovyDsl.getInput().getBodyMatchers();
matches = matchesForJsonPayload(groovyDsl, inputMessage, matchers, dslBody);
} else if (dslBody instanceof RegexProperty && inputMessage instanceof String) {
Pattern pattern = ((RegexProperty) dslBody).getPattern();
matches = pattern.matcher((String) inputMessage).matches();
bodyUnmatchedLog(dslBody, matches, pattern);
} else {
matches = dslBody.equals(inputMessage);
bodyUnmatchedLog(dslBody, matches, inputMessage);
}
return matches;
}
use of org.springframework.cloud.contract.spec.internal.RegexProperty in project spring-cloud-contract by spring-cloud.
the class StubRunnerIntegrationMessageSelector method headersMatch.
private List<String> headersMatch(Message<?> message, Contract groovyDsl) {
List<String> unmatchedHeaders = new ArrayList<>();
Map<String, Object> headers = message.getHeaders();
for (Header it : groovyDsl.getInput().getMessageHeaders().getEntries()) {
String name = it.getName();
Object value = it.getClientValue();
Object valueInHeader = headers.get(name);
boolean matches;
if (value instanceof RegexProperty || value instanceof Pattern) {
Pattern pattern = new RegexProperty(value).getPattern();
matches = pattern.matcher(valueInHeader.toString()).matches();
} else {
matches = valueInHeader != null && valueInHeader.toString().equals(value.toString());
}
if (!matches) {
unmatchedHeaders.add("Header with name [" + name + "] was supposed to " + unmatchedText(value) + " but the value is [" + (valueInHeader != null ? valueInHeader.toString() : "null") + "]");
}
}
return unmatchedHeaders;
}
Aggregations