Forum Discussion

richie's avatar
richie
Community Hero
10 months ago

Groovy - Save off hashmapped split() values to a Property

Hey,

I'm back working with ReadyAPI! and I've forgotten loads in the last 2 years....I'm going through my groovy notes and I'm struggling.

I have a full URL with 2 query parms - token and value and I'm using the split method to write out the values to the logging window. - the script is below:

//code originally provided by nmrao as an answer to another question I posted

def str = 'https://whatevs.azurewebsites.net/path?token=eyJ0eXAiOiJKV1Qi&value=123456789'
def map = new URL(str).query?.split('&').collectEntries{ [(it.split('=').first()): it.split('=').last()]}
log.info map.token
log.info map.value
log.info map

so the logging window writes

Sat Dec 16 21:46:36 GMT 2023: INFO: eyJ0eXAiOiJKV1Qi
Sat Dec 16 21:46:36 GMT 2023: INFO: 123456789
Sat Dec 16 21:46:36 GMT 2023: INFO: {token=eyJ0eXAiOiJKV1Qi, value=123456789}

However - I want to use the query parm values - I don't want to just log them

'token' and 'value' aren't actually properties/variables - so I can't write the associated property values out to a Properties step - cos those token and value values are just part of the value that makes up the whole string of '{token=eyJ0eXAiOiJKV1Qi, value=123456789}'

I was thinking about passing the whole 'map' variable to the Properties step and try using some substring method like split() or something else - but I have a couple of problems:

firstly - the map variable is a hashmap right?  It's not like a normal property that I can pass to a properties step and then use groovyscript to manipulate - when I tried the following

 

def str = 'https://whatevs.azurewebsites.net/path?token=eyJ0eXAiOiJKV1Qi&value=123456789'
def map = new URL(str).query?.split('&').collectEntries{ [(it.split('=').first()): it.split('=').last()]}
log.info map.token
log.info map.value
log.info map

def propertiesStep = context.testCase.testSteps["Properties"] 
propertiesStep.setPropertyValue("map", map)

I got an error warning of a problem - also - I noticed that in the logging - log.info map returns a whole string within curly braces - unlike when property values are normally written to the logging.

Ok - that's it  - could anyone advise how I can extract the two query parm values please?

If your answers could add a bit of explaining (or just good commenting maybe?) so I can then read up - I'm reading up on hashmaps and the split() method at the moment - cos I don't really understand what the 'def map' line in the script is actually doing - I can see the result - I just don't understand how it;s getting there.

Anyway -that's it - as always - I appreciate all and any help that anyone is kind enough to provide!

Cheers,

rich

 

  • In this use case, there is no need even to create map, then loop thru it and put the entries in Properties test step. Can be directly added to Properties step after the first split('&').

    Here it goes the simplified further:

    def properties = context.testCase.testSteps["Properties"]
    def str = 'https://whatevs.azurewebsites.net/path?token=eyJ0eXAiOiJKV1Qi&value=123456789'
    new URL(str).query?.split('&').each {
    	properties.setPropertyValue(it.split('=').first(), it.split('=').last())
    }
  • nmrao's avatar
    nmrao
    Champion Level 3

    In this use case, there is no need even to create map, then loop thru it and put the entries in Properties test step. Can be directly added to Properties step after the first split('&').

    Here it goes the simplified further:

    def properties = context.testCase.testSteps["Properties"]
    def str = 'https://whatevs.azurewebsites.net/path?token=eyJ0eXAiOiJKV1Qi&value=123456789'
    new URL(str).query?.split('&').each {
    	properties.setPropertyValue(it.split('=').first(), it.split('=').last())
    }
    • richie's avatar
      richie
      Community Hero

      Hey nmrao 

      This is brilliant!  Thanks man - really, really appreciate the help - especially your notes on breaking down the methods in that 'def str....' line

      Also great that you gave me an alternative  - a more efficient way of grabbing the values and passing to a Properties step - will really help me getting back into my scripting.

      Cheers!

      rich

    • ChrisAdams's avatar
      ChrisAdams
      Champion Level 3

      nmrao , URL class and then split params is really neat.  Didn't know about those, I'd have gone for a more complex string manipulation solution!

  • nmrao's avatar
    nmrao
    Champion Level 3

    Just add the below to your existing code

    // Loop map and add them to properties step
    map.each { k, v ->
    	propertiesStep.setPropertyValue(k, v)
    }

     

  • nmrao's avatar
    nmrao
    Champion Level 3

     

    the map variable is a hashmap right? 

    Yes.

    As you stated, the url has two query parameters.

    I don't really understand what the 'def map' line in the script is actually doing 

    Here is some info

    new URL(str).query? --> gets string after "?" from the input string i.e., "token=eyJ0eXAiOiJKV1Qi&value=123456789"

    .split('&') --> split method applied on a string with some character and output will be list, here gets a list after the string is split by "&" i.e., token=eyJ0eXAiOiJKV1Qi, value=123456789

    .collectEntries --> creates a map from the above list; while creating map, it again split by "=" where the first one is key and last is its value

    could anyone advise how I can extract the two query parm values please?

    The question itself has the answer as they can be seen in the log. Its just that you want them for later use, so you were trying put them in Properties step where you run into issue.