Layer7 API Management

  • 1.  Usage of the Map in the Execute JavaScript Assertion?

    Posted May 19, 2019 05:44 AM

    Can we use map in the Execute Javascript assertion?

     

    I have used the below code snippet. I have defined a context variable country = abc

     

    var country = context.getVariable('country');

    var countryMapping = new Map();
    countryMapping.set('abc',1);
    countryMapping.set('def',2);
    countryMapping.set('fgh',3);

    var flag = countryMapping.has(country);

    context.setVariable("flag", flag);

    if(flag){
    var code=countryMapping.get(country); }
    else {
    var code='Others';}

    context.setVariable("code", code);

     

    Request your help at the earliest.



  • 2.  Re: Usage of the Map in the Execute JavaScript Assertion?

    Posted May 21, 2019 11:23 AM


  • 3.  Re: Usage of the Map in the Execute JavaScript Assertion?

    Posted May 21, 2019 12:06 PM

    Hi LeandroDantas,

     

    I have more than 1000 values to add to this map. As it needs to be manually added one by one. It may be time taking and may land in human errors.

     

    I also have some logic to be performed on the same.

     

    Regards,

    Yeswin.



  • 4.  Re: Usage of the Map in the Execute JavaScript Assertion?

    Posted May 21, 2019 01:54 PM

    I see.

    Well, Gateway 9.4 uses JDK Nashorn engine configured as ECMAScript 5, with Java and extensions disabled, so you can't use Map or reference to java HashMap to work with this logic.

     

    If you have more than 1000 values, you should evaluate your options carefully.

    Maybe creating some logic using "Run Assertions for Each Item" and caching the results?

     

    Anyway, you can work around the map logic using a array.forEach or array.filter with JS:

    Sample using filter:

     

    Sample using forEach:



  • 5.  Re: Usage of the Map in the Execute JavaScript Assertion?

    Posted May 21, 2019 01:06 PM

    Hi Yeswin,

     

    Whats the exact use case?

     

    From what I see, you could put the entire map into a context variable, set as a message (application/json). Subsequently using the JSON Path to get the necessary value.

     

    With a bit more logic, you can give it a default value.

     

    For my use case, we put it into an encapsulated assertion for ease of use.

     

    Regards,

    Shawn