Forum Discussion

richie's avatar
richie
Community Hero
6 years ago
Solved

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...
  • aaronpliu's avatar
    aaronpliu
    6 years ago

    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