Forum Discussion

shiva1020243's avatar
shiva1020243
Occasional Contributor
4 years ago

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 mon...
  • TNeuschwanger's avatar
    4 years ago

    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...';