From deec88bea36afaedae7414303766da1f235f504f Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Fri, 21 May 2021 12:29:05 +0300 Subject: [PATCH] Test AnyContext and AnySubsystem functionality --- dispatchers/dispatchers_test.go | 312 +++++++++++++++++++- engine/attributes_test.go | 493 ++++++++++++++++++++++++++++++-- 2 files changed, 782 insertions(+), 23 deletions(-) diff --git a/dispatchers/dispatchers_test.go b/dispatchers/dispatchers_test.go index 963715add..334f125a9 100644 --- a/dispatchers/dispatchers_test.go +++ b/dispatchers/dispatchers_test.go @@ -1273,7 +1273,7 @@ func TestDispatcherServiceCall4(t *testing.T) { } } -func TestDispatchersdispatcherProfileForEventAnySSfalse(t *testing.T) { +func TestDispatchersdispatcherProfileForEventAnySSfalses(t *testing.T) { tmp := engine.Cache defer func() { engine.Cache = tmp @@ -1349,3 +1349,313 @@ func TestDispatchersdispatcherProfileForEventAnySSfalse(t *testing.T) { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", dsp1, rcv) } } + +func TestDispatchersdispatcherProfileForEventAnySSfalseFirstNotFound(t *testing.T) { + tmp := engine.Cache + defer func() { + engine.Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil) + engine.Cache = engine.NewCacheS(cfg, dm, nil) + + dS := &DispatcherService{ + cfg: cfg, + dm: dm, + fltrS: engine.NewFilterS(cfg, nil, dm), + } + dS.cfg.DispatcherSCfg().AnySubsystem = false + + dsp1 := &engine.DispatcherProfile{ + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Account:1002"}, + Subsystems: []string{utils.MetaSessionS}, + ID: "DSP_1", + Strategy: "*weight", + Weight: 10, + } + err := dS.dm.SetDispatcherProfile(dsp1, true) + if err != nil { + t.Error(err) + } + + dsp2 := &engine.DispatcherProfile{ + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Subsystems: []string{utils.MetaAny}, + ID: "DSP_2", + Strategy: "*weight", + Weight: 20, + } + err = dS.dm.SetDispatcherProfile(dsp2, true) + if err != nil { + t.Error(err) + } + + tnt := "cgrates.org" + ev := &utils.CGREvent{ + Tenant: tnt, + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + } + subsys := utils.MetaSessionS + + if rcv, err := dS.dispatcherProfileForEvent(tnt, ev, subsys); err != nil { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, err) + } else if !reflect.DeepEqual(rcv, dsp2) { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", dsp2, rcv) + } +} + +func TestDispatchersdispatcherProfileForEventAnySSfalseFound(t *testing.T) { + tmp := engine.Cache + defer func() { + engine.Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil) + engine.Cache = engine.NewCacheS(cfg, dm, nil) + + dS := &DispatcherService{ + cfg: cfg, + dm: dm, + fltrS: engine.NewFilterS(cfg, nil, dm), + } + dS.cfg.DispatcherSCfg().AnySubsystem = false + + dsp1 := &engine.DispatcherProfile{ + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Subsystems: []string{utils.MetaSessionS}, + ID: "DSP_1", + Strategy: "*weight", + Weight: 20, + } + err := dS.dm.SetDispatcherProfile(dsp1, true) + if err != nil { + t.Error(err) + } + + dsp2 := &engine.DispatcherProfile{ + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Subsystems: []string{utils.MetaAny}, + ID: "DSP_2", + Strategy: "*weight", + Weight: 10, + } + err = dS.dm.SetDispatcherProfile(dsp2, true) + if err != nil { + t.Error(err) + } + + tnt := "cgrates.org" + ev := &utils.CGREvent{ + Tenant: tnt, + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + } + subsys := utils.MetaSessionS + + if rcv, err := dS.dispatcherProfileForEvent(tnt, ev, subsys); err != nil { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, err) + } else if !reflect.DeepEqual(rcv, dsp1) { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", dsp1, rcv) + } +} + +func TestDispatchersdispatcherProfileForEventAnySSfalseNotFound(t *testing.T) { + tmp := engine.Cache + defer func() { + engine.Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil) + engine.Cache = engine.NewCacheS(cfg, dm, nil) + + dS := &DispatcherService{ + cfg: cfg, + dm: dm, + fltrS: engine.NewFilterS(cfg, nil, dm), + } + dS.cfg.DispatcherSCfg().AnySubsystem = false + + dsp1 := &engine.DispatcherProfile{ + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Account:1002"}, + Subsystems: []string{utils.MetaSessionS}, + ID: "DSP_1", + Strategy: "*weight", + Weight: 20, + } + err := dS.dm.SetDispatcherProfile(dsp1, true) + if err != nil { + t.Error(err) + } + + dsp2 := &engine.DispatcherProfile{ + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Account:1002"}, + Subsystems: []string{utils.MetaAny}, + ID: "DSP_2", + Strategy: "*weight", + Weight: 10, + } + err = dS.dm.SetDispatcherProfile(dsp2, true) + if err != nil { + t.Error(err) + } + + tnt := "cgrates.org" + ev := &utils.CGREvent{ + Tenant: tnt, + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + } + subsys := utils.MetaSessionS + + if rcv, err := dS.dispatcherProfileForEvent(tnt, ev, subsys); err == nil || err != utils.ErrNotFound { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err) + } else if rcv != nil { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, rcv) + } +} + +func TestDispatchersdispatcherProfileForEventAnySStrueNotFound(t *testing.T) { + tmp := engine.Cache + defer func() { + engine.Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil) + engine.Cache = engine.NewCacheS(cfg, dm, nil) + + dS := &DispatcherService{ + cfg: cfg, + dm: dm, + fltrS: engine.NewFilterS(cfg, nil, dm), + } + + dsp1 := &engine.DispatcherProfile{ + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Account:1002"}, + Subsystems: []string{utils.MetaSessionS}, + ID: "DSP_1", + Strategy: "*weight", + Weight: 20, + } + err := dS.dm.SetDispatcherProfile(dsp1, true) + if err != nil { + t.Error(err) + } + + dsp2 := &engine.DispatcherProfile{ + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Account:1002"}, + Subsystems: []string{utils.MetaAny}, + ID: "DSP_2", + Strategy: "*weight", + Weight: 10, + } + err = dS.dm.SetDispatcherProfile(dsp2, true) + if err != nil { + t.Error(err) + } + + tnt := "cgrates.org" + ev := &utils.CGREvent{ + Tenant: tnt, + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + } + subsys := utils.MetaSessionS + + if rcv, err := dS.dispatcherProfileForEvent(tnt, ev, subsys); err == nil || err != utils.ErrNotFound { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err) + } else if rcv != nil { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, rcv) + } +} + +func TestDispatchersdispatcherProfileForEventAnySStrueBothFound(t *testing.T) { + tmp := engine.Cache + defer func() { + engine.Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, cfg.CacheCfg(), nil) + engine.Cache = engine.NewCacheS(cfg, dm, nil) + + dS := &DispatcherService{ + cfg: cfg, + dm: dm, + fltrS: engine.NewFilterS(cfg, nil, dm), + } + + dsp1 := &engine.DispatcherProfile{ + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Subsystems: []string{utils.MetaSessionS}, + ID: "DSP_1", + Strategy: "*weight", + Weight: 10, + } + err := dS.dm.SetDispatcherProfile(dsp1, true) + if err != nil { + t.Error(err) + } + + dsp2 := &engine.DispatcherProfile{ + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Subsystems: []string{utils.MetaAny}, + ID: "DSP_2", + Strategy: "*weight", + Weight: 20, + } + err = dS.dm.SetDispatcherProfile(dsp2, true) + if err != nil { + t.Error(err) + } + + tnt := "cgrates.org" + ev := &utils.CGREvent{ + Tenant: tnt, + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + } + subsys := utils.MetaSessionS + + if rcv, err := dS.dispatcherProfileForEvent(tnt, ev, subsys); err != nil { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, err) + } else if !reflect.DeepEqual(rcv, dsp2) { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", dsp2, rcv) + } + + dsp1.Weight = 30 + err = dS.dm.SetDispatcherProfile(dsp1, true) + if err != nil { + t.Error(err) + } + + if rcv, err := dS.dispatcherProfileForEvent(tnt, ev, subsys); err != nil { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, err) + } else if !reflect.DeepEqual(rcv, dsp1) { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", dsp1, rcv) + } +} diff --git a/engine/attributes_test.go b/engine/attributes_test.go index cced6e037..a6e145d3c 100644 --- a/engine/attributes_test.go +++ b/engine/attributes_test.go @@ -527,7 +527,7 @@ func TestAttributesV1ProcessEventErrorMetaValueExponent(t *testing.T) { } -func TestAttributesattributeProfileForEvent(t *testing.T) { +func TestAttributesattributeProfileForEventAnyCtxFalseNotFound(t *testing.T) { tmp := Cache defer func() { Cache = tmp @@ -542,6 +542,7 @@ func TestAttributesattributeProfileForEvent(t *testing.T) { dm: dm, filterS: NewFilterS(cfg, nil, dm), } + alS.cgrcfg.AttributeSCfg().AnyContext = false postpaid, err := config.NewRSRParsers(utils.MetaPostpaid, utils.InfieldSep) if err != nil { @@ -593,7 +594,6 @@ func TestAttributesattributeProfileForEvent(t *testing.T) { }, } lastID := "" - alS.cgrcfg.AttributeSCfg().AnyContext = false if rcv, err := alS.attributeProfileForEvent(tnt, ctx, nil, nil, evNm, lastID); err != nil { @@ -610,18 +610,151 @@ func TestAttributesattributeProfileForEvent(t *testing.T) { } else if rcv != nil { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, rcv) } +} - Cache.Clear(nil) - ap1.FilterIDs = []string{"*string:~*req.Account:1001"} +func TestAttributesattributeProfileForEventAnyCtxFalseFound(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := NewInternalDB(nil, nil, true) + dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + alS := &AttributeService{ + cgrcfg: cfg, + dm: dm, + filterS: NewFilterS(cfg, nil, dm), + } + alS.cgrcfg.AttributeSCfg().AnyContext = false + + postpaid, err := config.NewRSRParsers(utils.MetaPostpaid, utils.InfieldSep) + if err != nil { + t.Error(err) + } + ap1 := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Contexts: []string{utils.MetaSessionS}, + Attributes: []*Attribute{ + { + Path: "*req.RequestType", + Type: utils.MetaConstant, + Value: postpaid, + }, + }, + Weight: 20, + } err = alS.dm.SetAttributeProfile(ap1, true) if err != nil { t.Error(err) } + + ap2 := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_2", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Contexts: []string{utils.MetaAny}, + Attributes: []*Attribute{ + { + Path: "*req.RequestType", + Type: utils.MetaConstant, + Value: postpaid, + }, + }, + Weight: 10, + } err = alS.dm.SetAttributeProfile(ap2, true) if err != nil { t.Error(err) } + tnt := "cgrates.org" + ctx := utils.StringPointer(utils.MetaSessionS) + evNm := utils.MapStorage{ + utils.MetaReq: utils.MapStorage{ + utils.AccountField: "1001", + }, + } + lastID := "" + + if rcv, err := alS.attributeProfileForEvent(tnt, ctx, nil, nil, evNm, + lastID); err != nil { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, err) + } else if !reflect.DeepEqual(rcv, ap1) { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", ap1, rcv) + } +} + +func TestAttributesattributeProfileForEventAnyCtxTrueBothFound(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := NewInternalDB(nil, nil, true) + dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + alS := &AttributeService{ + cgrcfg: cfg, + dm: dm, + filterS: NewFilterS(cfg, nil, dm), + } + + postpaid, err := config.NewRSRParsers(utils.MetaPostpaid, utils.InfieldSep) + if err != nil { + t.Error(err) + } + ap1 := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Contexts: []string{utils.MetaSessionS}, + Attributes: []*Attribute{ + { + Path: "*req.RequestType", + Type: utils.MetaConstant, + Value: postpaid, + }, + }, + Weight: 20, + } + err = alS.dm.SetAttributeProfile(ap1, true) + if err != nil { + t.Error(err) + } + + ap2 := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_2", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Contexts: []string{utils.MetaAny}, + Attributes: []*Attribute{ + { + Path: "*req.RequestType", + Type: utils.MetaConstant, + Value: postpaid, + }, + }, + Weight: 10, + } + err = alS.dm.SetAttributeProfile(ap2, true) + if err != nil { + t.Error(err) + } + + tnt := "cgrates.org" + ctx := utils.StringPointer(utils.MetaSessionS) + evNm := utils.MapStorage{ + utils.MetaReq: utils.MapStorage{ + utils.AccountField: "1001", + }, + } + lastID := "" + if rcv, err := alS.attributeProfileForEvent(tnt, ctx, nil, nil, evNm, lastID); err != nil { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, err) @@ -629,12 +762,7 @@ func TestAttributesattributeProfileForEvent(t *testing.T) { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", ap1, rcv) } - Cache.Clear(nil) - alS.cgrcfg.AttributeSCfg().AnyContext = true - err = alS.dm.SetAttributeProfile(ap1, true) - if err != nil { - t.Error(err) - } + ap2.Weight = 30 err = alS.dm.SetAttributeProfile(ap2, true) if err != nil { t.Error(err) @@ -643,9 +771,77 @@ func TestAttributesattributeProfileForEvent(t *testing.T) { if rcv, err := alS.attributeProfileForEvent(tnt, ctx, nil, nil, evNm, lastID); err != nil { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, err) - } else if !reflect.DeepEqual(rcv, ap1) { - t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", ap1, rcv) + } else if !reflect.DeepEqual(rcv, ap2) { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", ap2, rcv) } +} + +func TestAttributesattributeProfileForEventAnyCtxTrueErrMatching(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := NewInternalDB(nil, nil, true) + dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + alS := &AttributeService{ + cgrcfg: cfg, + dm: dm, + filterS: NewFilterS(cfg, nil, dm), + } + + postpaid, err := config.NewRSRParsers(utils.MetaPostpaid, utils.InfieldSep) + if err != nil { + t.Error(err) + } + ap1 := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Contexts: []string{utils.MetaSessionS}, + Attributes: []*Attribute{ + { + Path: "*req.RequestType", + Type: utils.MetaConstant, + Value: postpaid, + }, + }, + Weight: 20, + } + err = alS.dm.SetAttributeProfile(ap1, true) + if err != nil { + t.Error(err) + } + + ap2 := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_2", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Contexts: []string{utils.MetaAny}, + Attributes: []*Attribute{ + { + Path: "*req.RequestType", + Type: utils.MetaConstant, + Value: postpaid, + }, + }, + Weight: 10, + } + err = alS.dm.SetAttributeProfile(ap2, true) + if err != nil { + t.Error(err) + } + + tnt := "cgrates.org" + ctx := utils.StringPointer(utils.MetaSessionS) + evNm := utils.MapStorage{ + utils.MetaReq: utils.MapStorage{ + utils.AccountField: "1001", + }, + } + lastID := "" dbm := &DataDBMock{ GetKeysForPrefixF: func(s string) ([]string, error) { @@ -661,8 +857,149 @@ func TestAttributesattributeProfileForEvent(t *testing.T) { } else if rcv != nil { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, rcv) } +} - alS.cgrcfg.AttributeSCfg().IndexedSelects = true +func TestAttributesattributeProfileForEventAnyCtxTrueNotFound(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := NewInternalDB(nil, nil, true) + dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + alS := &AttributeService{ + cgrcfg: cfg, + dm: dm, + filterS: NewFilterS(cfg, nil, dm), + } + + postpaid, err := config.NewRSRParsers(utils.MetaPostpaid, utils.InfieldSep) + if err != nil { + t.Error(err) + } + ap1 := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_1", + FilterIDs: []string{"*string:~*req.Account:1002"}, + Contexts: []string{utils.MetaSessionS}, + Attributes: []*Attribute{ + { + Path: "*req.RequestType", + Type: utils.MetaConstant, + Value: postpaid, + }, + }, + Weight: 20, + } + err = alS.dm.SetAttributeProfile(ap1, true) + if err != nil { + t.Error(err) + } + + ap2 := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_2", + FilterIDs: []string{"*string:~*req.Account:1002"}, + Contexts: []string{utils.MetaAny}, + Attributes: []*Attribute{ + { + Path: "*req.RequestType", + Type: utils.MetaConstant, + Value: postpaid, + }, + }, + Weight: 10, + } + err = alS.dm.SetAttributeProfile(ap2, true) + if err != nil { + t.Error(err) + } + + tnt := "cgrates.org" + ctx := utils.StringPointer(utils.MetaSessionS) + evNm := utils.MapStorage{ + utils.MetaReq: utils.MapStorage{ + utils.AccountField: "1001", + }, + } + lastID := "" + + if rcv, err := alS.attributeProfileForEvent(tnt, ctx, nil, nil, evNm, + lastID); err == nil || err != utils.ErrNotFound { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err) + } else if rcv != nil { + t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, rcv) + } +} + +func TestAttributesattributeProfileForEventNoDBConn(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := NewInternalDB(nil, nil, true) + dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + alS := &AttributeService{ + cgrcfg: cfg, + dm: dm, + filterS: NewFilterS(cfg, nil, dm), + } + + postpaid, err := config.NewRSRParsers(utils.MetaPostpaid, utils.InfieldSep) + if err != nil { + t.Error(err) + } + ap1 := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Contexts: []string{utils.MetaSessionS}, + Attributes: []*Attribute{ + { + Path: "*req.RequestType", + Type: utils.MetaConstant, + Value: postpaid, + }, + }, + Weight: 20, + } + err = alS.dm.SetAttributeProfile(ap1, true) + if err != nil { + t.Error(err) + } + + ap2 := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_2", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Contexts: []string{utils.MetaAny}, + Attributes: []*Attribute{ + { + Path: "*req.RequestType", + Type: utils.MetaConstant, + Value: postpaid, + }, + }, + Weight: 10, + } + err = alS.dm.SetAttributeProfile(ap2, true) + if err != nil { + t.Error(err) + } + + tnt := "cgrates.org" + ctx := utils.StringPointer(utils.MetaSessionS) + evNm := utils.MapStorage{ + utils.MetaReq: utils.MapStorage{ + utils.AccountField: "1001", + }, + } + lastID := "" alS.dm = nil if rcv, err := alS.attributeProfileForEvent(tnt, ctx, []string{"ATTR_3"}, nil, evNm, @@ -671,44 +1008,156 @@ func TestAttributesattributeProfileForEvent(t *testing.T) { } else if rcv != nil { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, rcv) } +} + +func TestAttributesattributeProfileForEventErrNotFound(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := NewInternalDB(nil, nil, true) + dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + alS := &AttributeService{ + cgrcfg: cfg, + dm: dm, + filterS: NewFilterS(cfg, nil, dm), + } apNil := &AttributeProfile{} - tnt = "" - alS.dm = dm err = alS.dm.SetAttributeProfile(apNil, true) if err != nil { t.Error(err) } + + tnt := "" + ctx := utils.StringPointer(utils.MetaSessionS) + evNm := utils.MapStorage{ + utils.MetaReq: utils.MapStorage{ + utils.AccountField: "1001", + }, + } + lastID := "" + if rcv, err := alS.attributeProfileForEvent(tnt, ctx, []string{"ATTR_3"}, nil, evNm, lastID); err == nil || err != utils.ErrNotFound { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err) } else if rcv != nil { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, rcv) } +} - Cache.Clear(nil) - ap1.ActivationInterval = &utils.ActivationInterval{ +func TestAttributesattributeProfileForEventNotActive(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := NewInternalDB(nil, nil, true) + dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + alS := &AttributeService{ + cgrcfg: cfg, + dm: dm, + filterS: NewFilterS(cfg, nil, dm), + } + + postpaid, err := config.NewRSRParsers(utils.MetaPostpaid, utils.InfieldSep) + if err != nil { + t.Error(err) + } + ap := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Contexts: []string{utils.MetaSessionS}, + Attributes: []*Attribute{ + { + Path: "*req.RequestType", + Type: utils.MetaConstant, + Value: postpaid, + }, + }, + Weight: 20, + } + + ctx := utils.StringPointer(utils.MetaSessionS) + evNm := utils.MapStorage{ + utils.MetaReq: utils.MapStorage{ + utils.AccountField: "1001", + }, + } + lastID := "" + + ap.ActivationInterval = &utils.ActivationInterval{ ExpiryTime: time.Date(2021, 5, 14, 15, 0, 0, 0, time.UTC), } - err = alS.dm.SetAttributeProfile(ap1, true) + err = alS.dm.SetAttributeProfile(ap, true) if err != nil { t.Error(err) } actTime := utils.TimePointer(time.Date(2021, 5, 14, 16, 0, 0, 0, time.UTC)) - tnt = "cgrates.org" + tnt := "cgrates.org" + if rcv, err := alS.attributeProfileForEvent(tnt, ctx, nil, actTime, evNm, lastID); err == nil || err != utils.ErrNotFound { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err) } else if rcv != nil { t.Errorf("\nexpected: <%+v>, \nreceived: <%+v>", nil, rcv) } +} - Cache.Clear(nil) - ap1.ActivationInterval = nil - err = alS.dm.SetAttributeProfile(ap1, true) +func TestAttributesattributeProfileForEventErrPass(t *testing.T) { + tmp := Cache + defer func() { + Cache = tmp + }() + + cfg := config.NewDefaultCGRConfig() + dataDB := NewInternalDB(nil, nil, true) + dm := NewDataManager(dataDB, cfg.CacheCfg(), nil) + Cache = NewCacheS(cfg, dm, nil) + alS := &AttributeService{ + cgrcfg: cfg, + dm: dm, + filterS: NewFilterS(cfg, nil, dm), + } + + postpaid, err := config.NewRSRParsers(utils.MetaPostpaid, utils.InfieldSep) if err != nil { t.Error(err) } + ap := &AttributeProfile{ + Tenant: "cgrates.org", + ID: "ATTR_1", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Contexts: []string{utils.MetaSessionS}, + Attributes: []*Attribute{ + { + Path: "*req.RequestType", + Type: utils.MetaConstant, + Value: postpaid, + }, + }, + Weight: 20, + } + err = alS.dm.SetAttributeProfile(ap, true) + if err != nil { + t.Error(err) + } + + tnt := "cgrates.org" + ctx := utils.StringPointer(utils.MetaSessionS) + evNm := utils.MapStorage{ + utils.MetaReq: utils.MapStorage{ + utils.AccountField: "1001", + }, + } + lastID := "" + evNm = utils.MapStorage{ utils.MetaReq: 1, }