Service Virtualization

Expand all | Collapse all

Match Script does not allow me to return true or return false?

Jump to Best Answer
Anon Anon

Anon Anon04-26-2016 07:01 PM

Prem_Bairoliya

Prem_Bairoliya04-26-2016 07:11 PM

  • 1.  Match Script does not allow me to return true or return false?

    Posted 04-26-2016 02:29 PM

    Hello,

    I am writing a small match script in JavaScript.

    So far I have successfully

    • assigned variables to values from incomingRequest
    • assigned variables to values from sourceRequest
    • writing the variables to the vse log to see I am grabbing the right data

    But then I am clearly missing some basic piece of understanding.  When I try a small conditional:

     

    if ( plant !=="1010") {

      return false;

    } else if ( mode == src_mode && grid1 == src_grid1 && grid2 == src_grid2){

      return true;

    } else {

      return defaultMatcher.Matches();

    }

    DevTest tells me neither the 'return true' nor the 'return false' are valid returns.  But as match scripts return Booleans, I am now confused.

     

    Thanks in advance for any help received.



  • 2.  Re: Match Script does not allow me to return true or return false?

    Posted 05-02-2017 02:16 AM

    HI Ulrich,

     

    i too facing the same issue, i have placed my code below. how to return a boolean value based on the IF condition in Java Script. Based on this assertion (True/False) i need to perform other operations. Please help on this

     

    if(testExec.getStateValue("ResponseValue1").equals(testExec.getStateValue("Expected_Result1")))
    {
    Return True;
    }
    else
    {
    Return False;



  • 3.  Re: Match Script does not allow me to return true or return false?

    Broadcom Employee
    Posted 04-26-2016 02:52 PM

    Is the following a typo ?

     

    if ( plant !=="1010") { => this should be if ( plant !="1010") {



  • 4.  Re: Match Script does not allow me to return true or return false?

    Posted 04-26-2016 05:43 PM

    If I treat the code as ordinary JavaScript, sticking the snippet in a function context gets rid of the error.

     

    function match (plant, mode, grid1, grid2, src_mode,src_grid1,src_grid2) {

    if ( plant!=="1010") {

      return false;

      } else if ( mode == src_mode && grid1 == src_grid1 && grid2 == src_grid2){

      return true;

      } else {

      return defaultMatcher.Matches();

      }

     

    But the documentation for Match Scripts has examples with no functions defined, and I tried to follow the example. So I feel I am still missing something fundamental.

     

    I welcome any and all clues. :-)



  • 5.  Re: Match Script does not allow me to return true or return false?

    Posted 04-26-2016 07:18 PM

    Problem likely is that JavaScript does not support the concept of explicit return values (such as 'return false;') outside of functions. Try using other supported scripting languages such as Beanshell or Groovy instead.

    In JavaScript try the following code instead:

    if ( plant !=="1010") {

           plant.equals("1010");

    } else if ( mode == src_mode && grid1 == src_grid1 && grid2 == src_grid2){

     

           plant.equals("1010");

    } else {

           defaultMatcher.matches();

    }



  • 6.  Re: Match Script does not allow me to return true or return false?
    Best Answer

    Posted 05-02-2017 03:46 AM

    This is a duplicate of https://communities.ca.com/message/241976265?commentID=241976265#comment-241976265 

     

    JavaScript does not have a concept of a ‘return’ statement outside of functions. The return value of a JavaScript is the value of the expression evaluated last.

    Instead of the entire if statement, I suggest to try 

    testExec.getStateValue("ResponseValue1").equals(testExec.getStateValue("Expected_Result1"))

    and see if that works.

    Please see DevTest 8.0 - Scripting Guide - V1.1.pdf  for more details and some guidance.



  • 7.  Re: Match Script does not allow me to return true or return false?

    Posted 04-26-2016 03:48 PM

    I believe the '!==' operator is 'not equal value nor equal type'. 

    But I could split that out into two checks, and try the not-equal operator by itself and see if that changes the result.



  • 8.  Re: Match Script does not allow me to return true or return false?

    Broadcom Employee
    Posted 04-26-2016 06:44 PM

    I see   return defaultMatcher.Matches(); in your code, if you check the example, the correct API call is return defaultMatcher.matches();



  • 9.  Re: Match Script does not allow me to return true or return false?

    Posted 04-26-2016 07:33 PM

    Thanks.  I am not sure I can learn Beanshell or Groovy in the time I have for this project, but it will go on my to do list. For now, I will see if using JavaScript functions will work.

     

    Also, thanks for the excellent guide to scripting. It was concise and thorough enough to get started quickly.



  • 10.  Re: Match Script does not allow me to return true or return false?

    Posted 04-26-2016 03:55 PM

    Changing the operator to just not equal still gives the 'invalid return' error warning.

     

    But thanks for the idea.

    Any other suggestions?



  • 11.  Re: Match Script does not allow me to return true or return false?

    Posted 04-26-2016 07:01 PM

    Corrected. :-)



  • 12.  Re: Match Script does not allow me to return true or return false?

    Posted 04-26-2016 07:40 PM

    Please see my updated last response with a JavaScript sample that might work.



  • 13.  Re: Match Script does not allow me to return true or return false?

    Posted 04-26-2016 05:24 PM

    what you are trying to do with script in match script area ? if you can tel me the purpose I can tell you if that is possible to implement in any other way..



  • 14.  Re: Match Script does not allow me to return true or return false?

    Broadcom Employee
    Posted 04-26-2016 05:29 PM

    I see another typo

     

    return defaultMatcher.Matches();

    needs to be

    return defaultMatcher.matches();



  • 15.  Re: Match Script does not allow me to return true or return false?

    Broadcom Employee
    Posted 04-26-2016 07:11 PM

    Did it fix your issue?



  • 16.  Re: Match Script does not allow me to return true or return false?

    Posted 04-26-2016 07:52 PM

    Thank you very much. I will give that a spin later tonight. :-)



  • 17.  Re: Match Script does not allow me to return true or return false?

    Posted 05-06-2016 06:48 AM

    Did this suggestion resolve your issue?



  • 18.  Re: Match Script does not allow me to return true or return false?

    Posted 04-26-2016 07:01 PM

    I am trying to do a couple of things.

    The virtual service listens on the same JMS nodes as several other services.  I need to prevent it from responding to requests that are not from plant 1010.

    The signature matching is otherwise TOO restrictive; there are many elements in the meta that will not match on valid message matches. 

     

    Thanks for your help.



  • 19.  Re: Match Script does not allow me to return true or return false?

    Posted 05-06-2016 11:55 AM

    Ulrich,

     

    Moving everything into functions resolved the issue, yes.  Thanks for your help. ☺

     

    Greg