public void ItCanBeSerializedToJson()
        {
            // Arrange
            var rule = new RuleApiModel {
                Enabled = true
            };

            // Act
            var target = new AsaRefDataRule(rule);
            var json   = JsonConvert.SerializeObject(target);

            this.log.WriteLine("JSON: " + json);

            // Assert
            var expectedJSON = JsonConvert.SerializeObject(new
            {
                Id                = (string)null,
                Name              = (string)null,
                Description       = (string)null,
                GroupId           = (string)null,
                Severity          = (string)null,
                AggregationWindow = (string)null,
                Fields            = new string[] { },
                Actions           = new List <IActionApiModel>(),
                __rulefilterjs    = "return true;"
            });

            Assert.Equal(expectedJSON, json);
        }
Example #2
0
        public void JSONSerializationOfOneRuleWithoutConditionsWithoutTimeWindowIsCorrect()
        {
            // Arrange
            var rule = new RuleApiModel
            {
                Enabled     = true,
                Id          = Guid.NewGuid().ToString(),
                Name        = Guid.NewGuid().ToString(),
                Description = Guid.NewGuid().ToString(),
                GroupId     = Guid.NewGuid().ToString(),
                Severity    = Guid.NewGuid().ToString(),
                Calculation = SOURCE_NO_AGGREGATION
            };

            // Act
            var target = new AsaRefDataRule(rule);
            var json   = JsonConvert.SerializeObject(target);

            this.log.WriteLine("JSON: " + json);

            // Assert
            var expectedJSON = JsonConvert.SerializeObject(new
            {
                Id                = rule.Id,
                Name              = rule.Name,
                Description       = rule.Description,
                GroupId           = rule.GroupId,
                Severity          = rule.Severity,
                AggregationWindow = ASA_AGGREGATION_NONE,
                Fields            = new string[] { },
                __rulefilterjs    = "return true;"
            });

            Assert.Equal(expectedJSON, json);
        }
