public async Task SerializesExpectedProperties_BaseType()
        {
            var mockTransport = CreateMockTransport();
            var options       = new EventGridPublisherClientOptions
            {
                Transport = mockTransport
            };
            EventGridPublisherClient client =
                new EventGridPublisherClient(
                    new Uri("http://localHost"),
                    new AzureKeyCredential("fakeKey"),
                    options);
            var cloudEvent = new CloudEvent(
                "record",
                "Microsoft.MockPublisher.TestEvent",
                new DerivedTestPayload
            {
                Name            = "name",
                Age             = 10,
                DerivedProperty = 5
            },
                typeof(TestPayload));

            Assert.IsNull(cloudEvent.Data.ToObjectFromJson <DerivedTestPayload>().DerivedProperty);

            List <CloudEvent> eventsList = new List <CloudEvent>()
            {
                cloudEvent
            };

            await client.SendEventsAsync(eventsList);

            Assert.IsNull(cloudEvent.Data.ToObjectFromJson <DerivedTestPayload>().DerivedProperty);
        }
        public async Task CanPublishCloudEventToDomain()
        {
            EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.CloudEventDomainHost),
                    new AzureKeyCredential(TestEnvironment.CloudEventDomainKey),
                    options));

            #region Snippet:SendCloudEventsToDomain
            List <CloudEvent> eventsList = new List <CloudEvent>();

            for (int i = 0; i < 10; i++)
            {
                CloudEvent cloudEvent = new CloudEvent(
                    // the source is mapped to the domain topic
                    $"Subject-{i}",
                    "Microsoft.MockPublisher.TestEvent",
                    "hello")
                {
#if SNIPPET
                    Id   = $"event-{i}",
                    Time = DateTimeOffset.Now
#else
                    Id   = Recording.Random.NewGuid().ToString(),
                    Time = Recording.Now
#endif
                };
                eventsList.Add(cloudEvent);
            }

            await client.SendEventsAsync(eventsList);

            #endregion
        }
Exemplo n.º 3
0
        public async Task CanPublishCloudEventToDomain()
        {
            EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.CloudEventDomainHost),
                    new AzureKeyCredential(TestEnvironment.CloudEventDomainKey),
                    options));

            #region Snippet:CloudNativePublishToDomain
            CloudEvent cloudEvent =
                new CloudEvent
            {
                Type = "record",
                // Event Grid does not allow absolute URIs as the domain topic
                Source = new Uri("test", UriKind.Relative),
#if SNIPPET
                Id   = "eventId",
                Time = DateTimeOffset.Now,
#else
                Id   = Recording.Random.NewGuid().ToString(),
                Time = Recording.Now,
#endif
                Data = new TestPayload("name", 0)
            };

            await client.SendCloudNativeCloudEventAsync(cloudEvent);

            #endregion
        }
Exemplo n.º 4
0
        public async Task CanPublishEventWithNonJsonSerializedBinaryData()
        {
            EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.TopicHost),
                    new AzureKeyCredential(TestEnvironment.TopicKey),
                    options));

            List <EventGridEvent> eventsList = new List <EventGridEvent>();

            for (int i = 0; i < 10; i++)
            {
                eventsList.Add(
                    new EventGridEvent(
                        new BinaryData("data"),
                        $"Subject-{i}",
                        "Microsoft.MockPublisher.TestEvent",
                        "1.0")
                {
                    Id        = Recording.Random.NewGuid().ToString(),
                    EventTime = Recording.Now
                });
            }

            await client.SendEventsAsync(eventsList);
        }
Exemplo n.º 5
0
        public async Task CanPublishEventToDomain()
        {
            EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.DomainHost),
                    new AzureKeyCredential(TestEnvironment.DomainKey),
                    options));

            List <EventGridEvent> eventsList = new List <EventGridEvent>();

            for (int i = 0; i < 10; i++)
            {
                EventGridEvent newEGEvent = new EventGridEvent(
                    $"Subject-{i}",
                    "hello",
                    "Microsoft.MockPublisher.TestEvent",
                    "1.0")
                {
                    Id        = Recording.Random.NewGuid().ToString(),
                    EventTime = Recording.Now
                };
                newEGEvent.Topic = $"Topic-{i}";

                eventsList.Add(newEGEvent);
            }

            await client.PublishEventsAsync(eventsList);
        }
