Service Virtualization

 View Only
  • 1.  Replace CRLF to LF in Application Test | Please help

    Posted Mar 04, 2020 01:46 AM
    Hi Team,

    Could you please help with some scriptedAssertion for replacing CRLF to LF in a flat file?
    I have tried few options but did not get success yet

    getting exception errors

    e.g.
    test


    ------------------------------
    Best Regards,
    Nick.
    ------------------------------


  • 2.  RE: Replace CRLF to LF in Application Test | Please help

    Posted Mar 04, 2020 02:02 AM

    It is unclear from your statements below in which property this flat file text is located. Is it in the request body, or the response body, or somewhere else?

     

    If in request body:

     

               String asciiCRLF = lisa_vse_request.getBodyAsString();

               String asciiLF = asciiCRLF.replaceAll("\r", "");

               lisa_vse_request.setBody(asciiLF);

     

    If in some property:

              

               String asciiCRLF = (String) testExec.getStateValue("someAsciiString");

               String asciiLF = asciiCRLF.replaceAll("\r", "");

               testExec.setStateValue("someAsciiString", asciiLF);

              

    If in response body:

              

               String asciiCRLF = lisa_vse_response.get(0).getBodyAsString();

               String asciiLF = asciiCRLF.replaceAll("\r", "");

               lisa_vse_response.get(0).setBody(asciiLF);

     

    Cheers,

    Danny

     






  • 3.  RE: Replace CRLF to LF in Application Test | Please help

    Posted Mar 04, 2020 02:38 AM
    Hi Danny,

    Thanks for your quick response.

    It is not in virtual service(VSI, VSM, request/response) but in application test. ".tst".

    in the test,
    • I am using parser steps to create the different line records.
    • after each line record, need to give nextLine character. i.e. \n(LF) but some how the tool is taking CRLF and placing the same after each record.
    need either to handle the new line in parser or replacing the CRLF to LF, once all the records are added in the file.


    ------------------------------
    Best Regards,
    Nick.
    ------------------------------



  • 4.  RE: Replace CRLF to LF in Application Test | Please help

    Posted Mar 04, 2020 03:42 AM

    Ok, I don't know what you are doing inside parser steps or how you are finally writing the file out to disk, so I cannot comment on that. So, I can only propose on how to remove the CR after the file is already written out. So, not the most efficient solution but it should work functionally:

     

    Add these imports at the top of your script:

     

    import java.nio.file.Files;

    import java.nio.file.Path;

    import java.nio.file.Paths;

    import java.nio.file.StandardOpenOption;

     

    And add following lines after your file is created on disk (and closed)

     

    Path file = Paths.get("<yourFilePath>");

    String fileContent = new String(Files.readAllBytes(file));

    fileContent = fileContent.replaceAll("\r", "");

    Files.write(file, fileContent.getBytes(), StandardOpenOption.WRITE);

     

     

    Cheers,

    Danny

     

    ::DISCLAIMER::

    The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents (with or without referred errors) shall therefore not attach any liability on the originator or HCL or its affiliates. Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the views or opinions of HCL or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of this message without the prior written consent of authorized representative of HCL is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately. Before opening any email and/or attachments, please check them for viruses and other defects.






  • 5.  RE: Replace CRLF to LF in Application Test | Please help

    Posted Mar 04, 2020 03:53 PM
    Thanks Danny, it worked but adding some additional characters in the end.
    Any suggestions to handle that?


    ------------------------------
    Best Regards,
    Nick.
    ------------------------------



  • 6.  RE: Replace CRLF to LF in Application Test | Please help
    Best Answer

    Posted Mar 05, 2020 02:09 AM

    Ok, I don't think these characters are added, this is some of the original content of the file (containing the CRLFs) and because our new content is smaller in size (removed all the CRs), the last of the original file is not overwritten. Try below write statement with TRUNCATE_EXISTING added, see if that solves it:

     

               Files.write(file, fileContent.getBytes(), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);

     

    If it doesn't solve it then we will have to close and reopen the file between the reading and writing.

     

    Cheers,

    Danny

     

    ::DISCLAIMER::

    The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents (with or without referred errors) shall therefore not attach any liability on the originator or HCL or its affiliates. Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the views or opinions of HCL or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of this message without the prior written consent of authorized representative of HCL is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately. Before opening any email and/or attachments, please check them for viruses and other defects.