Add new unit tests on ees and ers

This commit is contained in:
armirveliaj
2024-08-07 09:28:29 -04:00
committed by Dan Christian Bogos
parent 0bab7d5557
commit 2f9f07a976
7 changed files with 324 additions and 0 deletions

48
ees/amqp_test.go Normal file
View File

@@ -0,0 +1,48 @@
/*
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 ees
import (
"testing"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
func TestAmqpGetMetrics(t *testing.T) {
expectedMetrics := &utils.SafeMapStorage{}
pstr := &AMQPee{
dc: expectedMetrics,
}
result := pstr.GetMetrics()
if result != expectedMetrics {
t.Errorf("expected metrics %v, got %v", expectedMetrics, result)
}
}
func TestCfg(t *testing.T) {
expectedCfg := &config.EventExporterCfg{ID: "testCfgID"}
pstr := &AMQPee{
cfg: expectedCfg,
}
result := pstr.Cfg()
if result != expectedCfg {
t.Errorf("expected cfg %v, got %v", expectedCfg, result)
}
}

94
ees/kafka_test.go Normal file
View File

@@ -0,0 +1,94 @@
/*
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 ees
import (
"testing"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
kafka "github.com/segmentio/kafka-go"
)
func TestKafkaEEConnect(t *testing.T) {
kafkaEE := &KafkaEE{
writer: nil,
cfg: &config.EventExporterCfg{},
dc: &utils.SafeMapStorage{},
reqs: &concReq{},
}
err := kafkaEE.Connect()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
func TestKafkaEE_Cfg(t *testing.T) {
expectedCfg := &config.EventExporterCfg{}
kafkaEE := &KafkaEE{
cfg: expectedCfg,
}
result := kafkaEE.Cfg()
if result != expectedCfg {
t.Errorf("expected %v, got %v", expectedCfg, result)
}
}
func TestKafkaEEGetMetrics(t *testing.T) {
safeMapStorage := &utils.SafeMapStorage{}
kafkaEE := &KafkaEE{
dc: safeMapStorage,
}
result := kafkaEE.GetMetrics()
if result != safeMapStorage {
t.Errorf("expected %v, got %v", safeMapStorage, result)
}
}
func TestKafkaEEClose(t *testing.T) {
writer := &kafka.Writer{
Addr: kafka.TCP("localhost:9092"),
Topic: "test-topic",
Balancer: &kafka.LeastBytes{},
}
kafkaEE := &KafkaEE{
writer: writer,
}
err := kafkaEE.Close()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
}
func TestKafkaEE_ExportEvent(t *testing.T) {
writer := &kafka.Writer{
Addr: kafka.TCP("localhost:9092"),
Topic: "test-topic",
Balancer: &kafka.LeastBytes{},
}
kafkaEE := &KafkaEE{
writer: writer,
reqs: &concReq{},
}
content := []byte("test message")
key := "test-key"
err := kafkaEE.ExportEvent(content, key)
if err == nil {
t.Errorf("expected no error, got %v", err)
}
}

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package ees
import (
"encoding/json"
"os"
"testing"
@@ -52,3 +53,36 @@ func testStopCgrEngine(t *testing.T) {
t.Error(err)
}
}
func TestGetOneData(t *testing.T) {
type testData struct {
Field1 string `json:"field1"`
Field2 int `json:"field2"`
}
ub := &engine.Account{}
expected1, _ := json.Marshal(ub)
got1, err1 := getOneData(ub, nil)
if err1 != nil {
t.Errorf("getOneData() error = %v", err1)
}
if string(got1) != string(expected1) {
t.Errorf("getOneData() got = %v, want %v", string(got1), string(expected1))
}
extraData := testData{Field1: "test", Field2: 123}
expected2, _ := json.Marshal(extraData)
got2, err2 := getOneData(nil, extraData)
if err2 != nil {
t.Errorf("getOneData() error = %v", err2)
}
if string(got2) != string(expected2) {
t.Errorf("getOneData() got = %v, want %v", string(got2), string(expected2))
}
expected3 := []byte(nil)
got3, err3 := getOneData(nil, nil)
if err3 != nil {
t.Errorf("getOneData() error = %v", err3)
}
if (got3 != nil && string(got3) != string(expected3)) || (got3 == nil && expected3 != nil) {
t.Errorf("getOneData() got = %v, want %v", got3, expected3)
}
}

View File

@@ -239,3 +239,25 @@ func TestGetNatsOptsClientCert(t *testing.T) {
t.Error("There was supposedly no certificate")
}
}
func TestNatsEECfg(t *testing.T) {
expectedCfg := &config.EventExporterCfg{}
pstr := &NatsEE{
cfg: expectedCfg,
}
actualCfg := pstr.Cfg()
if actualCfg != expectedCfg {
t.Errorf("Cfg() = %v, want %v", actualCfg, expectedCfg)
}
}
func TestNatsEEGetMetrics(t *testing.T) {
expectedMetrics := &utils.SafeMapStorage{}
pstr := &NatsEE{
dc: expectedMetrics,
}
actualMetrics := pstr.GetMetrics()
if actualMetrics != expectedMetrics {
t.Errorf("GetMetrics() = %v, want %v", actualMetrics, expectedMetrics)
}
}

60
ees/s3_test.go Normal file
View File

@@ -0,0 +1,60 @@
/*
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 ees
import (
"testing"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
func TestS3GetMetrics(t *testing.T) {
safeMapStorage := &utils.SafeMapStorage{}
pstr := &S3EE{
dc: safeMapStorage,
}
result := pstr.GetMetrics()
if result == nil {
t.Errorf("GetMetrics() returned nil; expected a non-nil SafeMapStorage")
return
}
if result != safeMapStorage {
t.Errorf("GetMetrics() returned unexpected result; got %v, want %v", result, safeMapStorage)
}
}
func TestClose(t *testing.T) {
pstr := &S3EE{}
err := pstr.Close()
if err != nil {
t.Errorf("Close() returned an error: %v; expected nil", err)
}
}
func TestS3Cfg(t *testing.T) {
expectedCfg := &config.EventExporterCfg{}
pstr := &S3EE{
cfg: expectedCfg,
}
actualCfg := pstr.Cfg()
if actualCfg != expectedCfg {
t.Errorf("Cfg() = %v; expected %v", actualCfg, expectedCfg)
}
}

View File

@@ -147,3 +147,15 @@ func TestOpenDBError3(t *testing.T) {
}
logger.Default = tmp
}
func TestPrepareMap(t *testing.T) {
sqlEe := &SQLEe{}
event := &utils.CGREvent{}
result, err := sqlEe.PrepareMap(event)
if err != nil {
t.Errorf("PrepareMap() returned an error: %v", err)
}
if result != nil {
t.Errorf("PrepareMap() returned a non-nil result: %v", result)
}
}

54
ers/amqp_test.go Normal file
View File

@@ -0,0 +1,54 @@
/*
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 ers
import (
"sync"
"testing"
)
func TestAmqpClientIsAvailable(t *testing.T) {
tests := []struct {
name string
available bool
}{
{
name: "Available",
available: true,
},
{
name: "Not Available",
available: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := &amqpClient{
available: tt.available,
mu: sync.RWMutex{},
}
got := client.isAvailable()
if got != tt.available {
t.Errorf("isAvailable() = %v; want %v", got, tt.available)
}
})
}
}