Search in sources :

Example 1 with ExecutionProperty

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

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

the class JsonBodyVerificationBuilder method methodForCommandExecution.

@Override
public void methodForCommandExecution(BodyMatcher bodyMatcher, BlockBuilder bb, Object copiedBody) {
    String path = quotedAndEscaped(bodyMatcher.path());
    // assert that path exists
    retrieveObjectByPath(copiedBody, bodyMatcher.path());
    ExecutionProperty property = (ExecutionProperty) bodyMatcher.value();
    bb.addLine(postProcessJsonPathCall.apply(property.insertValue("parsedJson.read(" + path + ")")));
    addColonIfRequired(lineSuffix, bb);
}
Also used : ExecutionProperty(org.springframework.cloud.contract.spec.internal.ExecutionProperty)

Example 3 with ExecutionProperty

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

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

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

the class YamlToContracts method serverValue.

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

Aggregations

ExecutionProperty (org.springframework.cloud.contract.spec.internal.ExecutionProperty)11 Pattern (java.util.regex.Pattern)5 DslProperty (org.springframework.cloud.contract.spec.internal.DslProperty)5 FromFileProperty (org.springframework.cloud.contract.spec.internal.FromFileProperty)5 Request (org.springframework.cloud.contract.spec.internal.Request)5 HashMap (java.util.HashMap)3 NamedProperty (org.springframework.cloud.contract.spec.internal.NamedProperty)3 ContentType (org.springframework.cloud.contract.verifier.util.ContentType)3 Collection (java.util.Collection)2 List (java.util.List)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Contract (org.springframework.cloud.contract.spec.Contract)2 BodyMatchers (org.springframework.cloud.contract.spec.internal.BodyMatchers)2 Cookies (org.springframework.cloud.contract.spec.internal.Cookies)2 Headers (org.springframework.cloud.contract.spec.internal.Headers)2 Input (org.springframework.cloud.contract.spec.internal.Input)2 MatchingStrategy (org.springframework.cloud.contract.spec.internal.MatchingStrategy)2