From ecb140a8af504dd4698de94239b400a31e72279f Mon Sep 17 00:00:00 2001 From: adragusin Date: Fri, 8 Nov 2019 13:29:26 +0200 Subject: [PATCH] Created tests for engine.Task DataProvider methods --- engine/task.go | 2 +- engine/task_test.go | 170 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 engine/task_test.go diff --git a/engine/task.go b/engine/task.go index 1a6792747..5ef89c824 100644 --- a/engine/task.go +++ b/engine/task.go @@ -48,7 +48,7 @@ func (t *Task) String() string { // AsNavigableMap implements config.DataProvider func (t *Task) AsNavigableMap(_ []*config.FCTemplate) (nm *config.NavigableMap, err error) { - nm = new(config.NavigableMap) + nm = config.NewNavigableMap(nil) nm.Set([]string{utils.UUID}, t.Uuid, false, false) nm.Set([]string{utils.AccountID}, t.AccountID, false, false) nm.Set([]string{utils.ActionsID}, t.ActionsID, false, false) diff --git a/engine/task_test.go b/engine/task_test.go new file mode 100644 index 000000000..3693ac5f3 --- /dev/null +++ b/engine/task_test.go @@ -0,0 +1,170 @@ +/* +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 engine + +import ( + "net" + "reflect" + "testing" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/utils" +) + +func TestTaskString(t *testing.T) { + task := &Task{ + Uuid: "test", + AccountID: "test2", + ActionsID: "test3", + } + eOut := "{\"Uuid\":\"test\",\"AccountID\":\"test2\",\"ActionsID\":\"test3\"}" + rcv := task.String() + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %q, received: %q", eOut, rcv) + } +} + +func TestTaskAsMavigableMap(t *testing.T) { + //empty check + task := new(Task) + eOut := config.NewNavigableMap(nil) + eOut.Set([]string{utils.UUID}, "", false, false) + eOut.Set([]string{utils.AccountID}, "", false, false) + eOut.Set([]string{utils.ActionsID}, "", false, false) + rcv, err := task.AsNavigableMap(nil) + if err != nil { + t.Error(err) + } + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %q, received: %q", eOut, rcv) + } + //normal check + task = &Task{ + Uuid: "test", + AccountID: "test2", + ActionsID: "test3", + } + eOut.Set([]string{utils.UUID}, "test", false, false) + eOut.Set([]string{utils.AccountID}, "test2", false, false) + eOut.Set([]string{utils.ActionsID}, "test3", false, false) + rcv, err = task.AsNavigableMap(nil) + if err != nil { + t.Error(err) + } + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %q, received: %q", eOut, rcv) + } +} + +func TestTaskFieldAsinterface(t *testing.T) { + //empty check + task := new(Task) + fldPath := []string{utils.UUID, utils.ActionsID} + rcv, err := task.FieldAsInterface(fldPath) + eOut := "" + if err != nil { + t.Error(err) + } + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %q, received: %q", eOut, rcv) + } + //Uuid check + task = &Task{ + Uuid: "test", + AccountID: "test2", + ActionsID: "test3", + } + rcv, err = task.FieldAsInterface(fldPath) + eOut = "test" + if err != nil { + t.Error(err) + } + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %q, received: %q", eOut, rcv) + } + +} + +func TestTaskFieldAsString(t *testing.T) { + //empty check + task := new(Task) + eOut := "" + var fldPath []string // := {"string1","string2"} + rcv, err := task.FieldAsString(fldPath) + if err != nil { + t.Error(err) + } + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %q, received: %q", eOut, rcv) + } + //Uuid check + task = &Task{ + Uuid: "test", + AccountID: "test2", + ActionsID: "test3", + } + fldPath = []string{utils.UUID, "string2"} + eOut = "test" + rcv, err = task.FieldAsString(fldPath) + if err != nil { + t.Error(err) + } + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %q, received: %q", eOut, rcv) + } + //AccountID check + fldPath = []string{utils.AccountID, "string2"} + eOut = "test2" + rcv, err = task.FieldAsString(fldPath) + if err != nil { + t.Error(err) + } + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %q, received: %q", eOut, rcv) + } + //ActionsID check + fldPath = []string{utils.ActionsID, "string2"} + eOut = "test3" + rcv, err = task.FieldAsString(fldPath) + if err != nil { + t.Error(err) + } + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %q, received: %q", eOut, rcv) + } + //default check + fldPath = []string{"default", "case"} + eOut = "" + rcv, err = task.FieldAsString(fldPath) + if err == nil { + t.Error("Expecting NOT_FOUND error, received nil") + } + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %q, received: %q", eOut, rcv) + } +} + +func TestTaskRemoteHost(t *testing.T) { + task := new(Task) + var eOut net.Addr + rcv := task.RemoteHost() + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expecting: %q, received: %q", eOut, rcv) + } +}