mongodbgroovysoapuiready-apigmongo

Create a mongo connection and make it alive for execution of an Entire Test Suite in Ready!API


If you want to make an gmongo connection alive for an entire test suite and then close it in a tear down operation after the entire test suite is executed then, How could we do that?

Currently what am I doing is, I am creating an connection for an particular test step and then after the test step is executed, I close the connection by using the code mongoClient.close()

But now there is a requirement where I need to create the connection before the test suite starts executing, use the same connection throughout the test suite inside the test cases/test steps and then close the connection the connection after the entire test suite gets executed.

Could anyone please tell me how could I do this using Ready!API?

I may sound retard cause I am new to Ready API so please bear with me This is the code that I use to create an Connection to mongo

 def dbUser = context.expand( '${#Project#MongoUser}' )
    def dbPassword = context.expand( '${#Project#MongoPassword}' )
    def dbServer = context.expand( '${#Project#MongoServer}' )
    def dbDatabase = context.expand( '${#Project#MongoDatabase}' )
    def credentials = MongoCredential.createCredential(dbUser,dbDatabase,dbPassword as char[])
    def mongoClient = new MongoClient( new ServerAddress(dbServer),Arrays.asList(credentials) )
    context.gmongo = new GMongo( mongoClient )
    context.mongoDB = context.gmongo.getDB(dbDatabase)

So i have been using the current code in order to create the connection. Actually I want this as three test suites. The First Test Suite would contain the groovy script to create the connection, The Second Test Suite would contain all of my Test Cases and the Third test suite would contain the mongo close connection script.

We use the Environment values from the properties file. Here the MongoServer has the values of the environment in which the connection is laid

I could not understand @Rao, how did you call the conn variable inside the test cases. Especially the context.testCase.testSuite.db?.connection part. What does the "?" denote and could you please tell me in the above context, how could carry out the process


Solution

  • Below script address how you achieve what you are looking for in ReadyAPI / SoapUI. Note that you already know how to connect to gmongo in Groovy which you need to add that logic in the place holder by following the comment inline.

    Below is the test suite level Setup Script to create the db connection.

    class DatabaseDetails {
        def server
        def user
        def password
        def log
        def getConnection() {
            log.info 'connection created'
            //Write logic to create connection
        }
        def closeConnection() {
            log.info 'Closing connection'
            //Write logic to close connection
        }
    }
    //Change server, user, password values according to your environment
    def db = [ server:'localhost', user:'dbuser', password: 'dbuserpasswd', log: log] as DatabaseDetails
    if (!db.connection) {
        db.connection
        testSuite.metaClass.db = db 
    }
    

    Below is the test suite level TearDown Script to close the db connection. Since this is in tear down script, connection gets closed automatically as soon the test suite execution is completed.

    testSuite.db?.closeConnection()
    

    Now, there is no need to have step to create the db connection again and again. You just need to use below script in Groovy Script test step to get the existing db connection.

    def conn = context.testCase.testSuite.db?.connection
    

    Using conn variable, you should be able to execute the queries.

    Note : Since the db connection is done in Setup Script of test suite, if you just run the test case(i.e., test suite is not invoked or executed), you may not able to get the connection. In such cases, manually execute the Setup Script of the test suite.

    EDIT: Based on OP's edit to the question and his code snippet, here is the updated test suite's Setup Script. This takes care of implementation of getConnection() and closeConnection() based on OP's edit. Please add / edit import statements for Mongo classes that are used as I am not really aware of those.

    Updated Test Suite's Setup Script

    import com.gmongo.*
    import com.mongodb.*
    
    class DatabaseDetails {
        def context
        def log
        def mongoClient
        def mongoDB
        def getConnection() {
            log.info 'Creating connection.'
            //Write logic to create connection
            if (!mongoDB){        
            def credentials = MongoCredential.createCredential(
               context.expand('${#Project#MongoUser}'),
               context.expand('${#Project#MongoDatabase}'),
               context.expand('${#Project#MongoPassword}') as char[])
            mongoClient = new MongoClient( new ServerAddress(context.expand('${#Project#MongoServer}')),Arrays.asList(credentials) ) 
            mongoDB = new GMongo( mongoClient ).getDB(context.expand('${#Project#MongoDatabase}'))          
          }
          mongoDB
        }
    
        def closeConnection() {
            log.info 'Closing connection'
            //Write logic to close connection
            mongoClient.close()
        }
    }
    
    def db = [ context: context, log: log] as DatabaseDetails
    if (!db.connection) {
        db.connection
        testSuite.metaClass.db = db 
    }
    

    As mentioned earlier, to get the connection, use below code and explaining it down.

    context.testCase.testSuite.db?.connection
    

    Groovy has great feature called ExpandoMetaclass. db is injected to testSuite class and db is object of DatabaseDetails class that we created and instantiated in Setup Script of test suite.

    And db contains getConnection() i.e., db.getConnection() which can also same as db.connection. That is how connection is available in the above statement.