cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy: Fails to Display Request Status

SOLVED
Mis3
Contributor

Groovy: Fails to Display Request Status

I am building a Groovy script to read an external file and execute a testStep one by one.

Somehow log.info can successfully display the data (basicMSISDN, NAM, etc) of the XML response but not the request status.  In log.info, resultCode=Null and message=Null.

Not sure what to do to get the resultCode so I have a log file for tracking.

Thanks.

 

------------------------------------------------------------------------------------------------------------------------------------

My Groovy script:

...

def xmlResponse = tStep.getPropertyValue("Response")
def holder = groovyUtils.getXmlHolder(xmlResponse)

def MESSAGE11 = holder.getNodeValue("/soap:Envelope/soap:Body/ns2:getImsiDataResponse/resultCode")
def MESSAGE12 = holder.getNodeValue("/soap:Envelope/soap:Body/ns2:getImsiDataResponse/message")

def MESSAGE1 = holder.getNodeValue("/soap:Envelope/soap:Body/ns2:getImsiDataResponse/imsiData/basicMsisdn")
def MESSAGE2 = holder.getNodeValue("/soap:Envelope/soap:Body/ns2:getImsiDataResponse/imsiData/serviceProfile/nam")
def MESSAGE3 = holder.getNodeValue("/soap:Envelope/soap:Body/ns2:getImsiDataResponse/imsiData/serviceProfile/roamingProfileId")
def MESSAGE4 = holder.getNodeValue("/soap:Envelope/soap:Body/ns2:getImsiDataResponse/imsiData/serviceProfile/odbProfileId")

log.info " Seq="+i+" "+data[0]+"| Result: "+MESSAGE1+", "+MESSAGE2+", "+MESSAGE3+", "+MESSAGE4+" "+MESSAGE11+" "+MESSAGE12

...

-----------------------------------------------------------------------------------------------------------------------------------------------

Response of the testStep:

<<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

   <soap:Body>

      <ns2:getImsiDataResponse resultCode="0" message="success" xmlns:ns2="http://www.netnumber.com/titan/sdm/soap/types">

         <imsiData imsi="302330000006120">

            <basicMsisdn>14313140097</basicMsisdn>

            <serviceProfile>

               <nam>csps</nam>

               <transferOfSm>cs</transferOfSm>

               <roamingProfileId>320</roamingProfileId>  ...

 

---------------------------------------------------------------------------------------------------------------------------------------------------

1 ACCEPTED SOLUTION

Accepted Solutions
nmrao
Community Hero

Re: Groovy: Fails to Display Request Status

@Mis3 

Oh, I got what you are point now.

 

You are trying to access the attributes of a node. The xpath is incorrect, that is why data is not shown for you.

Attributes are accessed using @Attribute

 

Here is another simple way of accessing xml data using XmlSlurper. If you notice, the data is accessed dot(.) after node name, then dot (.) and node name like how we see.

NOTE: you can replace printf with log.info

nmrao_0-1624370501720.png

 



Regards,
Rao.

View solution in original post

14 REPLIES 14
nmrao
Community Hero

Re: Groovy: Fails to Display Request Status

what is the expected value for what you are asking?
What does it show when the same is executed manually?
Why do you need to run with groovy? what is the use case?


Regards,
Rao.
Mis3
Contributor

Re: Groovy: Fails to Display Request Status

Below is part of the response of the testStep.  The request was successful.   Strange that I can extract the parameters like basicMSISDN, nam, roamingProfile to the log file, the resultCode and message show Null.

 

Response:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:getImsiDataResponse resultCode="0" message="success" xmlns:ns2="http://www.abc.com/titan/sdm/soap/types">
<imsiData imsi="302330000006120">
<basicMsisdn>12313140097</basicMsisdn>
<serviceProfile>
<nam>csps</nam>
<transferOfSm>cs</transferOfSm>
<roamingProfileId>320</roamingProfileId>
...

...

 

Success response:  <ns2:getImsiDataResponse resultCode="0" message="success" xmlns:ns2="http://www.abc.com/titan/sdm/soap/types">

Failed response:   <ns2:getImsiDataResponse resultCode="40" message="objectDoesNotExist" xmlns:ns2="http://www.abc.com/titan/sdm/soap/types"/>

 