Exemplo n.º 6
0
        public async Task CanPublishCloudEvent()
        {
            EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.CloudEventTopicHost),
                    new AzureKeyCredential(TestEnvironment.CloudEventTopicKey),
                    options));

            List <CloudEvent> eventsList = new List <CloudEvent>();

            for (int i = 0; i < 10; i++)
            {
                eventsList.Add(
                    new CloudEvent(
                        "record",
                        "Microsoft.MockPublisher.TestEvent")
                {
                    Id      = Recording.Random.NewGuid().ToString(),
                    Subject = $"Subject-{i}",
                    Time    = Recording.Now
                });
            }

            await client.PublishCloudEventsAsync(eventsList);
        }
        public async Task CanPublishCloudEvent()
        {
            EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.CloudEventTopicHost),
                    new AzureKeyCredential(TestEnvironment.CloudEventTopicKey),
                    options));

            List <CloudEvent> eventsList = new List <CloudEvent>();

            for (int i = 0; i < 10; i++)
            {
                eventsList.Add(
                    new CloudEvent(
                        "record",
                        new Uri("http://localHost"),
                        Recording.Random.NewGuid().ToString(),
                        Recording.Now.DateTime)
                {
                    Data = new TestPayload("name", i)
                }
                    );
            }

            await client.SendCloudEventsAsync(eventsList);
        }
        public async Task CanPublishCloudEventWithCustomObjectPayloadAndCustomSerializer()
        {
            EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.CloudEventTopicHost),
                    new AzureKeyCredential(TestEnvironment.CloudEventTopicKey),
                    options));
            var serializer = new JsonObjectSerializer(
                new JsonSerializerOptions()
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            });
            List <CloudEvent> eventsList = new List <CloudEvent>();

            for (int i = 0; i < 10; i++)
            {
                CloudEvent cloudEvent = new CloudEvent(
                    "record",
                    "Microsoft.MockPublisher.TestEvent",
                    serializer.Serialize(new TestPayload("name", i)),
                    "application/json",
                    dataFormat: CloudEventDataFormat.Json)
                {
                    Id      = Recording.Random.NewGuid().ToString(),
                    Subject = $"Subject-{i}",
                    Time    = Recording.Now
                };
                eventsList.Add(cloudEvent);
            }

            await client.SendEventsAsync(eventsList);
        }
        public async Task SerializesExpectedProperties_DerivedType()
        {
            var mockTransport = new MockTransport(new MockResponse(200));
            var options       = new EventGridPublisherClientOptions
            {
                Transport = mockTransport
            };
            EventGridPublisherClient client =
                new EventGridPublisherClient(
                    new Uri("http://localHost"),
                    new AzureKeyCredential("fakeKey"),
                    options);
            var egEvent = new EventGridEvent(
                "record",
                "Microsoft.MockPublisher.TestEvent",
                "TestPayload",
                new DerivedTestPayload
            {
                Name            = "name",
                Age             = 10,
                DerivedProperty = 5
            });

            Assert.AreEqual(5, egEvent.Data.ToObjectFromJson <DerivedTestPayload>().DerivedProperty);

            List <EventGridEvent> eventsList = new List <EventGridEvent>()
            {
                egEvent
            };

            await client.SendEventsAsync(eventsList);

            egEvent = DeserializeRequest(mockTransport.SingleRequest).First();
            Assert.AreEqual(5, egEvent.Data.ToObjectFromJson <DerivedTestPayload>().DerivedProperty);
        }
        public async Task RespectsPortFromUriSendingEventGridEvents()
        {
            var mockTransport = new MockTransport((request) =>
            {
                Assert.AreEqual(100, request.Uri.Port);
                return(new MockResponse(200));
            });
            var options = new EventGridPublisherClientOptions
            {
                Transport = mockTransport
            };
            EventGridPublisherClient client =
                new EventGridPublisherClient(
                    new Uri("https://contoso.com:100/api/events"),
                    new AzureKeyCredential("fakeKey"),
                    options);
            var egEvent = new EventGridEvent(
                "record",
                "Microsoft.MockPublisher.TestEvent",
                "TestPayload",
                new TestPayload
            {
                Name = "name",
                Age  = 10,
            });

            List <EventGridEvent> eventsList = new List <EventGridEvent>()
            {
                egEvent
            };

            await client.SendEventsAsync(eventsList);
        }
