cancel
Showing results for 
Search instead for 
Did you mean: 

Assertions with conditions

SOLVED
Highlighted
Occasional Contributor

Assertions with conditions

Hi,

I am new to SOAP UI, I have a JDBC request which is a select query and it returns an XML response. 

JDBC request (say Request B)==> In response, there is a field, Order total which returns 0 or +ve interger based on response returned in the previous response(say REST request A) (which returns a +ve or -ve integer)

 

So I want to check if the Order total(returned in request B) matches the response returned in request A only when reponse of A returns a +ve value. How can i acheive this assertion when I have a condition to be given?

 

Thanks in advance,

Udaya

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: Assertions with conditions

I mocked up a rest request with the response you provided, but I ran into a problem mocking out a JDBC request since I don't have ready access to a database. Still, I was able to put together a groovy script that will compare a value returned by the rest request in the morphOnlyNetAmount in the response against a REFUNDABLE_AMOUNT if the latter is a postive integer. You'll need to adjust the script to your own uses, but it should be a good start.

 

package json
import com.eviware.soapui.support.XmlHolder
import groovy.json.*


// Cna use the right-click Get-Data menu here to get the rest request response.
def restResponse = context.expand( '${REST Request#Response}' )

log.info(restResponse);

def r = new JsonSlurper().parseText(restResponse)

// Gets the morphOnlyNetAmount from response and converts to float.
def restValue = r.morphOnlyNetAmount.toFloat();

// You need to use the right-click Get-Data menu here to ensure it works as expected. Without a fully formed JDBC response, I cannot fully mock this out. 
def jdbcResponse = context.expand( '${JDBC Requests#Response#//REFUNDABLE_AMOUNT[1]}' ).toFloat();


if (jdbcResponse > 0)
{
	assert (jdbcResponse == restValue)
}




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

12 REPLIES 12
Highlighted
Community Leader

Re: Assertions with conditions

Hi @US_20 ,

 

First get the two different values in two different variables, then assert both of them for example:

 

def responseA = "Get the response"

def responseB = "Get the response"

 

assert reponseA == responseB : "response of A and B not matched"

 


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Highlighted
Community Hero

Re: Assertions with conditions

Hi,

I just want to make sure i understand.

You have a REST request that will return a response
You have a JDBC request that will return a response

You want to compare an attribute value from each response but ONLY if the one response returns >0 rows?

Cheers,

richie
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
Occasional Contributor

Re: Assertions with conditions

Hi Richie ,

 

Thanks for looking into my query. So here is my request:

 

1. REST request which will return a field A (+ve or -ve integer) as response

2. JDBC request that will return a response which has Order Total field (field B)-- can be 0 or +ve integer

 

Now I need to make sure if field B value and field A value are equal only if field B returns a +ve integer(>0)

 

Can you pls help me how to assert this?

 

 

Highlighted
Community Hero

Re: Assertions with conditions

My first thought is that this may have to be done via a groovy script. Can you provide some responses, sample or mocked up, so that we can see exactly what we're dealing with?




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Highlighted
Occasional Contributor

Re: Assertions with conditions

Hi ,

Here is what my sample response looks like:

 

Below is the response returned by the REST request

{
"morphOnlyNetAmount" : -20.31---- (This value can be a +ve or -ve value)
}

 

The JDBC response will return a field <REFUNDABLE_AMOUNT> -- can return 0 or >0.

 

Now I need to make sure if REFUNDABLE_AMOUNT value and morphOnlyNetAmount value are equal only if  REFUNDABLE_AMOUNT returns a +ve integer(>0)

 

 

 

So the 

Highlighted
Community Hero

Re: Assertions with conditions

Okay, that helps a bit! And pardon my potential ignorance, but what is +ve and/or -ve integer? What does that stand for? I am not familiar with that notation.




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!
Highlighted
Occasional Contributor

Re: Assertions with conditions

No worries 🙂 Sorry +ve is positive and -ve is negative.

Highlighted
Community Hero

Re: Assertions with conditions

I mocked up a rest request with the response you provided, but I ran into a problem mocking out a JDBC request since I don't have ready access to a database. Still, I was able to put together a groovy script that will compare a value returned by the rest request in the morphOnlyNetAmount in the response against a REFUNDABLE_AMOUNT if the latter is a postive integer. You'll need to adjust the script to your own uses, but it should be a good start.

 

package json
import com.eviware.soapui.support.XmlHolder
import groovy.json.*


// Cna use the right-click Get-Data menu here to get the rest request response.
def restResponse = context.expand( '${REST Request#Response}' )

log.info(restResponse);

def r = new JsonSlurper().parseText(restResponse)

// Gets the morphOnlyNetAmount from response and converts to float.
def restValue = r.morphOnlyNetAmount.toFloat();

// You need to use the right-click Get-Data menu here to ensure it works as expected. Without a fully formed JDBC response, I cannot fully mock this out. 
def jdbcResponse = context.expand( '${JDBC Requests#Response#//REFUNDABLE_AMOUNT[1]}' ).toFloat();


if (jdbcResponse > 0)
{
	assert (jdbcResponse == restValue)
}




---

Click the Accept as Solution button if my answer has helped, and remember to give kudos where appropriate too!

View solution in original post

Highlighted
Occasional Contributor

Re: Assertions with conditions

Thanks so much.. it worked!

New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Compete with community members in the TechCorner Challenge and get into the Leaderboard!

Rank Participant Points Earned
1 msiadak 12
2 nmrao 8
3 HimanshuTayal 3
Top Kudoed Authors