From 51af0635b06338c1934c765646aa2465bde5e04d Mon Sep 17 00:00:00 2001 From: gezimbll Date: Fri, 10 Mar 2023 10:57:25 -0500 Subject: [PATCH] Improving coverage test at engine --- engine/attributes_test.go | 57 +++++++++++ engine/resources_test.go | 153 ++++++++++++++++++++++++++++++ engine/responder_test.go | 193 ++++++++++++++++++++++++++++++++++++++ engine/stats_test.go | 7 +- 4 files changed, 409 insertions(+), 1 deletion(-) diff --git a/engine/attributes_test.go b/engine/attributes_test.go index 0a426a883..8c9bf07ad 100644 --- a/engine/attributes_test.go +++ b/engine/attributes_test.go @@ -2059,6 +2059,7 @@ func TestAttributesProcessEventSIPCIDWrongPathErr(t *testing.T) { } func TestAttributesProcessEventSIPCIDInvalidArgs(t *testing.T) { + Cache.Clear(nil) defaultCfg, _ := config.NewDefaultCGRConfig() data := NewInternalDB(nil, nil, true, defaultCfg.DataDbCfg().Items) dmAtr = NewDataManager(data, config.CgrConfig().CacheCfg(), nil) @@ -2115,3 +2116,59 @@ func TestAttributesProcessEventSIPCIDInvalidArgs(t *testing.T) { t.Errorf("expected: <%+v>, \n received: <%+v>", experr, err) } } + +func TestAttributeGetForEvent(t *testing.T) { + defaultCfg, _ := config.NewDefaultCGRConfig() + data := NewInternalDB(nil, nil, true, defaultCfg.DataDbCfg().Items) + dmAtr = NewDataManager(data, config.CgrConfig().CacheCfg(), nil) + attrService, err = NewAttributeService(dmAtr, NewFilterS(defaultCfg, nil, dmAtr), defaultCfg) + if err != nil { + t.Error(err) + } + + args := &AttrArgsProcessEvent{ + Context: utils.StringPointer("*sessions"), + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "testAttributeSGetAttributeForEvent", + Event: map[string]interface{}{ + utils.EVENT_NAME: "Internal", + utils.Account: "1003", + }, + }, + ArgDispatcher: &utils.ArgDispatcher{ + APIKey: utils.StringPointer("attr12345"), + }, + } + fltr := &Filter{ + Tenant: "cgrates.org", + ID: "ATTR_FLTR", + Rules: []*FilterRule{ + { + Type: utils.MetaString, + Element: "~*req.Account", + Values: []string{"1003"}, + }, + }, + } + dmAtr.SetFilter(fltr) + attr := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "attributeprofile1", + Contexts: []string{utils.MetaSessionS}, + FilterIDs: []string{"ATTR_FLTR"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2024, 7, 14, 14, 25, 0, 0, time.UTC), + ExpiryTime: time.Date(2025, 7, 14, 14, 25, 0, 0, time.UTC), + }, + + Weight: 20, + } + if err := dmAtr.SetAttributeProfile(attr, true); err != nil { + t.Error(err) + } + var reply AttributeProfile + if err := attrService.V1GetAttributeForEvent(args, &reply); err != nil { + t.Error(err) + } +} diff --git a/engine/resources_test.go b/engine/resources_test.go index d66d6eaea..487f90749 100644 --- a/engine/resources_test.go +++ b/engine/resources_test.go @@ -1197,3 +1197,156 @@ func TestResourceMatchingResourcesForEventLocksActivationInterval(t *testing.T) t.Fatalf("Expected resource to not be locked %q", rPrf.ID) } } + +func TestResourceForEvent(t *testing.T) { + Cache.Clear(nil) + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + rS, _ := NewResourceService(dm, cfg, + NewFilterS(cfg, nil, dm), nil) + + args := utils.ArgRSv1ResourceUsage{ + UsageID: "651a8db2-4f67-4cf8-b622-169e8a482e51", + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: utils.UUIDSha1Prefix(), + Event: map[string]interface{}{ + "Resource": "Resource1", + "Account": "1002", + "Subject": "1001", + "Destination": "1002"}, + }, + Units: 1, + ArgDispatcher: &utils.ArgDispatcher{ + APIKey: utils.StringPointer("res12345"), + }, + } + fltr := &Filter{ + Tenant: "cgrates.org", + ID: "RS_FLT", + Rules: []*FilterRule{ + { + Type: utils.MetaString, + Element: "~*req.Destination", + Values: []string{"1002", "1003"}, + }, + }, + } + dm.SetFilter(fltr) + fltr2 := &Filter{ + Tenant: "cgrates.org", + ID: "RS_FLT2", + Rules: []*FilterRule{ + { + Type: utils.MetaString, + Element: "~*req.Resource", + Values: []string{"Resource1"}, + }, + }, + } + dm.SetFilter(fltr2) + var reply Resources + + rsP := &ResourceProfile{ + Tenant: "cgrates.org", + ID: "RES20", + FilterIDs: []string{"RS_FLT"}, + UsageTTL: 10 * time.Second, + Limit: 10.00, + AllocationMessage: "AllocationMessage", + Weight: 20.00, + ThresholdIDs: []string{utils.META_NONE}, + } + + dm.SetResourceProfile(rsP, true) + dm.SetResource(&Resource{Tenant: "cgrates.org", + ID: rsP.ID}) + + rsP2 := &ResourceProfile{ + Tenant: "cgrates.org", + ID: "RES21", + UsageTTL: 10 * time.Second, + Limit: 10.00, + FilterIDs: []string{"RS_FLT2"}, + AllocationMessage: "AllocationMessage", + Weight: 20.00, + ThresholdIDs: []string{utils.META_NONE}, + ActivationInterval: &utils.ActivationInterval{ + ExpiryTime: time.Now().Add(-5 * time.Second), + }, + } + dm.SetResourceProfile(rsP2, true) + dm.SetResource(&Resource{Tenant: "cgrates.org", + ID: rsP2.ID}) + + if err := rS.V1ResourcesForEvent(args, &reply); err != nil { + t.Error(err) + } +} + +func TestResourcesRelease(t *testing.T) { + Cache.Clear(nil) + cfg, _ := config.NewDefaultCGRConfig() + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + rS, _ := NewResourceService(dm, cfg, + NewFilterS(cfg, nil, dm), nil) + + args := utils.ArgRSv1ResourceUsage{ + UsageID: "651a8db2-4f67-4cf8-b622-169e8a482e51", + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: utils.UUIDSha1Prefix(), + Event: map[string]interface{}{ + "Resource": "Resource1", + "Account": "1002", + "Subject": "1001", + "Destination": "1002"}, + }, + Units: 1, + ArgDispatcher: &utils.ArgDispatcher{ + APIKey: utils.StringPointer("res12345"), + }, + } + fltr := &Filter{ + Tenant: "cgrates.org", + ID: "RS_FLT", + Rules: []*FilterRule{ + { + Type: utils.MetaString, + Element: "~*req.Destination", + Values: []string{"1002", "1003"}, + }, + }, + } + dm.SetFilter(fltr) + rsP := &ResourceProfile{ + Tenant: "cgrates.org", + ID: "RES20", + FilterIDs: []string{"RS_FLT"}, + UsageTTL: 10 * time.Second, + Limit: 10.00, + AllocationMessage: "AllocationMessage", + Weight: 20.00, + ThresholdIDs: []string{utils.META_NONE}, + } + dm.SetResourceProfile(rsP, true) + dm.SetResource(&Resource{ + Tenant: "cgrates.org", + ID: rsP.ID, + Usages: map[string]*ResourceUsage{ + "651a8db2-4f67-4cf8-b622-169e8a482e51": { + Tenant: "cgrates.org", + ID: "651a8db2-4f67-4cf8-b622-169e8a482e21", + Units: 2, + }, + }, + }) + var reply string + if err := rS.V1ReleaseResource(args, &reply); err != nil { + t.Error(err) + } else if reply != utils.OK { + t.Errorf("Expected OK,Received %v", reply) + } +} diff --git a/engine/responder_test.go b/engine/responder_test.go index d59d76303..20c2e3f2d 100644 --- a/engine/responder_test.go +++ b/engine/responder_test.go @@ -474,3 +474,196 @@ func TestResponderGetMaxSessionTimeMaxUsageVOICE(t *testing.T) { t.Errorf("Expected %+v, received : %+v", utils.ErrMaxUsageExceeded, err) } } + +func TestResponderGetCostOnRatingPlans(t *testing.T) { + tmpCache := Cache + defer func() { + Cache = tmpCache + }() + arg := &utils.GetCostOnRatingPlansArgs{ + Tenant: "cgrates.org", + Account: "test", + Subject: "1001", + Destination: "*any", + SetupTime: time.Date(2023, 3, 10, 8, 0, 0, 0, time.UTC), + Usage: 20 * time.Second, + RatingPlanIDs: []string{"RPL1"}, + } + rp := &RatingPlan{ + Id: "RPL1", + Timings: map[string]*RITiming{ + "59a981b9": { + Years: utils.Years{}, + Months: utils.Months{}, + MonthDays: utils.MonthDays{}, + WeekDays: utils.WeekDays{1, 2, 3, 4, 5}, + StartTime: "00:00:00", + }, + }, + Ratings: map[string]*RIRate{ + "ebefae11": { + ConnectFee: 0, + Rates: []*Rate{ + { + GroupIntervalStart: 0, + Value: 0.2, + RateIncrement: 2 * time.Second, + RateUnit: time.Second, + }, + }, + RoundingMethod: utils.ROUNDING_MIDDLE, + RoundingDecimals: 1, + }, + }, + DestinationRates: map[string]RPRateList{ + "*any": []*RPRate{ + { + Timing: "59a981b9", + Rating: "ebefae11", + Weight: 10, + }, + }, + }, + } + Cache.Set(utils.CacheRatingPlans, "RPL1", rp, []string{}, true, utils.NonTransactional) + var reply map[string]interface{} + exp := map[string]interface{}{ + utils.Cost: 4.0, + utils.RatingPlanID: "RPL1", + } + if err := rsponder.GetCostOnRatingPlans(arg, &reply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(exp, reply) { + t.Errorf("Expected %v,Received %v", utils.ToJSON(exp), utils.ToJSON(reply)) + } +} + +func TestResponderGetCost(t *testing.T) { + tmpCache := Cache + cfg, err := config.NewDefaultCGRConfig() + if err != nil { + t.Error(err) + } + tmpDm := dm + db := NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := NewDataManager(db, cfg.CacheCfg(), nil) + + defer func() { + Cache = tmpCache + SetDataStorage(tmpDm) + }() + rsponder.MaxComputedUsage = map[string]time.Duration{ + utils.VOICE: time.Duration(20 * time.Second), + } + cd := &CallDescriptorWithArgDispatcher{ + CallDescriptor: &CallDescriptor{ + Category: "call", + Tenant: "cgrates.org", + Subject: "1001", + ToR: utils.VOICE, + Account: "1001", + Destination: "1002", + DurationIndex: 90, + TimeStart: time.Date(2023, 3, 10, 9, 30, 0, 0, time.UTC), + TimeEnd: time.Date(2023, 3, 10, 9, 30, 2, 0, time.UTC), + }, + } + rpDflt := &RatingPlan{ + Id: "RP_DFLT", + Timings: map[string]*RITiming{ + "30eab301": { + Years: utils.Years{}, + Months: utils.Months{}, + MonthDays: utils.MonthDays{}, + WeekDays: utils.WeekDays{}, + StartTime: "00:00:00", + }, + }, + Ratings: map[string]*RIRate{ + "b457f861": { + Rates: []*Rate{ + { + GroupIntervalStart: 0, + Value: 0.01, + RateIncrement: time.Second, + RateUnit: time.Second, + }, + }, + RoundingMethod: utils.ROUNDING_MIDDLE, + RoundingDecimals: 4, + }, + }, + DestinationRates: map[string]RPRateList{ + "DEST": []*RPRate{ + { + Timing: "30eab301", + Rating: "b457f861", + Weight: 10, + }, + }, + }, + } + + rpfTCDDBSWF := &RatingProfile{Id: utils.ConcatenatedKey(utils.META_OUT, cd.Tenant, "call", cd.Subject), + RatingPlanActivations: RatingPlanActivations{&RatingPlanActivation{ + ActivationTime: time.Date(2015, 01, 01, 8, 0, 0, 0, time.UTC), + RatingPlanId: rpDflt.Id, + }}, + } + dest := &Destination{ + Id: "DEST", + Prefixes: []string{"1001", "1002", "1003"}, + } + if err := dm.SetReverseDestination(dest, utils.NonTransactional); err != nil { + t.Error(err) + } + Cache.Set(utils.CacheRatingPlans, rpDflt.Id, rpDflt, []string{}, true, utils.NonTransactional) + Cache.Set(utils.CacheRatingProfilesTmp, rpfTCDDBSWF.Id, rpfTCDDBSWF, []string{}, true, utils.NonTransactional) + var reply CallCost + SetDataStorage(dm) + if err := rsponder.GetCost(cd, &reply); err != nil { + t.Error(err) + } else if reply.Cost != 0.02 { + t.Errorf("Received %v", reply.Cost) + } +} + +func TestResponderDebit11(t *testing.T) { + tmpCache := Cache + tmpDm := *dm + defer func() { + Cache = tmpCache + SetDataStorage(&tmpDm) + }() + rsponder.MaxComputedUsage = map[string]time.Duration{ + utils.VOICE: time.Duration(60 * time.Second), + } + arg := &CallDescriptorWithArgDispatcher{ + CallDescriptor: &CallDescriptor{ + Category: "call", + Tenant: "cgrates.org", + Subject: "1001", + ToR: utils.VOICE, + Account: "1001", + Destination: "1002", + DurationIndex: 90, + TimeStart: time.Date(2023, 3, 10, 9, 30, 0, 0, time.UTC), + TimeEnd: time.Date(2023, 3, 10, 9, 30, 16, 0, time.UTC), + }, + } + acc := &Account{ + ID: "cgrates.org:1001", + BalanceMap: map[string]Balances{ + utils.VOICE: { + &Balance{Value: 20 * float64(time.Second), + DestinationIDs: utils.NewStringMap("1002"), + Weight: 10, RatingSubject: "rif"}, + }}, + } + dm.SetAccount(acc) + var reply CallCost + if err := rsponder.Debit(arg, &reply); err != nil { + t.Error(err) + } + +} diff --git a/engine/stats_test.go b/engine/stats_test.go index 5605e40ea..fd410f26e 100644 --- a/engine/stats_test.go +++ b/engine/stats_test.go @@ -19,6 +19,7 @@ package engine import ( "reflect" + "sort" "testing" "time" @@ -611,7 +612,11 @@ func TestStatSGetQueueIDs(t *testing.T) { } if err := sS.V1GetQueueIDs("cgrates.org", &qIDs); err != nil { t.Error(err) - } else if !reflect.DeepEqual(expqIds, qIDs) { + } + sort.Slice(qIDs, func(i, j int) bool { + return qIDs[i] < qIDs[j] + }) + if !reflect.DeepEqual(expqIds, qIDs) { t.Errorf("Expected %v,Received %v", expqIds, qIDs) } }