public void UsingAuthenticationKey_NullOrEmptyEndpointKey_ShouldFailWithArgumentException(string authenticationKey)
 {
     Assert.Throws <ArgumentException>(
         () => EventGridPublisherBuilder
         .ForTopic(SampleTopicEndpoint)
         .UsingAuthenticationKey(authenticationKey));
 }
        public async Task PublishMultipleEvents_WithBuilder_ValidParameters_SucceedsWithTimeout()
        {
            // Arrange
            var topicEndpoint = Configuration.GetValue <string>("Arcus:EventGrid:TopicEndpoint");
            var endpointKey   = Configuration.GetValue <string>("Arcus:EventGrid:EndpointKey");
            var events        =
                Enumerable
                .Repeat <Func <Guid> >(Guid.NewGuid, 2)
                .Select(newGuid => new NewCarRegistered(
                            newGuid().ToString(),
                            subject: "integration-test",
                            licensePlate: "1-TOM-337"))
                .ToArray();

            // Act
            await EventGridPublisherBuilder
            .ForTopic(topicEndpoint)
            .UsingAuthenticationKey(endpointKey)
            .Build()
            .PublishMany(events);

            // Assert
            Assert.All(
                events,
                e =>
            {
                TracePublishedEvent(e.Id, events);
                string receivedEvent = _serviceBusEventConsumerHost.GetReceivedEvent(e.Id, TimeSpan.FromMinutes(10));
                AssertReceivedEvent(e.Id, e.EventType, e.Subject, e.Data.LicensePlate, receivedEvent);
            });
        }
Пример #3
0
        public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration(configuration =>
        {
            configuration.AddCommandLine(args);
            configuration.AddEnvironmentVariables();
        })
        .ConfigureServices((hostContext, services) =>
        {
            services.AddLogging();
            services.AddTransient(svc =>
            {
                var configuration  = svc.GetRequiredService <IConfiguration>();
                var eventGridTopic = configuration.GetValue <string>("EVENTGRID_TOPIC_URI");
                var eventGridKey   = configuration.GetValue <string>("EVENTGRID_AUTH_KEY");

                return(EventGridPublisherBuilder
                       .ForTopic(eventGridTopic)
                       .UsingAuthenticationKey(eventGridKey)
                       .Build());
            });
            services.AddServiceBusTopicMessagePump("Receive-All", configuration => configuration["ARCUS_SERVICEBUS_CONNECTIONSTRING"])
            .WithServiceBusMessageHandler <OrdersAzureServiceBusMessageHandler, Order>();

            services.AddTcpHealthProbes("ARCUS_HEALTH_PORT");
        });
        public async Task PublishSingleRawEvent_WithBuilder_ValidParameters_Succeeds()
        {
            // Arrange
            var          topicEndpoint   = Configuration.GetValue <string>("Arcus:EventGrid:TopicEndpoint");
            var          endpointKey     = Configuration.GetValue <string>("Arcus:EventGrid:EndpointKey");
            const string licensePlate    = "1-TOM-337";
            const string expectedSubject = "/";
            var          eventId         = Guid.NewGuid().ToString();
            var          @event          = new NewCarRegistered(eventId, licensePlate);
            var          rawEventBody    = JsonConvert.SerializeObject(@event.Data);

            // Act
            await EventGridPublisherBuilder
            .ForTopic(topicEndpoint)
            .UsingAuthenticationKey(endpointKey)
            .Build()
            .PublishRaw(@event.Id, @event.EventType, rawEventBody);

            TracePublishedEvent(eventId, @event);

            // Assert
            var receivedEvent = _serviceBusEventConsumerHost.GetReceivedEvent(eventId);

            AssertReceivedEvent(eventId, @event.EventType, expectedSubject, licensePlate, receivedEvent);
        }
