Forum Discussion

InekeBauw's avatar
InekeBauw
Occasional Contributor
9 years ago

Compara response (JSON) with data in mongodb.

Hey,

I need to compare an response from an GET rest API (JSON) to data in mongodb.

I connected my mongodb with groovy in my soapui:

 

import com.gmongo.GMongo
import com.mongodb.MongoClient
import com.mongodb.MongoCredential as MC
import com.mongodb.ServerAddress

def mongoClient = new MongoClient( new ServerAddress('MyDataBase:27017') )
def mongo = new GMongo( mongoClient )
def db = mongo.getDB('PADOC')

// attr uit GET halen
def response = context.expand( '${GET 1 Project#Response#$.PROJECT.UUID}' )

//Request weghalen
db.V1Project.findOne('content.PROJECT.UUID': response)

 

So I see the outcome in the log but how can I compare it with my response?

 

Many thanks in advance

1 Reply

  • tomlee2's avatar
    tomlee2
    New Contributor

    Depends....what is your response type?  Is it just one attribute or an array?

    Single attribute is easy by using the assert command.

    assuming your db.V1Project.findOne is working and also returns the same type as your response with single attribute

     

    def response = context.expand( '${GET 1 Project#Response#$.PROJECT.UUID}' )

    def tmp_uuid = db.V1Project.findOne('content.PROJECT.UUID': response)

     

    assert response == tmp_uuid

     

     

    However, if your response is complex type such as a json array...  you many need to look into a json sluper to find the attribute that you want to check.

     

    Good luck.