Search code examples
karate

Can a match or comparison of 2 arrays be based on key set


When trying to compare 2 different responses (source and actual) a conversion happens in our code that we need to account for and I am unsure how to do it.

Array1 = ["NEVER-CONNECTED","ACTIVE","NEVER-CONNECTED"] Array2 = ["N","A","N"]

While these are these same, a simple match does not work. Is it possible to convert one of the arrays based on a key of sort? convert A to ACTIVE, N to NEVER-CONNECTED, D to DISCONNECTEd. All other values are stored as is


Solution

  • Here you go:

    * def lookup = { N: 'NEVER-CONNECTED', A: 'ACTIVE' }
    * def expected = ["NEVER-CONNECTED", "ACTIVE", "NEVER-CONNECTED"] 
    * def response = ['N', 'A', 'N'] 
    * def converted = response.map(x => lookup[x])
    * match converted == expected
    

    And the "lookup" approach can scale to any number of key-value pairs. More explanation here: https://stackoverflow.com/a/59162760/143475