From ff7a2463cd3800d44cf006d7dbf4c773ac804094 Mon Sep 17 00:00:00 2001 From: Trial97 Date: Fri, 22 May 2020 12:17:49 +0300 Subject: [PATCH] Added test for DynamicDataProvider in AgentRequest --- agents/agentreq_test.go | 68 ++++++++++++++++++++++++++++++++++++ utils/dynamicdataprovider.go | 11 +++++- utils/mapstorage_test.go | 4 +-- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/agents/agentreq_test.go b/agents/agentreq_test.go index 199bc74a0..2c3f0d5ba 100644 --- a/agents/agentreq_test.go +++ b/agents/agentreq_test.go @@ -1967,3 +1967,71 @@ func TestAgReqFiltersInsideField(t *testing.T) { t.Errorf("expecting: %+v, \n received: %+v ", time.Date(2018, 10, 4, 15, 3, 10, 0, time.UTC), val) } } + +func TestAgReqDynamicPath(t *testing.T) { + cfg, _ := config.NewDefaultCGRConfig() + data := engine.NewInternalDB(nil, nil, true, cfg.DataDbCfg().Items) + dm := engine.NewDataManager(data, config.CgrConfig().CacheCfg(), nil) + filterS := engine.NewFilterS(cfg, nil, dm) + agReq := NewAgentRequest(nil, nil, nil, nil, nil, nil, "cgrates.org", "", filterS, nil, nil) + // populate request, emulating the way will be done in HTTPAgent + agReq.CGRRequest.Set(&utils.FullPath{Path: utils.ToR, PathItems: utils.PathItems{{Field: utils.ToR}}}, utils.NewNMData(utils.VOICE)) + agReq.CGRRequest.Set(&utils.FullPath{Path: utils.Account, PathItems: utils.PathItems{{Field: utils.Account}}}, utils.NewNMData("1001")) + agReq.CGRRequest.Set(&utils.FullPath{Path: utils.Destination, PathItems: utils.PathItems{{Field: utils.Destination}}}, utils.NewNMData("1002")) + agReq.CGRRequest.Set(&utils.FullPath{Path: utils.AnswerTime, PathItems: utils.PathItems{{Field: utils.AnswerTime}}}, utils.NewNMData( + time.Date(2013, 12, 30, 15, 0, 1, 0, time.UTC))) + agReq.CGRRequest.Set(&utils.FullPath{Path: utils.RequestType, PathItems: utils.PathItems{{Field: utils.RequestType}}}, utils.NewNMData(utils.META_PREPAID)) + agReq.CGRRequest.Set(&utils.FullPath{Path: "Routes.CGR_ROUTE1", PathItems: utils.PathItems{{Field: "Routes"}, {Field: "CGR_ROUTE1"}}}, utils.NewNMData(1001)) + agReq.CGRRequest.Set(&utils.FullPath{Path: "Routes.CGR_ROUTE2", PathItems: utils.PathItems{{Field: "Routes"}, {Field: "CGR_ROUTE2"}}}, utils.NewNMData(1002)) + agReq.CGRRequest.Set(&utils.FullPath{Path: "BestRoute", PathItems: utils.PathItems{{Field: "BestRoute"}}}, utils.NewNMData("ROUTE1")) + + agReq.CGRReply = &utils.NavigableMap2{} + + tplFlds := []*config.FCTemplate{ + {Tag: "Tenant", + Path: utils.MetaCgrep + utils.NestingSep + utils.Tenant, Type: utils.META_COMPOSED, + Value: config.NewRSRParsersMustCompile("cgrates.org", true, utils.INFIELD_SEP)}, + {Tag: "Account", + Path: utils.MetaCgrep + utils.NestingSep + utils.Account, Type: utils.META_COMPOSED, + Value: config.NewRSRParsersMustCompile("~*cgreq.Account", true, utils.INFIELD_SEP)}, + {Tag: "Destination", + Path: utils.MetaCgrep + utils.NestingSep + utils.Destination, Type: utils.META_COMPOSED, + Value: config.NewRSRParsersMustCompile("~*cgreq.Destination", true, utils.INFIELD_SEP)}, + {Tag: "Usage", + Path: utils.MetaCgrep + utils.NestingSep + utils.Usage, Type: utils.MetaVariable, + Value: config.NewRSRParsersMustCompile("30s", true, utils.INFIELD_SEP)}, + {Tag: "Route", + Path: utils.MetaCgrep + utils.NestingSep + "Route", + Type: utils.MetaVariable, Value: config.NewRSRParsersMustCompile("~*cgreq.Routes[CGR_|~*cgreq.BestRoute]", true, utils.INFIELD_SEP), + }, + // {Tag: "Route2", + // Path: utils.MetaCgrep + utils.NestingSep + "Route2[CGR_|]", + // Type: utils.MetaVariable, Value: config.NewRSRParsersMustCompile("~*cgreq.Routes[CGR_|~*cgreq.BestRoute]", true, utils.INFIELD_SEP), + // }, + } + for _, v := range tplFlds { + v.ComputePath() + } + eMp := &utils.NavigableMap2{} + eMp.Set(utils.PathItems{{Field: utils.Tenant}}, &utils.NMSlice{ + &config.NMItem{Data: "cgrates.org", Path: []string{utils.Tenant}, + Config: tplFlds[0]}}) + eMp.Set(utils.PathItems{{Field: utils.Account}}, &utils.NMSlice{ + &config.NMItem{Data: "1001", Path: []string{utils.Account}, + Config: tplFlds[1]}}) + eMp.Set(utils.PathItems{{Field: utils.Destination}}, &utils.NMSlice{ + &config.NMItem{Data: "1002", Path: []string{utils.Destination}, + Config: tplFlds[2]}}) + eMp.Set(utils.PathItems{{Field: "Usage"}}, &utils.NMSlice{ + &config.NMItem{Data: "30s", Path: []string{"Usage"}, + Config: tplFlds[3]}}) + eMp.Set(utils.PathItems{{Field: "Route"}}, &utils.NMSlice{ + &config.NMItem{Data: "1001", Path: []string{"Route"}, + Config: tplFlds[4]}}) + + if err := agReq.SetFields(tplFlds); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(agReq.CGRReply, eMp) { + t.Errorf("expecting: %+v,\n received: %+v", eMp, agReq.CGRReply) + } +} diff --git a/utils/dynamicdataprovider.go b/utils/dynamicdataprovider.go index df14ee820..d2e1fe0db 100644 --- a/utils/dynamicdataprovider.go +++ b/utils/dynamicdataprovider.go @@ -115,7 +115,7 @@ func (ddp *DynamicDataProvider) proccesFieldPath(fldPath string) (newPath string // GetFullFieldPath returns the full path for the func (ddp *DynamicDataProvider) GetFullFieldPath(fldPath string) (fpath *FullPath, err error) { var newPath string - if newPath, err = ddp.proccesFieldPath(fldPath); err != nil { + if newPath, err = ddp.proccesFieldPath(fldPath); err != nil || newPath == EmptyString { return } fpath = &FullPath{ @@ -124,3 +124,12 @@ func (ddp *DynamicDataProvider) GetFullFieldPath(fldPath string) (fpath *FullPat } return } + +// FieldAsString returns the value from path as string +func (ddp DynamicDataProvider) FieldAsString(fldPath []string) (str string, err error) { + var val interface{} + if val, err = ddp.FieldAsInterface(fldPath); err != nil { + return + } + return IfaceAsString(val), nil +} diff --git a/utils/mapstorage_test.go b/utils/mapstorage_test.go index 79754efd0..ad3bd0978 100644 --- a/utils/mapstorage_test.go +++ b/utils/mapstorage_test.go @@ -139,12 +139,12 @@ func TestNavMapGetField(t *testing.T) { }, "FirstLevel2": map[string]interface{}{ "SecondLevel2": []map[string]interface{}{ - map[string]interface{}{ + { "ThirdLevel2": map[string]interface{}{ "Fld1": "Val1", }, }, - map[string]interface{}{ + { "Count": 10, "ThirdLevel2": map[string]interface{}{ "Fld2": []string{"Val1", "Val2", "Val3"},