Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Need Help with Handling Request XML Data

satishms10
satishms10 Posts: 22

Hi There,

Hope you are doing well.

I am developing a virtual service in Parasoft and the sample Request XML is below

<?xml version="1.0" encoding="UTF-8"?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> </s:Header> <s:Body> <WorkItem> <IncomingData> <Enquiry> <EnquiryUId>3</EnquiryUId> <EnquiryBasics> <EnquiryBasicExtra> <DataItemName>FA_INDICATOR</DataItemName> <DataItemValue>N</DataItemValue> </EnquiryBasicExtra> </EnquiryBasics> <Parties> <Party> <ID>10</ID> </Party> <Party> <ID>70</ID> </Party> </Parties> </Enquiry> </IncomingData> </WorkItem> </s:Body> </s:Envelope>

My requirement is, I want to validate the ID field value from incoming Request XML

  1. If the value is <30 or > 100, replace the value in Request XML to 90 (to default)
  2. If the value is 30, I need to respond ERROR-1 xml
  3. If the value is 80, I need to respond ERROR-2 xml
  4. If the value is >= 30 and <= 100, I need to respond SUCCESS xml

As a solution, I have added two XML responders.

  1. One for two ERROR scenarios with responder correlation condition as ID = 30 OR ID = 80
  2. One for SUCCESS scenario with responder correlation condition as ID NOT = 30 OR ID NOT = 80

I am looking for some help to replace the ID field value to 90 in Request XML if the value is <30 or > 100. I tried with below groovy script but not able to succeed. Could someone help me with groovy script? I am adding this code under "Extension Tool"

`import com.parasoft.api.*

def replaceString(input,context) {

Application.showMessage("\n=========In replaceString()=========");

def req = new XmlSlurper().parseText(input);
Application.showMessage("Testing:" + req.Envelope.Body.WorkItem.IncomingData.Enquiry.Parties.Party[1].ID.text());


id = req.Envelope.Body.SubmitWorkItem.SourceData.Enquiry.Parties.Party[1].ID.text();

if (id < 30) or (id > 90) {

    input = input.replace(id,"50");
}
return "0";

}`

Thanks,
Sati

Best Answer

  • benken_parasoft
    benken_parasoft Posts: 1,290 ✭✭✭
    Answer ✓

    In XPath, operators like '<' and '>' are used to compare numeric values. Something like "1990-12-19" is not a number but a string. Yes, I was going to suggest that you could use the translate function to strip out the '-' characters in order to make them numbers.

Answers

  • benken_parasoft
    benken_parasoft Posts: 1,290 ✭✭✭

    What are you ultimately wanting to do with the value of the ID field from the request? Did you want to return it somewhere in the body of the response or use the value for data source correlation? There is probably a better place to translate the value or test its range depending on how or where you intend to use the ID value.

  • satishms10
    satishms10 Posts: 22

    Hi Benken,

    I am going to use the ID value for data source correlation.

    Thanks,
    Sati

  • benken_parasoft
    benken_parasoft Posts: 1,290 ✭✭✭

    For data source correlation, you could use an XPath expression like this:

    let $id := /*:Envelope/*:Body[1]/WorkItem[1]/IncomingData[1]/Enquiry[1]/Parties[1]/Party[1]/ID[1]/text()
    return if ($id < 30 or $id > 100)
      then 90
      else $id
    
  • satishms10
    satishms10 Posts: 22

    Thank you so much Benken, your code worked well for ID field as it is numeric field.

    I am trying to apply same logic for Date of Birth field as using below code

    let $inDOB := /*:Envelope/*:Body/*:SubmitWorkItem/*:oSource/*:SourceData/*:Enquiry/*:Parties/*:Party[1]/*:PartyBasics/*:PartyBasic/*:DateOfBirth/text() return if (inDOB < "1990-12-19" or inDOB > "1990-12-28" or inDOB = null or inDOB = "") then "1990-01-01" else $inDOB

    But I am getting below error when request contains 1990-12-30 value for DateOfBirth

    <Error> <Reason>Failed to respond to incoming message using data source row correlation</Reason> <Details>Values in incoming message did not match values in the data source "TestTable"</Details> <Values> <Value DataSourceColumnName="DateOfBirth"> <XPath path="let $inDOB := /*:Envelope/*:Body/SubmitWorkItem/SourceData/Enquiry/Parties/Party[1]/DateOfBirth/text()&#xd;&#xa;return if (inDOB &lt; 1990-12-02 or inDOB > 1990-12-22 or inDOB = null or inDOB = '')&#xd;&#xa; then &quot;1990-01-01&quot;&#xd;&#xa; else $inDOB">1990-12-30</XPath> </Value> </Values> </Error>

    I tried with single quotes and without quotes also in below condition but still the same error :(

    Could you please suggest me where I am making mistake in my code?

    Thanks,
    Sati

  • satishms10
    satishms10 Posts: 22

    Hi Benken,

    Below xpath script worked for me, however, please let me know if any better method is there to compare the dates

    let $partyDOB := translate(/*:Envelope/*:Body/../*:DateOfBirth/text(),'-','') return if ($partyDOB < "19901219" or $partyDOB > "19901228") then xs:date("1990-01-01") else /*:Envelope/*:Body/../*:DateOfBirth/text(),'-',''

    Thanks,
    Sati

  • satishms10
    satishms10 Posts: 22

    Thank you so much for your help Benken, translate function and comparison worked for me.

    Thanks,
    Sati