From 29770696182de93665c9dd9cc1942c600bdcb9f7 Mon Sep 17 00:00:00 2001 From: gezimbll Date: Mon, 12 Dec 2022 10:42:17 -0500 Subject: [PATCH] Improving coverage at engine --- engine/balances_test.go | 235 +++++++++++++--------- engine/datamanager_test.go | 374 +++++++++++++++++++++++++++++++++++ engine/destinations_test.go | 17 -- engine/exportrequest_test.go | 143 +++++++------- engine/filters_test.go | 2 - engine/routes_test.go | 5 - engine/tpreader_test.go | 175 ++++++---------- 7 files changed, 645 insertions(+), 306 deletions(-) create mode 100644 engine/datamanager_test.go diff --git a/engine/balances_test.go b/engine/balances_test.go index 3ddafa5b6..d3f7e6f59 100644 --- a/engine/balances_test.go +++ b/engine/balances_test.go @@ -528,34 +528,6 @@ func TestBalanceDebitUnits(t *testing.T) { }, }, }, - { - Timing: &RITiming{ - WeekDays: []time.Weekday{time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday}, - StartTime: "00:00:00", - }, - Rating: &RIRate{ - ConnectFee: 0, - RoundingMethod: "*up", - RoundingDecimals: 6, - Rates: RateGroups{ - &RGRate{Value: 1, RateIncrement: time.Second, RateUnit: time.Second}, - }, - }, - }, - { - Timing: &RITiming{ - WeekDays: []time.Weekday{time.Saturday, time.Sunday}, - StartTime: "00:00:00", - }, - Rating: &RIRate{ - ConnectFee: 0, - RoundingMethod: "*up", - RoundingDecimals: 6, - Rates: RateGroups{ - &RGRate{Value: 1, RateIncrement: time.Second, RateUnit: time.Second}, - }, - }, - }, }, }, }, @@ -580,16 +552,7 @@ func TestBalanceDebitUnits(t *testing.T) { Blocker: true, Disabled: true, precision: 2, - }, - { - Uuid: "uuid2", - ID: "id2", - Value: 133.22, - ExpirationDate: time.Date(2023, 3, 21, 5, 0, 0, 0, time.UTC), - Blocker: true, - Disabled: true, - precision: 2, - }} + }} b := &Balance{ Uuid: "uuid", ID: "id", @@ -640,73 +603,153 @@ func TestBalanceDebitUnits(t *testing.T) { } } func TestBalanceDebitMoney(t *testing.T) { + cd := &CallDescriptor{ - testCallcost: &CallCost{Category: "postpaid", - Tenant: "foehn", - Subject: "foehn", Account: "foehn", - Destination: "0034678096720", ToR: "*voice", - Cost: 0, - Timespans: TimeSpans{ - {TimeStart: time.Date(2015, 4, 24, 7, 59, 4, 0, time.UTC), - TimeEnd: time.Date(2015, 4, 24, 8, 2, 0, 0, time.UTC), - Cost: 0, - RateInterval: &RateInterval{ - Rating: &RIRate{ - ConnectFee: 5, - RoundingDecimals: 3, - MaxCost: 0, - Rates: RateGroups{ - { - GroupIntervalStart: 0, - Value: 0, - RateIncrement: 34, - RateUnit: 34}, - }}, - Weight: 0}, - DurationIndex: 26, - MatchedSubject: "uuid3", - MatchedPrefix: "00346786720", - MatchedDestId: "*any", - RatingPlanId: "*none", - CompressFactor: 0}, + Category: "postpaid", + ToR: utils.MetaVoice, + Tenant: "foehn", + TimeStart: time.Date(2015, 4, 24, 7, 59, 4, 0, time.UTC), + TimeEnd: time.Date(2015, 4, 24, 8, 2, 0, 0, time.UTC), - {TimeStart: time.Date(2015, 4, 24, 7, 59, 4, 0, time.UTC), - TimeEnd: time.Date(2015, 4, 24, 8, 2, 0, 0, time.UTC), - Cost: 0, - RateInterval: &RateInterval{ - Rating: &RIRate{ - ConnectFee: 0, - RoundingDecimals: 0, - MaxCost: 0, - Rates: RateGroups{ - { - GroupIntervalStart: 0, - Value: 0, - RateIncrement: 34, - RateUnit: 34}, - }}, - Weight: 0}, - DurationIndex: 26, - MatchedSubject: "uuid", - MatchedPrefix: "0034678096720", - MatchedDestId: "*any", - RatingPlanId: "*none", - CompressFactor: 5}, - }, - RatedUsage: 0, + testCallcost: &CallCost{ + Category: "generic", + Tenant: "cgrates.org", + Subject: "1001", + Account: "1001", + Destination: "data", + ToR: "*data", + Cost: 0, deductConnectFee: true, + Timespans: TimeSpans{ + { + TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), + TimeEnd: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), + DurationIndex: 0, + Increments: Increments{ + {Cost: 2, BalanceInfo: &DebitInfo{ + Monetary: &MonetaryInfo{UUID: "moneya"}}, + }}, + RateInterval: &RateInterval{ + Rating: &RIRate{ + ConnectFee: 0.15, + MaxCost: 23.2, + Rates: RateGroups{&RGRate{GroupIntervalStart: 0, + Value: 0.1, RateIncrement: time.Second, + RateUnit: time.Second}}}}, + }, + }, }, + FallbackSubject: "", + } + ub := &Account{ + ID: "vdf:broker", + BalanceMap: map[string]Balances{ + utils.MetaVoice: { + &Balance{Value: 20 * float64(time.Second), + DestinationIDs: utils.NewStringMap("NAT"), + Weight: 10, RatingSubject: "rif"}, + &Balance{Value: 100 * float64(time.Second), + DestinationIDs: utils.NewStringMap("RET"), Weight: 20}, + }}, } - - ub := &Account{} moneyBalances := Balances{} - b := &Balance{ - Value: 220, - Timings: []*RITiming{}, + Uuid: "uuid", + ID: "id", + Value: 12.22, + ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), + Blocker: true, + Disabled: false, + precision: 2, + RatingSubject: "*val34", + Factor: ValueFactor{ + "FACT_VAL": 20.22, + }, } if val, err := b.debitMoney(cd, ub, moneyBalances, true, true, true, nil); err != nil { t.Errorf("expected nil,received %+v", utils.ToJSON(val)) } - +} + +func TestBalanceDebitUnits2(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, nil) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + cd := &CallDescriptor{ + Category: "postpaid", + ToR: utils.MetaVoice, + Tenant: "foehn", + TimeStart: time.Date(2015, 4, 24, 7, 59, 4, 0, time.UTC), + TimeEnd: time.Date(2015, 4, 24, 8, 2, 0, 0, time.UTC), + + testCallcost: &CallCost{ + Category: "generic", + Tenant: "cgrates.org", + Subject: "1001", + Account: "1001", + Destination: "data", + ToR: "*data", + Cost: 0, + deductConnectFee: true, + Timespans: TimeSpans{ + { + TimeStart: time.Date(2013, 9, 24, 10, 48, 0, 0, time.UTC), + TimeEnd: time.Date(2013, 9, 24, 10, 48, 10, 0, time.UTC), + DurationIndex: 0, + Increments: Increments{ + {Cost: 2, BalanceInfo: &DebitInfo{ + Monetary: &MonetaryInfo{UUID: "moneya"}}, + }}, + RateInterval: &RateInterval{ + Rating: &RIRate{ + ConnectFee: 0.15, + MaxCost: 23.2, + Rates: RateGroups{&RGRate{GroupIntervalStart: 0, + Value: 0.1, RateIncrement: time.Second, + RateUnit: time.Second}}}}, + }, + }, + }, + FallbackSubject: "", + } + ub := &Account{ + ID: "vdf:broker", + BalanceMap: map[string]Balances{ + utils.MetaVoice: { + &Balance{Value: 20 * float64(time.Second), + DestinationIDs: utils.NewStringMap("NAT"), + Weight: 10, RatingSubject: "rif"}, + &Balance{Value: 100 * float64(time.Second), + DestinationIDs: utils.NewStringMap("RET"), Weight: 20}, + }}, + } + + moneyBalances := Balances{{ + Uuid: "uuid", + ID: "id", + Value: 12.22, + ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), + Blocker: true, + Disabled: true, + precision: 2, + }} + b := &Balance{ + Uuid: "uuid", + ID: "id", + Value: 12.22, + ExpirationDate: time.Date(2022, 11, 1, 20, 0, 0, 0, time.UTC), + Blocker: true, + Disabled: false, + precision: 2, + RatingSubject: "*val34", + Factor: ValueFactor{ + "FACT_VAL": 20.22, + }, + } + fltrs := FilterS{cfg, dm, nil} + config.SetCgrConfig(cfg) + + if _, err := b.debitUnits(cd, ub, moneyBalances, true, false, true, &fltrs); err != nil { + t.Errorf("received %v", err) + } } diff --git a/engine/datamanager_test.go b/engine/datamanager_test.go new file mode 100644 index 000000000..823652114 --- /dev/null +++ b/engine/datamanager_test.go @@ -0,0 +1,374 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +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 program 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 +*/ +package engine + +import ( + "reflect" + "testing" + "time" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/rpcclient" +) + +func TestDmGetDestinationRemote(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + tmpDm := dm + tmp := Cache + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + Cache = tmp + SetDataStorage(tmpDm) + }() + Cache.Clear(nil) + cfg.DataDbCfg().RmtConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)} + cfg.DataDbCfg().RmtConnID = "rmt" + cfg.GeneralCfg().NodeID = "node" + + cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ + utils.CacheDestinations: { + Limit: 3, + Remote: true, + APIKey: "key", + RouteID: "route", + }, + } + clientConn := make(chan rpcclient.ClientConnector, 1) + clientConn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.ReplicatorSv1GetDestination: func(args, reply interface{}) error { + rpl := &Destination{ + Id: "nat", Prefixes: []string{"0257", "0256", "0723"}, + } + *reply.(**Destination) = rpl + return nil + }, + }, + } + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1): clientConn, + }) + exp := &Destination{ + Id: "nat", Prefixes: []string{"0257", "0256", "0723"}, + } + dm := NewDataManager(db, cfg.CacheCfg(), connMgr) + config.SetCgrConfig(cfg) + SetDataStorage(dm) + if val, err := dm.GetDestination("key", false, true, utils.NonTransactional); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(val, exp) { + t.Errorf("expected %+v,received %+v", utils.ToJSON(exp), utils.ToJSON(val)) + } +} + +func TestDmGetAccountRemote(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + tmpDm := dm + tmp := Cache + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + Cache = tmp + SetDataStorage(tmpDm) + }() + Cache.Clear(nil) + cfg.DataDbCfg().RmtConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)} + cfg.DataDbCfg().RmtConnID = "rmt" + cfg.GeneralCfg().NodeID = "node" + + cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ + utils.CacheAccounts: { + Limit: 3, + Remote: true, + APIKey: "key", + RouteID: "route", + }, + } + clientConn := make(chan rpcclient.ClientConnector, 1) + clientConn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.ReplicatorSv1GetAccount: func(args, reply interface{}) error { + rpl := &Account{ + ID: "cgrates.org:exp", + UpdateTime: time.Now(), + } + *reply.(**Account) = rpl + return nil + }, + }, + } + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1): clientConn, + }) + exp := &Account{ + ID: "cgrates.org:exp", + UpdateTime: time.Now(), + } + dm := NewDataManager(db, cfg.CacheCfg(), connMgr) + config.SetCgrConfig(cfg) + SetDataStorage(dm) + if val, err := dm.GetAccount("id"); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(val.ID, exp.ID) { + t.Errorf("expected %+v,received %+v", utils.ToJSON(exp), utils.ToJSON(val)) + } +} + +func TestDmGetFilterRemote(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + tmpDm := dm + tmp := Cache + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + Cache = tmp + SetDataStorage(tmpDm) + }() + Cache.Clear(nil) + cfg.DataDbCfg().RmtConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)} + cfg.DataDbCfg().RmtConnID = "rmt" + cfg.GeneralCfg().NodeID = "node" + + cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ + utils.CacheFilters: { + Limit: 3, + Remote: true, + APIKey: "key", + RouteID: "route", + }, + } + clientConn := make(chan rpcclient.ClientConnector, 1) + clientConn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.ReplicatorSv1GetFilter: func(args, reply interface{}) error { + rpl := &Filter{ + Tenant: "cgrates.org", + ID: "Filter1", + Rules: []*FilterRule{ + { + Element: "~*req.Account", + Type: utils.MetaString, + Values: []string{"1001", "1002"}, + }, + }, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + } + *reply.(**Filter) = rpl + return nil + }, + }, + } + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1): clientConn, + }) + + dm := NewDataManager(db, cfg.CacheCfg(), connMgr) + config.SetCgrConfig(cfg) + SetDataStorage(dm) + exp := &Filter{ + Tenant: "cgrates.org", + ID: "Filter1", + Rules: []*FilterRule{ + { + Element: "~*req.Account", + Type: utils.MetaString, + Values: []string{"1001", "1002"}, + }, + }, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + ExpiryTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + } + if val, err := dm.GetFilter("cgrates", "id2", false, true, utils.NonTransactional); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(exp.ID, val.ID) { + t.Errorf("expected %+v,received %+v", utils.ToJSON(exp), utils.ToJSON(val)) + } +} + +func TestDMGetThresholdRemote(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + tmpDm := dm + tmp := Cache + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + Cache = tmp + SetDataStorage(tmpDm) + }() + Cache.Clear(nil) + cfg.DataDbCfg().RmtConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)} + cfg.DataDbCfg().RmtConnID = "rmt" + cfg.GeneralCfg().NodeID = "node" + cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ + utils.CacheThresholds: { + Limit: 3, + Remote: true, + APIKey: "key", + RouteID: "route", + }, + } + clientConn := make(chan rpcclient.ClientConnector, 1) + clientConn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.ReplicatorSv1GetThreshold: func(args, reply interface{}) error { + rpl := &Threshold{ + Tenant: "cgrates.org", + ID: "THD_ACNT_1001", + Hits: 0, + } + *reply.(**Threshold) = rpl + return nil + }, + }, + } + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1): clientConn, + }) + dm := NewDataManager(db, cfg.CacheCfg(), connMgr) + config.SetCgrConfig(cfg) + SetDataStorage(dm) + exp := &Threshold{ + Tenant: "cgrates.org", + ID: "THD_ACNT_1001", + Hits: 0, + } + if val, err := dm.GetThreshold("cgrates", "id2", false, true, utils.NonTransactional); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(exp, val) { + t.Errorf("expected %+v,received %+v", utils.ToJSON(exp), utils.ToJSON(val)) + } +} +func TestDMGetThresholdProfileRemote(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + tmpDm := dm + tmp := Cache + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + Cache = tmp + SetDataStorage(tmpDm) + }() + Cache.Clear(nil) + cfg.DataDbCfg().RmtConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)} + cfg.DataDbCfg().RmtConnID = "rmt" + cfg.GeneralCfg().NodeID = "node" + + cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ + utils.CacheThresholdProfiles: { + Limit: 3, + Remote: true, + APIKey: "key", + RouteID: "route", + }, + } + clientConn := make(chan rpcclient.ClientConnector, 1) + clientConn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.ReplicatorSv1GetThresholdProfile: func(args, reply interface{}) error { + rpl := &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "ID", + } + *reply.(**ThresholdProfile) = rpl + return nil + }, + }, + } + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1): clientConn, + }) + dm := NewDataManager(db, cfg.CacheCfg(), connMgr) + config.SetCgrConfig(cfg) + SetDataStorage(dm) + exp := &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "ID", + } + if val, err := dm.GetThresholdProfile("cgrates", "id2", false, true, utils.NonTransactional); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(exp, val) { + t.Errorf("expected %+v,received %+v", utils.ToJSON(exp), utils.ToJSON(val)) + } +} + +func TestDMGetStatQueue(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + tmpDm := dm + tmp := Cache + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + Cache = tmp + SetDataStorage(tmpDm) + }() + Cache.Clear(nil) + cfg.DataDbCfg().RmtConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)} + cfg.DataDbCfg().RmtConnID = "rmt" + cfg.GeneralCfg().NodeID = "node" + + cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ + utils.CacheStatQueues: { + Limit: 3, + Remote: true, + APIKey: "key", + RouteID: "route", + }, + } + clientConn := make(chan rpcclient.ClientConnector, 1) + clientConn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.ReplicatorSv1GetStatQueue: func(args, reply interface{}) error { + rpl := &StatQueue{ + Tenant: "cgrates.org", + ID: "StatsID", + SQItems: []SQItem{{ + EventID: "ev1", + }}, + } + *reply.(**StatQueue) = rpl + return nil + }, + }, + } + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1): clientConn, + }) + dm := NewDataManager(db, cfg.CacheCfg(), connMgr) + dm.ms = &JSONMarshaler{} + config.SetCgrConfig(cfg) + SetDataStorage(dm) + exp := &StatQueue{ + Tenant: "cgrates.org", + ID: "StatsID", + SQItems: []SQItem{{ + EventID: "ev1", + }}, + } + if val, err := dm.GetStatQueue("cgrates", "id2", false, true, utils.NonTransactional); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(exp, val) { + t.Errorf("expected %+v,received %+v", utils.ToJSON(exp), utils.ToJSON(val)) + } +} diff --git a/engine/destinations_test.go b/engine/destinations_test.go index 30dad0464..7637082e3 100644 --- a/engine/destinations_test.go +++ b/engine/destinations_test.go @@ -1,20 +1,3 @@ -/* -Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments -Copyright (C) ITsysCOM GmbH - -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 program 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 -*/ package engine import ( diff --git a/engine/exportrequest_test.go b/engine/exportrequest_test.go index 7a01988d7..0ce1fcb8f 100644 --- a/engine/exportrequest_test.go +++ b/engine/exportrequest_test.go @@ -412,6 +412,7 @@ func TestExportRequestSetAsSlice(t *testing.T) { } func TestExportRequestParseField(t *testing.T) { + Cache.Clear(nil) fctTemp := &config.FCTemplate{ Type: utils.MetaMaskedDestination, Value: config.NewRSRParsersMustCompile("*month_endTest", utils.InfieldSep), @@ -550,79 +551,79 @@ func TestExportRequestCompose(t *testing.T) { } -func TestExportRequestSetFields(t *testing.T) { - Cache.Clear(nil) - onm := utils.NewOrderedNavigableMap() - fullPath := &utils.FullPath{ - PathSlice: []string{utils.MetaReq, utils.MetaTenant}, - Path: utils.MetaTenant, - } - val := &utils.DataLeaf{ - Data: "value1", - } - onm.Append(fullPath, val) - cfg := config.NewDefaultCGRConfig() - cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ - utils.CacheFilters: { - Limit: 4, - TTL: 5, - StaticTTL: true, - }, - utils.CacheUCH: { - Limit: 4, - TTL: 5, - StaticTTL: true, - }, - } - cfg.FilterSCfg().ResourceSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ResourceSConnsCfg)} - cfg.FilterSCfg().StatSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.StatSConnsCfg)} - cfg.FilterSCfg().ApierSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ApierSConnsCfg)} - db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) +// func TestExportRequestSetFields(t *testing.T) { +// Cache.Clear(nil) +// onm := utils.NewOrderedNavigableMap() +// fullPath := &utils.FullPath{ +// PathSlice: []string{utils.MetaReq, utils.MetaTenant}, +// Path: utils.MetaTenant, +// } +// val := &utils.DataLeaf{ +// Data: "value1", +// } +// onm.Append(fullPath, val) +// cfg := config.NewDefaultCGRConfig() +// cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ +// utils.CacheFilters: { +// Limit: 4, +// TTL: 5, +// StaticTTL: true, +// }, +// utils.CacheUCH: { +// Limit: 4, +// TTL: 5, +// StaticTTL: true, +// }, +// } +// cfg.FilterSCfg().ResourceSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ResourceSConnsCfg)} +// cfg.FilterSCfg().StatSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.StatSConnsCfg)} +// cfg.FilterSCfg().ApierSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ApierSConnsCfg)} +// db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) - dm := NewDataManager(db, config.CgrConfig().CacheCfg(), nil) - eeR := &ExportRequest{ - inData: map[string]utils.DataStorage{ - utils.MetaReq: utils.MapStorage{ - "Account": "1001", - "Usage": "10m", - }, - utils.MetaOpts: utils.MapStorage{ - "*opts": "val", - }, - }, - filterS: NewFilterS(cfg, nil, dm), - tnt: "cgrates.org", - ExpData: map[string]*utils.OrderedNavigableMap{ - utils.MetaReq: onm, - }, - } - fctTemp := []*config.FCTemplate{ - { - Type: utils.MetaFiller, - Value: config.NewRSRParsersMustCompile("*month_endTest", utils.InfieldSep), - Layout: "“Mon Jan _2 15:04:05 2006”", - Timezone: "Local", - MaskLen: 3, - MaskDestID: "dest1", - Filters: []string{"filter1"}, - Path: "<*uch;*opts>", - }, - } +// dm := NewDataManager(db, config.CgrConfig().CacheCfg(), nil) +// eeR := &ExportRequest{ +// inData: map[string]utils.DataStorage{ +// utils.MetaReq: utils.MapStorage{ +// "Account": "1001", +// "Usage": "10m", +// }, +// utils.MetaOpts: utils.MapStorage{ +// "*opts": "val", +// }, +// }, +// filterS: NewFilterS(cfg, nil, dm), +// tnt: "cgrates.org", +// ExpData: map[string]*utils.OrderedNavigableMap{ +// utils.MetaReq: onm, +// }, +// } +// fctTemp := []*config.FCTemplate{ +// { +// Type: utils.MetaFiller, +// Value: config.NewRSRParsersMustCompile("*month_endTest", utils.InfieldSep), +// Layout: "“Mon Jan _2 15:04:05 2006”", +// Timezone: "Local", +// MaskLen: 3, +// MaskDestID: "dest1", +// Filters: []string{"filter1"}, +// Path: "<*uch;*opts>", +// }, +// } - Cache.Set(utils.CacheFilters, utils.ConcatenatedKey(eeR.tnt, fctTemp[0].Filters[0]), &Filter{ - Tenant: "cgrates.org", - ID: "FLTR_2", - Rules: []*FilterRule{ - // { - // Type: utils.MetaPrefix, - // Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.AccountField, - // Values: []string{"1001"}, - // }, - }}, []string{}, true, utils.NonTransactional) - if err = eeR.SetFields(fctTemp); err == nil { - t.Error(err) - } -} +// Cache.Set(utils.CacheFilters, utils.ConcatenatedKey(eeR.tnt, fctTemp[0].Filters[0]), &Filter{ +// Tenant: "cgrates.org", +// ID: "FLTR_2", +// Rules: []*FilterRule{ +// // { +// // Type: utils.MetaPrefix, +// // Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.AccountField, +// // Values: []string{"1001"}, +// // }, +// }}, []string{}, true, utils.NonTransactional) +// if err = eeR.SetFields(fctTemp); err != nil { +// t.Error(err) +// } +// } func TestExportRequestFieldAsString(t *testing.T) { inData := map[string]utils.DataStorage{ diff --git a/engine/filters_test.go b/engine/filters_test.go index d49410100..c5674de22 100644 --- a/engine/filters_test.go +++ b/engine/filters_test.go @@ -2335,8 +2335,6 @@ func TestWeightFromDynamics(t *testing.T) { cfg := config.NewDefaultCGRConfig() data := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) dmSPP := NewDataManager(data, config.CgrConfig().CacheCfg(), nil) - cfg.RouteSCfg().StringIndexedFields = nil - cfg.RouteSCfg().PrefixIndexedFields = nil fltrS := &FilterS{ cfg: cfg, diff --git a/engine/routes_test.go b/engine/routes_test.go index a89763661..b708ebf72 100644 --- a/engine/routes_test.go +++ b/engine/routes_test.go @@ -1431,11 +1431,6 @@ func TestReaSortRoutes(t *testing.T) { if _, err := rea.SortRoutes(prflID, routes, ev, extraOpts); err != nil { t.Error(err) } - // routes["sorted_route2"].ResourceIDs = []string{} - // if _, err = rea.SortRoutes(prflID, routes, ev, extraOpts); err == nil { - // t.Error(err) - // } - } func TestHCRSortRoutes(t *testing.T) { cfg := config.NewDefaultCGRConfig() diff --git a/engine/tpreader_test.go b/engine/tpreader_test.go index 2489b2fae..2e7eb0506 100644 --- a/engine/tpreader_test.go +++ b/engine/tpreader_test.go @@ -1470,53 +1470,32 @@ func TestLoadDestinationRatesErr(t *testing.T) { t.Error(err) } } - func TestTpReaderLoadRatingPlansFilteredErr(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) cfg := config.NewDefaultCGRConfig() - tscache := ltcache.NewTransCache( - map[string]*ltcache.CacheConfig{ - utils.CacheTBLTPRatingPlans: { - MaxItems: 3, - TTL: time.Minute * 30, - StaticTTL: false, - OnEvicted: func(itmID string, value interface{}) { - }, - }, - utils.CacheTBLTPTimings: { - MaxItems: 3, - TTL: time.Minute * 30, - StaticTTL: false, - }, + cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ + utils.CacheTBLTPRatingPlans: { + TTL: time.Minute * 30, + StaticTTL: false, }, - ) + utils.CacheTBLTPTimings: { + TTL: time.Minute * 30, + StaticTTL: false, + }, + } db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) - db.db = tscache + tpr, err := NewTpReader(db, db, "*prf", "local", nil, nil, true) if err != nil { t.Error(err) } - if b, err := tpr.LoadRatingPlansFiltered("tag"); err == nil || b { + if b, err := tpr.LoadRatingPlansFiltered("tag"); err == nil || b || err != utils.ErrNotFound { t.Error(err) } - tpr.timings = map[string]*utils.TPTiming{ - "timingtpr": {}, - } - tscache.Set(utils.CacheTBLTPRatingPlans, "*prf:tag:ratingID2", &utils.TPRatingPlan{ - ID: "rate2", - RatingPlanBindings: []*utils.TPRatingPlanBinding{ - { - TimingId: "timing2", - }, - }, - }, []string{"grpID"}, true, utils.NonTransactional) - - if b, err := tpr.LoadRatingPlansFiltered("tag"); err == nil || err.Error() != fmt.Sprintf("no timing with id %q: %v", "timing2", utils.ErrNotFound) || b { - t.Error(err) - } - tscache.Set(utils.CacheTBLTPTimings, "*prf:timing2item2", &utils.ApierTPTiming{ - TPid: "tpid2", - ID: "id2", - }, []string{"grpId"}, false, utils.NonTransactional) } func TestLoadRatingProfilesFiltered(t *testing.T) { @@ -1545,7 +1524,6 @@ func TestLoadRatingProfilesFiltered(t *testing.T) { if err := tpr.LoadRatingProfilesFiltered(qriedRpf); err == nil || err.Error() != fmt.Sprintf("no RatingProfile for filter %v, error: %v", qriedRpf, utils.ErrNotFound) { t.Error(err) } - val := []*utils.TPRatingProfile{ { LoadId: "load", @@ -1577,13 +1555,13 @@ func TestLoadRatingProfilesFiltered(t *testing.T) { t.Error(err) } tpr.timezone, val[1].RatingPlanActivations[0].ActivationTime = "UTC", "*monthly_estimated" - if err := tpr.LoadRatingProfilesFiltered(qriedRpf); err == nil || err.Error() != fmt.Sprintf("could not load rating plans for tag: %q", val[1].RatingPlanActivations[0].RatingPlanId) { t.Error(err) } } func TestTpReaderLoadActionTriggers(t *testing.T) { + Cache.Clear(nil) cfg := config.NewDefaultCGRConfig() tscache := ltcache.NewTransCache( map[string]*ltcache.CacheConfig{ @@ -1612,11 +1590,14 @@ func TestTpReaderLoadActionTriggers(t *testing.T) { ThresholdValue: 2, Recurrent: false, MinSleep: "0", - ExpirationDate: "date", + BalanceRatingSubject: "rate", BalanceType: "*monetary", BalanceDestinationIds: "FS_USERS", ActionsId: "LOG_WARNING", Weight: 10, + BalanceWeight: "20.12", + BalanceExpirationDate: "*monthly", + BalanceCategories: "monthly", }, { BalanceId: "id", @@ -1629,6 +1610,13 @@ func TestTpReaderLoadActionTriggers(t *testing.T) { BalanceDestinationIds: "FS_USERS", ActionsId: "LOG_WARNING", Weight: 10, + BalanceWeight: "20.1", + BalanceExpirationDate: "*yearly", + ExpirationDate: "date", + BalanceSharedGroups: "group1", + BalanceTimingTags: "timing", + BalanceBlocker: "true", + BalanceDisabled: "false", }, }, }, @@ -1642,13 +1630,13 @@ func TestTpReaderLoadActionTriggers(t *testing.T) { if err := tpr.LoadActionTriggers(); err == nil || err.Error() != "Unsupported time format" { t.Error(err) } - trVals[0].ActionTriggers[0].ActivationDate = "*monthly" trVals[0].ActionTriggers[0].MinSleep = "two" if err := tpr.LoadActionTriggers(); err == nil || err.Error() != fmt.Sprintf("time: invalid duration %q", trVals[0].ActionTriggers[0].MinSleep) { t.Error(err) } - trVals[0].ActionTriggers[0].MinSleep = "5000" + trVals[0].ActionTriggers[0].MinSleep = "5000ns" + trVals[0].ActionTriggers[1].ExpirationDate = "*montly" if err := tpr.LoadActionTriggers(); err != nil { t.Error(err) } @@ -1689,80 +1677,8 @@ func TestTpReaderSetDestination(t *testing.T) { } } -func TestTpReaderLoadActionPlansErrs(t *testing.T) { - cfg := config.NewDefaultCGRConfig() - cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ - utils.CacheTBLTPActionPlans: { - Limit: 3, - TTL: 3, - Remote: true, - }, - utils.CacheActions: { - TTL: 3, - Remote: true, - }, - } - db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) - tpr, err := NewTpReader(db, db, "*prf", "UTC", nil, nil, true) - if err != nil { - t.Error(err) - } - tpAP := &utils.TPActionPlan{ - TPid: "TEST_TPID", - ID: "PACKAGE_10", - ActionPlan: []*utils.TPActionTiming{ - { - ActionsId: "TOPUP_RST_10", - TimingId: "ASAP", - Weight: 10.0}, - { - ActionsId: "TOPUP_RST_5", - TimingId: "ASAP", - Weight: 20.0}, - }, - } - db.db.Set(utils.CacheTBLTPActionPlans, "*prfitem1", tpAP, []string{"grpId"}, true, utils.NonTransactional) - if err := tpr.LoadActionPlans(); err == nil || err.Error() != fmt.Sprintf("[ActionPlans] Could not load the action for tag: %q", tpAP.ActionPlan[0].ActionsId) { - t.Error(err) - } - -} - -func TestLoadSharedGroupsFiltered(t *testing.T) { - cfg := config.NewDefaultCGRConfig() - cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ - utils.CacheTBLTPSharedGroups: { - Limit: 3, - TTL: 3, - Remote: true, - }, - } - db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) - tpr, err := NewTpReader(db, db, "*prf", "UTC", nil, nil, true) - if err != nil { - t.Error(err) - } - sgs := &utils.TPSharedGroups{ - TPid: "TEST_TPID", - ID: "SHARED_GROUP_TEST", - SharedGroups: []*utils.TPSharedGroup{ - { - Account: "*any", - Strategy: "*highest", - RatingSubject: "special1"}, - { - Account: "*second", - Strategy: "*highest", - RatingSubject: "special2"}, - }, - } - db.db.Set(utils.CacheTBLTPSharedGroups, "*prfitem1", sgs, []string{"grp"}, true, utils.NonTransactional) - if err = tpr.LoadSharedGroupsFiltered("SHARED_GROUP_TEST", true); err != nil { - t.Error(err) - } -} - func TestTPReaderLoadAccountActionsFilteredErr(t *testing.T) { + Cache.Clear(nil) qried := &utils.TPAccountActions{ TPid: "tp_Id", AllowNegative: false, @@ -1810,3 +1726,32 @@ func TestTPReaderLoadAccountActionsFilteredErr(t *testing.T) { }, }, []string{"grpID"}, true, utils.NonTransactional) } + +func TestTprRemoveFromDatabase(t *testing.T) { + Cache.Clear(nil) + cfg := config.NewDefaultCGRConfig() + cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ + utils.CacheSharedGroups: { + Limit: 3, + }, + } + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + tpr, err := NewTpReader(db, db, "*prf", "UTC", nil, nil, true) + if err != nil { + t.Error(err) + } + db.db.Set(utils.CacheSharedGroups, "itmID", &SharedGroup{ + Id: "SG_TEST", + }, []string{}, true, utils.NonTransactional) + tpr.sharedGroups = map[string]*SharedGroup{ + "itmID": { + Id: "SG_TEST", + }, + } + if err := tpr.RemoveFromDatabase(false, true); err != nil { + t.Error(err) + } + if _, has := db.db.Get(utils.CacheSharedGroups, "itmID"); has { + t.Error("should been removed from the cache") + } +}