Search in sources :

Example 1 with ContentType

use of org.springframework.cloud.contract.verifier.util.ContentType in project spring-cloud-contract by spring-cloud.

the class JaxRsRequestMethodWhen method appendMethodAndBody.

void appendMethodAndBody(SingleContractMetadata metadata) {
    Request request = metadata.getContract().getRequest();
    ContentType type = metadata.getInputTestContentType();
    String method = request.getMethod().getServerValue().toString().toLowerCase();
    if (request.getBody() != null) {
        String contentType = StringUtils.hasText(metadata.getDefinedInputTestContentType()) ? metadata.getDefinedInputTestContentType() : type.getMimeType();
        Object body = request.getBody().getServerValue();
        String value;
        if (body instanceof ExecutionProperty) {
            value = body.toString();
        } else if (body instanceof FromFileProperty) {
            FromFileProperty fileProperty = (FromFileProperty) body;
            value = fileProperty.isByte() ? this.bodyReader.readBytesFromFileString(metadata, fileProperty, CommunicationType.REQUEST) : this.bodyReader.readStringFromFileString(metadata, fileProperty, CommunicationType.REQUEST);
        } else {
            value = "\"" + requestBodyAsString(metadata) + "\"";
        }
        this.blockBuilder.addIndented(".build(\"" + method.toUpperCase() + "\", entity(" + value + ", \"" + contentType + "\"))");
    } else {
        this.blockBuilder.addIndented(".build(\"" + method.toUpperCase() + "\")");
    }
}
Also used : ContentType(org.springframework.cloud.contract.verifier.util.ContentType) Request(org.springframework.cloud.contract.spec.internal.Request) ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty) FromFileProperty(org.springframework.cloud.contract.spec.internal.FromFileProperty)

Example 2 with ContentType

use of org.springframework.cloud.contract.verifier.util.ContentType in project spring-cloud-contract by spring-cloud.

the class ContractsToYaml method request.

protected void request(Contract contract, YamlContract yamlContract) {
    Request request = contract.getRequest();
    if (request != null) {
        ContentType requestContentType = evaluateClientSideContentType(request.getHeaders(), request.getBody());
        yamlContract.request = new YamlContract.Request();
        mapRequestMethod(yamlContract.request, request);
        mapRequestUrl(yamlContract.request, request);
        mapRequestUrlPath(yamlContract.request, request);
        mapRequestMatchers(yamlContract.request);
        Url requestUrl = Optional.ofNullable(request.getUrl()).orElse(request.getUrlPath());
        if (requestUrl.getQueryParameters() != null) {
            mapRequestQueryParameters(yamlContract.request, requestUrl);
            mapRequestMatchersQueryParameters(yamlContract.request, requestUrl);
        }
        mapRequestHeaders(yamlContract.request, request);
        mapRequestCookies(yamlContract.request, request);
        mapRequestBody(yamlContract.request, request);
        mapRequestMultipart(yamlContract.request, request);
        mapRequestMatchersBody(yamlContract.request, request);
        mapRequestMatchersUrl(yamlContract.request, request);
        mapRequestMatchersMultipart(yamlContract.request, request);
        // TODO: Cookie matchers - including absent
        if (XML != requestContentType) {
            setInputBodyMatchers(request.getBody(), yamlContract.request.matchers.body);
        }
        setInputHeadersMatchers(request.getHeaders(), yamlContract.request.matchers.headers);
    }
}
Also used : ContentUtils.evaluateClientSideContentType(org.springframework.cloud.contract.verifier.util.ContentUtils.evaluateClientSideContentType) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) Request(org.springframework.cloud.contract.spec.internal.Request) Url(org.springframework.cloud.contract.spec.internal.Url)

Example 3 with ContentType

use of org.springframework.cloud.contract.verifier.util.ContentType 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 4 with ContentType

use of org.springframework.cloud.contract.verifier.util.ContentType in project spring-cloud-contract by spring-cloud.

the class ContractsToYaml method response.

protected void response(YamlContract yamlContract, Contract contract) {
    if (contract.getResponse() != null) {
        Response contractResponse = contract.getResponse();
        ContentType contentType = evaluateClientSideContentType(contractResponse.getHeaders(), contractResponse.getBody());
        YamlContract.Response response = new YamlContract.Response();
        yamlContract.response = response;
        mapResponseAsync(contractResponse, response);
        mapResponseFixedDelayMilliseconds(contractResponse, response);
        mapResponseStatus(contractResponse, response);
        mapResponseHeaders(contractResponse, response);
        mapResponseCookies(contractResponse, response);
        mapResponseBody(contractResponse, response);
        mapResponseBodyMatchers(contractResponse, response);
        if (XML != contentType) {
            setOutputBodyMatchers(contractResponse.getBody(), yamlContract.response.matchers.body);
        }
        setOutputHeadersMatchers(contractResponse.getHeaders(), yamlContract.response.matchers.headers);
    }
}
Also used : Response(org.springframework.cloud.contract.spec.internal.Response) ContentUtils.evaluateClientSideContentType(org.springframework.cloud.contract.verifier.util.ContentUtils.evaluateClientSideContentType) ContentType(org.springframework.cloud.contract.verifier.util.ContentType)

Example 5 with ContentType

use of org.springframework.cloud.contract.verifier.util.ContentType in project spring-cloud-contract by spring-cloud.

