Attaching file in SoapUI with groovy - soap

I'm creating some tests in SoapUI. SOAP request, that i want to test has attachment. When I'm setting it manualy, everything is ok:
But in my case, i need to set attachment dynamically. I'm trying to made it by properties to hold file path, and groovy script to set attachment. but it's not work at all:
// get request
def request = testRunner.testCase.getTestStepByName( "UploadRoutingCodes" ).testRequest
// clear existing attachments
for( a in request.attachments ) {
request.removeAttachment( a )
}
// get file to attach
//def fileName = context.expand( '${Source of data#PathToXRC File data name }' )
def fileName = context.expand( '${#TestCase#XRC_file_name}' )
def filePath = context.expand( '${#Project#XRC_files_path}' )
log.info "file: " + filePath + fileName
def file = new File(filePath + fileName )
if ( file == null) {
log.error "bad filename"
}
else
{
// attach and set properties
def attachment = request.attachFile( file, true )
attachment.contentType = "application/octet-stream"
def list = fileName.tokenize("\\");
attachment.setPart(list.last())
}
After run this script, request look like this:
Documentation to SoapUI is not helpful at all.
So, my question is: what i'm doing wrong?

I found the answer:
def holder2 = groovyUtils.getXmlHolder( "UploadRoutingCodes#Request" ) // Get Request body
def startDate2 = holder2.setNodeValue( "//blac:FileByteStream","cid:"+list.last()); //Set "link" to attachment in request body
holder2.updateProperty() //and update
attachment.setPart(list.last()); //set attachment

Thaven, thank you for your answer. It helped. I will attach my full groovy script as I spent some time to fully assembled your parts, but anyhow all tributes goes to you.
Please note that:
//FileNamePath
def fileNamePath = testCase.getTestStepAt(testRunner.testCase.getTestStepIndexByName("FileNameProperties")).getProperty("FileNamePath")
//FileName
def fileName = testCase.getTestStepAt(testRunner.testCase.getTestStepIndexByName("FileNameProperties")).getProperty("FileName")
are the test step properties defined inside the test case. Filename: my_sample_filename.xml and FileNamePath: C:\samples\my_sample_filename.xml accordingly.
import groovy.xml.MarkupBuilder
import org.custommonkey.xmlunit.*
import java.util.Random
import java.security.MessageDigest
import java.nio.file.*
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def projectPath = groovyUtils.projectPath
log.info projectPath
def project = testRunner.testCase.testSuite.project
log.info "Project: " + project.name
def myTestSuite = testRunner.testCase.testSuite;
log.info "TestSuite: " + myTestSuite.name
def testCase = testRunner.testCase
log.info "TestCase: " + testCase.name
def testStepUploadDataAfterCheck = testCase.getTestStepByName("UploadDataAfterCheck")
def request= testStepUploadDataAfterCheck.testRequest
log.info "TestStep: " + testStepUploadDataAfterCheck.name
// clear existing attachments
for( a in request.attachments ) {
request.removeAttachment( a )
}
//FileNamePath
def fileNamePath = testCase.getTestStepAt(testRunner.testCase.getTestStepIndexByName("FileNameProperties")).getProperty("FileNamePath")
//FileName
def fileName = testCase.getTestStepAt(testRunner.testCase.getTestStepIndexByName("FileNameProperties")).getProperty("FileName")
// get file to attach
log.info "file to attach: " + fileNamePath.getValue()
def file = new File(fileNamePath.getValue() )
if ( file == null) {
log.error "bad filename"
}
else
{
// attach and set properties
def attachment = request.attachFile( file, true )
attachment.contentType = "application/octet-stream"
attachment.setPart(fileName.getValue())
def holder2 = groovyUtils.getXmlHolder( "UploadDataAfterCheck#Request" ) // Get Request body
holder2.setNodeValue( "//upl:UploadDataAfterCheckRequest/uploadedData","cid:"+fileName.getValue()); //Set "link" to attachment in request body
holder2.updateProperty() //and update
log.info "file attached succesfully"
}
And here is my soap request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:upl="http://www.acer.europa.eu/aris/upload">
<soapenv:Header/>
<soapenv:Body>
<upl:UploadDataAfterCheckRequest>
<uploadedData>cid:my_sample_filename.xml</uploadedData>
</upl:UploadDataAfterCheckRequest>
</soapenv:Body>
</soapenv:Envelope>

Related

SoapUI POST to REST with attached file in Groovy

