public async Task EventGridServiceSendEventReturnsSuccess()
        {
            // arrange
            var dummyEventGridEventData = A.Dummy <EventGridEventData>();

            // act
            await eventGridService.SendEventAsync(dummyEventGridEventData, "a subject", "an event type").ConfigureAwait(false);

            // assert
            A.CallTo(() => fakeEventGridClientService.SendEventAsync(A <List <EventGridEvent> > .Ignored, A <string> .Ignored, A <string> .Ignored, A <string> .Ignored)).MustHaveHappenedOnceExactly();
        }
Example #2
0
        public async Task SendEventAsync(EventGridEventData?eventGridEventData, string?subject, string?eventType)
        {
            _ = eventGridEventData ?? throw new ArgumentNullException(nameof(eventGridEventData));

            if (!IsValidEventGridClientOptions(eventGridClientOptions))
            {
                logger.LogWarning($"Unable to send to event grid due to invalid {nameof(eventGridClientOptions)} options");
                return;
            }

            logger.LogInformation($"Sending Event Grid message for: {eventGridEventData.DisplayText}");

            var eventGridEvents = new List <EventGridEvent>
            {
                new EventGridEvent
                {
                    Id          = Guid.NewGuid().ToString(),
                    Subject     = subject,
                    Data        = eventGridEventData,
                    EventType   = eventType,
                    EventTime   = DateTime.UtcNow,
                    DataVersion = "1.0",
                },
            };

            eventGridEvents.ForEach(f => f.Validate());

            await eventGridClientService.SendEventAsync(eventGridEvents, eventGridClientOptions.TopicEndpoint, eventGridClientOptions.TopicKey, subject).ConfigureAwait(false);
        }
Example #3
0
        public async Task EventGridServiceCompareAndSendEventAsyncReturnsSuccessForNoDifferences()
        {
            // arrange
            var existingContentPageModel = BuildValidContentPageModel();
            var updatedContentPageModel  = BuildValidContentPageModel();
            var eventGridService         = new EventGridService(fakeLogger, fakeEventGridClientService, eventGridPublishClientOptions);

            // act
            await eventGridService.CompareAndSendEventAsync(existingContentPageModel, updatedContentPageModel).ConfigureAwait(false);

            // assert
            A.CallTo(() => fakeEventGridClientService.SendEventAsync(A <List <EventGridEvent> > .Ignored, A <string> .Ignored, A <string> .Ignored, A <string> .Ignored)).MustNotHaveHappened();
        }
Example #4
0
        public async Task SendEventAsync(WebhookCacheOperation webhookCacheOperation, ContentPageModel?updatedContentPageModel)
        {
            _ = updatedContentPageModel ?? throw new ArgumentNullException(nameof(updatedContentPageModel));

            if (!IsValidEventGridPublishClientOptions(logger, eventGridPublishClientOptions))
            {
                logger.LogWarning("Unable to send to event grid due to invalid EventGridPublishClientOptions options");
                return;
            }

            var logMessage = $"{webhookCacheOperation} - {updatedContentPageModel.Id} - {updatedContentPageModel.CanonicalName}";

            logger.LogInformation($"Sending Event Grid message for: {logMessage}");

            var eventGridEvents = new List <EventGridEvent>
            {
                new EventGridEvent
                {
                    Id      = Guid.NewGuid().ToString(),
                    Subject = $"{eventGridPublishClientOptions.SubjectPrefix}{updatedContentPageModel.Id}",
                    Data    = new EventGridEventData
                    {
                        ItemId      = updatedContentPageModel.Id.ToString(),
                        Api         = $"{eventGridPublishClientOptions.ApiEndpoint}/{updatedContentPageModel.Id}",
                        DisplayText = updatedContentPageModel.CanonicalName,
                        VersionId   = updatedContentPageModel.Version.ToString(),
                        Author      = eventGridPublishClientOptions.SubjectPrefix,
                    },
                    EventType   = webhookCacheOperation == WebhookCacheOperation.Delete ? "deleted" : "published",
                    EventTime   = DateTime.Now,
                    DataVersion = "1.0",
                },
            };

            eventGridEvents.ForEach(f => f.Validate());

            await eventGridClientService.SendEventAsync(eventGridEvents, eventGridPublishClientOptions.TopicEndpoint, eventGridPublishClientOptions.TopicKey, logMessage).ConfigureAwait(false);
        }