Layer 7 API Management

Expand all | Collapse all

Calculate Integer Variables

Jump to Best Answer
  • 1.  Calculate Integer Variables

    Posted 7 days ago
    ​This is maybe a stupid question, but how can I calculate with Integer variables?
    For example variable1=3 and variable2=5 and I want to set a new variable as sum of both like ${variable1}+${variable2}.
    Is this somehow possible and if yes, with which assertion in which syntax?
    Alternativly it would also be fine to use calculation within the "Compare"-assertion.
    Any ideas and hints are welcome.
    Thank you!

    Ciao Stefan :)


  • 2.  RE: Calculate Integer Variables

    Posted 6 days ago
    Edited by Albert Traveria Molero 6 days ago
    Hello Stefan,

    I would suggest that you use the javascript assertion so you can perform multiple operations and calculations.
    https://docops.ca.com/ca-api-gateway/9-4/en/policy-assertions/assertion-palette/service-availability-assertions/execute-javascript-assertion

    something like:
    var var1 = context.getVariable('variable1');
    var var2 = context.getVariable('variable2');

    var var3 = var1 + var2;

    if(var3 > 5){
         //let's get the party started
    }else{
        //let's get the party started anyway :P
    }

    context.setVariable('varResult', var3);

    Kind regards,
    Albert.

    ------------------------------
    Security Engineer
    ELCA
    ------------------------------



  • 3.  RE: Calculate Integer Variables

    Posted 6 days ago

    Hi Albert,
    thanks for the hint with the Javascript-assertion. I also found it in the meanwhile. But it doesn't make a mathmatic operation.​ It behaves like concatenate, means 3+5 is NOT becoming 8, but 35 :(
    And as mentioned in the documentation, the internal JS-function eval() is NOT allowed.
    But it seems to work with the "Evaluate Response XPath"-Assertion. Just enter "$value1 + $value2" in the XPath-field (without " and without curly brackets).
    The result will then be displayed as "8.0" (in case of 3+5). A following integer comparison is working fine.
    I also tried the "Expression"-option in the "Compare Variable"-Assertion using ${value1} + ${value2}, but this also doesn't seem to work.
    Any other ideas?

    I heard, that there is an external math-assertion available via support. Is this true and can this be made available here or do I have to raise an official ticket for this?
    And would there be any advantages in regards to performance in comparison to the XPath method?
    Thank you!

    Ciao Stefan :)




  • 4.  RE: Calculate Integer Variables
    Best Answer

    Posted 6 days ago
    Hi
    It is working as it suppose to be.
    You are passing string variables so it is normally  concats. Pass int vars and you will get 8.

    Regards






  • 5.  RE: Calculate Integer Variables

    Posted 6 days ago
    Edited by Stefan Klotz 6 days ago
    Hi Ozan,
    you are indeed right. Issue seems to be the "Map Value"-assertion.​ Here you can't explicitely specify the variable type and the output variable seems to be from type string.
    So is there any way to work with this assertion (or similar one) and get integer values as output or do I have to rearrange it with an "At least"-folder?

    EDIT: I must recognize, that the latency for this JS-Assertion is really bad. It took around 5million ns, whereas the XPath-assertion took only around 40k ns. So I think JS is not an option for us at all. Or are there any tricks to optimize this?
    Thank you!

    Ciao Stefan :)


  • 6.  RE: Calculate Integer Variables

    Posted 6 days ago
    Hello Stefan,

    Just set them in a previous step with the assertion Set Variable to type int and later you will be able to work fine in javascript.

    Regards,
    Albert.

    ------------------------------
    Security Engineer
    ELCA
    ------------------------------



  • 7.  RE: Calculate Integer Variables

    Posted 5 days ago
    Edited by Stefan Klotz 5 days ago

    Hi Albert,
    thanks for the hint, but this doesn't seem to work with the "Map Value"-assertion.
    I checked it with the debugger and as soon as this assertion will be executed, the type of the variable is changing from Int to String.
    But as mentioned before, the JS-assertion is not an option for us at all due to very long assertion latency.
    We will proceed with the XPath-assertion as this is currently working fine for us.
    Interesting note: I don't know why, but XPath is not working with variables from type Int (I'm getting NaN as result). When I change it to String the calculation is successful and correct. Also comparison like greater or smaller are working fine with result.
    Thank you!

    Ciao Stefan :)