Exemplo n.º 11
0
        public async Task CanPublishEventWithCustomObjectPayload()
        {
            EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.TopicHost),
                    new AzureKeyCredential(TestEnvironment.TopicKey),
                    options));

            List <EventGridEvent> eventsList = new List <EventGridEvent>();

            for (int i = 0; i < 10; i++)
            {
                eventsList.Add(
                    new EventGridEvent(
                        $"Subject-{i}",
                        new TestPayload("name", i),
                        "Microsoft.MockPublisher.TestEvent",
                        "1.0")
                {
                    Id        = Recording.Random.NewGuid().ToString(),
                    EventTime = Recording.Now
                });
            }

            await client.PublishEventsAsync(eventsList);
        }
Exemplo n.º 12
0
        public async Task CanPublishCloudEventWithExtensionAttributes()
        {
            EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.CloudEventTopicHost),
                    new AzureKeyCredential(TestEnvironment.CloudEventTopicKey),
                    options));

            List <CloudEvent> eventsList = new List <CloudEvent>();

            for (int i = 0; i < 10; i++)
            {
                CloudEvent cloudEvent = new CloudEvent(
                    "record",
                    "Microsoft.MockPublisher.TestEvent")
                {
                    Id      = Recording.Random.NewGuid().ToString(),
                    Subject = $"Subject-{i}",
                    Time    = Recording.Now
                };
                cloudEvent.Data = "hello";
                cloudEvent.ExtensionAttributes.Add("testattribute1", "test");
                cloudEvent.ExtensionAttributes.Add("testattribute2", new TestPayload("name", i));
                eventsList.Add(cloudEvent);
            }

            await client.PublishCloudEventsAsync(eventsList);
        }
Exemplo n.º 13
0
        public async Task DoesNotSetCredentialForSystemPublisher()
        {
            var mockTransport = new MockTransport((request) =>
            {
                Assert.IsFalse(request.Headers.TryGetValue(Constants.SasKeyName, out var _));
                return(new MockResponse(200));
            });
            var options = new EventGridPublisherClientOptions
            {
                Transport = mockTransport
            };
            EventGridPublisherClient client =
                new EventGridPublisherClient(
                    new Uri("https://contoso.com:100/api/events"),
                    new AzureKeyCredential(EventGridKeyCredentialPolicy.SystemPublisherKey),
                    options);
            var cloudEvent = new CloudEvent(
                "record",
                "Microsoft.MockPublisher.TestEvent",
                null);

            List <CloudEvent> eventsList = new List <CloudEvent>()
            {
                cloudEvent
            };

            await client.SendEventsAsync(eventsList);
        }
        public async Task CanPublishCloudEventWithRawJsonData()
        {
            EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.CloudEventTopicHost),
                    new AzureKeyCredential(TestEnvironment.CloudEventTopicKey),
                    options));

            List <CloudEvent> eventsList = new List <CloudEvent>();

            for (int i = 0; i < 10; i++)
            {
                CloudEvent cloudEvent = new CloudEvent(
                    "record",
                    "Microsoft.MockPublisher.TestEvent",
                    JsonDocument.Parse("{\"property1\": \"abc\",  \"property2\": 123}").RootElement)
                {
                    Id      = Recording.Random.NewGuid().ToString(),
                    Subject = $"Subject-{i}",
                    Time    = Recording.Now
                };
                eventsList.Add(cloudEvent);
            }

            await client.SendEventsAsync(eventsList);
        }
        public async Task CustomizeSerializedJSONPropertiesToCamelCase()
        {
            EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
            var serializer = new JsonObjectSerializer(
                new JsonSerializerOptions()
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            });

            EventGridPublisherClient client = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.CustomEventTopicHost),
                    new AzureKeyCredential(TestEnvironment.CustomEventTopicKey),
                    options));
            List <BinaryData> eventsList = new List <BinaryData>();

            for (int i = 0; i < 10; i++)
            {
                eventsList.Add(serializer.Serialize(
                                   new TestEvent()
                {
                    DataVersion = "1.0",
                    EventTime   = Recording.Now,
                    EventType   = "Microsoft.MockPublisher.TestEvent",
                    Id          = Recording.Random.NewGuid().ToString(),
                    Subject     = $"Subject-{i}",
                    Topic       = $"Topic-{i}"
                }));
            }
            await client.SendEventsAsync(eventsList);
        }