Пример #5
0
        public async Task PublishMultipleRawEvents_WithBuilder_ValidParameters_SucceedsWithTimeout()
        {
            // Arrange
            var          topicEndpoint = Configuration.GetValue <string>("Arcus:EventGrid:TopicEndpoint");
            var          endpointKey   = Configuration.GetValue <string>("Arcus:EventGrid:EndpointKey");
            const string licensePlate  = "1-TOM-1337";
            var          events        =
                Enumerable
                .Repeat <Func <Guid> >(Guid.NewGuid, 2)
                .Select(newGuid => new RawEvent(
                            newGuid().ToString(),
                            eventSubject: "integration-test",
                            eventData: $"{{\"licensePlate\": \"{licensePlate}\"}}",
                            eventType: "Arcus.Samples.Cars.NewCarRegistered",
                            eventVersion: "1.0",
                            eventTime: DateTimeOffset.Now))
                .ToArray();

            // Act
            await EventGridPublisherBuilder
            .ForTopic(topicEndpoint)
            .UsingAuthenticationKey(endpointKey)
            .Build()
            .PublishManyRawAsync(events);

            // Assert
            Assert.All(events, rawEvent => AssertReceivedNewCarRegisteredEventWithTimeout(rawEvent, licensePlate));
        }
Пример #6
0
        public async Task PublishManyRaw_AnyNullRawEventWasSpecified_ShouldFailWithArgumentException()
        {
            // Arrange
            const string   topicEndpoint     = "http://myTopic";
            const string   authenticationKey = "myKey";
            string         eventId           = Guid.NewGuid().ToString();
            string         eventType         = "Arcus.Samples.Cars.NewCarRegistered";
            string         eventBody         = "{\"licensePlate\": \"1-TOM-1337\"}";
            string         eventSubject      = "/cars/volvo";
            string         dataVersion       = "1.0";
            DateTimeOffset eventTime         = DateTimeOffset.UtcNow;

            var eventGridPublisher =
                EventGridPublisherBuilder
                .ForTopic(topicEndpoint)
                .UsingAuthenticationKey(authenticationKey)
                .Build();

            IEnumerable <RawEvent> eventList = new[]
            {
                new RawEvent(eventId, eventType, eventBody, eventSubject, dataVersion, eventTime),
                null
            };

            // Act / Assert
            await Assert.ThrowsAsync <ArgumentException>(() => eventGridPublisher.PublishManyAsync(eventList));
        }
Пример #7
0
        private IEventGridPublisher BuildEventGridPublisher(IServiceProvider serviceProvider)
        {
            var rawTopicEndpoint  = Configuration[EnvironmentVariables.Runtime.EventGrid.TopicEndpoint];
            var authenticationKey = Configuration[EnvironmentVariables.Runtime.EventGrid.AuthKey];
            var topicUri          = new Uri(rawTopicEndpoint);

            return(EventGridPublisherBuilder.ForTopic(topicUri)
                   .UsingAuthenticationKey(authenticationKey)
                   .Build());
        }
Пример #8
0
        private static void AddEventGridPublisher(IFunctionsHostBuilder builder, IConfiguration configuration)
        {
            var topicEndpoint = configuration["EventGrid_Topic_Uri"];
            var topicKey      = configuration["EventGrid_Topic_Key"];

            var eventGridPublisher = EventGridPublisherBuilder
                                     .ForTopic(topicEndpoint)
                                     .UsingAuthenticationKey(topicKey)
                                     .Build();

            builder.Services.AddTransient <IEventGridPublisher>(provider => eventGridPublisher);
        }
Пример #9
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="configuration">Configuration of the application</param>
        /// <param name="serviceProvider">Collection of services that are configured</param>
        /// <param name="logger">Logger to write telemetry to</param>
        public OrdersMessagePump(IConfiguration configuration, IServiceProvider serviceProvider, ILogger <OrdersMessagePump> logger)
            : base(configuration, serviceProvider, logger)
        {
            var eventGridTopic = configuration.GetValue <string>("EVENTGRID_TOPIC_URI");
            var eventGridKey   = configuration.GetValue <string>("EVENTGRID_AUTH_KEY");

            _eventGridPublisher =
                EventGridPublisherBuilder
                .ForTopic(eventGridTopic)
                .UsingAuthenticationKey(eventGridKey)
                .Build();
        }
 private static void AddEventGridPublisher(WorkerOptions options, TestConfig config)
 {
     options.Services.AddTransient(svc =>
     {
         string eventGridTopic = config.GetTestInfraEventGridTopicUri();
         string eventGridKey   = config.GetTestInfraEventGridAuthKey();
         return(EventGridPublisherBuilder
                .ForTopic(eventGridTopic)
                .UsingAuthenticationKey(eventGridKey)
                .Build());
     });
 }
