Nov 20, 2021

Connecting Azure CosmosDB from SoapUI Opensource using Groovy script

I had made an attempt to connect azure cosmos DB from Soapui Groovy script. this helps building tests on soapui. 1. Create a project in soapUI --> testsuite --> add a testcase. 2. Create a REST requst using the cosmosDB endpoint https://hostname:port/resType 3. Add a properties step and create few variables as mentioned below just to pass on the input data or you can hardcode inside the code. 4. add a groovy script step and paste below code 5. Create Headers with names - Authorization (value will be filled by below code), x-ms-version (value find latest version date on google eg.2018-12-31), x-ms-date (will be filled by below code, Accept (value application/json) Copy below code and paste in your Groovy script step in your soapUI testcase. import import javax.crypto.Mac import javax.crypto.spec.SecretKeySpec import import; import //Read all variables from SoapUI properties, you can hardcode constant string too or pass dynamic in someother way def resType = context.expand( '${cosmosProps#resType}' ) def host = context.expand( '${cosmosProps#CosmosDBHost}' ) def cosmosAPIVersion = context.expand( '${cosmosProps#CosmosDBAPIVersion}' ) def access_key = context.expand( '${cosmosProps#CosmosDBMasterKey}' ) def method = context.expand( '${cosmosProps#method}' ) def resourceId = "" TimeZone.setDefault(TimeZone.getTimeZone('GMT')) def String GMTsourceDate = new Date() //RFC_1123_DATE_TIME conversion def String GMTdateString = new Date().parse('EEE MMM d HH:mm:ss z yyyy', GMTsourceDate).format("EEE, d MMM yyyy HH:mm:ss z") "Converted DateTimeString: "+GMTdateString //Method to HAMCSHA256 - pass decoded key and payload text static byte[] HmacSHA256(String payloadText, byte[] key) throws Exception { String algorithm="HmacSHA256" Mac mac = Mac.getInstance(algorithm); mac.init(new SecretKeySpec(key, algorithm)) return mac.doFinal(payloadText.getBytes("UTF-8")) } "masterKey is "+access_key //decode above masterkey since its already base64 encoded byte[] decodedKey = access_key.decodeBase64() //build PayloadString using verb, resType, resourceID and date, you can pass this dynamic if you want def String payLoadString = "get"+"\n"+resType+"\n"+resourceId+"\n"+GMTdateString.toLowerCase()+"\n"+""+"\n" payLoadString //call HMacSha256 by passing payload and key def hash256signed = HmacSHA256(payLoadString, decodedKey) String signature = hash256signed.encodeBase64()"encoded base64 signed: "+signature) //building URI def MasterToken = "master" def TokenVersion = "1.0" def headers = new StringToStringMap() //URL encoding def String toURLEncode = "type=" + MasterToken + "&ver=" + TokenVersion + "&sig=" + signature def AuthHdr =, "UTF-8") 'AUTHHDRIS: ' + AuthHdr //putting all caculated headers to soapui request headers.put("Authorization", AuthHdr) headers.put("x-ms-version", cosmosAPIVersion) headers.put("Accept", "application/json") headers.put("x-ms-date", GMTdateString) def testStep = testRunner.testCase.getTestStepByName("CosmosAPIRequest"); testStep.getHttpRequest().setRequestHeaders(headers) ("DONE")

No comments:

Post a Comment