Exemplo n.º 16
0
        public async Task CanPublishCloudEventWithBinaryData()
        {
            EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.CloudEventTopicHost),
                    new AzureKeyCredential(TestEnvironment.CloudEventTopicKey),
                    options));

            List <CloudEvent> eventsList = new List <CloudEvent>();

            for (int i = 0; i < 5; i++)
            {
                CloudEvent cloudEvent = new CloudEvent(
                    "record",
                    "Microsoft.MockPublisher.TestEvent")
                {
                    Id      = Recording.Random.NewGuid().ToString(),
                    Subject = $"Subject-{i}",
                    Time    = Recording.Now
                };
                // testing byte[]
                cloudEvent.Data = Encoding.UTF8.GetBytes("data");
                eventsList.Add(cloudEvent);
            }
            for (int i = 0; i < 5; i++)
            {
                CloudEvent cloudEvent = new CloudEvent(
                    "record",
                    "Microsoft.MockPublisher.TestEvent")
                {
                    Id      = Recording.Random.NewGuid().ToString(),
                    Subject = $"Subject-{i}",
                    Time    = Recording.Now
                };
                // testing ReadOnlyMemory<byte>
                cloudEvent.Data = new ReadOnlyMemory <byte>(Encoding.UTF8.GetBytes("data"));
                eventsList.Add(cloudEvent);
            }
            for (int i = 0; i < 5; i++)
            {
                CloudEvent cloudEvent = new CloudEvent(
                    "record",
                    "Microsoft.MockPublisher.TestEvent")
                {
                    Id      = Recording.Random.NewGuid().ToString(),
                    Subject = $"Subject-{i}",
                    Time    = Recording.Now
                };
                // testing IEnumerable<byte>
                cloudEvent.Data = Enumerable.Repeat((byte)1, 1);
                eventsList.Add(cloudEvent);
            }

            await client.PublishCloudEventsAsync(eventsList);
        }
Exemplo n.º 17
0
 public async Task CanPublishCustomEvent()
 {
     EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions());
     EventGridPublisherClient        client  = InstrumentClient(
         new EventGridPublisherClient(
             new Uri(TestEnvironment.CustomEventTopicHost),
             new AzureKeyCredential(TestEnvironment.CustomEventTopicKey),
             options));
     await client.PublishCustomEventsAsync(GetCustomEventsList());
 }
 public async Task CanPublishEvent()
 {
     EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
     EventGridPublisherClient        client  = InstrumentClient(
         new EventGridPublisherClient(
             new Uri(TestEnvironment.TopicHost),
             new AzureKeyCredential(TestEnvironment.TopicKey),
             options));
     await client.SendEventsAsync(GetEventsList());
 }
        public void CannotPublishCustomEventMissingApiEventsPathFromUri()
        {
            EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
            Uri host = new UriBuilder("https", new Uri(TestEnvironment.TopicHost).Host).Uri;
            EventGridPublisherClient client = InstrumentClient(
                new EventGridPublisherClient(
                    host,
                    new AzureKeyCredential(TestEnvironment.TopicKey),
                    options));

            Assert.ThrowsAsync <RequestFailedException>(async() => await client.SendEventAsync(new BinaryData(jsonSerializable: "data")));
        }
        public async Task SendCloudEventsToTopic()
        {
            string topicEndpoint  = TestEnvironment.CloudEventTopicHost;
            string topicAccessKey = TestEnvironment.CloudEventTopicKey;

            // Example of a custom ObjectSerializer used to serialize the event payload to JSON
            var myCustomDataSerializer = new JsonObjectSerializer(
                new JsonSerializerOptions()
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            });

            // Create the publisher client using an AzureKeyCredential
            // Custom topic should be configured to accept events of the CloudEvents 1.0 schema
            #region Snippet:CreateClientWithOptions
            EventGridPublisherClientOptions clientOptions = new EventGridPublisherClientOptions()
            {
                DataSerializer = myCustomDataSerializer
            };

            EventGridPublisherClient client = new EventGridPublisherClient(
                new Uri(topicEndpoint),
                new AzureKeyCredential(topicAccessKey),
                clientOptions);
            #endregion

            #region Snippet:SendCloudEventsToTopic
            // Add CloudEvents to a list to publish to the topic
            List <CloudEvent> eventsList = new List <CloudEvent>
            {
                // CloudEvent with populated data
                new CloudEvent(
                    "/cloudevents/example/source",
                    "Example.EventType",
                    "This is the event data"),

                // CloudEvents also supports sending binary-valued data
                new CloudEvent(
                    "/cloudevents/example/binarydata",
                    "Example.EventType",
                    new BinaryData("This is binary data"),
                    "example/binary")
            };

            // Send the events
            await client.SendEventsAsync(eventsList);

            #endregion
        }
