From 2f9f07a976cdc1b623c5afe3195c19aa0cf97a31 Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Wed, 7 Aug 2024 09:28:29 -0400 Subject: [PATCH] Add new unit tests on ees and ers --- ees/amqp_test.go | 48 ++++++++++++++++++++++++ ees/kafka_test.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++ ees/lib_test.go | 34 +++++++++++++++++ ees/nats_test.go | 22 +++++++++++ ees/s3_test.go | 60 ++++++++++++++++++++++++++++++ ees/sql_test.go | 12 ++++++ ers/amqp_test.go | 54 +++++++++++++++++++++++++++ 7 files changed, 324 insertions(+) create mode 100644 ees/amqp_test.go create mode 100644 ees/kafka_test.go create mode 100644 ees/s3_test.go create mode 100644 ers/amqp_test.go diff --git a/ees/amqp_test.go b/ees/amqp_test.go new file mode 100644 index 000000000..51136b9ca --- /dev/null +++ b/ees/amqp_test.go @@ -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 +*/ + +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) + } +} diff --git a/ees/kafka_test.go b/ees/kafka_test.go new file mode 100644 index 000000000..6b8a2f022 --- /dev/null +++ b/ees/kafka_test.go @@ -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 +*/ +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) + } +} diff --git a/ees/lib_test.go b/ees/lib_test.go index 11fc2f9ea..254e63b30 100644 --- a/ees/lib_test.go +++ b/ees/lib_test.go @@ -19,6 +19,7 @@ along with this program. If not, see 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) + } +} diff --git a/ees/nats_test.go b/ees/nats_test.go index d4259a9fb..2d8cfa1ab 100644 --- a/ees/nats_test.go +++ b/ees/nats_test.go @@ -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) + } +} diff --git a/ees/s3_test.go b/ees/s3_test.go new file mode 100644 index 000000000..fbc959790 --- /dev/null +++ b/ees/s3_test.go @@ -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 +*/ + +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) + } +} diff --git a/ees/sql_test.go b/ees/sql_test.go index 32efbc5b8..e7cf4624c 100644 --- a/ees/sql_test.go +++ b/ees/sql_test.go @@ -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) + } +} diff --git a/ers/amqp_test.go b/ers/amqp_test.go new file mode 100644 index 000000000..665c01fbe --- /dev/null +++ b/ers/amqp_test.go @@ -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 +*/ + +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) + } + }) + } +}