public async Task HandleAsync_ShouldFail_WhenReportedBlobUriIsNotUri(string data) { // Arrange var topicEndpointUri = new Uri("https://www.topichost.com"); var appInsightsUri = new Uri("https://www.appinsights.com"); var testEvent = new EventGridEvent { EventType = CustomEventTypes.RequestBlobCopy, DataVersion = "1.0", Data = JObject.Parse(data) }; EventGridEvent resultEvent = null; Mock.Get(_eventGridPublisher) .Setup(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>())) .Callback <EventGridEvent>((eventGridEvent) => resultEvent = eventGridEvent) .ReturnsAsync(true); Mock.Get(_logger) .Setup(x => x.LogExceptionObject( out appInsightsUri, LogEventIds.GridwichUnhandledException, It.IsAny <Exception>(), It.IsAny <object>())); // Act var handleAsyncResult = await _handler.HandleAsync(testEvent).ConfigureAwait(true); // Assert handleAsyncResult.ShouldBe(false); resultEvent.ShouldNotBeNull(); resultEvent.EventType.ShouldBe(CustomEventTypes.ResponseFailure); }
public async Task HandleAsync_ShouldReturnTrueAndNotLog_WhenNoErrors() { // Arrange var blobUri = "https://gridwichasset00sasb.com/container1/la_macarena.mp4"; var changeBlobTierData = new RequestBlobTierChangeDTO { AccessTier = BlobAccessTier.Archive, BlobUri = blobUri, RehydratePriority = BlobRehydratePriority.High }; var context = TestHelpers.CreateGUIDContext(); var appInsightsUri = new Uri("https://www.appinsights.com"); var testEvent = new EventGridEvent { EventType = CustomEventTypes.RequestBlobTierChange, DataVersion = "1.0", Data = JObject.FromObject(changeBlobTierData) }; EventGridEvent resultEvent = null; var x = LogEventIds.AboutToAttemptPublishOfEventWithId; // Arrange Mocks Mock.Get(_storageService) .Setup(x => x.ChangeBlobTierAsync(new Uri(blobUri), changeBlobTierData.AccessTier, changeBlobTierData.RehydratePriority, It.IsAny <StorageClientProviderContext>())) .ReturnsAsync(true); Mock.Get(_eventGridPublisher) .Setup(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>())) .Callback <EventGridEvent>((eventGridEvent) => resultEvent = eventGridEvent) .ReturnsAsync(true); Mock.Get(_logger) .Setup(x => x.LogExceptionObject( out appInsightsUri, It.IsAny <EventId>(), It.IsAny <Exception>(), It.IsAny <object>())); // Act var handleAsyncResult = await _handler.HandleAsync(testEvent).ConfigureAwait(true); // Assert Mock.Get(_logger).Verify(x => x.LogExceptionObject(It.IsAny <EventId>(), It.IsAny <Exception>(), It.IsAny <object>()), Times.Never); handleAsyncResult.ShouldBe(true); resultEvent.ShouldNotBeNull(); resultEvent.EventType.ShouldBe(CustomEventTypes.ResponseBlobTierChanged); var resultEventData = (ResponseBlobTierChangeSuccessDTO)resultEvent.Data; resultEventData.AccessTier.ShouldBe(changeBlobTierData.AccessTier); resultEventData.BlobUri.ShouldBe(changeBlobTierData.BlobUri); resultEventData.RehydratePriority.ShouldBe(changeBlobTierData.RehydratePriority); }
public async Task HandleAsync_ShouldReturnTrueAndNotLog_WhenNoErrors() { // Arrange var createContainerData = new RequestContainerCreateDTO { StorageAccountName = _storageAccountName, ContainerName = "unittestcontainer" + Guid.NewGuid().ToString() }; var blobContainerCreateId = Guid.NewGuid().ToString(); var testEvent = new EventGridEvent { Data = JObject.FromObject(createContainerData), EventType = CustomEventTypes.RequestBlobContainerCreate, DataVersion = "1.0", }; // Arrange Mocks Mock.Get(_storageService) .Setup(x => x.ContainerCreateAsync(createContainerData.StorageAccountName, createContainerData.ContainerName, It.IsAny <StorageClientProviderContext>())) .ReturnsAsync(true); EventGridEvent resultEvent = null; Mock.Get(_eventGridPublisher) .Setup(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>())) .Callback <EventGridEvent>((eventGridEvent) => resultEvent = eventGridEvent) .ReturnsAsync(true); // Act var handleAsyncResult = await _handler.HandleAsync(testEvent).ConfigureAwait(true); // Assert handleAsyncResult.ShouldBe(true); resultEvent.ShouldNotBeNull(); resultEvent.EventType.ShouldBe(CustomEventTypes.ResponseBlobContainerSuccess); var resultEventData = (ResponseContainerCreatedSuccessDTO)resultEvent.Data; resultEventData.StorageAccountName.ShouldBe(createContainerData.StorageAccountName); resultEventData.ContainerName.ShouldBe(createContainerData.ContainerName); }
public async Task HandleAsync_ShouldPublishAppropriately_GivenOperationContextsOfGuidsAndBlanks( bool shouldBePublished, string contextString, bool doAdjustViaContext, bool?toBeMuted) { string clientRequestIdForContext = contextString; if (doAdjustViaContext) { // run string through context to get final version for HTTP header var t = new StorageClientProviderContext(contextString, muted: toBeMuted); clientRequestIdForContext = t.ClientRequestID; } // Arrange const string BLOB_URL = _expectedInboxUrl; var topicEndpointUri = new Uri("https://www.topichost.com"); var testEvent = new EventGridEvent { EventTime = DateTime.UtcNow, EventType = EventTypes.StorageBlobCreatedEvent, DataVersion = "1.0", Data = JsonConvert.SerializeObject(new StorageBlobCreatedEventData { Url = BLOB_URL, ClientRequestId = clientRequestIdForContext, }) }; var expectedBlobMetadata = JsonHelpers.JsonToJObject("{\"someProp1\":\"someValue1\", \"someProp2\":\"someValue2\"}", true); EventGridEvent publishedEvent = null; // Arrange Mocks Mock.Get(_settingsProvider) .Setup(x => x.GetAppSettingsValue(Publishing.TopicOutboundEndpointSettingName)) .Returns(topicEndpointUri.ToString()); Mock.Get(_storageService) .Setup(x => x.GetBlobMetadataAsync( It.IsAny <Uri>(), It.IsAny <StorageClientProviderContext>())) .ReturnsAsync(expectedBlobMetadata); Mock.Get(_eventGridPublisher) .Setup(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>())) .Callback <EventGridEvent>((eventGridEvent) => publishedEvent = eventGridEvent) .ReturnsAsync(true); // Act var handleAsyncResult = await _handler.HandleAsync(testEvent).ConfigureAwait(true); // Assert handleAsyncResult.ShouldBe(true, "handleAsync should always return true"); // Assert publishedEvent: if (shouldBePublished) { publishedEvent.ShouldNotBeNull(); publishedEvent.Data.ShouldBeOfType(typeof(ResponseBlobCreatedSuccessDTO)); var data = (ResponseBlobCreatedSuccessDTO)publishedEvent.Data; data.OperationContext.ShouldNotBeNull(); JsonHelpers.SerializeOperationContext(data.OperationContext).ShouldBe(clientRequestIdForContext); Mock.Get(_eventGridPublisher) .Verify(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>()), Times.AtLeastOnce, "Events should have been published"); } else { publishedEvent.ShouldBeNull(); Mock.Get(_eventGridPublisher) .Verify(x => x.PublishEventToTopic(It.IsAny <EventGridEvent>()), Times.Never, "No events should have been published"); } }
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"); }