diff --git a/engine/attributes_test.go b/engine/attributes_test.go index 8c9bf07ad..f0b5bb045 100644 --- a/engine/attributes_test.go +++ b/engine/attributes_test.go @@ -2172,3 +2172,48 @@ func TestAttributeGetForEvent(t *testing.T) { t.Error(err) } } + +func TestAttrSGetAttributeForEventErrs(t *testing.T) { + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + attrService, err := NewAttributeService(dm, NewFilterS(cfg, nil, dm), cfg) + if err != nil { + t.Error(err) + } + testCases := []struct { + name string + args *AttrArgsProcessEvent + attrPrfl *AttributeProfile + }{ + { + name: "Missing CgrEvent", + args: &AttrArgsProcessEvent{}, + }, + { + name: "AttributeProfile not found", + args: &AttrArgsProcessEvent{ + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "CGREvent1", + Event: map[string]interface{}{ + utils.Account: "1002", + utils.Subject: "1002", + utils.Destination: "1001", + utils.SetupTime: time.Date(2022, 12, 1, 14, 25, 0, 0, time.UTC), + utils.Usage: "1m20s", + }, + }, + }, + attrPrfl: new(AttributeProfile), + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := attrService.V1GetAttributeForEvent(tc.args, tc.attrPrfl) + if err == nil { + t.Error("expected error") + } + }) + } +} diff --git a/engine/cdrs_test.go b/engine/cdrs_test.go index bf7f1b7e1..d5e8f6726 100644 --- a/engine/cdrs_test.go +++ b/engine/cdrs_test.go @@ -607,76 +607,75 @@ func TestCDRSThDSProcessEvent(t *testing.T) { // } -// func TestCRDSRefundEventCost(t *testing.T) { -// cfg, _ := config.NewDefaultCGRConfig() -// db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) -// dm := NewDataManager(db, cfg.CacheCfg(), nil) -// cfg.CdrsCfg().RaterConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaRALs)} -// cdrS := &CDRServer{cgrCfg: cfg, dm: dm, cdrDb: db} +func TestCRDSRefundEventCost(t *testing.T) { + cfg, _ := config.NewDefaultCGRConfig() + Cache.Clear(nil) + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + cfg.CdrsCfg().RaterConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaRALs)} + clientConn := make(chan birpc.ClientConnector, 1) + clientConn <- clMock(func(ctx *context.Context, serviceMethod string, _, _ interface{}) error { -// clientConn := make(chan birpc.ClientConnector, 1) -// clientConn <- clMock(func(serviceMethod string, _, _ interface{}) error { + if serviceMethod == utils.ResponderRefundIncrements { -// if serviceMethod == utils.ResponderRefundIncrements { + return nil + } + return utils.ErrNotImplemented + }) + cdrS := &CDRServer{cgrCfg: cfg, dm: dm, cdrDb: db, connMgr: NewConnManager(cfg, map[string]chan birpc.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.MetaRALs): clientConn, + })} + ec := &EventCost{ + Cost: utils.Float64Pointer(10), + Accounting: map[string]*BalanceCharge{ + "3463957": { + Units: 0.002623, + RatingID: "", + AccountID: "cgrates.org:1001", + ExtraChargeID: "", + }, + "fee8a3a": { + Units: 0.0787, + RatingID: "", + AccountID: "cgrates.org:1001", + ExtraChargeID: "", + }, + }, + AccountSummary: &AccountSummary{ + Tenant: "cgrates.org", + ID: "1001", -// return nil -// } -// return utils.ErrNotImplemented -// }) + BalanceSummaries: []*BalanceSummary{ + {ID: "voice2", Type: utils.VOICE, Value: 10, Disabled: false}, + }, + AllowNegative: true, + Disabled: false, + }, + Charges: []*ChargingInterval{ + { + RatingID: "c1a5ab9", + Increments: []*ChargingIncrement{ + { + Usage: time.Duration(0), + Cost: 0.1, + AccountingID: "3463957", + CompressFactor: 1, + }, + { + Usage: time.Duration(1 * time.Second), + Cost: 0, + AccountingID: "fee8a3a", + CompressFactor: 10, + }, + }, + CompressFactor: 1, + }, + }} -// ec := &EventCost{ -// Cost: utils.Float64Pointer(10), -// Accounting: map[string]*BalanceCharge{ -// "3463957": &BalanceCharge{ -// Units: 0.002623, -// RatingID: "", -// AccountID: "cgrates.org:1001", -// BalanceUUID: "154419f2-45e0-4629-a203-06034ccb493f", -// ExtraChargeID: "", -// }, -// "fee8a3a": &BalanceCharge{ -// Units: 0.0787, -// RatingID: "", -// AccountID: "cgrates.org:1001", -// BalanceUUID: "154419f2-45e0-4629-a203-06034ccb493f", -// ExtraChargeID: "", -// }, -// }, -// AccountSummary: &AccountSummary{ -// Tenant: "cgrates.org", -// ID: "1001", - -// BalanceSummaries: []*BalanceSummary{ -// {ID: "voice2", Type: utils.VOICE, Value: 10, Disabled: false}, -// }, -// AllowNegative: true, -// Disabled: false, -// }, -// Charges: []*ChargingInterval{ -// { -// RatingID: "c1a5ab9", -// Increments: []*ChargingIncrement{ -// { -// Usage: time.Duration(0), -// Cost: 0.1, -// AccountingID: "3463957", -// CompressFactor: 1, -// }, -// { -// Usage: time.Duration(1 * time.Second), -// Cost: 0, -// AccountingID: "fee8a3a", -// CompressFactor: 10, -// }, -// }, -// CompressFactor: 1, -// }, -// }} - -// if err := cdrS.refundEventCost(ec, "*prepaid", "*monetary"); err != nil { -// t.Error(err) -// } -// } + if _, err := cdrS.refundEventCost(ec, "*prepaid", "*monetary"); err != nil { + t.Error(err) + } +} func TestCDRSV1StoreSessionCostCache(t *testing.T) { cfg, _ := config.NewDefaultCGRConfig() @@ -838,7 +837,7 @@ func TestRemoveThresholdProfileRpl(t *testing.T) { cfg.DataDbCfg().Items[utils.MetaThresholdProfiles].Replicate = true cfg.DataDbCfg().RplConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)} clientConn := make(chan birpc.ClientConnector, 1) - clientConn <- clMock(func(ctx *context.Context,servicemethod string, _, _ interface{}) error { + clientConn <- clMock(func(ctx *context.Context, servicemethod string, _, _ interface{}) error { if servicemethod == utils.ReplicatorSv1RemoveThresholdProfile { return nil @@ -911,5 +910,42 @@ func TestCDRSV1ProcessCDRCache(t *testing.T) { if err := cdrS.V1ProcessCDR(cdr, &reply); err != nil { t.Error(err) } - +} + +func TestCDRSGetCostFromRater(t *testing.T) { + cfg, _ := config.NewDefaultCGRConfig() + cfg.CdrsCfg().RaterConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaResponder)} + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + clientConn := make(chan birpc.ClientConnector, 1) + clientConn <- clMock(func(ctx *context.Context, servicemethod string, _, reply interface{}) error { + if servicemethod == utils.ResponderGetCost { + return nil + } + return utils.ErrNotImplemented + }) + cdrS := &CDRServer{ + cgrCfg: cfg, + dm: dm, + cdrDb: db, + connMgr: NewConnManager(cfg, map[string]chan birpc.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.MetaResponder): clientConn, + }), + } + cdr := &CDRWithArgDispatcher{ + CDR: &CDR{ + Tenant: "cgrates.org", + RequestType: "default", + Account: "1001", + Subject: "1001", + Destination: "1002", + ToR: "voice", + SetupTime: time.Now(), + AnswerTime: time.Now().Add(time.Duration(2) * time.Second), + Usage: time.Duration(2) * time.Minute, + }, + } + if _, err := cdrS.getCostFromRater(cdr); err != nil { + t.Error(err) + } } diff --git a/engine/datamanager_test.go b/engine/datamanager_test.go index cba1498d4..c67bac2e6 100644 --- a/engine/datamanager_test.go +++ b/engine/datamanager_test.go @@ -1978,3 +1978,170 @@ func TestDataManagerRemoveChargerProfile(t *testing.T) { }) } } + +func TestDMGetFilter(t *testing.T) { + cfg, _ := config.NewDefaultCGRConfig() + defer func() { + cfg2, _ := config.NewDefaultCGRConfig() + config.SetCgrConfig(cfg2) + }() + cfg.DataDbCfg().RmtConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1)} + cfg.DataDbCfg().Items[utils.MetaFilters].Remote = true + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + clientConn := make(chan birpc.ClientConnector, 1) + clientConn <- clMock(func(_ *context.Context, serviceMethod string, _, reply interface{}) error { + if serviceMethod == utils.ReplicatorSv1GetFilter { + flt := &Filter{ + Tenant: "cgrates.org", + ID: "FLTR_2", + Rules: []*FilterRule{ + { + Type: "*prefix", + Element: utils.DynamicDataPrefix + utils.MetaReq + utils.NestingSep + utils.Destination, + Values: []string{"10", "20"}, + }, + }, + } + *reply.(**Filter) = flt + return nil + } + return utils.ErrNotFound + }) + dm := NewDataManager(db, cfg.CacheCfg(), NewConnManager(cfg, map[string]chan context.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.ReplicatorSv1): clientConn, + })) + Cache.Clear(nil) + testCases := []struct { + name string + dm *DataManager + tenant string + id string + cacheRead bool + cachWrite bool + transactionID string + expectedErr bool + expected *Filter + }{ + { + name: "GetFilter - Stored In Cache", + dm: dm, + tenant: "cgrates.org", + cacheRead: true, + id: "FLT_1", + cachWrite: false, + transactionID: utils.NonTransactional, + expectedErr: false, + expected: &Filter{ + Tenant: "cgrates.org", + ID: "FLT_1", + Rules: []*FilterRule{ + { + Type: "*string", + Element: "~*req.Destination", + Values: []string{"1002"}, + }, + }, + }, + }, + { + name: "GetFilter - Nil DataManager", + dm: nil, + cacheRead: false, + cachWrite: false, + transactionID: utils.NonTransactional, + + tenant: "cgrates.org", + expectedErr: true, + }, + { + name: "GetFilter - Inline Filter Error", + dm: dm, + tenant: "cgrates.org", + id: "*stringAccount:Error", + expectedErr: true, + }, + { + name: "GetFilter - Remote", + dm: dm, + tenant: "cgrates.org", + id: "FLT_2", + expectedErr: false, + cacheRead: false, + cachWrite: true, + transactionID: utils.NonTransactional, + }, + } + Cache.Set(utils.CacheFilters, utils.ConcatenatedKey("cgrates.org", "FLT_1"), &Filter{ + Tenant: "cgrates.org", + ID: "FLT_1", + Rules: []*FilterRule{ + { + Type: "*string", + Element: "~*req.Destination", + Values: []string{"1002"}, + }, + }, + }, []string{}, true, "") + config.SetCgrConfig(cfg) + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + val, err := GetFilter(tc.dm, tc.tenant, tc.id, tc.cacheRead, tc.cachWrite, tc.transactionID) + if (err != nil) != tc.expectedErr { + t.Errorf("Expected error: %v, received error: %v", tc.expectedErr, err) + } else if tc.expected != nil { + if !reflect.DeepEqual(val, tc.expected) { + t.Errorf("Expected: %v, received: %v", tc.expected, val) + } + } + }) + } +} + +func TestDmRemoveThresholdProfileErrs(t *testing.T) { + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + testCases := []struct { + name string + tenant string + id string + transactionID string + withIndex bool + }{ + { + name: "RemoveThresholdProfile - Profile not Found", + tenant: "cgrates.org", + id: "THP_1", + transactionID: "", + }, + { + name: "RemoveThresholdProfile - Broken Filter", + tenant: "cgrates.org", + id: "THP_2", + transactionID: "", + withIndex: true, + }, + } + dm.SetThresholdProfile(&ThresholdProfile{ + Tenant: "cgrates.org", + ID: "THP_2", + FilterIDs: []string{"FLT2"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2024, 7, 14, 14, 35, 0, 0, time.UTC), + ExpiryTime: time.Date(2024, 7, 14, 14, 35, 0, 0, time.UTC), + }, + MaxHits: -1, + MinSleep: time.Duration(5 * time.Minute), + Blocker: false, + Weight: 20.0, + }, true) + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := dm.RemoveThresholdProfile(tc.tenant, tc.id, tc.transactionID, tc.withIndex) + if err == nil { + t.Error(err) + } + }) + } +} diff --git a/engine/storage_test.go b/engine/storage_test.go index 7d444262f..9eab344ca 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -1030,6 +1030,7 @@ func TestTprReloadCache(t *testing.T) { cfg, _ := config.NewDefaultCGRConfig() dataDb := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) storDb := NewInternalDB(nil, nil, false, cfg.StorDbCfg().Items) + Cache.Clear(nil) tmpConn := connMgr defer func() { SetConnManager(tmpConn) @@ -1103,7 +1104,7 @@ func TestTprReloadCache(t *testing.T) { if err := tpr.LoadFilters(); err != nil { t.Error(err) } - if err := tpr.ReloadCache(utils.MetaLoad, false, nil, "cgrates.org"); err == nil { + if err := tpr.ReloadCache(utils.MetaLoad, false, nil, "cgrates.org"); err != nil { t.Error(err) } }