The use case is I have to run this getImsiData API many time.  So , I created a Groovy script to read the data from a CSV in my laptop.  The Groovy script will execute the testStep one by one.

 

 

 

 

 

nmrao
Community Hero

Re: Groovy: Fails to Display Request Status

Thanks, but i did not understand what you mean by result code and message? Would you please clarify.?
Possible to show the raw tab of response?


Regards,
Rao.
Mis3
Contributor

Re: Groovy: Fails to Display Request Status

Maybe I am not using the right terminology.

As you can see from the response (3rd line down, the line right after the soap:Body>, the resultCode and message are in this line.   These 2 fields give me the status of the API request.  

 

Success:  <ns2:getImsiDataResponse resultCode="0" message="success" xmlns:ns2="http://www.netnumber.com/titan/sdm/soap/types"> 

Failed:  <ns2:getImsiDataResponse resultCode="40" message="objectDoesNotExist" xmlns:ns2="http://www.netnumber.com/titan/sdm/soap/types"/> 

 

I like to capture this resultCode and message in the log file for tracking.

 

Maybe these 2 lines in my Groovy script does not provide me with the status of the response.

def xmlResponse = tStep.getPropertyValue("Response")
def holder = groovyUtils.getXmlHolder(xmlResponse)

nmrao
Community Hero

Re: Groovy: Fails to Display Request Status

@Mis3 

Oh, I got what you are point now.

 

You are trying to access the attributes of a node. The xpath is incorrect, that is why data is not shown for you.

Attributes are accessed using @Attribute

 

Here is another simple way of accessing xml data using XmlSlurper. If you notice, the data is accessed dot(.) after node name, then dot (.) and node name like how we see.

NOTE: you can replace printf with log.info

nmrao_0-1624370501720.png

 



Regards,
Rao.

View solution in original post

Mis3
Contributor

Re: Groovy: Fails to Display Request Status

Working now.  THANKS, Rao.

 

This is what I did:

def xmlResponse = tStep.getPropertyValue("Response")
def holder = groovyUtils.getXmlHolder(xmlResponse)
def xml = new XmlSlurper().parseText(xmlResponse)

def MESSAGE1 = xml.Body.getImsiDataResponse.@resultCode.text()
def MESSAGE2 = xml.Body.getImsiDataResponse.@message.text()
def MESSAGE3 = holder.getNodeValue("/soap:Envelope/soap:Body/ns2:getImsiDataResponse/imsiData/basicMsisdn")
def MESSAGE4 = holder.getNodeValue("/soap:Envelope/soap:Body/ns2:getImsiDataResponse/imsiData/serviceProfile/nam")
def MESSAGE5 = holder.getNodeValue("/soap:Envelope/soap:Body/ns2:getImsiDataResponse/imsiData/serviceProfile/roamingProfileId")
def MESSAGE6 = holder.getNodeValue("/soap:Envelope/soap:Body/ns2:getImsiDataResponse/imsiData/serviceProfile/odbProfileId")
//   def MESSAGE6 = xml.Body.getImsiDataResponse.imsiData.serviceProfile.odbProfileId.text()

 

log.info " Seq="+i+" "+data[0]+" Result: "+MESSAGE1+", "+MESSAGE2+", "+MESSAGE3+", "+MESSAGE4+" "+MESSAGE5+" "+MESSAGE6
report << "\n"+"Seq="+i+" "+sdf.format(log_date)+"|"+data[0]+"|Result: "+MESSAGE1+" "+MESSAGE2+" "+MESSAGE3+" "+MESSAGE4+ " "+MESSAGE5+" "+MESSAGE6

 

I noticed that if a parameter does not exist in the response:

if I use holder.getNodeValue, MESSAGE6 would display "null".  (I prefer this, more precise)

if I use xml.Body..., MESSAGE6 would display nothing.

Is this normal?

 

nmrao
Community Hero

Re: Groovy: Fails to Display Request Status

xml.Body contain another node, but not node value.


Regards,
Rao.
Mis3
Contributor

Re: Groovy: Fails to Display Request Status

Thanks again, Rao.

 

My API script contains a few XML requests and below is the response:

 

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:changeSdmDataResponse resultCode="0" message="success" xmlns:ns2="http://www.abcnumber.com/titan/sdm/soap/types">
<sdmDataResponse flowId="1">
<ns2:changeImsiDataResponse resultCode="0" message="success"/>
</sdmDataResponse>
<sdmDataResponse flowId="2">
<ns2:changeMsisdnDataResponse resultCode="0" message="success"/>
</sdmDataResponse>
<sdmDataResponse flowId="3">
<ns2:changeImsSubscriberDataResponse resultCode="0" message="success"/>
</sdmDataResponse>
<sdmDataResponse flowId="4">
<ns2:changeImsPrivateUserDataResponse resultCode="0" message="success"/>
</sdmDataResponse>
<sdmDataResponse flowId="5">
<ns2:changeImsPublicUserDataResponse resultCode="0" message="success"/>
</sdmDataResponse>
<sdmDataResponse flowId="6">
<ns2:changeImsPublicUserDataResponse resultCode="0" message="success"/>
</sdmDataResponse>
<sdmDataResponse flowId="7">
<ns2:changeImsPublicUserDataResponse resultCode="0" message="success"/>
</sdmDataResponse>
<sdmDataResponse flowId="8">
<ns2:changeImsSubscriberDataResponse resultCode="0" message="success"/>
</sdmDataResponse>
</ns2:changeSdmDataResponse>
</soap:Body>

 

My Groovy:

...

...

def xmlResponse = tStep.getPropertyValue("Response")
def holder = groovyUtils.getXmlHolder(xmlResponse)
def xml = new XmlSlurper().parseText(xmlResponse)

def MESSAGE1 = xml.Body.changeSdmDataResponse.@resultCode.text()
def MESSAGE2 = xml.Body.changeSdmDataResponse.sdmDataResponse.changeImsiDataResponse.@resultCode.text()
def MESSAGE3 = xml.Body.changeSdmDataResponse.sdmDataResponse.changeMsisdnDataResponse.@resultCode.text()
def MESSAGE4 = xml.Body.changeSdmDataResponse.sdmDataResponse.changeImsSubscriberDataResponse.@resultCode.text()
def MESSAGE5 = xml.Body.changeSdmDataResponse.sdmDataResponse.changeImsPrivateUserDataResponse.@resultCode.text()
def MESSAGE6 = xml.Body.changeSdmDataResponse.sdmDataResponse.changeImsPublicUserDataResponse.@resultCode.text()

log.info " Seq="+i+" "+data[0]+", "+data[1]+" Result: ["+MESSAGE1+"] "+MESSAGE2+" "+MESSAGE3+" "+MESSAGE4+" "+MESSAGE5+" "+MESSAGE6

...

...

 

In the request, I have to execute changeImsPublicUserData 3 times and changeImsSubscriberDataResponse 2 times (in a specific sequence but with different values).

Log record:   Tue Jun 22 21:05:02 EDT 2021:INFO: Seq=0 302330000006120, 12313140097 Result: [0] 0 0 00 0 000

In the response, since the 3 changeImsPublicUserData requests were all successfully, the log file show "000" (Zero means success).

 

Since there is no distinction of the 3 responses of changeImsPublicUserData, I guess there is no way to display it in a better way, right?

Ideally, I like to see Result: [0] 0 0 0 0 0 0 0 0  or Result: [0] 0 0 0-0 0 0-0-0

Any suggestions?

 

 

nmrao
Community Hero

Re: Groovy: Fails to Display Request Status

@Mis3 

 

Here is little optimized, but it may need you to spend little time to understand. Groovy code is easily readable though.

 

 

//Parse response
def xml = new XmlSlurper().parseText(xmlResponse)

//Closure to show the details of the item as input data
def showDetails = { item ->
    def sdms = xml.'**'.findAll {it.name() == item }*.parent()
    sdms.each { 
        log.info "${item} ==> FlowId: ${it.@flowId}, ResultCode: ${it."$item".@resultCode}, Message: ${it."$item".@message}"
    }    
}
//show all the changeImsPublicUserDataResponse
showDetails('changeImsPublicUserDataResponse')

//show all the changeImsSubscriberDataResponse
showDetails('changeImsSubscriberDataResponse')

 

.



Regards,
Rao.
New Here?
Join us and watch the welcome video:
Top Kudoed Authors