Thursday, December 20, 2012

Using mongoDB with soapUI

Using mongoDB with soapUI turns out to be incredible easy - mostly thanks to the gmongo project which provides a groovy layer on top of the mongo java driver. Let's have a quick look at some example on how this works.

Getting Started

Start by downloading the latest gmongo and mongo-java-driver versions and putting them in the soapUI\bin\ext folder - restart soapUI and you will see them getting picked up by soapUI as it starts (in the soapui log tab at the bottom of the main window):

If you don't already have mongoDB installed - download and install it on your machine and make sure it is running.

Writing Data to a mongoDB Collection

Let's start by writing some data to a mongoDB collection. I'm going to take the result from a call to the Flickr cameras API call which gives a list of all camera makers used by flickr users, and write each of these camera brands to my local database. The REST call to flickr is straight-forward:
Since Flickr requires an API Key I've put that in a project property so I can easily reuse it in all my other Flickr calls - the rest is straight forward for the flickr.cameras.getBrands call - you can see the response from Flickr to the right in the window above.

To write this to my mongoDB using a combination of a DataSource and DataSink TestStep - the DataSource uses the XML DataSource configuration to loop the brand elements in the response:

How this type of DataSource works is described in more detail at http://blog.smartbear.com/software-quality/bid/170520/How-to-Use-XML-DataSources-for-Response-Processing-in-soapUI.

Instead let's focus on the DataSink:


As you can see I've set up a Groovy DataSink with an extremely simple script to write the current rows data to a  mongo collection - this is called every time the DataSink TestStep is executed;

  • The connection to the database is set up the first time the DataSink is called and saved in the context
  • The current rows properties are written to the cameras collection; since properties is a Map it can be "streamed" directly into the collection using the << notation
That's it - the entire structure of my TestCase is as follows:
We have:
  1. Our REST Request that gets the list of camera brands
  2. Our DataSource that reads each row from the flickr response
  3. Our DataSink that writes each row to mongoDB
  4. A DataSourceLoop Step that ensures that we read (and write) each row
Running this unfortunately doesn't give us too much information. Lets' add another TestCase that reads this data from our cameras collection and uses it to make requests to flickr.

Reading from a mongoDB Database

The DataSource will be of type Groovy and looks as follows:
The script initializes its connection to mongoDB upon first execution and also creates a cursor for the cameras collection. Next it checks if there is another row and  in that case copies the row values to the DataSource properties. If there is no more data the result object is left empty - which is interpreted by soapUI that the DataSource has been exhausted.

We're going to use this data in a group-search request to Flickr - just to make sure that there are at least 3 Groups for each brand of camera. The request is straight-forward;

As you can see the ApiKey is being used here again - and the name property of the DataSource is being used in the text search field. When we run this we get something like:

As you can see we have a DataSource Loop at the end of the TestCase that will jump back to the Group Search Request for each row found in the DataSource. Pretty Neat - huh?

Making it easier

Ok, I admit that the above example was a bit overdone - let's just have a quick look at a groovy script step that would read all the data from the cameras collection and log it to the log:
import com.gmongo.GMongo

def gmongo = new GMongo()
def cursor = gmongo.getDB( "soapui" ).cameras.find()

while( cursor.hasNext() )
{ 
    def camera = cursor.next()
     log.info "Found camera with $camera.id names $camera.name" 
}

Dead simple - right?

Future Ideas

Some more ideas of what you could do with mongoDB in soapUI
so much fun - so little time :-)

Conclusion - wrapping up

Thanks to the gmongo library and the many scripting possilibities in soapUI - accessing a mongoDB database from soapUI is pretty straigh-forward.

You can download the gmongo distribution from https://github.com/mongodb/mongo-java-driver/downloads,  the java-mongo driver is available at https://github.com/mongodb/mongo-java-driver/downloads.

I suggest you dig into the gmongo documentation at https://github.com/poiati/gmongo - it has a lot of cool features for making mongo java programming much easier.

And as I'm sure you know - soapUI and soapUI Pro can be downloaded form http://www.soapui.org

Thanks for your time!

/Ole