Search code examples
springspring-bootgroovyspring-cloudspring-cloud-contract

Spring cloud contract with XML. Trying to specify mock depend on request


In the most cases I can specify mock via element content-type and the value inside of it. I'm connecting it with xpath and everything is going well. But now i got a case where inside wsdl every xsd got the same content-type. I have no idea how I can make a condition to specify concrete mock. I guess the only thing which is different in every xsd inside this url is root element. Have u got any solution? Do u think it is possible to specify mock depend on request root element and how ? This is example of my contract

Contract.make {
        request {
            priority(1)
            method 'POST'
            url '/ws/xxxx/xxx'
            body(new MatchingStrategy(anyNonEmptyString(), MatchingStrategy.Type.MATCHING))
            bodyMatchers {
                xPath("//*[local-name()='Chnl']/text()", new MatchingTypeValue(MatchingType.EQUALITY, "xxx"))
            }
            headers {
                header(contentType(), regex('application/.*xml.*xxxxxxx.*'))
            }
        }
        response {
            status 200
            body(fileAsBytes("xxxx.xml"))
            headers {
                contentType(applicationXml())
            }
        }
    }

Solution

  • I solved my problem by adding new xpath condition in bodyMatcher for the main xml element.

    xPath("//*[local-name()='Body']", new MatchingTypeValue(MatchingType.REGEX, ".*nameOfRootXmlElement.*"))