Validate the JDBC request with a user defined variable - jmeter

I am new to JMeter,
In my test I am creating a JDBC Connection to oracle DB and running a query which fetching me the count of records, which I want to validate must be equal to the SAMPLE-NUMBER (which is a defined variable in the user defined variable).
SELECT COUNT(*) FROM event_log WHERE audit_context_key LIKE '288017ec-0dcf-4fd5-9565-e8ad15e65cd2' AND event_desc = 'Success'
Response Body:
COUNT(*)
2
UserDefined Variable

You can do this, defined the variable name in JDBC request,
for example, TOTALCOUNT and add a JSR223 Assertion with the following code,
1.upto(vars.get('TOTALCOUNT_#') as int, {
if (vars.get('TOTALCOUNT_' + it) == '${__groovy(vars.get('SAMPLE-NUMBER'),)}') {
AssertionResult.setFailure(false);
}
})

Response Assertion can do the trick for you:
In the JDBC Request define "Variable Names", i.e. ACTUAL_COUNT
Once done you can compare the ACTUAL_COUNT variable value with the SAMPLE-NUMBER variable like:

Related

Implementing filters in ORM golang

I am working on an api which takes in parameters for filters (as given below)
/api/endpoint?filter_key_1=filter_value_1&...
I've previously worked on spring where the criteria API allows for dynamically building SQL queries without much hassle. In golang I'm using gorm for handling the ORM operations. Is there anyway to build the queries with optional parameters without writing redundant code?.
For example:
If the request sent is:
/api/endpoint?fk_1=fv_1&fk_2=fv_2&fk_3=fv_3
Query generated should be :
select * from table where fk_1 = fv_1 AND fk_2 = fv_2 AND fk_3 = fv_3
but in case of :
/api/endpoint?fk_1=fv_1
Query generated should be:
select * from table where fk_1 = fv_1
Currently my approach is to check if each variable is present and build the query as a string :
query:="select * from table where "
if fk_1 != ""{
query += "fk_1 = fv_1"
}
... and so on
but this seems very awkward and error prone
Any help will be appreciated! Thanks
EDIT
Building on #bjornaer's answer what helped me was to get the map[string][]string in a form that I can send the same to gorm, map[string]interface{}.
This thread will help in the same.
Now there's no longer a need for redundant checks or string operations in the filters
so it seems to me your question has 2 parts:
you need to retrieve your query values from your url and
insert them to your db query
I don't see how you are handling your requests so let's assume you use the http package: from req.URL you get the URL object and from that calling the Query() method yields a map[string][]string of your query parameters, with those in a variable URLQuery let's pause and look at how you query with gorm:
db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
QueryFields: true,
})
here I open a sqlite, then you can pass a variable reference to fill with your query, for example:
result := db.Where(map[string]interface{}{"name": "jinzhu", "age": 20}).Find(&users)
now from the example above, replace your variable in:
result := db.Where(map[string]interface{}URLQuery).Find(&users)
you can find it in the docs

How to fetch 2 DB column value and compare it with Single JSON element in JMeter

Below is the Table:
Customer ID     Customer number
1                        ABC123
null                    DEF123
JSON variable name is, CustomerDetail:
In my scenario I want to check if Customer ID is not null then in JSON, Customer ID should display against CustomerDetails. That means CustomerDetails: "1"
If Customer ID is null then in JSON Customer number should display against CustomerDetails. That means CustomerDetails: "DEF123"
How Can I perform this validation in JMeter using JSR223 assertion.
Question: For CutomerDetails (In JSON)- if CustomerID is not null then display its value otherwise display value for customer number. In below provided code how can I fetch value of both the column from Db and then compare them?
Your question is unclear, if you configure your JDBC Request sampler as follows:
it will generate the following JMeter Variables
CustomerID_1=1
CustomerID_2=null
CustomerID_#=2
CustomerNumber_1=ABC123
CustomerNumber_2=DEF123
CustomerNumber_#=2
In the JSR223 Assertion you can use vars shorthand for JMeterVariables class instance in order to access variable values like:
1.upto(vars.get('CustomerID_#') as int, { index ->
if (vars.get('CustomerID_' + index) == 'null') {
//do something
} else {
//do something else
}
})

