diff --git a/config/rsrparser.go b/config/rsrparser.go index 9a8f88f80..93063ddc6 100644 --- a/config/rsrparser.go +++ b/config/rsrparser.go @@ -107,7 +107,7 @@ func NewRSRParsersMustCompile(parsersRules string, rsrSeparator string) (prsrs R // RSRParsers is a set of RSRParser type RSRParsers []*RSRParser -func (prsrs RSRParsers) GetRule() (out string) { // ToDo: add tests for this +func (prsrs RSRParsers) GetRule() (out string) { for _, prsr := range prsrs { out += utils.INFIELD_SEP + prsr.Rules } diff --git a/config/rsrparser_test.go b/config/rsrparser_test.go index 6b520f9d2..952c9e789 100644 --- a/config/rsrparser_test.go +++ b/config/rsrparser_test.go @@ -143,6 +143,28 @@ func TestNewRSRParsersConstant2(t *testing.T) { } else if expected := "constant>;q=0.7;expires=3600constant"; out != expected { t.Errorf("Expected %q ,received %q", expected, out) } + + ruleStr = "constant;`>;q=0.7;expires=3600`;" + if rsrParsers, err := NewRSRParsers(ruleStr, utils.INFIELD_SEP); err != nil { + t.Error("Unexpected error: ", err.Error()) + } else if out, err := rsrParsers.ParseDataProvider(utils.MapStorage{}); err != nil { + t.Error(err) + } else if expected := "constant>;q=0.7;expires=3600"; out != expected { + t.Errorf("Expected %q ,received %q", expected, out) + } + + ruleStr = "constant;`>;q=0.7;expires=3600constant" + if _, err := NewRSRParsers(ruleStr, utils.INFIELD_SEP); err == nil { + t.Error("Unexpected error: ", err.Error()) + } + + ruleStr = "constant;`>;q=0.7;expires=3600`;~*req.Account" + if rsrParsers, err := NewRSRParsers(ruleStr, utils.INFIELD_SEP); err != nil { + t.Error("Unexpected error: ", err.Error()) + } else if _, err := rsrParsers.ParseDataProvider(utils.MapStorage{}); err != utils.ErrNotFound { + t.Error(err) + } + } func TestRSRParserCompileConstant(t *testing.T) { @@ -173,4 +195,132 @@ func TestNewRSRParsersParseDataProviderWithInterfaces(t *testing.T) { } else if expected := "~*accounts.1001"; out != expected { t.Errorf("Expected %q ,received %q", expected, out) } + + ruleStr = "constant;`>;q=0.7;expires=3600`;~*req.Account" + if rsrParsers, err := NewRSRParsers(ruleStr, utils.INFIELD_SEP); err != nil { + t.Error("Unexpected error: ", err.Error()) + } else if _, err := rsrParsers.ParseDataProviderWithInterfaces(utils.MapStorage{}); err != utils.ErrNotFound { + t.Error(err) + } +} + +func TestNewRSRParsersFromSlice(t *testing.T) { + if _, err := NewRSRParsersFromSlice([]string{""}); err == nil { + t.Error("Unexpected error: ", err) + } + + if _, err := NewRSRParsersFromSlice([]string{"~*req.Account{*"}); err == nil { + t.Error("Unexpected error: ", err) + } +} + +func TestNewRSRParsersMustCompile(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Error("Expected panic on wrong rule") + } + }() + NewRSRParsersMustCompile("~*req.Account{*", ";") +} + +func TestRSRParserGetRule(t *testing.T) { + ruleStr := "constant;~*req.Account" + if rsrParsers, err := NewRSRParsers(ruleStr, utils.INFIELD_SEP); err != nil { + t.Error("Unexpected error: ", err.Error()) + } else if rule := rsrParsers.GetRule(); rule != ruleStr { + t.Errorf("Expected: %q received: %q", ruleStr, rule) + } +} + +func TestRSRParsersCompile(t *testing.T) { + prsrs := RSRParsers{&RSRParser{ + Rules: ":>;q=0.7;expires=3600", + }} + ePrsr := RSRParsers{&RSRParser{ + Rules: ":>;q=0.7;expires=3600", + + path: ":>;q=0.7;expires=3600", + }} + if err := prsrs.Compile(); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(prsrs, ePrsr) { + t.Errorf("Expected %+v received %+v", ePrsr, prsrs) + } + prsrs = RSRParsers{&RSRParser{ + Rules: "~*req.Account{*unuportedConverter}", + }} + if err := prsrs.Compile(); err == nil { + t.Error("Expected error received:", err) + } +} + +func TestRSRParsersParseValue(t *testing.T) { + rsrParsers, err := NewRSRParsers("~*req.Account{*round}", utils.INFIELD_SEP) + if err != nil { + t.Error("Unexpected error: ", err.Error()) + } + if _, err = rsrParsers.ParseValue("A"); err == nil { + t.Error("Expected error received:", err) + } +} + +func TestNewRSRParserMustCompile(t *testing.T) { + rsr := NewRSRParserMustCompile("~*req.Account") + ePrsr := &RSRParser{ + Rules: "~*req.Account", + + path: "~*req.Account", + } + if !reflect.DeepEqual(rsr, ePrsr) { + t.Errorf("Expected %+v received %+v", ePrsr, rsr) + } + defer func() { + if r := recover(); r == nil { + t.Error("Expected panic on wrong rule") + } + }() + NewRSRParserMustCompile("~*req.Account{*") +} + +func TestRSRParserAttrName(t *testing.T) { + rsr := NewRSRParserMustCompile("~*req.Account") + expected := "*req.Account" + if attr := rsr.AttrName(); attr != expected { + t.Errorf("Expected: %q received: %q", expected, attr) + } +} + +func TestRSRParserRegexpMatched(t *testing.T) { + rsr := NewRSRParserMustCompile("~*req.Time:s/(.*)/${1}s/") + expected := "1ss" + if val, err := rsr.parseValue("1s"); err != nil { + t.Error(err) + } else if val != expected { + t.Errorf("Expected: %q received: %q", expected, val) + } + if !rsr.RegexpMatched() { + t.Error("Expected the regex to match") + } + rsr = NewRSRParserMustCompile("~*req.Time:s/(a+)/${1}s/") + expected = "1s" + if val, err := rsr.parseValue("1s"); err != nil { + t.Error(err) + } else if val != expected { + t.Errorf("Expected: %q received: %q", expected, val) + } + if rsr.RegexpMatched() { + t.Error("Expected the regex to not match") + } +} + +func TestRSRParserCompile3(t *testing.T) { + rsr := &RSRParser{Rules: "~*req.Account:s/(a+)/${1}s"} + if err := rsr.Compile(); err == nil { + t.Error("Expected error received:", err) + } + + rsr = &RSRParser{Rules: "~*req.Account:s/*/${1}s/"} + if err := rsr.Compile(); err == nil { + t.Error("Expected error received:", err) + } } diff --git a/loaders/loader_test.go b/loaders/loader_test.go index df8b08b4c..c23c56593 100644 --- a/loaders/loader_test.go +++ b/loaders/loader_test.go @@ -1354,9 +1354,9 @@ func TestLoaderProcessRateProfile(t *testing.T) { MaxCostStrategy: "*free", Rates: map[string]*engine.Rate{ "RT_WEEK": { - ID: "RT_WEEK", - Weight: 0, - ActivationStart: "* * * * 1-5", + ID: "RT_WEEK", + Weight: 0, + ActivationTime: "* * * * 1-5", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1373,9 +1373,9 @@ func TestLoaderProcessRateProfile(t *testing.T) { }, }, "RT_WEEKEND": { - ID: "RT_WEEKEND", - Weight: 10, - ActivationStart: "* * * * 0,6", + ID: "RT_WEEKEND", + Weight: 10, + ActivationTime: "* * * * 0,6", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1386,9 +1386,9 @@ func TestLoaderProcessRateProfile(t *testing.T) { }, }, "RT_CHRISTMAS": { - ID: "RT_CHRISTMAS", - Weight: 30, - ActivationStart: "* * 24 12 *", + ID: "RT_CHRISTMAS", + Weight: 30, + ActivationTime: "* * 24 12 *", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1541,9 +1541,9 @@ cgrates.org,RP1,,,,,,,,,,RT_CHRISTMAS,,* * 24 12 *,30,false,0s,0.06,1m,1s MaxCostStrategy: "*free", Rates: map[string]*engine.Rate{ "RT_WEEK": { - ID: "RT_WEEK", - Weight: 0, - ActivationStart: "* * * * 1-5", + ID: "RT_WEEK", + Weight: 0, + ActivationTime: "* * * * 1-5", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1600,9 +1600,9 @@ cgrates.org,RP1,,,,,,,,,,RT_CHRISTMAS,,* * 24 12 *,30,false,0s,0.06,1m,1s MaxCostStrategy: "*free", Rates: map[string]*engine.Rate{ "RT_WEEK": { - ID: "RT_WEEK", - Weight: 0, - ActivationStart: "* * * * 1-5", + ID: "RT_WEEK", + Weight: 0, + ActivationTime: "* * * * 1-5", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1619,9 +1619,9 @@ cgrates.org,RP1,,,,,,,,,,RT_CHRISTMAS,,* * 24 12 *,30,false,0s,0.06,1m,1s }, }, "RT_WEEKEND": { - ID: "RT_WEEKEND", - Weight: 10, - ActivationStart: "* * * * 0,6", + ID: "RT_WEEKEND", + Weight: 10, + ActivationTime: "* * * * 0,6", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1632,9 +1632,9 @@ cgrates.org,RP1,,,,,,,,,,RT_CHRISTMAS,,* * 24 12 *,30,false,0s,0.06,1m,1s }, }, "RT_CHRISTMAS": { - ID: "RT_CHRISTMAS", - Weight: 30, - ActivationStart: "* * 24 12 *", + ID: "RT_CHRISTMAS", + Weight: 30, + ActivationTime: "* * 24 12 *", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1696,9 +1696,9 @@ func TestLoaderRemoveRateProfileRates(t *testing.T) { MaxCostStrategy: "*free", Rates: map[string]*engine.Rate{ "RT_WEEK": { - ID: "RT_WEEK", - Weight: 0, - ActivationStart: "* * * * 1-5", + ID: "RT_WEEK", + Weight: 0, + ActivationTime: "* * * * 1-5", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1715,9 +1715,9 @@ func TestLoaderRemoveRateProfileRates(t *testing.T) { }, }, "RT_WEEKEND": { - ID: "RT_WEEKEND", - Weight: 10, - ActivationStart: "* * * * 0,6", + ID: "RT_WEEKEND", + Weight: 10, + ActivationTime: "* * * * 0,6", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1728,9 +1728,9 @@ func TestLoaderRemoveRateProfileRates(t *testing.T) { }, }, "RT_CHRISTMAS": { - ID: "RT_CHRISTMAS", - Weight: 30, - ActivationStart: "* * 24 12 *", + ID: "RT_CHRISTMAS", + Weight: 30, + ActivationTime: "* * 24 12 *", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1758,9 +1758,9 @@ func TestLoaderRemoveRateProfileRates(t *testing.T) { MaxCostStrategy: "*free", Rates: map[string]*engine.Rate{ "RT_WEEK": { - ID: "RT_WEEK", - Weight: 0, - ActivationStart: "* * * * 1-5", + ID: "RT_WEEK", + Weight: 0, + ActivationTime: "* * * * 1-5", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1777,9 +1777,9 @@ func TestLoaderRemoveRateProfileRates(t *testing.T) { }, }, "RT_WEEKEND": { - ID: "RT_WEEKEND", - Weight: 10, - ActivationStart: "* * * * 0,6", + ID: "RT_WEEKEND", + Weight: 10, + ActivationTime: "* * * * 0,6", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1790,9 +1790,9 @@ func TestLoaderRemoveRateProfileRates(t *testing.T) { }, }, "RT_CHRISTMAS": { - ID: "RT_CHRISTMAS", - Weight: 30, - ActivationStart: "* * 24 12 *", + ID: "RT_CHRISTMAS", + Weight: 30, + ActivationTime: "* * 24 12 *", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1850,9 +1850,9 @@ cgrates.org,RP1, MaxCostStrategy: "*free", Rates: map[string]*engine.Rate{ "RT_WEEK": { - ID: "RT_WEEK", - Weight: 0, - ActivationStart: "* * * * 1-5", + ID: "RT_WEEK", + Weight: 0, + ActivationTime: "* * * * 1-5", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1869,9 +1869,9 @@ cgrates.org,RP1, }, }, "RT_CHRISTMAS": { - ID: "RT_CHRISTMAS", - Weight: 30, - ActivationStart: "* * 24 12 *", + ID: "RT_CHRISTMAS", + Weight: 30, + ActivationTime: "* * 24 12 *", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second), @@ -1923,9 +1923,9 @@ cgrates.org,RP1, MaxCostStrategy: "*free", Rates: map[string]*engine.Rate{ "RT_WEEK": { - ID: "RT_WEEK", - Weight: 0, - ActivationStart: "* * * * 1-5", + ID: "RT_WEEK", + Weight: 0, + ActivationTime: "* * * * 1-5", IntervalRates: []*engine.IntervalRate{ { IntervalStart: time.Duration(0 * time.Second),