jenkins pipeline groovy.sql.Sql NotSerializableException - groovy

there is a problem with the class groovy.sql.Sql inside jenkinsfile.
We are using groovy.sql.Sql to call DB.
Try with simple import groovy.sql.Sql we are take error:
java.io.NotSerializableException: groovy.sql.Sql
Then, try to hide this class inside shell class in jenkins file, like this:
class Shell{
private groovy.sql.Sql sql
Shell(){
sql = Sql.newInstance("jdbc:oracle:thin:#$TNS", login, password, driver)
}
String callSql(String stmnt){
return sql.firstRow(stmnt).Variablename
}
}
But get another error "MissingPropertyException: No such property: Sql for class".
Can u help to fix this problem?

Related

How to define database 'driver' using Groovy sql module

I am trying to connect to an Oracle Sql Developer DB using Groovys sql class, which requires 4 pieces of information: (db url, username, password, db driver) I have all information needed except the driver. I have tried using oracle.jdbc.driver.OracleDrive and have set my GROOVY_HOME as : Variable: %GROOVY_HOME%\lib Value: C:\Oracle_SQL_DEVELOPER\sqldeveloper
I am receiving the following error :
Caused by: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
I have referenced a few answers here on StackOverflow but haven't had any luck setting up a connection. Here are the links I've read and tried:
Unable to connect to oracle database from groovy
SQLException: No suitable Driver Found for jdbc:oracle:thin:#//localhost:1521/orcl
I have also looked and researched the Groovy documentation but it is not clear on how to define the driver: https://groovy-lang.org/databases.html#_connecting_with_a_datasource
Any help would be much appreciated. Here is the code for reference:
import groovy.sql.Sql
class EstablishConnection {
def static url = 'url'
def static user = 'user'
def static password = 'pass'
def static driver = 'oracle.jdbc.driver.OracleDriver'
def static sql = Sql.newInstance(url, user, password, driver)
}
EstablishConnection.sql.eachRow('select * from ACCOUNT where CONSTI_ID = \'12345678\';'){
row ->
def a = row[0]
}

groovy.lang.MissingPropertyException: No such property:

I am trying to get my head around groovy scripting to make some changes to a jenkins pipeline and I keep getting this error:
groovy.lang.MissingPropertyException: No such property: credentials for class:
I have tried declaring the variable with def but I still get the exception, eclipse does not recognise that the property exists.
What am I doing wrong?!
#!/usr/bin/groovy
package common.pipeline
import common.pipeline.Credentials
Credentials credentials = new Credentials()
def withCredentials(steps) {
credentials.productionPipeline(steps)
}
This script will be compiled by groovy into a Script class with the field definition inside the run method, and with another method withCredentials that is trying to access the field (kinda like this):
import common.pipeline.Credentials
class Script1 extends Script {
def withCredentials(steps) {
credentials.productionPipeline(steps)
}
def run(args) {
Credentials credentials = new Credentials()
}
}
As you can see, this won't work, as the credentials aren't at Field level in the class...
Groovy has an annotation to make this happen:
#!/usr/bin/groovy
package common.pipeline
import common.pipeline.Credentials
import groovy.transform.Field
#Field Credentials credentials = new Credentials()
def withCredentials(steps) {
credentials.productionPipeline(steps)
}

null pointer exception in soapui groovy script

