Add new coverage tests on migrator and config

This commit is contained in:
armirveliaj
2024-09-18 10:37:43 -04:00
committed by Dan Christian Bogos
parent bfba6c6f74
commit b523024ca2
2 changed files with 220 additions and 0 deletions

View File

@@ -2479,3 +2479,110 @@ func TestDfTemplateSJsonCfg(t *testing.T) {
t.Errorf("Expected: %+v \n,received: %+v", utils.ToJSON(eCfg), utils.ToJSON(cfg))
}
}
func TestSentryPeerJson(t *testing.T) {
clientID := "1001"
clientSecret := "2014"
tokenUrl := "https://cgrates.org/token"
ipsUrl := "https://cgrates.org/ips"
numbersUrl := "https://cgrates.org/numbers"
audience := "audience"
grantType := "code"
validJSON := `{
"client_id": "1001",
"client_secret": "2014",
"token_url": "https://cgrates.org/token",
"ips_url": "https://cgrates.org/ips",
"numbers_url": "https://cgrates.org/numbers",
"audience": "audience",
"grant_type": "code"
}`
validRaw := json.RawMessage(validJSON)
invalidJSON := `{
"client_id": "1001",
"client_secret": 123,
"token_url": "https://cgrates.org/token",
"ips_url": "https://cgrates.org/ips"
}`
invalidRaw := json.RawMessage(invalidJSON)
expectedValid := &SentryPeerJsonCfg{
ClientID: &clientID,
ClientSecret: &clientSecret,
TokenUrl: &tokenUrl,
IpsUrl: &ipsUrl,
NumbersUrl: &numbersUrl,
Audience: &audience,
GrantType: &grantType,
}
tests := []struct {
name string
input CgrJsonCfg
expected *SentryPeerJsonCfg
expectError bool
}{
{
name: "Valid JSON",
input: CgrJsonCfg{
SentryPeerCfgJson: &validRaw,
},
expected: expectedValid,
expectError: false,
},
{
name: "Invalid JSON",
input: CgrJsonCfg{
SentryPeerCfgJson: &invalidRaw,
},
expected: nil,
expectError: true,
},
{
name: "Missing JSON",
input: CgrJsonCfg{},
expected: nil,
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.input.SentryPeerJson()
if (err != nil) != tt.expectError {
t.Fatalf("expected error: %v, got: %v", tt.expectError, err)
}
if err == nil {
if tt.expected == nil {
if got != nil {
t.Errorf("expected nil, got: %v", got)
}
} else {
if got.ClientID == nil || tt.expected.ClientID == nil || *got.ClientID != *tt.expected.ClientID {
t.Errorf("expected ClientID: %v, got: %v", *tt.expected.ClientID, got.ClientID)
}
if got.ClientSecret == nil || tt.expected.ClientSecret == nil || *got.ClientSecret != *tt.expected.ClientSecret {
t.Errorf("expected ClientSecret: %v, got: %v", *tt.expected.ClientSecret, got.ClientSecret)
}
if got.TokenUrl == nil || tt.expected.TokenUrl == nil || *got.TokenUrl != *tt.expected.TokenUrl {
t.Errorf("expected TokenUrl: %v, got: %v", *tt.expected.TokenUrl, got.TokenUrl)
}
if got.IpsUrl == nil || tt.expected.IpsUrl == nil || *got.IpsUrl != *tt.expected.IpsUrl {
t.Errorf("expected IpsUrl: %v, got: %v", *tt.expected.IpsUrl, got.IpsUrl)
}
if got.NumbersUrl == nil || tt.expected.NumbersUrl == nil || *got.NumbersUrl != *tt.expected.NumbersUrl {
t.Errorf("expected NumbersUrl: %v, got: %v", *tt.expected.NumbersUrl, got.NumbersUrl)
}
if got.Audience == nil || tt.expected.Audience == nil || *got.Audience != *tt.expected.Audience {
t.Errorf("expected Audience: %v, got: %v", *tt.expected.Audience, got.Audience)
}
if got.GrantType == nil || tt.expected.GrantType == nil || *got.GrantType != *tt.expected.GrantType {
t.Errorf("expected GrantType: %v, got: %v", *tt.expected.GrantType, got.GrantType)
}
}
}
})
}
}

113
migrator/routes_test.go Normal file
View File

@@ -0,0 +1,113 @@
/*
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 <http://www.gnu.org/licenses/>
*/
package migrator
import (
"testing"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
)
func TestConvertSupplierToRoute(t *testing.T) {
supplierProfile := &SupplierProfile{
Tenant: "cgrates.org",
ID: "ProfileID",
FilterIDs: []string{"filter1", "filter2"},
ActivationInterval: &utils.ActivationInterval{},
Sorting: "weight",
SortingParameters: []string{"param1", "param2"},
Suppliers: []*Supplier{
{
ID: "supplier1",
FilterIDs: []string{"filterA"},
AccountIDs: []string{"account1"},
RatingPlanIDs: []string{"rating1"},
ResourceIDs: []string{"resource1"},
StatIDs: []string{"stat1"},
Weight: 10.0,
Blocker: false,
SupplierParameters: "param1",
},
{
ID: "supplier2",
FilterIDs: []string{"filterB"},
AccountIDs: []string{"account2"},
RatingPlanIDs: []string{"rating2"},
ResourceIDs: []string{"resource2"},
StatIDs: []string{"stat2"},
Weight: 20.0,
Blocker: true,
SupplierParameters: "param2",
},
},
Weight: 15.0,
}
expectedRoute := &engine.RouteProfile{
Tenant: "cgrates.org",
ID: "ProfileID",
FilterIDs: []string{"filter1", "filter2"},
ActivationInterval: &utils.ActivationInterval{},
Sorting: "weight",
SortingParameters: []string{"param1", "param2"},
Weight: 15.0,
Routes: []*engine.Route{
{
ID: "supplier1",
FilterIDs: []string{"filterA"},
AccountIDs: []string{"account1"},
RatingPlanIDs: []string{"rating1"},
ResourceIDs: []string{"resource1"},
StatIDs: []string{"stat1"},
Weight: 10.0,
Blocker: false,
RouteParameters: "param1",
},
{
ID: "supplier2",
FilterIDs: []string{"filterB"},
AccountIDs: []string{"account2"},
RatingPlanIDs: []string{"rating2"},
ResourceIDs: []string{"resource2"},
StatIDs: []string{"stat2"},
Weight: 20.0,
Blocker: true,
RouteParameters: "param2",
},
},
}
result := convertSupplierToRoute(supplierProfile)
if result.Tenant != expectedRoute.Tenant {
t.Errorf("expected Tenant %s, got %s", expectedRoute.Tenant, result.Tenant)
}
if result.ID != expectedRoute.ID {
t.Errorf("expected ID %s, got %s", expectedRoute.ID, result.ID)
}
if len(result.FilterIDs) != len(expectedRoute.FilterIDs) {
t.Errorf("expected FilterIDs length %d, got %d", len(expectedRoute.FilterIDs), len(result.FilterIDs))
}
for i, filterID := range expectedRoute.FilterIDs {
if result.FilterIDs[i] != filterID {
t.Errorf("expected FilterID %s, got %s", filterID, result.FilterIDs[i])
}
}
}