In Json array of strings, how can I match a part of string is present? Karate DSL

Viewed 388

---Edited the question to add solution---
---Question---
I have a Json array of strings as below.

How can I validate whether it contains today's date 2021-10-11 or the word PROCESSING-1 as a sub string in it? BTW, I am trying it with Karate DSL

[
  "TEST1019005-2021-10-19T18:19:41.271055Z-PROCESSING-2",
  "TEST1019005-2021-10-19T18:19:41.271055Z-PROCESSING-1",
  "TEST1019002-2021-10-19T14:50:16.810678Z-PROCESSING-2",
  "TEST1019002-2021-10-19T14:50:16.810678Z-PROCESSING-1",
  "TEST1019002-2021-10-19T14:50:16.810678Z-PROCESSING-0",
  "777777-2021-10-19T09:24:33.064845Z-PROCESSING-2",
  "777777-2021-10-19T08:23:10.213230Z-PROCESSING-1",
  "777777-2021-10-19T08:23:10.213230Z-PROCESSING-0",
  "TEST1018001-2021-10-18T16:31:32.643392Z-PROCESSING-0",
  "TEST1013001-2021-10-13T15:13:59.974540Z-PROCESSING-0",
  "777777-2021-10-13T14:17:45.727585Z-PROCESSING-2",
  "Test01-2021-10-11T20:20:05.968159Z-PROCESSING-1",
  "Test01-2021-10-11T20:20:05.968159Z-PROCESSING-0",
  "333333333-2021-10-11T20:11:26.474697Z-PROCESSING-0",
  "333333333-2021-10-08T21:43:28.251925Z-PROCESSING-0",
  "232623234-2021-10-08T21:27:04.363014Z-PROCESSING-0",
]

----Solution--- I could achieve by the following reg-ex.

* match myArray contains "#regex (?i).*" + '2021-11-08' + ".*" 

Here is the code if it helps someone :

  • def myArray =
    """
    [
    "Cust1019005-2021-10-27T18:19:41.271055Z-PROCESSING-2",
    "Cust1019005-2021-10-19T18:19:41.271055Z-PROCESSING-1",
    "Cust1013002-2021-10-13T15:23:57.510469Z-PROCESSING-2",
    "333333333-2021-10-08T21:43:28.251925Z-PROCESSING-0",
    ]
    """
  • def query = '2021-11-08'
  • match myArray contains "#regex (?i)." + query + "."
1 Answers

Here's part of the solution:

* def today = 
"""
function() {
  var Formatter = Java.type("java.time.format.DateTimeFormatter");
  var LocalDate = Java.type("java.time.LocalDate");  
  var dtf = Formatter.ofPattern("yyyy-MM-dd");
  return dtf.format(LocalDate.now());
}
"""
* match each response contains today()

The rest is up to you. Note that there is a #regex match option as well: https://github.com/karatelabs/karate#fuzzy-matching

Also refer these answers: https://stackoverflow.com/a/60945563/143475

Related