Ask a Question

A bit more groovy help? Populating a REST URI with Properties Set On TestCase?

SOLVED
richie
Community Hero

A bit more groovy help? Populating a REST URI with Properties Set On TestCase?

Hey!

 

I'm currently trying to play around with some groovy snippets people have helped me with to build a REST request using parameters saved as properties on the TestCase object - but surprise, surprise! it doesnt work! 

 

to reiterate - I was thinking about setting a couple of properties on the TestCase object and then grabbing the values to build the REST URI. 

 

So - I was thinking have a groovy step, followed by a REST step - the groovy is as follows:

 

// get CommodityType_Name and EffectiveFrom properties from TestCase - i.e. properties were set at TestCase level level
def prop1 = testRunner.testCase.getPropertyValue( "CommodityType_Name" )
def prop2 = testRunner.testCase.getPropertyValue( "EffectiveFrom" )


// write the CommodityType_Name to the REST Request step
testRunner.testCase.testSteps["REST Request"].setPropertyValue( "CommodityType_Name", prop1 )

log.info("prop1")

// write the EffectiveFrom to the REST Request step
testRunner.testCase.testSteps["REST Request"].setPropertyValue( "EffectiveFrom", prop2 )

log.info("prop2")

First things first - according to some help @Lucian gave me last week - I'm thinking my mistake here is that I need to expand the property first (but I don't know the syntax to do this on my "getProperty line" line) - currently my log.info debug attempt is returning the propertynames rather than the values - would anyone know how to do this?

 

 

Finally - when I run the test case - I was 'hoping' that the CommodityType_Name and EffectiveFrom parms and values would be built into the REST URI - they weren't.  Would anyone know what I'm doing wrong here?

 

 

LowPriority:  Finally - and i know this is a little open subject so shouldnt be asking - but its always so frustrating my having to ask  you guys for coding help....does anyone know any "SoapUI groovy from scratch, no previous coding experience" books/resources/artefacts I can pick up?  There's lots of stuff out there - but its for the person who can already code - I need something that can teach me about it knowing absolutely nothing, the groovy tutorials out there aren't tailored for someone with my minimal coding skillset!

 

Thanks guys - appreciate all/any advice/guidance/help you can give me!

 

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
12 REPLIES 12
nmrao
Champion Level 2

@richie,

Could not follow all along the issue with REST. I know you had created a feature request as well. Are you still talking about it? Can you simplify the issue otherwise?

However, below thread should help regard improving to use groovy in SoapUI
https://community.smartbear.com/t5/SoapUI-Open-Source/How-to-read-and-use-SoapUI-API-for-non-Dev-peo...


Regards,
Rao.
richie
Community Hero

Hey @nmrao 

 

yeah - I'm still talking about the same thing - I'm getting boring - right? 🙂

 

So - yeah - I have a REST GET API where the query parameters on the request correspond to attributes of tables.  This will be for a reference data service providing lots of different internal systems with referencedata when required.

 

So - say I have a table called 'PersonalDetails' with the following attributes: ID, firstname, lastname, country, phonenumber and multiple records populate this table - but for now lets use the following data

 

ID = 123456

firstname = rich

lastname = jones

country = uk

phonenumber = 07958 985 000

 

In the system - namespace is a grouping of tables and dataset is an alias for tables - the following GET URI 

 

my GET URI --> http://help.azurewebsites.net/api/1/{namespace}/{dataset}?queryparm1=value&queryparm2=value

 

the RAW of the above would be as follows:

 

http://help.azurewebsites.net/api/1/biology/PersonalDetails?firstname=rich&lastname=jones

 

Now - cos I can't parameterize the QueryParm names (values I can parameterize, but not the names) with the OTB functionality - the only option left to me is to hardcode multiple REST requests in the Projects section of ReadyAPI! essentially hardcoding the QueryParameters.  I have 1500 tests at the moment - thats a lot of requests in my project.

 

Hence the reason I'm trying to put something together that allows me to call the queryparms and values to build the query string of my GET request from say a Properties step.

 

