diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index a719b9251..dbf033e3f 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -18,8 +18,11 @@ along with this program. If not, see package engine import ( + "bytes" "log" + "os" "reflect" + "strings" "sync" "testing" "time" @@ -2612,3 +2615,27 @@ func TestCdAddRatingInfos(t *testing.T) { } } + +func TestCDRefundIncrementWarning(t *testing.T) { + utils.Logger.SetLogLevel(4) + utils.Logger.SetSyslog(nil) + buf := new(bytes.Buffer) + log.SetOutput(buf) + defer func() { + utils.Logger.SetLogLevel(0) + log.SetOutput(os.Stderr) + + }() + increments := Increments{ + &Increment{Cost: 2, BalanceInfo: &DebitInfo{ + Monetary: &MonetaryInfo{UUID: "moneya2"}, AccountID: "acc"}}, + } + cd := &CallDescriptor{ToR: utils.MetaVoice, Increments: increments} + expLog := `Could not get the account to be refunded` + if _, err := cd.refundIncrements(nil); err != nil { + t.Error(err) + } else if rcvLog := buf.String(); !strings.Contains(rcvLog, expLog) { + t.Errorf("Logger %v doesn't contain %v", rcvLog, expLog) + } + +} diff --git a/engine/datamanager_test.go b/engine/datamanager_test.go index 1b818f615..98d72b60a 100644 --- a/engine/datamanager_test.go +++ b/engine/datamanager_test.go @@ -3202,10 +3202,9 @@ func TestDMRatingProfile(t *testing.T) { // config.SetCgrConfig(config.NewDefaultCGRConfig()) // }() // Cache.Clear(nil) -// cfg := config.NewDefaultCGRConfig() -// dataDB := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) +// cfg := config.NewDefaultCGRConfig() +// dataDB := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) // dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) - // oldFlt := &Filter{ // Tenant: cfg.GeneralCfg().DefaultTenant, // ID: "DISPATCHER_FLTR1", @@ -3217,6 +3216,7 @@ func TestDMRatingProfile(t *testing.T) { // if err := dm.SetFilter(oldFlt, true); err != nil { // t.Error(err) // } + // disp := &DispatcherProfile{ // Tenant: cfg.GeneralCfg().DefaultTenant, // ID: "Dsp", @@ -3245,11 +3245,10 @@ func TestDMRatingProfile(t *testing.T) { // expindx := map[string]utils.StringSet{ // "*string:*req.Destination": {"Dsp": {}}, // } - -// if getindx, err := dm.GetIndexes(utils.CacheDispatcherFilterIndexes, cfg.GeneralCfg().DefaultTenant, utils.EmptyString, true, true); err != nil { +// if getidx, err := dm.GetIndexes(utils.CacheDispatcherFilterIndexes, cfg.GeneralCfg().DefaultTenant, utils.EmptyString, true, true); err != nil { // t.Error(err) -// } else if !reflect.DeepEqual(expindx, getindx) { -// t.Errorf("Expected %v, Received %v", utils.ToJSON(expindx), utils.ToJSON(getindx)) +// } else if !reflect.DeepEqual(expindx, getidx) { +// t.Errorf("Expected %v, Received %v", utils.ToJSON(expindx), utils.ToJSON(getidx)) // } // } @@ -3463,3 +3462,155 @@ func TestCacheDataFromDB(t *testing.T) { t.Error(err) } } + +func TestDMGetRouteProfile(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().Items = map[string]*config.ItemOpt{ + utils.CacheRouteProfiles: { + Limit: 3, + Remote: true, + APIKey: "key", + RouteID: "route", + Replicate: true, + }, + } + rpL := &RouteProfile{Tenant: "cgrates.org", + ID: "ROUTE_ACNT_1002", + FilterIDs: []string{"FLTR_ACNT_1002"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2017, 11, 27, 00, 00, 00, 00, time.UTC), + }, + Sorting: utils.MetaLC, + Routes: []*Route{ + { + ID: "route1", + RatingPlanIDs: []string{"RP_1002_LOW"}, + Weight: 10, + Blocker: false, + }, + }, + Weight: 10, + } + clientConn := make(chan rpcclient.ClientConnector, 1) + clientConn <- &ccMock{ + calls: map[string]func(args interface{}, reply interface{}) error{ + utils.ReplicatorSv1GetRouteProfile: func(args, reply interface{}) error { + *reply.(**RouteProfile) = 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) + if val, err := dm.GetRouteProfile(rpL.Tenant, rpL.ID, false, true, utils.NonTransactional); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(val, rpL) { + t.Errorf("expected %v,received %v", utils.ToJSON(rpL), utils.ToJSON(val)) + } +} + +func TestUpdateFilterIndexStatErr1(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) + cfg := config.NewDefaultCGRConfig() + dataDB := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) + dm.dataDB = &DataDBMock{ + GetIndexesDrvF: func(idxItmType, tntCtx, idxKey string) (indexes map[string]utils.StringSet, err error) { + return map[string]utils.StringSet{ + utils.CacheStatFilterIndexes: { + "ATTR_TEST": {}, + }, + }, nil + }, + } + + oldFlt := &Filter{ + Tenant: "cgrates.org", + ID: "fltr_test", + Rules: []*FilterRule{ + { + Type: utils.MetaString, + Element: "~*req.Cost", + Values: []string{"unRegVal2"}, + }, + }, + } + newFlt := &Filter{ + Tenant: "cgrates.org", + ID: "fltr_test", + Rules: []*FilterRule{ + { + Type: utils.MetaPrefix, + Element: "~*req.Usage", + Values: []string{"10s"}, + }, + }, + } + if err := UpdateFilterIndex(dm, oldFlt, newFlt); err != utils.ErrNotImplemented { + t.Errorf("Expected error <%v>, Received error <%v>", utils.ErrNotImplemented, err) + } +} + +func TestUpdateFilterIndexRemoveThresholdErr1(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) + cfg := config.NewDefaultCGRConfig() + dataDB := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) + dm.dataDB = &DataDBMock{ + GetIndexesDrvF: func(idxItmType, tntCtx, idxKey string) (indexes map[string]utils.StringSet, err error) { + return map[string]utils.StringSet{ + utils.CacheThresholdFilterIndexes: { + "ATTR_TEST": {}, + }, + }, nil + }, + } + + oldFlt := &Filter{ + Tenant: "cgrates.org", + ID: "fltr_test", + Rules: []*FilterRule{ + { + Type: utils.MetaString, + Element: "~*req.Cost", + Values: []string{"unRegVal2"}, + }, + }, + } + newFlt := &Filter{ + Tenant: "cgrates.org", + ID: "fltr_test", + Rules: []*FilterRule{ + { + Type: utils.MetaPrefix, + Element: "~*req.Usage", + Values: []string{"10s"}, + }, + }, + } + if err := UpdateFilterIndex(dm, oldFlt, newFlt); err != utils.ErrNotImplemented { + t.Errorf("Expected error <%v>, Received error <%v>", utils.ErrNotImplemented, err) + } +} diff --git a/engine/exportrequest_test.go b/engine/exportrequest_test.go index 767dd88ea..2e20a1f3b 100644 --- a/engine/exportrequest_test.go +++ b/engine/exportrequest_test.go @@ -617,3 +617,20 @@ func TestExportRequestFieldAsString(t *testing.T) { t.Error(err) } } +func TestExportRequestParseFieldErr(t *testing.T) { + mp := utils.NewSecureMapStorage() + inData := map[string]utils.DataStorage{ + utils.MetaReq: mp, + } + EventReq := NewExportRequest(inData, "", nil, nil) + fctTemp := &config.FCTemplate{ + Type: utils.MetaMaskedDestination, + Value: config.NewRSRParsersMustCompile("*daily", utils.InfieldSep), + Layout: "“Mon Jan _2 15:04:05 2006”", + Timezone: "Local", + } + if _, err := EventReq.ParseField(fctTemp); err == nil { + t.Error(err) + } + +} diff --git a/engine/filters_test.go b/engine/filters_test.go index 50f829bcb..eefc60811 100644 --- a/engine/filters_test.go +++ b/engine/filters_test.go @@ -2389,5 +2389,31 @@ func TestFilterPassRegexErr(t *testing.T) { if pass, err := rf.passRegex(cd); err != nil || pass { t.Error(err) } - +} +func TestFilterLazyPassErr(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + Cache.Clear(nil) + cfg := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, config.CgrConfig().CacheCfg(), nil) + filterS := FilterS{ + cfg: cfg, + dm: dm, + } + fltrID := "*string:~*req.Account:1007" + passEvent := map[string]interface{}{ + "Account": "1007", + } + dm.dataDB = &DataDBMock{} + fEv := utils.MapStorage{} + fEv.Set([]string{utils.MetaReq}, passEvent) + prefixes := []string{utils.DynamicDataPrefix + utils.MetaReq} + Cache.Set(utils.CacheFilters, utils.ConcatenatedKey("cgrates.org", fltrID), nil, []string{}, true, utils.NonTransactional) + if _, _, err := filterS.LazyPass("cgrates.org", + []string{fltrID}, fEv, prefixes); err == nil || err.Error() != utils.ErrPrefixNotFound(fltrID).Error() { + t.Errorf(err.Error()) + } } diff --git a/engine/responder_test.go b/engine/responder_test.go index 0b066dcff..997c54eff 100644 --- a/engine/responder_test.go +++ b/engine/responder_test.go @@ -29,6 +29,7 @@ import ( "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" + "github.com/cgrates/rpcclient" ) var rsponder = &Responder{MaxComputedUsage: config.CgrConfig().RalsCfg().MaxComputedUsage} @@ -1161,3 +1162,47 @@ func TestGetMaxSessionTimeOnAccountsErr(t *testing.T) { } } + +func TestResponderCall(t *testing.T) { + tmpConn := connMgr + tmp := Cache + defer func() { + Cache = tmp + connMgr = tmpConn + }() + Cache.Clear(nil) + cfg := config.NewDefaultCGRConfig() + cfg.CacheCfg().Partitions[utils.CacheRPCResponses].Limit = 1 + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + config.SetCgrConfig(cfg) + rs := &Responder{ + Timezone: "UTC", + FilterS: &FilterS{ + cfg: cfg, + dm: dm, + }, + MaxComputedUsage: map[string]time.Duration{}, + } + clientConn := make(chan rpcclient.ClientConnector, 1) + clientConn <- rs + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{}) + config.SetCgrConfig(cfg) + SetConnManager(connMgr) + var reply CallCost + attr := &CallDescriptorWithAPIOpts{ + CallDescriptor: &CallDescriptor{ + Category: "call", + Tenant: "cgrates.org", + Subject: "dan", + ToR: utils.MetaAny, + Account: "dan", + Destination: "+4917621621391", + DurationIndex: 9, + }, + } + if err := rs.Call(utils.ResponderGetCost, attr, &reply); err != nil { + t.Error(err) + } +} diff --git a/engine/routes_test.go b/engine/routes_test.go index 0bfa02022..0bddd6d15 100644 --- a/engine/routes_test.go +++ b/engine/routes_test.go @@ -19,6 +19,7 @@ package engine import ( "bytes" + "errors" "fmt" "log" "os" @@ -551,6 +552,10 @@ func TestRoutesSortedForEventWithLimitAndOffset(t *testing.T) { } func TestRoutesAsOptsGetRoutesMaxCost(t *testing.T) { + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + + }() cfg := config.NewDefaultCGRConfig() data := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) dmSPP := NewDataManager(data, config.CgrConfig().CacheCfg(), nil) @@ -596,6 +601,10 @@ func TestRoutesMatchWithIndexFalse(t *testing.T) { routeService := NewRouteService(dmSPP, &FilterS{ dm: dmSPP, cfg: cfg}, cfg, nil) prepareRoutesData(t, dmSPP) + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + + }() routeService.cgrcfg.RouteSCfg().IndexedSelects = false sprf, err := routeService.matchingRouteProfilesForEvent("cgrates.org", testRoutesArgs[0]) @@ -624,6 +633,10 @@ func TestRoutesMatchWithIndexFalse(t *testing.T) { } func TestRoutesSortedForEventWithLimitAndOffset2(t *testing.T) { + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + + }() Cache.Clear(nil) testRoutesPrfs := RouteProfiles{ &RouteProfile{ @@ -763,6 +776,10 @@ func TestRoutesSortedForEventWithLimitAndOffset2(t *testing.T) { } } func TestRouteProfileCompileCacheParameters(t *testing.T) { + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + + }() rp := &RouteProfile{ Tenant: "tnt", ID: "id2", @@ -836,8 +853,11 @@ func TestRouteProfileCompileCacheParameters(t *testing.T) { func TestRouteServiceStatMetrics(t *testing.T) { - testMock := &ccMock{ + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + }() + testMock := &ccMock{ calls: map[string]func(args, reply interface{}) error{ utils.StatSv1GetQueueFloatMetrics: func(args, reply interface{}) error { rpl := map[string]float64{ @@ -860,13 +880,55 @@ func TestRouteServiceStatMetrics(t *testing.T) { utils.ConcatenatedKey(utils.MetaInternal, utils.MetaStats): clientconn, }) rpS := NewRouteService(dmSPP, &FilterS{dm: dmSPP, cfg: cfg, connMgr: nil}, cfg, connMgr) - if _, err := rpS.statMetrics([]string{"stat1", "stat2"}, "cgrates.org"); err != nil { + exp := map[string]float64{ + "metric1": 21.11, + } + + if val, err := rpS.statMetrics([]string{"stat1", "stat2"}, "cgrates.org"); err != nil { t.Error(err) + } else if !reflect.DeepEqual(val, exp) { + t.Errorf("Expected %v,Received %v", utils.ToJSON(exp), utils.ToJSON(val)) + } +} +func TestRouteServiceStatMetricsLog(t *testing.T) { + utils.Logger.SetLogLevel(4) + utils.Logger.SetSyslog(nil) + buf := new(bytes.Buffer) + log.SetOutput(buf) + defer func() { + utils.Logger.SetLogLevel(0) + log.SetOutput(os.Stderr) + }() + testMock := &ccMock{ + calls: map[string]func(args, reply interface{}) error{ + utils.StatSv1GetQueueFloatMetrics: func(args, reply interface{}) error { + return errors.New("Error") + }, + }, + } + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dmSPP := NewDataManager(data, config.CgrConfig().CacheCfg(), nil) + cfg.RouteSCfg().StatSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaStats)} + clientconn := make(chan rpcclient.ClientConnector, 1) + clientconn <- testMock + connMgr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.MetaStats): clientconn, + }) + rpS := NewRouteService(dmSPP, &FilterS{dm: dmSPP, cfg: cfg, connMgr: nil}, cfg, connMgr) + expLog := `getting statMetrics for stat` + if _, err := rpS.statMetrics([]string{"stat1", "stat2"}, "cgrates.org"); err != nil { + t.Error(err) + } else if rcvLog := buf.String(); strings.Contains(rcvLog, expLog) { + t.Errorf("Logger %v doesn't contain %v", rcvLog, expLog) } } - func TestRouteServiceV1GetRouteProfilesForEvent(t *testing.T) { cfg := config.NewDefaultCGRConfig() + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + + }() data := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) dmSPP := NewDataManager(data, config.CgrConfig().CacheCfg(), nil) cfg.RouteSCfg().StringIndexedFields = nil @@ -952,6 +1014,10 @@ func TestRouteServiceV1GetRouteProfilesForEvent(t *testing.T) { } func TestRouteServiceV1GetRoutes(t *testing.T) { + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + + }() ccMock := &ccMock{ calls: map[string]func(args interface{}, reply interface{}) error{ utils.AttributeSv1ProcessEvent: func(args, reply interface{}) error { @@ -1035,6 +1101,10 @@ func TestRouteServiceV1GetRoutes(t *testing.T) { } func TestRouteServiceSortRoutes(t *testing.T) { + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + + }() cfg := config.NewDefaultCGRConfig() data := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) dmSPP := NewDataManager(data, config.CgrConfig().CacheCfg(), nil) @@ -1120,6 +1190,8 @@ func TestRDSRSortRoutes(t *testing.T) { defer func() { utils.Logger.SetLogLevel(0) log.SetOutput(os.Stderr) + config.SetCgrConfig(config.NewDefaultCGRConfig()) + }() cfg := config.NewDefaultCGRConfig() data := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) @@ -1190,6 +1262,10 @@ func TestRDSRSortRoutes(t *testing.T) { } func TestQosRSortRoutes(t *testing.T) { + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + + }() cfg := config.NewDefaultCGRConfig() data := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) dmSPP := NewDataManager(data, config.CgrConfig().CacheCfg(), nil) @@ -1291,6 +1367,10 @@ func TestQosRSortRoutes(t *testing.T) { } } func TestReaSortRoutes(t *testing.T) { + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + + }() cfg := config.NewDefaultCGRConfig() db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) dm := NewDataManager(db, config.CgrConfig().CacheCfg(), nil) @@ -1379,6 +1459,10 @@ func TestReaSortRoutes(t *testing.T) { } func TestHCRSortRoutes(t *testing.T) { + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + + }() Cache.Clear(nil) cfg := config.NewDefaultCGRConfig() data := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) @@ -1568,6 +1652,10 @@ func TestLoadDistributionSorterSortRoutes(t *testing.T) { } func TestRouteServicePopulateSortingData(t *testing.T) { + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + + }() Cache.Clear(nil) ccMock := &ccMock{ @@ -1688,6 +1776,10 @@ func TestRouteServicePopulateSortingData(t *testing.T) { } func TestNewOptsGetRoutes(t *testing.T) { + defer func() { + config.SetCgrConfig(config.NewDefaultCGRConfig()) + + }() cfg := config.NewDefaultCGRConfig() db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) dm := NewDataManager(db, cfg.CacheCfg(), nil) diff --git a/engine/stats_test.go b/engine/stats_test.go index 80c8159bc..272e84e2c 100644 --- a/engine/stats_test.go +++ b/engine/stats_test.go @@ -3149,3 +3149,231 @@ func TestStatQueueStoreStatQueueCacheSetErr(t *testing.T) { utils.Logger.SetLogLevel(0) } + +func TestStatQueueV1GetStatQueuesForSliceOptsErr(t *testing.T) { + tmpC := config.CgrConfig() + defer func() { + config.SetCgrConfig(tmpC) + }() + cfg := config.NewDefaultCGRConfig() + cfg.StatSCfg().Opts.ProfileIDs = []string{utils.OptsStatsProfileIDs} + data := NewInternalDB(nil, nil, true, config.CgrConfig().DataDbCfg().Items) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + Cache.Clear(nil) + filterS := NewFilterS(cfg, nil, dm) + sS := NewStatService(dm, cfg, filterS, nil) + sqPrf1 := &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "SQ1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + ActivationInterval: &utils.ActivationInterval{ + ExpiryTime: time.Date(2021, 6, 1, 12, 0, 0, 0, time.UTC), + }, + Weight: 10, + Blocker: true, + QueueLength: 10, + ThresholdIDs: []string{"*none"}, + MinItems: 5, + Metrics: []*MetricWithFilters{ + { + MetricID: utils.MetaTCD, + }, + }, + } + if err := dm.SetStatQueueProfile(sqPrf1, true); err != nil { + t.Error(err) + } + sqPrf2 := &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "SQ2", + ActivationInterval: &utils.ActivationInterval{ + ExpiryTime: time.Date(2021, 6, 1, 12, 0, 0, 0, time.UTC), + }, + Weight: 20, + Blocker: false, + QueueLength: 10, + ThresholdIDs: []string{"*none"}, + MinItems: 5, + Metrics: []*MetricWithFilters{ + { + MetricID: utils.MetaACD, + }, + }, + } + if err := dm.SetStatQueueProfile(sqPrf2, true); err != nil { + t.Error(err) + } + args := &utils.CGREvent{ + ID: "TestGetStatQueuesForEvent", + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + APIOpts: map[string]interface{}{ + utils.OptsStatsProfileIDs: "SQ1", + }, + } + var reply []string + if err := sS.V1GetStatQueuesForEvent(args, &reply); err == nil { + t.Error(err) + } +} +func TestStatQueueV1GetStatQueuesForEventBoolOptsErr(t *testing.T) { + tmpC := config.CgrConfig() + defer func() { + config.SetCgrConfig(tmpC) + }() + + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, true, config.CgrConfig().DataDbCfg().Items) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + Cache.Clear(nil) + filterS := NewFilterS(cfg, nil, dm) + sS := NewStatService(dm, cfg, filterS, nil) + + sqPrf1 := &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "SQ1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + ActivationInterval: &utils.ActivationInterval{ + ExpiryTime: time.Date(2021, 6, 1, 12, 0, 0, 0, time.UTC), + }, + Weight: 10, + Blocker: true, + QueueLength: 10, + ThresholdIDs: []string{"*none"}, + MinItems: 5, + Metrics: []*MetricWithFilters{ + { + MetricID: utils.MetaTCD, + }, + }, + } + if err := dm.SetStatQueueProfile(sqPrf1, true); err != nil { + t.Error(err) + } + sqPrf2 := &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "SQ2", + ActivationInterval: &utils.ActivationInterval{ + ExpiryTime: time.Date(2021, 6, 1, 12, 0, 0, 0, time.UTC), + }, + Weight: 20, + Blocker: false, + QueueLength: 10, + ThresholdIDs: []string{"*none"}, + MinItems: 5, + Metrics: []*MetricWithFilters{ + { + MetricID: utils.MetaACD, + }, + }, + } + if err := dm.SetStatQueueProfile(sqPrf2, true); err != nil { + t.Error(err) + } + args := &utils.CGREvent{ + ID: "TestGetStatQueuesForEvent", + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + APIOpts: map[string]interface{}{ + utils.OptsStatsProfileIgnoreFilters: "test", + }, + } + var reply []string + if err := sS.V1GetStatQueuesForEvent(args, &reply); err == nil { + t.Error(err) + } +} + +func TestMatchingStatQueuesForEventErr(t *testing.T) { + tmpC := config.CgrConfig() + defer func() { + config.SetCgrConfig(tmpC) + }() + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, true, config.CgrConfig().DataDbCfg().Items) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + Cache.Clear(nil) + filterS := NewFilterS(cfg, nil, dm) + sS := NewStatService(dm, cfg, filterS, nil) + + args := &utils.CGREvent{ + ID: "TestGetStatQueuesForEvent", + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + } + evNm := utils.MapStorage{ + utils.MetaReq: args.Event, + utils.MetaOpts: args.APIOpts, + } + if _, err := sS.matchingStatQueuesForEvent("cgrates.org", []string{"SQ1", "SQ2"}, args.Time, evNm, true); err == nil || err != utils.ErrNotFound { + t.Error(err) + } +} + +func TestStatQueueProcessEventErr(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, true, config.CgrConfig().DataDbCfg().Items) + dm := NewDataManager(data, cfg.CacheCfg(), nil) + filterS := NewFilterS(cfg, nil, dm) + sS := NewStatService(dm, cfg, filterS, nil) + + sqPrf := &StatQueueProfile{ + Tenant: "cgrates.org", + ID: "SQ1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + ActivationInterval: &utils.ActivationInterval{ + ExpiryTime: time.Date(2021, 6, 1, 12, 0, 0, 0, time.UTC), + }, + Weight: 10, + Blocker: true, + QueueLength: 10, + ThresholdIDs: []string{"*none"}, + MinItems: 5, + Metrics: []*MetricWithFilters{ + { + MetricID: utils.MetaTCD, + }, + }, + } + sq := &StatQueue{ + sqPrfl: sqPrf, + Tenant: "cgrates.org", + ID: "SQ1", + SQItems: []SQItem{ + { + EventID: "SqProcessEvent", + }, + }, + SQMetrics: make(map[string]StatMetric), + } + + if err := dm.SetStatQueueProfile(sqPrf, true); err != nil { + t.Error(err) + } + if err := dm.SetStatQueue(sq); err != nil { + t.Error(err) + } + + args := &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "SqProcessEvent", + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + APIOpts: map[string]interface{}{ + utils.OptsStatsProfileIDs: "SQ1", + }, + } + if _, err := sS.processEvent(args.Tenant, args); err == nil { + t.Error(err) + } + args.APIOpts[utils.OptsStatsProfileIDs] = []string{"SQ1"} + args.APIOpts[utils.OptsStatsProfileIgnoreFilters] = "test" + if _, err := sS.processEvent(args.Tenant, args); err == nil { + t.Error(err) + } + +}