Пример #1
0
        public async void ShouldRunUnsuccessfullyOnInvalidServiceBusMessage()
        {
            // create message to pass to Run method that will cause deserialization error
            dynamic iconicsFault = new
            {
                PartitionId = "PartitionId",
            };
            string  messageBody = JsonConvert.SerializeObject(iconicsFault);
            Message message     = new Message(Encoding.UTF8.GetBytes(messageBody));

            // set actualMessageContents equal to the first parameter passed into the SendMessageToErrorQueue method which should be called within the Run method
            string actualMessageContents = null;

            this.errorQueueServiceMock.Setup(service => service.SendMessageToErrorQueueAsync(It.IsAny <string>(), It.IsAny <string>())).Callback <string, string>((messageContents, validationMessage) => actualMessageContents = messageContents);

            using FakeTelemetryChannel fakeTelemetryChannel = new FakeTelemetryChannel();

            using TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration(INSTRUMENTATIONID, fakeTelemetryChannel);

            var durableClientMock = new Mock <IDurableEntityClient>();

            // instantiate function class and run function
            CreateAlertFromIconicsFault function = new CreateAlertFromIconicsFault(telemetryConfiguration, this.dynamicsEntityServiceMock.Object, this.validationServiceMock.Object, this.errorQueueServiceMock.Object, this.instanceId, this.httpClientMock.Object);
            await function.Run(message, this.logger).ConfigureAwait(false);

            this.errorQueueServiceMock.Verify(x => x.SendMessageToErrorQueueAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Once);

            // verify that SendMessageToErrorQueue received the correct messageContents parameter and that the correct error is logged
            string expectedErrorText = "Exception while processing message:";

            this.errorQueueServiceMock.Verify(service => service.SendMessageToErrorQueueAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Once);
            Assert.Equal(Encoding.UTF8.GetString(message.Body), actualMessageContents);
            Assert.Contains(expectedErrorText, this.logger.Logs[0], StringComparison.OrdinalIgnoreCase);
        }
Пример #2
0
        public async void ShouldRunSuccessfullyOnValidInActiveFault()
        {
            // mock IsValidFault and IsCdsServiceReady calls to return true
            string validationMessage;

            this.validationServiceMock.Setup(service => service.IsValidFault(It.IsAny <IconicsFault>(), out validationMessage)).Returns(true);
            this.dynamicsEntityServiceMock.Setup(service => service.IsCdsServiceReady()).Returns(true);

            var durableClientMock = new Mock <IDurableEntityClient>();

            // create message to pass to Run method
            IconicsFault iconicsFault = new IconicsFault()
            {
                FaultName            = "FaultName",
                AssetName            = "AssetName",
                AssetPath            = "AssetPath",
                FaultState           = "InActive",
                FaultActiveTime      = "2020-08-13T20:01:04.6565528Z",
                EventEnqueuedUtcTime = DateTime.UtcNow.ToString("o", CultureInfo.CurrentCulture),
            };

            string  messageBody = JsonConvert.SerializeObject(iconicsFault);
            Message message     = new Message(Encoding.UTF8.GetBytes(messageBody));

            // instantiate function class and run function
            using (TelemetryConfiguration tc = new TelemetryConfiguration(INSTRUMENTATIONID))
            {
                // instantiate function class and run function
                CreateAlertFromIconicsFault function = new CreateAlertFromIconicsFault(tc, this.dynamicsEntityServiceMock.Object, this.validationServiceMock.Object, this.errorQueueServiceMock.Object, this.instanceId, this.httpClientMock.Object);
                await function.Run(message, this.logger).ConfigureAwait(false);
            }

            // verify that alert is updated
            this.dynamicsEntityServiceMock.Verify(service => service.UpdateIoTAlert(It.IsAny <IconicsFault>(), It.IsAny <ILogger>(), It.IsAny <int>(), It.IsAny <int>()), Times.Once);
        }