I am receiving this null pointer exception and not sure how to resolve/debug it. The script contains a class with two methods. Code is doing what it is supposed to do. Any pointers on geeting started to resolve this?
java.lang.NullPointerException
at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:154)
at com.eviware.soapui.model.testsuite.TestStep$run.call(Unknown Source)
at Script48.run(Script48.groovy:22)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:92)
at com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.GroovyScriptAssertion.assertScript(GroovyScriptAssertion.java:116)
at com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.GroovyScriptAssertion.internalAssertResponse(GroovyScriptAssertion.java:128)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion.assertResponse(WsdlMessageAssertion.java:150)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest.assertResponse(WsdlTestRequest.java:176)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.propertyChange(WsdlTestRequestStep.java:339)
at com.eviware.soapui.impl.wsdl.support.assertions.AssertionsSupport.propertyChange(AssertionsSupport.java:79)
at java.beans.PropertyChangeSupport.fire(Unknown Source
Script looks like this.
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner
import com.eviware.soapui.support.types.StringToObjectMap
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext
context.setProperty("searchChange", new searchChange())
class searchChange{
def search(a,b,TCRunner){
def search_TestCase = TCRunner.testCase.testSuite.getTestCaseByName("TestCaseName")
search_TestCase.setPropertyValue("a", a)
search_TestCase.setPropertyValue("b", b)
search_TestCase.run(new com.eviware.soapui.support.types.StringToObjectMap(), false)
}
def runner(tCase,mExchange){
new WsdlTestCaseRunner( tCase, new StringToObjectMap() );
}
}
When using the above class,the line(groovy:22) in code that throws exception is
scripts.testCases["Library"].testSteps["Lib"].run(context.getTestRunner(),context)

null object error when calling code from script assertion - soapui (creating test Runner in script assertion)

In a soapui groovy script test step I've this.
context.setProperty("searchA", new searchA());
class searchA{
def testRunner
def searchA(testRunner){
this.testRunner=testRunner
}
def search(a,b){
def search_TestCase = testRunner.testCase.testSuite.getTestCaseByName("Search")
search_TestCase.setPropertyValue("ABC", a)
search_TestCase.setPropertyValue("DEF", b)
search_TestCase.run(new com.eviware.soapui.support.types.StringToObjectMap(), false)
}
}
and in an assertion script in a different test suite I am calling the above code like this.
scripts = messageExchange.modelItem.testStep.testCase.testSuite.project.testSuites["Test"]
scripts.testCases["Lib123"].testSteps["TestLib123"].run(context.getTestRunner(),context)
context.searchA.search("value1","value2")
but this gives me error "can not get property testCase on null object". whats wrong here?
I am not seeing null object error now. The issue was that testRunner is not available in script assertion so we need to create it like this in script assertion and then pass it in the caller method.
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner
import com.eviware.soapui.support.types.StringToObjectMap
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext
testCase = messageExchange.modelItem.testStep.testCase
tcRunner = new WsdlTestCaseRunner( testCase, new StringToObjectMap() );
context.searchA.search("value1","value2",tcRunner)
This thread helped me.

Pentaho data source access from Groovy

In Pentaho Report Designer (PRD), I want to connect to my Data source using a Groovy script (or EMCAScript script) by referring to JNDI created in default.properties file. So, I will create a mult-value list parameter containing JNDI names and connect to the data source based on the JNDI selected in the parameter.
Script:
import groovy.sql.Sql
import javax.naming.InitialContext
import javax.sql.DataSource
class SqlClient {
InitialContext context = new InitialContext()
DataSource dataSource = context.lookup("OLTP") as DataSource
def sql = new Sql(dataSource)
sql.execute("SELECT * FROM Person.User_JNDI")
TypedTableModel model = new TypedTableModel();
while(sql.next())
model.addRow(new Object [] {rs.getString(1)});
c.close();
return model;
}
I am getting the following error:
Caused by: org.apache.bsf.BSFException: exception from Groovy: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
expression: 11: unexpected token: sql # line 11, column 5.
sql.execute("SELECT * FROM Person.User_JNDI")
Also, PRD expects the script to return a model, probably something to do with TypedtableModel class.
Can anyone give me an example of Groovy script?
Assuming that you have filled the default.properties as it says here, you should be able to access the data source with something like:
import groovy.sql.Sql
import javax.naming.InitialContext
import javax.sql.DataSource
class SqlClient {
InitialContext context = new InitialContext()
DataSource dataSource = context.lookup("SampleData") as DataSource
def sql = new Sql(dataSource)
sql.execute '''
select * from ... '''
...
}
Using a Sql instance.

Resources