Forum Discussion

Mis3's avatar
Mis3
Frequent Contributor
3 years ago
Solved

if multiple attributes in a XML response, how to separate them in log.info?

I am working on yet another Groovy script to send the same testStep to an external source with different properties (read from a CSV file).

I like to improve on the log report.   

Below are a sample of the log.info, the Groovy to parse the response and the actual XML response.  Since the service.code repeat itself a few times in the XML response, the Groovy would concatenate them altogether in one string.   In the response, there are 4 service codes: DATA, Roaming, VoLTE and SOS.  In log.info, the Groovy outputs DATAROAMINGVoLTESOS.  Similarly for the avp, since the response consists of a few avp, the Groovy again concatenates them together into one string.

Is there any way I can ask the Groovy to separate them by spaces or commas (like DATA ROAMING VoLTE SOS)?

 

Sat Jul 10 18:59:00 EDT 2021:INFO: Seq=4 13313140097 Result: PTPT 13313140097 DATAROAMINGVoLTESOS profileKeyXMNORMALprofileKey1XM200profileKey1XM201blockStatus98blockStatus99

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

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

 

def MESSAGE1 = xml.Body.GetSubscriberResponse.subscriber.name.fullName.text()
def MESSAGE2 = xml.Body.GetSubscriberResponse.subscriber.credential.networkId.text()
def MESSAGE3 = xml.Body.GetSubscriberResponse.subscriber.service.code.text()
def MESSAGE4 = xml.Body.GetSubscriberResponse.subscriber.avp.text()

 

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

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

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

   <se:Body>

      <GetSubscriberResponse xmlns="http://broadhop.com/unifiedapi/soap/types">

         <errorCode>0</errorCode>

         <errorMessage>Request completed successfully</errorMessage>

         <subscriber>

            <id>00650000cc4fbd3860ca5c83</id>

            <name>

               <fullName>PTPT</fullName>

            </name>

            <credential>

               <networkId>13313140097</networkId>

               <type>MSISDN</type>

            </credential>

            <service>

               <code>DATA</code>

               <enabled>true</enabled>

            </service>

            <service>

               <code>ROAMING</code>

               <enabled>true</enabled>

            </service>

            <service>

               <code>VoLTE</code>

               <enabled>true</enabled>

            </service>

            <service>

               <code>SOS</code>

               <enabled>true</enabled>

            </service>

            <status>ACTIVE</status>

            <role>READ_ALL</role>

            <billingInfo/>

            <avp>

               <code>profileKey</code>

               <value>XMNORMAL</value>

            </avp>

            <avp>

               <code>profileKey1</code>

               <value>XM200</value>

            </avp>

            <avp>

               <code>profileKey1</code>

               <value>XM201</value>

            </avp>

            <avp>

               <code>blockStatus</code>

               <value>98</value>

            </avp>

            <avp>

               <code>blockStatus</code>

               <value>99</value>

            </avp>

            <version>39</version>

         </subscriber>

      </GetSubscriberResponse>

   </se:Body>

</se:Envelope>

  • Mis3 

     

    I believe that we have to first understand  how things work rather than saying "did not work".

     

    Each solution would depend on the data.

     

    You can try this excerpt

    Note, this is not full script

     

     

     

    def xml = new XmlSlurper().parseText(xmlResponse)
    def getData = { childElement, parentElement = null ->
    	if (parentElement) {
    		xml.'**'.findAll {it.name() == parentElement}."$childElement"*.text()
    	} else {
    		xml.'**'.findAll {it.name() == childElement}*.text()
    	}
    }
    def list = ["Seq=${i}", data[0], "Status=${getData('errorCode')}", getData('fullName'), getData('networkId'), getData('code', 'service'), getData('value', 'avp')]
    log.info list.flatten().join(' ')

     

     

     

    log.info => this statement any ways  shows the data time. You don't have to explicit add the date time again.

     

    EDIT:

    Haved edited the script based on your latest reply. Please the script one more time and try.

8 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    please refer previous solutions provided, there it has.
    • Mis3's avatar
      Mis3
      Frequent Contributor

      I did copy some codes from the previous project (with FlowID).  I tried various forms of  findAll, collectMany but it did not work.

      Below is the last one I tried.  While there was no longer syntax error, it outputs nothing more than the errorCode of the API.  I hope to output all the Service,code and avp.code separated by space or comma.

       

      ....

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

      line = ["Seq=${i}", data[0], "Status=${xml.Body.GetSubscriberResponse.errorCode}" ]
      line << xml.'**'.findAll {it.name() == 'GetSubscriberResponse.subscriber.service'}.collectMany{ "${it.children()[0].@code.text()}" }
      log.info line.flatten().join(' ')

      ...

       

      Log.info:

      Mon Jul 12 11:25:45 EDT 2021:INFO:Seq=0 14313140097 Status=0

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

      XML response:

       

      <se:Envelope xmlns:se="http://schemas.xmlsoap.org/soap/envelope/">
      <se:Body>
      <GetSubscriberResponse xmlns="http://broadhop.com/unifiedapi/soap/types">
      <errorCode>0</errorCode>
      <errorMessage>Request completed successfully</errorMessage>
      <subscriber>
      <id>00950000b11e343960ebbaa8</id>
      <name>
      <fullName>John-Pierre</fullName>
      </name>
      <credential>
      <networkId>14313140097</networkId>
      <type>MSISDN</type>
      </credential>
      <service>
      <code>DATA</code>
      <enabled>true</enabled>
      </service>
      <service>
      <code>ROAMING</code>
      <enabled>true</enabled>
      </service>
      <service>
      <code>VoLTE</code>
      <enabled>true</enabled>
      </service>
      <service>
      <code>SOS</code>
      <enabled>true</enabled>
      </service>
      <status>ACTIVE</status>
      <billingInfo/>
      <avp>
      <code>profileKey</code>
      <value>NORMAL</value>
      </avp>
      <avp>
      <code>profileKey1</code>
      <value>XM200</value>
      </avp>
      <avp>
      <code>profileKey1</code>
      <value>XM201</value>
      </avp>
      <version>1</version>
      </subscriber>
      </GetSubscriberResponse>
      </se:Body>
      </se:Envelope>

       

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