cancel
Showing results for 
Search instead for 
Did you mean: 

Retreiving data from Mongo db using Groovy and assigning it to a test case property

SOLVED
Highlighted
Occasional Contributor

Retreiving data from Mongo db using Groovy and assigning it to a test case property

Hi, I'm looking to create a test step that can connect to MongoDB, run a query, and then assign the result to a test case property. I have the connection working and I can retrieve a result as a row count. However I cannot get the actual result to assign it to a test case property. I know how to assign a value to a test case property but simply cannot get the result in order to do that. Below is my existing code. I'm connecting to a MongoDB called database called 'digital', within that there is a collection called 'QuoteSummary'. Within 'QuoteSummary' I'm searching for a "quoteRefNo" where "workReferenceNo" = GPE0024879. Its the resulting "quoteRefNo" that I want to assign to a test case property.

 

import com.gmongo.GMongoClient
import com.mongodb.MongoCredential
import com.mongodb.ServerAddress
import com.mongodb.BasicDBObject

def credentials = MongoCredential.createCredential('hidden_user', 'digital', 'hidden_pwsd' as char[])
def serverAddress = new ServerAddress('hidden_servername_and_port')
def mongo = new GMongoClient(serverAddress, [credentials])
def db = mongo.getDB('digital')
BasicDBObject query = new BasicDBObject("workReferenceNo":"GPE0024879")
BasicDBObject query1 = new BasicDBObject(quoteRefNo: 1,_id: 0)

def collection = db.getCollection('QuoteSummary')
//log.info(collection.find(query).count())
log.info(collection.find((query),(query1)).count())

1 ACCEPTED SOLUTION

Accepted Solutions
Contributor

Re: Retreiving data from Mongo db using Groovy and assigning it to a test case property

hello martinrampton,

 

that query looks a little out of place...   Maybe just stick with the collection to debug for a little bit then add the query.

.

.

def db = mongo.getDB('digital')
def collection = db.getCollection('QuoteSummary')

log.info collection.find().first();

// or

Document myDoc = collection.find().first();

log.info myDoc.toString();

// or

log.info myDoc.toJson();
.

.

if you can identify content in the collection, you can start forming query.

i.e.

myDoc = collection.find(eq("workReferenceNo", "GPE0024879")).first();

log.info myDoc.toString();

// or

log.info myDoc.toJson();

3 REPLIES 3
Contributor

Re: Retreiving data from Mongo db using Groovy and assigning it to a test case property

hello martinrampton,

 

that query looks a little out of place...   Maybe just stick with the collection to debug for a little bit then add the query.

.

.

def db = mongo.getDB('digital')
def collection = db.getCollection('QuoteSummary')

log.info collection.find().first();

// or

Document myDoc = collection.find().first();

log.info myDoc.toString();

// or

log.info myDoc.toJson();
.

.

if you can identify content in the collection, you can start forming query.

i.e.

myDoc = collection.find(eq("workReferenceNo", "GPE0024879")).first();

log.info myDoc.toString();

// or

log.info myDoc.toJson();

Moderator

Re: Retreiving data from Mongo db using Groovy and assigning it to a test case property

Hi,

 

@martinrampton, do you have any updates on the case?

Did you have a chance to try the suggestion that @Todd_Neuschwang provided?

 

If it answers your question, please click the Accept as Solution button below that suggestion.

 

 

We are looking forward to hearing from you,

 

Olga Terentieva,
SmartBear Assistant Community Manager

Occasional Contributor

Re: Retreiving data from Mongo db using Groovy and assigning it to a test case property

Thanks Todd, I worked though this in debug as you suggested and eventually manged to get some meaningful data out of Mongo DB. I now have it passing this data back into my test case.

New Here?
Join us and watch the welcome video:
Top Kudoed Authors