//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/ericlagergren/decimal"

	"github.com/cgrates/birpc/context"
	"github.com/cgrates/cgrates/config"
	"github.com/cgrates/cgrates/utils"
)

var (
	rdsITdb   *RedisStorage
	mgoITdb   *MongoStorage
	onStor    *DataManager
	onStorCfg string

	// subtests to be executed for each confDIR
	sTestsOnStorIT = []func(t *testing.T){
		testOnStorITFlush,
		testOnStorITIsDBEmpty,
		// ToDo: test cache flush for a prefix
		// ToDo: testOnStorITLoadAccountingCache
		testOnStorITResource,
		testOnStorITResourceProfile,
		//testOnStorITCRUDHistory,
		testOnStorITCRUDStructVersion,
		testOnStorITStatQueueProfile,
		testOnStorITStatQueue,
		testOnStorITThresholdProfile,
		testOnStorITThreshold,
		testOnStorITFilter,
		testOnStorITRouteProfile,
		testOnStorITAttributeProfile,
		testOnStorITFlush,
		testOnStorITIsDBEmpty,
		testOnStorITTestAttributeSubstituteIface,
		testOnStorITChargerProfile,
		testOnStorITRateProfile,
		testOnStorITActionProfile,
		testOnStorITAccount,
		//testOnStorITCacheActionTriggers,
		//testOnStorITCRUDActionTriggers,
	}
)

func TestOnStorIT(t *testing.T) {
	cfg := config.NewDefaultCGRConfig()
	switch *utils.DBType {
	case utils.MetaInternal:
		idb, err := NewInternalDB(nil, nil, nil, cfg.DbCfg().Items)
		if err != nil {
			t.Fatal(err)
		}
		dbCM := NewDBConnManager(map[string]DataDB{utils.MetaDefault: idb}, cfg.DbCfg())
		onStor = NewDataManager(dbCM, cfg, nil)
	case utils.MetaRedis:
		cfg := config.NewDefaultCGRConfig()
		var err error
		rdsITdb, err = NewRedisStorage("127.0.0.1:6379",
			4, utils.CGRateSLwr, cfg.DbCfg().DBConns[utils.MetaDefault].Password, cfg.GeneralCfg().DBDataEncoding,
			cfg.DbCfg().DBConns[utils.MetaDefault].Opts.RedisMaxConns, cfg.DbCfg().DBConns[utils.MetaDefault].Opts.RedisConnectAttempts, "", false,
			0, 0, 0, 0, 0, 150*time.Microsecond, 0, false, utils.EmptyString, utils.EmptyString, utils.EmptyString, nil, nil)
		if err != nil {
			t.Fatal("Could not connect to Redis", err.Error())
		}
		onStorCfg = cfg.DbCfg().DBConns[utils.MetaDefault].Name
		dbCM := NewDBConnManager(map[string]DataDB{utils.MetaDefault: rdsITdb}, cfg.DbCfg())
		onStor = NewDataManager(dbCM, cfg, nil)
	case utils.MetaMySQL:
		t.SkipNow()
	case utils.MetaPostgres, utils.MetaMongo:
		t.SkipNow()
	default:
		t.Fatal("Unknown Database type")
	}

	for _, stest := range sTestsOnStorIT {
		t.Run(*utils.DBType, stest)
	}
}

func testOnStorITFlush(t *testing.T) {
	if err := onStor.DataDB()[utils.MetaDefault].Flush(""); err != nil {
		t.Error(err)
	}
	Cache.Clear(nil)
}

func testOnStorITIsDBEmpty(t *testing.T) {
	test, err := onStor.DataDB()[utils.MetaDefault].IsDBEmpty()
	if err != nil {
		t.Error(err)
	} else if test != true {
		t.Errorf("Expecting: true got :%+v", test)
	}
}

func testOnStorITResourceProfile(t *testing.T) {
	rL := &utils.ResourceProfile{
		Tenant: "cgrates.org",
		ID:     "RL_TEST2",
		Weights: utils.DynamicWeights{
			{
				Weight: 10,
			}},
		FilterIDs:    []string{"*string:~*req.Account:1001", "*ai:~*req.AnswerTime:2014-07-03T13:43:00Z|2014-07-03T13:44:00Z"},
		Limit:        1,
		ThresholdIDs: []string{"TEST_ACTIONS"},
		UsageTTL:     3 * time.Nanosecond,
	}
	if _, rcvErr := onStor.GetResourceProfile(context.TODO(), rL.Tenant, rL.ID,
		true, false, utils.NonTransactional); rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	if err := onStor.SetResourceProfile(context.TODO(), rL, false); err != nil {
		t.Error(err)
	}
	//get from database
	if rcv, err := onStor.GetResourceProfile(context.TODO(), rL.Tenant, rL.ID,
		false, true, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(rL, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(rL), utils.ToJSON(rcv))
	}
	expectedR := []string{"rsp_cgrates.org:RL_TEST2"}
	if itm, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.ResourceProfilesPrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expectedR, itm) {
		t.Errorf("Expected : %+v, but received %+v", expectedR, itm)
	}
	//update
	rL.ThresholdIDs = []string{"TH1", "TH2"}
	if err := onStor.SetResourceProfile(context.TODO(), rL, false); err != nil {
		t.Error(err)
	}

	//get from database
	if rcv, err := onStor.GetResourceProfile(context.TODO(), rL.Tenant, rL.ID,
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(rL, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(rL), utils.ToJSON(rcv))
	}

	if err := onStor.RemoveResourceProfile(context.TODO(), rL.Tenant, rL.ID,
		false); err != nil {
		t.Error(err)
	}
	//check database if removed
	if _, rcvErr := onStor.GetResourceProfile(context.TODO(), rL.Tenant, rL.ID,
		false, false, utils.NonTransactional); rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
}

