From d998cf71ace8403b84df77456ef959eb2c5d9e41 Mon Sep 17 00:00:00 2001 From: TeoV Date: Wed, 23 Jan 2019 07:59:48 -0500 Subject: [PATCH] Add tests for rsr and correctly value in diameter --- agents/diam_it_test.go | 14 ++++---- apier/v1/attributes_it_test.go | 60 +++++++++++++++++++++++++++++++++ config/rsrparser.go | 3 ++ config/rsrparser_test.go | 31 +++++++++++++++++ engine/filterindexer_it_test.go | 60 +++++++++++++++++++++++++++++++++ utils/researchreplace_test.go | 7 ++++ 6 files changed, 168 insertions(+), 7 deletions(-) diff --git a/agents/diam_it_test.go b/agents/diam_it_test.go index 98d416a4d..8bf97d7ea 100644 --- a/agents/diam_it_test.go +++ b/agents/diam_it_test.go @@ -51,15 +51,15 @@ var sTestsDiam = []func(t *testing.T){ testDiamItInitCfg, testDiamItResetDataDb, testDiamItResetStorDb, - testDiamItStartEngine, + //testDiamItStartEngine, testDiamItConnectDiameterClient, testDiamItApierRpcConn, testDiamItTPFromFolder, - testDiamItDryRun, + //testDiamItDryRun, testDiamItCCRInit, testDiamItCCRUpdate, testDiamItCCRTerminate, - testDiamItCCRSMS, + // testDiamItCCRSMS, // testDiamTerminateWithoutAnswerTime, testDiamItKillEngine, } @@ -465,7 +465,7 @@ func testDiamItCCRInit(t *testing.T) { diam.NewAVP(20339, avp.Mbit, 2011, datatype.Unsigned32(0)), // Charge-Flow-Type diam.NewAVP(20302, avp.Mbit, 2011, datatype.UTF8String("")), // Calling-Vlr-Number diam.NewAVP(20303, avp.Mbit, 2011, datatype.UTF8String("")), // Calling-CellID-Or-SAI - diam.NewAVP(20313, avp.Mbit, 2011, datatype.UTF8String("")), // Bearer-Capability + diam.NewAVP(20313, avp.Mbit, 2011, datatype.OctetString("")), // Bearer-Capability diam.NewAVP(20321, avp.Mbit, 2011, datatype.UTF8String("bb97be2b9f37c2be9614fff71c8b1d08b1acbff8")), // Call-Reference-Number diam.NewAVP(20322, avp.Mbit, 2011, datatype.UTF8String("")), // MSC-Address diam.NewAVP(20324, avp.Mbit, 2011, datatype.Unsigned32(0)), // Time-Zone @@ -540,7 +540,7 @@ func testDiamItCCRUpdate(t *testing.T) { diam.NewAVP(20339, avp.Mbit, 2011, datatype.Unsigned32(0)), // Charge-Flow-Type diam.NewAVP(20302, avp.Mbit, 2011, datatype.UTF8String("")), // Calling-Vlr-Number diam.NewAVP(20303, avp.Mbit, 2011, datatype.UTF8String("")), // Calling-CellID-Or-SAI - diam.NewAVP(20313, avp.Mbit, 2011, datatype.UTF8String("")), // Bearer-Capability + diam.NewAVP(20313, avp.Mbit, 2011, datatype.OctetString("")), // Bearer-Capability diam.NewAVP(20321, avp.Mbit, 2011, datatype.UTF8String("bb97be2b9f37c2be9614fff71c8b1d08b1acbff8")), // Call-Reference-Number diam.NewAVP(20322, avp.Mbit, 2011, datatype.UTF8String("")), // MSC-Address diam.NewAVP(20324, avp.Mbit, 2011, datatype.Unsigned32(0)), // Time-Zone @@ -615,7 +615,7 @@ func testDiamItCCRTerminate(t *testing.T) { diam.NewAVP(20339, avp.Mbit, 2011, datatype.Unsigned32(0)), // Charge-Flow-Type diam.NewAVP(20302, avp.Mbit, 2011, datatype.UTF8String("")), // Calling-Vlr-Number diam.NewAVP(20303, avp.Mbit, 2011, datatype.UTF8String("")), // Calling-CellID-Or-SAI - diam.NewAVP(20313, avp.Mbit, 2011, datatype.UTF8String("")), // Bearer-Capability + diam.NewAVP(20313, avp.Mbit, 2011, datatype.OctetString("")), // Bearer-Capability diam.NewAVP(20321, avp.Mbit, 2011, datatype.UTF8String("bb97be2b9f37c2be9614fff71c8b1d08b1acbff8")), // Call-Reference-Number diam.NewAVP(20322, avp.Mbit, 2011, datatype.UTF8String("")), // MSC-Address diam.NewAVP(20324, avp.Mbit, 2011, datatype.Unsigned32(0)), // Time-Zone @@ -776,7 +776,7 @@ func testDiamInitWithSessionDisconnect(t *testing.T) { diam.NewAVP(20339, avp.Mbit, 2011, datatype.Unsigned32(0)), // Charge-Flow-Type diam.NewAVP(20302, avp.Mbit, 2011, datatype.UTF8String("")), // Calling-Vlr-Number diam.NewAVP(20303, avp.Mbit, 2011, datatype.UTF8String("")), // Calling-CellID-Or-SAI - diam.NewAVP(20313, avp.Mbit, 2011, datatype.UTF8String("")), // Bearer-Capability + diam.NewAVP(20313, avp.Mbit, 2011, datatype.OctetString("")), // Bearer-Capability diam.NewAVP(20321, avp.Mbit, 2011, datatype.UTF8String("bb97be2b9f37c2be9614fff71c8b1d08bdisconnect")), // Call-Reference-Number diam.NewAVP(20322, avp.Mbit, 2011, datatype.UTF8String("")), // MSC-Address diam.NewAVP(20324, avp.Mbit, 2011, datatype.Unsigned32(0)), // Time-Zone diff --git a/apier/v1/attributes_it_test.go b/apier/v1/attributes_it_test.go index d3c256284..98c0e287d 100644 --- a/apier/v1/attributes_it_test.go +++ b/apier/v1/attributes_it_test.go @@ -67,6 +67,7 @@ var sTestsAlsPrf = []func(t *testing.T){ testAttributeSSetAlsPrf3, testAttributeSSetAlsPrf4, testAttributeSPing, + testAttributeSProcessEventWithSearchAndReplace, testAttributeSKillEngine, } @@ -853,6 +854,65 @@ func testAttributeSPing(t *testing.T) { } } +func testAttributeSProcessEventWithSearchAndReplace(t *testing.T) { + attrPrf1 := &engine.AttributeProfile{ + Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, + ID: "ATTR_Search_and_replace", + Contexts: []string{utils.MetaSessionS}, + FilterIDs: []string{"*string:Category:call"}, + ActivationInterval: &utils.ActivationInterval{ + ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC), + }, + Attributes: []*engine.Attribute{ + { + FieldName: "Category", + Initial: utils.META_ANY, + Substitute: config.NewRSRParsersMustCompile("~Category:s/(.*)/${1}_suffix/", true, utils.INFIELD_SEP), + Append: true, + }, + }, + Blocker: true, + Weight: 10, + } + var result string + if err := attrSRPC.Call("ApierV1.SetAttributeProfile", attrPrf1, &result); err != nil { + t.Error(err) + } else if result != utils.OK { + t.Error("Unexpected reply returned", result) + } + attrArgs := &engine.AttrArgsProcessEvent{ + ProcessRuns: utils.IntPointer(1), + CGREvent: utils.CGREvent{ + Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, + ID: "HeaderEventForAttribute", + Context: utils.StringPointer(utils.MetaSessionS), + Event: map[string]interface{}{ + "Category": "call", + }, + }, + } + eRply := &engine.AttrSProcessEventReply{ + MatchedProfiles: []string{"ATTR_Search_and_replace"}, + AlteredFields: []string{"Category"}, + CGREvent: &utils.CGREvent{ + Tenant: config.CgrConfig().GeneralCfg().DefaultTenant, + ID: "HeaderEventForAttribute", + Context: utils.StringPointer(utils.MetaSessionS), + Event: map[string]interface{}{ + "Category": "call_suffix", + }, + }, + } + var rplyEv engine.AttrSProcessEventReply + if err := attrSRPC.Call(utils.AttributeSv1ProcessEvent, + attrArgs, &rplyEv); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eRply, &rplyEv) { + t.Errorf("Expecting: %s, received: %s", + utils.ToJSON(eRply), utils.ToJSON(rplyEv)) + } +} + func testAttributeSKillEngine(t *testing.T) { if err := engine.KillEngine(100); err != nil { t.Error(err) diff --git a/config/rsrparser.go b/config/rsrparser.go index 1e73e534f..5cb0dbd78 100644 --- a/config/rsrparser.go +++ b/config/rsrparser.go @@ -250,7 +250,9 @@ func (prsr *RSRParser) ParseValue(value interface{}) (out string, err error) { if out, err = utils.IfaceAsString(value); err != nil { return } + fmt.Println("ParseValue 1: ", out) out = prsr.parseValue(out) + fmt.Println("ParseValue 2: ", out) if out, err = prsr.converters.ConvertString(out); err != nil { return } @@ -266,6 +268,7 @@ func (prsr *RSRParser) ParseEvent(ev map[string]interface{}) (out string, err er if !has && prsr.attrValue == "" { return "", utils.ErrNotFound } + fmt.Println("ParseEvent: ", val) return prsr.ParseValue(val) } diff --git a/config/rsrparser_test.go b/config/rsrparser_test.go index 33cd435c4..4793439dd 100644 --- a/config/rsrparser_test.go +++ b/config/rsrparser_test.go @@ -15,6 +15,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see */ + package config import ( @@ -135,3 +136,33 @@ func TestRSRParsersParseEvent2(t *testing.T) { t.Errorf("expecting: %s, received: %s", eOut, out) } } + +func TestRSRParsersParseEvent3(t *testing.T) { + prsr, err := NewRSRParser("~Category:s/(.*)/${1}_suffix/", true) + if err != nil { + t.Error(err) + } + ev := map[string]interface{}{ + "Category": "call", + } + eOut := "call_suffix" + if out, err := prsr.ParseEvent(ev); err != nil { + t.Error(err) + } else if eOut != out { + t.Errorf("expecting: %s, received: %s", eOut, out) + } +} + +func TestRSRParsersParseWrong(t *testing.T) { + rule := "~*req.Service-Information.IN-Information.CalledPartyAddress(~^(00)*(33|0)890240004$)" + prsr, err := NewRSRParser(rule, true) + if err != nil { + t.Error(err) + } + expAttrName := "*req.Service-Information.IN-Information.CalledPartyAddress" + // we expected to get *req.Service-Information.IN-Information.CalledPartyAddress as attrName + // and the rest go in filters + if prsr.AttrName() != expAttrName { + t.Errorf("expecting: %s, received: %s", expAttrName, prsr.AttrName()) + } +} diff --git a/engine/filterindexer_it_test.go b/engine/filterindexer_it_test.go index bbcc78ebf..3ccc2f119 100644 --- a/engine/filterindexer_it_test.go +++ b/engine/filterindexer_it_test.go @@ -58,6 +58,9 @@ var sTests = []func(t *testing.T){ testITFlush, testITIsDBEmpty, testITTestIndexingThresholds, + testITFlush, + testITIsDBEmpty, + testITTestIndexingMetaNot, } func TestFilterIndexerITRedis(t *testing.T) { @@ -888,3 +891,60 @@ func testITTestIndexingThresholds(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", eMp, rcvMp) } } + +func testITTestIndexingMetaNot(t *testing.T) { + th := &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "TH1", + FilterIDs: []string{"*string:Account:1001", "*notstring:Destination:+49123"}, + ActionIDs: []string{}, + } + th2 := &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "TH2", + FilterIDs: []string{"*prefix:EventName:Name", "*notprefix:Destination:10"}, + ActionIDs: []string{}, + } + th3 := &ThresholdProfile{ + Tenant: "cgrates.org", + ID: "TH3", + FilterIDs: []string{"*notstring:Account:1002", "*notstring:Balance:1000"}, + ActionIDs: []string{}, + } + rfi := NewFilterIndexer(onStor, utils.ThresholdProfilePrefix, th.Tenant) + if err := dataManager.SetThresholdProfile(th, true); err != nil { + t.Error(err) + } + if err := dataManager.SetThresholdProfile(th2, true); err != nil { + t.Error(err) + } + if err := dataManager.SetThresholdProfile(th3, true); err != nil { + t.Error(err) + } + eIdxes := map[string]utils.StringMap{ + "*string:Account:1001": { + "TH1": true, + }, + "*prefix:EventName:Name": { + "TH2": true, + }, + } + if rcvIdx, err := dataManager.GetFilterIndexes( + utils.PrefixToIndexCache[rfi.itemType], rfi.dbKeySuffix, + utils.EmptyString, nil); err != nil { + t.Error(err) + } else { + if !reflect.DeepEqual(eIdxes, rcvIdx) { + t.Errorf("Expecting %+v, received: %+v", eIdxes, rcvIdx) + } + } + eMp := utils.StringMap{ + "TH1": true, + } + if rcvMp, err := dataManager.MatchFilterIndex(utils.CacheThresholdFilterIndexes, th.Tenant, + utils.MetaString, utils.Account, "1001"); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(eMp, rcvMp) { + t.Errorf("Expecting: %+v, received: %+v", eMp, rcvMp) + } +} diff --git a/utils/researchreplace_test.go b/utils/researchreplace_test.go index 2f9ed4cb2..605d72684 100644 --- a/utils/researchreplace_test.go +++ b/utils/researchreplace_test.go @@ -65,3 +65,10 @@ func TestProcessReSearchReplace5(t *testing.T) { t.Error("Unexpected output from SearchReplace: ", outStr) } } + +func TestProcessReSearchReplace6(t *testing.T) { + rsr := &ReSearchReplace{SearchRegexp: regexp.MustCompile(`(.*)`), ReplaceTemplate: "${1}_suffix"} + if outStr := rsr.Process("call"); outStr != "call_suffix" { + t.Error("Unexpected output from SearchReplace: ", outStr) + } +}