Harsha
Harsha

Reputation: 1

How to compare DB result with JSON if JDBC query is returning multiple rows and data is available in JSON in the form of array

Below is the table:
FirstName   LastName   Email
James         Thomas     [email protected]
Teresa         Smith       [email protected]
Kristina        Joseph      [email protected]

Below is the JSON in the form of array
Personal Details [
{
FirstName: James
LastName:Thomas
Email: [email protected]
}
{
FirstName: Teresa
LastName: Smith
Email: [email protected]
}
{
FirstName: Kristina
LastName: Joseph
Email: [email protected]
} ]

In My JDBC request query result I am getting 3 rows.
In JMeter how can I validate:
Row 1 from DB with PersonalDetails[0].FirstName, PersonalDetails[0].LastName and PersonalDetails[0].Email from JSON
Row 2 from DB with PersonalDetails[1].FirstName, PersonalDetails[1].LastName and PersonalDetails[1].Email from JSON
Row 3 from DB with PersonalDetails[2].FirstName, PersonalDetails[2].LastName and PersonalDetails[2].Email from JSON

Upvotes: 0

Views: 171

Answers (1)

Dmitri T
Dmitri T

Reputation: 168072

If you specify the "Variable Names" in the JDBC Request sampler like:

enter image description here

you will get the following JMeter Variables:

FirstName_1=James
FirstName_2=Teresa
FirstName_3=Kristina
FirstName_#=3
LastName_1=Thomas
etc.

Then you can add a JSR223 Assertion to the request which returns the JSON and put the following code into "Script" area:

def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())

json.eachWithIndex { entry, index ->
    if (entry.get('FirstName') != vars.get('FirstName_' + (index + 1))) {
        AssertionResult.setFailure(true)
        AssertionResult.setFailureMessage('First name mismatch')
    }
    if (entry.get('LastName') != vars.get('LastName_' + (index + 1))) {
        AssertionResult.setFailure(true)
        AssertionResult.setFailureMessage('Last name mismatch')
    }
    if (entry.get('Email') != vars.get('Email_' + (index + 1))) {
        AssertionResult.setFailure(true)
        AssertionResult.setFailureMessage('Email name mismatch')
    }
}

Upvotes: 1

Related Questions