Exemplo n.º 21
0
        public async Task CustomizeSerializedJSONPropertiesToCamelCase()
        {
            EventGridPublisherClientOptions options = Recording.InstrumentClientOptions(new EventGridPublisherClientOptions());

            options.Serializer = new JsonObjectSerializer(
                new JsonSerializerOptions()
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            });

            EventGridPublisherClient client = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.CustomEventTopicHost),
                    new AzureKeyCredential(TestEnvironment.CustomEventTopicKey),
                    options));
            await client.PublishCustomEventsAsync(GetCustomEventsList());
        }
 public async Task CanPublishSingleEventAAD()
 {
     EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
     EventGridPublisherClient        client  = InstrumentClient(
         new EventGridPublisherClient(
             new Uri(TestEnvironment.TopicHost),
             TestEnvironment.Credential,
             options));
     await client.SendEventAsync(
         new EventGridEvent(
             "Subject",
             "Microsoft.MockPublisher.TestEvent",
             "1.0",
             "hello")
     {
         Id        = Recording.Random.NewGuid().ToString(),
         EventTime = Recording.Now
     });
 }
 public async Task CanPublishSingleCustomEvent()
 {
     EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
     EventGridPublisherClient        client  = InstrumentClient(
         new EventGridPublisherClient(
             new Uri(TestEnvironment.CustomEventTopicHost),
             new AzureKeyCredential(TestEnvironment.CustomEventTopicKey),
             options));
     await client.SendEventAsync(
         new BinaryData(new TestEvent()
     {
         DataVersion = "1.0",
         EventTime = Recording.Now,
         EventType = "Microsoft.MockPublisher.TestEvent",
         Id = Recording.Random.NewGuid().ToString(),
         Subject = "Subject",
         Topic = "Topic"
     }));
 }
        public async Task CanPublishSingleCloudEvent()
        {
            EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.CloudEventTopicHost),
                    new AzureKeyCredential(TestEnvironment.CloudEventTopicKey),
                    options));

            await client.SendEventAsync(
                new CloudEvent(
                    "record",
                    "Microsoft.MockPublisher.TestEvent",
                    null)
            {
                Id      = Recording.Random.NewGuid().ToString(),
                Subject = "Subject",
                Time    = Recording.Now
            });
        }
