diff --git a/utils/accountcharges.go b/utils/accountcharges.go deleted file mode 100644 index be2b7f8c9..000000000 --- a/utils/accountcharges.go +++ /dev/null @@ -1,19 +0,0 @@ -/* -Real-time Online/Offline Charging System (OerS) 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 utils diff --git a/utils/accountprofile.go b/utils/accountprofile.go deleted file mode 100644 index 7a6b01b0b..000000000 --- a/utils/accountprofile.go +++ /dev/null @@ -1,524 +0,0 @@ -/* -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 utils - -import ( - "sort" - "time" - - "github.com/ericlagergren/decimal" -) - -// AccountProfile represents one Account on a Tenant -type AccountProfile struct { - Tenant string - ID string // Account identificator, unique within the tenant - FilterIDs []string - ActivationInterval *ActivationInterval - Weights DynamicWeights - Opts map[string]interface{} - Balances map[string]*Balance - ThresholdIDs []string -} - -// BalancesAltered detects altering of the Balances by comparing the Balance values with the ones from backup -func (ap *AccountProfile) BalancesAltered(abb AccountBalancesBackup) (altred bool) { - if len(ap.Balances) != len(abb) { - return true - } - for blncID, blnc := range ap.Balances { - if bkpVal, has := abb[blncID]; !has { - return true - } else if blnc.Units.Big.Cmp(bkpVal) != 0 { - return true - } - } - return -} - -func (ap *AccountProfile) RestoreFromBackup(abb AccountBalancesBackup) { - for blncID, val := range abb { - ap.Balances[blncID].Units.Big = val - } -} - -// AccountBalancesBackup returns a backup of all balance values -func (ap *AccountProfile) AccountBalancesBackup() (abb AccountBalancesBackup) { - if ap.Balances != nil { - abb = make(AccountBalancesBackup) - for blncID, blnc := range ap.Balances { - abb[blncID] = new(decimal.Big).Copy(blnc.Units.Big) - } - } - return -} - -// AccountBalanceBackups is used to create balance snapshots as backups -type AccountBalancesBackup map[string]*decimal.Big - -// NewDefaultBalance returns a balance with default costIncrements -func NewDefaultBalance(id string) *Balance { - const torFltr = "*string:~*req.ToR:" - return &Balance{ - ID: id, - Type: MetaConcrete, - Units: NewDecimal(0, 0), - CostIncrements: []*CostIncrement{ - { - FilterIDs: []string{torFltr + MetaVoice}, - Increment: NewDecimal(int64(time.Second), 0), - RecurrentFee: NewDecimal(0, 0), - }, - { - FilterIDs: []string{torFltr + MetaData}, - Increment: NewDecimal(1024*1024, 0), - RecurrentFee: NewDecimal(0, 0), - }, - { - FilterIDs: []string{torFltr + MetaSMS}, - Increment: NewDecimal(1, 0), - RecurrentFee: NewDecimal(0, 0), - }, - }, - } -} - -// Balance represents one Balance inside an Account -type Balance struct { - ID string // Balance identificator, unique within an Account - FilterIDs []string - Weights DynamicWeights - Type string - Units *Decimal - UnitFactors []*UnitFactor - Opts map[string]interface{} - CostIncrements []*CostIncrement - AttributeIDs []string - RateProfileIDs []string -} - -// CostIncrement enforces cost calculation to specific balance increments -type CostIncrement struct { - FilterIDs []string - Increment *Decimal - FixedFee *Decimal - RecurrentFee *Decimal -} - -// Clone returns a copy of the CostIncrement -func (cI *CostIncrement) Clone() (cIcln *CostIncrement) { - cIcln = new(CostIncrement) - if cI.FilterIDs != nil { - cIcln.FilterIDs = make([]string, len(cI.FilterIDs)) - for i, fID := range cI.FilterIDs { - cIcln.FilterIDs[i] = fID - } - } - if cI.Increment != nil { - cIcln.Increment = cI.Increment.Clone() - } - if cI.FixedFee != nil { - cIcln.FixedFee = cI.FixedFee.Clone() - } - if cI.RecurrentFee != nil { - cIcln.RecurrentFee = cI.RecurrentFee.Clone() - } - return -} - -//Clone return a copy of the UnitFactor -func (uF *UnitFactor) Clone() (untFct *UnitFactor) { - untFct = new(UnitFactor) - if uF.FilterIDs != nil { - untFct.FilterIDs = make([]string, len(uF.FilterIDs)) - for i, value := range uF.FilterIDs { - untFct.FilterIDs[i] = value - } - } - if uF.Factor != nil { - untFct.Factor = uF.Factor.Clone() - } - return -} - -// UnitFactor is a multiplicator for the usage received -type UnitFactor struct { - FilterIDs []string - Factor *Decimal -} - -// Equals compares two UnitFactors -func (uF *UnitFactor) Equals(nUf *UnitFactor) (eq bool) { - if uF.FilterIDs == nil && nUf.FilterIDs != nil || - uF.FilterIDs != nil && nUf.FilterIDs == nil || - len(uF.FilterIDs) != len(nUf.FilterIDs) { - return - } - for i := range uF.FilterIDs { - if uF.FilterIDs[i] != nUf.FilterIDs[i] { - return - } - } - if uF.Factor == nil && nUf.Factor != nil || - uF.Factor != nil && nUf.Factor == nil { - return - } - if uF.Factor == nil && nUf.Factor == nil { - return true - } - return uF.Factor.Compare(nUf.Factor) == 0 -} - -// TenantID returns the combined Tenant:ID -func (aP *AccountProfile) TenantID() string { - return ConcatenatedKey(aP.Tenant, aP.ID) -} - -// Clone returns a clone of the Account -func (aP *AccountProfile) Clone() (acnt *AccountProfile) { - acnt = &AccountProfile{ - Tenant: aP.Tenant, - ID: aP.ID, - ActivationInterval: aP.ActivationInterval.Clone(), - Weights: aP.Weights.Clone(), - } - if aP.FilterIDs != nil { - acnt.FilterIDs = make([]string, len(aP.FilterIDs)) - for i, value := range aP.FilterIDs { - acnt.FilterIDs[i] = value - } - } - if aP.Opts != nil { - acnt.Opts = make(map[string]interface{}) - for key, value := range aP.Opts { - acnt.Opts[key] = value - } - } - if aP.Balances != nil { - acnt.Balances = make(map[string]*Balance, len(aP.Balances)) - for i, value := range aP.Balances { - acnt.Balances[i] = value.Clone() - } - } - if aP.ThresholdIDs != nil { - acnt.ThresholdIDs = make([]string, len(aP.ThresholdIDs)) - for i, value := range aP.ThresholdIDs { - acnt.ThresholdIDs[i] = value - } - } - return -} - -//Clone returns a clone of the ActivationInterval -func (aI *ActivationInterval) Clone() *ActivationInterval { - if aI == nil { - return nil - } - return &ActivationInterval{ - ActivationTime: aI.ActivationTime, - ExpiryTime: aI.ExpiryTime, - } -} - -//Clone return a clone of the Balance -func (bL *Balance) Clone() (blnc *Balance) { - blnc = &Balance{ - ID: bL.ID, - Weights: bL.Weights.Clone(), - Type: bL.Type, - } - if bL.FilterIDs != nil { - blnc.FilterIDs = make([]string, len(bL.FilterIDs)) - for i, value := range bL.FilterIDs { - blnc.FilterIDs[i] = value - } - } - if bL.Units != nil { - blnc.Units = bL.Units.Clone() - } - if bL.UnitFactors != nil { - blnc.UnitFactors = make([]*UnitFactor, len(bL.UnitFactors)) - for i, value := range bL.UnitFactors { - blnc.UnitFactors[i] = value.Clone() - } - } - if bL.Opts != nil { - blnc.Opts = make(map[string]interface{}) - for key, value := range bL.Opts { - blnc.Opts[key] = value - } - } - if bL.CostIncrements != nil { - blnc.CostIncrements = make([]*CostIncrement, len(bL.CostIncrements)) - for i, value := range bL.CostIncrements { - blnc.CostIncrements[i] = value.Clone() - } - } - if bL.AttributeIDs != nil { - blnc.AttributeIDs = make([]string, len(bL.AttributeIDs)) - for i, value := range bL.AttributeIDs { - blnc.AttributeIDs[i] = value - } - } - if bL.RateProfileIDs != nil { - blnc.RateProfileIDs = make([]string, len(bL.RateProfileIDs)) - for i, value := range bL.RateProfileIDs { - blnc.RateProfileIDs[i] = value - } - } - return -} - -// AccountProfileWithWeight attaches static weight to AccountProfile -type AccountProfileWithWeight struct { - *AccountProfile - Weight float64 - LockID string -} - -// AccountProfilesWithWeight is a sortable list of AccountProfileWithWeight -type AccountProfilesWithWeight []*AccountProfileWithWeight - -// Sort is part of sort interface, sort based on Weight -func (aps AccountProfilesWithWeight) Sort() { - sort.Slice(aps, func(i, j int) bool { return aps[i].Weight > aps[j].Weight }) -} - -// AccountProfiles returns the list of AccountProfiles -func (apWws AccountProfilesWithWeight) AccountProfiles() (aps []*AccountProfile) { - if apWws != nil { - aps = make([]*AccountProfile, len(apWws)) - for i, apWw := range apWws { - aps[i] = apWw.AccountProfile - } - } - return -} - -// LockIDs returns the list of LockIDs -func (apWws AccountProfilesWithWeight) LockIDs() (lkIDs []string) { - if apWws != nil { - lkIDs = make([]string, len(apWws)) - for i, apWw := range apWws { - lkIDs[i] = apWw.LockID - } - } - return -} - -func (apWws AccountProfilesWithWeight) TenantIDs() (tntIDs []string) { - if apWws != nil { - tntIDs = make([]string, len(apWws)) - for i, apWw := range apWws { - tntIDs[i] = apWw.AccountProfile.TenantID() - } - } - return -} - -// BalanceWithWeight attaches static Weight to Balance -type BalanceWithWeight struct { - *Balance - Weight float64 -} - -// BalancesWithWeight is a sortable list of BalanceWithWeight -type BalancesWithWeight []*BalanceWithWeight - -// Sort is part of sort interface, sort based on Weight -func (blcs BalancesWithWeight) Sort() { - sort.Slice(blcs, func(i, j int) bool { return blcs[i].Weight > blcs[j].Weight }) -} - -// Balances returns the list of Balances -func (bWws BalancesWithWeight) Balances() (blncs []*Balance) { - if bWws != nil { - blncs = make([]*Balance, len(bWws)) - for i, bWw := range bWws { - blncs[i] = bWw.Balance - } - } - return -} - -// APIAccountProfileWithOpts is used in API calls -type APIAccountProfileWithOpts struct { - *APIAccountProfile - APIOpts map[string]interface{} -} - -type AccountProfileWithAPIOpts struct { - *AccountProfile - APIOpts map[string]interface{} -} - -// ArgsAccountForEvent arguments used for process event -type ArgsAccountsForEvent struct { - *CGREvent - AccountIDs []string -} - -type ReplyMaxUsage struct { - AccountID string - MaxUsage time.Duration - Cost *EventCharges -} - -// APIAccountProfile represents one APIAccount on a Tenant -type APIAccountProfile struct { - Tenant string - ID string - FilterIDs []string - ActivationInterval *ActivationInterval - Weights string - Opts map[string]interface{} - Balances map[string]*APIBalance - ThresholdIDs []string -} - -// AsAccountProfile convert APIAccountProfile struct to AccountProfile struct -func (ext *APIAccountProfile) AsAccountProfile() (profile *AccountProfile, err error) { - profile = &AccountProfile{ - Tenant: ext.Tenant, - ID: ext.ID, - FilterIDs: ext.FilterIDs, - ActivationInterval: ext.ActivationInterval, - Opts: ext.Opts, - ThresholdIDs: ext.ThresholdIDs, - } - if ext.Weights != EmptyString { - if profile.Weights, err = NewDynamicWeightsFromString(ext.Weights, ";", "&"); err != nil { - return nil, err - } - } - if len(ext.Balances) != 0 { - profile.Balances = make(map[string]*Balance, len(ext.Balances)) - for i, bal := range ext.Balances { - if profile.Balances[i], err = bal.AsBalance(); err != nil { - return nil, err - } - } - } - return -} - -// APIBalance represents one APIBalance inside an APIAccount -type APIBalance struct { - ID string // Balance identificator, unique within an Account - FilterIDs []string - Weights string - Type string - Units float64 - UnitFactors []*APIUnitFactor - Opts map[string]interface{} - CostIncrements []*APICostIncrement - AttributeIDs []string - RateProfileIDs []string -} - -// AsBalance convert APIBalance struct to Balance struct -func (ext *APIBalance) AsBalance() (balance *Balance, err error) { - balance = &Balance{ - ID: ext.ID, - FilterIDs: ext.FilterIDs, - Type: ext.Type, - Units: NewDecimalFromFloat64(ext.Units), - Opts: ext.Opts, - AttributeIDs: ext.AttributeIDs, - RateProfileIDs: ext.RateProfileIDs, - } - if ext.Weights != EmptyString { - if balance.Weights, err = NewDynamicWeightsFromString(ext.Weights, ";", "&"); err != nil { - return nil, err - } - } - if len(ext.UnitFactors) != 0 { - balance.UnitFactors = make([]*UnitFactor, len(ext.UnitFactors)) - for i, uFct := range ext.UnitFactors { - balance.UnitFactors[i] = uFct.AsUnitFactor() - } - } - if len(ext.CostIncrements) != 0 { - balance.CostIncrements = make([]*CostIncrement, len(ext.CostIncrements)) - for i, cIncr := range ext.CostIncrements { - balance.CostIncrements[i] = cIncr.AsCostIncrement() - } - } - return - -} - -// APICostIncrement represent one CostIncrement inside an APIBalance -type APICostIncrement struct { - FilterIDs []string - Increment *float64 - FixedFee *float64 - RecurrentFee *float64 -} - -// AsCostIncrement convert APICostIncrement struct to CostIncrement struct -func (ext *APICostIncrement) AsCostIncrement() (cIncr *CostIncrement) { - cIncr = &CostIncrement{ - FilterIDs: ext.FilterIDs, - } - if ext.Increment != nil { - cIncr.Increment = NewDecimalFromFloat64(*ext.Increment) - } - if ext.FixedFee != nil { - cIncr.FixedFee = NewDecimalFromFloat64(*ext.FixedFee) - } - if ext.RecurrentFee != nil { - cIncr.RecurrentFee = NewDecimalFromFloat64(*ext.RecurrentFee) - } - return -} - -// APIUnitFactor represent one UnitFactor inside an APIBalance -type APIUnitFactor struct { - FilterIDs []string - Factor float64 -} - -// AsUnitFactor convert APIUnitFactor struct to UnitFactor struct -func (ext *APIUnitFactor) AsUnitFactor() *UnitFactor { - return &UnitFactor{ - FilterIDs: ext.FilterIDs, - Factor: NewDecimalFromFloat64(ext.Factor), - } -} - -type ArgsActSetBalance struct { - Tenant string - AccountID string - Diktats []*BalDiktat - Reset bool - APIOpts map[string]interface{} -} - -type BalDiktat struct { - Path string - Value string -} - -type ArgsActRemoveBalances struct { - Tenant string - AccountID string - BalanceIDs []string - APIOpts map[string]interface{} -} diff --git a/utils/accountprofile_test.go b/utils/accountprofile_test.go deleted file mode 100644 index 880cc8b87..000000000 --- a/utils/accountprofile_test.go +++ /dev/null @@ -1,753 +0,0 @@ -/* -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 utils - -import ( - "reflect" - "testing" - "time" - - "github.com/ericlagergren/decimal" -) - -func TestCloneBalance(t *testing.T) { - expBlc := &Balance{ - ID: "TEST_ID1", - FilterIDs: []string{"*string:~*req.Account:1001"}, - Weights: DynamicWeights{ - { - Weight: 1.1, - }, - }, - Type: "*abstract", - Opts: map[string]interface{}{ - "Destination": 10, - }, - CostIncrements: []*CostIncrement{ - { - FilterIDs: []string{"*string:~*req.Account:1001"}, - Increment: &Decimal{decimal.New(1, 1)}, - FixedFee: &Decimal{decimal.New(75, 1)}, - RecurrentFee: &Decimal{decimal.New(20, 1)}, - }, - }, - AttributeIDs: []string{"attr1", "attr2"}, - RateProfileIDs: []string{"RATE1", "RATE2"}, - UnitFactors: []*UnitFactor{ - { - FilterIDs: []string{"*string:~*req.Account:1001"}, - Factor: &Decimal{decimal.New(20, 2)}, - }, - }, - Units: &Decimal{decimal.New(125, 3)}, - } - if rcv := expBlc.Clone(); !reflect.DeepEqual(rcv, expBlc) { - t.Errorf("Expected %+v \n, received %+v", ToJSON(expBlc), ToJSON(rcv)) - } - - expBlc.Opts = nil - if rcv := expBlc.Clone(); !reflect.DeepEqual(rcv, expBlc) { - t.Errorf("Expected %+v \n, received %+v", ToJSON(expBlc), ToJSON(rcv)) - } -} - -func TestCloneAccountProfile(t *testing.T) { - actPrf := &AccountProfile{ - Tenant: "cgrates.org", - ID: "Profile_id1", - FilterIDs: []string{"*string:~*req.Account:1001"}, - ActivationInterval: &ActivationInterval{ - ActivationTime: time.Date(2020, 7, 21, 10, 0, 0, 0, time.UTC), - ExpiryTime: time.Date(2020, 7, 22, 10, 0, 0, 0, time.UTC), - }, - Weights: DynamicWeights{ - { - Weight: 2.4, - }, - }, - Opts: map[string]interface{}{ - "Destination": 10, - }, - Balances: map[string]*Balance{ - "VoiceBalance": { - ID: "VoiceBalance", - FilterIDs: []string{"*string:~*req.Account:1001"}, - Weights: DynamicWeights{ - { - Weight: 1.1, - }, - }, - Type: "*abstract", - Opts: map[string]interface{}{ - "Destination": 10, - }, - CostIncrements: []*CostIncrement{ - { - FilterIDs: []string{"*string:~*req.Account:1001"}, - Increment: &Decimal{decimal.New(1, 1)}, - FixedFee: &Decimal{decimal.New(75, 1)}, - RecurrentFee: &Decimal{decimal.New(20, 1)}, - }, - }, - AttributeIDs: []string{"attr1", "attr2"}, - UnitFactors: []*UnitFactor{ - { - FilterIDs: []string{"*string:~*req.Account:1001"}, - Factor: &Decimal{decimal.New(20, 2)}, - }, - }, - Units: &Decimal{decimal.New(125, 3)}, - }, - }, - ThresholdIDs: []string{"*none"}, - } - if rcv := actPrf.Clone(); !reflect.DeepEqual(rcv, actPrf) { - t.Errorf("Expected %+v, received %+v", ToJSON(actPrf), ToJSON(rcv)) - } - - actPrf.Opts = nil - actPrf.ActivationInterval = nil - if rcv := actPrf.Clone(); !reflect.DeepEqual(rcv, actPrf) { - t.Errorf("Expected %+v \n, received %+v", ToJSON(actPrf), ToJSON(rcv)) - } -} - -func TestTenantIDAccountProfile(t *testing.T) { - actPrf := &AccountProfile{ - Tenant: "cgrates.org", - ID: "test_ID1", - } - exp := "cgrates.org:test_ID1" - if rcv := actPrf.TenantID(); rcv != exp { - t.Errorf("Expected %+v, received %+v", exp, rcv) - } -} - -func TestAccountProfileAsAccountProfile(t *testing.T) { - apiAccPrf := &APIAccountProfile{ - Tenant: "cgrates.org", - ID: "test_ID1", - Opts: map[string]interface{}{}, - Balances: map[string]*APIBalance{ - "VoiceBalance": { - ID: "VoiceBalance", - FilterIDs: []string{"*string:~*req.Account:1001"}, - Weights: ";1.2", - Type: "*abstract", - Opts: map[string]interface{}{ - "Destination": 10, - }, - Units: 0, - }, - }, - Weights: ";10", - } - expected := &AccountProfile{ - Tenant: "cgrates.org", - ID: "test_ID1", - Opts: map[string]interface{}{}, - Balances: map[string]*Balance{ - "VoiceBalance": { - ID: "VoiceBalance", - FilterIDs: []string{"*string:~*req.Account:1001"}, - Weights: DynamicWeights{ - { - Weight: 1.2, - }, - }, - Type: "*abstract", - Opts: map[string]interface{}{ - "Destination": 10, - }, - Units: NewDecimal(0, 0), - }, - }, - Weights: DynamicWeights{ - { - Weight: 10, - }, - }, - } - if rcv, err := apiAccPrf.AsAccountProfile(); err != nil { - t.Error(err) - } else if !reflect.DeepEqual(expected, rcv) { - t.Errorf("Expected %+v, received %+v", ToJSON(expected), ToJSON(rcv)) - } -} - -func TestAsAccountProfileError(t *testing.T) { - apiAccPrf := &APIAccountProfile{ - Tenant: "cgrates.org", - ID: "test_ID1", - Opts: map[string]interface{}{}, - Balances: map[string]*APIBalance{ - "MonetaryBalance": { - Weights: ";10", - }, - }, - Weights: "10", - } - expectedErr := "invalid DynamicWeight format for string <10>" - if _, err := apiAccPrf.AsAccountProfile(); err == nil || err.Error() != expectedErr { - t.Errorf("Expected %+v, received %+v", expectedErr, err) - } - - apiAccPrf.Weights = ";10" - apiAccPrf.Balances["MonetaryBalance"].Weights = "10" - expectedErr = "invalid DynamicWeight format for string <10>" - if _, err := apiAccPrf.AsAccountProfile(); err == nil || err.Error() != expectedErr { - t.Errorf("Expected %+v, received %+v", expectedErr, err) - } -} - -func TestAPIBalanceAsBalance(t *testing.T) { - blc := &APIBalance{ - ID: "VoiceBalance", - CostIncrements: []*APICostIncrement{ - { - FilterIDs: []string{"*string:~*req.Account:1001"}, - Increment: Float64Pointer(1), - FixedFee: Float64Pointer(10), - RecurrentFee: Float64Pointer(35), - }, - }, - Weights: ";10", - UnitFactors: []*APIUnitFactor{ - { - FilterIDs: []string{"*string:~*req.Account:1001"}, - Factor: 20, - }, - }, - } - expected := &Balance{ - ID: "VoiceBalance", - CostIncrements: []*CostIncrement{ - { - FilterIDs: []string{"*string:~*req.Account:1001"}, - Increment: NewDecimal(1, 0), - FixedFee: NewDecimal(10, 0), - RecurrentFee: NewDecimal(35, 0), - }, - }, - Weights: DynamicWeights{ - { - Weight: 10, - }, - }, - UnitFactors: []*UnitFactor{ - { - FilterIDs: []string{"*string:~*req.Account:1001"}, - Factor: NewDecimal(20, 0), - }, - }, - Units: NewDecimal(0, 0), - } - if rcv, err := blc.AsBalance(); err != nil { - t.Error(err) - } else if !reflect.DeepEqual(rcv, expected) { - t.Errorf("Expected %+v \n, received %+v", ToJSON(expected), ToJSON(rcv)) - } - -} - -func TestAccountProfileBalancesAlteredCompareLength(t *testing.T) { - actPrf := &AccountProfile{ - Balances: map[string]*Balance{ - "testString": {}, - "testString2": {}, - }, - } - - actBk := map[string]*decimal.Big{ - "testString": {}, - } - - result := actPrf.BalancesAltered(actBk) - if result != true { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", true, result) - } - -} - -func TestAccountProfileBalancesAlteredCheckKeys(t *testing.T) { - actPrf := &AccountProfile{ - Balances: map[string]*Balance{ - "testString": {}, - }, - } - - actBk := map[string]*decimal.Big{ - "testString2": {}, - } - - result := actPrf.BalancesAltered(actBk) - if result != true { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", true, result) - } - -} - -func TestAccountProfileBalancesAlteredCompareValues(t *testing.T) { - actPrf := &AccountProfile{ - Balances: map[string]*Balance{ - "testString": { - Units: &Decimal{decimal.New(1, 1)}, - }, - }, - } - - actBk := map[string]*decimal.Big{ - "testString": {}, - } - - result := actPrf.BalancesAltered(actBk) - if result != true { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", true, result) - } - -} - -func TestAccountProfileBalancesAlteredFalse(t *testing.T) { - actPrf := &AccountProfile{} - - actBk := AccountBalancesBackup{} - - result := actPrf.BalancesAltered(actBk) - if result != false { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", false, result) - } - -} - -func TestAPRestoreFromBackup(t *testing.T) { - actPrf := &AccountProfile{ - Balances: map[string]*Balance{ - "testString": { - Units: &Decimal{}, - }, - }, - } - - actBk := AccountBalancesBackup{ - "testString": decimal.New(1, 1), - } - - actPrf.RestoreFromBackup(actBk) - for key, value := range actBk { - if actPrf.Balances[key].Units.Big != value { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", actPrf.Balances[key].Units.Big, value) - } - } -} - -func TestAPAccountBalancesBackup(t *testing.T) { - actPrf := &AccountProfile{ - Balances: map[string]*Balance{ - "testKey": { - Units: &Decimal{decimal.New(1234, 3)}, - }, - }, - } - - actBk := actPrf.AccountBalancesBackup() - for key, value := range actBk { - if actPrf.Balances[key].Units.Big.Cmp(value) != 0 { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", actPrf.Balances[key].Units.Big, value) - } - } -} - -func TestAPNewDefaultBalance(t *testing.T) { - - const torFltr = "*string:~*req.ToR:" - id := "testID" - - expected := &Balance{ - ID: id, - Type: MetaConcrete, - Units: NewDecimal(0, 0), - CostIncrements: []*CostIncrement{ - { - FilterIDs: []string{torFltr + MetaVoice}, - Increment: NewDecimal(int64(time.Second), 0), - RecurrentFee: NewDecimal(0, 0), - }, - { - FilterIDs: []string{torFltr + MetaData}, - Increment: NewDecimal(1024*1024, 0), - RecurrentFee: NewDecimal(0, 0), - }, - { - FilterIDs: []string{torFltr + MetaSMS}, - Increment: NewDecimal(1, 0), - RecurrentFee: NewDecimal(0, 0), - }, - }, - } - - received := NewDefaultBalance(id) - - if !reflect.DeepEqual(received, expected) { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, received) - } -} - -func TestAPApsSort(t *testing.T) { - - apS := AccountProfilesWithWeight{ - { - Weight: 2, - }, - { - Weight: 1, - }, - { - Weight: 3, - }, - } - expected := AccountProfilesWithWeight{ - { - Weight: 3, - }, - { - Weight: 2, - }, - { - Weight: 1, - }, - } - - apS.Sort() - if !reflect.DeepEqual(apS, expected) { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", ToJSON(expected), ToJSON(apS)) - } -} - -func TestAPAccountProfiles(t *testing.T) { - - apS := AccountProfilesWithWeight{ - { - AccountProfile: &AccountProfile{ - Tenant: "testTenant1", - ID: "testID1", - FilterIDs: []string{"testFID1", "testFID2"}, - ActivationInterval: &ActivationInterval{ - ActivationTime: time.Date(2020, time.April, 12, 0, 0, 0, 0, time.UTC), - ExpiryTime: time.Date(2020, time.April, 12, 10, 0, 0, 0, time.UTC), - }, - Weights: nil, - Balances: map[string]*Balance{ - "testBalance1": { - ID: "testBalance1", - Type: MetaAbstract, - Units: &Decimal{decimal.New(0, 0)}, - }, - }, - }, - Weight: 23, - LockID: "testString1", - }, - { - AccountProfile: &AccountProfile{ - Tenant: "testTenant2", - ID: "testID2", - FilterIDs: []string{"testFID1", "testFID2"}, - ActivationInterval: &ActivationInterval{ - ActivationTime: time.Date(2020, time.April, 12, 0, 0, 0, 0, time.UTC), - ExpiryTime: time.Date(2020, time.April, 12, 10, 0, 0, 0, time.UTC), - }, - Weights: nil, - Balances: map[string]*Balance{ - "testBalance2": { - ID: "testBalance2", - Type: MetaAbstract, - Units: &Decimal{decimal.New(0, 0)}, - }, - }, - }, - Weight: 15, - LockID: "testString2", - }, - } - - expected := make([]*AccountProfile, 0) - for i := range apS { - expected = append(expected, apS[i].AccountProfile) - } - received := apS.AccountProfiles() - - if !reflect.DeepEqual(received, expected) { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", ToJSON(expected), ToJSON(received)) - } -} - -func TestAPLockIDs(t *testing.T) { - apS := AccountProfilesWithWeight{ - { - AccountProfile: &AccountProfile{ - Tenant: "testTenant1", - ID: "testID1", - FilterIDs: []string{"testFID1", "testFID2"}, - ActivationInterval: &ActivationInterval{ - ActivationTime: time.Date(2020, time.April, 12, 0, 0, 0, 0, time.UTC), - ExpiryTime: time.Date(2020, time.April, 12, 10, 0, 0, 0, time.UTC), - }, - Weights: nil, - Balances: map[string]*Balance{ - "testBalance1": { - ID: "testBalance1", - Type: MetaAbstract, - Units: &Decimal{decimal.New(0, 0)}, - }, - }, - }, - Weight: 23, - LockID: "testString1", - }, - { - AccountProfile: &AccountProfile{ - Tenant: "testTenant2", - ID: "testID2", - FilterIDs: []string{"testFID1", "testFID2"}, - ActivationInterval: &ActivationInterval{ - ActivationTime: time.Date(2020, time.April, 12, 0, 0, 0, 0, time.UTC), - ExpiryTime: time.Date(2020, time.April, 12, 10, 0, 0, 0, time.UTC), - }, - Weights: nil, - Balances: map[string]*Balance{ - "testBalance2": { - ID: "testBalance2", - Type: MetaAbstract, - Units: &Decimal{decimal.New(0, 0)}, - }, - }, - }, - Weight: 15, - LockID: "testString2", - }, - } - - expected := make([]string, 0) - for i := range apS { - expected = append(expected, apS[i].LockID) - } - received := apS.LockIDs() - - if !reflect.DeepEqual(received, expected) { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, received) - } -} - -func TestAPTenantIDs(t *testing.T) { - apS := AccountProfilesWithWeight{ - { - AccountProfile: &AccountProfile{ - Tenant: "testTenant1", - ID: "testID1", - FilterIDs: []string{"testFID1", "testFID2"}, - ActivationInterval: &ActivationInterval{ - ActivationTime: time.Date(2020, time.April, 12, 0, 0, 0, 0, time.UTC), - ExpiryTime: time.Date(2020, time.April, 12, 10, 0, 0, 0, time.UTC), - }, - Weights: nil, - Balances: map[string]*Balance{ - "testBalance1": { - ID: "testBalance1", - Type: MetaAbstract, - Units: &Decimal{decimal.New(0, 0)}, - }, - }, - }, - Weight: 23, - LockID: "testString1", - }, - { - AccountProfile: &AccountProfile{ - Tenant: "testTenant2", - ID: "testID2", - FilterIDs: []string{"testFID1", "testFID2"}, - ActivationInterval: &ActivationInterval{ - ActivationTime: time.Date(2020, time.April, 12, 0, 0, 0, 0, time.UTC), - ExpiryTime: time.Date(2020, time.April, 12, 10, 0, 0, 0, time.UTC), - }, - Weights: nil, - Balances: map[string]*Balance{ - "testBalance2": { - ID: "testBalance2", - Type: MetaAbstract, - Units: &Decimal{decimal.New(0, 0)}, - }, - }, - }, - Weight: 15, - LockID: "testString2", - }, - } - - expected := make([]string, 0) - for _, val := range apS { - id := val.AccountProfile.Tenant - tenant := val.AccountProfile.ID - expected = append(expected, ConcatenatedKey(id, tenant)) - } - received := apS.TenantIDs() - - if !reflect.DeepEqual(received, expected) { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", expected, received) - } -} - -func TestAPBlcsSort(t *testing.T) { - - blncS := BalancesWithWeight{ - { - Weight: 2, - }, - { - Weight: 1, - }, - { - Weight: 3, - }, - } - expected := BalancesWithWeight{ - { - Weight: 3, - }, - { - Weight: 2, - }, - { - Weight: 1, - }, - } - - blncS.Sort() - if !reflect.DeepEqual(blncS, expected) { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", ToJSON(expected), ToJSON(blncS)) - } -} - -func TestAPBalances(t *testing.T) { - blncS := BalancesWithWeight{ - { - Balance: &Balance{ - ID: "testID1", - FilterIDs: []string{"testFID1", "testFID2"}, - Type: MetaAbstract, - Units: &Decimal{decimal.New(1234, 3)}, - Weights: nil, - UnitFactors: []*UnitFactor{ - { - Factor: NewDecimal(1, 1), - }, - }, - Opts: map[string]interface{}{ - MetaBalanceLimit: -1.0, - }, - CostIncrements: []*CostIncrement{ - { - Increment: NewDecimal(int64(time.Duration(time.Second)), 0), - RecurrentFee: NewDecimal(0, 0), - }, - }, - AttributeIDs: []string{"testString1"}, - RateProfileIDs: []string{"testString2"}, - }, - Weight: 23, - }, - { - Balance: &Balance{ - ID: "testID2", - FilterIDs: []string{"testFID3", "testFID4"}, - Type: MetaAbstract, - Units: &Decimal{decimal.New(1234, 3)}, - Weights: nil, - UnitFactors: []*UnitFactor{ - { - Factor: NewDecimal(1, 1), - }, - }, - Opts: map[string]interface{}{ - MetaBalanceLimit: -1.0, - }, - CostIncrements: []*CostIncrement{ - { - Increment: NewDecimal(int64(time.Duration(time.Second)), 0), - RecurrentFee: NewDecimal(0, 0), - }, - }, - AttributeIDs: []string{"testString3"}, - RateProfileIDs: []string{"testString4"}, - }, - Weight: 23, - }, - } - - expected := make([]*Balance, 0) - for i := range blncS { - expected = append(expected, blncS[i].Balance) - } - received := blncS.Balances() - - if !reflect.DeepEqual(received, expected) { - t.Errorf("\nExpected: <%+v>, \nReceived: <%+v>", ToJSON(expected), ToJSON(received)) - } -} - -func TestEqualsUnitFactor(t *testing.T) { - uf1 := &UnitFactor{ - FilterIDs: []string{"*string:~*req.Account:1003"}, - Factor: NewDecimal(10, 0), - } - uf2 := &UnitFactor{ - FilterIDs: []string{"*string:~*req.Account:1003"}, - Factor: NewDecimal(10, 0), - } - if !uf1.Equals(uf2) { - t.Errorf("Unexpected equal result") - } - - uf1.FilterIDs = []string{"*string:~*req.Account:1004"} - if uf1.Equals(uf2) { - t.Errorf("Unexpected equal result") - } - uf1.FilterIDs = nil - - if uf1.Equals(uf2) { - t.Errorf("Unexpected equal result") - } - uf1.FilterIDs = []string{"*string:~*req.Account:1003"} - - uf1.Factor = NewDecimal(100, 0) - if uf1.Equals(uf2) { - t.Errorf("Unexpected equal result") - } - - uf1.Factor = nil - uf2.Factor = nil - if !uf1.Equals(uf2) { - t.Errorf("Unexpected equal result") - } - - uf2.Factor = NewDecimal(10, 0) - if uf1.Equals(uf2) { - t.Errorf("Unexpected equal result") - } -}