Пример #11
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="configuration">Configuration of the application</param>
        /// <param name="logger">Logger to write telemetry to</param>
        public OrdersMessageHandler(IConfiguration configuration, ILogger <OrdersMessageHandler> logger)
        {
            _logger = logger;
            var eventGridTopic = configuration.GetValue <string>("EVENTGRID_TOPIC_URI");
            var eventGridKey   = configuration.GetValue <string>("EVENTGRID_AUTH_KEY");

            _eventGridPublisher =
                EventGridPublisherBuilder
                .ForTopic(eventGridTopic)
                .UsingAuthenticationKey(eventGridKey)
                .Build();
        }
        public void ForTopicUsingAuthentication_NonNullOrEmptyEndpointTopicAndAuthenticationKey_ShouldCreatePublisher()
        {
            // Act
            var publisher = EventGridPublisherBuilder
                            .ForTopic(SampleTopicEndpoint)
                            .UsingAuthenticationKey(SampleAuthenticationKey)
                            .Build();

            // Assert
            Assert.NotNull(publisher);
            Assert.IsType <EventGridPublisher>(publisher);
            Assert.Equal(SampleTopicEndpoint, publisher.TopicEndpoint);
        }
        public void ForTopic_HttpEndpointTopic_WithUriOverload_ShouldCreatePublisher(string topic)
        {
            var uri = new Uri(topic);
            IEventGridPublisher publisher =
                EventGridPublisherBuilder
                .ForTopic(uri)
                .UsingAuthenticationKey(SampleAuthenticationKey)
                .Build();

            Assert.NotNull(publisher);
            Assert.IsType <EventGridPublisher>(publisher);
            Assert.Equal(topic, publisher.TopicEndpoint);
        }
Пример #14
0
        /// <summary>
        /// Creates an <see cref="IEventGridPublisher"/> implementation that can publish events to an endpoint corresponding with the given <paramref name="eventSchema"/>.
        /// </summary>
        /// <param name="eventSchema">The schema that corresponds to the target endpoint to which the publisher will publish events.</param>
        /// <param name="configuration">The instance to retrieve the required values to configure the publisher.</param>
        public static IEventGridPublisher CreateEventPublisher(EventSchema eventSchema, TestConfig configuration)
        {
            Guard.NotNull(configuration, nameof(configuration));

            string topicEndpoint = configuration.GetEventGridTopicEndpoint(eventSchema);
            string endpointKey   = configuration.GetEventGridEndpointKey(eventSchema);

            IEventGridPublisher publisher =
                EventGridPublisherBuilder
                .ForTopic(topicEndpoint)
                .UsingAuthenticationKey(endpointKey)
                .Build();

            return(publisher);
        }
Пример #15
0
        public async Task PublishManyRaw_NoRawEventsWasSpecified_ShouldFailWithArgumentNullException()
        {
            // Arrange
            const string topicEndpoint     = "http://myTopic";
            const string authenticationKey = "myKey";

            var eventGridPublisher =
                EventGridPublisherBuilder
                .ForTopic(topicEndpoint)
                .UsingAuthenticationKey(authenticationKey)
                .Build();

            // Act / Assert
            await Assert.ThrowsAsync <ArgumentNullException>(() => eventGridPublisher.PublishManyAsync(null));
        }
Пример #16
0
        public async Task Publish_EmptyCollectionOfEventsSpecified_ShouldFailWithArgumentException()
        {
            // Arrange
            const string            topicEndpoint     = "http://myTopic";
            const string            authenticationKey = "myKey";
            List <NewCarRegistered> events            = new List <NewCarRegistered>();

            // Act
            var eventGridPublisher =
                EventGridPublisherBuilder
                .ForTopic(topicEndpoint)
                .UsingAuthenticationKey(authenticationKey)
                .Build();

            // Assert
            await Assert.ThrowsAsync <ArgumentException>(() => eventGridPublisher.PublishManyAsync(events));
        }
Пример #17
0
        /// <summary>
        /// Adds an <see cref="IEventGridPublisher"/> instance to the <paramref name="options"/>.
        /// </summary>
        /// <param name="options">The options to add the publisher to.</param>
        /// <param name="config">The test configuration which will be used to retrieve the Azure Event Grid authentication information.</param>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="options"/> or the <paramref name="config"/> is <c>null</c>.</exception>
        public static WorkerOptions AddEventGridPublisher(this WorkerOptions options, TestConfig config)
        {
            Guard.NotNull(options, nameof(options), "Requires a set of worker options to add the Azure Event Grid publisher to");
            Guard.NotNull(config, nameof(config), "Requires a test configuration instance to retrieve the Azure Event Grid authentication inforation");

            options.Services.AddTransient(svc =>
            {
                string eventGridTopic = config.GetTestInfraEventGridTopicUri();
                string eventGridKey   = config.GetTestInfraEventGridAuthKey();
                return(EventGridPublisherBuilder
                       .ForTopic(eventGridTopic)
                       .UsingAuthenticationKey(eventGridKey)
                       .Build());
            });

            return(options);
        }
