From ac5c0b0dfcd61095feb4639383f78b4b14a5e136 Mon Sep 17 00:00:00 2001 From: adragusin Date: Tue, 12 Nov 2019 17:26:29 +0200 Subject: [PATCH] Updated tests in sessions --- sessions/libsessions.go | 9 ++++ sessions/libsessions_test.go | 16 +++++++ sessions/sessions.go | 8 ---- sessions/sessions_test.go | 90 +++++++++++++++++++++++++++++++++++- 4 files changed, 114 insertions(+), 9 deletions(-) diff --git a/sessions/libsessions.go b/sessions/libsessions.go index e747fd053..c0814be43 100644 --- a/sessions/libsessions.go +++ b/sessions/libsessions.go @@ -20,6 +20,7 @@ package sessions import ( "math/rand" + "strings" "time" "github.com/cgrates/cgrates/engine" @@ -95,3 +96,11 @@ func GetSetCGRID(ev engine.MapEvent) (cgrID string) { } return } + +func getFlagIDs(flag string) []string { + flagWithIDs := strings.Split(flag, utils.InInFieldSep) + if len(flagWithIDs) <= 1 { + return nil + } + return strings.Split(flagWithIDs[1], utils.INFIELD_SEP) +} diff --git a/sessions/libsessions_test.go b/sessions/libsessions_test.go index fb539c270..0642ae37b 100644 --- a/sessions/libsessions_test.go +++ b/sessions/libsessions_test.go @@ -19,6 +19,7 @@ along with this program. If not, see package sessions import ( + "reflect" "testing" "time" @@ -130,3 +131,18 @@ func TestLibSessionSgetSessionTTL(t *testing.T) { t.Errorf("Unexpected ttl : %+v", ttl) } } + +func TestGetFlagIDs(t *testing.T) { + //empty check + rcv := getFlagIDs("") + var eOut []string + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected %s , received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } + //normal check + rcv = getFlagIDs("*attributes:ATTR1;ATTR2") + eOut = []string{"ATTR1", "ATTR2"} + if !reflect.DeepEqual(eOut, rcv) { + t.Errorf("Expected %s , received: %s", utils.ToJSON(eOut), utils.ToJSON(rcv)) + } +} diff --git a/sessions/sessions.go b/sessions/sessions.go index c2db60323..85b1043b9 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -1750,14 +1750,6 @@ func NewV1AuthorizeArgs(attrs bool, attributeIDs []string, return } -func getFlagIDs(flag string) []string { - flagWithIDs := strings.Split(flag, utils.InInFieldSep) - if len(flagWithIDs) <= 1 { - return nil - } - return strings.Split(flagWithIDs[1], utils.INFIELD_SEP) -} - // V1AuthorizeArgs are options available in auth request type V1AuthorizeArgs struct { GetAttributes bool diff --git a/sessions/sessions_test.go b/sessions/sessions_test.go index c9c723cf1..9f97f5261 100644 --- a/sessions/sessions_test.go +++ b/sessions/sessions_test.go @@ -874,6 +874,13 @@ func TestSessionSNewV1UpdateSessionArgs(t *testing.T) { if !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting %+v, received: %+v", expected, rply) } + //test with len(AttributeIDs) != 0 + attributeIDs := []string{"ATTR1", "ATTR2"} + rply = NewV1UpdateSessionArgs(false, attributeIDs, true, cgrEv, nil) + expected.AttributeIDs = []string{"ATTR1", "ATTR2"} + if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting %+v, received: %+v", expected, rply) + } } func TestSessionSNewV1TerminateSessionArgs(t *testing.T) { @@ -901,6 +908,19 @@ func TestSessionSNewV1TerminateSessionArgs(t *testing.T) { if !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting %+v, received: %+v", expected, rply) } + //test with len(thresholdIDs) != 0 && len(StatIDs) != 0 + thresholdIDs := []string{"ID1", "ID2"} + statIDs := []string{"test1", "test2"} + expected = &V1TerminateSessionArgs{ + CGREvent: cgrEv, + ThresholdIDs: []string{"ID1", "ID2"}, + StatIDs: []string{"test1", "test2"}, + } + rply = NewV1TerminateSessionArgs(false, false, false, thresholdIDs, false, statIDs, cgrEv, nil) + if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting %+v, received: %+v", expected, rply) + } + } func TestSessionSNewV1ProcessMessageArgs(t *testing.T) { @@ -935,9 +955,31 @@ func TestSessionSNewV1ProcessMessageArgs(t *testing.T) { if !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting %+v, received: %+v", expected, rply) } + //test with len(thresholdIDs) != 0 && len(StatIDs) != 0 + attributeIDs := []string{"ATTR1", "ATTR2"} + thresholdIDs := []string{"ID1", "ID2"} + statIDs := []string{"test3", "test4"} + + expected = &V1ProcessMessageArgs{ + AllocateResources: true, + GetAttributes: true, + CGREvent: cgrEv, + GetSuppliers: true, + SuppliersMaxCost: utils.MetaSuppliersEventCost, + SuppliersIgnoreErrors: true, + AttributeIDs: []string{"ATTR1", "ATTR2"}, + ThresholdIDs: []string{"ID1", "ID2"}, + StatIDs: []string{"test3", "test4"}, + } + rply = NewV1ProcessMessageArgs(true, attributeIDs, false, thresholdIDs, false, statIDs, true, false, true, true, true, cgrEv, nil, utils.Paginator{}) + if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting %+v, received: %+v", expected, rply) + } + } func TestSessionSNewV1InitSessionArgs(t *testing.T) { + //t1 cgrEv := &utils.CGREvent{ Tenant: "cgrates.org", ID: "Event", @@ -946,7 +988,35 @@ func TestSessionSNewV1InitSessionArgs(t *testing.T) { utils.Destination: "1002", }, } + attributeIDs := []string{"ATTR1", "ATTR2"} + thresholdIDs := []string{"test1", "test2"} + statIDs := []string{"test3", "test4"} expected := &V1InitSessionArgs{ + GetAttributes: true, + AllocateResources: true, + InitSession: true, + ProcessThresholds: true, + ProcessStats: true, + AttributeIDs: []string{"ATTR1", "ATTR2"}, + ThresholdIDs: []string{"test1", "test2"}, + StatIDs: []string{"test3", "test4"}, + CGREvent: cgrEv, + } + rply := NewV1InitSessionArgs(true, attributeIDs, true, thresholdIDs, true, statIDs, true, true, cgrEv, nil) + if !reflect.DeepEqual(expected, rply) { + t.Errorf("Expecting %+v, received: %+v", expected, rply) + } + + //t2 + cgrEv = &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "Event", + Event: map[string]interface{}{ + utils.Account: "1001", + utils.Destination: "1002", + }, + } + expected = &V1InitSessionArgs{ GetAttributes: true, AllocateResources: true, InitSession: true, @@ -954,7 +1024,7 @@ func TestSessionSNewV1InitSessionArgs(t *testing.T) { ProcessStats: true, CGREvent: cgrEv, } - rply := NewV1InitSessionArgs(true, nil, true, nil, true, nil, true, true, cgrEv, nil) + rply = NewV1InitSessionArgs(true, nil, true, nil, true, nil, true, true, cgrEv, nil) if !reflect.DeepEqual(expected, rply) { t.Errorf("Expecting %+v, received: %+v", expected, rply) } @@ -1412,3 +1482,21 @@ func TestSessionSgetSessionIDsMatchingIndexes(t *testing.T) { t.Errorf("Expected %s , received: %s", utils.ToJSON(expmatchingSRuns), utils.ToJSON(matchingSRuns)) } } + +/* + +type testRPCClientConnection struct {} +func (testRPCClientConnection) Call(string, interface{}, interface{}) error { return nil } +func TestNewSessionS(t *testing.T) { + cgrCGF := &config.CGRConfig{MaxCallDuration: time.Duration(10)} + + + + rpc := rpcclient.RpcClientConnection{Call(string, interface{},interface{}) error {return nil}} + + } + //var rcv *SessionS + sS := NewSessionS(cgrCGF, rpc, nil, nil, nil, nil, nil, nil, nil, nil, nil, "UTC") + fmt.println("rcv: ",sS) +} +*/