官术网_书友最值得收藏!

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:

  1. 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()
    }

    Note

    Before running, make sure that the fileName variable is set to the correct path.

  2. 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")

Tip

Parameterize file paths

To improve the example, rather than hardcoding the file path, it would be a better practice to use a property:

def fileName = testRunner.testCase.getPropertyValue("invoiceFileName")

That's assuming the invoiceFileName property was set on TestCase.

See also

主站蜘蛛池模板: 肃宁县| 樟树市| 河北省| 仙居县| 永春县| 会昌县| 高尔夫| 双城市| 同心县| 宜阳县| 兴隆县| 大新县| 洪泽县| 成安县| 开封县| 浦北县| 南皮县| 镇江市| 宜昌市| 双流县| 萨迦县| 五大连池市| 都江堰市| 双鸭山市| 佳木斯市| 安徽省| 临清市| 甘洛县| 马尔康县| 尼玛县| 界首市| 通榆县| 福海县| 喀喇| 大石桥市| 迁安市| 滦平县| 名山县| 聊城市| 民和| 黑山县|