Forum Discussion

chirangv's avatar
chirangv
New Contributor
2 months ago
Solved

Accessing database connection string from groovy test step

Hi, I have different databases for different environments. I was trying to get the database connection string based on the active environment. Can someone help me on how to access the database connection string from groovy test step.  Thank you in advance.

  • Hello chirangv 

    import groovy.sql.*;
    import com.eviware.soapui.support.GroovyUtilsPro;
    
    
    def jdbcConnectionName = "your jdbc name defined in environment configuration page";   //log.info "   jdbcConnectionName=$jdbcConnectionName";
    def groovyUtilsPro = new  GroovyUtilsPro(context);
    def sqlConnection = groovyUtilsPro.getJdbcConnection(jdbcConnectionName);
    def sql = Sql.newInstance(sqlConnection);
    
    def sqlQuery = """
       SELECT colname
       FROM tablename
       WHERE colname = 'something'
    """.toString();
    log.info "   sqlQuery=$sqlQuery";
    dbRows = sql.rows(sqlQuery);
    log.info "   dbRows.size()=${dbRows.size()}";
    assert dbRows.size() > 0, "Need a row returned but none was... No data found.";
    dbRows.each { row ->
       log.info "   row.id=${row.id}";
    };

     

  • TNeuschwanger's avatar
    TNeuschwanger
    Community Leader

    Hello chirangv 

    import groovy.sql.*;
    import com.eviware.soapui.support.GroovyUtilsPro;
    
    
    def jdbcConnectionName = "your jdbc name defined in environment configuration page";   //log.info "   jdbcConnectionName=$jdbcConnectionName";
    def groovyUtilsPro = new  GroovyUtilsPro(context);
    def sqlConnection = groovyUtilsPro.getJdbcConnection(jdbcConnectionName);
    def sql = Sql.newInstance(sqlConnection);
    
    def sqlQuery = """
       SELECT colname
       FROM tablename
       WHERE colname = 'something'
    """.toString();
    log.info "   sqlQuery=$sqlQuery";
    dbRows = sql.rows(sqlQuery);
    log.info "   dbRows.size()=${dbRows.size()}";
    assert dbRows.size() > 0, "Need a row returned but none was... No data found.";
    dbRows.each { row ->
       log.info "   row.id=${row.id}";
    };

     

  • To access a database connection string based on the active environment in Groovy, you can use environment variables or a properties file. Retrieve the connection string by checking the active environment, like System.getenv('ACTIVE_ENVIRONMENT'), and use it to fetch the corresponding DB string.

  • adamjoy's avatar
    adamjoy
    Occasional Contributor

    In order to access the database connection string dynamically in your Groovy test step. You can retrieve it based on the active environment from a properties file or a central configuration service.

    I found this approach particularly useful in systems leveraging Edge Computing, where database interactions often need to be secure and environment-specific. As proper encryption and secure storage of connection details are important to address security challenges in edge computing effectively.