Пример #3
0
        public async void ShouldThrowErrorOnValidFaultWhenCdsServiceIsNotReady()
        {
            // mock IsValidFault call to return true, but IsCdsServiceReady to return false
            string validationMessage;

            this.validationServiceMock.Setup(service => service.IsValidFault(It.IsAny <IconicsFault>(), out validationMessage)).Returns(true).Verifiable();
            this.dynamicsEntityServiceMock.Setup(service => service.IsCdsServiceReady()).Returns(false).Verifiable();

            // create message to pass to Run method
            IconicsFault iconicsFault = new IconicsFault()
            {
                FaultName = "FaultName",
                AssetName = "AssetName",
                AssetPath = "AssetPath",
            };

            string  messageBody = JsonConvert.SerializeObject(iconicsFault);
            Message message     = new Message(Encoding.UTF8.GetBytes(messageBody))
            {
                MessageId = Guid.NewGuid().ToString()
            };

            using FakeTelemetryChannel fakeTelemetryChannel = new FakeTelemetryChannel();

            using TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration(INSTRUMENTATIONID, fakeTelemetryChannel);

            using HttpResponseMessage httpResponseMsg = new HttpResponseMessage { StatusCode = HttpStatusCode.Accepted };

            // Setting up a mock message handler to ensure that HttpClient.PostAsJsonAsync is called.
            // PostAsJsonAsync is an extension method, and thus cannot use normal setup/verify steps.
            // Using a mock messge handler to verify the handler's SendAsync method is called.
            var mockHttpMessageHandler = new Mock <HttpMessageHandler>();

            mockHttpMessageHandler
            .Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(httpResponseMsg).Verifiable();

            using HttpClient httpClient = new HttpClient(mockHttpMessageHandler.Object);

            // instantiate function class and run function
            CreateAlertFromIconicsFault function = new CreateAlertFromIconicsFault(
                telemetryConfiguration,
                this.dynamicsEntityServiceMock.Object,
                this.validationServiceMock.Object,
                this.errorQueueServiceMock.Object,
                this.instanceId,
                httpClient);

            await Assert.ThrowsAsync <ApplicationException>(async() => await function.Run(message, this.logger).ConfigureAwait(false)).ConfigureAwait(false);

            // verify that error is thrown and that correct error is logged
            string expectedErrorText = "Failed to connect to the CDS";

            Assert.Contains(expectedErrorText, this.logger.Logs[1], StringComparison.OrdinalIgnoreCase);

            Mock.Verify();
        }
Пример #4
0
        public async void ShouldRecordIoTAlerCreatedTelemetryEvent()
        {
            // mock IsValidFault call to return true, but IsCdsServiceReady to return false
            string validationMessage;

            this.validationServiceMock.Setup(service => service.IsValidFault(It.IsAny <IconicsFault>(), out validationMessage)).Returns(true);
            this.dynamicsEntityServiceMock.Setup(service => service.IsCdsServiceReady()).Returns(true);

            var durableClientMock = new Mock <IDurableEntityClient>();

            using FakeTelemetryChannel fakeTelemetryChannel = new FakeTelemetryChannel();

            using TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration(INSTRUMENTATIONID, fakeTelemetryChannel);

            // create message to pass to Run method
            IconicsFault iconicsFault = new IconicsFault()
            {
                FaultName            = "FaultName",
                FaultActiveTime      = "2020-08-13T20:01:04.6565528Z",
                AssetName            = "AssetName",
                AssetPath            = "AssetPath",
                EventEnqueuedUtcTime = "2020-08-13T20:02:04.6565528Z",
            };

            string  messageBody = JsonConvert.SerializeObject(iconicsFault);
            Message message     = new Message(Encoding.UTF8.GetBytes(messageBody));

            // Instantiate function class and run function
            CreateAlertFromIconicsFault function = new CreateAlertFromIconicsFault(telemetryConfiguration, this.dynamicsEntityServiceMock.Object, this.validationServiceMock.Object, this.errorQueueServiceMock.Object, this.instanceId, this.httpClientMock.Object);

            await function.Run(message, this.logger).ConfigureAwait(false);

            string msg;

            this.validationServiceMock.Verify(x => x.IsValidFault(It.IsAny <IconicsFault>(), out msg), Times.Once);
            this.dynamicsEntityServiceMock.Verify(x => x.IsCdsServiceReady(), Times.Once);

            Assert.Single(fakeTelemetryChannel.SentEventTelemetries);

            if (fakeTelemetryChannel.SentEventTelemetries.Any())
            {
                var eventTelemetry = fakeTelemetryChannel.SentEventTelemetries.ToList()[0];

                Assert.NotNull(eventTelemetry);
                Assert.Equal("IoTAlertCreated", eventTelemetry.Name);

                Assert.Single(eventTelemetry.Metrics);
                Assert.True(eventTelemetry.Metrics.ContainsKey("AlertCreatedElapsedTimeMs"));
            }
        }
