Forum Discussion

jdario's avatar
jdario
Occasional Contributor
6 years ago

Reading SOAP Request Attachment using Groovy

Hello, 

 

I have a SOAP request with an attachment. I'm trying to read the attachment, generate it's SHA-256 hash, and put the value in the request before sending. I've found various snippets of each step in the community, but nothing seems to work. 

 

This is what I have so far in my Groovy script:

import java.security.MessageDigest

def attachment = testRunner.testCase.testSteps['SOAP Request'].httpRequest.getAttachmentAt(0).data
def digest = MessageDigest.getInstance("SHA-256")
def sha256 = { var.java.security.MessageDigest.getInstance("SHA-256").digest(attachment).toString()}

 

The result I get is: "Script-result: Script54$_run_closure1@6fa2bce9"

 

Any help is appreciated. 

 

Thanks

 

 

  • I figured it out. Let me first state what I wanted to do.

    I wanted to get the SHA-256 value from the cached attachment in my SOAP Request and then place that value in the SOAP Request as a property. If the attachment changes it doesnt matter because the test will generate a new hash value from it. 

     

    To do this:

         1. I created a Groovy Script test step to go get the attachment, generate the SHA-256 value and return it.

    import java.security.MessageDigest
    import java.math.BigInteger
    	
    	def attachment = testRunner.testCase.testSteps['SOAP_Request'].httpRequest.getAttachmentAt(0).data
    	String sha256 = org.apache.commons.codec.digest.DigestUtils.sha256Hex(attachment);
    	assert sha256
    	return sha256
    
    	log.info(sha256)

         2. I created a Property Transfer test step to move the "result" property value to a property I created at the Test Case level. 

         3. I then reference the custom Test Case property in my SOAP Request by using ${#TestCase#sha256}       (where sha256 is the name I gave the custom proerty) in the corresponding XML element.

  • aaronpliu's avatar
    aaronpliu
    Frequent Contributor

    you defined a closure, if no parameter input, would use {-> }, and then call it sha256.call()

    • Olga_T's avatar
      Olga_T
      SmartBear Alumni (Retired)

      Hi all,

       

      Thanks for the suggestion, aaronpliu.

       

      jdario have you tried the above tip?  Does it help? If so, don't hesitate to mark it as a solution. Other users will easily find it then. Otherwise, can you please provide us with further details? Thanks in advance!

       

      • jdario's avatar
        jdario
        Occasional Contributor

        Hi Olga, 

         

        I'm just trying the suggested solution today. Unfortunatelly I've had no luck so far. I'm reading the groovy-lang.org documentation. I'll be sure to post a solution when I find it. 

         

        Thanks!

    • jdario's avatar
      jdario
      Occasional Contributor

      Hello. Thanks for your reply. Apologies for such a late response. I'm ashamed to admit I don't understand your solution. I'm not a developer or technical person at all, but I really want to improve my testing. 

       

      I'm assuming you are refering tho the line

       

      def sha256 = {var.java.security.MessageDigest.getInstance("SHA-256").digest(attachment).toString()}

       

      I tried a couple things with no luck.

       

      // I tried this with no luck
      def sha256 = { [->]
      	       var.java.security.MessageDigest.getInstance("SHA-256").digest(attachment).toString()
      		  sha256.call()
      	}
      //Doesnt work either
      def sha256 = { sha256 ->
      	       var.java.security.MessageDigest.getInstance("SHA-256").digest(attachment).toString()
      		  sha256.call()
      	}

      I came back to this after the holidays and It's turned into a headache.

       

       

  • jdario's avatar
    jdario
    Occasional Contributor

    I figured it out. Let me first state what I wanted to do.

    I wanted to get the SHA-256 value from the cached attachment in my SOAP Request and then place that value in the SOAP Request as a property. If the attachment changes it doesnt matter because the test will generate a new hash value from it. 

     

    To do this:

         1. I created a Groovy Script test step to go get the attachment, generate the SHA-256 value and return it.

    import java.security.MessageDigest
    import java.math.BigInteger
    	
    	def attachment = testRunner.testCase.testSteps['SOAP_Request'].httpRequest.getAttachmentAt(0).data
    	String sha256 = org.apache.commons.codec.digest.DigestUtils.sha256Hex(attachment);
    	assert sha256
    	return sha256
    
    	log.info(sha256)

         2. I created a Property Transfer test step to move the "result" property value to a property I created at the Test Case level. 

         3. I then reference the custom Test Case property in my SOAP Request by using ${#TestCase#sha256}       (where sha256 is the name I gave the custom proerty) in the corresponding XML element.

    • Olga_T's avatar
      Olga_T
      SmartBear Alumni (Retired)

      Well done, jdario!
      Thanks for sharing the solution!