How can I validate a SOAP response against an XSD file that defines the response schema. the web service I'm calling has an XMLDocument as input and output, so can't use WSDL for response schema validation.
4 Answers
23
votes
1
votes
Use script assertion:
def project = messageExchange.modelItem.testStep.testCase.testSuite.project
def wsdlcontext = project.getInterfaceAt(0).getDefinitionContext()
def validator = new com.eviware.soapui.impl.wsdl.support.wsdl.WsdlValidator(wsdlcontext);
def errors = validator.assertRequest(messageExchange, false)
assert errors.length < 1
0
votes
You can use the groovy script for validation the response against the xsd file. Here is the way to validate
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;
import javax.xml.XMLConstants;
//Read your xsd file and get the conten into a variable like below.
def xsdContent = "Some Schema Standard";
//Take the response into another variable that you have to validate.
def actualXMLResponse = "Actual XML Response ";
//create a SchemaFactory object
def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//Create a given schema object with help of factory
def schema = factory.newSchema(new StreamSource(new StringReader(xsdContent ));
//Create a validator
def validator = schema.newValidator();
//now validate the actual response against the given schema
try {
validator.validate(new StreamSource(new StringReader(actualXMLResponse )));
} catch(Exception e) {
log.info (e);
assert false;
}
I hope this will help you :-)
-1
votes
This not worked me caused try not working
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;
import javax.xml.XMLConstants;
//Read your xsd file and get the conten into a variable like below.
// trim - XSD SCHEME no spaces
def xsdscheme = context.expand('${Properties-XSD_Scheme_Black_and_White#XSDSchemeWhite}')
def xsdscheme2 = xsdscheme.replace(' ', '')
xsdscheme2 = xsdscheme2.replaceAll("[\n\r]", "");
log.info "RES2 TRIMED " + xsdscheme2
def xsdContent = xsdscheme2;
//Take the response into another variable that you have to validate.
Res = context.expand('${#TestCase#WhiteListDecoded}');
def Res2 = Res.replace(' ', '')
Res2 = Res2.replaceAll("[\n\r]", "");
log.info "RES2 TRIMED " + Res2
def actualXMLResponse = Res2
//create a SchemaFactory object
def factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//Create a given schema object with help of factory
def schema = factory.newSchema(new StreamSource(new StringReader(xsdContent ));
//Create a validator
def validator = schema.newValidator();
//now validate the actual response against the given schema
try {
validator.validate(new StreamSource(new StringReader(actualXMLResponse )));
} catch(Exception e) {
log.info (e);
assert false;
}