diff --git a/ngcp/ngcp.lua b/ngcp/ngcp.lua index 34e3b5b..7a3c1ee 100644 --- a/ngcp/ngcp.lua +++ b/ngcp/ngcp.lua @@ -42,8 +42,6 @@ NGCPConfig_MT = { __index = NGCPConfig } db_pass = "somepasswd", db_database = "kamailio", default = { - prof = { - }, contract = { }, peer = { diff --git a/ngcp/pprof.lua b/ngcp/pprof.lua index 0185d17..1e1de66 100644 --- a/ngcp/pprof.lua +++ b/ngcp/pprof.lua @@ -61,50 +61,30 @@ NGCPProfilePrefs_MT.__tostring = function () end end - function NGCPProfilePrefs:_defaults(level) - local defaults = self.config:get_defaults('prof') - local keys = {} - local k,_ - - if defaults then - for k,v in pairs(defaults) do - table.insert(keys, k) - end - end - return keys, defaults - end - function NGCPProfilePrefs:_load(level, uuid) local con = assert (self.config:getDBConnection()) local query = "SELECT prefs.* FROM provisioning.voip_subscribers as usr LEFT JOIN ".. self.db_table .." AS prefs ON usr.profile_id = prefs.uuid WHERE usr.uuid = '".. uuid .. "'" local cur = assert (con:execute(query)) - local defaults - local keys + local keys = {} local result = {} local row = cur:fetch({}, "a") local k,v local xavp - keys, defaults = self:_defaults(level) - - if row then - while row do - --sr.log("info", string.format("result:%s row:%s", table.tostring(result), table.tostring(row))) + if table.size(row) > 0 then + while table.size(row) > 0 do + --sr.log("debug", string.format("result:%s row:%s", table.tostring(result), table.tostring(row))) table.insert(result, row) table.add(keys, row.attribute) - defaults[row.attribute] = nil row = cur:fetch({}, "a") end else sr.log("dbg", string.format("no results for query:%s", query)) end cur:close() - - xavp = self:xavp(level, result) - for k,v in pairs(defaults) do - sr.log("dbg", string.format("setting default[%s]:%s", k, tostring(v))) - xavp(k, v) + if table.size(result) > 0 then + xavp = self:xavp(level, result) end return keys end diff --git a/ngcp/utils.lua b/ngcp/utils.lua index e20748b..342a7cd 100644 --- a/ngcp/utils.lua +++ b/ngcp/utils.lua @@ -104,6 +104,18 @@ function table.merge(t, other) return t; end +function table.size(t) + if t then + local c = 0 + local _,v + for _,v in pairs(t) do + c = c + 1 + end + return c + end + return 0 +end + function table.val_to_str ( v ) if "string" == type( v ) then v = string.gsub( v, "\n", "\\n" ) diff --git a/ngcp/xavp.lua b/ngcp/xavp.lua index 27f0336..3578cb0 100644 --- a/ngcp/xavp.lua +++ b/ngcp/xavp.lua @@ -112,7 +112,8 @@ NGCPXAvp_MT = { end sr.pv.seti(id, value) else - sr.log("err",string.format("can't set value:%s of type:%d", value, vtype)) + sr.log("err",string.format("can't set value:%s of type:%s", + tostring(value), tostring(vtype))) end if value and id then check = sr.pv.get(id) @@ -135,7 +136,7 @@ NGCPXAvp_MT = { sr.log("dbg",string.format("%s created with dummy value:%s", name, self.level)) end for i=1,#l do - name = string.format("$xavp(%s[0]=>%s)", self.name, l[i].attribute) + name = string.format("$xavp(%s[0]=>%s)", tostring(self.name), tostring(l[i].attribute)) table.add(self.keys, l[i].attribute) NGCPXAvp._setvalue(name, l[i].type, l[i].value) end diff --git a/tests/ngcp.lua b/tests/ngcp.lua index 960d6d3..5a8a41b 100644 --- a/tests/ngcp.lua +++ b/tests/ngcp.lua @@ -212,7 +212,7 @@ TestNGCP = {} --class self.cur:close() -- self.con:execute("SELECT prefs.* FROM provisioning.voip_subscribers as usr LEFT JOIN prof_preferences AS prefs ON usr.profile_id = prefs.uuid WHERE usr.uuid = 'ae736f72-21d1-4ea6-a3ea-4d7f56b3887c'") ;mc :returns(self.cur) - self.cur:fetch(mc.ANYARGS) ;mc :returns(nil) + self.cur:fetch(mc.ANYARGS) ;mc :returns({}) -- this is what I got on real mysql self.cur:close() -- connection check self.con:execute("SELECT 1") ;mc :returns(self.cur) diff --git a/tests/ngcp_pprof.lua b/tests/ngcp_pprof.lua new file mode 100644 index 0000000..25caa8e --- /dev/null +++ b/tests/ngcp_pprof.lua @@ -0,0 +1,205 @@ +-- +-- Copyright 2013 SipWise Team +-- +-- 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 . +-- . +-- 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('lemock') +require 'ngcp.utils' +require 'tests_v.pprof_vars' + +if not sr then + require 'mocks.sr' + sr = srMock:new() +else + argv = {} +end + +local mc,env,con +local pprof_vars = PProfFetch:new() + +package.loaded.luasql = nil +package.preload['luasql.mysql'] = function () + luasql = {} + luasql.mysql = function () + return env + end +end +require 'ngcp.pprof' + +TestNGCPProfilePrefs = {} --class + + function TestNGCPProfilePrefs:setUp() + mc = lemock.controller() + env = mc:mock() + con = mc:mock() + self.cur = mc:mock() + + package.loaded.luasql = nil + package.preload['luasql.mysql'] = function () + luasql = {} + luasql.mysql = function () + return env + end + end + + self.config = NGCPConfig:new() + self.config.env = env + self.config.getDBConnection = function () + return con + end + + self.d = NGCPProfilePrefs:new(self.config) + pprof_vars:reset() + end + + function TestNGCPProfilePrefs:tearDown() + sr.pv.unset("$xavp(caller_dom_prefs)") + sr.pv.unset("$xavp(callee_dom_prefs)") + sr.pv.unset("$xavp(caller_prof_prefs)") + sr.pv.unset("$xavp(callee_prof_prefs)") + sr.pv.unset("$xavp(caller_prof_prefs)") + sr.pv.unset("$xavp(callee_prof_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", "---TestNGCPProfilePrefs::cleaned---") + end + + function TestNGCPProfilePrefs:test_init() + --print("TestNGCPProfilePrefs:test_init") + assertEquals(self.d.db_table, "prof_preferences") + end + + function TestNGCPProfilePrefs:test_caller_load_empty() + assertTrue(self.d.config) + assertEquals(self.d:caller_load(), {}) + end + + function TestNGCPProfilePrefs:test_callee_load_empty() + assertTrue(self.d.config) + assertEquals(self.d:callee_load(), {}) + end + + function TestNGCPProfilePrefs:test_caller_load() + assertTrue(self.d.config) + con:execute("SELECT prefs.* FROM provisioning.voip_subscribers as usr LEFT JOIN prof_preferences AS prefs ON usr.profile_id = prefs.uuid WHERE usr.uuid = 'ah736f72-21d1-4ea6-a3ea-4d7f56b3887c'") ;mc :returns(self.cur) + self.cur:fetch(mc.ANYARGS) ;mc :returns(pprof_vars:val("prof_2")) + self.cur:fetch(mc.ANYARGS) ;mc :returns(pprof_vars:val("prof_2")) + self.cur:fetch(mc.ANYARGS) ;mc :returns(pprof_vars:val("prof_2")) + self.cur:fetch(mc.ANYARGS) ;mc :returns(nil) + self.cur:close() + + mc:replay() + local keys = self.d:caller_load("ah736f72-21d1-4ea6-a3ea-4d7f56b3887c") + mc:verify() + + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>dummy)"), "caller") + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>sst_enable)"),"yes") + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>sst_refresh_method)"), "UPDATE_FALLBACK_INVITE") + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>outbound_from_user)"), "upn") + end + + function TestNGCPProfilePrefs:test_callee_load() + assertTrue(self.d.config) + con:execute("SELECT prefs.* FROM provisioning.voip_subscribers as usr LEFT JOIN prof_preferences AS prefs ON usr.profile_id = prefs.uuid WHERE usr.uuid = 'ah736f72-21d1-4ea6-a3ea-4d7f56b3887c'") ;mc :returns(self.cur) + self.cur:fetch(mc.ANYARGS) ;mc :returns(pprof_vars:val("prof_2")) + self.cur:fetch(mc.ANYARGS) ;mc :returns(pprof_vars:val("prof_2")) + self.cur:fetch(mc.ANYARGS) ;mc :returns(pprof_vars:val("prof_2")) + self.cur:fetch(mc.ANYARGS) ;mc :returns(nil) + self.cur:close() + + mc:replay() + local keys = self.d:callee_load("ah736f72-21d1-4ea6-a3ea-4d7f56b3887c") + mc:verify() + + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>dummy)"), "callee") + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>sst_enable)"),"yes") + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>sst_refresh_method)"), "UPDATE_FALLBACK_INVITE") + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>outbound_from_user)"), "upn") + end + + function TestNGCPProfilePrefs:test_clean() + local xavp = NGCPProfilePrefs:xavp('callee') + xavp("testid",1) + xavp("foo","foo") + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>testid)"),1) + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>foo)"),"foo") + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>dummy)"),"caller") + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>dummy)"),"callee") + self.d:clean() + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>dummy)"),"caller") + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>dummy)"),"callee") + assertFalse(sr.pv.get("$xavp(prof)")) + end + + function TestNGCPProfilePrefs:test_callee_clean() + local callee_xavp = NGCPProfilePrefs:xavp('callee') + callee_xavp("testid",1) + callee_xavp("foo","foo") + local caller_xavp = NGCPProfilePrefs:xavp('caller') + caller_xavp("other",1) + caller_xavp("otherfoo","foo") + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>testid)"),1) + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>foo)"),"foo") + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>dummy)"),"caller") + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>other)"),1) + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>otherfoo)"),"foo") + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>dummy)"),"callee") + self.d:clean('callee') + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>dummy)"),'caller') + assertFalse(sr.pv.get("$xavp(callee_prof_prefs=>testid)")) + assertFalse(sr.pv.get("$xavp(callee_prof_prefs=>foo)")) + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>other)"),1) + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>otherfoo)"),"foo") + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>dummy)"),"callee") + end + + function TestNGCPProfilePrefs:test_caller_clean() + local callee_xavp = NGCPProfilePrefs:xavp('callee') + callee_xavp("testid",1) + callee_xavp("foo","foo") + local caller_xavp = NGCPProfilePrefs:xavp('caller') + caller_xavp("other",1) + caller_xavp("otherfoo","foo") + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>testid)"),1) + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>foo)"),"foo") + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>dummy)"),"caller") + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>other)"),1) + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>otherfoo)"),"foo") + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>dummy)"),"callee") + self.d:clean('caller') + assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>dummy)"),"caller") + assertFalse(sr.pv.get("$xavp(caller_prof_prefs=>other)")) + assertFalse(sr.pv.get("$xavp(caller_prof_prefs=>otherfoo)")) + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>testid)"),1) + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>foo)"),"foo") + assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>dummy)"),"callee") + end + + function TestNGCPProfilePrefs:test_tostring() + local callee_xavp = NGCPProfilePrefs:xavp('callee') + callee_xavp("testid",1) + callee_xavp("foo","foo") + local caller_xavp = NGCPProfilePrefs:xavp('caller') + caller_xavp("other",1) + caller_xavp("otherfoo","foo") + assertEquals(tostring(self.d), 'caller_prof_prefs:{other={1},otherfoo={"foo"},dummy={"caller"}}\ncallee_prof_prefs:{dummy={"callee"},testid={1},foo={"foo"}}\n') + end +-- class TestNGCPProfilePrefs +--EOF \ No newline at end of file diff --git a/tests/test_all.lua b/tests/test_all.lua index e085a29..36f9d23 100644 --- a/tests/test_all.lua +++ b/tests/test_all.lua @@ -22,7 +22,7 @@ require 'tests.mocks' require 'tests.ngcp_avp' require 'tests.ngcp_xavp' require 'tests.ngcp_pref' ---require 'tests.ngcp_pprof' +require 'tests.ngcp_pprof' require 'tests.ngcp_dp' require 'tests.ngcp_up' require 'tests.ngcp_pp' diff --git a/tests/utils.lua b/tests/utils.lua index 3421804..ba038f4 100644 --- a/tests/utils.lua +++ b/tests/utils.lua @@ -86,6 +86,17 @@ TestUtils = {} --class assertEquals(t, {}) end + function TestUtils:test_table_size() + local t = table.size(nil) + assertEquals(t, 0) + t = table.size({1,2}) + assertEquals(t, 2) + t = table.size({}) + assertEquals(t, 0) + t = table.size({hola={1,2},adios=2}) + assertEquals(t, 2) + end + function TestUtils:test_table_shuffle() assertEquals(self.simple_list, {1,2,3}) table.add(self.simple_list, 4)