For example - if I can query the table and extract the columns and a single record's values - I can build a Properties step containing all attributes and the values and then hopefully I can build the REST requests QueryParameters from the attributes and values set in the Properties step 

 

(OR - I can just create a single properties file at the start and load in the table attributes and 1 record's values for later use)

 

 

I hope I've been clear - I know I tend to ramble a bit - but its to try and ensure I've been clear in my query.

 

Thanks for providing the URL - I have questions about this - but I'll ask in a separate post

 

Again thanks to all and Rao for helping me out so much!  I even submitted a support call to SmartBear - but they don't support groovy coding so I know they're just gonna reject my support request - but I thought it was worth a shot!

 

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
nmrao
Champion Level 2

@richie,

Thank you for your patience. Bear with me, few more questions.

1.  You like to query db and then use the data in GET call of REST. correct? If yes, then the issue which you are mentioning for REST query parameters will be an issue even to do dynamic sql query as well. Isn't it?

2. The final objective is to compare sql query response with REST call (GET with query parameters) response. Is this understanding correct?



Regards,
Rao.
richie
Community Hero

Hey,

 

ok - I tried an approach @aaronpliu suggested that I'd mistaken as a lot more complicated than it appears to be

 

I followed all the instructions, setup the groovy - but the REST step is NOT inserting the Query parms (defined as properties) into the GET request.  

 

The test case hierarchy is as follows:

 

PropertiesStep
GroovyStep1
RESTStep
GroovyStep2

The testcase and groovy executes fine - the 'setPropertyValue' just isn't adding the properties as query parms/values at all

 

I have a setupscript as follows:

// initialize a property "index" for loop
testCase.setPropertyValue("index", "0")

The properties step holds 2 parms and values

 

GroovyStep1 as follows:

//setup size of properties in order to loop run
def propsList = testRunner.testCase.testSteps["Properties"].getPropertyList()
def index = context.expand('${#TestCase#index}').toInteger()
testRunner.testCase.setPropertyValue("CountOfProps", propsList.size().toString())
// setup parameter for REST step
testRunner.testCase.testSteps["RestStep"].testRequst.setPropertyValue(propsList[index].name, propsList[index].value)

GroovyStep2 is as follows:

 

def index = context.expand('${#TestCase#index}').toInteger()
def max = context.expand('${#TestCase#CountOfProps}').toInteger()

if (index + 1 < max) {
    // go to run step 1, here 'Groovy' name is step 1 name
    testRunner.testCase.setPropertyValue("index", (++index).toString())
    testRunner.gotoStepByName("GroovyStep1")
}
// note that your "REST" step will be added more parameters, if you just want to run "REST" step with one parameter, then you need to "remove" others or setup empty

 

In summary - test executes, groovy steps work without reporting compilation/runtime errors - but the REST URI doesnt get generated with the parameters specified in the properties file.

 

I thought I was onto a winner here - cos lets face it - it's someone else's groovy - can anyone point me in direction where I'm going wrong?

 

Oh yeah - I've attached the project file wiht a single test case of the above if anyone wants to have a look!

 

Again thanks to all -especially @aaronpliu for supplying the groovy in first place (which I idiotically didn't notice)

 

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
richie
Community Hero

Hey @nmrao,

I think we posted simultaneously, hence the reason my last post doesnt answer your query.
I will respond tomorrow,

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
nmrao
Champion Level 2

No worries.

If the test objective is clarified from bird's eye view/high leve (not in terms of what steps in the test case etc) it would be good.
For instance, database query result and rest should be equal. Add if there are any conditions to satisfy.



Regards,
Rao.
richie
Community Hero

Hey!

 

sorry - I tend to ramble on and not be clear in my posts - so apologies if I wasn't clear.

 

We are developing a new ReferenceData system that will serve multiple internal systems with this referencedata.  For example - in the new ReferenceData system there will be a table called 'country'.  This will hold various country related details - countryname, countrycode, currency, etc. and these internal systems will source this data rather to populate certain field attributes in their applications rather than contain referencedata tables themselves.

 

These internal systems will access the ReferenceData system via a GET request.  The GET request's parameters are split into template/uri and query parameters.  The REST API's template/uri parameters specify the table being queried, the REST API's query parameters wiill specify the columns (within the table being queried) (and some additional operators equivalent to a number of partial match operators as well as sorting the returned data).  These internal systems can query the ReferenceData system's tables using ANY of the columns of the columns in the tables.

 

Essentially the GET request will execute the equivalent of an SQL select query with the following basic format:

select * from table
where column1 = 'X'
and column2 = 'Y'
and columnX = 'Z'

The request also includes SQL partial matches as follows (SQL equivalent and REST API equivalent below too):

 

All records where table's column 'contains', SQL is (column like '%value%'), REST API --> QueryParm=~value
All records where table's column 'starts with', SQL is (column like 'value%'), REST API --> QueryParm=^value
All records where table's column 'ends with', SQL is (column like '%value'), REST API --> QueryParm=$value
All records where table's column does 'not contain', SQL is (column not like '%value%), REST API --> QueryParm=!value

 

The request also includes support of the IN operator

 

All records where table's column contains values 'A' or 'B' or 'C' (column in ('A', 'B', 'C') --> QueryParm=[A,B,C]

 

The request also includes support of the ORDER BY (SORT) operator

Sort records by table's columnA in descending order, SQL (order by columnA desc), REST API --> _sort=QueryParm desc

To summarise the GET resource and parameters would look like the following - I've covered every function and SQL operator that is required (like, not like, =, IN, ORDER BY):

/api/1/{schema}/{table}?Column1=X
/api/1/{schema}/{table}?Column1=X&Column2=Y
/api/1/{schema}/{table}?Column1=X&Column2=Y&_sort=Column3 desc
/api/1/{schema}/{table}?Column1=~X
/api/1/{schema}/{table}?Column1=^X
/api/1/{schema}/{table}?Column1=$X
/api/1/{schema}/{table}?Column1=!X /api/1/{schema}/{table}?Column1=[X,Y,Z]

There is no limit to the number of table columns I can specify in the GET request, so if a table has 50 attributes, then strictly speaking I should be able to specify 50 columns as QueryParameters in the GET request.

 

Before I realised you couldn't parameterize a REST API's Query parameter names, I envisioned each of my tests have 3 test steps.

 

Step1 - GET (against a table - e.g. country) using a certain filter (currencycode=GBP)
Step2 - Properties - grab the filter value from Step1's resultset
Step3 - JDBCStep - select * from country where country.currency = '${Properties#currencycode}'

Essentially I was going to execute the GET request and follow up with a JDBC step - proving the GET request worked correctly by verifying the same data was retrieved in the GET request as what was returned in the JDBC step.

 

The purpose of my testing is as follows:

1. Prove the GET request works correctly (retrieves the info I expect),

2. Prove the different operator functions (contains, not contains, starts with, ends with, sort etc.) that equate to the SQL operators (like, not like, order by, IN etc.) work correctly

3. Prove the data is returned correctly (in json response) - charset is a ISO-8859-1 (so I'm inserting a record with the whole 8859 charset to ensure it displays correctly in the response

4. Prove the data is NOT truncated in the json response (I am going to insert a record maxing out the varchar columns that are included in the json response and verify the values are displayed correctly

 

 

Have I been clear?  I hope so!

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
nmrao
Champion Level 2

So the above helped?


Regards,
Rao.
nmrao
Champion Level 2

@richie ,

 

Here is the working groovy script which reads properties from Properties Test step(add this step if you are going to test below script) and use them as query parameters and make the REST Call (sample for GET call ).

 

Note that this does not use native REST Request Test step to make rest call. In stead uses a library which needs to be copied under READYAPI_HOME/bin/ext directory and restart the tool before running.

 

Selection_026.png

https://raw.githubusercontent.com/nmrao/soapUIGroovyScripts/9cc8d444c10ca99faada4dead7f63aea5f0f61bb...

 

Please change variables as per your environment and give it a try.



Regards,
Rao.
cancel
Showing results for 
Search instead for 
Did you mean: