0

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?

рüффп
  • 5,172
  • 34
  • 67
  • 113

1 Answers1

1

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"));
рüффп
  • 5,172
  • 34
  • 67
  • 113
Johan
  • 37,479
  • 32
  • 149
  • 237
  • Probably, but in xpath I can use something using the function `namespace-uri()` but it looks not working in a RESTAssured/GPATH context. – рüффп Jul 18 '14 at 07:23
  • The second xmlConfig() does not work as is. I had to use `new XmlConfig()` instead. – рüффп Apr 12 '16 at 12:10
  • @ruffp You need to statically import xmlConfig from XmlConfig – Johan Apr 12 '16 at 12:24
  • 1
    Perfect, I did not found another way but this workaround is working well, I also added one more sample to clarify how to use the single quotes with multiple elements. – рüффп Apr 12 '16 at 12:41