From 8ea764e60278db236e8d1a74ccb5a3b5185cb7eb Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Thu, 18 Jul 2024 10:48:45 -0400 Subject: [PATCH] Add new unit tests on agents --- agents/fsevent_test.go | 134 ++++++++++++++++++++++++++++++++++++++++ agents/kamagent_test.go | 53 ++++++++++++++++ 2 files changed, 187 insertions(+) diff --git a/agents/fsevent_test.go b/agents/fsevent_test.go index af34ac827..0126c3a9b 100644 --- a/agents/fsevent_test.go +++ b/agents/fsevent_test.go @@ -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) + } + }) + } +} diff --git a/agents/kamagent_test.go b/agents/kamagent_test.go index 96ac1fa24..0680f3d75 100644 --- a/agents/kamagent_test.go +++ b/agents/kamagent_test.go @@ -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) + } + +}