Service Virtualization

  • 1.  How to read/write a .txt file on Match Script  ??

    Posted Apr 20, 2018 03:24 PM

    Hi guys,


    I am trying to virtualize two webservices (REST).
    For instance :
    1) https:ws.mycompany.com (POST and PUT) 
    2) https:/wsquery.company.com (GET)
    I want to record some data in a txt file from one URI to use it on the other.

    Is there any way to do this with JavaScript ?
    If not; which  ?
    Thank you!!!


  • 2.  Re: How to read/write a .txt file on Match Script  ??

    Posted May 03, 2018 04:12 PM
    Hi,
    After some days i can say that i found a way to read/write data from/to a database and use it  with theses two WebService.
    Of course, there are others ways like: SharedModelMap , Read Properties From a File  and Write to Delimited File steps for the data to become persistent  but for me is QUITE hard to implement it.
    I am using Apache Derby that comes with Devtest in the LISA BANK application.
    Just for now.

     

    // *******************************************************************************************************
    import org.apache.derby.jdbc.ClientDriver;
    import java.sql.*;
    try{
        Class.forName ("org.apache.derby.jdbc.ClientDriver");
        Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1529/lisa-demo-server.db", "sa", "sa");
        stmt = conn.createStatement();
        PreparedStatement stmt = conn.prepareStatement("insert into cartoes(numero) values(?)");
        stmt.setString(1,t2);
        stmt.execute();
      
    }catch (SQLException ef){
         System.err.println("#### Exception: " + ef.getMessage());
     }catch (Exception e) {
         e.printStackTrace();
     }finally {
             try {
                    stmt.close();
                    conn.close();
                  }catch (Exception e) {
                             e.printStackTrace();
                  }
      }


  • 3.  Re: How to read/write a .txt file on Match Script  ??
    Best Answer

    Posted May 04, 2018 02:02 PM

    Hello Manuel,

     

    I highly recommend that you switch over to using either SharedModelMap or PersistentModelMap. The APIs for these are very straight forward and much easier than writing JDBC java code coupled with SQL statements.

     

    The difference between SharedModelMap and PersistentModelMap is that the former only stores data into memory and doesn't persist such as when JVM shuts down/restarts. The latter persists data to the actual Registry database and is not effected if JVM shuts down. Another important difference is that you can only store String values in the PersistentModelMap and not Java objects, which you can for SharedModelMap. Therefore, you may need to serialize the Java object prior to storing data for PersistentModelMap.

     

    At a high level, these are the methods you will use

    com.itko.lisa.vse.SharedModelMap.putObject(String namespace, String key, Object value)

    ex: com.itko.lisa.vse.SharedModelMap.putObject("wsquery.company.com.namespace", "myKey", "myValue");

     

    to get the value that you saved use this method:

    com.itko.lisa.vse.SharedModelMap.getObject(String namespace, String key)

    ex: Object value = com.itko.lisa.vse.SharedModelMap.getObject("wsquery.company.com.namespace", "myKey")

     

    For PersistentModelMap, which is an interface implemented by the following class: com.itko.lisa.coordinator.TestRegistry

     

    ex:

    com.itko.lisa.coordinator.TestRegistry testRegistry = com.itko.lisa.test.Environment.getTestRegistry();

    testRegistry.putMapValue("wsquery.company.com.namespace", "myKey", "myValue");

     

    to get value, ex.:

    String value = testRegistry.getMapValue("wsquery.company.com.namespace", "myKey");