Soap UI, REST API, update database - rest

I am going to use soapUI to test the REST API framework.
Is there a way through which i can insert/update records inside the MongoDB with data in a file type(csv, txt etc) using soapUI tool?
What i am trying to do is validate the API calls and update the database from a data file.

If you are willing to use Groovy script, then you can do this pretty easily.
Put your jdbc driver in SoapUI's bin\ext directory.
https://github.com/mongodb/mongo-java-driver/downloads
(probably where you can it for mongodb)
Then you need roughly these things in your script:
import groovy.sql.Sql
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
groovyUtils.registerJdbcDriver("org.postgresql.Driver") // NOT SURE WHAT STRING FOR MONGODB
def connectString = "....."
sql = Sql.newInstance(connectString) // TEST YOUR CONNECT STRING IN A SQL BROWSER
def misc = sql.firstRow("SELECT * from table")
groovy.sql.Sql is very nice!
http://groovy.codehaus.org/api/groovy/sql/Sql.html

You can easily use below groovy code in "Groovy test step" of your test case and connect to mongodb. Before that please ensure that mongodb java client jar file and gmongo are in {Installation Directory}\bin\ext folder of your soapUI installation
Gmongo: http://mvnrepository.com/artifact/com.gmongo/gmongo/1.5
Mongodb Java Client : http://mvnrepository.com/artifact/org.mongodb/mongo-java-driver/3.2.2
import com.gmongo.GMongoClient
import com.gmongo.GMongo
import com.mongodb.MongoCredential
import com.mongodb.ServerAddress
//def credentials = MongoCredential.createMongoCRCredential('admin', 'students', 'admin' as char[])
//def client = new GMongoClient(new ServerAddress("127.0.0.1:27017"))
context.gmongo=new GMongo()
def db=context.gmongo.getDB("test")
log.info db.fruit.find().count()
db.fruit.find().each{
doc->log.info doc
}

Related

Scala Slick configure with Amazon X-Ray

anyone tried to use this:
X-Ray
with Slick ?
import slick.dbio.DBIO
import slick.jdbc.JdbcBackend.Database
import slick.jdbc.PostgresProfile.api._
private[database] class PostgresConnector extends DatabaseConnector {
protected final val configurationPath = "mycompany.backend.database.postgres"
protected lazy val database = Database.forConfig(configurationPath)
Probably no way because its based on tomcat:
These interceptors are in the aws-xray-recorder-sql-postgres and
aws-xray-recorder-sql-mysql submodules, respectively. They implement
org.apache.tomcat.jdbc.pool.JdbcInterceptor and are compatible with
Tomcat connection pools.
If you are trying to instrument a SQL connection using a provider other than MySQL or Postgres, you can try to use the generic JDBC-based SQL Library, documented here: https://github.com/aws/aws-xray-sdk-java#intercept-jdbc-based-sql-queries
Alternatively, you can use the X-Ray auto-instrumentation agent for Java, which automatically captures all JDBC-based SQL queries.

Is it possible writing down to RDS raw sql (PostgreSQL) using AWS/Glue/Spark shell?

I have a Glue/Connection for an RDS/PostgreSQL DB pre-built via CloudFormation, which works fine in a Glue/Scala/Sparkshell via getJDBCSink API to write down a DataFrame to that DB.
But also I need to write down to the same db, plain sql like create index ... or create table ... etc.
How can I forward that sort of statements in the same Glue/Spark shell?
In python, you can provide pg8000 dependency to the spark glue jobs and then run the sql commands by establishing the connection to the RDS using pg8000.
In scala you can directly establish a JDBC connection without the need of any external library as far as driver is concerned, postgres driver is available in aws glue.
You can create connection as
import java.sql.{Connection, DriverManager, ResultSet}
object pgconn extends App {
println("Postgres connector")
classOf[org.postgresql.Driver]
val con_st = "jdbc:postgresql://localhost:5432/DB_NAME?user=DB_USER"
val conn = DriverManager.getConnection(con_str)
try {
val stm = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
val rs = stm.executeQuery("SELECT * from Users")
while(rs.next) {
println(rs.getString("quote"))
}
} finally {
conn.close()
}
}
or follow this blog

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
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.

Connect to MongoDb using X509 certificate

I am trying to connect to MongoDB using mongoX509. I am using mongo java driver 3.3.0 jar. In api reference I can see MongoCredential to pass subject name and authenticate but i am not able to import this in my code. When I decompile the jar I am not able to see MongoCredential as well.
Am I missing any dependencies. Is there any easy way to connect to Mongodb without using MongoCredential? The details i have is server, port and certificate subject name?
I can post the code I am trying if anyone wants to take a look at it as well
Thanks in Advance
Code - I am using SoapUI to run this code
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
try{
def subjectName="CN=xx,OU=xx,O=xx,C=US,ST=CA,L=xx"
MongoCredential credential = MongoCredential.createMongoX509Credential(subjectName);
def URI = "mongodb://server1:27017,server2:27017,server3:27017/<<database>>?replicaSet=<<XYZ>>&authMechanism=MONGODB-X509&ssl=true"
MongoClientURI uri = new MongoClientURI(URI)
MongoClient client = new MongoClient(uri, Arrays.asList(credential));
DB database = client.getDB(<<database>>);
collection = database.isAuthenticated();
log.info collection
}
catch (Exception e){
log.info e
}
The issue was because of incorrect jar that I was using. I got the correct version and it worked.

Read application.conf configuration in Play for Scala

I have the following data source configured in application.conf that I use to run Slick statements.
I need to access the same database through an OLAP engine that will use the same user and password.
Since it's already configured, I'd like to get these two from there, is it possible to read application.conf from Scala? I know I can read the physical file, but is there a library to get the parsed data?
## JDBC Datasource
# ~~~~~
#
dbConfig = {
url = "jdbc:mysql://localhost:3306/db"
driver = com.mysql.jdbc.Driver
connectionPool = disabled
keepAliveConnection = true
user=root
password=xxxx
}
Working with play, you simply inject the configuration like this:
import javax.inject.Inject
import play.api.Configuration
class Something #Inject()(configuration: Configuration) {
val url: Option[String] = configuration.getString("dbConfig.url")
val keepAliveConnection: Option[Boolean] = configuration.getBoolean("dbConfig.keepAliveConnection")
...
}
Also see Configuration API on how to get your properties in various types and formats.