I'm trying to POST to a Sharepoint REST service an attached file with SoapUI Pro. I've tried the examples at: https://support.smartbear.com/readyapi/docs/requests/attachment/rest.html
But with no luck.
It should work with POST with byte-array as body. But how do I do that in SoapUI and Groovy?
In the tool Insomnia it works with "Binary File".
I add these headers:
Accept: application/json;odata=verbose
Content-Type: application/octet-stream
Media type = multipart/mixed and Post QueryString
But the file won't be uploaded to SharePoint.
PowerShell code that works:
$headers = #{
'X-RequestDigest' = 'xxxxxxxxxxxxxxxxxxxxxxx'
'Accept' = 'application/json;odata=verbose'
}
$document = [System.IO.File]::ReadAllBytes('C:\temp\myFile.docx')
Invoke-RestMethod -Method Post -UseDefaultCredentials -Uri "https://xxxx.xxx/add(url='myFile.docx',%20overwrite=true)" -Headers $headers -Body $document
I tried to go through this as well a while ago but I found it easier to use HTTP to do this.
You may try to see if it fits your requirements
My groovy script for attachment :
// upload source file before import
// get uploading request
def source_file = context.expand( '${#TestCase#source_file_path}' )
log.info "upload $source_file"
def aPIToolsTestSuite = context.expand( '${#Project#APIToolsTestSuite}' ) // the test suite that contains the test case with the HTTP request
tc_name = "import - upload resource files"
request = testRunner.testCase.testSuite.project.testSuites[aPIToolsTestSuite].testCases[tc_name].getTestStepByName("Request 1").testRequest
// clear request from any existing attachment
for (a in request.attachments)
{
request.removeAttachment(a)
}
// attach file to upload
def file = new File(source_file)
if (file == null)
{
log.error "bad file name : $source_file"
}
else
{
// attach file and set properties
try{
def attachment = request.attachFile (file, true)
attachment.contentType = "application/octet-stream"
attachment.setPart("upload file '$source_file'")
}
catch (Exception e){
log.error "file ${file.name} : exception $e"
}
}
// now upload file - launch the request
def jsonSlurper = new groovy.json.JsonSlurper()
def TC;
def async = false
TC = testRunner.testCase.testSuite.project.getTestSuiteByName(aPIToolsTestSuite).getTestCaseByName(tc_name)
result = TC.run (context.getProperties(), async)
if (String.valueOf( result.status ) != "PASS")
{
msg = "unexpected failure during $tc_name when uploading $source_file"
testRunner.fail(msg)
log.error msg
}
else
{
// this part is for further processing
// file uploaded, go through the import and properties backup process
resource_to_import = TC.getPropertyValue("testResponse").split('\"')[1]
// file uploaded, go through the import and properties backup process
testRunner.testCase.setPropertyValue("resource_id", resource_to_import)
}
And the HTTP request contained in the test case APIToolsTestSuite/import - upload resource files
first step : get endpoint
def env = testRunner.testCase.testSuite.project.activeEnvironment
rest = env.getRestServiceAt(0)
config = rest.getEndpoint().config
endpoint = new XmlSlurper().parseText(config.toString())
testRunner.testCase.setPropertyValue("endpoint", endpoint.toString())
second step, HTTP request:
POST
with Request tab parameters :
name : metadata
value : {"storageType":"FILESYSTEM","itemName":"my_source_file"}
type : QUERY
media type : multipart/form-data
Post QueryString
Headers : application/json
Good luck :)

Save attachments from response in SoapUI

I get 2 files in response of a SOAP request. I try to save these files with followig Groovy script. I use script as a script assertion for test step. First file is saved successfully in execution, but couldn't find second one.
def fileName = "C:\\<mydirectory>"+'/test.pdf'
def fileName1 = "C:\\<mydirectory>"+'/test1.pdf'
def response = messageExchange.response
assert null != response, "response is null"
def outFile = new FileOutputStream(new File(fileName))
def outFile1 = new FileOutputStream(new File(fileName1))
def ins = messageExchange.responseAttachments[0]?.inputStream
def ins1 = messageExchange.responseAttachments[0]?.inputStream
if (ins) {
com.eviware.soapui.support.Tools.writeAll(outFile, ins)
}
ins.close()
outFile.close()
if (ins1) {
com.eviware.soapui.support.Tools.writeAll(outFile1, ins)
}
ins1.close()
outFile1.close()

Scala/Spray/Akka unable to leverage mapRequest

I am new to Scala/Spray/AKKA so please excuse this dumb requests.
I have the following Directive and it is being called as the first
logger line ("inside") is showing up in logs.
However, anything inside mapRequest{} is skipped over. The logging line ("headers:") isn't showing up
private def directiveToGetHeaders(input: String) : Directive0 = {
logger.info("inside")
mapRequest { request =>
val headList: List[HttpHeader] = request.headers
logger.info("headers: " + headList.size)
request
}
}
I am not sure what I did wrong. My goal is to pull out all the HTTP headers. Any tip/pointer much appreciated. Thanks
-v
You can use extractRequest directive for getting headers.
private def directiveToGetHeaders(input: String) : Directive0 = {
logger.info("inside")
extractRequest { request =>
val headList: Seq[HttpHeader] = request.headers
logger.info("headers: " + headList.size)
complete(HttpResponse())
}
}

