Search in sources :

Example 1 with MatchingTypeValue

use of org.springframework.cloud.contract.spec.internal.MatchingTypeValue 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)

Example 2 with MatchingTypeValue

use of org.springframework.cloud.contract.spec.internal.MatchingTypeValue in project spring-cloud-contract by spring-cloud.

the class YamlToContracts method mapRequestBodyMatchers.

private void mapRequestBodyMatchers(YamlContract.Request yamlContractRequest, Request dslContractRequest) {
    dslContractRequest.bodyMatchers((bodyMatchers) -> Optional.ofNullable(yamlContractRequest.matchers).map(stubMatchers -> stubMatchers.body).ifPresent(stubMatchers -> stubMatchers.forEach(stubMatcher -> {
        ContentType contentType = evaluateClientSideContentType(yamlHeadersToContractHeaders(Optional.ofNullable(yamlContractRequest.headers).orElse(new HashMap<>())), Optional.ofNullable(yamlContractRequest.body).orElse(null));
        MatchingTypeValue value = null;
        switch(stubMatcher.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 = stubMatcher.value;
                if (stubMatcher.predefined != null) {
                    regex = predefinedToPattern(stubMatcher.predefined).pattern();
                }
                value = bodyMatchers.byRegex(regex);
                break;
            case by_equality:
                value = bodyMatchers.byEquality();
                break;
            case by_type:
                value = bodyMatchers.byType(matchingTypeValueHolder -> {
                    if (stubMatcher.minOccurrence != null) {
                        matchingTypeValueHolder.minOccurrence(stubMatcher.minOccurrence);
                    }
                    if (stubMatcher.maxOccurrence != null) {
                        matchingTypeValueHolder.maxOccurrence(stubMatcher.maxOccurrence);
                    }
                });
                break;
            case by_null:
                // do nothing
                break;
            default:
                throw new UnsupportedOperationException("The type [" + stubMatcher.type + "] is" + " unsupported.Hint:If you 're using <predefined> remember to pass <type:by_regex > ");
        }
        if (value != null) {
            if (XML == contentType) {
                bodyMatchers.xPath(stubMatcher.path, value);
            } else {
                bodyMatchers.jsonPath(stubMatcher.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)

Example 3 with MatchingTypeValue

use of org.springframework.cloud.contract.spec.internal.MatchingTypeValue in project spring-cloud-contract by spring-cloud.

the class YamlToContracts method mapOutputBodyMatchers.

private void mapOutputBodyMatchers(YamlContract.OutputMessage yamlContractOutputMessage, OutputMessage dslContractOutputMessage) {
    if (yamlContractOutputMessage.matchers != null) {
        dslContractOutputMessage.bodyMatchers(dslContractOutputMessageBodyMatchers -> Optional.ofNullable(yamlContractOutputMessage.matchers.body).ifPresent(yamlContractBodyTestMatchers -> yamlContractBodyTestMatchers.forEach(yamlContractBodyTestMatcher -> {
            ContentType contentType = evaluateClientSideContentType(yamlHeadersToContractHeaders(yamlContractOutputMessage.headers), yamlContractOutputMessage.body);
            MatchingTypeValue value;
            switch(yamlContractBodyTestMatcher.type) {
                case by_date:
                    value = dslContractOutputMessageBodyMatchers.byDate();
                    break;
                case by_time:
                    value = dslContractOutputMessageBodyMatchers.byTime();
                    break;
                case by_timestamp:
                    value = dslContractOutputMessageBodyMatchers.byTimestamp();
                    break;
                case by_regex:
                    String regex = yamlContractBodyTestMatcher.value;
                    if (yamlContractBodyTestMatcher.predefined != null) {
                        regex = predefinedToPattern(yamlContractBodyTestMatcher.predefined).pattern();
                    }
                    value = dslContractOutputMessageBodyMatchers.byRegex(regex);
                    break;
                case by_equality:
                    value = dslContractOutputMessageBodyMatchers.byEquality();
                    break;
                case by_type:
                    value = dslContractOutputMessageBodyMatchers.byType(v -> {
                        if (yamlContractBodyTestMatcher.minOccurrence != null) {
                            v.minOccurrence(yamlContractBodyTestMatcher.minOccurrence);
                        }
                        if (yamlContractBodyTestMatcher.maxOccurrence != null) {
                            v.maxOccurrence(yamlContractBodyTestMatcher.maxOccurrence);
                        }
                    });
                    break;
                case by_command:
                    value = dslContractOutputMessageBodyMatchers.byCommand(yamlContractBodyTestMatcher.value);
                    break;
                case by_null:
                    value = dslContractOutputMessageBodyMatchers.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 (XML == contentType) {
                dslContractOutputMessageBodyMatchers.xPath(yamlContractBodyTestMatcher.path, value);
            } else {
                dslContractOutputMessageBodyMatchers.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)

Example 4 with MatchingTypeValue

use of org.springframework.cloud.contract.spec.internal.MatchingTypeValue in project spring-cloud-contract by spring-cloud.

the class YamlToContracts method mapInputBodyMatchers.

private void mapInputBodyMatchers(YamlContract.Input yamlContractInput, Input dslContractInput) {
    dslContractInput.bodyMatchers(dslContractInputBodyMatchers -> Optional.ofNullable(yamlContractInput.matchers.body).ifPresent(yamlContractBodyStubMatchers -> yamlContractBodyStubMatchers.forEach(yamlContractBodyStubMatcher -> {
        ContentType contentType = evaluateClientSideContentType(yamlHeadersToContractHeaders(Optional.ofNullable(yamlContractInput.messageHeaders).orElse(new HashMap<>())), Optional.ofNullable(yamlContractInput.messageBody).orElse(null));
        MatchingTypeValue value;
        switch(yamlContractBodyStubMatcher.type) {
            case by_date:
                value = dslContractInputBodyMatchers.byDate();
                break;
            case by_time:
                value = dslContractInputBodyMatchers.byTime();
                break;
            case by_timestamp:
                value = dslContractInputBodyMatchers.byTimestamp();
                break;
            case by_regex:
                String regex = yamlContractBodyStubMatcher.value;
                if (yamlContractBodyStubMatcher.predefined != null) {
                    regex = predefinedToPattern(yamlContractBodyStubMatcher.predefined).pattern();
                }
                value = dslContractInputBodyMatchers.byRegex(regex);
                break;
            case by_equality:
                value = dslContractInputBodyMatchers.byEquality();
                break;
            default:
                throw new UnsupportedOperationException("The type " + "[" + yamlContractBodyStubMatcher.type + "] is unsupported. " + "Hint: If you're using <predefined> remember to pass < type:by_regex > ");
        }
        if (XML == contentType) {
            dslContractInputBodyMatchers.xPath(yamlContractBodyStubMatcher.path, value);
        } else {
            dslContractInputBodyMatchers.jsonPath(yamlContractBodyStubMatcher.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

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)4 File (java.io.File)4 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 MalformedURLException (java.net.MalformedURLException)4 URISyntaxException (java.net.URISyntaxException)4 URL (java.net.URL)4 URLClassLoader (java.net.URLClassLoader)4 Files (java.nio.file.Files)4 Paths (java.nio.file.Paths)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Set (java.util.Set)4