Monday, 17 January 2011

RestFixture with namespaces support

I finally found some time to fix namespaces support on the RestFixture. Now it's possible to specify a namespace context where aliases are defined, and use such aliases in the XPaths that match the response body.

The namespace context is defined via the RestFixtureConfig fixture, defining the key restfixture.xml.namespace.context. The alias can then be used in any XPath subsequently.

The following two snippets are from the auto generated RestFixture documentation (section XPaths and namespaces). This shows the executed RestFixtureConfig with two aliases defined



whilst this shows the usage of one of the two aliases:



Note: the default namespace of a XML body must be explicitly defined as alias!

The latest release (v 1.1) of the Rest Fixture is downloadable from Google code here, and the source code is on GitHub here

12 comments:

Benny said...

Hello, I was trying to use the RestFixture but seems it is mostly designed to work with the Fit Runner and not with SLiM, I mean I have seen no examples using SLiM decision tables.
Am I right? Or is there any documentation on how to use it in SLiM style?

I am asking because Fit seems not to be that supported anymore (in fitnesse) and usually gives me troubles that the SLiM engine doesn't.

Cheers

smartrics said...

Yes you're right - it's primarily for Fit. I will at some point port it to slim and push the code on github

the Apprehensive said...
This comment has been removed by the author.
Benny said...

Thanks for the info, I have another question.
A common test case for services that have a non empty body response is to validate the XML response against an XSD, or even more in general to apply some global validation logic that may not be feasible with XPath or XSD.
So I would like to chain RestFixture tests with another fixture that validates the body of the RestFixture response against an XML Schema that I resolve by path, with the path defined as a table parameter for my fixture.
Should I use the let method to store the entire RestFixture response body in a global variable? Afterward I should be able access the global variable in my own fixture using Fixture.getSymbol I guess.
Or there is a more elegant way to do that.
Thanks again.

smartrics said...

@Benny you could extend the RestFixture and add you public method. for example

public void validateBodyWithXsd(String pathToXsd){ ... }

and provide an implementation that uses last response body String.

or make it more generic using some sort of filter pattern that you can configure - for example - using the Config fixture that comes with RestFixture.

I agree that LET is a bit ugly as solution.

if you clone the github project and extend, send me a pull request and i'll be happy to check and eventually merge

smartrics said...

in your extended fixture you will be able, then, to do like

| validateBodyWithXsd | /path/to/xsd |

Benny said...

Right now unfortunately I don't have the time for that and also I wouldn't invest on FIT, on some OSX versions FIT tests just hang while SLIM seem to work always fine.
So for now I just created a separate simple fixture to validate an xml given a URL, with schema URL that is optional, the fixture will validate against the schema declared in the XML otherwise.
One more note, I also tried to reuse your "let" mechanism in my validation fixture but I found out it only works if the XPath expression selects text content ( node and attribute values), doesn't work with nodes.
For example if I specify:
|let | $response | body | //* | |
the response variable in Symbols will only contain indentation spaces of the response XML, not a text representation of the XML document.
Cheers.

smartrics said...

I just released RestFixture v2.0 with SLIM support + other new features

Mike said...

I've been trying to follow your namespace example but not getting the desired result in the right most column (i.e., the xml result column).

I'm using the following:

!|smartrics.rest.fitnesse.fixture.RestFixtureConfig|
|restfixture.xml.namespace.context|ns1alias=http://smartrics/ns1|
!|smartrics.rest.fitnesse.fixture.RestFixture|http:mymachine:9001/system|
|GET|/hello|200|Content-Type : application/xml|/resource/nstag/ns1alias:number[text()='3']|

I've copied the exact XML (as given in your example) to the Java Code which returns the GET result for /hello tag.
Please provide your inputs.

smartrics said...

@Mike, please post the query here

https://github.com/smartrics/restfixture/issues

also explain what results are you getting

Anonymous said...

Let's assume I have no clue what you're talking about...because I don't. I'm relatively new to all this. I used to have really good developer support for this but sadly it's no longer true.

I've posted this problem to a couple of different blogs and forums with little or no help.

I'm trying to extract a value from an XML response using XPath. I found a RESTFixtureTest that handles this exact scenario using a test file. When I try in in my production system the result is blank. I've copied the xml into an Xpath validator where I can successfully extract the value I need. I past it into me Fitnesse test and....nothing. I'm assuming it may be a namespace problem.

I'm at a total loss. Anyone?

smartrics said...

@Anonymous, i take you're the author of the issue#53.

I have commented on github

https://github.com/smartrics/RestFixture/issues/53#issuecomment-15011654