How to print atribute value from Response in teardown script

I want to print attribute value from response if my assertion fails. Sample error Response:
<soapenv:Body>
<ns0:Fault xmlns:ns1="http://www.w3.org/2003/05/soap-envelope" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/">
<faultcode>OSB-382500</faultcode>
<faultstring>Mandatory Parameter Customer Type cannot be empty (uuid: 1f8b9637-11b1-47ea-9ebd-3abf2fda950e)</faultstring>
<detail>
<ns0:Fault xmlns:ns0="http://group.vodafone.com/contract/vfo/fault/v1" xmlns:ns2="http://group.vodafone.com/contract/vho/header/v1" xmlns:ns3="http://group.vodafone.com/schema/common/v1" xmlns:ns6="http://docs.oasis-open.org/wsrf/bf-2" xmlns:ns7="http://www.w3.org/2005/08/addressing">
<ns6:Timestamp>2017-08-16T20:44:27.15+05:30</ns6:Timestamp>
<ns6:ErrorCode>500</ns6:ErrorCode>
<ns0:Name/>
<ns0:Severity>Critical</ns0:Severity>
<ns0:Category>Technical</ns0:Category>
<ns0:ReasonCode>ReasonCode</ns0:ReasonCode>
<ns0:Message>Service Callout Failure</ns0:Message>
</ns0:Fault>
</detail>
</ns0:Fault>
</soapenv:Body>
I want to print value "Service Callout Failure" if my assertion fails.
Currently my script is printing assertion status and testcase name. I want to print the particular attribute value from response. My Teardown Script:
import com.eviware.soapui.model.testsuite.Assertable.AssertionStatus
import jxl.*;
import jxl.write.*;
def TestCase = testRunner.getTestCase()
def StepList = TestCase.getTestStepList()
def status
def i = 0
WritableWorkbook workbook1 = Workbook.createWorkbook(new File("C:\\report1.xls"));
WritableSheet sheet1 = workbook1.createSheet("Report Worksheet", 0);
StepList.each{
if(it.metaClass.hasProperty(it,'assertionStatus')){
if(it.assertionStatus == AssertionStatus.FAILED){
log.info "${it.name} FAIL..."
status = "FAIL";
}else if(it.assertionStatus == AssertionStatus.VALID){
log.info "${it.name} OK!"
status = "Passed";
}else if(it.assertionStatus == AssertionStatus.UNKNOWN){
log.info "${it.name} UNKNOWN (PROBABLY NOT EXECUTED)"
status = "UNKNOWN";
}
}
Label label1 = new Label(i, sheet1.rows, it.name);
Label label2 = new Label(i+1, sheet1.rows, status);
sheet1.addCell(label1);
sheet1.addCell(label2);
}
workbook1.write();
workbook1.close();
I am using context.expand to get data from response to groovy,
for example
def hotel = context.expand( '${SearchHotels#Response#declare namespace ns1=\'someNamespace\'; declare namespace ns2=\'someNamespace2\'; //ns2:SearchHotelsResponse[1]/ns2:SearchHotelsResult[1]/ns1:TWS_HotelList[1]/ns1:Hotel[1]}' )
Namespace aside, at SearchHotels#Response... SearchHotels is name of test step, you can get response message using correct path to ns0:Message and then print it instead of constant data...
I used the below statement:
def req = it.name; message[k] = context.expand('${'+req+'#Response#declare namespace ns0=\'http://group.vodafone.com/contract/vfo/fault/v1\'; //ns0:Message}')
and it worked.

How to do Rest PUT/POST in groovy

How to do HTTP PUT/POSTs from inside Groovy code without having to import any libraries (if at all possible)? I know there is a simple getText() methods that Groovy adds to the java.net.URL class, that could be used without adding any dependencies. Is there a way to do Rest PUT in the same fashion?
You can do it with HttpURLConnection in a similar way as you would do it with java:
def url = new URL('http://your_rest_endpoint')
def http = url.openConnection()
http.setDoOutput(true)
http.setRequestMethod('PUT')
http.setRequestProperty('User-agent', 'groovy script')
def out = new OutputStreamWriter(http.outputStream)
out.write('data')
out.close()
http.inputStream // read server response from it
import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.JSON
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
url = "http://restapi3.apiary.io"
#Grab (group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '0.5.0')
def client = new RESTClient(url)
def jsonObj = new JsonSlurper().parseText('{ "title": "Pick-up posters from Post-Office" }')
def response = client.put(path: "/notes/id",
contentType: JSON,
body: jsonObj,
headers: [Accept: 'application/json'])
println("Status: " + response.status)
if (response.data) {
println("Content Type: " + response.contentType)
println("Headers: " + response.getAllHeaders())
println("Body:\n" + JsonOutput.prettyPrint(JsonOutput.toJson(response.data)))
}