Forum Discussion

KeyofSea123's avatar
KeyofSea123
Contributor
12 years ago

[Resolved] Iteration of a map using a list

I know how to build the map and the list but I can't seem to figure out how to retrieve the values that match the key names in the list.

I've tried so many things that I'm not sure what code to send to you. For each row of my data driven POST request, I could have as few as 1 pair in the map or as many as 21. I build my map and list for each data row. I'm trying to return the map value that matches the key from the list.

These are examples of my map and list:
map: ['requestRegion':'WA', 'requestCity':'SEATTLE', 'requestPostal_code':'98101', 'requestContact_phone_number':'2538159999', 'requestEmail':'Email20130412085140%40wvus.org', 'requestContact_phone_country_code':'1', 'requestDirect_mail_restriction':'DO_NOT', 'requestFirst_name':'First2013_04_12T08_51_40_546', 'requestPhone_restriction':'DO_NOT', 'requestAddress1':'085140546Lane20130412', 'requestCounty':'KING', 'requestLast_name':'Last2013_04_12T08_51_40_546', 'requestEmail_restriction':'DO_NOT', 'requestCountry_code':'US', 'requestParty_name':'First2013_04_12T08_51_40_546Last2013_04_12T08_51_40_546']

fieldNames: ['requestRegion', 'requestCity', 'requestPostal_code', 'requestContact_phone_number', 'requestEmail', 'requestContact_phone_country_code', 'requestDirect_mail_restriction', 'requestFirst_name', 'requestPhone_restriction', 'requestAddress1', 'requestCounty', 'requestLast_name', 'requestEmail_restriction', 'requestCountry_code', 'requestParty_name']

If I have the wrong approach please tell me the best way to do this.

Also, there is %40 instead of @ in the email address. I've tried c.replaceAll("%40", "@") and it doesn't work. What should I be using?

Thanks in advance for your help.
Trish

4 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Try these:

    def map = ['requestRegion':'WA', 'requestCity':'SEATTLE'] // etc
    def fieldNames = ['requestRegion', 'requestCity'] // etc

    // iterate over each item of the fieldNames list
    fieldNames.each {
    def value = map[it]
    // do something with the value
    println value
    }

    // you probably don't need the fieldNames list if they just represent the keys of the map

    map.each { key, value ->
    // do something with the value
    println value
    }

    // seems like you have a URL encoded email, so just decode it
    java.net.URLDecoder.decode('example%40email.com', 'UTF-8')
  • I actually got the map and list working a couple of days ago - using what you suggested. :-)

    Sometimes I have an email address in my Excel spreadsheet and sometimes I generate a random email address. This is part of my Groovy script that runs just prior to the REST POST step.

    if (email.length() > 1)
    {
    testRunner.testCase.setPropertyValue( "Email", email )
    }
    else
    {
    email = startDate.format("'Email'yyyyMMddHHmmss") + "@" + randomEmailAddresses[new Random().nextInt(3)]
    log.info email
    email = java.net.URLDecoder.decode(email, 'UTF-8')
    testRunner.testCase.setPropertyValue( "Email", email )
    }

    The result (the REST POST request) looks like this:

    message_topic_application=Testing&party_name=First2013_04_17T15_01_00_941Last2013_04_17T15_01_00_941&country_code=US&email_restriction=DO_NOT&last_name=Last2013_04_17T15_01_00_941&county=KING&address1=150100941Lane20130417&phone_restriction=DO_NOT&first_name=First2013_04_17T15_01_00_941&direct_mail_restriction=DO_NOT&email=Email20130417150100%40gmail.com&postal_code=98101&city=SEATTLE&region=WA

    The email variable actually contains "Email20130417150042@gmail.com" but is converted on the way to the REST step. Therefore, it no longer is a valid email address and isn't written to the database. I've tried many solutions, including the one you sent to me, but all with the same (or worse) results.

    The "err" in the REST request should be "Email20130417150042%".

    How do I "force" this @ symbol into the REST POST request?

    Thanks!
  • Thanks for the information even though it's not what I wanted to hear.

    Hopefully, the developer of the REST POST service will be able do something to help me out.