diff --git a/engine/actions_test.go b/engine/actions_test.go index ee61dc286..00a857e4c 100644 --- a/engine/actions_test.go +++ b/engine/actions_test.go @@ -2652,7 +2652,9 @@ func TestCdrLogAction(t *testing.T) { utils.ConcatenatedKey(utils.MetaInternal, utils.MetaCDRs): internalChan, }) - var extraData interface{} + extraData := map[string]interface{}{ + "test": "val", + } acc := &Account{ ID: "cgrates.org:1001", BalanceMap: map[string]Balances{ @@ -2725,6 +2727,7 @@ func TestCdrLogAction(t *testing.T) { "Tenant": "cgrates.org", "ToR": "*monetary", "Usage": mock.args.CGREvent.Event["Usage"], + "test": "val", }, APIOpts: map[string]interface{}{}, } @@ -3145,7 +3148,16 @@ func TestExportAction(t *testing.T) { balanceValue: 10, }, } - if err := export(ub, a, acs, nil, utils.CGREvent{Tenant: "cgrates.org", ID: "id"}); err != nil { + extraData := &utils.CGREvent{ + Tenant: "tenant", + ID: "id1", + Time: utils.TimePointer(time.Date(2022, 12, 1, 1, 0, 0, 0, time.UTC)), + Event: map[string]interface{}{}, + APIOpts: map[string]interface{}{}, + } + if err := export(ub, a, acs, nil, nil); err != nil { + t.Errorf("received %v", err) + } else if err = export(nil, a, acs, nil, extraData); err != nil { t.Errorf("received %v", err) } } @@ -3384,3 +3396,186 @@ func TestRemoveSessionCost(t *testing.T) { t.Error(err) } } + +func TestLogAction(t *testing.T) { + acc := &Account{ + ID: "cgrates.org:1001", + BalanceMap: map[string]Balances{ + utils.MetaMonetary: { + &Balance{Value: 20}, + }, + }, + UnitCounters: UnitCounters{ + utils.MetaMonetary: []*UnitCounter{ + { + Counters: CounterFilters{ + &CounterFilter{Value: 1}, + }, + }, + }, + }, + } + extraData := map[string]interface{}{ + "field1": "value", + "field2": "second", + } + if err := logAction(acc, nil, nil, nil, nil); err != nil { + t.Error(err) + } else if err = logAction(nil, nil, nil, nil, extraData); err != nil { + t.Error(err) + } + +} + +func TestCdrLogProviderFieldAsInterface(t *testing.T) { + acc := &Account{ + ID: "ACCID", + ActionTriggers: ActionTriggers{ + &ActionTrigger{ + ID: "acTrigger", + UniqueID: "uuid_acc", + Recurrent: false, + }, + &ActionTrigger{ + ID: "acTrigger1", + UniqueID: "uuid_acc1", + Recurrent: false, + }, + }, + BalanceMap: map[string]Balances{ + utils.MetaMonetary: { + + &Balance{Value: 10, + DestinationIDs: utils.StringMap{ + + "*ddc_dest": true, + "*dest": false, + }}, + }, + utils.MetaVoice: { + &Balance{Value: 10, Weight: 20, DestinationIDs: utils.NewStringMap("NAT")}, + &Balance{Weight: 10, DestinationIDs: utils.NewStringMap("RET")}, + }, + }, + UnitCounters: UnitCounters{ + utils.MetaMonetary: []*UnitCounter{ + { + Counters: CounterFilters{ + &CounterFilter{Value: 1}, + }, + }, + }, + }, + } + a := &Action{ + + Id: "CDRLog1", + ActionType: utils.CDRLog, + ExtraParameters: "{\"BalanceID\":\"~*acnt.BalanceID\",\"ActionID\":\"~*act.ActionID\",\"BalanceValue\":\"~*acnt.BalanceValue\"}", + Weight: 50, + Balance: &BalanceFilter{ + Uuid: utils.StringPointer("uuid113"), + ID: utils.StringPointer("b_id_22"), + Type: utils.StringPointer("*prepaid"), + RatingSubject: utils.StringPointer("rate"), + DestinationIDs: &utils.StringMap{ + "dest1": true, + }, + Value: &utils.ValueFormula{ + Static: 3.0, + }, + Categories: &utils.StringMap{ + "category": true, + }, + SharedGroups: &utils.StringMap{ + "group1": true, + }, + ExpirationDate: utils.TimePointer(time.Date(2022, 1, 1, 2, 0, 0, 0, time.UTC)), + Weight: utils.Float64Pointer(323.0), + }, + } + cd := &cdrLogProvider{action: a, + acnt: acc, + cache: utils.MapStorage{ + "field": "val", + }} + if val, err := cd.FieldAsInterface([]string{utils.MetaAcnt, utils.AccountID}); err != nil { + t.Error(err) + } else if val != acc.ID { + t.Errorf("expected %v,received %v", acc.ID, val) + } + if _, has := cd.cache[utils.MetaAcnt]; !has { + t.Error("field does not exist") + } + if val, err := cd.FieldAsInterface([]string{utils.MetaAcnt, utils.BalanceUUID}); err != nil { + t.Error(err) + } else if val != *a.Balance.Uuid { + t.Errorf("expected %v,received %v", *a.Balance.Uuid, val) + } + if _, has := cd.cache[utils.MetaAcnt]; !has { + t.Error("field does not exist") + } + + if val, err := cd.FieldAsInterface([]string{utils.MetaAcnt, utils.DestinationIDs}); err != nil { + t.Error(err) + } else if val != a.Balance.DestinationIDs.String() { + t.Errorf("expected %v,received %v", *a.Balance.Uuid, val) + } + if _, has := cd.cache[utils.MetaAcnt]; !has { + t.Error("field does not exist") + } + + if val, err := cd.FieldAsInterface([]string{utils.MetaAcnt, utils.ExtraParameters}); err != nil { + t.Error() + } else if val != a.ExtraParameters { + t.Errorf("expected %v,received %v", *a.Balance.Uuid, val) + } + if _, has := cd.cache[utils.MetaAcnt]; !has { + t.Error("field does not exist") + } + + if val, err := cd.FieldAsInterface([]string{utils.MetaAcnt, utils.RatingSubject}); err != nil { + t.Error() + } else if val != *a.Balance.RatingSubject { + t.Errorf("expected %v,received %v", *a.Balance.Uuid, val) + } + if _, has := cd.cache[utils.MetaAcnt]; !has { + t.Error("field does not exist") + } + + if val, err := cd.FieldAsInterface([]string{utils.MetaAcnt, utils.Category}); err != nil { + t.Error() + } else if val != a.Balance.Categories.String() { + t.Errorf("expected %v,received %v", *a.Balance.Uuid, val) + } + if _, has := cd.cache[utils.MetaAcnt]; !has { + t.Error("field does not exist") + } + + if val, err := cd.FieldAsInterface([]string{utils.MetaAcnt, utils.SharedGroups}); err != nil { + t.Error() + } else if val != a.Balance.SharedGroups.String() { + t.Errorf("expected %v,received %v", *a.Balance.Uuid, val) + } + if _, has := cd.cache[utils.MetaAcnt]; !has { + t.Error("field does not exist") + } + + if val, err := cd.FieldAsInterface([]string{utils.MetaAct, utils.ActionType}); err != nil { + t.Error() + } else if val != a.ActionType { + t.Errorf("expected %v,received %v", *a.Balance.Uuid, val) + } + if _, has := cd.cache[utils.MetaAct]; !has { + t.Error("field does not exist") + } + if val, err := cd.FieldAsInterface([]string{"val"}); err != nil { + t.Error() + } else if val != "val" { + t.Errorf("expected %v,received %v", *a.Balance.Uuid, val) + } + if _, has := cd.cache["val"]; !has { + t.Error("field does not exist") + } + +} diff --git a/engine/destinations_test.go b/engine/destinations_test.go index 39a92854e..30dad0464 100644 --- a/engine/destinations_test.go +++ b/engine/destinations_test.go @@ -21,7 +21,6 @@ import ( "encoding/json" "reflect" "testing" - "time" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" @@ -196,8 +195,8 @@ func TestDPNewLibNumber(t *testing.T) { } -func TestDMSetDestination(t *testing.T) { - +func TestDMSetDestinationSucces(t *testing.T) { + Cache.Clear(nil) cfg := config.NewDefaultCGRConfig() cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ utils.MetaDestinations: { @@ -208,17 +207,7 @@ func TestDMSetDestination(t *testing.T) { cfg.DataDbCfg().RplFiltered = true cfg.DataDbCfg().RplCache = "cache" - db := NewInternalDB(nil, nil, true, map[string]*config.ItemOpt{ - utils.CacheDestinations: { - Limit: 2, - TTL: 2 * time.Minute, - StaticTTL: true, - Remote: true, - Replicate: true, - RouteID: "route", - APIKey: "api", - }, - }) + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) clientConn := make(chan rpcclient.ClientConnector, 1) clientConn <- &ccMock{ calls: map[string]func(args interface{}, reply interface{}) error{ @@ -237,24 +226,24 @@ func TestDMSetDestination(t *testing.T) { Prefixes: []string{}, } dm := NewDataManager(db, cfg.CacheCfg(), connMngr) + config.SetCgrConfig(cfg) - Cache = NewCacheS(cfg, dm, nil) if err := dm.SetDestination(dest, utils.NonTransactional); err != nil { t.Error(err) } } -func TestDMSetAccount(t *testing.T) { +func TestDMSetAccountSucces(t *testing.T) { Cache.Clear(nil) cfg := config.NewDefaultCGRConfig() - cfg.DataDbCfg().RplConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg)} + /*cfg.DataDbCfg().RplConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg)} cfg.DataDbCfg().RplFiltered = false cfg.DataDbCfg().Items = map[string]*config.ItemOpt{ utils.MetaAccounts: { Replicate: true, }, - } + }*/ clientConn := make(chan rpcclient.ClientConnector, 1) clientConn <- &ccMock{ calls: map[string]func(args interface{}, reply interface{}) error{ @@ -267,16 +256,8 @@ func TestDMSetAccount(t *testing.T) { connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg): clientConn, }) - db := NewInternalDB(nil, nil, true, map[string]*config.ItemOpt{ - utils.CacheAccounts: { - Limit: 3, - TTL: 2 * time.Minute, - StaticTTL: true, - Remote: true, - Replicate: true, - }, - }) - dm := NewDataManager(db, cfg.CacheCfg(), connMgr) + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + acc := &Account{ ID: "id", BalanceMap: map[string]Balances{ @@ -293,11 +274,38 @@ func TestDMSetAccount(t *testing.T) { AllowNegative: true, Disabled: false, } - - Cache = NewCacheS(cfg, dm, nil) + dm := NewDataManager(db, cfg.CacheCfg(), connMgr) + config.SetCgrConfig(cfg) if err := dm.SetAccount(acc); err != nil { t.Error(err) } } + +func TestDMSetReverseDestination(t *testing.T) { + Cache.Clear(nil) + cfg := config.NewDefaultCGRConfig() + + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + clientConn := make(chan rpcclient.ClientConnector, 1) + clientConn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.ReplicatorSv1SetReverseDestination: func(args, reply interface{}) error { + *reply.(*string) = "reply" + return nil + }, + }, + } + connMngr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicationConnsCfg): clientConn, + }) + + dm := NewDataManager(db, cfg.CacheCfg(), connMngr) + config.SetCgrConfig(cfg) + + if err := dm.SetReverseDestination("val", []string{"prf"}, utils.NonTransactional); err != nil { + t.Error(err) + } + +} diff --git a/engine/storage_test.go b/engine/storage_test.go index 4f6a8abca..2708f2695 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -18,6 +18,8 @@ along with this program. If not, see package engine import ( + "reflect" + "strings" "testing" "time" @@ -401,3 +403,72 @@ func TestIDBRemoveIndexesDrv(t *testing.T) { t.Error("group should be removed") } } + +func TestIDBGetDispatcherHostDrv(t *testing.T) { + idb := NewInternalDB(nil, nil, true, map[string]*config.ItemOpt{ + utils.CacheDispatcherHosts: { + Limit: 2, + Remote: true, + }, + }) + dsp := &DispatcherHost{ + Tenant: "cgrates.org", + } + tenant, acc := "cgrates", "acc1" + idb.db.Set(utils.CacheDispatcherHosts, utils.ConcatenatedKey(tenant, acc), dsp, []string{"id", "id3"}, true, utils.NonTransactional) + + if val, err := idb.GetDispatcherHostDrv(tenant, acc); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(dsp, val) { + t.Errorf("expected %+v,received %+v", utils.ToJSON(dsp), utils.ToJSON(val)) + } +} + +func TestIDBRemoveDispatcherHostDrv(t *testing.T) { + idb := NewInternalDB(nil, nil, true, map[string]*config.ItemOpt{ + utils.CacheDispatcherHosts: { + Limit: 2, + Remote: true, + }, + }) + dsp := &DispatcherHost{ + Tenant: "cgrates.org", + } + tenant, acc := "cgrates", "acc1" + idb.db.Set(utils.CacheDispatcherHosts, utils.ConcatenatedKey(tenant, acc), dsp, []string{"id", "id3"}, true, utils.NonTransactional) + + if err := idb.RemoveDispatcherHostDrv(tenant, acc); err != nil { + t.Error(err) + } + if _, has := idb.db.Get(utils.CacheDispatcherHosts, utils.ConcatenatedKey(tenant, acc)); has { + t.Error("should been removed") + } +} + +func TestIDBSetStatQueueDrvNil(t *testing.T) { + idb := NewInternalDB(nil, nil, true, map[string]*config.ItemOpt{ + utils.CacheStatQueues: { + Limit: 4, + StaticTTL: true, + }, + }) + ssq := &StoredStatQueue{ + Tenant: "cgrates", + ID: "id", + SQItems: []SQItem{ + { + EventID: "event1", + }, + { + EventID: "event2", + }, + }, + Compressed: false, + SQMetrics: map[string][]byte{ + strings.Join([]string{utils.MetaASR, "test"}, "#"): []byte("val"), + }, + } + if err := idb.SetStatQueueDrv(ssq, nil); err == nil { + t.Error(err) + } +} diff --git a/engine/tpreader_test.go b/engine/tpreader_test.go index 0757e397e..034fb6566 100644 --- a/engine/tpreader_test.go +++ b/engine/tpreader_test.go @@ -1193,3 +1193,90 @@ func TestTpReaderLoadAccountActions(t *testing.T) { t.Error(err) } } + +func TestTPCSVImporterChargerProfiles(t *testing.T) { + db := NewInternalDB(nil, nil, true, map[string]*config.ItemOpt{ + utils.CacheTBLTPChargers: { + Limit: 2, + }, + }) + tpCharger := &utils.TPChargerProfile{ + TPid: "chargetp", + Tenant: "cgrates", + ID: "id", + } + db.db.Set(utils.CacheTBLTPChargers, "tpid:itm1", tpCharger, []string{"grp"}, true, utils.NonTransactional) + tpImp := &TPCSVImporter{ + TPid: "tpid", + Verbose: false, + csvr: db, + StorDb: db, + } + if err := tpImp.importChargerProfiles("fn"); err != nil { + t.Error(err) + } + if val, has := db.db.Get(utils.CacheTBLTPChargers, utils.ConcatenatedKey(tpCharger.TPid, tpCharger.Tenant, tpCharger.ID)); !has { + t.Error("has no value") + } else if !reflect.DeepEqual(val, tpCharger) { + t.Errorf("expected %+v,received %+v", utils.ToJSON(tpCharger), utils.ToJSON(val)) + } +} + +func TestTPCSVImporterDispatcherProfiles(t *testing.T) { + + db := NewInternalDB(nil, nil, true, map[string]*config.ItemOpt{ + utils.CacheTBLTPDispatchers: { + Limit: 3, + }, + }) + dsP := &utils.TPDispatcherProfile{ + TPid: "disTP", + Tenant: "tnt", + ID: "id", + } + db.db.Set(utils.CacheTBLTPDispatchers, "tpid:dsp1", dsP, []string{"grp"}, true, utils.NonTransactional) + tpImp := &TPCSVImporter{ + TPid: "tpid", + Verbose: false, + csvr: db, + StorDb: db, + } + if err := tpImp.importDispatcherProfiles("fn"); err != nil { + t.Error(err) + } + if val, has := db.db.Get(utils.CacheTBLTPDispatchers, utils.ConcatenatedKey(dsP.TPid, dsP.Tenant, dsP.ID)); !has { + t.Error("has no value") + } else if !reflect.DeepEqual(val, dsP) { + t.Errorf("expected %+v,received %+v", utils.ToJSON(dsP), utils.ToJSON(val)) + } + +} + +func TestTPCSVImporterDispatcherHosts(t *testing.T) { + db := NewInternalDB(nil, nil, true, map[string]*config.ItemOpt{ + utils.CacheTBLTPDispatcherHosts: { + Limit: 3, + }, + }) + dsH := &utils.TPDispatcherHost{ + TPid: "dshTp", + Tenant: "host", + ID: "host_id", + } + db.db.Set(utils.CacheTBLTPDispatcherHosts, "tpid:dsp1", dsH, []string{"grp"}, true, utils.NonTransactional) + tpImp := &TPCSVImporter{ + TPid: "tpid", + Verbose: false, + csvr: db, + StorDb: db, + } + if err := tpImp.importDispatcherHosts("fn"); err != nil { + t.Error(err) + } + if val, has := db.db.Get(utils.CacheTBLTPDispatcherHosts, utils.ConcatenatedKey(dsH.TPid, dsH.Tenant, dsH.ID)); !has { + t.Error("has no value") + } else if !reflect.DeepEqual(val, dsH) { + t.Errorf("expected %+v,received %+v", utils.ToJSON(dsH), utils.ToJSON(val)) + } + +}