You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lua-ngcp-kamailio/tests/ngcp_rp.lua

266 lines
9.8 KiB

--
-- Copyright 2013 SipWise Team <development@sipwise.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This package is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
-- .
-- On Debian systems, the complete text of the GNU General
-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
--
require('luaunit')
require 'ngcp.utils'
require 'ngcp.rp'
require 'tests_v.dp_vars'
require 'tests_v.up_vars'
require 'lemock'
if not sr then
require 'mocks.sr'
sr = srMock:new()
else
argv = {}
end
local mc = nil
PFetch = {
__class__ = 'PFetch',
_i = { domain=1, user=1 },
_var = { domain=dp_vars, user=up_vars}
}
function PFetch:new()
local t = {}
return setmetatable(t, { __index = PFetch })
end
function PFetch:val(group, uuid)
if not self._i[group] then
error(string.format("group:%s unknown", group))
end
self._i[group] = self._i[group] + 1
local temp = self._var[group][uuid][self._i[group]-1]
if not temp then
print("var nil")
end
end
function PFetch:reset(group)
self._i[group] = 1
end
TestNGCPRealPrefs = {} --class
function TestNGCPRealPrefs:setUp()
self.real = NGCPRealPrefs:new()
end
function TestNGCPRealPrefs:tearDown()
sr.pv.unset("$xavp(caller_dom_prefs)")
sr.pv.unset("$xavp(callee_dom_prefs)")
sr.pv.unset("$xavp(caller_peer_prefs)")
sr.pv.unset("$xavp(callee_peer_prefs)")
sr.pv.unset("$xavp(caller_usr_prefs)")
sr.pv.unset("$xavp(callee_usr_prefs)")
sr.pv.unset("$xavp(caller_real_prefs)")
sr.pv.unset("$xavp(callee_real_prefs)")
sr.log("info", "---cleaned---")
end
function TestNGCPRealPrefs:test_caller_load_empty()
assertError(self.real.caller_load, nil)
end
function TestNGCPRealPrefs:test_callee_load_empty()
assertError(self.real.callee_load, nil)
end
function TestNGCPRealPrefs:test_caller_peer_load()
local keys = {"uno"}
local xavp = {
domain = NGCPDomainPrefs:xavp("caller"),
user = NGCPUserPrefs:xavp("caller"),
peer = NGCPPeerPrefs:xavp("caller"),
real = NGCPRealPrefs:xavp("caller")
}
xavp.domain("uno",1)
assertEquals(sr.pv.get("$xavp(caller_dom_prefs=>uno)"),1)
xavp.user("uno",2)
assertEquals(sr.pv.get("$xavp(caller_usr_prefs=>uno)"),2)
xavp.peer("uno",3)
local real_keys = self.real:caller_peer_load(keys)
assertEquals(real_keys, keys)
assertEquals(xavp.real("uno"),nil)
assertEquals(xavp.peer("uno"),3)
end
function TestNGCPRealPrefs:test_caller_usr_load()
local keys = {"uno"}
local xavp = {
domain = NGCPDomainPrefs:xavp("caller"),
user = NGCPUserPrefs:xavp("caller"),
real = NGCPRealPrefs:xavp("caller")
}
xavp.domain("uno",1)
assertEquals(sr.pv.get("$xavp(caller_dom_prefs=>uno)"),1)
xavp.user("uno",2)
assertEquals(sr.pv.get("$xavp(caller_usr_prefs=>uno)"),2)
local real_keys = self.real:caller_usr_load(keys)
assertEquals(real_keys, keys)
assertEquals(xavp.real("uno"),2)
end
function TestNGCPRealPrefs:test_caller_usr_load1()
local keys = {"uno", "dos"}
local xavp = {
domain = NGCPDomainPrefs:xavp("caller"),
user = NGCPUserPrefs:xavp("caller"),
real = NGCPRealPrefs:xavp("caller")
}
xavp.domain("uno",1)
assertEquals(sr.pv.get("$xavp(caller_dom_prefs=>uno)"),1)
xavp.user("dos",2)
assertEquals(sr.pv.get("$xavp(caller_usr_prefs=>dos)"),2)
local real_keys = self.real:caller_usr_load(keys)
assertItemsEquals(real_keys, keys)
assertEquals(xavp.real("uno"),1)
assertEquals(xavp.real("dos"),2)
end
function TestNGCPRealPrefs:test_callee_usr_load()
local keys = {"uno"}
local xavp = {
domain = NGCPDomainPrefs:xavp("callee"),
user = NGCPUserPrefs:xavp("callee"),
real = NGCPRealPrefs:xavp("callee")
}
xavp.domain("uno",1)
assertEquals(sr.pv.get("$xavp(callee_dom_prefs=>uno)"),1)
xavp.user("uno",2)
assertEquals(sr.pv.get("$xavp(callee_usr_prefs=>uno)"),2)
local real_keys = self.real:callee_usr_load(keys)
assertEquals(real_keys, keys)
assertEquals(xavp.real("uno"),2)
end
function TestNGCPRealPrefs:test_callee_usr_load1()
local keys = {"uno", "dos"}
local xavp = {
domain = NGCPDomainPrefs:xavp("callee"),
user = NGCPUserPrefs:xavp("callee"),
real = NGCPRealPrefs:xavp("callee")
}
xavp.domain("uno",1)
assertEquals(sr.pv.get("$xavp(callee_dom_prefs=>uno)"),1)
xavp.user("dos",2)
assertEquals(sr.pv.get("$xavp(callee_usr_prefs=>dos)"),2)
local real_keys = self.real:callee_usr_load(keys)
assertItemsEquals(real_keys, keys)
assertEquals(xavp.real("uno"),1)
assertEquals(xavp.real("dos"),2)
end
function TestNGCPRealPrefs:test_set()
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>dummy)"),"callee")
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>dummy)"), "caller")
assertFalse(sr.pv.get("$xavp(callee_real_prefs=>testid)"))
assertFalse(sr.pv.get("$xavp(callee_real_prefs=>foo)"))
local callee_xavp = NGCPRealPrefs:xavp("callee")
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>dummy)"),'callee')
callee_xavp("testid", 1)
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>testid)"), 1)
callee_xavp("foo","foo")
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>foo)"),"foo")
end
function TestNGCPRealPrefs:test_clean()
local callee_xavp = NGCPRealPrefs:xavp("callee")
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>dummy)"),'callee')
callee_xavp("testid",1)
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>testid)"),1)
callee_xavp("foo","foo")
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>foo)"),"foo")
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>dummy)"),"caller")
self.real:clean()
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>dummy)"),"caller")
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>dummy)"),"callee")
end
function TestNGCPRealPrefs:test_callee_clean()
local callee_xavp = NGCPRealPrefs:xavp("callee")
local caller_xavp = NGCPRealPrefs:xavp("caller")
callee_xavp("testid",1)
callee_xavp("foo","foo")
caller_xavp("other",1)
caller_xavp("otherfoo","foo")
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>testid)"),1)
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>foo)"),"foo")
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>dummy)"),"caller")
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>other)"),1)
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>otherfoo)"),"foo")
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>dummy)"),"callee")
self.real:clean('callee')
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>dummy)"),'caller')
assertFalse(sr.pv.get("$xavp(callee_real_prefs=>testid)"))
assertFalse(sr.pv.get("$xavp(callee_real_prefs=>foo)"))
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>other)"),1)
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>otherfoo)"),"foo")
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>dummy)"),"callee")
end
function TestNGCPRealPrefs:test_caller_clean()
local callee_xavp = NGCPRealPrefs:xavp("callee")
local caller_xavp = NGCPRealPrefs:xavp("caller")
callee_xavp("testid",1)
callee_xavp("foo","foo")
caller_xavp("other",1)
caller_xavp("otherfoo","foo")
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>testid)"),1)
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>foo)"),"foo")
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>dummy)"),"caller")
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>other)"),1)
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>otherfoo)"),"foo")
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>dummy)"),"callee")
self.real:clean('caller')
assertEquals(sr.pv.get("$xavp(caller_real_prefs=>dummy)"),"caller")
assertFalse(sr.pv.get("$xavp(caller_real_prefs=>other)"))
assertFalse(sr.pv.get("$xavp(caller_real_prefs=>otherfoo)"))
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>testid)"),1)
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>foo)"),"foo")
assertEquals(sr.pv.get("$xavp(callee_real_prefs=>dummy)"),"callee")
end
function TestNGCPRealPrefs:test_tostring()
local callee_xavp = NGCPRealPrefs:xavp("callee")
local caller_xavp = NGCPRealPrefs:xavp("caller")
callee_xavp("testid",1)
callee_xavp("foo","foo")
caller_xavp("other",1)
caller_xavp("otherfoo","foo")
assertEquals(tostring(self.real),'caller_real_prefs:{other={1},otherfoo={"foo"},dummy={"caller"}}\ncallee_real_prefs:{dummy={"callee"},testid={1},foo={"foo"}}\n')
end
-- class TestNGCPRealPrefs
--EOF