From a67caa6dc2df0f1900670badacea5dd8931e12ce Mon Sep 17 00:00:00 2001 From: NikolasPetriti Date: Fri, 28 Jul 2023 16:59:56 +0200 Subject: [PATCH] Add coverage tests for action.go --- engine/action_test.go | 480 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 479 insertions(+), 1 deletion(-) diff --git a/engine/action_test.go b/engine/action_test.go index 60c64c905..ca61a3768 100644 --- a/engine/action_test.go +++ b/engine/action_test.go @@ -18,7 +18,14 @@ along with this program. If not, see package engine -import "testing" +import ( + "encoding/json" + "reflect" + "testing" + "time" + + "github.com/cgrates/cgrates/utils" +) func TestActionErrors(t *testing.T) { tests := []struct { @@ -121,3 +128,474 @@ func TestActionErrors(t *testing.T) { }) } } + +var sm utils.StringMap = utils.StringMap{str: bl} +var rtm RITiming = RITiming{ + Years: utils.Years{2021}, + Months: utils.Months{8}, + MonthDays: utils.MonthDays{28}, + WeekDays: utils.WeekDays{5}, + StartTime: str2, + EndTime: str2, + cronString: str2, + tag: str2, +} +var vf ValueFactor = ValueFactor{str2: fl2} +var vfr utils.ValueFormula = utils.ValueFormula{ + Method: str2, + Params: map[string]any{str2: str2}, + Static: fl2, +} +var bf BalanceFilter = BalanceFilter{ + Uuid: &str2, + ID: &str2, + Type: &str2, + Value: &vfr, + ExpirationDate: &tm2, + Weight: &fl2, + DestinationIDs: &sm, + RatingSubject: &str2, + Categories: &sm, + SharedGroups: &sm, + TimingIDs: &sm, + Timings: []*RITiming{&rtm}, + Disabled: &bl, + Factor: &vf, + Blocker: &bl, +} +var cf CounterFilter = CounterFilter{ + Value: fl, + Filter: &bf, +} +var uc UnitCounter = UnitCounter{ + CounterType: "*balance", + Counters: CounterFilters{&cf}, +} +var at ActionTrigger = ActionTrigger{ + ID: str2, + UniqueID: str2, + ThresholdType: str2, + Recurrent: bl, + MinSleep: 1 * time.Millisecond, + ExpirationDate: tm2, + ActivationDate: tm2, + Balance: &bf, + Weight: fl2, + ActionsID: str2, + MinQueuedItems: nm2, + Executed: bl, + LastExecutionTime: tm2, +} + +var ac Account = Account{ + ID: "test:test", + UnitCounters: UnitCounters{str: {&uc}}, + ActionTriggers: ActionTriggers{&at}, + AllowNegative: bl, + Disabled: bl, + UpdateTime: tm2, + executingTriggers: bl, +} +var a Action = Action{ + Id: str2, + ActionType: str2, + ExtraParameters: str2, + Filter: str2, + ExpirationString: str2, + Weight: fl2, + Balance: &bf, + balanceValue: fl2, +} + +func TestActionClone(t *testing.T) { + var a *Action + + rcv := a.Clone() + + if rcv != nil { + t.Error(rcv) + } +} + +func TestActionlogAction(t *testing.T) { + ub := Account{} + a := Action{} + acs := Actions{} + + err := logAction(&ub, &a, acs, nil) + if err != nil { + if err.Error() != "" { + t.Error(err) + } + } + + err = logAction(nil, &a, acs, &ub) + if err != nil { + if err.Error() != "" { + t.Error(err) + } + } +} + +func TestActionresetAction(t *testing.T) { + + acs := Actions{} + + err := debitResetAction(&ac, &a, acs, nil) + if err != nil { + if err.Error() != "" { + t.Error(err) + } + } + + if ac.BalanceMap == nil { + t.Error("didn't reset action") + } +} + +func TestActiongetOneData(t *testing.T) { + ub := Account{} + + rcv, err := getOneData(&ub, nil) + if err != nil { + t.Error(err) + } + exp, _ := json.Marshal(&ub) + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", exp, rcv) + } + + rcv, err = getOneData(nil, &ub) + if err != nil { + t.Error(err) + } + exp, _ = json.Marshal(&ub) + + if !reflect.DeepEqual(rcv, exp) { + t.Errorf("expected %v, received %v", exp, rcv) + } + + rcv, err = getOneData(nil, nil) + if err != nil { + t.Error(err) + } + + if rcv != nil { + t.Error(rcv) + } +} + +func TestAccountsendErrors(t *testing.T) { + type args struct { + ub *Account + a *Action + acs Actions + extraData any + } + tests := []struct { + name string + rcv error + exp string + }{ + { + name: "sendAMQP error check", + rcv: sendAMQP(nil, nil, nil, make(chan int)), + exp: "json: unsupported type: chan int", + }, + { + name: "sendAWS error check", + rcv: sendAWS(nil, nil, nil, make(chan int)), + exp: "json: unsupported type: chan int", + }, + { + name: "sendSQS error check", + rcv: sendSQS(nil, nil, nil, make(chan int)), + exp: "json: unsupported type: chan int", + }, + { + name: "sendKafka error check", + rcv: sendKafka(nil, nil, nil, make(chan int)), + exp: "json: unsupported type: chan int", + }, + { + name: "sendS3 error check", + rcv: sendS3(nil, nil, nil, make(chan int)), + exp: "json: unsupported type: chan int", + }, + { + name: "callURL error check", + rcv: callURL(nil, nil, nil, make(chan int)), + exp: "json: unsupported type: chan int", + }, + { + name: "callURLAsync error check", + rcv: callURLAsync(nil, nil, nil, make(chan int)), + exp: "json: unsupported type: chan int", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.rcv != nil { + if tt.rcv.Error() != tt.exp { + t.Errorf("expected %s, received %s", tt.exp, tt.rcv) + } + } else { + t.Error("was expecting an error") + } + }) + } +} + +func TestActionsetBalanceAction(t *testing.T) { + err := setBalanceAction(nil, nil, nil, nil) + + if err != nil { + if err.Error() != `nil account for null action` { + t.Error(err) + } + } else { + t.Error("was expecting an error") + } +} + +func TestActiontransferMonetaryDefaultAction(t *testing.T) { + err := transferMonetaryDefaultAction(nil, nil, nil, nil) + + if err != nil { + if err.Error() != "ACCOUNT_NOT_FOUND" { + t.Error(err) + } + } + + blc := Balance{ + Uuid: str2, + ID: str2, + Value: fl2, + ExpirationDate: tm2, + Weight: fl2, + DestinationIDs: sm, + RatingSubject: str2, + Categories: sm, + SharedGroups: sm, + Timings: []*RITiming{&rtm}, + TimingIDs: sm, + Disabled: bl, + Factor: ValueFactor{str2: fl2}, + Blocker: bl, + precision: nm2, + dirty: bl, + } + ac := Account{ + BalanceMap: map[string]Balances{str: {&blc}}, + } + + err = transferMonetaryDefaultAction(&ac, nil, nil, nil) + + if err != nil { + if err.Error() != "NOT_FOUND" { + t.Error(err) + } + } +} + +func TestActionpublishAccount(t *testing.T) { + sm := utils.StringMap{ + "test1": bl, + } + rtm := RITiming{ + Years: utils.Years{2021}, + Months: utils.Months{8}, + MonthDays: utils.MonthDays{28}, + WeekDays: utils.WeekDays{5}, + StartTime: str, + EndTime: str, + cronString: str, + tag: str, + } + blc := Balance{ + Uuid: str2, + ID: str2, + Value: fl2, + ExpirationDate: tm2, + Weight: fl2, + DestinationIDs: sm, + RatingSubject: str2, + Categories: sm, + SharedGroups: sm, + Timings: []*RITiming{&rtm}, + TimingIDs: sm, + Disabled: bl, + Factor: ValueFactor{str2: fl2}, + Blocker: bl, + precision: nm2, + dirty: bl, + } + ac := Account{ + BalanceMap: map[string]Balances{str: {&blc}}, + } + + err := publishAccount(&ac, nil, nil, nil) + if err != nil { + t.Error(err) + } +} + +func TestActionCloneNil(t *testing.T) { + var apl Actions + + rcv, err := apl.Clone() + if err != nil { + t.Error(err) + } + + if rcv != nil { + t.Error(err) + } +} + +var cdrP cdrLogProvider = cdrLogProvider{ + acnt: &ac, + action: &a, + cache: utils.MapStorage{}, +} + +func TestActionString(t *testing.T) { + rcv := cdrP.String() + exp := utils.ToJSON(cdrP) + + if rcv != exp { + t.Errorf("expected %s, received %s", exp, rcv) + } +} + +func TestActionFieldAsInterface(t *testing.T) { + cdrP := cdrLogProvider{ + acnt: &ac, + action: &a, + cache: utils.MapStorage{}, + } + + tests := []struct { + name string + arg []string + exp any + err string + }{ + { + name: "empty field path", + arg: []string{}, + exp: nil, + err: "NOT_FOUND", + }, + { + name: "AccountID case", + arg: []string{"AccountID"}, + exp: "test:test", + err: "", + }, + { + name: "ActionID case", + arg: []string{"ActionID"}, + exp: "test", + err: "", + }, + { + name: "ActionType case", + arg: []string{"ActionType"}, + exp: "test", + err: "", + }, + { + name: "BalanceUUID case", + arg: []string{"BalanceUUID"}, + exp: "test", + err: "", + }, + { + name: "BalanceID case", + arg: []string{"BalanceID"}, + exp: "test", + err: "", + }, + { + name: "BalanceValue case", + arg: []string{"BalanceValue"}, + exp: "0", + err: "", + }, + { + name: "DestinationIDs case", + arg: []string{"DestinationIDs"}, + exp: cdrP.action.Balance.DestinationIDs.String(), + err: "", + }, + { + name: "ExtraParameters case", + arg: []string{"ExtraParameters"}, + exp: cdrP.action.ExtraParameters, + err: "", + }, + { + name: "RatingSubject case", + arg: []string{"RatingSubject"}, + exp: "test", + err: "", + }, + { + name: "Category case", + arg: []string{"Category"}, + exp: cdrP.action.Balance.Categories.String(), + err: "", + }, + { + name: "SharedGroups case", + arg: []string{"SharedGroups"}, + exp: cdrP.action.Balance.SharedGroups.String(), + err: "", + }, + { + name: "default case", + arg: []string{"test"}, + exp: "test", + err: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rcv, err := cdrP.FieldAsInterface(tt.arg) + + if err != nil { + if err.Error() != tt.err { + t.Error(err) + } + } + + if !reflect.DeepEqual(rcv, tt.exp) { + t.Errorf("expected %v, received %v", tt.exp, rcv) + } + }) + } +} + +func TestActionFieldAsString(t *testing.T) { + cdrP.acnt.ID = "test" + + _, err := cdrP.FieldAsString([]string{"AccountID"}) + + if err != nil { + if err.Error() != "Unsupported format for TenantAccount: test" { + t.Error(err) + } + } +} + +func TestActionRemoteHost(t *testing.T) { + rcv := cdrP.RemoteHost() + + if rcv.String() != "local" { + t.Error(rcv) + } +} \ No newline at end of file