Пример #5
0
        public async void ShouldRunSuccessfullyOnValidActiveFault()
        {
            // mock IsValidFault and IsCdsServiceReady calls to return true
            string validationMessage;

            this.validationServiceMock.Setup(service => service.IsValidFault(It.IsAny <IconicsFault>(), out validationMessage)).Returns(true);
            this.dynamicsEntityServiceMock.Setup(service => service.IsCdsServiceReady()).Returns(true);

            var durableClientMock = new Mock <IDurableEntityClient>();

            // create message to pass to Run method
            IconicsFault iconicsFault = new IconicsFault()
            {
                FaultName            = "FaultName",
                AssetName            = "AssetName",
                AssetPath            = "AssetPath",
                FaultState           = "Active",
                FaultActiveTime      = "2020-08-13T20:01:04.6565528Z",
                EventEnqueuedUtcTime = DateTime.UtcNow.ToString("o", CultureInfo.CurrentCulture),
            };

            string  messageBody = JsonConvert.SerializeObject(iconicsFault);
            Message message     = new Message(Encoding.UTF8.GetBytes(messageBody));

            using FakeTelemetryChannel fakeTelemetryChannel = new FakeTelemetryChannel();

            using TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration(INSTRUMENTATIONID, fakeTelemetryChannel);

            // instantiate function class and run function
            CreateAlertFromIconicsFault function = new CreateAlertFromIconicsFault(telemetryConfiguration, this.dynamicsEntityServiceMock.Object, this.validationServiceMock.Object, this.errorQueueServiceMock.Object, this.instanceId, this.httpClientMock.Object);
            await function.Run(message, this.logger).ConfigureAwait(false);

            // verify that asset and alert are created
            string msg;

            this.validationServiceMock.Verify(x => x.IsValidFault(It.IsAny <IconicsFault>(), out msg), Times.Once);
            this.dynamicsEntityServiceMock.Verify(x => x.IsCdsServiceReady(), Times.Once);
            this.dynamicsEntityServiceMock.Verify(service => service.CreateAssetIfNotExists(It.IsAny <IconicsFault>(), It.IsAny <ILogger>()), Times.Once);
            this.dynamicsEntityServiceMock.Verify(service => service.CreateAlert(It.IsAny <IconicsFault>(), It.IsAny <Guid>(), It.IsAny <ILogger>()), Times.Once);

            var logMsg = this.logger.Logs[0];

            Assert.Equal(
                $"Received ICONICS fault with FaultName '{iconicsFault.FaultName}' and FaultActiveTime of '{iconicsFault.FaultActiveTime}' and AssetPath of '{iconicsFault.AssetPath}'.",
                logMsg);
        }
