SoapUI: How to cast 0 as 'false' - xpath

I'm using SoapUI to test a WCF service. I have an XPath Match assertion in which the Declare is:
if (boolean(//a:IsClick/text()[1])) then //a:IsClick else ''
For the source XML, the node is
<a:IsClick>false</a:IsClick>
so the Declare section equates to 'false'.
The Expected box has this:
${#ResponseAsXml#(//CLICK[text()],'')}
and the XML (from a JDBC Test Step) is:
<CLICK>0</CLICK>
so the expected value is 0.
I need to have these two equate so my assertion will pass. One way to do this would be to cast the Expected result from 0 to 'false'. How can I do that? Or is there a better approach?

In XPath the boolean() function returns a boolean for number, string, or node-set. In your case where you want to cast a number to a boolean, boolean(0) returns false, for the rest of numbers boolean(n) returns true. In the other hand boolean() of string returns false for boolean('false') or for boolean('') (empty string) for the rest of strings boolean() returns true. So your problem is that using text() you're getting the '0' as string instead of a number so when you try to cast boolean('0') you're getting true.
In your case if you've some XML result from your JDBC Test Step like:
<Results>
<CLICK>0</CLICK>
</Results>
You can convert this 0 to false adding boolean() to your expression and also using number() function instead of text(). So to cast the 0 to false use:
${#ResponseAsXml#(boolean(//CLICK[number()]))}
instead of:
${#ResponseAsXml#(//CLICK[text()],'')}
Hope this helps,

The simplest solution is to turn this into a Groovy problem - a Groovy assertion.
Here is a visualization (see documentation):
def negIsClick = "false"
def negCLICK = "0"
def posIsClick = "true"
def posCLICK = "1"
// fake "cast" the text to boolean
assert !(negIsClick.equals("true") ?: false)
assert (posIsClick.equals("true") ?: false)
assert !negCLICK.toInteger() // zero is false
assert posCLICK.toInteger() // all other numbers are true
// note the exclamations everywhere
assert !(negIsClick.equals("true") ?: false) == !negCLICK.toInteger()
assert !(posIsClick.equals("true") ?: false) == !posCLICK.toInteger()
// this fails
assert (negIsClick.equals("true") ?: false) == negCLICK.toInteger()
The last one fails, because you cannot compare a boolean to an integer. But in the cases before that, the ! first casts everything to booleans.
So in your case, you will need to do something like:
// read in the two values
def IsClick = context.expand( '${XML test step#Response//*:IsClick}' )
def CLICK = context.expand( '${JDBC test step#ResponseAsXml//*:CLICK}' )
// now compare them
assert !(IsClick.equals("true") ?: false) == !CLICK.toInteger()

assert '1'.toInteger().asBoolean()
assert !'0'.toInteger().asBoolean()

Related

I get this error: 'list' object has no attribute 'countPoints'

When I import this code that I typed in here in to another code I get the error: 'list' object has no attribute 'countPoints' .
import ModuleRollDice as dce
def judge(rollPlayerA,rollPlayerB):
if not all(item in dce.options for item in rollPlayerA) or
not all(item in dce.options for item in rollPlayerB):
print("error")
if rollPlayerA.countPoints(rollPlayerA[0]) == len(rollPlayerA) and
rollPlayerB.countPoints(rollPlayerB[0]) != len(rollPlayerB):
return "A"
if rollPlayerA.countPoints(rollPlayerA[0]) != len(rollPlayerA) and
rollPlayerB.countPoints(rollPlayerB[0]) == len(rollPlayerB):
return "B"
elif sum(rollPlayerA) == sum(rollPlayerB):
return "tie"
elif sum(rollPlayerA) > sum(rollPlayerB):
return "A"
else:
return "B"
return
It appears as though the arguments passed to rollPlayerA/B are not list types objects. You should sanity check this beforehand to ensure that what is recieved is in fact a list type object. The issue can be resolved by both sanity checking and looking at the caller function and seeing what is being sent as arguments.
A simple sanity check could look like:
def judge(rollPlayerA,rollPlayerB):
if type(rollPlayerA) is not type(list) or
type(rollPlayerB) is not type(list):
print("judge function did not recieve proper input types")
return
...
Another way of doing this would be using pythons assert
def judge(rollPlayerA,rollPlayerB):
assert(type(rollPlayerA) == type(list)), "rollPlayerA is not a list"
assert(type(rollPlayerB) == type(list)), "rollPlayerB is not a list"
An even nicer way of doing this would be using PEP 3107's implementation of type assertions in definition declarations:
def judge(rollPlayerA: list, rollplayerB: list) -> None:
....
This also allows you to remove the redundant 'return' statement at the end of your function, which you didn't really need anyways

Ruby transform string to method name

To test image urls in a ruby project I can call a function as follows
validate_url("some_valid_url", "valid")
validate_url("inv#alid-url", "invalid")
The function looks like this:
def validate_url(image_url, state)
assert state === 'valid' ? new_product(image_url).valid? : new_product(image_url).invalid?,
"#{image_url} should always be " + state.upcase
end
is there a way to rewrite the line:
assert state === 'valid' ? new_product(image_url).valid? : new_product(image_url).invalid?
to something like
assert new_product(image_url).state.What-To-Do-Here?
which would then be equals to the following if state contains the string "valid"
assert new_product(image_url).valid?
You can do....
assert new_product(image_url).send(state + '?'), "#{image_url} should always be #{state.upcase}"
It would be prudent to ensure that state can only contain "valid" or "invalid"

How can check variables is null or empty with one function in laravel 8?

I want to check if there is a function in laravel 8 like IsNullOrEmpty for C# to check for empty and null value using just one function.
you can use empty() PHP function.
The empty() function checks whether a variable is empty or not.
This function returns false if the variable exists and is not empty, otherwise it returns true.
The following values evaluates to empty:
0
0.0
"0"
""
FALSE
array()
NULL

Return True if Array Contains a Specific String - JSONata

Is there a way in JSONata to have a function return TRUE if it finds a specific string within a provided array? For example I have an array of colors:
const myArray = [red,blue,green,pink]
I am trying to figure out an expression that would search that array for "blue" and return true if it finds the value.
On the JSONata documentation, I found a function called $boolean(arg)that I think I would need to use but I'm not sure how to implement it. The documentation shows an argument type option as "array: contains a member that casts to true", but I can't really tell how to implement it.
Would it be as simple as $boolean(myArray, "blue")?
The in operator is what you need. See https://docs.jsonata.org/comparison-operators#in-inclusion
In your case, the expression "blue" in myArray will return true. See https://try.jsonata.org/r0q7GnSOh
edit: Thought this was python, but maybe you could use something similar for JSONata
you can make a for loop with an if condition to check your condition
listOfStrings = ['red','green','blue']
for strings in listOfStrings:
if listOfStrings[strings] == 'blue':
return True

cast XPath Expression to Boolean in Apache Camel

I have xml with flag:<ns4:flag>false</ns4:flag>. And I want to read this flag and set it's value to FLAG property:
.setProperty( FLAG, xpath("//*[local-name()='flag']/text()", Boolean.class))
Using code above I get 'true' value instead of 'false'. Also tried resultType(Boolean.class) and boolean() xpath function inside expression, but it didn't work out. Any ideas how can I do this cast?
In XPath, the following expression will return boolean value true when the text content equals string value "true", and return boolean value false otherwise :
//*[local-name()='flag']/text() = 'true'
xpathteseter.com demo
So I guess, changing your XPath expression to the above XPath would work.

Resources