Junit Assert condtion on XML data using XPATH - junit

I have the below xml as response from one end system . I want to write Junit test case using assert condition for evaluating the element values.
xml is :
<?xml version="1.0" encoding="UTF-8"?>
<table>
<record>
<field name="name" type="java.lang.String">whiteskylabs</field>
</record>
</table>
Is there anyway in Junit to use Xpath. If can u please help me out on it.
Regards
Vikram

You can use XMLUnit, to perform assertions on xml documents, or use Java XPath classes (http://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/package-summary.html).
Regards.

You could use a separate helper test flow for the XPath evaluation, or just plain Java XPath. See here for an example of Java XPath: http://www.rgagnon.com/javadetails/java-0550.html

Related

WSO2 data service and data mapper

I am trying to apply the data mapper mediator to the output of the XML data service defined within WSO2EI. Documentation indicates, that to use the data mapper you need to have a fully qualified names in the XML input files.
The data service I am creating does not include qualified prefixes within the XML it generates.
I tried to export the XSLT data mapping from the CAR file and run it along the sample XML generated by the data service through the external XML transformer - it did not work. However, if I added qualified prefixes in the input XML manually, everything works fine.
It seems that the reason for my data mapper not working is the default, and not qualified, namespace in the input XML. Unfortunately, I cannot get the data service including namespace prexifes in its output. Any ideas?
To illustrate the nature of the problem let us consider two slightly different inputs; first XML input file uses the default names, second one qualified names:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<users xmlns="http://ws.wso2.org/dataservice">
<user>
<last>Waelchi</last>
<first>Xzavier</first>
<country>Swaziland</country>
</user>
</users>
</soapenv:Body>
</soapenv:Envelope>
This XML is not properly handled by the XSLT, no matter if within WSO2EI, or external XML processor. However, the same XML with qualified names:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<users xmlns:p="http://ws.wso2.org/dataservice">
<p:user>
<p:last>Waelchi</p:last>
<p:first>Xzavier</p:first>
<p:country>Swaziland</p:country>
</p:user>
</users>
</soapenv:Body>
</soapenv:Envelope>
is properly interpreted at least by the external XML processor. My problem is, that I cannot get WSO2EI data service to include qualified prefixes in its output.
OK, I have managed to bypass the problem transforming XML output by WSO2 data service into qualified XML using XSLT transform. However, I am still unable to get qualified XML directly from the data service; any suggestion shall be appreciated.

jmeter regex extractor for response header in html

I am trying to grab the values from the response header of my login sampler and then use it in the following samplers. However the response header I see is in HTML format as opposed to the colon-separated as most of the other posts have.
Here is my response header:
<authentication>
<token>6e36dcf1f5d84fcc7062b981891dd2b233bd8373d74f56f03a3bf328560976a2</token>
<anonymous>false</anonymous>
<parameters>
<header>
<userName>ps-subscriber2#mysite.com</userName>
<userType>SUBSCRIPTION_USER</userType>
<Authorization>6e36dcf1f5d84fcc7062b981891dd2b233bd8373d74f56f03a3bf328560976a2</Authorization>
</header>
</parameters>
</authentication>
I am trying different ways to extract the header values but none seem to be working:
And here is what i see in the Results Tree when I run the plan:
I am not well versed with regex which I think is the problem. Can someone help me with getting the regex correct?
It doesn't look like a header to me, it seems like you're testing an API hence I would suggest switching your Regular Expression Extractor to Body.
JMeter provides XPath Extractor which is designed to work with XML and XHTML data, consider switching to this test element instead of the Regular Expression Extractor. The relevant configuration will look like:
Reference Name: the same as for RegEx, i.e. userName
XPath query: //userName
If response is not XML/XHTML-compliant you will need to check Use Tidy box
You can use View Results Tree listener which has RegExp Tester and XPath Tester to evaluate your expressions against real response
References:
XPath Tutorial
XPath Language Specification

How to decode base64 string as HTML content in XSLT?

I'm trying to convert my encoded base64 HTML content from an attribute and place it in the HTML content using XLST, hope somebody can help.
I have this string:
<p><b>Hello!</b><span>This is a span</span></p>
And this is the base64 encoded value:
PHA+PGI+SGVsbG8hPC9iPjxzcGFuPlRoaXMgaXMgYSBzcGFuPC9zcGFuPjwvcD4=
The XML data looks like this:
<Data Get="True">
<Result TextFlow="PHA+PGI+SGVsbG8hPC9iPjxzcGFuPlRoaXMgaXMgYSBzcGFuPC9zcGFuPjwvcD4=">
</Result>
</Data>
My last attempt was using this:
fn:cast($XML/Data/Result/#TextFlow,'xs:base64Binary','xs:String',false())
That code send this error:
Unknown function - Name and number or arguments do not match any function signature in the static context.
Thanks for your help!
There's no built-in capability in XSLT to do this.
Coming soon is the EXPath binary module:
http://expath.org/spec/binary
The bin:decode-string() function is what you need. We've got an implementation of this for Saxon but it's not integrated into the product yet. Meanwhile there is the extension function saxon:base64BinaryToString which is essentially the same:
http://www.saxonica.com/documentation/index.html#!functions/saxon/base64Binary-to-string
and which is available in Saxon-PE or higher.
Both functions take an argument of type xs:base64Binary; to convert a string in base64 to a value of this type, you use the constructor function xs:base64Binary(string). This requires XSLT 2.0 of course.
If your (decoded) string contains markup, then to copy it to the serialized HTML output you will need to use the deprecated disable-output-escaping="yes" option.
UPDATE (April 2019)
Recent releases of Saxon (the current release is 9.9) include the EXPath binary module as standard in Saxon-PE and higher editions.

SchemaLocation in JIBX generated xml

I am generating xml from Java object using JIBX. I want schemalocation to be set in generated xml. I am able to put namespace but but schemaLocation. Anyone has one idea how to do it?
You should be able to do this as an attribute value on the root element of your output document, along these lines:
<value name="schemaLocation" ns="http://www.w3.org/2001/XMLSchema" field="locationField"/>

SAXParseException: Element type SOAP:Text must be followed by either attribute specifications, ">" or "/>"

I'm attempting to read a response from a web service call in a junit test running in Eclipse Galileo. I'm able to successfully receive responses except when the response is a SOAP fault. Then I get the following exception:
org.xml.sax.SAXParseException: Element type "SOAP:Text" must be followed by either attribute specifications, ">" or "/>"
I have validated the XML in LiquidXML Studio against the SOAP 1.2 schema and it checks out.
Here is the XML response that SAX appears to be choking on. It has been stripped to the minimum in an attempt to eliminate anything obvious (I even made sure it didn't have any self closing elements):
<SOAP:Envelope xmlns:SOAP="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP_ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP:Header>
</SOAP:Header>
<SOAP:Body>
<SOAP:Fault>
<SOAP:Code>
<SOAP:Value>SOAP:Sender</SOAP:Value>
<SOAP:Subcode>
<SOAP:Value>SOAP:Sender</SOAP:Value>
</SOAP:Subcode>
</SOAP:Code>
<SOAP:Reason>
<SOAP:Text xml:lang="">
</SOAP:Text>
</SOAP:Reason>
<SOAP:Node>
</SOAP:Node>
<SOAP:Role>
</SOAP:Role>
<SOAP:Detail>
</SOAP:Detail>
</SOAP:Fault>
</SOAP:Body>
</SOAP:Envelope>
Any help would be appreciated.
Its obviously not regcognising 'xml:lang=""' as an attribute. CHeck with your xsd or xmlSchema what attributes are valid. Also you should be using
xml:lang=""
rather than "" although most parsers forgive you for this.
I think the problem is in mapping the soap fault xml to its corresponding object.
It turns out the problem was related to a tool I was using to return static string responses to web service requests. The static response XML contained the xml:lang attribute. However, when the tool was returning the static string, it was modifying it on the way out and replacing xml:lang on-the-fly with the fully qualified namespace equivalent {http://www.w3.org/XML/1998/namespace}lang. When this response was received, the SAXParser was choking because it couldn't interpret the fully qualified equivalent.
The tool returning the static responses used a Groovy xml parser as an integral part of sending the response.
The XmlParser Groovy class has a constructor that I had to change to set validating and namespaceAware attributes to false. So instead of XmlParser(), the tool now calls XmlParser(false, false).
Problem solved.
Thanks for the responses.