lonelydev101
lonelydev101

Reputation: 1911

SOAPUI assert json response if equals to json

I have in my TestCase 'CheckResult' where I am calling my service, and I have set of 36 same pieces like this:

{
 "data": [
    {
        "idCalculation": 111,
        "idCalculationResult": 707,
        "exchangeRate": 120.3,
        "interestRate": 4.2,
        "anuityDomesticCurrencyAmount": 165669.9171,
        "anuityForeignCurrencyAmount": 1377.1397,
        "totalDomesticCurrencyAmount": 11554978.0125,
        "totalForeignCurrencyAmount": 96051.355,
        "dti": 0.6036
    },
    {
        "idCalculation": 111,
        "idCalculationResult": 708,
        "exchangeRate": 120.3,
        "interestRate": 4.7,
        "anuityDomesticCurrencyAmount": 183875.1364,
        "anuityForeignCurrencyAmount": 1528.4716,
        "totalDomesticCurrencyAmount": 11991903.275,
        "totalForeignCurrencyAmount": 99683.3189,
        "dti": 0.5438
    },
    {
        "idCalculation": 111,
        "idCalculationResult": 709,
        "exchangeRate": 120.3,
        "interestRate": 5.2,
        "anuityDomesticCurrencyAmount": 202349.3784,
        "anuityForeignCurrencyAmount": 1682.0397,
        "totalDomesticCurrencyAmount": 12435285.0834,
        "totalForeignCurrencyAmount": 103368.9533,
        "dti": 0.4941
    },
}
 ... and 33 others
 ]
}

I want to check first 35 objects (last one object is excluded from assertion) with my control json (also 35 objects). I want to check values as well properties is there exact number in each object.

I've started something like this:

import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def slurper = new JsonSlurper()
def writeResponse = slurper.parseText(messageExchange.responseContent)

def data0 = writeResponse.data[0]
def data1 = writeResponse.data[1]
def data2 = writeResponse.data[2]
def data3 = writeResponse.data[3]

//for(i=0, i<36, i++){
//if(data[i].idCalculation == 111) return true
//if(data[i].idCalculationResult == 707) return true
//}


log.info(data0)

For sure, there is much cleaner way to compare two json "files" in soapui?

Upvotes: 1

Views: 1886

Answers (3)

craigcaulfield
craigcaulfield

Reputation: 3538

Do you need to do an exact match between the documents (apart from the final element) or to just make sure that a JSON payload conforms to a particular schema? If it's the latter, then you can use a JSON schema to validate your payload. This will check the structure of your payload (including mandatory and optional elements) and the values (not necessarily exact values, but does a value meet a particular pattern).

For example, a minimal JSON schema that validates your payload would be:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Stack Overflow",
    "description": "Minimal JSON schema",
    "type": "object",
    "properties": {
        "data": {
            "type": "array",
            "items": [
                {
                    "type": "object",
                    "properties": {
                        "idCalculation": {
                            "minimum": 0,
                            "maximum": 1000,
                            "type": "integer"
                        },
                        "idCalculationResult": {
                            "minimum": 0,
                            "maximum": 1000,
                            "type": "integer"
                        },
                        "exchangeRate": {
                            "type": "number",
                            "minimum": 0,
                            "pattern": "^[0-9]{3}.[0-9]{1}$"
                        },
                        "interestRate": {
                            "type": "number",
                            "minimum": 0,
                            "pattern": "^[0-9]{1,2}.[0-9]{1,2}$"
                        },
                        "anuityDomesticCurrencyAmount": {
                            "type": "number",
                            "minimum": 0
                        },
                        "anuityForeignCurrencyAmount": {
                            "type": "number",
                            "minimum": 0
                        },
                        "totalDomesticCurrencyAmount": {
                            "type": "number",
                            "minimum": 0,
                            "exclusiveMinimum": true
                        },
                        "totalForeignCurrencyAmount": {
                            "type": "number",
                            "minimum": 0
                        },
                        "dti": {
                            "type": "number",
                            "minimum": 0
                        }
                    },
                    "required": [
                        "idCalculation",
                        "idCalculationResult",
                        "exchangeRate",
                        "interestRate",
                        "anuityDomesticCurrencyAmount",
                        "anuityForeignCurrencyAmount",
                        "totalDomesticCurrencyAmount",
                        "totalForeignCurrencyAmount",
                        "dti"
                    ]
                }
            ]
        }
    },
    "required": ["data"]
}

It's up to you how exact you get with the patterns to validate the contents of your payload. See Understanding JSON Schema for more details about JSON Schema.

If you have Ready API, there's a built-in JSON Schema Compliance Assertion where you can use this schema. If you have soapUI, you can call out to Groovy or Java to perform the schema validation for you. I use json-schema-validator and in my test cases the comparison boils down to:

assert JsonValidatorUtils.isJsonValid(schemaFile, jsonPayload)

All this might seem like a lot of work, but if you invest the time upfront you may end up with a generic solution you can reuse.

Upvotes: 1

Steen
Steen

Reputation: 873

I would JsonSlurpify both JSON files. (Same format, right?)

Assuming the result is ordered in the same way in both places, you should be able to simply loop over the entries. Something like:

def jsonFile1 = slurper.parseText(messageExchange.responseContent)
def jsonFile2 = slurper.parseText([whereEverYouHaveYourControlJsonFile])
for (def x=0; x<35; x++) {
    assert jsonFile1.data[x].idCalculation == jsonFile2.data[x].idCalculation
    assert jsonFile1.data[x].idCalculationResult== jsonFile2.data[x].idCalculationResult
    // etc... Repeat for each variable
}

Upvotes: 1

daggett
daggett

Reputation: 28634

like this?

...
def writeResponse = slurper.parseText(messageExchange.responseContent)
writeResponse.data.eachWithIndex{dat, idx->
    if(idx<35){
        assert dat.idCalculation == 111
        assert dat.idCalculationResult == 707
    }
}

Upvotes: 0

Related Questions