Forum Discussion

MV1234's avatar
MV1234
New Contributor
6 years ago

Connection timeout when connecting to mongo db through groovy script through open source soap ui

I have been trying to connect to mongodb using the following groovy script through open source soap UI.  I have already plaved gmongo 1.5 jar and mongo java driver jar 3.10.1 in soap ui lib folder....
  • MV1234's avatar
    6 years ago

    Hi,

     

    I have solved it myself. Please find the steps below:

    1) Download the pem file and generate a trust store using the following command from JDK cmd prompt

    keytool -import -alias test -file <pem file path> -keystore <set path for trust store file> 

    2) Import the certificate to C:/Program Files/SmartBear/jre/lib/security/cacerts 

    Command:

    keytool -import -trustcacerts -keystore "C:\Program Files\SmartBear\SoapUI-5.3.0\jre\lib\security\cacerts" -storepass <pwd> -alias <aliasname> -import -file <truststore path>

    3) Open soapui and double click on the project and set the following properties

    Click on truststores tab, add the path "C:\Program Files\SmartBear\SoapUI-5.3.0\jre\lib\security\cacerts", set the password

    4) Open the SoapUI and navigate to tab Preferences and configure the preferences:

    Give the keystore path as  "C:\Program Files\SmartBear\SoapUI-5.3.0\jre\lib\security\cacerts" enter the password, enter mock trust store and mock trust store password also similar to the above one. Check the box - requires client authentication

     

    5.Open vmoptions file and append the following properties to it.

    -Dsoapui.https.protocols=SSLv3

    -Dsun.security.ssl.allowUnsafeRenegotiation=true

    -Djavax.net.ssl.trustStore=C:/Program Files/SmartBear/SoapUI-5.3.0/jre/lib/security/cacerts

    -Djavax.net.ssl.trustStorePassword=<pwd>

    -Djavax.security.auth.useSubjectCredsOnly=false

    -Djavax.net.ssl.trustStoreType=JKS

     

     

    Groovy Script:

     

    import com.gmongo.GMongo

     

    import com.mongodb.BasicDBObject

    import com.mongodb.DB

    import com.mongodb.DBCollection

    import com.mongodb.DBCursor

    import com.mongodb.*

    import com.mongodb.MongoException

     

    import java.util.Arrays

    import java.util.Iterator

     

    import org.bson.Document

    import com.mongodb.MongoClient

    import com.mongodb.MongoClientOptions

    import com.mongodb.MongoCredential

    import com.mongodb.ReadPreference

    import com.mongodb.ServerAddress

    import com.mongodb.client.FindIterable

    import com.mongodb.client.MongoCollection

    import com.mongodb.client.MongoDatabase

     

     

    import com.eviware.soapui.settings.SSLSettings

    import com.eviware.soapui.SoapUI

     

     

     

    System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files\\SmartBear\\SoapUI-5.3.0\\jre\\lib\\security\\cacerts")

     System.setProperty("javax.net.ssl.trustStorePassword","<pwd>")

     

    def mongoClientOptions = MongoClientOptions.builder().sslEnabled(true).sslInvalidHostNameAllowed(true).requiredReplicaSetName("<replicaset name>").readPreference(ReadPreference.primary()).build();

     

    def mongoCredentials = MongoCredential.createCredential("<username>", "<dbname>", "<pwd>".toCharArray());

     

    def mongoClient = new MongoClient( new ServerAddress("<IPAddress>" , <port>),

                                                      Arrays.asList(mongoCredentials),

                                                      mongoClientOptions);

     

    def database = mongoClient.getDatabase("<dbname>")

    def collection = database.getCollection("<collection name>")

    def iterDoc = collection.find()

    def it = iterDoc.iterator()

     

    while (it.hasNext()) {

                    log.info it.next()

    }