From 8d991672ab53dba929e6edc2a95da6773dc514f7 Mon Sep 17 00:00:00 2001 From: DanB Date: Wed, 20 Nov 2013 18:20:30 +0100 Subject: [PATCH] Adding ApierV1.TMDestination local tests --- apier/v1/{apier_test.go => apier_ltest.go} | 59 ++++++++++++++++++++++ apier/v1/tpdestinations.go | 12 ++--- utils/apitpdata.go | 7 +++ 3 files changed, 69 insertions(+), 9 deletions(-) rename apier/v1/{apier_test.go => apier_ltest.go} (69%) diff --git a/apier/v1/apier_test.go b/apier/v1/apier_ltest.go similarity index 69% rename from apier/v1/apier_test.go rename to apier/v1/apier_ltest.go index 56b087a40..e5663f683 100644 --- a/apier/v1/apier_test.go +++ b/apier/v1/apier_ltest.go @@ -33,6 +33,8 @@ import ( "testing" ) +// ToDo: Replace rpc.Client with internal rpc server and Apier using internal map as both data and stor so we can run the tests non-local + /* README: @@ -54,6 +56,7 @@ var testLocal = flag.Bool("local", false, "Perform the tests only on local test var dataDir = flag.String("data_dir", "/usr/share/cgrates/data", "CGR data dir path here") var waitRater = flag.Int("wait_rater", 200, "Number of miliseconds to wait for rater to start and cache") +// Empty tables before using them func TestCreateTables(t *testing.T) { if !*testLocal { return @@ -178,3 +181,59 @@ func TestApierTPTiming(t *testing.T) { t.Errorf("Calling ApierV1.GetTPTimingIds expected: %v, received: %v", expectedTmIds, rplyTmIds) } } + +// Test here TPTiming APIs +func TestApierTPDestination(t *testing.T) { + if !*testLocal { + return + } + reply := "" + dstDe := &utils.TPDestination{TPid:engine.TEST_SQL, DestinationId: "GERMANY", Prefixes: []string{"+49"}} + dstDeMobile := &utils.TPDestination{TPid:engine.TEST_SQL, DestinationId: "GERMANY_MOBILE", Prefixes: []string{"+4915", "+4916", "+4917"}} + dstFs := &utils.TPDestination{TPid:engine.TEST_SQL, DestinationId: "FS_USERS", Prefixes: []string{"10"}} + dstDe2 := new(utils.TPDestination) + *dstDe2 = *dstDe // Data which we use for remove, still keeping the sample data to check proper loading + dstDe2.DestinationId = "GERMANY2" + for _, dst := range []*utils.TPDestination{dstDe, dstDeMobile, dstFs, dstDe2} { + if err := rater.Call("ApierV1.SetTPDestination", dst, &reply); err!=nil { + t.Error("Got error on ApierV1.SetTPDestination: ", err.Error()) + } else if reply != "OK" { + t.Error("Unexpected reply received when calling ApierV1.SetTPDestination: ", reply) + } + } + // Check second set + if err := rater.Call("ApierV1.SetTPDestination", dstDe2, &reply); err!=nil { + t.Error("Got error on second ApierV1.SetTPDestination: ", err.Error()) + } else if reply != "OK" { + t.Error("Calling ApierV1.SetTPDestination got reply: ", reply) + } + // Check missing params + if err := rater.Call("ApierV1.SetTPDestination", new(utils.TPDestination), &reply); err==nil { + t.Error("Calling ApierV1.SetTPDestination, expected error, received: ", reply) + } else if err.Error() != "MANDATORY_IE_MISSING:[TPid DestinationId Prefixes]" { + t.Error("Calling ApierV1.SetTPDestination got unexpected error: ", err.Error()) + } + // Test get + var rplyDstDe2 *utils.TPDestination + if err := rater.Call("ApierV1.GetTPDestination", AttrGetTPDestination{dstDe2.TPid, dstDe2.DestinationId}, &rplyDstDe2); err!=nil { + t.Error("Calling ApierV1.GetTPDestination, got error: ", err.Error()) + } else if !reflect.DeepEqual(dstDe2, rplyDstDe2) { + t.Errorf("Calling ApierV1.GetTPDestination expected: %v, received: %v", dstDe2, rplyDstDe2) + } + // Test remove + if err := rater.Call("ApierV1.RemTPDestination", AttrGetTPDestination{dstDe2.TPid, dstDe2.DestinationId}, &reply); err!=nil { + t.Error("Calling ApierV1.RemTPTiming, got error: ", err.Error()) + } else if reply != "OK" { + t.Error("Calling ApierV1.RemTPTiming received: ", reply) + } + // Test getIds + var rplyDstIds []string + expectedDstIds := []string{"FS_USERS", "GERMANY", "GERMANY_MOBILE"} + if err := rater.Call("ApierV1.GetTPDestinationIds", AttrGetTPDestinationIds{dstDe.TPid}, &rplyDstIds); err!=nil { + t.Error("Calling ApierV1.GetTPDestinationIds, got error: ", err.Error()) + } else if !reflect.DeepEqual(expectedDstIds, rplyDstIds) { + t.Errorf("Calling ApierV1.GetTPDestinationIds expected: %v, received: %v", expectedDstIds, rplyDstIds) + } +} + + diff --git a/apier/v1/tpdestinations.go b/apier/v1/tpdestinations.go index c6ea9249d..2efe3c9b7 100644 --- a/apier/v1/tpdestinations.go +++ b/apier/v1/tpdestinations.go @@ -25,14 +25,8 @@ import ( "github.com/cgrates/cgrates/utils" ) -type ApierTPDestination struct { - TPid string // Tariff plan id - DestinationId string // Destination id - Prefixes []string // Prefixes attached to this destination -} - // Creates a new destination within a tariff plan -func (self *ApierV1) SetTPDestination(attrs ApierTPDestination, reply *string) error { +func (self *ApierV1) SetTPDestination(attrs utils.TPDestination, reply *string) error { if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationId", "Prefixes"}); len(missing) != 0 { //Params missing return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } @@ -49,7 +43,7 @@ type AttrGetTPDestination struct { } // Queries a specific destination -func (self *ApierV1) GetTPDestination(attrs AttrGetTPDestination, reply *ApierTPDestination) error { +func (self *ApierV1) GetTPDestination(attrs AttrGetTPDestination, reply *utils.TPDestination) error { if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationId"}); len(missing) != 0 { //Params missing return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } @@ -58,7 +52,7 @@ func (self *ApierV1) GetTPDestination(attrs AttrGetTPDestination, reply *ApierTP } else if dst == nil { return errors.New(utils.ERR_NOT_FOUND) } else { - *reply = ApierTPDestination{attrs.TPid, dst.Id, dst.Prefixes} + *reply = utils.TPDestination{attrs.TPid, dst.Id, dst.Prefixes} } return nil } diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 4a7f4c2b6..b2e7e7c72 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -24,6 +24,13 @@ import ( "time" ) +type TPDestination struct { + TPid string // Tariff plan id + DestinationId string // Destination id + Prefixes []string // Prefixes attached to this destination +} + + // This file deals with tp_* data definition type TPRate struct {