Ask a Question

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

SOLVED
Mis3
Frequent Contributor

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

9 REPLIES 9
nmrao
Community Hero

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.



Regards,
Rao.
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
Community Hero

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.


Regards,
Rao.
nmrao
Community Hero

@Mis3 

Here you go:

 

nmrao_0-1624613651527.png

 

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

 



Regards,
Rao.
Mis3
Frequent Contributor

Thanks, Rao. 

I did not copy in the full Groovy because if the post is too long, it would be hard to read.   This time, I have create a small document to include the full request, Groovy script, test input file, log.info and the external log file.

 

I copied the code from your previous post and it works well.

I see 2 minor issues:

1. In the output, I am missing the resultCode of the whole request; ie: changeSdmDataResponse  (3rd line from the top)

2. In the report file, there is no line break.  (your code looks complicated, I do not want to touch it"

nmrao
Community Hero

- May be I am not aware, so it got missed

- You added report statement, it should have been the same as previous log.info

- Cleaned up lot of unwanted code. But I can't try. Sorry if I mistakenly removed something. You can add back in that case.

- Saw many places as below:

log.info "Some text" + variable + "rest of the text"

 

This above can be changed to include the variables as ${variable} and remove + symbols. This is more expressive, easy to read, removes the noise.

 

log.info "Some text  ${variable} rest of the text"

 

Attaching the changed file.

 



Regards,
Rao.
Mis3
Frequent Contributor

Your script works well.

Is there any way I can remove the "[" and "]" in the outputs (both log.info and report)?

(Groovy script attached)

 

Log.info:

Fri Jun 25 13:39:56 EDT 2021:INFO:[Seq=0, 302130000006120, 14313140097, Status=0, FlowId=1, Status=0, FlowId=2, Status=0, FlowId=3, Status=0, FlowId=4, Status=0, FlowId=5, Status=0, FlowId=6, Status=0, FlowId=7, Status=0, FlowId=8, Status=0]
Fri Jun 25 13:39:57 EDT 2021:INFO:[Seq=1, 302130000006120, 14313140097, Status=0, FlowId=1, Status=0, FlowId=2, Status=0, FlowId=3, Status=0, FlowId=4, Status=0, FlowId=5, Status=0, FlowId=6, Status=0, FlowId=7, Status=0, FlowId=8, Status=0]

Report:

 

SDM-Activate Start: 25_06_2021_13_45_54

[Seq=0, 302130000006120, 14313140097, Status=0, FlowId=1, Status=0, FlowId=2, Status=0, FlowId=3, Status=0, FlowId=4, Status=0, FlowId=5, Status=0, FlowId=6, Status=0, FlowId=7, Status=0, FlowId=8, Status=0,
]
[Seq=1, 302130000006120, 14313140097, Status=0, FlowId=1, Status=0, FlowId=2, Status=0, FlowId=3, Status=0, FlowId=4, Status=0, FlowId=5, Status=0, FlowId=6, Status=0, FlowId=7, Status=0, FlowId=8, Status=0,
]

 

nmrao
Community Hero

I thought I saw those brackets from your response.

Just change
line.flatten()
To
line.flatten().join(',')


Regards,
Rao.
Mis3
Frequent Contributor

Perfect.   Although I do not understand your code (one day I will), the script works great.

Below is the new report.  When I run the campaign for a thousand entries, I can import the report file into EXCEL and easily identify the failures.

Thanks.

 

Seq=0 302330000006120 13313140097 Status=0 FlowId=1 Status=0 FlowId=2 Status=0 FlowId=3 Status=0 FlowId=4 Status=0 FlowId=5 Status=0 FlowId=6 Status=0 FlowId=7 Status=0 FlowId=8 Status=0
Seq=1 302330000007120 13313140098 Status=0 FlowId=1 Status=0 FlowId=2 Status=0 FlowId=3 Status=0 FlowId=4 Status=0 FlowId=5 Status=0 FlowId=6 Status=0 FlowId=7 Status=0 FlowId=8 Status=0 

cancel
Showing results for 
Search instead for 
Did you mean: