Forum Discussion

Awesome's avatar
Awesome
Frequent Contributor
15 years ago

how to grab attachment from response and store in List<>

hi all,

i'm trying to do following and i'm a bit stumped:
1) grab a mime attachment, a .csv file, from the soap response (response attachment below)
2) store the .csv file in a List<>

Thanks in advanced for the help!

----Code so far...-----
log.info testRunner.testCase.getTestStepByName( "runReport_1" ).testStep.testRequest.response.attachments[0].getName();
log.info testRunner.testCase.getTestStepByName( "runReport_1" ).testStep.testRequest.response.attachments[0].getContentType();
log.info testRunner.testCase.getTestStepByName( "runReport_1" ).testStep.testRequest.response.attachments[0].data;

log output:
Fri Nov 19 22:49:14 PST 2010:INFO:report
Fri Nov 19 22:49:14 PST 2010:INFO:application/vnd.ms-excel
Fri Nov 19 22:49:14 PST 2010:INFO:null

--- runReport_1 RESPONSE, MIME ATTACHMENT .CSV ----
...
...
</runReportReturn></ns1:runReportResponse></soapenv:Body></soapenv:Envelope>
------=_Part_26_361028225.1290216136604
Content-Type: application/vnd.ms-excel
Content-Transfer-Encoding: binary
Content-Id: <report>

Replication Session History,,,,,
ORG_ID,SESSION_ID,START_TIME,END_TIME,STATUS,MESSAGES
00DB0000000bozD,1,"Nov 12, 2010","Nov 12, 2010",FAILED,Maximum number of object replication failures encountered.
00DB0000000bozD,2,"Nov 12, 2010","Nov 12, 2010",FAILED,Maximum number of object replication failures encountered.
00DB0000000bozD,3,"Nov 12, 2010","Nov 12, 2010",SUCCESS,
00DB0000000bozD,4,"Nov 12, 2010","Nov 12, 2010",SUCCESS,
,,,,,

------=_Part_26_361028225.1290216136604--

1 Reply

  • Awesome's avatar
    Awesome
    Frequent Contributor
    SOLUTION: store attachment stream in a reader; pass reader object to OpenCSV to parse; store each line in a List[]

    // READ .CSV attachment from SOAP RESPONSE
    log.info " Attachment Name: " + testRunner.testCase.getTestStepByName( "runReport_1").testStep.testRequest.response.attachments[0].getName();
    log.info " Attachment ContentType: " + testRunner.testCase.getTestStepByName( "runReport_1").testStep.testRequest.response.attachments[0].getContentType();
    def attachment_stream = testRunner.testCase.getTestStepByName( "runReport_1").testStep.testRequest.response.attachments[0].getInputStream();
    def rdr = new InputStreamReader(attachment_stream);

    // OpenCSV read into List
    CSVReader reader = new CSVReader(rdr);
    def List theList = reader.readAll();
    log.info " Number of Lines in List: " + theList.size()