Пример #6
0
        public async void ShouldRunUnsuccessfullyOnInvalidFault()
        {
            // mock IsValidFault call to return false
            string validationMessage;

            this.validationServiceMock.Setup(service => service.IsValidFault(It.IsAny <IconicsFault>(), out validationMessage)).Returns(false);

            // create message to pass to Run method
            IconicsFault iconicsFault = new IconicsFault()
            {
                FaultName            = "FaultName",
                AssetName            = "AssetName",
                AssetPath            = "AssetPath",
                EventEnqueuedUtcTime = DateTime.UtcNow.ToString("o", CultureInfo.CurrentCulture),
            };

            string  messageBody = JsonConvert.SerializeObject(iconicsFault);
            Message message     = new Message(Encoding.UTF8.GetBytes(messageBody));

            var durableClientMock = new Mock <IDurableEntityClient>();

            // set actualMessageContents equal to the first parameter passed into the SendMessageToErrorQueue method which should be called within the Run method
            string actualMessageContents = null;

            this.errorQueueServiceMock.Setup(service => service.SendMessageToErrorQueueAsync(It.IsAny <string>(), It.IsAny <string>())).Callback <string, string>((messageContents, validationMessage) => actualMessageContents = messageContents);

            using FakeTelemetryChannel fakeTelemetryChannel = new FakeTelemetryChannel();

            using TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration(INSTRUMENTATIONID, fakeTelemetryChannel);

            // instantiate function class and run function
            CreateAlertFromIconicsFault function = new CreateAlertFromIconicsFault(telemetryConfiguration, this.dynamicsEntityServiceMock.Object, this.validationServiceMock.Object, this.errorQueueServiceMock.Object, this.instanceId, this.httpClientMock.Object);
            await function.Run(message, this.logger).ConfigureAwait(false);

            string msg;

            this.validationServiceMock.Verify(x => x.IsValidFault(It.IsAny <IconicsFault>(), out msg), Times.Once);

            // verify that SendMessageToErrorQueue was called and received the correct messageContents parameter
            string expectedErrorText = "Iconics fault data failed validation. Sending message to the error queue.";

            this.errorQueueServiceMock.Verify(service => service.SendMessageToErrorQueueAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Once);
            Assert.Equal(Encoding.UTF8.GetString(message.Body), actualMessageContents);
            Assert.Equal(expectedErrorText, this.logger.Logs[1]);
        }
Пример #7
0
        public async void ShouldLogWarningIfEventEnqueuedTimeUnavailable()
        {
            // mock IsValidFault call to return true, but IsCdsServiceReady to return false
            string validationMessage;

            this.validationServiceMock.Setup(service => service.IsValidFault(It.IsAny <IconicsFault>(), out validationMessage)).Returns(true);
            this.dynamicsEntityServiceMock.Setup(service => service.IsCdsServiceReady()).Returns(true);

            var durableClientMock = new Mock <IDurableEntityClient>();

            // create message to pass to Run method
            IconicsFault iconicsFault = new IconicsFault()
            {
                FaultName       = "FaultName",
                FaultActiveTime = "2020-08-13T20:01:04.6565528Z",
                AssetName       = "AssetName",
                AssetPath       = "AssetPath",
            };

            string  messageBody = JsonConvert.SerializeObject(iconicsFault);
            Message message     = new Message(Encoding.UTF8.GetBytes(messageBody));

            using FakeTelemetryChannel fakeTelemetryChannel = new FakeTelemetryChannel();

            using TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration(INSTRUMENTATIONID, fakeTelemetryChannel);

            // instantiate function class and run function
            CreateAlertFromIconicsFault function = new CreateAlertFromIconicsFault(telemetryConfiguration, this.dynamicsEntityServiceMock.Object, this.validationServiceMock.Object, this.errorQueueServiceMock.Object, this.instanceId, this.httpClientMock.Object);

            await function.Run(message, this.logger).ConfigureAwait(false);

            string msg;

            this.validationServiceMock.Verify(x => x.IsValidFault(It.IsAny <IconicsFault>(), out msg), Times.Once);
            this.dynamicsEntityServiceMock.Verify(x => x.IsCdsServiceReady(), Times.Once);

            Assert.Empty(fakeTelemetryChannel.SentEventTelemetries);
            Assert.Equal("Unable to determine IoT alert created elapsed time due to missing fault event enqueued time.", this.logger.Logs[1]);
        }