Exemplo n.º 25
0
        public async Task SerializesExpectedProperties_BaseType()
        {
            var mockTransport = new MockTransport(new MockResponse(200));
            var options       = new EventGridPublisherClientOptions
            {
                Transport = mockTransport
            };
            EventGridPublisherClient client =
                new EventGridPublisherClient(
                    new Uri("http://localHost"),
                    new AzureKeyCredential("fakeKey"),
                    options);
            var cloudEvent = new CloudEvent(
                "record",
                "Microsoft.MockPublisher.TestEvent",
                new DerivedTestPayload
            {
                Name            = "name",
                Age             = 10,
                DerivedProperty = 5
            },
                "TestPayload",
                typeof(TestPayload));

            // since the data has not yet been serialized (CloudEvent not constructed from Parse method), GetData returns the passed in instance.
            Assert.AreEqual(5, cloudEvent.GetData <DerivedTestPayload>().DerivedProperty);

            // GetData returns as BinaryData so it will always serialize first even if cloudEvent was not constructed by calling Parse.
            Assert.IsNull(cloudEvent.GetData().ToObjectFromJson <DerivedTestPayload>().DerivedProperty);

            List <CloudEvent> eventsList = new List <CloudEvent>()
            {
                cloudEvent
            };

            await client.SendEventsAsync(eventsList);

            cloudEvent = DeserializeRequest(mockTransport.SingleRequest).First();
            Assert.IsNull(cloudEvent.GetData <DerivedTestPayload>().DerivedProperty);
            Assert.IsNull(cloudEvent.GetData().ToObjectFromJson <DerivedTestPayload>().DerivedProperty);
        }
        public async Task CanPublishSingleCloudEvent()
        {
            EventGridPublisherClientOptions options = InstrumentClientOptions(new EventGridPublisherClientOptions());
            EventGridPublisherClient        client  = InstrumentClient(
                new EventGridPublisherClient(
                    new Uri(TestEnvironment.CloudEventTopicHost),
                    new AzureKeyCredential(TestEnvironment.CloudEventTopicKey),
                    options));

            CloudEvent cloudEvent =
                new CloudEvent(
                    "record",
                    new Uri("http://localHost"),
                    Recording.Random.NewGuid().ToString(),
                    Recording.Now.DateTime)
            {
                Data = new TestPayload("name", 0)
            };

            await client.SendCloudEventAsync(cloudEvent);
        }
        public async Task SerializesExpectedProperties_BaseType()
        {
            var mockTransport = new MockTransport(new MockResponse(200));
            var options       = new EventGridPublisherClientOptions
            {
                Transport = mockTransport
            };
            EventGridPublisherClient client =
                new EventGridPublisherClient(
                    new Uri("http://localHost"),
                    new AzureKeyCredential("fakeKey"),
                    options);
            var egEvent = new EventGridEvent(
                "record",
                "Microsoft.MockPublisher.TestEvent",
                "TestPayload",
                new DerivedTestPayload
            {
                Name            = "name",
                Age             = 10,
                DerivedProperty = 5
            },
                typeof(TestPayload));

            // Data is a BinaryData so it will always serialize first even if cloudEvent was not constructed by calling Parse.
            Assert.IsNull(egEvent.Data.ToObjectFromJson <DerivedTestPayload>().DerivedProperty);

            List <EventGridEvent> eventsList = new List <EventGridEvent>()
            {
                egEvent
            };

            await client.SendEventsAsync(eventsList);

            egEvent = DeserializeRequest(mockTransport.SingleRequest).First();
            Assert.IsNull(egEvent.Data.ToObjectFromJson <DerivedTestPayload>().DerivedProperty);
        }
