From a2bc6f5cd39d69524f9ea88028dfc03754dff571 Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Mon, 7 Jun 2021 16:55:24 +0300 Subject: [PATCH] Update integration tests for threshold apis --- apis/thresholds_it_test.go | 148 +++++++++++++++--- .../samples/thresholds_internal/cgrates.json | 1 + .../samples/thresholds_mongo/cgrates.json | 2 + .../samples/thresholds_mysql/cgrates.json | 3 +- 4 files changed, 135 insertions(+), 19 deletions(-) diff --git a/apis/thresholds_it_test.go b/apis/thresholds_it_test.go index e037923cb..e3e83c006 100644 --- a/apis/thresholds_it_test.go +++ b/apis/thresholds_it_test.go @@ -54,9 +54,15 @@ var ( testThresholdsGetThresholdProfileCount, testThresholdsGetThresholdsForEvent, testThresholdsRemoveThresholdProfiles, - testThresholdsGetThresholdProfileAfterRemove, + testThresholdsGetThresholdsAfterRemove, + + // test if actions are executed properly when thresholds are hit + testThresholdsSetActionProfileBeforeProcessEv, testThresholdsSetThresholdProfilesBeforeProcessEv, testThresholdsProcessEvent, + testThresholdsGetThresholdsAfterFirstEvent, + testThresholdsProcessEvent, + testThresholdsGetThresholdsAfterSecondEvent, testThresholdsPing, testThresholdsKillEngine, } @@ -134,7 +140,7 @@ func testThresholdsPing(t *testing.T) { } func testThresholdsGetThresholdBeforeSet(t *testing.T) { - var rplyTh *engine.Threshold + var rplyTh engine.Threshold if err := thRPC.Call(context.Background(), utils.ThresholdSv1GetThreshold, &utils.TenantWithAPIOpts{ @@ -151,13 +157,6 @@ func testThresholdsSetActionProfile(t *testing.T) { Actions: []*engine.APAction{ { ID: "actID", - // Type: utils.MetaHTTPPost, - // Diktats: []*engine.APDiktat{ - // { - // Path: rsSrv.URL, - // }, - // }, - // TTL: time.Duration(time.Minute), }, }, }, @@ -238,7 +237,7 @@ func testThresholdsGetThresholdAfterSet(t *testing.T) { t.Error(err) } else if !reflect.DeepEqual(rplyTh, expTh) { t.Errorf("expected: <%+v>, \nreceived: <%+v>", - utils.ToJSON(rplyTh), utils.ToJSON(expTh)) + utils.ToJSON(expTh), utils.ToJSON(rplyTh)) } if err := thRPC.Call(context.Background(), utils.AdminSv1GetThresholdProfile, @@ -276,7 +275,7 @@ func testThresholdsGetThresholdAfterSet(t *testing.T) { t.Error(err) } else if !reflect.DeepEqual(rplyTh, expTh) { t.Errorf("expected: <%+v>, \nreceived: <%+v>", - utils.ToJSON(rplyTh), utils.ToJSON(expTh)) + utils.ToJSON(expTh), utils.ToJSON(rplyTh)) } if err := thRPC.Call(context.Background(), utils.AdminSv1GetThresholdProfile, @@ -341,7 +340,7 @@ func testThresholdsGetThresholdsForEvent(t *testing.T) { ThresholdIDs: []string{"THD_1", "THD_2"}, CGREvent: &utils.CGREvent{ Tenant: "cgrates.org", - ID: "ThresholdEventTest", + ID: "GetThresholdEventTest", Event: map[string]interface{}{ utils.AccountField: "1001", }, @@ -396,17 +395,52 @@ func testThresholdsRemoveThresholdProfiles(t *testing.T) { } } -func testThresholdsGetThresholdProfileAfterRemove(t *testing.T) { - var rplyTh engine.ThresholdProfile - - if err := thRPC.Call(context.Background(), utils.ThresholdSv1GetThreshold, - &utils.TenantWithAPIOpts{ +func testThresholdsGetThresholdsAfterRemove(t *testing.T) { + args := &engine.ThresholdsArgsProcessEvent{ + ThresholdIDs: []string{"THD_1", "THD_2"}, + CGREvent: &utils.CGREvent{ Tenant: "cgrates.org", - }, &rplyTh); err == nil || err.Error() != utils.ErrNotFound.Error() { + ID: "RemThresholdEventTest", + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + }, + } + + var rplyThs engine.Thresholds + if err := thRPC.Call(context.Background(), utils.ThresholdSv1GetThresholdsForEvent, + args, &rplyThs); err == nil || err.Error() != utils.ErrNotFound.Error() { t.Errorf("expected: <%+v>, \nreceived: <%+v>", utils.ErrNotFound, err) } } +func testThresholdsSetActionProfileBeforeProcessEv(t *testing.T) { + actPrf := &engine.ActionProfileWithAPIOpts{ + ActionProfile: &engine.ActionProfile{ + Tenant: "cgrates.org", + ID: "actPrfID", + Actions: []*engine.APAction{ + { + ID: "actID", + Type: utils.MetaResetThreshold, + }, + }, + Targets: map[string]utils.StringSet{ + utils.MetaThresholds: { + "THD_1": struct{}{}, + "THD_2": struct{}{}, + }, + }, + }, + } + + var reply *string + if err := thRPC.Call(context.Background(), utils.AdminSv1SetActionProfile, + actPrf, &reply); err != nil { + t.Error(err) + } +} + func testThresholdsSetThresholdProfilesBeforeProcessEv(t *testing.T) { thPrf1 := &engine.ThresholdProfileWithAPIOpts{ ThresholdProfile: &engine.ThresholdProfile{ @@ -471,3 +505,81 @@ func testThresholdsProcessEvent(t *testing.T) { } } } + +func testThresholdsGetThresholdsAfterFirstEvent(t *testing.T) { + args := &engine.ThresholdsArgsProcessEvent{ + ThresholdIDs: []string{"THD_1", "THD_2"}, + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "ThresholdEventTest", + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + }, + } + expThs := engine.Thresholds{ + &engine.Threshold{ + Tenant: "cgrates.org", + ID: "THD_2", + Hits: 0, + }, + &engine.Threshold{ + Tenant: "cgrates.org", + ID: "THD_1", + Hits: 1, + }, + } + + var rplyThs engine.Thresholds + if err := thRPC.Call(context.Background(), utils.ThresholdSv1GetThresholdsForEvent, + args, &rplyThs); err != nil { + t.Error(err) + } else { + for idx, thd := range rplyThs { + thd.Snooze = expThs[idx].Snooze + } + if !reflect.DeepEqual(rplyThs, expThs) { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", + utils.ToJSON(expThs), utils.ToJSON(rplyThs)) + } + } +} + +func testThresholdsGetThresholdsAfterSecondEvent(t *testing.T) { + args := &engine.ThresholdsArgsProcessEvent{ + ThresholdIDs: []string{"THD_1", "THD_2"}, + CGREvent: &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "ThresholdEventTest", + Event: map[string]interface{}{ + utils.AccountField: "1001", + }, + }, + } + expThs := engine.Thresholds{ + &engine.Threshold{ + Tenant: "cgrates.org", + ID: "THD_2", + Hits: 0, + }, + &engine.Threshold{ + Tenant: "cgrates.org", + ID: "THD_1", + Hits: 2, + }, + } + + var rplyThs engine.Thresholds + if err := thRPC.Call(context.Background(), utils.ThresholdSv1GetThresholdsForEvent, + args, &rplyThs); err != nil { + t.Error(err) + } else { + for idx, thd := range rplyThs { + thd.Snooze = expThs[idx].Snooze + } + if !reflect.DeepEqual(rplyThs, expThs) { + t.Errorf("expected: <%+v>, \nreceived: <%+v>", + utils.ToJSON(expThs), utils.ToJSON(rplyThs)) + } + } +} diff --git a/data/conf/samples/thresholds_internal/cgrates.json b/data/conf/samples/thresholds_internal/cgrates.json index a52ade471..e1a2bac5d 100644 --- a/data/conf/samples/thresholds_internal/cgrates.json +++ b/data/conf/samples/thresholds_internal/cgrates.json @@ -16,6 +16,7 @@ "actions": { "enabled": true, + "thresholds_conns": ["*internal"], }, "thresholds": { diff --git a/data/conf/samples/thresholds_mongo/cgrates.json b/data/conf/samples/thresholds_mongo/cgrates.json index 6eae47859..d97ace58c 100644 --- a/data/conf/samples/thresholds_mongo/cgrates.json +++ b/data/conf/samples/thresholds_mongo/cgrates.json @@ -20,10 +20,12 @@ "actions": { "enabled": true, + "thresholds_conns": ["*internal"], }, "thresholds": { "enabled": true, + "store_interval": "-1", "actions_conns": ["*internal"], }, diff --git a/data/conf/samples/thresholds_mysql/cgrates.json b/data/conf/samples/thresholds_mysql/cgrates.json index e75bc579f..a64cc47c0 100644 --- a/data/conf/samples/thresholds_mysql/cgrates.json +++ b/data/conf/samples/thresholds_mysql/cgrates.json @@ -17,11 +17,12 @@ "actions": { "enabled": true, + "thresholds_conns": ["*internal"], }, "thresholds": { "enabled": true, - "store_interval": "", + "store_interval": "-1", "actions_conns": ["*internal"], },