Files
cgrates/engine/resources_it_test.go
2025-10-29 19:42:24 +01:00

146 lines
4.6 KiB
Go

//go:build integration
// +build integration
/*
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 Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
*/
package engine
import (
"reflect"
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
// TestRSCacheSetGet assurace the presence of private params in cached resource
func TestRSCacheSetGet(t *testing.T) {
if *utils.DBType == utils.MetaInternal {
t.SkipNow()
}
r := &Resource{
Tenant: "cgrates.org",
ID: "RL",
rPrf: &ResourceProfile{
Tenant: "cgrates.org",
ID: "RL",
FilterIDs: []string{"FLTR_RES_RL"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC),
ExpiryTime: time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC),
},
AllocationMessage: "ALLOC_RL",
Weight: 50,
Limit: 2,
ThresholdIDs: []string{"TEST_ACTIONS"},
UsageTTL: time.Duration(1 * time.Millisecond),
},
Usages: map[string]*ResourceUsage{
"RU2": {
Tenant: "cgrates.org",
ID: "RU2",
ExpiryTime: time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC),
Units: 2,
},
},
tUsage: utils.Float64Pointer(2),
dirty: utils.BoolPointer(true),
}
Cache.Set(utils.CacheResources, r.TenantID(), r, nil, true, "")
if x, ok := Cache.Get(utils.CacheResources, r.TenantID()); !ok {
t.Error("not in cache")
} else if x == nil {
t.Error("nil resource")
} else if !reflect.DeepEqual(r, x.(*Resource)) {
t.Errorf("Expecting: %+v, received: %+v", r, x)
}
}
func TestResourceCaching(t *testing.T) {
if *utils.DBType == utils.MetaInternal {
t.SkipNow()
}
//clear the cache
Cache.Clear(nil)
// start fresh with new dataManager
defaultCfg, _ := config.NewDefaultCGRConfig()
data := NewInternalDB(nil, nil, true, defaultCfg.DataDbCfg().Items)
dmRES = NewDataManager(data, config.CgrConfig().CacheCfg(), nil)
defaultCfg.ResourceSCfg().StoreInterval = 1
defaultCfg.ResourceSCfg().StringIndexedFields = nil
defaultCfg.ResourceSCfg().PrefixIndexedFields = nil
resService, err = NewResourceService(dmRES, defaultCfg,
&FilterS{dm: dmRES, cfg: defaultCfg}, nil)
if err != nil {
t.Errorf("Error: %+v", err)
}
resProf := &ResourceProfile{
Tenant: "cgrates.org",
ID: "ResourceProfileCached",
FilterIDs: []string{"*string:~*req.Account:1001"},
ActivationInterval: &utils.ActivationInterval{
ActivationTime: time.Date(2014, 7, 14, 14, 25, 0, 0, time.UTC),
},
UsageTTL: time.Duration(-1),
Limit: 10.00,
AllocationMessage: "AllocationMessage",
Weight: 20.00,
ThresholdIDs: []string{utils.META_NONE},
}
Cache.Set(utils.CacheResourceProfiles, "cgrates.org:ResourceProfileCached",
resProf, nil, cacheCommit(utils.EmptyString), utils.EmptyString)
res := &Resource{Tenant: resProf.Tenant,
ID: resProf.ID,
Usages: make(map[string]*ResourceUsage)}
Cache.Set(utils.CacheResources, "cgrates.org:ResourceProfileCached",
res, nil, cacheCommit(utils.EmptyString), utils.EmptyString)
resources := Resources{res}
Cache.Set(utils.CacheEventResources, "TestResourceCaching", resources.resIDsMp(), nil, true, "")
ev := &utils.CGREvent{
Tenant: "cgrates.org",
ID: utils.UUIDSha1Prefix(),
Event: map[string]any{
"Account": "1001",
"Destination": "3002"},
}
mres, err := resService.matchingResourcesForEvent(ev,
"TestResourceCaching", nil)
if err != nil {
t.Errorf("Error: %+v", err)
}
mres.unlock()
if !reflect.DeepEqual(resources[0].Tenant, mres[0].Tenant) {
t.Errorf("Expecting: %+v, received: %+v", resources[0].Tenant, mres[0].Tenant)
} else if !reflect.DeepEqual(resources[0].ID, mres[0].ID) {
t.Errorf("Expecting: %+v, received: %+v", resources[0].ID, mres[0].ID)
} else if !reflect.DeepEqual(resources[0].rPrf, mres[0].rPrf) {
t.Errorf("Expecting: %+v, received: %+v", resources[0].rPrf, mres[0].rPrf)
} else if !reflect.DeepEqual(resources[0].ttl, mres[0].ttl) {
t.Errorf("Expecting: %+v, received: %+v", resources[0].ttl, mres[0].ttl)
}
}