Forum Discussion

nkpalli's avatar
nkpalli
Contributor
8 years ago

Sending JDBC response to REST API assertion

Need some input on this below scenario(Using SOAPUI Free version to test this scenario) :validate distinct group name in JDBC request exist in REST API response (JSON format)

 

 

1) Generated a JDBC request to get distinct values from a table:

 

select distinct(groupname) from tableA . Here below is how my sample JDBC response :

<Results>
<ResultSet fetchSize="128">
<Row rowNumber="1">
<GROUPNAME>All TEST</GROUPNAME>
</Row>
<Row rowNumber="2">
<GROUPNAME>TEST 1</GROUPNAME>
</Row>
<Row rowNumber="3">
<GROUPNAME>TEST 2</GROUPNAME>
</Row>
<Row rowNumber="4">
<GROUPNAME>TEST 3</GROUPNAME>
</Row>
</ResultSet>
</Results>

 

Step2) created a REST API request which gets this below response.

{
"data": [
{
"Leadtime": 60,
"size": 17,
"name": "All TESTS",
"isactive": true,
"isapprove": true,
"isocommit": true,
},
{
"Leadtime": 60,
"size": 16,
"name": "TEST1",
"isactive": true,
"isapprove": true,
"isocommit": true,
},

{
"Leadtime": 60,
"size": 18,
"name": "TEST2",
"isactive": true,
"isapprove": true,
"isocommit": true,
},
],
"status": 200,
"success": true
}

 

3) Added a JSONPATH match assertion to REST API request where  assertion fails 


Jsonpathexpression = $..data.name  
expected results = ${JDBC Request#ResponseAsXml#//ResultSet/Row/NAME}

Actual was [ALL TESTS,Group 1, group2,group3,group4]
Expected [ALL TESTS]

for some reason Expected results is only picking first name node [ALL TESTS] as opposed to " [ALL TESTS,Group 1, group2,group3,group4]" and fails my test

 

Can anyone shed some light on what i am doing wrong on this

3 Replies

  • PaulMS's avatar
    PaulMS
    Super Contributor

    The Xpath expression returns more than 1 match, but the expected result shows only the first match. This seems to be a limitation of property expansion in SoapUI.

     

    You could add a separate assertion for each name and just increment the indexes.

    JSONPath Expression = $..data.name[0]
    Expected Result = ${JDBC Request#ResponseAsXml#//ResultSet/Row[1]/GROUPNAME}

     

    Or insert a Groovy script test step to transfer the full list as a string to a property.

     

    import com.eviware.soapui.support.XmlHolder
    
    def holder = new XmlHolder( context.expand('${JDBC Request#ResponseAsXml}') )
    def nodelist = holder["//ResultSet/Row/GROUPNAME"]
    
    def asString = nodelist.join(", ")
    log.info asString
    
    testRunner.testCase.setPropertyValue( "MyProp", "[" + asString + "]" )

     

    • nkpalli's avatar
      nkpalli
      Contributor

      Thank you Paul that was helpful !  Can you elaborate on what this statement does in your script assertion as commenting out this statement in assertion script gives me the same value as uncommenting this statement .. so trying to understand what it does

       

      //testRunner.testCase.setPropertyValue( "MyProp", "[" + asString + "]" )

       

       

       

      • PaulMS's avatar
        PaulMS
        Super Contributor

        That line was to set a property value at the test case level with the list of names (enclosed in brackets so it would match a JSONpath expression).

         

        I was using a Groovy script test step, but in a script assertion it would be slightly different because messageExchange is invoked

        messageExchange.modelItem.testStep.testCase.setPropertyValue( "MyProp", "[" + asString + "]" )

         

        I didn't mention that the next step for the JsonPath Match assertion would be like

        JSONPath Expression = $..data.name
        Expected Result = ${#TestCase#MyProp}