From f1eb30a3bd8b634ebe58bd427131f0e596b4bead Mon Sep 17 00:00:00 2001 From: andronache Date: Thu, 29 Apr 2021 13:59:38 +0300 Subject: [PATCH] Cover tests for apis/rates.go --- apis/rates_test.go | 106 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 apis/rates_test.go diff --git a/apis/rates_test.go b/apis/rates_test.go new file mode 100644 index 000000000..4352f7a5e --- /dev/null +++ b/apis/rates_test.go @@ -0,0 +1,106 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +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 apis + +import ( + "reflect" + "testing" + + "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/utils" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" +) + +func TestRatesGetRateProfileErrMandatoryIeMissing(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + connMgr := engine.NewConnManager(cfg, nil) + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, nil, connMgr) + admS := NewAdminSv1(cfg, dm, connMgr) + args := &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{}, + } + rply := &utils.RateProfile{} + err := admS.GetRateProfile(context.Background(), args, rply) + expected := "MANDATORY_IE_MISSING: [ID]" + if err == nil || err.Error() != expected { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", expected, rply) + } +} + +func TestRatesGetRateProfile(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.GeneralCfg().DefaultCaching = utils.MetaNone + connMgr := engine.NewConnManager(cfg, nil) + dataDB := engine.NewInternalDB(nil, nil, true) + dm := engine.NewDataManager(dataDB, nil, connMgr) + admS := NewAdminSv1(cfg, dm, connMgr) + ext := &utils.APIRateProfile{ + ID: "DefaultRate", + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.APIRate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, + }, + } + + var rtRply string + err := admS.SetRateProfile(context.Background(), ext, &rtRply) + if err != nil { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) + } + args := &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + ID: "DefaultRate", + }, + } + var result utils.RateProfile + + expected := &utils.RateProfile{ + ID: "DefaultRate", + Tenant: "cgrates.org", + FilterIDs: []string{"*string:~*req.Subject:1001"}, + Rates: map[string]*utils.Rate{ + "RT_WEEK": { + ID: "RT_WEEK", + ActivationTimes: "* * * * *", + }, + }, + } + err = admS.GetRateProfile(context.Background(), args, &result) + rslt := &result + + if !reflect.DeepEqual(expected.Rates["RT_WEEK"].ID, rslt.Rates["RT_WEEK"].ID) { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON(expected), utils.ToJSON(rslt)) + } + if !reflect.DeepEqual(expected.Rates["RT_WEEK"].ActivationTimes, rslt.Rates["RT_WEEK"].ActivationTimes) { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON(expected), utils.ToJSON(rslt)) + } + expected.Rates = nil + rslt.Rates = nil + if err != nil { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", nil, err) + } else if !reflect.DeepEqual(expected, rslt) { + t.Errorf("\nExpected <%+v>, \nReceived <%+v>", utils.ToJSON(expected), utils.ToJSON(rslt)) + } + +}