RestAssured and GPath expression for attribute not consistent

655 Views Asked by At

We use the REST-assured framework for doing some unit/integration testing in Java.

The XML answer from a REST service is similar to this:

<?xml version="1.0" encoding="UTF-8"?>
<Items xmlns="urn:service:com:namespace:item/1"
    returned="3" found="3">

    <ItemRef object="urn:svc:com:car:item:123456" type="door">door-123456.pdf</ItemRef>
    <ItemRef object="urn:svc:com:car:item:983425" type="mirror">mirror-43562577.pdf</ItemRef>
    <ItemRef object="urn:svc:com:car:item:983425" type="wheel" >door-94584854.pdf</ItemRef>    
</Items>

In my test I am interested to check the number of items returned by reading the attribute returned like this

givenOK()
    .expect()
        .body("Items.@returned", equalTo("3")) // this is a string
    .when()
    .get(myurl)

And it works well

Now I want as well to control if the URN in the xmlns is correct with the same logic:

givenOK()
    .expect()
        .body("Items.@returned", equalTo("3")) // this is a string
        .body("Items.@xmlns", equalTo("urn:service:com:namespace:item/1"))
    .when()
    .get(myurl)

But when my test run, the expression Items.@xmlns seems not returning the value of the attribute but empty: []

Any idea why this is not working?

1

There are 1 best solutions below

4
On BEST ANSWER

Could it be that the "xmlns" attribute is treated specially because it indicates a namespace?

A possible work-around would be to declare the namespace in the XmlConfig and verify something in the body.

given().
        config(RestAssured.config().xmlConfig(XMLConfig.xmlConfig().declareNamespace("ns", "urn:service:com:namespace:item/1"))).
when().
        get(myUrl).
then().
        body("'ns:ItemRef'[0]", equalTo("door-123456.pdf"));

And another example with multiple nodes and attributes (explicit path):

given()
    .config(
        RestAssured.config()
            .xmlConfig(XmlConfig.xmlConfig()
                .declareNamespace("ns", "urn:service:com:namespace:item/1"))).
    when()
        .get(myUrl)
    .then()
        .body("'ns:RootNode'.'ns:Level1'.'ns:Level2'[0].'@ns:id'", equalTo("AN-ID-123"));