Ask a Question

how to access jdbc connection string set at environment level?

SOLVED
venkee_rb
New Contributor

how to access jdbc connection string set at environment level?

Hello,

 

currently, I am using the below to get the connection string in groovy.  This obtains the connection string from database container which is static across environments.  Instead of this I want to get the connection string from the JDBC Connections tab from the soapui environments.  Is there a way to do that?

 

 def ConObj = context.testCase.testSuite.project.databaseConnectionContainer.getResourceByName('testdb');
 def constring = ConObj.getConnectionString();
 def sql = Sql.newInstance(constring);

 sql.eachRow(<select statment>)

1 ACCEPTED SOLUTION

Accepted Solutions
Radford
Super Contributor

Re: how to access jdbc connection string set at environment level?

You need to check to see if you are in the environment mode or not and if appropriate go via the Environment object, something like:

 

def jdbcConnectionName = 'testdb'

if (testRunner.testCase.testSuite.project.isEnvironmentMode()) { jdbcConnection = testRunner.testCase.testSuite.project.activeEnvironment.databaseConnectionContainer.getDatabaseConnectionByName(jdbcConnectionName) } else { jdbcConnection = testRunner.testCase.testSuite.project.databaseConnectionContainer.getDatabaseConnectionByName(jdbcConnectionName) } log.info 'JDBC Name = ' + jdbcConnection.getName() log.info 'JDBC Connection String = ' + jdbcConnection.getConnectionString()

 

 

Note, if you are just after a groovy.sql.Sql object I think you can use the following which I believe takes care of checking if you are in environment mode or not:

 

import com.eviware.soapui.support.GroovyUtilsPro

def groovyUtilsPro = new  GroovyUtilsPro(context)
def sql = groovyUtilsPro.getGroovySql('testdb')

View solution in original post

3 REPLIES 3
Radford
Super Contributor

Re: how to access jdbc connection string set at environment level?

You need to check to see if you are in the environment mode or not and if appropriate go via the Environment object, something like:

 

def jdbcConnectionName = 'testdb'

if (testRunner.testCase.testSuite.project.isEnvironmentMode()) { jdbcConnection = testRunner.testCase.testSuite.project.activeEnvironment.databaseConnectionContainer.getDatabaseConnectionByName(jdbcConnectionName) } else { jdbcConnection = testRunner.testCase.testSuite.project.databaseConnectionContainer.getDatabaseConnectionByName(jdbcConnectionName) } log.info 'JDBC Name = ' + jdbcConnection.getName() log.info 'JDBC Connection String = ' + jdbcConnection.getConnectionString()

 

 

Note, if you are just after a groovy.sql.Sql object I think you can use the following which I believe takes care of checking if you are in environment mode or not:

 

import com.eviware.soapui.support.GroovyUtilsPro

def groovyUtilsPro = new  GroovyUtilsPro(context)
def sql = groovyUtilsPro.getGroovySql('testdb')

View solution in original post

venkee_rb
New Contributor

Re: how to access jdbc connection string set at environment level?

Thanks for the reply.  This did help to address the issue.  The 'groovyUtilsPro.getGroovySql' works very well and indeed it takes care of checking the environment mode. 

 

The first option using Environment opject complains as below.  do we have to import a specific package or pass an object as argument instead of string?

 

groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.support.connections.DefaultDatabaseConnectionContainer.getDatabaseConnectionByName() is applicable for argument types: (java.lang.String) values: [testDb] error at line: 3

 

 

Radford
Super Contributor

Re: how to access jdbc connection string set at environment level?

I've just double checked my code above, cutting and pasting the above (albeit changing the value of jdbcConnectionName to a valid name for my project) directly into a new empty Groovy Script test step and it works for me in both the default and environment mode.

 

I'm not sure why it doesn't work for you? I am using a older version of Ready API, version 1.7 so perhaps that might have an impact?

 

On the plus side, I'm glad the GroovyUtilsPro method work for you as I feel this is a simpler and neater solution.

cancel
Showing results for 
Search instead for 
Did you mean: