Forum Discussion

shiva1020243's avatar
shiva1020243
Occasional Contributor
3 years ago
Solved

How to write a groovy script to aggregate amount and validate with total amount

Hi Team,

can somebody help me how can i solve this problem? i tried couple of scenarios but none of them worked..

 

so this API will return the response like this for each account based on the month in which we are hitting the API, in this case it is showing me data from Jan to Nov, if we hit API in December we get data from Jan to Dec (this will change based on month), now my question is how can i write a script to validate like i should take jan month amount, Feb month amount etc like Dec month amount and add them up and validate with total Amount .. how can do that? 

 

Sample API response - 

 

{
"totalAmount": 20,
"ClientInfo": [
{
"acc": "9521668512",
"type": "savings",
"totalList": [
{
"date": "Nov 2021",
"Amount": 1
},
{
"date": "Oct 2021",
"Amount": 1
},
{
"date": "Sep 2021",
"Amount": 3
},
{
"date": "Aug 2021",
"Amount": 2
},
{
"date": "Jul 2021",
"Amount": 2
},
{
"date": "Jun 2021",
"Amount": 1
},
{
"date": "May 2021",
"Amount": 0
},
{
"date": "Apr 2021",
"Amount": 0
},
{
"date": "Mar 2021",
"Amount": 0
},
{
"date": "Feb 2021",
"Amount": 0
},
{
"date": "Jan 2021",
"Amount": 0
},

{
"acc": "123521362572",
"type": "savings",
"totalList": [
{
"date": "Nov 2021",
"Amount": 1
},
{
"date": "Oct 2021",
"Amount": 1
},
{
"date": "Sep 2021",
"Amount": 2
},
{
"date": "Aug 2021",
"Amount": 6
},
{
"date": "Jul 2021",
"Amount": 0
},
{
"date": "Jun 2021",
"Amount": 0
},
{
"date": "May 2021",
"Amount": 0
},
{
"date": "Apr 2021",
"Amount": 0
},
{
"date": "Mar 2021",
"Amount": 0
},
{
"date": "Feb 2021",
"Amount": 0
},
{
"date": "Jan 2021",
"Amount": 0
}
]
}
]
}

  • Hello shiva1020243 ,

     

    Your sample json was not syntactically correct, so I had to make some assumptions on how it would be formatted from an actual response.  Below is groovy test step code that does what I think your question asked.

     

    Regards,

    Todd

    import groovy.json.JsonSlurper;
    
    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" start...';
    log.info "";
    
    def String jsonStr = """
    {
    	"totalAmount": 20,
    	"ClientInfo": [
    		{
    			"acc": "9521668512",
    			"type": "savings",
    			"totalList": [
    				{
    					"date": "Nov 2021",
    					"Amount": 1
    				},
    				{
    					"date": "Oct 2021",
    					"Amount": 1
    				},
    				{
    					"date": "Sep 2021",
    					"Amount": 3
    				},
    				{
    					"date": "Aug 2021",
    					"Amount": 2
    				},
    				{
    					"date": "Jul 2021",
    					"Amount": 2
    				},
    				{
    					"date": "Jun 2021",
    					"Amount": 1
    				},
    				{
    					"date": "May 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Apr 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Mar 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Feb 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Jan 2021",
    					"Amount": 0
    				}
    			]
    		},
    		{
    			"acc": "123521362572",
    			"type": "savings",
    			"totalList": [
    				{
    					"date": "Nov 2021",
    					"Amount": 1
    				},
    				{
    					"date": "Oct 2021",
    					"Amount": 1
    				},
    				{
    					"date": "Sep 2021",
    					"Amount": 2
    				},
    				{
    					"date": "Aug 2021",
    					"Amount": 6
    				},
    				{
    					"date": "Jul 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Jun 2021",
    					"Amount": 0
    				},
    				{
    					"date": "May 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Apr 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Mar 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Feb 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Jan 2021",
    					"Amount": 0
    				}
    			]
    		}
    	]
    }
    """;
    
    def jsonSlurper = new JsonSlurper();
    def jsonObj = jsonSlurper.parseText(jsonStr);  
    log.info "jsonObj=$jsonObj";
    
    def totalAmt = jsonObj.totalAmount;
    log.info "totalAmt=$totalAmt";
    
    aggregateAmt = 0;
    jsonObj.ClientInfo.each { account ->
       log.info "";
       log.info "account=${account}";
       account.totalList.each {month ->
          log.info "month=$month";
          aggregateAmt = aggregateAmt + month.Amount;
       };
    };
    log.info "aggregateAmt=$aggregateAmt";
    
    assert totalAmt == aggregateAmt, "Expected totalAmount ($totalAmt) to be equal to summed content ($aggregateAmt), but it was not";
          
    log.info "";
    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" done...';

3 Replies

  • TNeuschwanger's avatar
    TNeuschwanger
    Champion Level 1

    Hello shiva1020243 ,

     

    Your sample json was not syntactically correct, so I had to make some assumptions on how it would be formatted from an actual response.  Below is groovy test step code that does what I think your question asked.

     

    Regards,

    Todd

    import groovy.json.JsonSlurper;
    
    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" start...';
    log.info "";
    
    def String jsonStr = """
    {
    	"totalAmount": 20,
    	"ClientInfo": [
    		{
    			"acc": "9521668512",
    			"type": "savings",
    			"totalList": [
    				{
    					"date": "Nov 2021",
    					"Amount": 1
    				},
    				{
    					"date": "Oct 2021",
    					"Amount": 1
    				},
    				{
    					"date": "Sep 2021",
    					"Amount": 3
    				},
    				{
    					"date": "Aug 2021",
    					"Amount": 2
    				},
    				{
    					"date": "Jul 2021",
    					"Amount": 2
    				},
    				{
    					"date": "Jun 2021",
    					"Amount": 1
    				},
    				{
    					"date": "May 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Apr 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Mar 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Feb 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Jan 2021",
    					"Amount": 0
    				}
    			]
    		},
    		{
    			"acc": "123521362572",
    			"type": "savings",
    			"totalList": [
    				{
    					"date": "Nov 2021",
    					"Amount": 1
    				},
    				{
    					"date": "Oct 2021",
    					"Amount": 1
    				},
    				{
    					"date": "Sep 2021",
    					"Amount": 2
    				},
    				{
    					"date": "Aug 2021",
    					"Amount": 6
    				},
    				{
    					"date": "Jul 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Jun 2021",
    					"Amount": 0
    				},
    				{
    					"date": "May 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Apr 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Mar 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Feb 2021",
    					"Amount": 0
    				},
    				{
    					"date": "Jan 2021",
    					"Amount": 0
    				}
    			]
    		}
    	]
    }
    """;
    
    def jsonSlurper = new JsonSlurper();
    def jsonObj = jsonSlurper.parseText(jsonStr);  
    log.info "jsonObj=$jsonObj";
    
    def totalAmt = jsonObj.totalAmount;
    log.info "totalAmt=$totalAmt";
    
    aggregateAmt = 0;
    jsonObj.ClientInfo.each { account ->
       log.info "";
       log.info "account=${account}";
       account.totalList.each {month ->
          log.info "month=$month";
          aggregateAmt = aggregateAmt + month.Amount;
       };
    };
    log.info "aggregateAmt=$aggregateAmt";
    
    assert totalAmt == aggregateAmt, "Expected totalAmount ($totalAmt) to be equal to summed content ($aggregateAmt), but it was not";
          
    log.info "";
    log.info 'Test Step "' + testRunner.runContext.currentStep.name + '" done...';
  • Are we trying to sum the "amounts" node or simply validate some nodes produce 0 while others produce a certain number.

    If we create the sum of the nodes, we wont necessarily know which node has a value and which one does not.