groovybase64soapuigroovyscriptengine

How to correctly write groovy script in assertion to decode response and recive it in pdf?


I'm trying to receive pdf document with conent from 'rest request' respons via assertion script. I was trying in few ways, but for each of them the result was diffrent then expected. Could you please review my few options and propose some solutions ? That's my first steps with 'groovy scripts' and I'm not so familiar with endcode/decode functionalities, so please be understanding if i made some huge mistakes Thanks in advance.

//Option number 1

import org.apache.commons.codec.binary.Base64; 
import groovy.json.JsonSlurper
import com.itextpdf.text.*
import com.itextpdf.text.pdf.PdfWriter;

def content = messageExchange.response.responseContent
def cont = messageExchange.response.responseContent
def fileName = messageExchange.modelItem.testStep.testCase.getPropertyValue("DOC") + '_test.pdf'
Base64 coder = new Base64();
//assert null != response, "response is null"
def encodedString = cont.getBytes("UTF-8").encodeBase64().toString()
def decoded = encodedString.decodeBase64();
def res = new File( "F:\\Test\\Testing\\Files\\assertion\\$fileName")
res.write(content, "UTF-8")
res.delete();
res << encodedString
log.info res

Result:

I'm expecting document with correct pdf content.

From Option 1 I'm able to received pdf file with content which is stil encoded liket this: "JVBERi0xLjQNCiXvv73vv73vv73vv70NCjEgMCBvYmoKPDwKL0F1dGhvciAoQW5ua2F0aHJpbi BTdGVwaGFuKQovQ3JlYXRpb25EYXRlIChEOjIwMTkwNDE4MTcwNTI2KzAzJzAwJykKL0NyZWF0 b3IgKFBERi1YQ2hhbmdlIE9mZmljZSBBZGRpbikKL0NyZWF0b3JUb29sIChQREYtWENoYW5nZS..."

//Option 2

import org.apache.commons.codec.binary.Base64; 
import groovy.json.JsonSlurper
import com.itextpdf.text.*
import com.itextpdf.text.pdf.PdfWriter;

def content = messageExchange.response.responseContent
def cont = messageExchange.response.responseContent
def fileName = messageExchange.modelItem.testStep.testCase.getPropertyValue("DOC") + '_test.pdf'
Base64 coder = new Base64();
//assert null != response, "response is null"
def encodedString = cont.getBytes("UTF-8").encodeBase64().toString()
def decoded = encodedString.decodeBase64();
def res = new File( "F:\\Test\\Testing\\Files\\assertion\\$fileName")
res.write(content, "UTF-8")
//res.delete(); -> without this line 
res << encodedString
log.info res

I'm expecting document with correct pdf content. Result: From Option 2 - File is created with 2 blank pages

//Option 3

import org.apache.commons.codec.binary.Base64; 
import groovy.json.JsonSlurper
import com.itextpdf.text.*
import com.itextpdf.text.pdf.PdfWriter;

def fileName = messageExchange.modelItem.testStep.testCase.getPropertyValue("DOC_PID") + '_test.pdf'
def cont = messageExchange.response.responseContent
String content = cont
def encoded = content.getBytes("UTF-8").encodeBase64().toString()
byte[] decoded = encoded.decodeBase64()
def document = new Document()
PdfWriter.getInstance(document,new FileOutputStream(fileName));

I'm expecting document with correct pdf content. Result: From Option 3 - i'm reciving error window with message "name of file (access is denied)

Which option is the best ? and how to improve it ?


*Thanks for response, at first I need to admitt that i made mistake, and i took wrong type of response it was 'Raw', and i should use 'XML' which has correct response. Also I had limitation in 'Max size' property which affected response. Now i set correct size, and I changed content of response. Code looks like that:

import com.eviware.soapui.support.XmlHolder
def cont = new XmlHolder(messageExchange.responseContentAsXml)
content = cont["//*:data"]
def fileName = messageExchange.modelItem.testStep.testCase.getPropertyValue("DOC") + '_test.pdf'
new File( "F:\\Test\\Testing\\Files\\assertion\\$fileName").bytes = content.decodeBase64()

Assertion is passed but stil pdf file has blank pages. I'm sure that this is Base64 encoded document and I need to decode it.

Final solution which works for me is(but remeber to have response in JSON which is encode in Base64):

import org.apache.commons.codec.binary.Base64; 
import groovy.json.JsonSlurper
//grab the response
def content = messageExchange.response.responseContent
def jsonSlurper = new JsonSlurper().parseText(content)
assert !(jsonSlurper.isEmpty())
document_content = jsonSlurper.fileContent
def fileName = messageExchange.modelItem.testStep.testCase.getPropertyValue("DOC") + '_.pdf'
new File( ""F:\\Test\\Testing\\Files\\assertion\\$fileName"").bytes = document_content.decodeBase64()

log.info fileName

Solution

  • if response content contains base64 encoded pdf then following should work to write decoded pdf into a file:

    def content = messageExchange.response.responseContent
    new File( "F:\\Test\\Testing\\Files\\assertion\\$fileName").bytes = content.decodeBase64()
    

    String in groovy has built-in method decodeBase64() that returns decoded content as bytes

    Then you just need to write bytes into a file.