Exemplo n.º 28
0
        public async Task SetsTraceParentExtension(bool inclTraceparent, bool inclTracestate)
        {
            var mockTransport = new MockTransport(new MockResponse(200));
            var options       = new EventGridPublisherClientOptions
            {
                Transport = mockTransport
            };
            EventGridPublisherClient client =
                new EventGridPublisherClient(
                    new Uri("http://localHost"),
                    new AzureKeyCredential("fakeKey"),
                    options);
            var activity = new Activity($"{nameof(EventGridPublisherClient)}.{nameof(EventGridPublisherClient.SendEvents)}");

            activity.SetW3CFormat();
            activity.Start();
            List <CloudEvent> inputEvents = new List <CloudEvent>();

            for (int i = 0; i < 10; i++)
            {
                var cloudEvent =
                    new CloudEvent
                {
                    Subject = "record",
                    Source  = new Uri("http://localHost"),
                    Id      = Guid.NewGuid().ToString(),
                    Time    = DateTime.Now,
                    Type    = "test"
                };

                if (inclTraceparent && inclTracestate && i % 2 == 0)
                {
                    cloudEvent.SetAttributeFromString("traceparent", "traceparentValue");
                }
                if (inclTracestate && i % 2 == 0)
                {
                    cloudEvent.SetAttributeFromString("tracestate", "param:value");
                }
                inputEvents.Add(cloudEvent);
            }
            await client.SendCloudNativeCloudEventsAsync(inputEvents);

            activity.Stop();
            List <CloudEvent>        endEvents = DeserializeRequest(mockTransport.SingleRequest);
            IEnumerator <CloudEvent> inputEnum = inputEvents.GetEnumerator();

            foreach (CloudEvent cloudEvent in endEvents)
            {
                inputEnum.MoveNext();
                var inputAttributes = inputEnum.Current.GetPopulatedAttributes().Select(pair => pair.Key.Name).ToList();
                if (inputAttributes.Contains(TraceParentHeaderName) &&
                    inputAttributes.Contains(TraceStateHeaderName))
                {
                    Assert.AreEqual(
                        inputEnum.Current[TraceParentHeaderName],
                        cloudEvent[TraceParentHeaderName]);

                    Assert.AreEqual(
                        inputEnum.Current[TraceStateHeaderName],
                        cloudEvent[TraceStateHeaderName]);
                }
                else if (inputAttributes.Contains(TraceParentHeaderName))
                {
                    Assert.AreEqual(
                        inputEnum.Current[TraceParentHeaderName],
                        cloudEvent[TraceParentHeaderName]);
                }
                else if (inputAttributes.Contains(TraceStateHeaderName))
                {
                    Assert.AreEqual(
                        inputEnum.Current[TraceStateHeaderName],
                        cloudEvent[TraceStateHeaderName]);
                }
                else
                {
                    Assert.AreEqual(
                        activity.Id,
                        cloudEvent[TraceParentHeaderName]);
                }
            }
        }
        public async Task SetsTraceParentExtension(bool inclTraceparent, bool inclTracestate)
        {
            var mockTransport = new MockTransport(new MockResponse(200));
            var options       = new EventGridPublisherClientOptions
            {
                Transport = mockTransport
            };
            EventGridPublisherClient client =
                new EventGridPublisherClient(
                    new Uri("http://localHost"),
                    new AzureKeyCredential("fakeKey"),
                    options);
            var activity = new Activity($"{nameof(EventGridPublisherClient)}.{nameof(EventGridPublisherClient.SendEvents)}");

            activity.SetW3CFormat();
            activity.Start();
            List <CloudEvent> eventsList = new List <CloudEvent>();

            for (int i = 0; i < 10; i++)
            {
                var cloudEvent =
                    new CloudEvent(
                        "record",
                        new Uri("http://localHost"),
                        Guid.NewGuid().ToString(),
                        DateTime.Now);

                if (inclTraceparent && inclTracestate && i % 2 == 0)
                {
                    cloudEvent.GetAttributes().Add("traceparent", "traceparentValue");
                }
                if (inclTracestate && i % 2 == 0)
                {
                    cloudEvent.GetAttributes().Add("tracestate", "param:value");
                }
                eventsList.Add(cloudEvent);
            }
            await client.SendCloudEventsAsync(eventsList);

            activity.Stop();
            List <CloudEvent>        cloudEvents = DeserializeRequest(mockTransport.SingleRequest);
            IEnumerator <CloudEvent> cloudEnum   = eventsList.GetEnumerator();

            foreach (CloudEvent cloudEvent in cloudEvents)
            {
                cloudEnum.MoveNext();
                IDictionary <string, object> cloudEventAttr = cloudEnum.Current.GetAttributes();
                if (cloudEventAttr.ContainsKey(TraceParentHeaderName) &&
                    cloudEventAttr.ContainsKey(TraceStateHeaderName))
                {
                    Assert.AreEqual(
                        cloudEventAttr[TraceParentHeaderName],
                        cloudEvent.GetAttributes()[TraceParentHeaderName]);

                    Assert.AreEqual(
                        cloudEventAttr[TraceStateHeaderName],
                        cloudEvent.GetAttributes()[TraceStateHeaderName]);
                }
                else if (cloudEventAttr.ContainsKey(TraceParentHeaderName))
                {
                    Assert.AreEqual(
                        cloudEventAttr[TraceParentHeaderName],
                        cloudEvent.GetAttributes()[TraceParentHeaderName]);
                }
                else if (cloudEventAttr.ContainsKey(TraceStateHeaderName))
                {
                    Assert.AreEqual(
                        cloudEventAttr[TraceStateHeaderName],
                        cloudEvent.GetAttributes()[TraceStateHeaderName]);
                }
                else
                {
                    Assert.AreEqual(
                        activity.Id,
                        cloudEvent.GetAttributes()[TraceParentHeaderName]);
                }
            }
        }
