views:

1415

answers:

2

I am testing a web service with soapUI. One of the responses looks like this:

    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"&gt;
   <S:Body>
      <ns2:getInputConfigFilesResponse xmlns:ns2="http://ws.pdb.ericsson.se/"&gt;
         <return>UEsDBBQACAAIAO1GNToAAAAAAAAAAAAAAAANAAAAc2NyaXB0cy9lbGxza [...] AATAAAAAAAAAAAAAAAAAAGXAAAbGRhcF9ub2RlX2NvbmZpZ3VyYXRpb24vZ2VuZXJhdGVkX2xkaWZfZmlsZXMvX210YXM0X2N4cDQ0NF9yNF9JbXNtb2JpbGUubGRpZlBLAQIUABQACAAIAO1GNTp8eBuZRAEAABMDAAAmAAAAAAAAAAAAAAAAAKJiAABsZGFwX25vZGVfY29uZmlndXJhdGlvbi9lbGxzaC1jb21tYW5kc1BLBQYAAAAABQAFAIgBAAA6ZAAAAAA=</return>
      </ns2:getInputConfigFilesResponse>
   </S:Body>
</S:Envelope>

The response is a byte array which should represent a zip file. How can I go about verifying this?

A: 

You're going to have to use some groovy.

  1. output the byte array to a file.
  2. execute some command line (7zip, zip whatever) with an output you can parse.
  3. make sure it's the correct output.

Edit: added some sample code.

def saveFile = new java.io.File(saveTestDir + "\\testreturn.zip")

FileOutputStream fos = new java.io.FileOutputStream(saveFile);

def zipBytes = context.expand( '${Step#Request#//ns2:getInputConfigFilesResponse[1]/return[1]}' );

fos.write( zipBytes.getBytes() );

fos.flush();

fos.close();

Nathan
step one is kind of the critical part i'm curious about
carrier
added some sample code
Nathan
+2  A: 

Hi carrier,

Although semantically correct, I believe Nathan's proposed solution is somewhat incomplete.

I guess he forgot do Base64 decode the extracted text prior to step 1. I would also include an improvement so that no human intervention is needed by verifying the temporary file using 3rd party ZIP utilities.

That would translate into accessing the temporary ZIP file and checking whether it's a valid ZIP file. Therefore, an algorithm for implementing your requirement would be:

  1. Access element Base64-encoded text content and Base64 decode it
  2. Output ZIP raw text into a temporary file, thus creating a ZIP file
  3. Check if the temporarily created ZIP file is valid

With all that in mind, here's how the complete Groovy script would look like:

import org.apache.commons.codec.binary.Base64

// Step 1: Access element Base64-encoded text content and Base64 decode it
String tempZipFilename = "temp.zip"
def textBase64 = context.expand(
  '${Step#Request#//ns2:getInputConfigFilesResponse[1]/return[1]}' )
def b64 = new Base64()
def zipTextBytes = b64.decode(textBase64.getBytes())

// Step 2: Output ZIP raw text into a temporary file
def zipFile = new java.io.File(tempZipFilename)
FileOutputStream fos = new java.io.FileOutputStream(zipFile)
fos.write( zipTextBytes )
fos.flush()
fos.close()
log.info "Temporary ZIP file stored as: ${zipFile.getCanonicalPath()}"

// Step 3: Check if the temporarily created ZIP file is valid
boolean responseValid
try {
  zipFile = new java.util.zip.ZipFile(tempZipFilename)
  responseValid = true
  log.info "Number of files in the ZIP file: ${zipFile.size()}"
} catch (java.util.zip.ZipException e) {
  responseValid = false
  log.error "The received response contains a bad ZIP"
}
log.info "Web service response is " + (responseValid ? "valid" : "invalid")

Please let me know if this works for you as it does for me. :-)

Cheers!
Shonzilla

p.s. I would suggest adding "ZIP" tag to this question so that people can more easily find a solution for handling ZIPs from Groovy that's embedded here.

Shonzilla