Forum Discussion

DavidEtcell's avatar
DavidEtcell
Occasional Contributor
12 years ago

How to get values to properties using a Groovy DataSource?

Hi Everyone,

I I am using a groovy datasource, and can not figure out how to get the values returned from the database into the properties.

This is setting up the connection, this always remains the same:
import groovy.sql.Sql
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver( "net.sourceforge.jtds.jdbc.Driver" )

def sql = Sql.newInstance('jdbc:jtds:sqlserver://HINICMTSTDB:1433/icmpa12s', "<login>", "<password>", "net.sourceforge.jtds.jdbc.Driver")



This returns a closed recordset which I can not seem to get anything out of, so I dont use it

//def res = sql.executeQuery("exec CWB.GetDocumentLink 'HE100196','E9324231'")



This returns a list of arrays of values

def res = sql.rows("exec CWB.GetDocumentLink 'HE100196','E9324231'")


Say the call to the database returns 2 rows of 3 columns.

If I use something like the following:

result["prop1"] = res.Column1Name.toString()

when it asks to 'specify number of rows to get', the number i enter i get in rows of '[Column1Name.Row1,Column1Name.Row2]' (arrays of all values.) Also, if I make this '0' it runs for infinity and I have to manually end the SoapUI Pro process with Task Manager.

My real question though, is how do I load the properties of the datasource so each property contains different values from the database call?

How do I get the datasource to populate 1 record per row?
  • DavidEtcell's avatar
    DavidEtcell
    Occasional Contributor
    As shown here: http://forum.soapui.org/viewtopic.php?f=5&t=22836, I have attempted to use a JDBC datasource, but it refused to return the results of the stored procedure.

    My mistake here though, was assuming that the groovy datasource was a datasource which extended the basic 'grid' class, with a groovy script attached to it. No problem though, I have already solved this problem with just that - a grid datasource and a groovy datasource.

    Code for this was:

    import groovy.sql.Sql
    com.eviware.soapui.support.GroovyUtils.registerJdbcDriver( "net.sourceforge.jtds.jdbc.Driver" )

    def DataSourceStep = testRunner.testCase.getTestStepByName("DataSource3")

    def sql = Sql.newInstance('jdbc:jtds:sqlserver://HINICMTSTDB:1433/icmpa12s', "<login>", "<password>", "net.sourceforge.jtds.jdbc.Driver")
    def res = sql.eachRow("exec CWB.GetDocumentLink 'HE100196','E9324231'"){

    def currentrow = DataSourceStep.dataSource.gridModel.getRowCount()-1

    DataSourceStep.dataSource.gridModel.setValueAt(it[0].toString(),currentrow,0)
    DataSourceStep.dataSource.gridModel.setValueAt(it[1].toString(),currentrow,1)

    //didn't need this after all as there is always an extra 'blank' line at the bottom of the list, meaning as soon as a value is populated in one row, another blank row is added
    //DataSourceStep.dataSource.gridModel.insertRows(DataSourceStep.dataSource.gridModel.getRowCount()-1,1)

    }