コード例 #1
0
        public async void HandleAsync_ShouldReturnNullOperationContext_WhenItIsAGuid(bool muteContext)
        {
            // Arrange
            const string BLOB_URL         = _expectedInboxUrl;
            var          topicEndpointUri = new Uri("https://www.topichost.com");
            var          reqId            = new StorageClientProviderContext("9d87e668-0000-0000-0000-b84ff6a53784");

            reqId.IsMuted = muteContext;

            var testEvent = new EventGridEvent
            {
                EventTime   = DateTime.UtcNow,
                EventType   = EventTypes.StorageBlobDeletedEvent,
                DataVersion = "1.0",
                Data        = JsonConvert.SerializeObject(new StorageBlobDeletedEventData
                {
                    Url             = BLOB_URL,
                    ClientRequestId = reqId.ClientRequestID,
                })
            };
            EventGridEvent publishedEvent = null;

            // Arrange Mocks
            Mock.Get(_settingsProvider)
            .Setup(x => x.GetAppSettingsValue(Publishing.TopicOutboundEndpointSettingName))
            .Returns(topicEndpointUri.ToString());
            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 (muteContext)
            {
                Mock.Get(_eventGridPublisher).Verify(x =>
                                                     x.PublishEventToTopic(It.IsAny <EventGridEvent>()),
                                                     Times.Never,
                                                     "Muted context results should not be published.");
                publishedEvent.ShouldBeNull();
            }
            else
            {
                publishedEvent.Data.ShouldBeOfType(typeof(ResponseBlobDeleteSuccessDTO));
                var data = (ResponseBlobDeleteSuccessDTO)publishedEvent.Data;
                data.OperationContext.ShouldNotBeNull();
                data.OperationContext.ShouldBeEquivalentTo(reqId.ClientRequestIdAsJObject);
            }
        }
コード例 #2
0
        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");
            }
        }