diff --git a/agents/diamagent_test.go b/agents/diamagent_test.go index 106fff56f..a8139e670 100644 --- a/agents/diamagent_test.go +++ b/agents/diamagent_test.go @@ -535,3 +535,16 @@ func TestProcessRequest(t *testing.T) { } } + +func TestV1GetActiveSessionIDs(t *testing.T) { + da := &DiameterAgent{} + ignParam := "ignore" + var sessionIDs []*sessions.SessionID + err := da.V1GetActiveSessionIDs(ignParam, &sessionIDs) + if err != utils.ErrNotImplemented { + t.Errorf("Expected ErrNotImplemented, but got: %v", err) + } + if len(sessionIDs) != 0 { + t.Errorf("Expected sessionIDs slice to be empty, but got %d items", len(sessionIDs)) + } +} diff --git a/agents/kamevent_test.go b/agents/kamevent_test.go index 688b7f0ac..2716d4303 100644 --- a/agents/kamevent_test.go +++ b/agents/kamevent_test.go @@ -826,3 +826,89 @@ func TestKamEventKamReplyString(t *testing.T) { t.Errorf("Unexpected TransactionLabel in parsed KamReply: expected %s, got %v", "label", parsedReply["TransactionLabel"]) } } + +func TestKamDlgReplyString(t *testing.T) { + kdr := &KamDlgReply{} + expectedJSON := `{"Event":"","Jsonrpl_body":null}` + result := kdr.String() + if result != expectedJSON { + t.Errorf("Expected %s, but got %s", expectedJSON, result) + } +} + +func TestNewKamDlgReply(t *testing.T) { + validJSON := []byte(`{"Field1":"test","Field2":123}`) + expectedReply := KamDlgReply{} + rpl, err := NewKamDlgReply(validJSON) + if err != nil { + t.Errorf("Expected no error, but got %v", err) + } + if rpl != expectedReply { + t.Errorf("Expected %+v, but got %+v", expectedReply, rpl) + } + invalidJSON := []byte(`{"Field1":"test","Field2":}`) + _, err = NewKamDlgReply(invalidJSON) + if err == nil { + t.Errorf("Expected an error, but got nil") + } + emptyJSON := []byte(`{}`) + expectedEmptyReply := KamDlgReply{} + rpl, err = NewKamDlgReply(emptyJSON) + if err != nil { + t.Errorf("Expected no error, but got %v", err) + } + + if rpl != expectedEmptyReply { + t.Errorf("Expected %+v, but got %+v", expectedEmptyReply, rpl) + } +} + +func TestAsKamAuthReplyProcessStats(t *testing.T) { + kamEvData := KamEvent{} + authArgs := &sessions.V1AuthorizeArgs{ + ProcessStats: true, + } + statQueueIDs := []string{"queue1", "queue2", "queue3"} + authReply := &sessions.V1AuthorizeReply{ + StatQueueIDs: &statQueueIDs, + } + kar, err := kamEvData.AsKamAuthReply(authArgs, authReply, nil) + if err != nil { + t.Errorf("Expected no error, but got %v", err) + } + expectedStatQueues := "queue1,queue2,queue3" + if kar.StatQueues != expectedStatQueues { + t.Errorf("Expected StatQueues to be %s, but got %s", expectedStatQueues, kar.StatQueues) + } +} + +func TestAsKamAuthReplyProcessThresholds(t *testing.T) { + kamEvData := KamEvent{ + KamTRIndex: "index123", + KamTRLabel: "label123", + } + authArgs := &sessions.V1AuthorizeArgs{ + ProcessThresholds: true, + } + thresholdIDs := []string{"threshold1", "threshold2", "threshold3"} + authReply := &sessions.V1AuthorizeReply{ + ThresholdIDs: &thresholdIDs, + } + kar, err := kamEvData.AsKamAuthReply(authArgs, authReply, nil) + if err != nil { + t.Errorf("Expected no error, but got %v", err) + } + expectedThresholds := "threshold1,threshold2,threshold3" + if kar.Thresholds != expectedThresholds { + t.Errorf("Expected Thresholds to be %s, but got %s", expectedThresholds, kar.Thresholds) + } +} + +func TestV1AuthorizeArgsParseFlags(t *testing.T) { + kev := make(KamEvent) + args := kev.V1AuthorizeArgs() + if !args.GetMaxUsage { + t.Error("Expected GetMaxUsage to be true when CGRFlags is not present") + } + +} diff --git a/agents/libdiam_test.go b/agents/libdiam_test.go index 320cea6fd..98630f05e 100644 --- a/agents/libdiam_test.go +++ b/agents/libdiam_test.go @@ -1189,3 +1189,19 @@ func TestLibDiamBareErr(t *testing.T) { t.Errorf("Expected CommandFlags to be set to ErrorFlag, got %v", result.Header.CommandFlags) } } + +func TestHeaderLenDiam(t *testing.T) { + a := &diam.AVP{ + Flags: avp.Vbit, + } + result := headerLen(a) + if result != 12 { + t.Errorf("Expected headerLen to return 12 for Vbit set, got: %d", result) + } + a.Flags = 0 + result = headerLen(a) + if result != 8 { + t.Errorf("Expected headerLen to return 8 for Vbit not set, got: %d", result) + } + +} diff --git a/agents/libhttpagent_test.go b/agents/libhttpagent_test.go index eb14657db..8c1692e17 100644 --- a/agents/libhttpagent_test.go +++ b/agents/libhttpagent_test.go @@ -22,8 +22,11 @@ import ( "bufio" "bytes" "net/http" + "net/http/httptest" "strings" "testing" + + "github.com/cgrates/cgrates/utils" //"github.com/cgrates/cgrates/utils" ) @@ -191,3 +194,39 @@ func TestHttpXmlDPFieldAsInterface2(t *testing.T) { t.Errorf("expecting: 0.0225, received: <%s>", data) } } + +func TestStringMethod(t *testing.T) { + req, err := http.NewRequest("GET", "http://cgrates.com", nil) + if err != nil { + t.Fatalf("Failed to create request: %v", err) + } + hU := &httpUrlDP{ + req: req, + } + str := hU.String() + expectedPrefix := "GET / HTTP/1.1\r\nHost: cgrates.com\r\n" + if str[:len(expectedPrefix)] != expectedPrefix { + t.Errorf("Expected request string to start with '%s', got '%s'", expectedPrefix, str) + } +} + +func TestNewHATextPlainEncoder(t *testing.T) { + w := httptest.NewRecorder() + _, err := newHATextPlainEncoder(w) + if err != nil { + t.Fatalf("Unexpected error creating encoder: %v", err) + } + +} + +func TestRemoteHost(t *testing.T) { + addr := "192.168.1.1:2012" + hU := &httpXmlDP{ + addr: addr, + } + remoteAddr := hU.RemoteHost() + expectedAddr := utils.NewNetAddr("TCP", addr) + if remoteAddr.String() != expectedAddr.String() { + t.Errorf("Expected RemoteHost to return %v, but got %v", expectedAddr, remoteAddr) + } +}