func testOnStorITResource(t *testing.T) {
	res := &utils.Resource{
		Tenant: "cgrates.org",
		ID:     "RL1",
		Usages: map[string]*utils.ResourceUsage{
			"RU1": {
				ID:         "RU1",
				ExpiryTime: time.Date(2014, 7, 3, 13, 43, 0, 0, time.UTC),
				Units:      2,
			},
		},
		TTLIdx: []string{"RU1"},
	}
	if _, rcvErr := onStor.GetResource(context.TODO(), res.Tenant, res.ID,
		true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	if err := onStor.SetResource(context.TODO(), res); err != nil {
		t.Error(err)
	}
	//get from database
	if rcv, err := onStor.GetResource(context.TODO(), "cgrates.org", "RL1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(res, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(res), utils.ToJSON(rcv))
	}
	expectedT := []string{"res_cgrates.org:RL1"}
	if itm, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.ResourcesPrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expectedT, itm) {
		t.Errorf("Expected : %+v, but received %+v", expectedT, itm)
	}
	//update
	res.TTLIdx = []string{"RU1", "RU2"}
	if err := onStor.SetResource(context.TODO(), res); err != nil {
		t.Error(err)
	}

	//get from database
	if rcv, err := onStor.GetResource(context.TODO(), "cgrates.org", "RL1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(res, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(res), utils.ToJSON(rcv))
	}

	if err := onStor.RemoveResource(context.TODO(), res.Tenant, res.ID); err != nil {
		t.Error(err)
	}
	//check database if removed
	if _, rcvErr := onStor.GetResource(context.TODO(), res.Tenant, res.ID,
		false, false, utils.NonTransactional); rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
}

func testOnStorITCRUDHistory(t *testing.T) {
	time := time.Date(2012, 1, 1, 0, 0, 0, 0, time.UTC)
	ist := &utils.LoadInstance{
		LoadID:           "Load",
		RatingLoadID:     "RatingLoad",
		AccountingLoadID: "Account",
		LoadTime:         time,
	}
	if err := onStor.DataDB()[utils.MetaDefault].AddLoadHistory(ist, 1, utils.NonTransactional); err != nil {
		t.Error(err)
	}
	if rcv, err := onStor.DataDB()[utils.MetaDefault].GetLoadHistory(1, true, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(ist, rcv[0]) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(ist), utils.ToJSON(rcv[0]))
	}
}

func testOnStorITCRUDStructVersion(t *testing.T) {
	if _, err := onStor.DataDB()[utils.MetaDefault].GetVersions(utils.AccountsStr); err != utils.ErrNotFound {
		t.Error(err)
	}
	vrs := Versions{
		utils.AccountsStr: 3,
		utils.Actions:     2,
		utils.CostDetails: 1,
	}
	if err := onStor.DataDB()[utils.MetaDefault].SetVersions(vrs, false); err != nil {
		t.Error(err)
	}
	if rcv, err := onStor.DataDB()[utils.MetaDefault].GetVersions(""); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(vrs, rcv) {
		t.Errorf("Expecting: %v, received: %v", vrs, rcv)
	}
	delete(vrs, utils.Actions)
	if err := onStor.DataDB()[utils.MetaDefault].SetVersions(vrs, true); err != nil { // overwrite
		t.Error(err)
	}
	if rcv, err := onStor.DataDB()[utils.MetaDefault].GetVersions(""); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(vrs, rcv) {
		t.Errorf("Expecting: %v, received: %v", vrs, rcv)
	}
	eAcnts := Versions{utils.AccountsStr: vrs[utils.AccountsStr]}
	if rcv, err := onStor.DataDB()[utils.MetaDefault].GetVersions(utils.AccountsStr); err != nil { //query one element
		t.Error(err)
	} else if !reflect.DeepEqual(eAcnts, rcv) {
		t.Errorf("Expecting: %v, received: %v", eAcnts, rcv)
	}
	if _, err := onStor.DataDB()[utils.MetaDefault].GetVersions(utils.NotAvailable); err != utils.ErrNotFound { //query non-existent
		t.Error(err)
	}
	eAcnts[utils.AccountsStr] = 2
	vrs[utils.AccountsStr] = eAcnts[utils.AccountsStr]
	if err := onStor.DataDB()[utils.MetaDefault].SetVersions(eAcnts, false); err != nil { // change one element
		t.Error(err)
	}
	if rcv, err := onStor.DataDB()[utils.MetaDefault].GetVersions(""); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(vrs, rcv) {
		t.Errorf("Expecting: %v, received: %v", vrs, rcv)
	}
	if err := onStor.DataDB()[utils.MetaDefault].RemoveVersions(eAcnts); err != nil { // remove one element
		t.Error(err)
	}
	delete(vrs, utils.AccountsStr)
	if rcv, err := onStor.DataDB()[utils.MetaDefault].GetVersions(""); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(vrs, rcv) {
		t.Errorf("Expecting: %v, received: %v", vrs, rcv)
	}
	if err := onStor.DataDB()[utils.MetaDefault].RemoveVersions(nil); err != nil { // remove one element
		t.Error(err)
	}
	if _, err := onStor.DataDB()[utils.MetaDefault].GetVersions(""); err != utils.ErrNotFound { //query non-existent
		t.Error(err)
	}
}