Example #3
0
        public void JSONSerializationOfOneRuleWithConditionsWithTimeWindowIsCorrect(string sourceOperator, string jsOperator)
        {
            // Arrange
            var rule = new RuleApiModel
            {
                Enabled     = true,
                Id          = Guid.NewGuid().ToString(),
                Name        = Guid.NewGuid().ToString(),
                Description = Guid.NewGuid().ToString(),
                GroupId     = Guid.NewGuid().ToString(),
                Severity    = Guid.NewGuid().ToString(),
                Calculation = SOURCE_AVG_AGGREGATOR,
                TimePeriod  = SOURCE_5MINS_AGGREGATION,
                Conditions  = new List <ConditionApiModel>
                {
                    new ConditionApiModel
                    {
                        Field    = Guid.NewGuid().ToString("N"),
                        Operator = sourceOperator,
                        Value    = (new Random().Next(-10000, 10000) / 1.1).ToString()
                    },
                    new ConditionApiModel
                    {
                        Field    = Guid.NewGuid().ToString("N"),
                        Operator = sourceOperator,
                        Value    = new Random().Next(-10000, 10000).ToString()
                    },
                    new ConditionApiModel
                    {
                        Field    = Guid.NewGuid().ToString("N"),
                        Operator = sourceOperator,
                        Value    = (new Random().Next(-10000, 10000) / 1.3).ToString()
                    },
                }
            };

            // Act
            var target = new AsaRefDataRule(rule);
            var json   = JsonConvert.SerializeObject(target);

            this.log.WriteLine("JSON: " + json);

            // Assert
            var cond1        = $"record.__aggregates.{rule.Conditions[0].Field}{ASA_JS_AVG_FIELD} {jsOperator} {rule.Conditions[0].Value}";
            var cond2        = $"record.__aggregates.{rule.Conditions[1].Field}{ASA_JS_AVG_FIELD} {jsOperator} {rule.Conditions[1].Value}";
            var cond3        = $"record.__aggregates.{rule.Conditions[2].Field}{ASA_JS_AVG_FIELD} {jsOperator} {rule.Conditions[2].Value}";
            var expectedJSON = JsonConvert.SerializeObject(new
            {
                Id                = rule.Id,
                Name              = rule.Name,
                Description       = rule.Description,
                GroupId           = rule.GroupId,
                Severity          = rule.Severity,
                AggregationWindow = ASA_AGGREGATION_WINDOW_TUMBLING_5MINS,
                Fields            = rule.Conditions.Select(x => x.Field),
                __rulefilterjs    = $"return ({cond1} && {cond2} && {cond3}) ? true : false;"
            });

            Assert.Equal(expectedJSON, json);
        }
        public void ItDoesntHaveFieldsByDefault()
        {
            // Act
            var target1 = new AsaRefDataRule();
            var target2 = new AsaRefDataRule(new RuleApiModel());

            // Assert
            Assert.Equal(0, target1.Fields.Count);
            Assert.Equal(0, target2.Fields.Count);
        }
        public void JSONSerializationOfRulesWithMinMaxAvgEtcAggregationIsCorrect(string aggregator, string jsField)
        {
            // Arrange
            var rule = new RuleApiModel
            {
                Enabled     = true,
                Id          = Guid.NewGuid().ToString(),
                Name        = Guid.NewGuid().ToString(),
                Description = Guid.NewGuid().ToString(),
                GroupId     = Guid.NewGuid().ToString(),
                Severity    = Guid.NewGuid().ToString(),
                Actions     = new List <IActionApiModel>()
                {
                    GetSampleActionData()
                },
                Calculation = aggregator,
                TimePeriod  = SOURCE_5MINS_AGGREGATION,
                Conditions  = new List <ConditionApiModel>
                {
                    new ConditionApiModel {
                        Field = "foo", Operator = ">", Value = "123"
                    }
                }
            };

            // Act
            var target = new AsaRefDataRule(rule);
            var json   = JsonConvert.SerializeObject(target);

            this.log.WriteLine("JSON: " + json);

            // Assert
            var cond1        = $"record.__aggregates.{rule.Conditions[0].Field}{jsField} > {rule.Conditions[0].Value}";
            var expectedJSON = JsonConvert.SerializeObject(new
            {
                Id                = rule.Id,
                Name              = rule.Name,
                Description       = rule.Description,
                GroupId           = rule.GroupId,
                Severity          = rule.Severity,
                AggregationWindow = ASA_AGGREGATION_WINDOW_TUMBLING_5MINS,
                Fields            = rule.Conditions.Select(x => x.Field),
                Actions           = rule.Actions,
                __rulefilterjs    = $"return ({cond1}) ? true : false;"
            });

            Assert.Equal(expectedJSON, json);
        }
        public void JSONSerializationOfOneRuleWithoutConditionsWithTimeWindowIsCorrect(long sourceAggr, string asaAggr)
        {
            // Arrange
            var rule = new RuleApiModel
            {
                Enabled     = true,
                Id          = Guid.NewGuid().ToString(),
                Name        = Guid.NewGuid().ToString(),
                Description = Guid.NewGuid().ToString(),
                GroupId     = Guid.NewGuid().ToString(),
                Severity    = Guid.NewGuid().ToString(),
                Actions     = new List <IActionApiModel>()
                {
                    GetSampleActionData()
                },
                Calculation = SOURCE_AVG_AGGREGATOR,
                TimePeriod  = sourceAggr
            };

            // Act
            var target = new AsaRefDataRule(rule);
            var json   = JsonConvert.SerializeObject(target);

            this.log.WriteLine("JSON: " + json);

            // Assert
            var expectedJSON = JsonConvert.SerializeObject(new
            {
                Id                = rule.Id,
                Name              = rule.Name,
                Description       = rule.Description,
                GroupId           = rule.GroupId,
                Severity          = rule.Severity,
                AggregationWindow = asaAggr,
                Fields            = new string[] { },
                Actions           = rule.Actions,
                __rulefilterjs    = "return true;"
            });

            Assert.Equal(expectedJSON, json);
        }