Add new unit tests on agents

This commit is contained in:
armirveliaj
2024-07-18 10:48:45 -04:00
committed by Dan Christian Bogos
parent 5e5d615aad
commit 8ea764e602
2 changed files with 187 additions and 0 deletions

View File

@@ -1101,3 +1101,137 @@ func TestFSEventGetOriginatorIP(t *testing.T) {
t.Errorf("For non-empty check, expected: %s, got: %s", expectedNonEmptyResult, nonEmptyResult)
}
}
func TestGetSessionIds(t *testing.T) {
expectedUUID := ""
FSEvent := FSEvent{}
result := FSEvent.GetSessionIds()
expected := []string{expectedUUID}
if len(result) != len(expected) {
t.Errorf("expected length %d, got %d", len(expected), len(result))
}
for i, v := range result {
if v != expected[i] {
t.Errorf("expected value at index %d to be '%s', got '%s'", i, expected[i], v)
}
}
}
func TestGetEndTime(t *testing.T) {
fsev := FSEvent{
"END_TIME": "0001-01-01 00:00:00 +0000 UTC",
}
timezone := "UTC"
result, err := fsev.GetEndTime("END_TIME", timezone)
if err != nil {
t.Errorf("error parsing time: %v", err)
}
expectedTime := time.Date(0001, 01, 01, 00, 00, 00, 0000, time.UTC)
if !result.Equal(expectedTime) {
t.Errorf("expected '%v', got '%v'", expectedTime, result)
}
}
func TestFseventMissingParameter(t *testing.T) {
testCases := []struct {
name string
fsev map[string]string
want string
}{
{
name: "missing_subject",
fsev: map[string]string{
ACCOUNT: "account",
DESTINATION: "destination",
CATEGORY: "category",
UUID: "uuid",
CSTMID: "tenant",
CALL_DEST_NR: "callDestNr",
},
want: "",
},
{
name: "missing_destination",
fsev: map[string]string{
ACCOUNT: "account",
SUBJECT: "subject",
CATEGORY: "category",
UUID: "uuid",
CSTMID: "tenant",
CALL_DEST_NR: "callDestNr",
},
want: "",
},
{
name: "missing_category",
fsev: map[string]string{
ACCOUNT: "account",
SUBJECT: "subject",
DESTINATION: "destination",
UUID: "uuid",
CSTMID: "tenant",
CALL_DEST_NR: "callDestNr",
},
want: "",
},
{
name: "missing_uuid",
fsev: map[string]string{
ACCOUNT: "account",
SUBJECT: "subject",
DESTINATION: "destination",
CATEGORY: "category",
CSTMID: "tenant",
CALL_DEST_NR: "callDestNr",
},
want: utils.OriginID,
},
{
name: "missing_tenant",
fsev: map[string]string{
ACCOUNT: "account",
SUBJECT: "subject",
DESTINATION: "destination",
CATEGORY: "category",
UUID: "uuid",
CALL_DEST_NR: "callDestNr",
},
want: "",
},
{
name: "missing_callDestNr",
fsev: map[string]string{
ACCOUNT: "account",
SUBJECT: "subject",
DESTINATION: "destination",
CATEGORY: "category",
UUID: "uuid",
CSTMID: "tenant",
},
want: CALL_DEST_NR,
},
{
name: "all_present",
fsev: map[string]string{
ACCOUNT: "account",
SUBJECT: "subject",
DESTINATION: "destination",
CATEGORY: "category",
UUID: "uuid",
CSTMID: "tenant",
CALL_DEST_NR: "callDestNr",
},
want: "",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
fsev := FSEvent(tt.fsev)
got := fsev.MissingParameter("")
if got != tt.want {
t.Errorf("expected %v, got %v", tt.want, got)
}
})
}
}

View File

@@ -20,9 +20,62 @@ package agents
import (
"testing"
"github.com/cgrates/birpc/context"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/sessions"
"github.com/cgrates/kamevapi"
)
func TestKAsSessionSClientIface(t *testing.T) {
_ = sessions.BiRPClient(new(KamailioAgent))
}
func TestKAReload(t *testing.T) {
cfg := &config.KamAgentCfg{}
ka := &KamailioAgent{
cfg: cfg,
}
ka.Reload()
expectedLength := len(cfg.EvapiConns)
if len(ka.conns) != expectedLength {
t.Errorf("Expected ka.conns to have length %d, got %d", expectedLength, len(ka.conns))
}
for _, conn := range ka.conns {
if conn != nil {
t.Errorf("Expected nil KamEvapi instance in ka.conns, got non-nil")
}
}
for i := range ka.conns {
ka.conns[i] = &kamevapi.KamEvapi{}
}
for _, conn := range ka.conns {
if conn == nil {
t.Errorf("Expected non-nil KamEvapi instance in ka.conns, got nil")
}
}
}
func TestKACall(t *testing.T) {
ka := &KamailioAgent{}
ctx := &context.Context{}
serviceMethod := "SomeService.Method"
args := struct{ Key string }{"value"}
reply := struct{ Result string }{}
err := ka.Call(ctx, serviceMethod, args, &reply)
if err == nil {
t.Errorf("Call didn't return an error: %v", err)
}
expected := ""
if reply.Result != expected {
t.Errorf("Expected reply.Result to be %q, got %q", expected, reply.Result)
}
}
func TestKAShutdown(t *testing.T) {
agent := &KamailioAgent{}
err := agent.Shutdown()
if err != nil {
t.Errorf("Shutdown returned an error: %v", err)
}
}