Paramaterize the REST query parm NAME value rather than just the PARM value?
Hey,
I have a number of GET requests with a number of query parameters that emulate an SQL query filter.
Essentially I have 4 namespaces (databases) with multiple datasets (tables) and my GET request retrieves this info - but there are a number of additional query parameter functionality available to allow the user to filter the GET accordingly.
the following is an example of one of the possible requests:
GET --> /api/1/{namespace}/{dataset}?attributename=value
The above query would equate to the following in SQL
select * from database.table where attributename = value;
Now this 'attributename' can be ANY field in the table - I want to be able to parameterize this 'attributename' - but I've only ever parameterized query parm values in SoapUI - not the actual parm names.
Please see the image to provide a bit more clarity
As you can see - in the request I've setup - the query parm's name is 'attribute' - I woudl like some way to parameterize this so I can say do a GET and return a record then do a property transfer grabbing the json node name rather than just the node value and populate the query parameter's NAME as well as the VALUE.
If I can't parameterize this - it means a lot of hardcoding requests in the projects section - and that seems an awfully inefficient way of doing things.
I hope Ive explained what I mean clearly!
I'm totally stumped and don't know if ReadyAPI! actually does include a facility for what I need.
thanks to all!
richie
Hi richie,
You may take a look at following solution if you're interested in it
// Step 1: create 4 folders with your properties file. // 4 folders named with your database name // each properties file named with your table name under current database ./namespace1/table1.properties ./namespace1/table2.properties ... ./namepsace4/table16.properties // Step 2: create a config file to construct relationship of database and table // assume that namespace1 / table1 is name of database / table // (Example: the config file named with config.groovy) database { namespace1 = ["table1", "table2", "table3", "table4", "table5"] namespace2 = ["table6", "table7", "table8", "table9", "table10"] namespace3 = ["table11", "table12", "table13", "table14", "table15"] namespace4 = ["table16", "table117", "table18", "table19", "table20"] } // Step 3: set global property (in test case level) testRunner.testCase.setPropertyValue("namespace_index", "0") testRunner.testCase.setPropertyValue("table_index", "0") // Step 4: Groovy step to load config file and set property def index = context.expand('${#TestCase#namespace_index}').toInteger() def config = new groovy.util.ConfigSlurper().parse(new File('./config.groovy').toURI().toURL()) def _map = config.database Map.metaClass.takeAt = {int num -> def keys = _map.keySet() as String[] def key = keys[num] ["$key": _map.get(key)] } //add a extension method to get a sub map def map = config.database.takeAt(index) // return a submap def namespace = map.keySet() as String[] testRunner.testCase.setPropertyValue("namespace", namespace[0]) //namespace name testRunner.testCase.setPropertyValue("namespaceMap", groovy.json.JsonOutput.toJson(map)) testRunner.testCase.setPropertyValue("namespaceCount", config.database.size().toString()) // Step 5: Groovy step to set property of table def map = new groovy.json.JsonSlurper().parseText(context.expand('${#TestCase#namespaceMap}')) def key = map.keySet()[0] testRunner.testCase.setPropertyValue("tableCount", map.get(key).size().toString()) // Step 6: Groovy step to load properties file def props = new Properties() def map = new groovy.json.JsonSlurper().parseText(context.expand('${#TestCase#namespaceMap}')) def index = context.expand('${#TestCase#table_index}').toInteger() def tableName = map.entrySet()[0].value[index] testRunner.testCase.setPropertyValue("dataset", tableName) new File("./${tableName}.properties").withInputStream { props.load(it) } //remove property def teststep = testRunner.testCase.testSteps["Step7"] teststep.getPropertyList().each{ if (! it.name.equalsIgnoreCase("namespace") || ! it.name.equalsIgnoreCase("dataset")) teststep.testRequest.removeProperty(it.name) } //add property for (p in props) { //set all properties into REST step testRunner.testCase.testSteps["Step7"].testRequest.setPropertyValue(p.key, p.value) } // Step 7: REST step to query // parameterize "namespace" and "dataset" namespace = ${#TestCase#namespace} dataset = ${#TestCase#dataset} // Step 8: Groovy step to control table loop def table_index = context.expand('${#TestCase#table_index}').toInteger() def tableCount = context.expand('${#TestCase#tableCount}').toInteger() def namespace = context.expand('${#TestCase#namespace}') if (table_index + 1 < tableCount) { testRunner.testCase.setPropertyValue("table_index", (++table_index).toString()) testRunner.gotoStepByName("Step6") } else { log.info("Query all tables with parameters under $namespace") } // Step 9: Groovy step to control namespace loop def namespace_index = context.expand('${#TestCase#namesapce_index}').toInteger() def namespaceCount = context.expand('${#TestCase#namespaceCount}').toInteger() if (namespace_index + 1 < namespaceCount) { testRunner.testCase.setPropertyValue("namespace_index", (++namespace_index).toString()) testRunner.gotoStepByName("Step4") } else { log.info("Query all namespace done") }
Thanks,
/Aaron