the class YamlToContracts method mapResponseBodyMatchers.

private void mapResponseBodyMatchers(YamlContract.Response yamlContractResponse, Response dslContractResponse) {
    dslContractResponse.bodyMatchers(bodyMatchers -> Optional.ofNullable(yamlContractResponse.matchers).map(yamlContractResponseTestMatchers -> yamlContractResponseTestMatchers.body).ifPresent(yamlContractBodyTestMatchers -> yamlContractBodyTestMatchers.forEach(yamlContractBodyTestMatcher -> {
        ContentType contentType = evaluateClientSideContentType(yamlHeadersToContractHeaders(yamlContractResponse.headers), yamlContractResponse.body);
        MatchingTypeValue value;
        switch(yamlContractBodyTestMatcher.type) {
            case by_date:
                value = bodyMatchers.byDate();
                break;
            case by_time:
                value = bodyMatchers.byTime();
                break;
            case by_timestamp:
                value = bodyMatchers.byTimestamp();
                break;
            case by_regex:
                String regex = yamlContractBodyTestMatcher.value;
                if (yamlContractBodyTestMatcher.predefined != null) {
                    regex = predefinedToPattern(yamlContractBodyTestMatcher.predefined).pattern();
                }
                value = bodyMatchers.byRegex(regex);
                break;
            case by_equality:
                value = bodyMatchers.byEquality();
                break;
            case by_type:
                value = bodyMatchers.byType(v -> {
                    if (yamlContractBodyTestMatcher.minOccurrence != null) {
                        v.minOccurrence(yamlContractBodyTestMatcher.minOccurrence);
                    }
                    if (yamlContractBodyTestMatcher.maxOccurrence != null) {
                        v.maxOccurrence(yamlContractBodyTestMatcher.maxOccurrence);
                    }
                });
                break;
            case by_command:
                value = bodyMatchers.byCommand(yamlContractBodyTestMatcher.value);
                break;
            case by_null:
                value = bodyMatchers.byNull();
                break;
            default:
                throw new UnsupportedOperationException("The type [" + yamlContractBodyTestMatcher.type + "] is unsupported. " + "Hint: If you're using <predefined> remember to pass < type:by_regex > ");
        }
        if (yamlContractBodyTestMatcher.path != null) {
            if (XML == contentType) {
                bodyMatchers.xPath(yamlContractBodyTestMatcher.path, value);
            } else {
                bodyMatchers.jsonPath(yamlContractBodyTestMatcher.path, value);
            }
        }
    })));
}
Also used : Arrays(java.util.Arrays) MatchingTypeValue(org.springframework.cloud.contract.spec.internal.MatchingTypeValue) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) HashMap(java.util.HashMap) NamesUtil(org.springframework.cloud.contract.verifier.util.NamesUtil) 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) Header(org.springframework.cloud.contract.spec.internal.Header) ArrayList(java.util.ArrayList) Yaml(org.yaml.snakeyaml.Yaml) NamedProperty(org.springframework.cloud.contract.spec.internal.NamedProperty) URLClassLoader(java.net.URLClassLoader) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) Contract(org.springframework.cloud.contract.spec.Contract) Map(java.util.Map) Input(org.springframework.cloud.contract.spec.internal.Input) Collectors.toSet(java.util.stream.Collectors.toSet) DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) MapUtils(org.apache.commons.collections.MapUtils) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) List(java.util.List) Paths(java.nio.file.Paths) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) RegexPatterns(org.springframework.cloud.contract.spec.internal.RegexPatterns) Response(org.springframework.cloud.contract.spec.internal.Response) Collections(java.util.Collections) Headers(org.springframework.cloud.contract.spec.internal.Headers) StringUtils(org.springframework.util.StringUtils) ContentUtils.evaluateClientSideContentType(org.springframework.cloud.contract.verifier.util.ContentUtils.evaluateClientSideContentType) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) MatchingTypeValue(org.springframework.cloud.contract.spec.internal.MatchingTypeValue)

Aggregations

ContentType (org.springframework.cloud.contract.verifier.util.ContentType)23 Pattern (java.util.regex.Pattern)13 Map (java.util.Map)11 ContentUtils.evaluateClientSideContentType (org.springframework.cloud.contract.verifier.util.ContentUtils.evaluateClientSideContentType)11 List (java.util.List)10 BodyMatchers (org.springframework.cloud.contract.spec.internal.BodyMatchers)10 DslProperty (org.springframework.cloud.contract.spec.internal.DslProperty)10 Request (org.springframework.cloud.contract.spec.internal.Request)10 Collectors (java.util.stream.Collectors)9 RegexProperty (org.springframework.cloud.contract.spec.internal.RegexProperty)9 Collection (java.util.Collection)8 Optional (java.util.Optional)8 Contract (org.springframework.cloud.contract.spec.Contract)8 ExecutionProperty (org.springframework.cloud.contract.spec.internal.ExecutionProperty)8 Input (org.springframework.cloud.contract.spec.internal.Input)8 NamedProperty (org.springframework.cloud.contract.spec.internal.NamedProperty)8 Response (org.springframework.cloud.contract.spec.internal.Response)8 HashMap (java.util.HashMap)7 Headers (org.springframework.cloud.contract.spec.internal.Headers)7 OutputMessage (org.springframework.cloud.contract.spec.internal.OutputMessage)7