MD5 using Groovy in SoapUI

2019-04-17 18:24发布

问题:

I created a test case which contains parameters that consist of key and value. I need to take all those parameters in an alphabetic order, and then create a MD5 hash value which i will pass to one of the parameters of this test.

Any suggestions? i looked in google and cannot find anything helpful. Thanks ahead!

回答1:

You can retrieve the SoapUI's test case properties using a Groovy Script test step as given below.

def map = context.testCase.properties.keySet().inject([:]){map, key ->  map[key] = context.testCase.getPropertyValue(key); map }  
map.each { key, value ->     log.info "Property name : ${key} and value : ${value}" }

Since you have the properties in map / key=value format, you should able to achieve what you are looking for.

Regarding md5 generation Refer here

import java.security.MessageDigest

def generateMD5_A(String s){ MessageDigest.getInstance("MD5").digest(s.bytes).encodeHex().toString() }



回答2:

I have done the following:

import java.security.MessageDigest
def testStep = testRunner.testCase.testSteps["3D Secure Call"]
def str = new StringBuilder();
 for (prop in testStep.getPropertyList()){

    if(prop.getName() != "K" && prop.getName() != "RawRequest" && prop.getName() != "Domain" && prop.getName() != "Password" && prop.getName() != "ResponseAsXml" && prop.getName() != "Request" &&  prop.getName() != "RawRequest" && prop.getName() != "Response" && prop.getName() != "Username" && prop.getName() != "Endpoint"){
        str.append(prop.getName() + "=" + testStep.getPropertyValue(prop.getName()) + "&" )

    }
}
str.append("K=1473942615907cuwmviz")
return (MessageDigest.getInstance("MD5").digest(str.bytes).encodeHex().toString())

I get the string exactly how i wish: M=10000330&PKey=8c124262b3d66f9e47185fd34eec13bbef2acd03170bcf8c284985193bc9a6&a4=50000&a5=EUR&XID=12345678912345678912&K=1473942615907cuwmviz

but now i need to apply the MD5, and i am getting an error. any suggestions? No such property: bytes for class: java.lang.StringBuilder