Search in sources :

Example 1 with DslProperty

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

the class ContractsToYaml method setOutputBodyMatchers.

protected void setOutputBodyMatchers(DslProperty<?> body, List<YamlContract.BodyTestMatcher> bodyMatchers) {
    Object testSideValues = MapConverter.getTestSideValues(body);
    JsonPaths paths = new JsonToJsonPathsConverter().transformToJsonPathWithTestsSideValues(body);
    paths.stream().filter(m -> m.valueBeforeChecking() instanceof Pattern).forEach((m) -> {
        Object element = JsonToJsonPathsConverter.readElement(testSideValues, m.keyBeforeChecking());
        YamlContract.BodyTestMatcher bodyTestMatcher = new YamlContract.BodyTestMatcher();
        bodyTestMatcher.path = m.keyBeforeChecking();
        bodyTestMatcher.type = YamlContract.TestMatcherType.by_regex;
        bodyTestMatcher.value = ((Pattern) m.valueBeforeChecking()).pattern();
        bodyTestMatcher.regexType = regexType(element);
        bodyMatchers.add(bodyTestMatcher);
    });
    Optional.ofNullable(body).filter(b -> b.getServerValue() instanceof Pattern).ifPresent((b) -> {
        YamlContract.BodyTestMatcher bodyTestMatcher = new YamlContract.BodyTestMatcher();
        bodyTestMatcher.type = YamlContract.TestMatcherType.by_regex;
        bodyTestMatcher.value = ((Pattern) b.getServerValue()).pattern();
        bodyMatchers.add(bodyTestMatcher);
    });
}
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) JsonToJsonPathsConverter(org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter) JsonPaths(org.springframework.cloud.contract.verifier.util.JsonPaths)

Example 2 with DslProperty

use of org.springframework.cloud.contract.spec.internal.DslProperty 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 3 with DslProperty

use of org.springframework.cloud.contract.spec.internal.DslProperty 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);
    }
}
Also used : HashMap(java.util.HashMap) DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) NamedProperty(org.springframework.cloud.contract.spec.internal.NamedProperty) ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty)

Example 4 with DslProperty

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

the class YamlToContracts method clientValue.

protected DslProperty<?> clientValue(Object value, YamlContract.KeyValueMatcher matcher, String key) {
    Object clientValue = value instanceof DslProperty ? ((DslProperty<?>) value).getClientValue() : value;
    if (matcher != null && matcher.regex != null) {
        clientValue = Pattern.compile(matcher.regex);
        Pattern pattern = (Pattern) clientValue;
        assertPatternMatched(pattern, value, key);
    } else if (matcher != null && matcher.predefined != null) {
        Pattern pattern = predefinedToPattern(matcher.predefined);
        clientValue = pattern;
        assertPatternMatched(pattern, value, key);
    } else if (matcher != null && matcher.command != null) {
        return new DslProperty<>(value, new ExecutionProperty(matcher.command));
    }
    return new DslProperty<>(clientValue, value);
}
Also used : Pattern(java.util.regex.Pattern) DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty)

Example 5 with DslProperty

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

the class YamlToContracts method queryParamValue.

protected Object queryParamValue(YamlContract yamlContract, String key, Object value) {
    Request request = new Request();
    YamlContract.QueryParameterMatcher matcher = yamlContract.request.matchers.queryParameters.stream().filter(queryParameter -> queryParameter.key.equals(key)).findFirst().orElse(null);
    if (matcher == null) {
        return value;
    }
    switch(matcher.type) {
        case equal_to:
            return new DslProperty<>(request.equalTo(matcher.value), value);
        case containing:
            return new DslProperty<>(request.containing(matcher.value), value);
        case matching:
            return new DslProperty<>(request.matching(matcher.value), value);
        case not_matching:
            return new DslProperty<>(request.notMatching(matcher.value), value);
        case equal_to_json:
            return new DslProperty<>(request.equalToJson(matcher.value), value);
        case equal_to_xml:
            return new DslProperty<>(request.equalToXml(matcher.value), value);
        case absent:
            return new DslProperty<Object>(request.absent(), null);
        default:
            throw new UnsupportedOperationException("The provided matching type [" + matcher + "] is unsupported. Use on of " + Arrays.toString(YamlContract.MatchingType.values()));
    }
}
Also used : DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) Request(org.springframework.cloud.contract.spec.internal.Request)

Aggregations

DslProperty (org.springframework.cloud.contract.spec.internal.DslProperty)17 Map (java.util.Map)9 Collection (java.util.Collection)8 GString (groovy.lang.GString)7 Pattern (java.util.regex.Pattern)7 Request (org.springframework.cloud.contract.spec.internal.Request)7 ExecutionProperty (org.springframework.cloud.contract.spec.internal.ExecutionProperty)6 FromFileProperty (org.springframework.cloud.contract.spec.internal.FromFileProperty)6 ContentType (org.springframework.cloud.contract.verifier.util.ContentType)6 DslPart (au.com.dius.pact.consumer.dsl.DslPart)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 ClientDslProperty (org.springframework.cloud.contract.spec.internal.ClientDslProperty)5 NamedProperty (org.springframework.cloud.contract.spec.internal.NamedProperty)5 MapConverter (org.springframework.cloud.contract.verifier.util.MapConverter)5 PactDslJsonArray (au.com.dius.pact.consumer.dsl.PactDslJsonArray)4 Optional (java.util.Optional)4 Contract (org.springframework.cloud.contract.spec.Contract)4 BodyMatchers (org.springframework.cloud.contract.spec.internal.BodyMatchers)4 MatchingStrategy (org.springframework.cloud.contract.spec.internal.MatchingStrategy)4