Forum Discussion

tonaortega's avatar
tonaortega
Occasional Contributor
8 years ago

Can I load excel datasource properties into ServiceV Dispatcher script?

Hello,   I'm trying to virtualize a service for Login call. It's basically two responses: LoginSuccess and LoginFail. So I'm trying to write a SCRIPT to dispatch the response.   Its a REST Reques...
  • tonaortega's avatar
    8 years ago

    The solution was to create a mysql DB instance in my server and copy all the info from the spreadsheet into it.

     

    As I need to validate & compare incoming request data in my dispatcher script I used the following script:

     

    This is a REST service:

     

    1. Create Custom Properties with dbName, dbUsername, dbPassword, dbHost and add the values needed.

     

    2. Use this code into dispatcher script:

     

    //Get data from Request
    def jsonSlurper = new JsonSlurper()
    def requestObj = jsonSlurper.parseText(mockRequest.getRequestContent())
    assert requestObj instanceof Map

    // open connection
    def Virtservice = context.getMockService()
    def conString = "jdbc:mysql://" + Virtservice.getPropertyValue( "dbHost" ) + "/" + Virtservice.getPropertyValue( "dbName" )
    log.info conString

     

    //Define SQL Connection as follow:
    def sql = Sql.newInstance(conString,
    Virtservice.getPropertyValue( "dbUsername" ),
    Virtservice.getPropertyValue( "dbPassword" ),
    'com.mysql.jdbc.Driver')

    // Execute Query: Where userId is defined from incoming request data
    def query = "SELECT sessionToken FROM users WHERE userID LIKE '" + requestObj.userId + "';"

    //row value will get the entire row as result from query above.

    def row = sql.firstRow(query)

    //Set property value email with value found in row.email.
    context.setProperty("email",row.email)

    //check for connection in context
    sql.close()

     

    I hope this helps! Let me know if you have any question.

     

    Tona O.