Forum Discussion

Yoav_Seroussi's avatar
Yoav_Seroussi
Occasional Contributor
16 years ago

Re: Context won't expand certain properties inside a Groovy class

Hi,

I'm trying to do a REST request which calls a groovy class for one of it's parameters, the call looks like this:
${=MyClass.call("${#Project#property}", request, context, log)}


The context which is passed is the DefaultPropertyExpansionContext.
Inside the Groovy class's method I need to access the request parameters, but some of them require expansion.

This is where I get stuck, since if I try to expand the property I get nothing.
The context will only expand properties on the project level, but not any lower (TestCase/TestSuite).

Example from the script:
log.info(s + "=" +  request.getPropertyValue(s))
log.info(s + "=" + context.expand("'" + request.getPropertyValue(s))+ "'")


Which prints out:
Tue Apr 13 09:40:56 IDT 2010:INFO:method=activateContent
Tue Apr 13 09:40:56 IDT 2010:INFO:method='activateContent'
Tue Apr 13 09:40:56 IDT 2010:INFO:msisdn=${#TestCase#phoneNumber}
Tue Apr 13 09:40:56 IDT 2010:INFO:msisdn=''
Tue Apr 13 09:40:56 IDT 2010:INFO:operator_id=${#TestSuite#operatorId}
Tue Apr 13 09:40:56 IDT 2010:INFO:operator_id=''


Please advise as to how this can be done.

Regard,
Mike

6 Replies

  • Hi Mike,

    hmm.. exactly where in soapUI are you specifying this

    ${=MyClass.call("${#Project#property}", request, context, log)}

    ?

    can you attach a screenshot (so I don't misunderstand...)?

    regards!

    /Ole
    eviware.com
  • Yoav_Seroussi's avatar
    Yoav_Seroussi
    Occasional Contributor
    Hi Ole,

    I attached the screenshot of the SoapUI.

    Here's the code for the class:

    package com.muzicall;

    import org.apache.log4j.Logger
    import com.eviware.soapui.impl.rest.RestRequestInterface
    import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext
    import javax.crypto.Mac
    import javax.crypto.spec.SecretKeySpec
    import org.apache.commons.codec.binary.Base64

    class Rbt4AllApiSignatureGenerator {

    public static String sign(String key, RestRequestInterface request, PropertyExpansionContext context, Logger log) {
    Set<String> sortedKeys = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER)

    sortedKeys.addAll(Arrays.asList(request.getParams().getPropertyNames()))

    sortedKeys.remove("signature")
    sortedKeys.remove("public_key")

    log.info("Request parameters: ${sortedKeys}")

    def restCall = ""
    for (String s : sortedKeys) {
    restCall += s
    restCall += context.expand(request.getPropertyValue(s))
    log.info(s + "=" +  request.getPropertyValue(s))
    log.info(s + "=" + context.expand("'" + request.getPropertyValue(s))+ "'")

    }

    log.info(context.expand('${#TestSuite#storeId}'))

    ...
    sign the key
    ...

    return signedKey;
    }
    }


    Regards,
    Mike
  • Hi,

    I'm trying to reproduce this but can't seem to.. can you mail me the project and the groovy file (remove any private stuff) so I can try with that? (mail to support@eviware.com)

    thanks in advance!

    regards,

    /Ole
    eviware.com
  • Yoav_Seroussi's avatar
    Yoav_Seroussi
    Occasional Contributor
    Hi Ole,

    I sent it to that email address.
    Feel free to contact me either there or here.

    Thanks,
    Mike
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Just to chime in, notice that the log lines where the property is missing have single quotes.

    Tue Apr 13 09:40:56 IDT 2010:INFO:msisdn=''


    Does it work if you remove them?

    log.info(s + "=" + context.expand(request.getPropertyValue(s)) )
  • Yoav_Seroussi's avatar
    Yoav_Seroussi
    Occasional Contributor
    Hi McDonald,

    I tried witout the single quotes and it prints out an empty string:
    Wed Apr 14 14:14:30 IDT 2010:INFO:msisdn=${#TestCase#testCaseProp2}
    Wed Apr 14 14:14:30 IDT 2010:INFO:msisdn=
    Wed Apr 14 14:14:30 IDT 2010:INFO:operator_id=${#TestSuite#testSuiteProp1}
    Wed Apr 14 14:14:30 IDT 2010:INFO:operator_id=
    Wed Apr 14 14:14:30 IDT 2010:INFO:set_as_default=${#Project#projectProp}
    Wed Apr 14 14:14:30 IDT 2010:INFO:set_as_default=project property (expands ok)


    As you can see, the project level property gets expanded fine, but lower level ones don't (yes, I put values in them ).

    Regards,
    Mike