use of org.springframework.cloud.contract.spec.internal.NamedProperty in project spring-cloud-contract by spring-cloud.
the class ContractsToYaml method mapRequestMultipart.
private void mapRequestMultipart(YamlContract.Request yamlContractRequest, Request request) {
Multipart multipart = request.getMultipart();
if (multipart != null) {
yamlContractRequest.multipart = new YamlContract.Multipart();
Map<String, Object> map = (Map<String, Object>) MapConverter.getTestSideValues(multipart);
map.forEach((key, value) -> {
if (value instanceof NamedProperty) {
Object fileName = Optional.ofNullable(((NamedProperty) value).getName()).map(DslProperty::getServerValue).orElse(null);
Object contentType = Optional.ofNullable(((NamedProperty) value).getContentType()).map(DslProperty::getServerValue).orElse(null);
Object fileContent = Optional.ofNullable(((NamedProperty) value).getValue()).map(DslProperty::getServerValue).orElse(null);
YamlContract.Named named = new YamlContract.Named();
named.paramName = key;
named.fileName = fileName instanceof String ? Optional.ofNullable(((NamedProperty) value).getName()).map(DslProperty::getServerValue).map(Object::toString).orElse(null) : null;
named.fileContent = (String) Optional.ofNullable(fileContent).filter(f -> f instanceof String).orElse(null);
named.fileContentAsBytes = fileContent instanceof FromFileProperty ? new String(((FromFileProperty) fileContent).asBytes()) : null;
named.fileContentFromFileAsBytes = resolveFileNameAsBytes(fileContent);
named.contentType = (String) Optional.ofNullable(contentType).filter(f -> f instanceof String).orElse(null);
named.fileNameCommand = fileName instanceof ExecutionProperty ? fileName.toString() : null;
named.fileContentCommand = fileContent instanceof ExecutionProperty ? fileContent.toString() : null;
named.contentTypeCommand = contentType instanceof ExecutionProperty ? contentType.toString() : null;
yamlContractRequest.multipart.named.add(named);
} else {
yamlContractRequest.multipart.params.put(key, value != null ? value.toString() : null);
}
});
}
}
use of org.springframework.cloud.contract.spec.internal.NamedProperty 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.NamedProperty in project spring-cloud-contract by spring-cloud.
the class YamlToContracts method mapRequestMultiPart.
private void mapRequestMultiPart(YamlContract.Request yamlContractRequest, Request dslContractRequest) {
if (yamlContractRequest.multipart != null) {
Map<String, Object> multipartMap = new HashMap<>();
yamlContractRequest.multipart.params.forEach((paramKey, paramValue) -> {
YamlContract.KeyValueMatcher matcher = yamlContractRequest.matchers.multipart.params.stream().filter((param) -> param.key.equals(paramKey)).findFirst().orElse(null);
Object value = paramValue;
if (matcher != null) {
value = matcher.regex != null ? Pattern.compile(matcher.regex) : predefinedToPattern(matcher.predefined);
}
multipartMap.put(paramKey, new DslProperty<>(value, paramValue));
});
yamlContractRequest.multipart.named.forEach(namedParam -> {
YamlContract.MultipartNamedStubMatcher matcher = yamlContractRequest.matchers.multipart.named.stream().filter((stubMatcher) -> stubMatcher.paramName.equals(namedParam.paramName)).findFirst().orElse(null);
Object fileNameValue = namedParam.fileName;
Object fileContentValue = namedParam.fileContent;
String fileContentAsBytes = namedParam.fileContentAsBytes;
String fileContentFromFileAsBytes = namedParam.fileContentFromFileAsBytes;
String contentTypeCommand = namedParam.contentTypeCommand;
String fileContentCommand = namedParam.fileContentCommand;
String fileNameCommand = namedParam.fileNameCommand;
Object contentTypeValue = namedParam.contentType;
if (matcher != null && matcher.fileName != null) {
fileNameValue = matcher.fileName.regex != null ? Pattern.compile(matcher.fileName.regex) : predefinedToPattern(matcher.fileName.predefined);
}
if (matcher != null && matcher.fileContent != null) {
fileContentValue = matcher.fileContent.regex != null ? Pattern.compile(matcher.fileContent.regex) : predefinedToPattern(matcher.fileContent.predefined);
}
if (matcher != null && matcher.contentType != null) {
contentTypeValue = matcher.contentType.regex != null ? Pattern.compile(matcher.contentType.regex) : predefinedToPattern(matcher.contentType.predefined);
}
multipartMap.put(namedParam.paramName, new NamedProperty(new DslProperty<>(fileNameValue, fileNameCommand != null ? new ExecutionProperty(fileNameCommand) : namedParam.fileName), new DslProperty<>(fileContentValue, namedParam.fileContent != null ? namedParam.fileContent : fileContentFromFileAsBytes != null ? dslContractRequest.fileAsBytes(namedParam.fileContentFromFileAsBytes) : fileContentAsBytes != null ? fileContentAsBytes.getBytes() : new ExecutionProperty(fileContentCommand)), new DslProperty<>(contentTypeValue, contentTypeCommand != null ? new ExecutionProperty(contentTypeCommand) : namedParam.contentType)));
});
dslContractRequest.multipart(multipartMap);
}
}
Aggregations