I am using parasoft SOATest to test a service response and I got a failure
Message: DataSource: products (row 1): Value Assertion: For element "../item", expected: abc but was: bcd
My Requirement is to validate the following response.
{
"samples" : {
"prds" : [
"abc",
"bcd"
]
}
}
And I have a datasource table which is like follows. First row as the column name.
prds
abc
bcd
In the SOATest I have a JSON Assertor and inside JSON Assertor I have configured a Value Assertion. In the Value Assertion I selected the first item and then in the next step I selected Apply to all "item[*]". Then Finish.
In the Expected Value I select Parameterized and select the prds from the drop down menu.
After all when the service return the above payload it failed with the above given message.
Is this a bug/limitation of SOATest or am I missing some step in here.
I believe this is just because you opted Apply to all "item[*]" instead of Apply to "item[1]" Only
Related
I'm trying to get python to create a json formatted like :
[
{
"machine_working": true
},
{
"MachineName": "TBL165-169",
"MachineType": "Rig Test"
}
]
However, i can seam to do it, this is the code i have currently but its giving me error
this_is_a_dict_too=[]
this_is_a_dict_too = dict(State="on",dict(MachineType="machinetype1",MachineName="MachineType2"))
File "c:\printjson.py", line 40
this_is_a_dict_too = dict(Statedsf="test",dict(MachineType="Rig Test",MachineName="TBL165-169")) SyntaxError: non-keyword arg after
keyword arg
this_is_a_dict_too = [dict(machine_working=True),dict(MachineType="machinetype1",MachineName="MachineType2")]
print(this_is_a_dict_too)
You are trying to make dictionary in dictionary, the error message say that you try to add element without name (corresponding key)
dict(a='b', b=dict(state='on'))
will work, but
dict(a='b', dict(state='on'))
won't.
The thing that you presented is list, so you can use
list((dict(a='b'), dict(b='a')))
Note that example above use two dictionaries packed into tuple.
or
[ dict(a='b'), dict(b='a') ]
I have a JSON response like this
Members":
[
{
"id":"ABC",
"name":"XXXX",
"XXX":"XXX",
"XXXX":"XXXX",
"Managers":
[
{
"id":XYZ,
"name":"XXX",
"XXXX":XXXX,
}
],
I need to get the value ABC and XYZ from the above response and I am using 2 JSON extractor to fetch the value and storing it in different variable.
JSON Extractor 1 expression:-
$..Members.[*].id
JSON Extractor 2 expression:-
$.Members..Managers.[*].id
But the above code picks the value from different arrays like sometime it picks the Members id as ABC but picks the Managers ID from different array. I want it to pick the value from same array value.
Any suggestions?
Assuming that you need to extract first member and his first manager:
Add JSON Extractor as a child of the request which returns above JSON and configure it as follows:
Variable names: memberId; ManagerId
JSON Path expressions: $.Members[0].id; $.Members[0].Managers[0].id
Match No: 1; 1
Default Values: NOT_FOUND; NOT_FOUND
Refer ABC as ${memberId} and XYZ as ${ManagerId} where required. You can see JMeter Variables using Debug Sampler and View Results Tree Listener combination
I need value from MySQL send into PLC via OPC UA (in NODE-RED). Everything works ok, but I don't know how to get pure value without descriptions of array etc.
I use this code:
SELECT `user_info` FROM `users` WHERE `user_name` LIKE 'Lukas'
The answer is:
array[1]
0: object
user_info: "6"
If I send it to PLC as STRING the value in PLC is:
[object Object]
Can I edit the code somehow? I need answer only:
6
Thank you
The answer is array[1] 0: object user_info: "6"
I assume you've copied that out of the Debug window which shows you the exact structure of the payload you've received.
That is saying, the payload is an array with a single element. That element is an object with a user_info property of value 6.
In other words:
[
{
"user_info": "6"
}
]
In which case, to access the value you would use:
msg.payload[0].user_info
For example, a Function node to pull that value out and put it into the payload would be:
msg.payload = msg.payload[0].user_info;
return msg;
Or you could use a Change node to set the value of msg.payload to the value of msg.payload[0].user_info.
I am working with:
Spring MVC Test
Hamcrest
JsonPath
I have the following how a response from the server:
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = {
"id" : "100",
"nombre" : "Jesús Você",
"apellido" : "Mão Nuñez",
"fecha" : "1977-12-08"
}
Forwarded URL = null
Redirected URL = null
The following works as expected (is valid):
.andExpect(jsonPath("$").exists())
.andExpect(jsonPath("$", notNullValue()))
.andExpect(jsonPath("$", isA(LinkedHashMap.class)))
.andExpect(jsonPath("$.*").exists())
.andExpect(jsonPath("$.*", notNullValue()))
.andExpect(jsonPath("$.*", isA(JSONArray.class)))
.andExpect(jsonPath("$.*", hasSize(is(4))))
I need test that ("$") is 1. Confirm exists 1 item. It to confirm again the following:
Body = {
"id" : "100",
"nombre" : "Jesús Você",
"apellido" : "Mão Nuñez",
"fecha" : "1977-12-08"
}
I've tried:
.andExpect(jsonPath("$", hasSize(is(1))))
Observe the difference between $ and $.*, for the latter I know it counts the number of fields. But from the former I always get:
java.lang.AssertionError: JSON path "$"
Expected: a collection with size is <1>
but: was <{id=100, nombre=Jesús Você, apellido=Mão Nuñez, fecha=1977-12-08}>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18
'Seems' the data is not a collection, but remember that .andExpect(jsonPath("$", isA(LinkedHashMap.class))) pass. I am confused in someway.
Therefore is possible test that ("$") is 1.? If yes, how?.
I've read count members with jsonpath?
And says:
To test size of array: jsonPath("$", hasSize(4))
To count members of object: jsonPath("$.*", hasSize(4))
My data returned is not an array, it is a LinkedHashMap.class because if I use .andExpect(jsonPath("$").isArray()) I get:
java.lang.AssertionError: Expected an array at JSON path "$" but found: {id=100, nombre=Jesús Você, apellido=Mão Nuñez, fecha=1977-12-08}
Expected: an instance of java.util.List
but: <{id=100, nombre=Jesús Você, apellido=Mão Nuñez, fecha=1977-12-08}> is a java.util.LinkedHashMap
BTW: .andExpect(jsonPath("$.*").isArray()) pass.
To validate the size of a Map instead of:
.andExpect(jsonPath("$", hasSize(1)))
you should use:
.andExpect(jsonPath("$", aMapWithSize(1)))
Note: Check this link with org.hamcrest.Matchers javadoc
I am trying to use Groovy script for my Json response in soapui and finding if the property exists in my response. I used If statement as below. For what ever property name i use to check it returns only true statement even though if the property not exists. I am not sure whats wrong I am doing here.
import groovy.json.JsonSlurper
def slurper = new JsonSlurper()
def i = 0
responseContent = testRunner.testCase.getTestStepByName("DAY").getPropertyValue("response")
slurperresponse = new JsonSlurper().parseText(responseContent)
if(slurperresponse.day_details.activities.to_locans) {
println "************************"
res = "Crew_base found"
} else {
res = "Crew_base not found"
}
This is caused by a few things coming together:
Groovy returns a null for unknown keys
Groovy lets you use xpath type variable lookup, including if that goes via a nested list
Groovy truthy on a list is based on the list size - even if the list only contains null, if its not empty, then it evaluates to true.
Your comment above reveals the problem:
For valid property name
Fri Apr 22 16:01:19 EDT 2016:INFO:<java.util.ArrayList#260080 elementData=[[NYP, NYP]] size=1 modCount=1>
For invalid property name
Fri Apr 22 16:01:35 EDT 2016:INFO:<java.util.ArrayList#3e0 elementData=[[null, null]] size=1 modCount=1>
You see in both cases the result is a list (of lists) of two items - so always evaluates to true.
As a map, from your final comment where you print the dump of that variable, it looks like this:
[
day_details: [
[
fra_status:'', block_training_day:0, holiday_name:'', activities:[
[ from_location:'WAS', craft:'Conductor', departure_datetime:'2016-04-26T13:02:00-04:00', end_datetime:'2016-04-26T16:30:00-04:00', crew_base:'NYP', ends_next_day:false, description:'Unit', train_number:172, to_location:'NYP', arrival_datetime:'2016-04-26T16:30:00-04:00', zone:'Zone 2', start_datetime:'2016-04-26T12:52:00-04:00' ],
[ from_location:'NYP', craft:'Engineer', to_location:'WAS', zone:'Zone 2', start_datetime:'2016-04-26T17:55:00-04:00' ]
]
]
]
]
Reading that, you will see that day_details and activities are both nested lists (hence the two lists in the response - so groovy navigates to each of the maps inside activities and looks for the key to_locans - not finding the key, it returns null, and puts those two nulls into a list.
Changing the if check to the following will work as you originally intended (that is with the assumption that you only want to ever consider the first element in each of those lists):
if (slurperresponse.day_details[0].activities[0].to_locans){