How to extract value from Jmeter get property and save as variables. These values will be used in api call

getproperty values passed from Thread Group 1 to Thread group2
Result from BeanShell assertion
Step 1- USing jdbc request to get data from database with 2 columns and multiple rows.
Step 2 - From ThreadGroup 1, Set property to the database results using ${__setProperty(StateCodeProperty,${stateDetails})};
Step 3 - Access in Thread Group 2 by get property using beanshell assertion- String result = (vars.get("${__property(StateCodeProperty)}")); I need help on how to separate the columns and use it in api call. –
In any case if you want to access the DB results in different Thread group then you can try to do something like this inside beanshell assertion (not sure though) -
ArrayList results = ${__property(StateCodeProperty)}; //it should return the object as an arraylist
for (int i; i < results.size(); i++) {
if (results.get(i).get("statecode").equals("NY")) { //iterating the results, 'statecode' is the name of your 1st column, similarly you can do for 'State'
//Do your comparisons or whatever you like here
}
}

jmeter jdbc request parameter type

I am constructing a jmeter jdbc Prepared Select statement request.
I have the query as,
select * from tableName where c=?
Parameter Value: ${columnId}
Parameter Type: LONGVARCHAR
When I run I get a: type mismatch error UNSIGNED_LONG and CHAR for c='1234'
I need to pass a long value here. Also, how would I pass a binary array?
You should use User defined variables as reference. This way you can avid using Parameter fields.
Reference: http://jmeter.apache.org/usermanual/functions.html
select * from tableName where c='${columnId}'

IBM Mobilefirst SQL adapter with parameters for select IN statement

I've a problem calling a MobileFirst SQL adapter to retrieve result from an Oracle Select-where-IN statement.
My environment is MobileFirst v7.0
The adapter definition is:
sqlGetResultsStatement = WL.Server.createSQLStatement ("select * from table where field IN (?)");
function getResults(param) {
return WL.Server.invokeSQLStatement({
preparedStatement : sqlGetResultsStatement,
parameters : [param]
});
}
If I call adapter with a single value in parameter (e.g. '0001') all works fine, and I obtain results. But if I call adapter with this type of parameter: "'0001','0002','0003'" I obtain an empty-resultset (w/o errors) response like this
{
"isSuccessful": true,
"resultSet": [
]
}
Is there something in the call that is wrong?
You cannot pass in a set of values to a single prepared statement parameter. One of the key features of prepared statements is that it helps prevent SQL injection attacks and thus it makes sense that when there is one parameter inside of the statement it is considered as a single value, that's why it encloses the value you passed in with quotes "'0001','0002','0003'". Also, MobileFirst doesn't allow you to create prepared statements inside if functions in the JS adapters and therefore you cannot modify the number of parameters when the procedure is invoked. With that being said there are two approaches you can take to accomplish this.
Javascript Adapter
Determine the maximum number of parameters that you will ever pass to this procedure and add the parameters to the prepared statement beforehand. Let's say you are never going to pass more than 10 parameters, then I would use something like:
var MAX_PARAMS = 10;
sqlGetResultsStatement = WL.Server.createSQLStatement ("select * from table where field IN (?,?,?,?,?,?,?,?,?,?)");
function getResults(param) {
/*
* (arguments) is an array of parameters passed to the procedure
*/
return WL.Server.invokeSQLStatement({
preparedStatement : sqlGetResultsStatement,
parameters : fillVars(arguments)
});
}
// helper function to fill all the values for the SQL Statement Parameters
function fillVars(vars) {
var list = [];
for(var i = 0; i < MAX_PARAMS; i++) {
if(vars.length >= i + 1) {
list.push(vars[i]);
} else {
// some value that will not be in your db
list.push(null);
}
}
return list;
}
Java Adapter The other option would be to use a Java Adapter and connect to your DB directly and write your own queries/prepared statements. FYI: this option will give you more flexibility but you will have to include DB driver jar files and write all the DB connection/querying logic, etc.

Resources