1

---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 + "."
Maaza
  • 79
  • 1
  • 1
  • 8

1 Answers1

0

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

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I tried that route. But match each or match any were not finding the portion of the string. match failed: CONTAINS_ANY $ | actual does not contain expected | actual array does not contain any of the expected items (LIST:STRING) $[15] | not equal (STRING:STRING) '232623234-2021-10-08T21:27:04.363014Z-PROCESSING-0' '2021-10-21' $[14] | not equal (STRING:STRING) '333333333-2021-10-08T21:43:28.251925Z-PROCESSING-0' '2021-10-21' ....... – Maaza Oct 22 '21 at 03:05
  • @Maaza works for me. follow this process please: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Oct 22 '21 at 04:20
  • Sample project created to demo the actual issue in : https://github.com/HelpingFriends/Karate_TryOuts.git and the issue started as : https://github.com/karatelabs/karate/issues/1810 – Maaza Oct 24 '21 at 04:36
  • @Maaza sorry, you seem to have ignored the use of `match each`. if what I commented as an answer does not help, please assume that karate does not support what you want and look for another tool – Peter Thomas Oct 24 '21 at 05:01
  • 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-", "333333333-2021-10-08T21:43-0", ] """ * def query = '2021-11-08' * match myArray contains "#regex (?i).*" + query + ".*" – Maaza Nov 08 '21 at 19:43