public void LogsError_OnImproperlyFormattedJson()
        {
            // Arrange
            // no commas
            string data = "{\"created\":1539035437937\"modified\":1539035437937\"rule.description\":\"description\"\"rule.severity\":\"Warning\"}";

            // Act
            var result = AlarmParser.ParseAlarmList(data, this.loggerMock.Object);

            // Assert
            this.loggerMock.Verify(x => x.Error(It.IsAny <string>(), It.IsAny <Func <object> >()));
            Assert.Empty(result);
        }
Esempio n. 2
0
        /**
         * Processes all alarms and executes any actions associated with the alarms
         */
        public async Task ProcessEventsAsync(PartitionContext context, IEnumerable <EventData> messages)
        {
            foreach (EventData eventData in messages)
            {
                if (eventData.Body.Array != null)
                {
                    string data = Encoding.UTF8.GetString(eventData.Body.Array);
                    IEnumerable <AsaAlarmApiModel> alarms = AlarmParser.ParseAlarmList(data, this.logger);
                    await this.actionManager.ExecuteAlarmActions(alarms);
                }
            }

            await context.CheckpointAsync();
        }
        public void CanParse_ProperlyFormattedJson()
        {
            // Arrange
            string data = "{\"created\":1539035437937,\"modified\":1539035437937,\"rule.description\":\"description\",\"rule.severity\":\"Warning\",\"rule.id\":\"TestRuleId\",\"rule.actions\":[{\"Type\":\"Email\",\"Parameters\":{\"Notes\":\"Test Note\",\"Subject\":\"Test Subject\",\"Recipients\":[\"[email protected]\"]}}],\"device.id\":\"Test Device Id\",\"device.msg.received\":1539035437937}" +
                          "{\"created\":1539035437940,\"modified\":1539035437940,\"rule.description\":\"description2\",\"rule.severity\":\"Info\",\"rule.id\":\"1234\",\"device.id\":\"Device Id\",\"device.msg.received\":1539035437940}";

            // Act
            var result = AlarmParser.ParseAlarmList(data, this.loggerMock.Object);

            // Assert
            AsaAlarmApiModel[] resultArray = result.ToArray();
            Assert.Equal(2, resultArray.Length);
            Assert.Equal("description", resultArray[0].RuleDescription);
            Assert.Equal("description2", resultArray[1].RuleDescription);
            Assert.Equal(1, resultArray[0].Actions.Count);
            var action = resultArray[0].Actions[0];

            Assert.Equal(ActionType.Email, action.Type);
            var recipients = ((EmailAction)action).GetRecipients();

            Assert.Single(recipients);
            Assert.Equal("*****@*****.**", recipients[0]);
        }