func testOnStorITStatQueueProfile(t *testing.T) {
	sq := &StatQueueProfile{
		Tenant:       "cgrates.org",
		ID:           "test",
		FilterIDs:    []string{"*string:~*req.Account:1001"},
		QueueLength:  2,
		TTL:          0,
		Stored:       true,
		ThresholdIDs: []string{"Thresh1"},
	}
	if _, rcvErr := onStor.GetStatQueueProfile(context.TODO(), sq.Tenant, sq.ID,
		true, false, utils.NonTransactional); rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	if err := onStor.SetStatQueueProfile(context.TODO(), sq, false); err != nil {
		t.Error(err)
	}
	//get from database
	if rcv, err := onStor.GetStatQueueProfile(context.TODO(), sq.Tenant,
		sq.ID, false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(sq, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(sq), utils.ToJSON(rcv))
	}
	expectedR := []string{"sqp_cgrates.org:test"}
	if itm, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.StatQueueProfilePrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expectedR, itm) {
		t.Errorf("Expected : %+v, but received %+v", expectedR, itm)
	}
	//update
	sq.ThresholdIDs = []string{"TH1", "TH2"}
	if err := onStor.SetStatQueueProfile(context.TODO(), sq, false); err != nil {
		t.Error(err)
	}

	//get from database
	if rcv, err := onStor.GetStatQueueProfile(context.TODO(), sq.Tenant,
		sq.ID, false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(sq, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(sq), utils.ToJSON(rcv))
	}
	if err := onStor.RemoveStatQueueProfile(context.TODO(), sq.Tenant, sq.ID,
		false); err != nil {
		t.Error(err)
	}
	//check database if removed
	if _, rcvErr := onStor.GetStatQueueProfile(context.TODO(), sq.Tenant,
		sq.ID, true, false, utils.NonTransactional); rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
}

func testOnStorITStatQueue(t *testing.T) {
	eTime := utils.TimePointer(time.Date(2013, 10, 1, 0, 0, 0, 0, time.UTC))
	sq := &StatQueue{
		Tenant: "cgrates.org",
		ID:     "Test_StatQueue",
		SQItems: []SQItem{
			{EventID: "cgrates.org:ev1", ExpiryTime: eTime},
			{EventID: "cgrates.org:ev2", ExpiryTime: eTime},
			{EventID: "cgrates.org:ev3", ExpiryTime: eTime},
		},
		SQMetrics: map[string]StatMetric{
			utils.MetaASR: &StatASR{
				Metric: &Metric{
					Value: utils.NewDecimal(2, 0),
					Count: 3,
					Events: map[string]*DecimalWithCompress{
						"cgrates.org:ev1": {Stat: utils.NewDecimal(1, 0)},
						"cgrates.org:ev2": {Stat: utils.NewDecimal(1, 0)},
						"cgrates.org:ev3": {Stat: utils.NewDecimal(0, 0)},
					},
				},
			},
		},
	}
	if _, rcvErr := onStor.GetStatQueue(context.TODO(), sq.Tenant, sq.ID,
		true, false, utils.NonTransactional); rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	if err := onStor.SetStatQueue(context.TODO(), sq); err != nil {
		t.Error(err)
	}
	//get from database
	if rcv, err := onStor.GetStatQueue(context.TODO(), sq.Tenant,
		sq.ID, false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(sq, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(sq), utils.ToJSON(rcv))
	}
	expectedT := []string{"stq_cgrates.org:Test_StatQueue"}
	if itm, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.StatQueuePrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expectedT, itm) {
		t.Errorf("Expected : %+v, but received %+v", expectedT, itm)
	}
	//update
	sq.SQMetrics = map[string]StatMetric{
		utils.MetaASR: &StatASR{
			Metric: &Metric{
				Value: utils.NewDecimal(3, 0),
				Count: 3,
				Events: map[string]*DecimalWithCompress{
					"cgrates.org:ev1": {Stat: utils.NewDecimal(1, 0)},
					"cgrates.org:ev2": {Stat: utils.NewDecimal(1, 0)},
					"cgrates.org:ev3": {Stat: utils.NewDecimal(1, 0)},
				},
			},
		},
	}
	if err := onStor.SetStatQueue(context.TODO(), sq); err != nil {
		t.Error(err)
	}

	//get from database
	if rcv, err := onStor.GetStatQueue(context.TODO(), sq.Tenant,
		sq.ID, false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(sq, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(sq), utils.ToJSON(rcv))
	}
	if err := onStor.RemoveStatQueue(context.TODO(), sq.Tenant, sq.ID); err != nil {
		t.Error(err)
	}
	//check database if removed
	if _, rcvErr := onStor.GetStatQueue(context.TODO(), sq.Tenant,
		sq.ID, false, false, utils.NonTransactional); rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
}

