SOAPUI: Validate response against xsd schema file
Asked Answered
C

4

8

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.

Corrianne answered 25/11, 2008 at 11:11 Comment(0)
B
25

In case you still need this (valid for SOAP UI version 2.5.1): File, Preferences, Editor Setting, Validate Response.

Behl answered 9/1, 2010 at 22:37 Comment(1)
How this answered your question? You wanted to validate part of response against custom XSD (something that I also want to achieve). This answer shows how to validate response against XSD from WSDL (soup can do this automatically)Labellum
H
1

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

Holler answered 15/10, 2013 at 17:30 Comment(0)
G
0

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 :-)

Grantland answered 1/5, 2015 at 5:36 Comment(0)
A
-1

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;
}
Aleph answered 9/11, 2020 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.