Search in sources :

Example 1 with Multipart

use of org.springframework.cloud.contract.spec.internal.Multipart 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);
            }
        });
    }
}
Also used : JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) HashMap(java.util.HashMap) ContentUtils.evaluateClientSideContentType(org.springframework.cloud.contract.verifier.util.ContentUtils.evaluateClientSideContentType) Request(org.springframework.cloud.contract.spec.internal.Request) XML(org.springframework.cloud.contract.verifier.util.ContentType.XML) OutputMessage(org.springframework.cloud.contract.spec.internal.OutputMessage) MatchingType(org.springframework.cloud.contract.spec.internal.MatchingType) FromFileProperty(org.springframework.cloud.contract.spec.internal.FromFileProperty) NamedProperty(org.springframework.cloud.contract.spec.internal.NamedProperty) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) Cookies(org.springframework.cloud.contract.spec.internal.Cookies) BodyMatchers(org.springframework.cloud.contract.spec.internal.BodyMatchers) Url(org.springframework.cloud.contract.spec.internal.Url) Input(org.springframework.cloud.contract.spec.internal.Input) MapConverter(org.springframework.cloud.contract.verifier.util.MapConverter) Multipart(org.springframework.cloud.contract.spec.internal.Multipart) DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) List(java.util.List) NotToEscapePattern(org.springframework.cloud.contract.spec.internal.NotToEscapePattern) MatchingStrategy(org.springframework.cloud.contract.spec.internal.MatchingStrategy) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) QueryParameter(org.springframework.cloud.contract.spec.internal.QueryParameter) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Response(org.springframework.cloud.contract.spec.internal.Response) Headers(org.springframework.cloud.contract.spec.internal.Headers) Multipart(org.springframework.cloud.contract.spec.internal.Multipart) FromFileProperty(org.springframework.cloud.contract.spec.internal.FromFileProperty) DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) NamedProperty(org.springframework.cloud.contract.spec.internal.NamedProperty) ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Multipart

use of org.springframework.cloud.contract.spec.internal.Multipart 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);
            }
        });
    }
}
Also used : NotToEscapePattern(org.springframework.cloud.contract.spec.internal.NotToEscapePattern) Pattern(java.util.regex.Pattern) Multipart(org.springframework.cloud.contract.spec.internal.Multipart) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) NamedProperty(org.springframework.cloud.contract.spec.internal.NamedProperty) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 Pattern (java.util.regex.Pattern)2 Multipart (org.springframework.cloud.contract.spec.internal.Multipart)2 NamedProperty (org.springframework.cloud.contract.spec.internal.NamedProperty)2 NotToEscapePattern (org.springframework.cloud.contract.spec.internal.NotToEscapePattern)2 RegexProperty (org.springframework.cloud.contract.spec.internal.RegexProperty)2 Collection (java.util.Collection)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Contract (org.springframework.cloud.contract.spec.Contract)1 BodyMatchers (org.springframework.cloud.contract.spec.internal.BodyMatchers)1 Cookies (org.springframework.cloud.contract.spec.internal.Cookies)1 DslProperty (org.springframework.cloud.contract.spec.internal.DslProperty)1 ExecutionProperty (org.springframework.cloud.contract.spec.internal.ExecutionProperty)1 FromFileProperty (org.springframework.cloud.contract.spec.internal.FromFileProperty)1 Headers (org.springframework.cloud.contract.spec.internal.Headers)1 Input (org.springframework.cloud.contract.spec.internal.Input)1