func testOnStorITThresholdProfile(t *testing.T) {
	fp := &Filter{
		Tenant: "cgrates.org",
		ID:     "TestFilter2",
		Rules: []*FilterRule{
			{
				Element: "Account",
				Type:    utils.MetaString,
				Values:  []string{"1001", "1002"},
			},
		},
	}
	th := &ThresholdProfile{
		Tenant:    "cgrates.org",
		ID:        "test",
		FilterIDs: []string{"TestFilter2"},
		MaxHits:   12,
		MinSleep:  0,
		Blocker:   true,
		Weights: utils.DynamicWeights{
			{
				Weight: 1.4,
			},
		},
		ActionProfileIDs: []string{"Action1"},
	}
	if err := onStor.SetFilter(context.TODO(), fp, true); err != nil {
		t.Error(err)
	}
	if _, rcvErr := onStor.GetThresholdProfile(context.TODO(), th.Tenant, th.ID,
		true, false, utils.NonTransactional); rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	if err := onStor.SetThresholdProfile(context.TODO(), th, true); err != nil {
		t.Error(err)
	}
	//get from database
	if rcv, err := onStor.GetThresholdProfile(context.TODO(), th.Tenant, th.ID,
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(th, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(th), utils.ToJSON(rcv))
	}
	expectedR := []string{"thp_cgrates.org:test"}
	if itm, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.ThresholdProfilePrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expectedR, itm) {
		t.Errorf("Expected : %+v, but received %+v", expectedR, itm)
	}
	//update
	th.ActionProfileIDs = []string{"Action1", "Action2"}
	if err := onStor.SetThresholdProfile(context.TODO(), th, true); err != nil {
		t.Error(err)
	}

	//get from database
	if rcv, err := onStor.GetThresholdProfile(context.TODO(), th.Tenant, th.ID,
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(th, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(th), utils.ToJSON(rcv))
	}
	if err := onStor.RemoveThresholdProfile(context.TODO(), th.Tenant,
		th.ID, false); err != nil {
		t.Error(err)
	}
	//check database if removed
	if _, rcvErr := onStor.GetThresholdProfile(context.TODO(), th.Tenant,
		th.ID, false, false, utils.NonTransactional); rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
}

func testOnStorITThreshold(t *testing.T) {
	th := &Threshold{
		Tenant: "cgrates.org",
		ID:     "TH1",
		Snooze: time.Date(2016, 10, 1, 0, 0, 0, 0, time.UTC),
		Hits:   10,
	}
	if _, rcvErr := onStor.GetThreshold(context.TODO(), "cgrates.org", "TH1",
		true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	if err := onStor.SetThreshold(context.TODO(), th); err != nil {
		t.Error(err)
	}
	//get from database
	if rcv, err := onStor.GetThreshold(context.TODO(), "cgrates.org", "TH1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(th, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(th), utils.ToJSON(rcv))
	}
	expectedT := []string{"thd_cgrates.org:TH1"}
	if itm, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.ThresholdPrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expectedT, itm) {
		t.Errorf("Expected : %+v, but received %+v", expectedT, itm)
	}
	//update
	th.Hits = 20
	if err := onStor.SetThreshold(context.TODO(), th); err != nil {
		t.Error(err)
	}

	//get from database
	if rcv, err := onStor.GetThreshold(context.TODO(), "cgrates.org", "TH1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(th, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(th), utils.ToJSON(rcv))
	}
	if err := onStor.RemoveThreshold(context.TODO(), th.Tenant, th.ID); err != nil {
		t.Error(err)
	}
	//check database if removed
	if _, rcvErr := onStor.GetThreshold(context.TODO(), th.Tenant, th.ID,
		false, false, utils.NonTransactional); rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
}

