From 20804f1e964691b138eaac70a1fe108167fecfb8 Mon Sep 17 00:00:00 2001 From: Marco Capetta Date: Tue, 28 Dec 2021 15:48:29 +0100 Subject: [PATCH] TT#156754 Add the new function NGCPDlgCounters:is_in_set_regex The new function behaves like NGCPDlgCounters:is_in_set but it uses a regex match to see if the key is contained in the list Additionally added the helper function 'contains_regex' in the utils.lua * ignore vscode files Change-Id: Iee89f5298ced10c2a9cdf7ebc19a4c0945b8cc9d (cherry picked from commit 51454fd2b5069b2031e044aed2646d359585ac5b) --- .gitignore | 1 + ngcp/dlgcnt.lua | 8 ++++++++ ngcp/utils.lua | 11 +++++++++++ tests/ngcp_dlgcnt.lua | 26 ++++++++++++++++++++++++++ tests/utils.lua | 11 +++++++++++ 5 files changed, 57 insertions(+) diff --git a/.gitignore b/.gitignore index 6d541ad..42ad241 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ kamailio reports *.pyc +.vscode/* diff --git a/ngcp/dlgcnt.lua b/ngcp/dlgcnt.lua index f0c0148..ac5a8f7 100644 --- a/ngcp/dlgcnt.lua +++ b/ngcp/dlgcnt.lua @@ -113,6 +113,14 @@ end return utable.contains(res, key); end + function NGCPDlgCounters:is_in_set_regex(callid, key) + if not self._test_connection(self.pair) then + self.pair = self._connect(self.config.pair); + end + local res = self.pair:lrange(callid, 0, -1); + return utable.contains_regex(res, key); + end + function NGCPDlgCounters:set(callid, key) if not self._test_connection(self.central) then self.central = self._connect(self.config.central); diff --git a/ngcp/utils.lua b/ngcp/utils.lua index 89fe6da..00ce944 100644 --- a/ngcp/utils.lua +++ b/ngcp/utils.lua @@ -112,6 +112,17 @@ function ut.contains(t, element) return false end +function ut.contains_regex(t, element) + if t then + for _, value in pairs(t) do + if string.match(value, element) then + return true + end + end --for + end --if + return false +end + -- add if element is not in table function ut.add(t, element) if not ut.contains(t, element) then diff --git a/tests/ngcp_dlgcnt.lua b/tests/ngcp_dlgcnt.lua index 55910cb..fd6529a 100644 --- a/tests/ngcp_dlgcnt.lua +++ b/tests/ngcp_dlgcnt.lua @@ -230,6 +230,32 @@ TestNGCPDlgCnt = {} --class lu.assertTrue(res) end + function TestNGCPDlgCnt:test_is_in_set_regex_ok() + self.pair:ping() ;mc :returns(true) + self.pair:lrange("callid0", 0, -1) ;mc :returns({"user:whatever", "fake", "jojo"}) + + mc:replay() + local res = self.dlg:is_in_set_regex("callid0", "^user:") + mc:verify() + + lu.assertIs(self.dlg.central, self.central) + lu.assertIs(self.dlg.pair, self.pair) + lu.assertTrue(res) + end + + function TestNGCPDlgCnt:test_is_in_set_regex_fail() + self.pair:ping() ;mc :returns(true) + self.pair:lrange("callid0", 0, -1) ;mc :returns({"user:whatever", "fake", "jojo"}) + + mc:replay() + local res = self.dlg:is_in_set_regex("callid0", "^ser:") + mc:verify() + + lu.assertIs(self.dlg.central, self.central) + lu.assertIs(self.dlg.pair, self.pair) + lu.assertFalse(res) + end + function TestNGCPDlgCnt:test_del_key() self.pair:ping() ;mc :returns(true) self.pair:lrem("callid0", 1, "key1") ;mc :returns(1) diff --git a/tests/utils.lua b/tests/utils.lua index e1b768f..981b363 100644 --- a/tests/utils.lua +++ b/tests/utils.lua @@ -34,6 +34,9 @@ TestUtils = {} cone = self.simple_list, ctwo = self.simple_hash } + self.simple_list_str = { + "uno", "dos", "tres", "user:1", "user:2", + } end function TestUtils:test_table_deepcopy() @@ -49,6 +52,14 @@ TestUtils = {} lu.assertError(utils.table.contains, "hola",1) end + function TestUtils:test_table_contains_regex() + lu.assertTrue(utils.table.contains_regex(self.simple_list_str, '^u.+$')) + lu.assertFalse(utils.table.contains_regex(self.simple_list_str, '.*q.*')) + lu.assertFalse(utils.table.contains_regex(nil)) + lu.assertTrue(utils.table.contains_regex(self.simple_list_str, '^user:.+$')) + lu.assertError(utils.table.contains_regex, "hola",1) + end + function TestUtils:test_table_add() lu.assertEquals(self.simple_list, {1,2,3}) utils.table.add(self.simple_list, 1)