Service Virtualization

 View Only
Expand all | Collapse all

How to validate XML Request/Response - Parsing

  • 1.  How to validate XML Request/Response - Parsing

    Posted Jun 09, 2016 08:00 AM

    Hi

    I want to validate my XML request and response to check if there are nay parse errors. Like missing tag, closing tag not found.

    Just want to check if XML parsing is successful.

     

    Example: XML validator in notepad which tells XML parsing error at line xxxx



  • 2.  Re: How to validate XML Request/Response - Parsing

    Posted Jun 09, 2016 09:56 AM

    there is no function in DT thru 9.1 that would do this for XML documents. there is an assertion for JSON document schema validation.

     

    I have added my own capability to do xml document schema validation using the Xerces support in groovy as a VSM step

    import javax.xml.transform.Source;

    import javax.xml.transform.stream.StreamSource;

    import javax.xml.validation.Schema;

    import javax.xml.validation.SchemaFactory;

    import javax.xml.validation.Validator;

     

    XMLShema is a variable that holds the text of the XSD file

    XMLData is a variable that holds the text of the XML document to validate

     

                try

                {

                    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

                    Schema schema = factory.newSchema(new StreamSource(new StringReader(XMLSchema)));

                    Validator validator = schema.newValidator();

     

                    Source source = new StreamSource(new StringReader(XMLData));

     

                    validator.validate(source);

                    _logger.info("schema check successful");

     

                }

                catch (SAXParseException ex)

    etc..

               

     

    in XML, the schema is an XSD. I wrote a java utility to extract the XSD from the WSDL so that the users would not have to do this.



  • 3.  Re: How to validate XML Request/Response - Parsing

    Posted Jun 09, 2016 10:41 AM

    Sam's approach is clean.  +1 to that...

     

    As an alternative, I have seen the following approach used to validate the request side XML.

    In the VSM,

    1) add an Ensure XML Validation Assertion

    2) add a secondary VSI Selection Step that invokes a VSI having only an SI Stateless Response Unknown response that contains the desired failure response

    3) if the assertion fires, branch to the secondary VSI, select and send the invalid XML message back to the caller

    You could probably send the response body as the source to validate the response side.

    01_Validation_Assertion.JPG

     

    02_Validation_Assertion.JPG

    The secondary VSI response might be:

    03_Validation_Image.JPG



  • 4.  Re: How to validate XML Request/Response - Parsing

    Posted Jun 09, 2016 11:10 AM

    Thanks Joel, I guess I didn't see that assertion, or it didn't look like what I was looking for..

     

    I see it does provide for schema checking too.. and uses the wsdl as input..

     

    as a general rule, I dislike having these things as assertions on other steps.. it hides behaviors from new developers..

    its a continuing tradeoff  between maintainability and functionality



  • 5.  Re: How to validate XML Request/Response - Parsing

    Posted Jun 09, 2016 11:41 AM

    Hi Joel

     

    Actually in my case I am done with virtualization  part. But now I am building one utility that will just take request from Excel and will hit deployed service on VSE and will put back the response to excel file. Just a log utility for sanity whenever deployment is done for new release or in new environment.

     

    I just want to validate this input XML(Request) and Output (response) such that it will give me parse error if invalid.

     

    I have only request response pairs with me no WSDL.

    Please let me know if that will work for me . If yes hen I just need  kick off to begin.



  • 6.  Re: How to validate XML Request/Response - Parsing
    Best Answer

    Posted Jun 09, 2016 11:52 AM

    I have not tried the approach you are asking about; however, you should be able to add the assertion after your test case reads the input request.  Since you have no WSDL, uncheck Schema, and uncheck all Schema locations.  The only box that is checked is:  Well Formed XML.  Branch to your error logic on failure. 

     

    Repeat the process for the response after the call is made.  Perhaps, filter the response XML into a property. 

     

    The "source" field in the Assertion is a property (e.g., {{yourRequestXML}} or {{yourResponseXML}}.  You could place the assertion inside a Do Nothing Step by itself and then name the step as Validate Request XML to help the readability of the test case or insert it on the appropriate step.  That decision would be up to you.



  • 7.  Re: How to validate XML Request/Response - Parsing

    Posted Jun 09, 2016 12:03 PM

    Yeah I am saving request response in a separate property for better understanding .

     

    Looks like that will surely work.

     

    I will give that a try and get back to you .

     

    Thanks a ton !



  • 8.  Re: How to validate XML Request/Response - Parsing

    Posted Jun 11, 2016 03:13 AM

    Thanks Joel

    It Worked for me.

     

    I have made one scripted step (Exception handling) that will handle this exception (when False) according to my requirement.

    Now I am looking for script for writing the response to same Excel file from which request will be fetched just in a cell next to it.

     

    Looks like there is inbuilt steps in latest version to write to excel but I am using Lisa 6.x.x that does not supports one.

     

    XMLAssrtion1.pngXMLAssrtion2.png



  • 9.  Re: How to validate XML Request/Response - Parsing

    Posted Jun 09, 2016 11:36 AM

    Thanks for your reply along with code snippet

     

    I have to try that once and will get back if faced any issue because for the time I have just used simple javascript for validations and not worked with external libraries.

     

    Do I need to import any jar files specific to this.



  • 10.  Re: How to validate XML Request/Response - Parsing

    Posted Jul 14, 2016 03:17 PM

    sdetweil2 under XMLSchema are passing full list of xsds ?

     

    for example if we have many operation in same virtual service and different xsds then how can we pass all ? or can we pass wsdl in the code to validate the schema.. is it possible ?



  • 11.  Re: How to validate XML Request/Response - Parsing

    Posted Jul 14, 2016 06:47 PM

    I wrote a small wsdl to XSD converter to get the XSD out of the WSDL.