예제 #1
0
        // Test that default JSON serialization is indeed camelCase for Gridwich models.
        // Ignore pretty-print formatting -- test will strip out spaces so will work with either be Indented or None.
        public void JSON_Serialization_Casing2()
        {
            RequestBlobAnalysisCreateDTO src = new RequestBlobAnalysisCreateDTO()
            {
                BlobUri = new Uri("http://xyz.com/")
            };
            string srcJsonExpected = $"{{'blobUri':'{src.BlobUri}'}}".Replace('\'', '"');

            var srcJsonActual = JsonConvert.SerializeObject(src);

            srcJsonActual = Regex.Replace(srcJsonActual, @"\s", string.Empty); // get rid of whitespace (to make formatting Indented & None equivalent)

            // Console.WriteLine("Expected = '{0}'\nActual   = '{1}'", srcJsonExpected, srcJsonActual);

            (srcJsonExpected == srcJsonActual).ShouldBeTrue();
        }
        public async Task HandleAsync_ShouldReturnFalseAndLogEvent_WhenPublishingThrows()
        {
            // Arrange
            var data = new RequestBlobAnalysisCreateDTO()
            {
                AnalyzerSpecificData = JsonHelpers.JsonToJObject(_expectedAnalyzerSpecificData, true),
                BlobUri          = new Uri(_expectedInboxUrl),
                OperationContext = JsonHelpers.DeserializeOperationContext(_expectedOperationContext),
            };
            var topicEndpointUri = new Uri("https://www.topichost.com");
            var testEvent        = new EventGridEvent
            {
                EventType   = CustomEventTypes.RequestBlobAnalysisCreate,
                DataVersion = "1.0",
                Data        = JsonConvert.SerializeObject(data)
            };
            JObject report = JsonHelpers.JsonToJObject("{}", true);

            // Arrange Mocks
            Mock.Get(_settingsProvider)
            .Setup(x => x.GetAppSettingsValue(Publishing.TopicOutboundEndpointSettingName))
            .Returns(topicEndpointUri.ToString());
            JObject blobMetadata = null;

            Mock.Get(_storageService)
            .Setup(x => x.GetBlobMetadataAsync(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>()))
            .ReturnsAsync(blobMetadata);
            Mock.Get(_eventGridPublisher)
            .Setup(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>()))
            .ThrowsAsync(new InvalidOperationException());
            Mock.Get(_mediaInfoReportService)
            .Setup(x => x.GetMediaInfoCompleteInformForUriAsync(data.BlobUri, It.IsAny <StorageClientProviderContext>()))
            .ReturnsAsync(report);

            // Act
            var handleAsyncResult = await _handler.HandleAsync(testEvent).ConfigureAwait(true);

            // Assert
            handleAsyncResult.ShouldBe(false);
            Mock.Get(_logger).Verify(x =>
                                     x.LogExceptionObject(LogEventIds.FailedCriticallyToPublishEvent,
                                                          It.IsAny <InvalidOperationException>(), It.IsAny <object>()),
                                     Times.AtLeastOnce,
                                     "An exception should be logged when the publishing fails");
        }
        public async Task HandleAsync_ShouldReturnTrueAndNotLog_WhenNoErrors()
        {
            // Arrange
            var data = new RequestBlobAnalysisCreateDTO()
            {
                AnalyzerSpecificData = JsonHelpers.JsonToJObject(_expectedAnalyzerSpecificData, true),
                BlobUri          = new Uri(_expectedInboxUrl),
                OperationContext = JsonHelpers.DeserializeOperationContext(_expectedOperationContext),
            };
            var topicEndpointUri = new Uri("https://www.topichost.com");
            var testEvent        = new EventGridEvent
            {
                EventType   = CustomEventTypes.RequestBlobAnalysisCreate,
                DataVersion = "1.0",
                Data        = JsonConvert.SerializeObject(data)
            };
            const string   connectionString = "CONNECTION_STRING";
            JObject        report           = JsonHelpers.JsonToJObject("{}", true);
            JObject        metadata         = JsonHelpers.JsonToJObject("{}", true);
            EventGridEvent resultEvent      = null;

            // Arrange Mocks
            Mock.Get(_settingsProvider)
            .Setup(x => x.GetAppSettingsValue(Publishing.TopicOutboundEndpointSettingName))
            .Returns(topicEndpointUri.ToString());
            Mock.Get(_storageService)
            .Setup(x => x.GetConnectionStringForAccount(It.IsAny <string>(), It.IsAny <StorageClientProviderContext>()))
            .Returns(connectionString);
            Mock.Get(_eventGridPublisher)
            .Setup(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>()))
            .Callback <EventGridEvent>((eventGridEvent) => resultEvent = eventGridEvent)
            .ReturnsAsync(true);
            Mock.Get(_mediaInfoReportService)
            .Setup(x => x.GetMediaInfoCompleteInformForUriAsync(data.BlobUri, It.IsAny <StorageClientProviderContext>()))
            .ReturnsAsync(report);
            Mock.Get(_storageService)
            .Setup(x => x.GetBlobMetadataAsync(data.BlobUri, It.IsAny <StorageClientProviderContext>()))
            .ReturnsAsync(metadata);

            // Act
            var handleAsyncResult = await _handler.HandleAsync(testEvent).ConfigureAwait(true);

            // Assert positive results
            handleAsyncResult.ShouldBe(true, "handleAsync should always return true");

            resultEvent.ShouldNotBeNull();
            resultEvent.Data.ShouldNotBeNull();
            resultEvent.Data.ShouldBeOfType(typeof(ResponseBlobAnalysisSuccessDTO));

            ResponseBlobAnalysisSuccessDTO eventData = (ResponseBlobAnalysisSuccessDTO)resultEvent.Data;

            eventData.AnalysisResult.ShouldNotBeNull();
            eventData.BlobMetadata.ShouldNotBeNull();
            eventData.BlobUri.ToString().ShouldBe(_expectedInboxUrl);
            // eventData.Md5.ShouldBe(_expectedMd5);  // TODO
            eventData.AnalysisResult.ShouldBe(report);

            Mock.Get(_logger).Verify(x =>
                                     x.LogEventObject(LogEventIds.StartingEventHandling, It.IsAny <object>()),
                                     Times.Once,
                                     "An accepted event type should log information when it is about to begin");
            Mock.Get(_logger).Verify(x =>
                                     x.LogEventObject(LogEventIds.AboutToCallAnalysisDeliveryEntry, It.IsAny <object>()),
                                     Times.Once,
                                     "An accepted event type should log information when it is about to begin analysis");
            Mock.Get(_logger).Verify(x =>
                                     x.LogEventObject(LogEventIds.AnalysisOfDeliveryFileSuccessful, It.IsAny <object>()),
                                     Times.Once,
                                     "An accepted event type should log information when analysis is successful");
            Mock.Get(_logger).Verify(x =>
                                     x.LogEventObject(LogEventIds.FinishedEventHandling, It.IsAny <object>()),
                                     Times.Once,
                                     "An accepted event type should log information when the event handling is complete");
            // Assert negative results
            Mock.Get(_logger).Verify(x =>
                                     x.LogExceptionObject(LogEventIds.EventNotSupported, It.IsAny <Exception>(), It.IsAny <object>()),
                                     Times.Never,
                                     "An exception should NOT be logged when the publishing succeeds");
            Mock.Get(_logger).Verify(x =>
                                     x.LogExceptionObject(LogEventIds.FailedToPublishEvent, It.IsAny <Exception>(), It.IsAny <object>()),
                                     Times.Never,
                                     "An exception should NOT be logged when the publishing succeeds");
        }
        public async Task HandleAsync_ShouldHandleReportGenerationResult_WhenReportGenerationFails()
        {
            // Arrange
            var fileUri = new Uri(_expectedInboxUrl);
            var data    = new RequestBlobAnalysisCreateDTO()
            {
                AnalyzerSpecificData = JsonHelpers.JsonToJObject(_expectedAnalyzerSpecificData, true),
                BlobUri          = new Uri(_expectedInboxUrl),
                OperationContext = JsonHelpers.DeserializeOperationContext(_expectedOperationContext),
            };
            var topicEndpointUri = new Uri("https://www.topichost.com");
            var appInsightsUri   = new Uri("https://www.appinsights.com");
            var testEvent        = new EventGridEvent
            {
                EventType   = CustomEventTypes.RequestBlobAnalysisCreate,
                DataVersion = "1.0",
                Data        = JsonConvert.SerializeObject(data)
            };
            var storageContext = new StorageClientProviderContext(_expectedOperationContext);

            const string   connectionString = "CONNECTION_STRING";
            EventGridEvent resultEvent      = null;

            // Arrange Mocks
            Mock.Get(_settingsProvider)
            .Setup(x => x.GetAppSettingsValue(Publishing.TopicOutboundEndpointSettingName))
            .Returns(topicEndpointUri.ToString());
            Mock.Get(_storageService)
            .Setup(x => x.GetConnectionStringForAccount(It.IsAny <string>(), It.IsAny <StorageClientProviderContext>()))
            .Returns(connectionString);
            Mock.Get(_eventGridPublisher)
            .Setup(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>()))
            .Callback <EventGridEvent>((eventGridEvent) => resultEvent = eventGridEvent)
            .ReturnsAsync(true);
            // Note the exact LogEventId does not matter
            Mock.Get(_mediaInfoReportService)
            .Setup(x => x.GetMediaInfoCompleteInformForUriAsync(fileUri, It.IsAny <StorageClientProviderContext>()))
            .ThrowsAsync(new GridwichMediaInfoLibException("Error", LogEventIds.MediaInfoLibOpenBufferInitFailed, storageContext.ClientRequestIdAsJObject));
            JObject blobMetadata = null;

            Mock.Get(_storageService)
            .Setup(x => x.GetBlobMetadataAsync(It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>()))
            .ReturnsAsync(blobMetadata);
            Mock.Get(_logger)
            .Setup(x => x.LogEvent(
                       out appInsightsUri,
                       LogEventIds.AnalysisOfDeliveryFileFailed.Id,
                       It.IsAny <string>()));
            Mock.Get(_logger)
            .Setup(x => x.LogExceptionObject(
                       out appInsightsUri,
                       LogEventIds.FailedCriticallyToPublishEvent,
                       It.IsAny <GridwichMediaInfoLibException>(),
                       It.IsAny <object>()));

            // Act
            var handleAsyncResult = await _handler.HandleAsync(testEvent).ConfigureAwait(true);

            // Assert positive results
            handleAsyncResult.ShouldBe(false, "handleAsync should still make an event on failure");

            resultEvent.ShouldNotBeNull();
            resultEvent.Data.ShouldNotBeNull();
            resultEvent.Data.ShouldBeOfType(typeof(ResponseFailureDTO));

            var eventData = (ResponseFailureDTO)resultEvent.Data;

            eventData.HandlerId.ShouldBe("A7250940-98C8-4CC5-A66C-45A2972FF3A2");
            eventData.LogEventMessage.ShouldNotBeNullOrEmpty();
            eventData.EventHandlerClassName.ShouldNotBeNullOrEmpty();
            eventData.OperationContext.ShouldBe(JsonHelpers.JsonToJObject(_expectedOperationContext, true));

            Mock.Get(_logger).Verify(x =>
                                     x.LogExceptionObject(out appInsightsUri, It.IsAny <EventId>(),
                                                          It.IsAny <GridwichMediaInfoLibException>(), It.IsAny <object>()),
                                     Times.Once,
                                     "An exception should be logged when report generation fails");
        }