func testOnStorITFilter(t *testing.T) {
	fp := &Filter{
		Tenant: "cgrates.org",
		ID:     "Filter1",
		Rules: []*FilterRule{
			{
				Element: "Account",
				Type:    utils.MetaString,
				Values:  []string{"1001", "1002"},
			},
		},
	}
	if err := fp.Compile(); err != nil {
		t.Fatal(err)
	}
	if _, rcvErr := onStor.GetFilter(context.TODO(), "cgrates.org", "Filter1",
		true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	if err := onStor.SetFilter(context.TODO(), fp, true); err != nil {
		t.Error(err)
	}
	//get from cache
	if rcv, err := onStor.GetFilter(context.TODO(), "cgrates.org", "Filter1",
		true, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(fp, rcv) {
		t.Errorf("Expecting: %v, received: %v", fp, rcv)
	}
	//get from database
	if rcv, err := onStor.GetFilter(context.TODO(), "cgrates.org", "Filter1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(fp, rcv) {
		t.Errorf("Expecting: %v, received: %v", fp, rcv)
	}
	expectedT := []string{"ftr_cgrates.org:Filter1", "ftr_cgrates.org:TestFilter2"}
	if itm, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.FilterPrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(len(expectedT), len(itm)) {
		t.Errorf("Expected : %+v, but received %+v", len(expectedT), len(itm))
	}
	//update
	fp.Rules = []*FilterRule{
		{
			Element: "Account",
			Type:    utils.MetaString,
			Values:  []string{"1001", "1002"},
		},
		{
			Element: "Destination",
			Type:    utils.MetaString,
			Values:  []string{"10", "20"},
		},
	}
	if err := fp.Compile(); err != nil {
		t.Fatal(err)
	}
	if err := onStor.SetFilter(context.TODO(), fp, true); err != nil {
		t.Error(err)
	}

	//get from cache
	if rcv, err := onStor.GetFilter(context.TODO(), "cgrates.org", "Filter1",
		true, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(fp, rcv) {
		t.Errorf("Expecting: %v, received: %v", fp, rcv)
	}
	//get from database
	if rcv, err := onStor.GetFilter(context.TODO(), "cgrates.org", "Filter1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(fp, rcv) {
		t.Errorf("Expecting: %v, received: %v", fp, rcv)
	}
	if err := onStor.RemoveFilter(context.TODO(), fp.Tenant, fp.ID, true); err != nil {
		t.Error(err)
	}
	//check cache if removed
	if _, rcvErr := onStor.GetFilter(context.TODO(), "cgrates.org", "Filter1",
		true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	//check database if removed
	if _, rcvErr := onStor.GetFilter(context.TODO(), "cgrates.org", "Filter1",
		false, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
}

func testOnStorITRouteProfile(t *testing.T) {
	splProfile := &utils.RouteProfile{
		Tenant:            "cgrates.org",
		ID:                "SPRF_1",
		FilterIDs:         []string{"*string:~*reg.Accout:1002", "*string:~*reg.Destination:11", "*ai:~*req.AnswerTime:2014-07-14T14:25:00Z"},
		Sorting:           "*lowest_cost",
		SortingParameters: []string{},
		Routes: []*utils.Route{
			{
				ID:              "supplier1",
				FilterIDs:       []string{"FLTR_DST_DE"},
				AccountIDs:      []string{"Account1"},
				RateProfileIDs:  []string{"RPL_1"},
				ResourceIDs:     []string{"ResGR1"},
				StatIDs:         []string{"Stat1"},
				Weights:         utils.DynamicWeights{{Weight: 10}},
				RouteParameters: "param1",
			},
		},
		Weights: utils.DynamicWeights{{Weight: 20}},
	}
	if _, rcvErr := onStor.GetRouteProfile(context.Background(), "cgrates.org", "SPRF_1",
		true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	if err := onStor.SetRouteProfile(context.Background(), splProfile, false); err != nil {
		t.Error(err)
	}
	//get from cache
	if rcv, err := onStor.GetRouteProfile(context.Background(), "cgrates.org", "SPRF_1",
		true, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(splProfile, rcv) {
		t.Errorf("Expecting: %v, received: %v", splProfile, rcv)
	}
	//get from database
	if rcv, err := onStor.GetRouteProfile(context.Background(), "cgrates.org", "SPRF_1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(splProfile, rcv) {
		t.Errorf("Expecting: %v, received: %v", splProfile, rcv)
	}
	expectedT := []string{"rpp_cgrates.org:SPRF_1"}
	if itm, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.RouteProfilePrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expectedT, itm) {
		t.Errorf("Expected : %+v, but received %+v", expectedT, itm)
	}
	//update
	splProfile.Routes = []*utils.Route{
		{
			ID:              "supplier1",
			FilterIDs:       []string{"FLTR_DST_DE"},
			AccountIDs:      []string{"Account1"},
			RateProfileIDs:  []string{"RPL_1"},
			ResourceIDs:     []string{"ResGR1"},
			StatIDs:         []string{"Stat1"},
			Weights:         utils.DynamicWeights{{Weight: 10}},
			RouteParameters: "param1",
		},
		{
			ID:              "supplier2",
			FilterIDs:       []string{"FLTR_DST_DE"},
			AccountIDs:      []string{"Account2"},
			RateProfileIDs:  []string{"RPL_2"},
			ResourceIDs:     []string{"ResGR2"},
			StatIDs:         []string{"Stat2"},
			Weights:         utils.DynamicWeights{{Weight: 20}},
			RouteParameters: "param2",
		},
	}
	if err := onStor.SetRouteProfile(context.Background(), splProfile, false); err != nil {
		t.Error(err)
	}

	//get from cache
	if rcv, err := onStor.GetRouteProfile(context.Background(), "cgrates.org", "SPRF_1",
		true, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(splProfile, rcv) {
		t.Errorf("Expecting: %v, received: %v", splProfile, rcv)
	}
	//get from database
	if rcv, err := onStor.GetRouteProfile(context.Background(), "cgrates.org", "SPRF_1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(splProfile, rcv) {
		t.Errorf("Expecting: %v, received: %v", splProfile, rcv)
	}
	if err := onStor.RemoveRouteProfile(context.Background(), splProfile.Tenant, splProfile.ID,
		false); err != nil {
		t.Error(err)
	}
	//check cache if removed
	if _, rcvErr := onStor.GetRouteProfile(context.Background(), "cgrates.org", "SPRF_1",
		true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	//check database if removed
	if _, rcvErr := onStor.GetRouteProfile(context.Background(), "cgrates.org", "SPRF_1",
		false, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
}

func testOnStorITAttributeProfile(t *testing.T) {
	attrProfile := &utils.AttributeProfile{
		Tenant:    "cgrates.org",
		ID:        "AttrPrf1",
		FilterIDs: []string{"*string:~*reg.Accout:1002", "*string:~*reg.Destination:11", "*ai:~*req.AnswerTime:2014-07-14T14:25:00Z", "*string:~*opts.*context:con1"},
		Attributes: []*utils.Attribute{
			{
				Path:  utils.MetaReq + utils.NestingSep + "FN1",
				Value: utils.NewRSRParsersMustCompile("Al1", utils.InfieldSep),
			},
		},
		Weights: utils.DynamicWeights{
			{
				Weight: 20,
			},
		},
	}
	if _, rcvErr := onStor.GetAttributeProfile(context.TODO(), "cgrates.org", "AttrPrf1",
		true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	if err := onStor.SetAttributeProfile(context.TODO(), attrProfile, false); err != nil {
		t.Error(err)
	}
	//get from cache
	if rcv, err := onStor.GetAttributeProfile(context.TODO(), "cgrates.org", "AttrPrf1",
		true, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(attrProfile, rcv) {
		t.Errorf("Expecting: %v, received: %v", attrProfile, rcv)
	}
	//get from database
	if rcv, err := onStor.GetAttributeProfile(context.TODO(), "cgrates.org", "AttrPrf1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(attrProfile, rcv) {
		t.Errorf("Expecting: %v, received: %v", attrProfile, rcv)
	}
	expectedT := []string{"alp_cgrates.org:AttrPrf1"}
	if itm, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.AttributeProfilePrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expectedT, itm) {
		t.Errorf("Expected : %+v, but received %+v", expectedT, itm)
	}
	//update
	if err := onStor.SetAttributeProfile(context.TODO(), attrProfile, false); err != nil {
		t.Error(err)
	}

	//get from cache
	if rcv, err := onStor.GetAttributeProfile(context.TODO(), "cgrates.org", "AttrPrf1",
		true, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(attrProfile, rcv) {
		t.Errorf("Expecting: %v, received: %v", attrProfile, rcv)
	}
	//get from database
	if rcv, err := onStor.GetAttributeProfile(context.TODO(), "cgrates.org", "AttrPrf1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(attrProfile, rcv) {
		t.Errorf("Expecting: %v, received: %v", attrProfile, rcv)
	}
	if err := onStor.RemoveAttributeProfile(context.TODO(), attrProfile.Tenant,
		attrProfile.ID, false); err != nil {
		t.Error(err)
	}
	//check cache if removed
	if _, rcvErr := onStor.GetAttributeProfile(context.TODO(), "cgrates.org", "AttrPrf1",
		true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	//check database if removed
	if _, rcvErr := onStor.GetAttributeProfile(context.TODO(), "cgrates.org", "AttrPrf1",
		false, true, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
}

func testOnStorITTestAttributeSubstituteIface(t *testing.T) {
	attrProfile := &utils.AttributeProfile{
		Tenant:    "cgrates.org",
		ID:        "AttrPrf1",
		FilterIDs: []string{"*string:~*reg.Accout:1002", "*string:~*reg.Destination:11", "*ai:~*req.AnswerTime:2014-07-14T14:25:00Z", "*string:~*opts.*context:con1"},
		Attributes: []*utils.Attribute{
			{
				Path:  utils.MetaReq + utils.NestingSep + "FN1",
				Value: utils.NewRSRParsersMustCompile("Val1", utils.InfieldSep),
			},
		},
		Weights: utils.DynamicWeights{
			{
				Weight: 20,
			},
		},
	}
	if _, rcvErr := onStor.GetAttributeProfile(context.TODO(), "cgrates.org", "AttrPrf1",
		true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	if err := onStor.SetAttributeProfile(context.TODO(), attrProfile, false); err != nil {
		t.Error(err)
	}
	//check database
	if rcv, err := onStor.GetAttributeProfile(context.TODO(), "cgrates.org", "AttrPrf1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(attrProfile, rcv) {
		t.Errorf("Expecting: %v, received: %v", attrProfile, rcv)
	}
	attrProfile.Attributes = []*utils.Attribute{
		{
			Path:  utils.MetaReq + utils.NestingSep + "FN1",
			Value: utils.NewRSRParsersMustCompile("123.123", utils.InfieldSep),
		},
	}
	if err := onStor.SetAttributeProfile(context.TODO(), attrProfile, false); err != nil {
		t.Error(err)
	}
	//check database
	if rcv, err := onStor.GetAttributeProfile(context.TODO(), "cgrates.org", "AttrPrf1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(attrProfile, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(attrProfile), utils.ToJSON(rcv))
	}
	attrProfile.Attributes = []*utils.Attribute{
		{
			Path:  utils.MetaReq + utils.NestingSep + "FN1",
			Value: utils.NewRSRParsersMustCompile("true", utils.InfieldSep),
		},
	}
	if err := onStor.SetAttributeProfile(context.TODO(), attrProfile, false); err != nil {
		t.Error(err)
	}
	//check database
	if rcv, err := onStor.GetAttributeProfile(context.TODO(), "cgrates.org", "AttrPrf1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(attrProfile, rcv) {
		t.Errorf("Expecting: %v, received: %v", utils.ToJSON(attrProfile), utils.ToJSON(rcv))
	}
}

func testOnStorITChargerProfile(t *testing.T) {
	cpp := &utils.ChargerProfile{
		Tenant:       "cgrates.org",
		ID:           "CPP_1",
		FilterIDs:    []string{"*string:~*req.Account:1001", "*ai:~*req.AnswerTime:2014-07-14T14:25:00Z"},
		RunID:        "*rated",
		AttributeIDs: []string{"ATTR_1"},
		Weights: utils.DynamicWeights{
			{
				Weight: 20,
			},
		},
	}
	if _, rcvErr := onStor.GetChargerProfile(context.Background(), "cgrates.org", "CPP_1",
		true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	if err := onStor.SetChargerProfile(context.Background(), cpp, false); err != nil {
		t.Error(err)
	}
	//get from database
	if rcv, err := onStor.GetChargerProfile(context.Background(), "cgrates.org", "CPP_1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(cpp, rcv) {
		t.Errorf("Expecting: %v, received: %v", cpp, rcv)
	}
	expectedT := []string{"cpp_cgrates.org:CPP_1"}
	if itm, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.ChargerProfilePrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expectedT, itm) {
		t.Errorf("Expected : %+v, but received %+v", expectedT, itm)
	}
	//update
	cpp.FilterIDs = []string{"*string:~*req.Accout:1001", "*prefix:~*req.Destination:10"}
	if err := onStor.SetChargerProfile(context.Background(), cpp, false); err != nil {
		t.Error(err)
	}

	//get from database
	if rcv, err := onStor.GetChargerProfile(context.Background(), "cgrates.org", "CPP_1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(cpp, rcv) {
		t.Errorf("Expecting: %v, received: %v", cpp, rcv)
	}
	if err := onStor.RemoveChargerProfile(context.Background(), cpp.Tenant, cpp.ID,
		false); err != nil {
		t.Error(err)
	}
	//check database if removed
	if _, rcvErr := onStor.GetChargerProfile(context.Background(), "cgrates.org", "CPP_1",
		false, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
}

func testOnStorITRateProfile(t *testing.T) {
	rPrf := &utils.RateProfile{
		Tenant:    "cgrates.org",
		ID:        "RP1",
		FilterIDs: []string{"*string:~*req.Subject:1001", "*string:~*req.Subject:1002"},
		Weights: utils.DynamicWeights{
			{
				Weight: 0,
			},
		},
		MaxCostStrategy: "*free",
		Rates: map[string]*utils.Rate{
			"FIRST_GI": {
				ID:        "FIRST_GI",
				FilterIDs: []string{"*gi:~*req.Usage:0"},
				Weights: utils.DynamicWeights{
					{
						Weight: 0,
					},
				},
				Blocker: false,
			},
			"SECOND_GI": {
				ID:        "SECOND_GI",
				FilterIDs: []string{"*gi:~*req.Usage:1m"},
				Weights: utils.DynamicWeights{
					{
						Weight: 10,
					},
				},
				Blocker: false,
			},
		},
	}
	if _, rcvErr := onStor.GetRateProfile(context.TODO(), "cgrates.org", "RP1",
		true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
	if err := onStor.SetRateProfile(context.TODO(), rPrf, false, false); err != nil {
		t.Error(err)
	}
	if err := rPrf.Compile(); err != nil {
		t.Fatal(err)
	}
	//get from database
	if rcv, err := onStor.GetRateProfile(context.TODO(), "cgrates.org", "RP1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(rPrf, rcv) {
		t.Errorf("Expecting: %v, received: %v", rPrf, rcv)
	}
	expectedT := []string{"rtp_cgrates.org:RP1"}
	if itm, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.RateProfilePrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expectedT, itm) {
		t.Errorf("Expected : %+v, but received %+v", expectedT, itm)
	}
	//update
	rPrf.FilterIDs = []string{"*string:~*req.Accout:1001", "*prefix:~*req.Destination:10"}
	if err := onStor.SetRateProfile(context.TODO(), rPrf, false, false); err != nil {
		t.Error(err)
	}

	//get from database
	if rcv, err := onStor.GetRateProfile(context.TODO(), "cgrates.org", "RP1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(rPrf, rcv) {
		t.Errorf("Expecting: %v, received: %v", rPrf, rcv)
	}
	if err := onStor.RemoveRateProfile(context.TODO(), rPrf.Tenant, rPrf.ID,
		false); err != nil {
		t.Error(err)
	}
	//check database if removed
	if _, rcvErr := onStor.GetRateProfile(context.TODO(), "cgrates.org", "RP1",
		false, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound {
		t.Error(rcvErr)
	}
}

func testOnStorITActionProfile(t *testing.T) {
	actPrf := &utils.ActionProfile{
		Tenant:    "cgrates.org",
		ID:        "TEST_ID1",
		FilterIDs: []string{"*string:~*req.Account:1001"},
		Weights: utils.DynamicWeights{
			{
				Weight: 20,
			},
		},
		Schedule: utils.MetaASAP,
		Targets: map[string]utils.StringSet{
			utils.MetaAccounts: utils.NewStringSet([]string{"acc1", "acc2", "acc3"}),
		},
		Actions: []*utils.APAction{
			{
				ID:        "TOPUP",
				FilterIDs: []string{},
				Type:      "*topup",
				Diktats: []*utils.APDiktat{{
					Opts: map[string]any{
						"*balancePath": "~*balance.TestBalance.Value",
					},
				}},
			},
			{
				ID:        "TOPUP_TEST_VOICE",
				FilterIDs: []string{},
				Type:      "*topup",
				Diktats: []*utils.APDiktat{{
					Opts: map[string]any{
						"*balancePath": "~*balance.TestVoiceBalance.Value",
					},
				}},
			},
		},
	}

	//empty in database
	if _, err := onStor.GetActionProfile(context.TODO(), "cgrates.org", "TEST_ID1",
		true, false, utils.NonTransactional); err != utils.ErrNotFound {
		t.Error(err)
	}

	//get from database
	if err := onStor.SetActionProfile(context.TODO(), actPrf, false); err != nil {
		t.Error(err)
	}
	if rcv, err := onStor.GetActionProfile(context.TODO(), "cgrates.org", "TEST_ID1",
		true, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(rcv, actPrf) {
		t.Errorf("Expecting: %v, received: %v", actPrf, rcv)
	}

	//craft akeysFromPrefix
	expectedKey := []string{"acp_cgrates.org:TEST_ID1"}
	if rcv, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.ActionProfilePrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expectedKey, rcv) {
		t.Errorf("Expecting: %v, received: %v", expectedKey, rcv)
	}

	//updateFilters
	actPrf.FilterIDs = []string{"*prefix:~*req.Destination:10"}
	if err := onStor.SetActionProfile(context.TODO(), actPrf, false); err != nil {
		t.Error(err)
	} else if rcv, err := onStor.GetActionProfile(context.TODO(), "cgrates.org", "TEST_ID1",
		false, false, utils.NonTransactional); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(actPrf, rcv) {
		t.Errorf("Expecting: %v, received: %v", actPrf, rcv)
	}

	//remove from database
	if err := onStor.RemoveActionProfile(context.TODO(), "cgrates.org", "TEST_ID1",
		false); err != nil {
		t.Error(err)
	} else if _, err := onStor.GetActionProfile(context.TODO(), "cgrates.org", "TEST_ID1",
		false, false, utils.NonTransactional); err != utils.ErrNotFound {
		t.Error(err)
	}
}

func testOnStorITAccount(t *testing.T) {
	acctPrf := &utils.Account{
		Tenant:    "cgrates.org",
		ID:        "RP1",
		FilterIDs: []string{"test_filterId", "*ai:~*req.AnswerTime:2014-07-14T14:25:00Z|2014-07-15T14:25:00Z"},
		Weights: utils.DynamicWeights{
			{
				Weight: 2,
			},
		},
		Balances: map[string]*utils.Balance{
			"VoiceBalance": {
				ID:        "VoiceBalance",
				FilterIDs: []string{"FLTR_RES_GR2"},
				Weights: utils.DynamicWeights{
					{
						Weight: 10,
					},
				},
				Type: utils.MetaVoice,
				Units: &utils.Decimal{
					Big: decimal.WithContext(utils.DecimalContext).SetUint64(10),
				},
				Opts: map[string]any{
					"key1": "val1",
				},
			}},
		ThresholdIDs: []string{"test_thrs"},
	}

	//empty in database
	if _, err := onStor.GetAccount(context.TODO(), "cgrates.org", "RP1"); err != utils.ErrNotFound {
		t.Error(err)
	}

	//get from database
	if err := onStor.SetAccount(context.TODO(), acctPrf, false); err != nil {
		t.Error(err)
	}
	if rcv, err := onStor.GetAccount(context.TODO(), "cgrates.org", "RP1"); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(rcv, acctPrf) {
		t.Errorf("Expecting: %v, received: %v", acctPrf, rcv)
	}

	//craft akeysFromPrefix
	expectedKey := []string{"acn_cgrates.org:RP1"}
	if rcv, err := onStor.DataDB()[utils.MetaDefault].GetKeysForPrefix(context.TODO(), utils.AccountPrefix); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(expectedKey, rcv) {
		t.Errorf("Expecting: %v, received: %v", expectedKey, rcv)
	}

	//updateFilters
	acctPrf.FilterIDs = []string{"*prefix:~*req.Destination:10"}
	if err := onStor.SetAccount(context.TODO(), acctPrf, false); err != nil {
		t.Error(err)
	} else if rcv, err := onStor.GetAccount(context.TODO(), "cgrates.org", "RP1"); err != nil {
		t.Error(err)
	} else if !reflect.DeepEqual(acctPrf, rcv) {
		t.Errorf("Expecting: %v, received: %v", acctPrf, rcv)
	}

	//remove from database
	if err := onStor.RemoveAccount(context.TODO(), "cgrates.org", "RP1",
		false); err != nil {
		t.Error(err)
	} else if _, err := onStor.GetAccount(context.TODO(), "cgrates.org", "RP1"); err != utils.ErrNotFound {
		t.Error(err)
	}
}
