From 0a0aec58b3deb0e31ffe48f256ba9e7091a55cf5 Mon Sep 17 00:00:00 2001 From: armirveliaj Date: Thu, 29 Aug 2024 10:05:40 -0400 Subject: [PATCH] Add new unit tests on agents and apier/v1 --- agents/astagent_test.go | 43 +++++++++++++++++ apier/v1/libapier_test.go | 14 ++++++ apier/v1/rankings_test.go | 99 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 apier/v1/rankings_test.go diff --git a/agents/astagent_test.go b/agents/astagent_test.go index f17971bc7..86e9ffa25 100644 --- a/agents/astagent_test.go +++ b/agents/astagent_test.go @@ -18,6 +18,7 @@ along with this program. If not, see package agents import ( + "errors" "testing" "github.com/cgrates/birpc" @@ -119,3 +120,45 @@ func TestAsteriskAgentV1AlterSession(t *testing.T) { t.Errorf("Expected error: %v, got: %v", utils.ErrNotImplemented, err) } } + +func TestAsteriskAgentCall(t *testing.T) { + sma := &AsteriskAgent{} + ctx := context.Background() + + tests := []struct { + name string + serviceMethod string + args any + reply any + expectedError error + }{ + { + name: "Valid service method", + serviceMethod: "testMethod", + args: nil, + reply: nil, + expectedError: nil, + }, + { + name: "Invalid service method", + serviceMethod: "invalidMethod", + args: nil, + reply: nil, + expectedError: errors.New("unexpected method"), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := sma.Call(ctx, tt.serviceMethod, tt.args, tt.reply) + + if err == nil && tt.expectedError == nil { + t.Errorf("expected no error, but got: %v", err) + } else if err == nil && tt.expectedError != nil { + t.Errorf("expected error: %v, but got no error", tt.expectedError) + } else if err == nil && tt.expectedError != nil { + t.Errorf("expected error: %v, but got: %v", tt.expectedError, err) + } + }) + } +} diff --git a/apier/v1/libapier_test.go b/apier/v1/libapier_test.go index 3a7ac20d5..2a7d7f7f7 100644 --- a/apier/v1/libapier_test.go +++ b/apier/v1/libapier_test.go @@ -281,3 +281,17 @@ func TestCallCacheForFilter(t *testing.T) { t.Errorf("Expected %s ,received: %s", utils.ToJSON(exp), utils.ToJSON(rpl)) } } + +func TestResourceSv1Ping(t *testing.T) { + rsv1 := &ResourceSv1{} + ctx := context.Background() + ign := &utils.CGREvent{} + var reply string + err := rsv1.Ping(ctx, ign, &reply) + if err != nil { + t.Errorf("expected no error, got %v", err) + } + if reply != utils.Pong { + t.Errorf("expected reply to be %v, got %v", utils.Pong, reply) + } +} diff --git a/apier/v1/rankings_test.go b/apier/v1/rankings_test.go new file mode 100644 index 000000000..5a000eaf1 --- /dev/null +++ b/apier/v1/rankings_test.go @@ -0,0 +1,99 @@ +/* +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 v1 + +import ( + "testing" + + "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func TestRankingSv1Ping(t *testing.T) { + sa := &RankingSv1{} + ctx := context.Background() + ign := &utils.CGREvent{} + var reply string + err := sa.Ping(ctx, ign, &reply) + if err != nil { + t.Errorf("expected no error, got %v", err) + } + if reply != utils.Pong { + t.Errorf("expected reply to be %v, got %v", utils.Pong, reply) + } +} +func TestNewRankingSv1(t *testing.T) { + rankingSvc := NewRankingSv1() + if rankingSvc == nil { + t.Errorf("NewRankingSv1() returned nil") + } +} + +func TestRemoveRankingProfile(t *testing.T) { + dataManager := &engine.DataManager{} + apierSv1 := &APIerSv1{ + DataManager: dataManager, + } + args := &utils.TenantIDWithAPIOpts{ + APIOpts: map[string]interface{}{ + utils.CacheOpt: "cacheOptValue", + }, + } + var reply string + err := apierSv1.RemoveRankingProfile(nil, args, &reply) + if err == nil { + t.Fatalf("RemoveRankingProfile() returned an error: %v", err) + } + if reply == utils.OK { + t.Errorf("RemoveRankingProfile() returned reply = %v, want %v", reply, utils.OK) + } + +} + +func TestRalsPing(t *testing.T) { + rsv1 := &RALsV1{} + ctx := context.Background() + var ign *utils.CGREvent + var reply string + err := rsv1.Ping(ctx, ign, &reply) + if err != nil { + t.Errorf("Expected no error, got %v", err) + } + expectedReply := utils.Pong + if reply != expectedReply { + t.Errorf("Expected reply %v, got %v", expectedReply, reply) + } +} + +func TestRalsCall(t *testing.T) { + rsv1 := &RALsV1{} + ctx := context.Background() + serviceMethod := "TestServiceMethod" + args := "TestArgs" + var reply string + err := rsv1.Call(ctx, serviceMethod, args, &reply) + if err == nil { + t.Errorf("UNSUPPORTED_SERVICE_METHOD") + } + expectedReply := "response" + if reply == expectedReply { + t.Errorf("Expected reply %v, got %v", expectedReply, reply) + } +}