Пример #18
0
        public async Task Publish_NoEventSpecified_ShouldFailWithArgumentNullException()
        {
            // Arrange
            const string     topicEndpoint     = "http://myTopic";
            const string     authenticationKey = "myKey";
            NewCarRegistered @event            = null;

            // Act
            var eventGridPublisher =
                EventGridPublisherBuilder
                .ForTopic(topicEndpoint)
                .UsingAuthenticationKey(authenticationKey)
                .Build();

            // Assert
            await Assert.ThrowsAsync <ArgumentNullException>(() => eventGridPublisher.PublishAsync(@event));
        }
Пример #19
0
        public async Task PublishRawWithoutDetailedEventInfo_EmptyEventTypeWasSpecified_ShouldFailWithArgumentException()
        {
            // Arrange
            const string topicEndpoint     = "http://myTopic";
            const string authenticationKey = "myKey";
            string       eventId           = Guid.NewGuid().ToString();
            string       eventType         = string.Empty;
            string       eventBody         = "{\"licensePlate\": \"1-TOM-1337\"}";

            // Act
            var eventGridPublisher =
                EventGridPublisherBuilder
                .ForTopic(topicEndpoint)
                .UsingAuthenticationKey(authenticationKey)
                .Build();

            // Assert
            await Assert.ThrowsAsync <ArgumentException>(() => eventGridPublisher.PublishRawEventGridEventAsync(eventId, eventType, eventBody));
        }
Пример #20
0
        public async Task PublishRawWithoutDetailedEventInfo_EventBodyIsNoJson_ShouldFailWithArgumentException()
        {
            // Arrange
            const string topicEndpoint     = "http://myTopic";
            const string authenticationKey = "myKey";
            string       eventId           = Guid.NewGuid().ToString();
            string       eventType         = "Arcus.Samples.Cars.NewCarRegistered";
            string       eventBody         = "Invalid-Body";

            // Act
            var eventGridPublisher =
                EventGridPublisherBuilder
                .ForTopic(topicEndpoint)
                .UsingAuthenticationKey(authenticationKey)
                .Build();

            // Assert
            await Assert.ThrowsAsync <ArgumentException>(() => eventGridPublisher.PublishRawEventGridEventAsync(eventId, eventType, eventBody));
        }
        public async Task Run([ServiceBusTrigger("new-deprecation-notices", "event-grid-notifications", Connection = "ServiceBus_ConnectionString")] NewDeprecationNoticePublishedV1Message newDeprecationNoticePublishedV1Message, ILogger log)
        {
            var eventGridTopicEndpoint = _configuration["EVENTGRID_ENDPOINT"];
            var eventGridAuthKey       = _configuration["EVENTGRID_AUTH_KEY"];

            var eventGridPublisher = EventGridPublisherBuilder
                                     .ForTopic(eventGridTopicEndpoint)
                                     .UsingAuthenticationKey(eventGridAuthKey)
                                     .Build();

            var @event = new CloudEvent(CloudEventsSpecVersion.V1_0,
                                        "NewDeprecationNoticePublishedV1",
                                        new Uri("https://github.com/azure-deprecation/dashboard"),
                                        subject: $"/{newDeprecationNoticePublishedV1Message.DeprecationInfo.Impact.Services.First()}")
            {
                Data            = Serializer.Serialize(newDeprecationNoticePublishedV1Message),
                DataContentType = new ContentType("application/json")
            };

            await eventGridPublisher.PublishAsync(@event);
        }