Exemplo n.º 30
0
        public async Task SetsTraceParentExtension(bool inclTraceparent, bool inclTracestate)
        {
            var mockTransport = new MockTransport(new MockResponse(200));
            var options       = new EventGridPublisherClientOptions
            {
                Transport = mockTransport
            };
            EventGridPublisherClient client =
                new EventGridPublisherClient(
                    new Uri("http://localHost"),
                    new AzureKeyCredential("fakeKey"),
                    options);
            var activity = new Activity($"{nameof(EventGridPublisherClient)}.{nameof(EventGridPublisherClient.SendEvents)}");

            activity.SetW3CFormat();
            activity.Start();
            List <CloudEvent> eventsList = new List <CloudEvent>();

            for (int i = 0; i < 10; i++)
            {
                CloudEvent cloudEvent = new CloudEvent(
                    "record",
                    "Microsoft.MockPublisher.TestEvent",
                    JsonDocument.Parse("{\"property1\": \"abc\",  \"property2\": 123}").RootElement)
                {
                    Id      = "id",
                    Subject = $"Subject-{i}",
                    Time    = DateTimeOffset.UtcNow
                };
                if (inclTraceparent && i % 2 == 0)
                {
                    cloudEvent.ExtensionAttributes.Add("traceparent", "traceparentValue");
                }
                if (inclTracestate && i % 2 == 0)
                {
                    cloudEvent.ExtensionAttributes.Add("tracestate", "param:value");
                }
                eventsList.Add(cloudEvent);
            }
            await client.SendEventsAsync(eventsList);

            activity.Stop();
            List <CloudEvent>        cloudEvents = DeserializeRequest(mockTransport.SingleRequest);
            IEnumerator <CloudEvent> cloudEnum   = eventsList.GetEnumerator();

            foreach (CloudEvent cloudEvent in cloudEvents)
            {
                cloudEnum.MoveNext();
                Dictionary <string, object> cloudEventAttr = cloudEnum.Current.ExtensionAttributes;
                if (cloudEventAttr.ContainsKey(TraceParentHeaderName) &&
                    cloudEventAttr.ContainsKey(TraceStateHeaderName))
                {
                    Assert.AreEqual(
                        cloudEventAttr[TraceParentHeaderName],
                        cloudEvent.ExtensionAttributes[TraceParentHeaderName]);

                    Assert.AreEqual(
                        cloudEventAttr[TraceStateHeaderName],
                        cloudEvent.ExtensionAttributes[TraceStateHeaderName]);
                }
                else if (cloudEventAttr.ContainsKey(TraceParentHeaderName))
                {
                    Assert.AreEqual(
                        cloudEventAttr[TraceParentHeaderName],
                        cloudEvent.ExtensionAttributes[TraceParentHeaderName]);
                }
                else if (cloudEventAttr.ContainsKey(TraceStateHeaderName))
                {
                    Assert.AreEqual(
                        cloudEventAttr[TraceStateHeaderName],
                        cloudEvent.ExtensionAttributes[TraceStateHeaderName]);
                }
                else
                {
                    Assert.AreEqual(
                        activity.Id,
                        cloudEvent.ExtensionAttributes[TraceParentHeaderName]);
                }
            }
        }