diff --git a/apier/v1/filter_indexes_health_it_test.go b/apier/v1/filter_indexes_health_it_test.go index ab5e56870..792d15a52 100644 --- a/apier/v1/filter_indexes_health_it_test.go +++ b/apier/v1/filter_indexes_health_it_test.go @@ -23,6 +23,7 @@ import ( "net/rpc" "path" "reflect" + "sort" "testing" "time" @@ -40,9 +41,13 @@ var ( testV1FIdxHResetStorDb, testV1FIdxHStartEngine, testV1FIdxHRpcConn, - testV1FIdxHLoadFromFolder, + testV1FIdxHLoadFromFolderTutorial2, testV1FIdxHAccountActionPlansHealth, testV1FIdxHReverseDestinationHealth, + testV1FIdxHdxInitDataDb, + testV1FIdxHResetStorDb, + testV1FIdxHLoadFromFolderTutorial, + testV1FIdxGetThresholdsIndexesHealth, testV1FIdxHStopEngine, } @@ -102,7 +107,7 @@ func testV1FIdxHRpcConn(t *testing.T) { } } -func testV1FIdxHLoadFromFolder(t *testing.T) { +func testV1FIdxHLoadFromFolderTutorial2(t *testing.T) { var reply string attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial2")} if err := tFIdxHRpc.Call(utils.APIerSv1LoadTariffPlanFromFolder, attrs, &reply); err != nil { @@ -145,6 +150,80 @@ func testV1FIdxHReverseDestinationHealth(t *testing.T) { } } +func testV1FIdxHLoadFromFolderTutorial(t *testing.T) { + var reply string + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "tutorial")} + if err := tFIdxHRpc.Call(utils.APIerSv1LoadTariffPlanFromFolder, attrs, &reply); err != nil { + t.Error(err) + } + time.Sleep(100 * time.Millisecond) +} + +func testV1FIdxGetThresholdsIndexesHealth(t *testing.T) { + tPrfl = &engine.ThresholdProfileWithAPIOpts{ + ThresholdProfile: &engine.ThresholdProfile{ + Tenant: tenant, + ID: "TEST_PROFILE1", + FilterIDs: []string{"*string:~*req.Account:1004", + "*prefix:~*opts.Destination:+442|+554"}, + MaxHits: 1, + MinSleep: 5 * time.Minute, + Blocker: false, + Weight: 20.0, + ActionIDs: []string{"ACT_1", "ACT_2"}, + Async: true, + }, + } + + var rplyok string + if err := tFIdxHRpc.Call(utils.APIerSv1SetThresholdProfile, tPrfl, &rplyok); err != nil { + t.Error(err) + } else if rplyok != utils.OK { + t.Error("Unexpected reply returned", rplyok) + } + + expiIdx := []string{ + "*string:*req.Account:1002:THD_ACNT_1002", + "*string:*req.Account:1001:THD_ACNT_1001", + "*string:*req.Account:1004:TEST_PROFILE1", + "*prefix:*opts.Destination:+442:TEST_PROFILE1", + "*prefix:*opts.Destination:+554:TEST_PROFILE1", + } + var result []string + if err := tFIdxHRpc.Call(utils.APIerSv1GetFilterIndexes, &AttrGetFilterIndexes{ + ItemType: utils.MetaThresholds, + Tenant: "cgrates.org", + }, &result); err != nil { + t.Error(err) + } else { + sort.Strings(result) + sort.Strings(expiIdx) + if !reflect.DeepEqual(expiIdx, result) { + t.Errorf("Expecting: %+v, received: %+v", expiIdx, result) + } + } + + args := &engine.IndexHealthArgsWith3Ch{} + expRPly := &engine.FilterIHReply{ + MissingIndexes: map[string][]string{ + "cgrates.org:*prefix:*opts.Destination:+442": {"TEST_PROFILE1"}, + "cgrates.org:*prefix:*opts.Destination:+554": {"TEST_PROFILE1"}, + "cgrates.org:*string:*req.Account:1001": {"THD_ACNT_1001"}, + "cgrates.org:*string:*req.Account:1002": {"THD_ACNT_1002"}, + "cgrates.org:*string:*req.Account:1004": {"TEST_PROFILE1"}, + }, + BrokenIndexes: map[string][]string{}, + MissingFilters: map[string][]string{}, + } + var rply *engine.FilterIHReply + if err := tFIdxHRpc.Call(utils.APIerSv1GetThresholdsIndexesHealth, + args, &rply); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rply, expRPly) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(expRPly), utils.ToJSON(rply)) + } +} + func testV1FIdxHStopEngine(t *testing.T) { if err := engine.KillEngine(100); err != nil { t.Error(err) diff --git a/engine/cdrs_test.go b/engine/cdrs_test.go new file mode 100644 index 000000000..641d54477 --- /dev/null +++ b/engine/cdrs_test.go @@ -0,0 +1,218 @@ +/* +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 engine + +import ( + "fmt" + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" + "github.com/cgrates/rpcclient" + "testing" + "time" +) + +type clMock func(_ string, _ interface{}, _ interface{}) error + +func (c clMock) Call(m string, a interface{}, r interface{}) error { + return c(m, a, r) +} + +func TestCDRSV1ProcessCDRNoTenant(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.CdrsCfg().AttributeSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAttributes)} + clMock := clMock(func(_ string, args interface{}, reply interface{}) error { + rply, cancast := reply.(*AttrSProcessEventReply) + if !cancast { + return fmt.Errorf("can't cast") + } + newArgs, cancast := args.(*AttrArgsProcessEvent) + if !cancast { + return fmt.Errorf("can't cast") + } + if newArgs.Tenant == utils.EmptyString { + return fmt.Errorf("Tenant is missing") + } + *rply = AttrSProcessEventReply{ + AlteredFields: []string{utils.AccountField}, + CGREvent: &utils.CGREvent{ + ID: "TestBiRPCv1AuthorizeEventNoTenant", + Time: utils.TimePointer(time.Date(2016, time.January, 5, 18, 30, 49, 0, time.UTC)), + Event: map[string]interface{}{ + "Account": "1002", + "Category": "call", + "Destination": "1003", + "OriginHost": "local", + "OriginID": "123456", + "ToR": "*voice", + "Usage": "10s", + }, + }, + } + return nil + }) + chanClnt := make(chan rpcclient.ClientConnector, 1) + chanClnt <- clMock + connMngr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.MetaAttributes): chanClnt, + }) + db := NewInternalDB(nil, nil, true) + dm := NewDataManager(db, cfg.CacheCfg(), connMngr) + cdrs := &CDRServer{ + cgrCfg: cfg, + connMgr: connMngr, + cdrDb: NewInternalDB(nil, nil, true), + dm: dm, + } + cdr := &CDRWithAPIOpts{ // no tenant, take the default + CDR: &CDR{ + CGRID: "Cdr1", + OrderID: 123, + ToR: utils.MetaVoice, + OriginID: "OriginCDR1", + OriginHost: "192.168.1.1", + Source: "test", + RequestType: utils.MetaRated, + Category: "call", + Account: "1001", + Subject: "1001", + Destination: "+4986517174963", + RunID: utils.MetaDefault, + Usage: time.Duration(0), + ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, + Cost: 1.01, + }, + } + var reply string + if err := cdrs.V1ProcessCDR(cdr, &reply); err != nil { + t.Error(err) + } +} + +func TestCDRSV1ProcessEventNoTenant(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.CdrsCfg().ChargerSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaChargers)} + clMock := clMock(func(_ string, args interface{}, reply interface{}) error { + rply, cancast := reply.(*[]*ChrgSProcessEventReply) + if !cancast { + return fmt.Errorf("can't cast") + } + newArgs, cancast := args.(*utils.CGREvent) + if !cancast { + return fmt.Errorf("can't cast") + } + if newArgs.Tenant == utils.EmptyString { + return fmt.Errorf("Tenant is missing") + } + *rply = []*ChrgSProcessEventReply{} + return nil + }) + chanClnt := make(chan rpcclient.ClientConnector, 1) + chanClnt <- clMock + connMngr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.MetaChargers): chanClnt, + }) + db := NewInternalDB(nil, nil, true) + dm := NewDataManager(db, cfg.CacheCfg(), connMngr) + cdrs := &CDRServer{ + cgrCfg: cfg, + connMgr: connMngr, + cdrDb: NewInternalDB(nil, nil, true), + dm: dm, + } + args := &ArgV1ProcessEvent{ + Flags: []string{utils.MetaChargers}, + CGREvent: utils.CGREvent{ + ID: "TestV1ProcessEventNoTenant", + Event: map[string]interface{}{ + utils.CGRID: "test1", + utils.RunID: utils.MetaDefault, + utils.OriginID: "testV1CDRsRefundOutOfSessionCost", + utils.RequestType: utils.MetaPrepaid, + utils.AccountField: "testV1CDRsRefundOutOfSessionCost", + utils.Destination: "+4986517174963", + utils.AnswerTime: time.Date(2019, 11, 27, 12, 21, 26, 0, time.UTC), + utils.Usage: 123 * time.Minute, + }, + }, + } + var reply string + + if err := cdrs.V1ProcessEvent(args, &reply); err != nil { + t.Error(err) + } +} + +func TestCDRSV1V1ProcessExternalCDRNoTenant(t *testing.T) { + cfg := config.NewDefaultCGRConfig() + cfg.CdrsCfg().ChargerSConns = []string{utils.ConcatenatedKey(utils.MetaInternal, utils.MetaChargers)} + clMock := clMock(func(_ string, args interface{}, reply interface{}) error { + rply, cancast := reply.(*[]*ChrgSProcessEventReply) + if !cancast { + return fmt.Errorf("can't cast") + } + newArgs, cancast := args.(*utils.CGREvent) + if !cancast { + return fmt.Errorf("can't cast") + } + if newArgs.Tenant == utils.EmptyString { + return fmt.Errorf("Tenant is missing") + } + *rply = []*ChrgSProcessEventReply{} + return nil + }) + chanClnt := make(chan rpcclient.ClientConnector, 1) + chanClnt <- clMock + connMngr := NewConnManager(cfg, map[string]chan rpcclient.ClientConnector{ + utils.ConcatenatedKey(utils.MetaInternal, utils.MetaChargers): chanClnt, + }) + db := NewInternalDB(nil, nil, true) + dm := NewDataManager(db, cfg.CacheCfg(), connMngr) + cdrs := &CDRServer{ + cgrCfg: cfg, + connMgr: connMngr, + cdrDb: NewInternalDB(nil, nil, true), + dm: dm, + } + + args := &ExternalCDRWithAPIOpts{ + ExternalCDR: &ExternalCDR{ + ToR: utils.MetaVoice, + OriginID: "testDspCDRsProcessExternalCDR", + OriginHost: "127.0.0.1", + Source: utils.UnitTest, + RequestType: utils.MetaRated, + Tenant: "cgrates.org", + Category: "call", + Account: "1003", + Subject: "1003", + Destination: "1001", + SetupTime: "2014-08-04T13:00:00Z", + AnswerTime: "2014-08-04T13:00:07Z", + Usage: "1s", + ExtraFields: map[string]string{"field_extr1": "val_extr1", "fieldextr2": "valextr2"}, + }, + } + var reply string + + if err := cdrs.V1ProcessExternalCDR(args, &reply); err != nil { + t.Error(err) + } +} + diff --git a/utils/consts.go b/utils/consts.go index 50bab1c59..f8287541d 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -1280,6 +1280,7 @@ const ( APIerSv1ComputeFilterIndexIDs = "APIerSv1.ComputeFilterIndexIDs" APIerSv1GetAccountActionPlansIndexHealth = "APIerSv1.GetAccountActionPlansIndexHealth" APIerSv1GetReverseDestinationsIndexHealth = "APIerSv1.GetReverseDestinationsIndexHealth" + APIerSv1GetThresholdsIndexesHealth = "APIerSv1.GetThresholdsIndexesHealth" APIerSv1Ping = "APIerSv1.Ping" APIerSv1SetDispatcherProfile = "APIerSv1.SetDispatcherProfile" APIerSv1GetDispatcherProfile = "APIerSv1.GetDispatcherProfile"