Пример #22
0
        public async Task PublishRaw_EventBodyIsNoJson_ShouldFailWithArgumentException()
        {
            // Arrange
            const string   topicEndpoint     = "http://myTopic";
            const string   authenticationKey = "myKey";
            string         eventId           = Guid.NewGuid().ToString();
            string         eventType         = "Arcus.Samples.Cars.NewCarRegistered";
            string         eventBody         = "Invalid-Body";
            string         eventSubject      = "/cars/volvo";
            string         dataVersion       = "1.0";
            DateTimeOffset eventTime         = DateTimeOffset.UtcNow;

            // Act
            var eventGridPublisher =
                EventGridPublisherBuilder
                .ForTopic(topicEndpoint)
                .UsingAuthenticationKey(authenticationKey)
                .Build();

            // Assert
            await Assert.ThrowsAsync <ArgumentException>(() => eventGridPublisher.PublishRawEventGridEventAsync(eventId, eventType, eventBody, eventSubject, dataVersion, eventTime));
        }
Пример #23
0
        public async Task PublishRaw_EmptyEventIdWasSpecified_ShouldFailWithArgumentException()
        {
            // Arrange
            const string   topicEndpoint     = "http://myTopic";
            const string   authenticationKey = "myKey";
            string         eventId           = string.Empty;
            string         eventType         = "Arcus.Samples.Cars.NewCarRegistered";
            string         eventBody         = "{\"licensePlate\": \"1-TOM-1337\"}";
            string         eventSubject      = "/cars/volvo";
            string         dataVersion       = "1.0";
            DateTimeOffset eventTime         = DateTimeOffset.UtcNow;

            // Act
            var eventGridPublisher =
                EventGridPublisherBuilder
                .ForTopic(topicEndpoint)
                .UsingAuthenticationKey(authenticationKey)
                .Build();

            // Assert
            await Assert.ThrowsAsync <ArgumentException>(() => eventGridPublisher.PublishRawEventGridEventAsync(eventId, eventType, eventBody, eventSubject, dataVersion, eventTime));
        }
        public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration(configuration =>
        {
            configuration.AddCommandLine(args);
            configuration.AddEnvironmentVariables();
        })
        .ConfigureServices((hostContext, services) =>
        {
            services.AddTransient(svc =>
            {
                var configuration  = svc.GetRequiredService <IConfiguration>();
                var eventGridTopic = configuration.GetValue <string>("EVENTGRID_TOPIC_URI");
                var eventGridKey   = configuration.GetValue <string>("EVENTGRID_AUTH_KEY");

                return(EventGridPublisherBuilder
                       .ForTopic(eventGridTopic)
                       .UsingAuthenticationKey(eventGridKey)
                       .Build());
            });
            services.AddServiceBusQueueMessagePump <OrdersMessagePump>(configuration => configuration["ARCUS_SERVICEBUS_CONNECTIONSTRING"]);
            services.AddTcpHealthProbes("ARCUS_HEALTH_PORT", builder => builder.AddCheck("sample", () => HealthCheckResult.Healthy()));
        });
Пример #25
0
        public async Task PublishMultipleEvents_WithBuilder_ValidParameters_SucceedsWithTimeout()
        {
            // Arrange
            var topicEndpoint = Configuration.GetValue <string>("Arcus:EventGrid:TopicEndpoint");
            var endpointKey   = Configuration.GetValue <string>("Arcus:EventGrid:EndpointKey");
            var events        =
                Enumerable
                .Repeat <Func <Guid> >(Guid.NewGuid, 2)
                .Select(newGuid => new NewCarRegistered(
                            newGuid().ToString(),
                            subject: "integration-test",
                            licensePlate: "1-TOM-337"))
                .ToArray();

            // Act
            await EventGridPublisherBuilder
            .ForTopic(topicEndpoint)
            .UsingAuthenticationKey(endpointKey)
            .Build()
            .PublishManyAsync(events);

            // Assert
            Assert.All(events, @event => AssertReceivedNewCarRegisteredEventWithTimeout(@event, @event.GetPayload()?.LicensePlate));
        }
        public void ForTopic_NonHttpEndpointTopic_WitUriOverload_ShouldFailWithUriFormatException(string topic)
        {
            var uri = new Uri(topic);

            Assert.Throws <UriFormatException>(() => EventGridPublisherBuilder.ForTopic(uri));
        }
 public void ForTopic_NonHttpEndpointTopic_ShouldFailWithArgumentException(string topic)
 {
     Assert.Throws <ArgumentException>(() => EventGridPublisherBuilder.ForTopic(topic));
 }
 private static IEventGridPublisherBuilderWithExponentialRetry CreateEventGridBuilder()
 {
     return(EventGridPublisherBuilder
            .ForTopic(SampleTopicEndpoint)
            .UsingAuthenticationKey(SampleAuthenticationKey));
 }