0

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..."

  • many pages instead of 2 which I expect

//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
Matelko
  • 15
  • 1
  • 5
  • please edit your question and describe in words what you are trying to do. also provide details what your service returning, what you'd like to have in the file, etc. – daggett Aug 21 '19 at 09:44
  • I hope it is more clear now – Matelko Aug 21 '19 at 10:49
  • not yet. ok, why you try to use base64 ? your pdf document is encoded with base64 ? why you are reading response as a string when pdf could contain binary data ? – daggett Aug 21 '19 at 11:07
  • yes, exactly this pdf is encoded with base64. Regarding 'String' i was just checking if it could be helpful to read it as a 'String' - method of try's and failure's. I think the first method i the closest one, but still i don't know how to decode this lines of signs. – Matelko Aug 21 '19 at 11:30
  • You are running this as an assertion? Why are you doing anything but assertions inside an assertion script? Shouldn't you move this to a separate Groovy Test Step? – Steen Aug 23 '19 at 06:34
  • I prefere to use Script assertion because i don't need to create new test step, everything is happening as a assertion, so we can be sure that step is correct and actions which we exepect from this step. – Matelko Aug 23 '19 at 09:23
  • I respect your decision, but I think the logic is flawed. I pity whoever that someday may have to take over the responibility for this test, trying to figure out how it works. He/she can then see some PDF is produced, but may be challenged to figure out where and how. I don't think it will be intuitive for anyone to go browsing through the assertions to find it. Sorry. – Steen Aug 23 '19 at 11:35
  • No problem, this is your point of view, I'm prefering to do it in that way, we can have different approach to this kind of stuff. If it will be necessary, I can change it to 'groovy script' step, but for me it is more useful when I'm able to do it in 'assertion' way. – Matelko Aug 23 '19 at 12:54

1 Answers1

1

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.

daggett
  • 26,404
  • 3
  • 40
  • 56
  • Unfortunately I'm reciving blank pages. – Matelko Aug 22 '19 at 09:15
  • @Matelko, unfortunately can't help you without providing more details in your question. Response example for example. – daggett Aug 22 '19 at 11:15
  • https://gofile.io/?c=GfGi8p -> this is my response as a xml which i'm trying to decode to pdf. – Matelko Aug 22 '19 at 13:13
  • seems your pdf document is corrupted. i used online base64 decoder and [online pdf validator](https://www.pdf-online.com/osa/validate.aspx) and i got errors: The file format (header, trailer, objects, xref, streams) is corrupted. The document's meta data is either missing or inconsistent or corrupt. The document does not conform to the PDF 1.7 standard. – daggett Aug 22 '19 at 14:30
  • That what I also noticed. It's turned out I have wrong endpoint. I changed my endpoint to get response in JSON and I use your line with 'decode' and it works :) – Matelko Aug 23 '19 at 09:15