Search in sources :

Example 1 with RegexProperty

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);
            }
        });
    }
}
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)

Example 2 with RegexProperty

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);
        }
    }));
}
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) NotToEscapePattern(org.springframework.cloud.contract.spec.internal.NotToEscapePattern) Pattern(java.util.regex.Pattern) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) NotToEscapePattern(org.springframework.cloud.contract.spec.internal.NotToEscapePattern) ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty)

Example 3 with RegexProperty

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;
}
Also used : Pattern(java.util.regex.Pattern) Header(org.springframework.cloud.contract.spec.internal.Header) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) ArrayList(java.util.ArrayList)

Example 4 with RegexProperty

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;
}
Also used : Pattern(java.util.regex.Pattern) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) BodyMatchers(org.springframework.cloud.contract.spec.internal.BodyMatchers)

Example 5 with RegexProperty

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;
}
Also used : Pattern(java.util.regex.Pattern) Header(org.springframework.cloud.contract.spec.internal.Header) RegexProperty(org.springframework.cloud.contract.spec.internal.RegexProperty) ArrayList(java.util.ArrayList)

Aggregations

Pattern (java.util.regex.Pattern)14 RegexProperty (org.springframework.cloud.contract.spec.internal.RegexProperty)14 BodyMatchers (org.springframework.cloud.contract.spec.internal.BodyMatchers)8 ContentType (org.springframework.cloud.contract.verifier.util.ContentType)8 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 Header (org.springframework.cloud.contract.spec.internal.Header)4 NamedProperty (org.springframework.cloud.contract.spec.internal.NamedProperty)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 Contract (org.springframework.cloud.contract.spec.Contract)3 DslProperty (org.springframework.cloud.contract.spec.internal.DslProperty)3 FromFileProperty (org.springframework.cloud.contract.spec.internal.FromFileProperty)3 MatchingStrategy (org.springframework.cloud.contract.spec.internal.MatchingStrategy)3 MatchingType (org.springframework.cloud.contract.spec.internal.MatchingType)3 Request (org.springframework.cloud.contract.spec.internal.Request)3 Url (org.springframework.cloud.contract.spec.internal.Url)3