Search in sources :

Example 1 with BodyMatchers

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

the class GenericXmlBodyThen method apply.

@Override
public MethodVisitor<Then> apply(SingleContractMetadata metadata) {
    BodyMatchers bodyMatchers = this.bodyParser.responseBodyMatchers(metadata);
    Object convertedResponseBody = this.bodyParser.convertResponseBody(metadata);
    XmlBodyVerificationBuilder xmlBodyVerificationBuilder = new XmlBodyVerificationBuilder(metadata.getContract(), Optional.of(this.blockBuilder.getLineEnding()));
    xmlBodyVerificationBuilder.addXmlResponseBodyCheck(this.blockBuilder, convertedResponseBody, bodyMatchers, this.bodyParser.responseAsString(), true);
    return this;
}
Also used : BodyMatchers(org.springframework.cloud.contract.spec.internal.BodyMatchers)

Example 2 with BodyMatchers

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

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

the class XmlBodyVerificationBuilderTest method shouldAddXmlProcessingLines.

@Test
public void shouldAddXmlProcessingLines() {
    // Given
    XmlBodyVerificationBuilder builder = new XmlBodyVerificationBuilder(new Contract(), Optional.of(";"));
    BlockBuilder blockBuilder = new BlockBuilder(" ");
    BodyMatchers matchers = new BodyMatchers();
    // When
    builder.addXmlResponseBodyCheck(blockBuilder, xml, matchers, xml, true);
    // Then
    String test = blockBuilder.toString();
    assertThat(test).contains("DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();").contains("builderFactory.setNamespaceAware(true);").contains("DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();").contains("Document parsedXml = documentBuilder.parse(new InputSource(new StringReader(").contains(xml);
}
Also used : BodyMatchers(org.springframework.cloud.contract.spec.internal.BodyMatchers) Contract(org.springframework.cloud.contract.spec.Contract) Test(org.junit.Test)

Example 4 with BodyMatchers

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

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

the class StubRunnerIntegrationMessageSelector 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)

Aggregations

BodyMatchers (org.springframework.cloud.contract.spec.internal.BodyMatchers)14 ContentType (org.springframework.cloud.contract.verifier.util.ContentType)11 Pattern (java.util.regex.Pattern)10 RegexProperty (org.springframework.cloud.contract.spec.internal.RegexProperty)10 Map (java.util.Map)6 Contract (org.springframework.cloud.contract.spec.Contract)6 Collection (java.util.Collection)5 List (java.util.List)5 Optional (java.util.Optional)5 Collectors (java.util.stream.Collectors)5 DslProperty (org.springframework.cloud.contract.spec.internal.DslProperty)5 FromFileProperty (org.springframework.cloud.contract.spec.internal.FromFileProperty)5 MatchingStrategy (org.springframework.cloud.contract.spec.internal.MatchingStrategy)5 MatchingType (org.springframework.cloud.contract.spec.internal.MatchingType)5 NamedProperty (org.springframework.cloud.contract.spec.internal.NamedProperty)5 Request (org.springframework.cloud.contract.spec.internal.Request)5 Url (org.springframework.cloud.contract.spec.internal.Url)5 JsonPaths (org.springframework.cloud.contract.verifier.util.JsonPaths)5 JsonToJsonPathsConverter (org.springframework.cloud.contract.verifier.util.JsonToJsonPathsConverter)5 MapConverter (org.springframework.cloud.contract.verifier.util.MapConverter)5