Forum Discussion

Mis3's avatar
Mis3
Frequent Contributor
4 years ago

How to assign attributes of a node to variables to output to a log file?

Not sure how to assign the attributes of a node to variables so I can output to a log file.  Please help.

The request contains 8 API and the result contains 9 responses (1 for the whole request and 8 individual responses of each API).

Below are the full response, the Groovy and the log.info.

Since I will have to process the teststep thousands of times with different data, the log file will be too big.   I am hoping to assign MESSAGE1 to the status of API-1, MESSAGE2 to the status of API-2 and so on.  This way, I can manipulate these MESSAGES to log.info as well as a external report file, preferably one line per teststep.

Something like:

Thu Jun 24 01:38:58 EDT 2021:INFO: Seq=0 302330000006120, 12313140097 FlowId: 1 Status: 0,  FlowId: 2 Status: 0, FlowId: 3 Status: 0..... 

Thu Jun 24 01:38:58 EDT 2021:INFO: Seq=0 302330000007120, 12313140098 FlowId: 1 Status: 0,  FlowId: 2 Status: 0, FlowId: 3 Status: 0..... 

Then the external report file:

Seq=0 302330000006120, 12313140097 FlowId: 1 Status: 0,  FlowId: 2 Status: 0, FlowId: 3 Status: 0..... 

Seq=0 302330000007120, 12313140098 FlowId: 1 Status: 0,  FlowId: 2 Status: 0, FlowId: 3 Status: 0..... 

 

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

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

 

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

...


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

log.info " Seq="+i+" "+data[0]+", "+data[1]

//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}"
log.info "${item} ==> FlowId: ${it.@flowId}, Status: ${it."$item".@resultCode}"
}
}
showDetails('changeImsiDataResponse')
showDetails('changeMsisdnDataResponse')
showDetails('changeImsSubscriberDataResponse')
showDetails('changeImsPrivateUserDataResponse')
showDetails('changeImsPublicUserDataResponse')

...

...

 

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

Log.Info

 

Thu Jun 24 01:38:58 EDT 2021:INFO: Seq=0 302330000006120, 12313140097
Thu Jun 24 01:38:58 EDT 2021:INFO:changeImsiDataResponse ==> FlowId: 1, Status: 0
Thu Jun 24 01:38:58 EDT 2021:INFO:changeMsisdnDataResponse ==> FlowId: 2, Status: 0
Thu Jun 24 01:38:58 EDT 2021:INFO:changeImsSubscriberDataResponse ==> FlowId: 3, Status: 0
Thu Jun 24 01:38:58 EDT 2021:INFO:changeImsSubscriberDataResponse ==> FlowId: 8, Status: 0
Thu Jun 24 01:38:58 EDT 2021:INFO:changeImsPrivateUserDataResponse ==> FlowId: 4, Status: 0
Thu Jun 24 01:38:58 EDT 2021:INFO:changeImsPublicUserDataResponse ==> FlowId: 5, Status: 0
Thu Jun 24 01:38:58 EDT 2021:INFO:changeImsPublicUserDataResponse ==> FlowId: 6, Status: 0
Thu Jun 24 01:38:58 EDT 2021:INFO:changeImsPublicUserDataResponse ==> FlowId: 7, Status: 0

  • Mis3 

    Here you go:

     

     

    If that is output you need? Then please use

     

     

    //Assuming you have data array, variable i, and xmlString
    //Apart from that you can remove everything from the script
    def line = ["Seq=${i}", data[0], data[1] ]
    line << new XmlSlurper().parseText(xmlString).'**'.findAll {it.name() == 'sdmDataResponse' }.collectMany{ ["FlowId=${it.@flowId}", "Status=${it.children()[0].@resultCode}" ]}
    log.info line.flatten() 

     

  • I thought I saw those brackets from your response.

    Just change
    line.flatten()
    To
    line.flatten().join(',')
  • nmrao's avatar
    nmrao
    Champion Level 3

    Couple of things

    - in the previous question, it was answer how to access the values of node attributes using @ for ex, @resultCode, @message

    - In the response provided, there is no where values available 302330000007120, 12313140098 etc

    - Understand that you might to print the data in certain format. Please show the expected data to be logged from given response. I case see you mentioned print in online. But can't understand MESSAGE1, 2 etc.,

    - In the given response, only flowId is different, and resultCode and message are same.

    • Mis3's avatar
      Mis3
      Frequent Contributor

      My comments inline below.

      The reason of this Groovy script is I have to read from an external file with hundreds of entries and execute a teststep with each entry. 

      As it stands now, there are at least 8 lines per execution of the teststep.   

      I am hoping to find a way to "flatten" the report file.  I can then import this report file in EXCEL and filters out the failed API.

       

      - in the previous question, it was answer how to access the values of node attributes using @ for ex, @resultCode, @message

      -->  Yes, I have learned how to read the node attributes.  I like to learn how to organize these results to a report file.

       

      - In the response provided, there is no where values available 302330000007120, 12313140098 etc

      -->  302330000007120, 12313140098 are the data the Groovy read from an external file.

      -->  I added this line  log.info " Seq="+i+" "+data[0]+", "+data[1]  to identify each data entry.

      -->  The CSV file:

      302330000006120, 12313140097

      302330000007120, 12313140098

       

      - Understand that you might to print the data in certain format. Please show the expected data to be logged from given response. I case see you mentioned print in online. But can't understand MESSAGE1, 2 etc.,

      -->  I am hoping to equate:

      MESSAGE1 to  @resultCode of FlowId=1 (changeImsiDataRequest)

      MESSAGE2  to @resultCode of FlowId=1 (changeMsisdnDataRequest)

      etc.

      -->  This way, I can use this line << "\n"+ "Seq="+i+" "+sdf.format(log_date)+" "+data[0]+" "+data[1]+" Result: ["+MESSAGE1+"] "+MESSAGE2

      to output to the report file like:

      Seq=0 06/24/2021 08:55:43 302330000006120 12313140097 Result: FlowId=1, Status=0, FlowId=2, Staus=0

      Seq=0 06/24/2021 08:55:43 302330000007120 12313140099 Result: FlowId=1, Status=0, FlowId=2, Staus=0

       

      - In the given response, only flowId is different, and resultCode and message are same.

      -->   resultCode can be different.  0=Success, 30=Inconsistent data, 40=record not found

       

      • nmrao's avatar
        nmrao
        Champion Level 3
        Still not sure about adding data from csv. But that ok. Probably you will be able to handle it if the other part is available.
        I am under impression that the response might be partial. So are those values you send them in request?
        Thanks for your responses.