- SoapUI Cookbook
- Rupert Anderson
- 694字
- 2021-07-23 20:19:20
Importing CSV file data into an in-memory H2 database with Groovy
There are times when you just need a quick database loaded with test data and don't want to persist, set up, or install anything. Well, this is where the in-memory mode of the H2 database engine can come in handy (http://www.h2database.com/)!
Getting ready
Before using the H2 database, we need to download its JAR and add it to SoapUI's classpath. You can get the latest H2 JAR from http://mvnrepository.com/artifact/com.h2database/h2/ (I took version 1.4.181). Then, add it to <SoapUI Installation Directory>/java/app/bin/ext/
.
You'll also need some headed CSV data. Amazingly, the script might be able to handle any valid CSV structure (see http://www.h2database.com/html/functions.html#csvread). We'll use a simple invoice example invoices_with_headers.csv
that can be found in the chapter 2 samples.
I have provided a completed SoapUI project GroovyInMemoryDB-soapui-project.xml
in the Chapter2
samples.
How to do it...
Assuming you have a project, TestSuite
and TestCase
, we'll add a Groovy TestStep
to register the H2 JDBC driver, load the CSV test data into a new table, select the data from the table, and log the results. Perform the following steps:
- Create a
Groovy TestStep
and add the following code:import groovy.sql.Sql import org.h2.Driver com.eviware.soapui.support.GroovyUtils.registerJdbcDriver("org.h2.Driver") def db = Sql.newInstance("jdbc:h2:mem:test", "org.h2.Driver") //Change this to the location of your CSV file. def fileName = "/temp/invoices_with_headers.csv" db.execute("create table if not exists invoices as select * from csvread('$fileName')") db.eachRow("select * from invoices"){invoice-> log.info invoice.toString() }
- Running the
Groovy TestStep
should show the CSV data output to the log:Thu Aug 28 16:40:57 BST 2014:INFO:[ID:1, COMPANY:comp1, AMOUNT:100.0, DUE_DATE:2014-12-01 00:00:00] Thu Aug 28 16:40:57 BST 2014:INFO:[ID:2, COMPANY:comp2, AMOUNT:200.0, DUE_DATE:2014-12-01 00:00:00] Thu Aug 28 16:40:57 BST 2014:INFO:[ID:3, COMPANY:comp3, AMOUNT:300.0, DUE_DATE:2014-12-01 00:00:00]
That's it!
How it works...
One of the key requirements for working with JDBC drivers in SoapUI Groovy TestStep
scripts is to register the driver using the GroovyUtils.registerJdbcDriver
method. If you don't do this, you get a no suitable driver found
error when trying to get a new database connection on the next line.
The groovy.sql.Sql
class provides a very convenient wrapper to hide all the usual Java JDBC connectivity code and connection management.
Tip
Groovy SQL
It's worth taking a better look at this if you want to do more Groovy scripting with JDBC data sources. Apart from the driver details and SQL, the code here would be applicable to other JDBC databases like MySQL. For more info, see http://groovy.codehaus.org/api/groovy/sql/Sql.html.
Apart from specifying the driver's class name as org.h2.Driver
, the connection string jdbc:h2:mem:test
specifies that we want our H2 database to be called test
and created in memory (mem
).
Note
The in-memory mode
One thing to say about the convenience of in-memory mode is that the H2 database instance doesn't stop running after your Groovy script has finished, and remains available until SoapUI's JVM is closed down. This is why I put the if not exists
clause in the create table statement. Otherwise, rerunning the script will cause a table already exists error
.
Next, we have a pretty compact and dynamic SQL statement:
create table if not exists invoices as select * from csvread('$fileName')
This not only creates the table if it doesn't already exist, but also defines its structure based on the CSV file and then loads it with the data—Pow!
The last statement is fairly standard Groovy just to select all the invoice records, then iterate over them, and print each one to the log.
There's more...
The preceding example is very compact and can prove to be useful when setting up test data. See Chapter 3, Developing and Deploying Dynamic REST and SOAP Mocks for an example. If you need to tear down the data, you can either delete the records or drop the table:
db.execute("delete from invoices") db.execute("drop table invoices")
That's assuming the invoiceFileName
property was set on TestCase
.
See also
- The SoapUI online help has a useful page with lots of Groovy scripting examples at http://www.soapui.org/Scripting-Properties/tips-a-tricks.html
- 新編Visual Basic程序設計上機實驗教程
- DevOps for Networking
- PHP 7底層設計與源碼實現
- Windows系統管理與服務配置
- Easy Web Development with WaveMaker
- Apex Design Patterns
- Mastering Apache Spark 2.x(Second Edition)
- Python機器學習基礎教程
- Tableau 10 Bootcamp
- 0 bug:C/C++商用工程之道
- Learning YARN
- Internet of Things with ESP8266
- Node.js 12實戰
- 現代C:概念剖析和編程實踐
- Java7程序設計入門經典