public void CreateEventWithExtensions()
        {
            var cloudEvent = new CloudEvent(
                "com.github.pull.create",
                new Uri("https://github.com/cloudevents/spec/pull/123"),
                "A234-1234-1234",
                new DateTime(2018, 4, 5, 17, 31, 0, DateTimeKind.Utc),
                new ComExampleExtension1Extension()
            {
                ComExampleExtension1 = "value"
            },
                new ComExampleExtension2Extension()
            {
                ComExampleExtension2 = new ComExampleExtension2Data()
                {
                    OtherValue = 5
                }
            })
            {
                DataContentType = new ContentType(MediaTypeNames.Text.Xml),
                Data            = "<much wow=\"xml\"/>"
            };

            Assert.Equal(CloudEventsSpecVersion.Default, cloudEvent.SpecVersion);
            Assert.Equal("com.github.pull.create", cloudEvent.Type);
            Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
            Assert.Equal("A234-1234-1234", cloudEvent.Id);
            Assert.Equal(DateTime.Parse("2018-04-05T17:31:00Z").ToUniversalTime(),
                         cloudEvent.Time.Value.ToUniversalTime());
            Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
            Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);

            Assert.Equal("value", cloudEvent.Extension <ComExampleExtension1Extension>().ComExampleExtension1);
            Assert.Equal(5, cloudEvent.Extension <ComExampleExtension2Extension>().ComExampleExtension2.OtherValue);
        }
        public void CreateEventWithExtensions()
        {
            var cloudEvent = new CloudEvent(
                "com.github.pull.create",
                new Uri("https://github.com/cloudevents/spec/pull/123"),
                "A234-1234-1234",
                sampleTimestamp,
                new ComExampleExtension1Extension()
            {
                ComExampleExtension1 = "value"
            })
            {
                DataContentType = new ContentType(MediaTypeNames.Text.Xml),
                Data            = "<much wow=\"xml\"/>"
            };

            Assert.Equal(CloudEventsSpecVersion.Default, cloudEvent.SpecVersion);
            Assert.Equal("com.github.pull.create", cloudEvent.Type);
            Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), cloudEvent.Source);
            Assert.Equal("A234-1234-1234", cloudEvent.Id);
            AssertTimestampsEqual("2018-04-05T17:31:00Z", cloudEvent.Time.Value);
            Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), cloudEvent.DataContentType);
            Assert.Equal("<much wow=\"xml\"/>", cloudEvent.Data);

            Assert.Equal("value", cloudEvent.Extension <ComExampleExtension1Extension>().ComExampleExtension1);
        }
        public LogDetail CreateLogDetail()
        {
            CloudEvent cloudEvent = _options.Value.CloudEventAccessor();

            if (cloudEvent == null)
            {
                return(new LogDetail {
                    CanExtract = false
                });
            }
            return(new LogDetail
            {
                CanExtract = cloudEvent != null,
                PageRest = cloudEvent.Id,
                PageRest2 = cloudEvent.Type,
                Page = cloudEvent.Subject,
                Verb = "Event",
                Uri = cloudEvent.Source.ToString(),
                ServerName = cloudEvent.Source.DnsSafeHost,
                HostAddress = string.Empty,
                UserAgent = cloudEvent.SpecVersion.ToString(),
                CorrelationId = cloudEvent.Extension <DistributedTracingExtension>()?.TraceParent ?? string.Empty,
                Payload = cloudEvent.Data.ToString(),
                Warning = string.Empty
            });
        }
Example #4
0
 private static void InitPartitioningKey(Message <string, byte[]> message, CloudEvent cloudEvent)
 {
     if (!string.IsNullOrEmpty(message.Key))
     {
         var extension = cloudEvent.Extension <PartitioningExtension>();
         extension.PartitioningKeyValue = message.Key;
     }
 }
Example #5
0
        public void KafkaBinaryMessageTest()
        {
            // Kafka doesn't provide any way to get to the message transport level to do the test properly
            // and it doesn't have an embedded version of a server for .Net so the lowest we can get is
            // the `Message<T, K>`

            var jsonEventFormatter = new JsonEventFormatter();
            var cloudEvent         = new CloudEvent("com.github.pull.create",
                                                    new Uri("https://github.com/cloudevents/spec/pull/123"),
                                                    extensions: new PartitioningExtension())
            {
                Id              = "A234-1234-1234",
                Time            = new DateTimeOffset(2018, 4, 5, 17, 31, 0, TimeSpan.Zero),
                DataContentType = new ContentType(MediaTypeNames.Text.Xml),
                Data            = Encoding.UTF8.GetBytes("<much wow=\"xml\"/>")
            };

            var attrs = cloudEvent.GetAttributes();

            attrs["comexampleextension1"] = "value";
            cloudEvent.Extension <PartitioningExtension>().PartitioningKeyValue = "hello much wow";

            var message = new KafkaCloudEventMessage(cloudEvent, ContentMode.Binary, new JsonEventFormatter());

            Assert.True(message.IsCloudEvent());

            // using serialization to create fully independent copy thus simulating message transport
            // real transport will work in a similar way
            var serialized = JsonConvert.SerializeObject(message, new HeaderConverter());
            var settings   = new JsonSerializerSettings
            {
                Converters = { new HeadersConverter(), new HeaderConverter() }
            };
            var messageCopy = JsonConvert.DeserializeObject <Message <string, byte[]> >(serialized, settings);

            Assert.True(messageCopy.IsCloudEvent());
            var receivedCloudEvent = messageCopy.ToCloudEvent(jsonEventFormatter, new PartitioningExtension());

            Assert.Equal(CloudEventsSpecVersion.Default, receivedCloudEvent.SpecVersion);
            Assert.Equal("com.github.pull.create", receivedCloudEvent.Type);
            Assert.Equal(new Uri("https://github.com/cloudevents/spec/pull/123"), receivedCloudEvent.Source);
            Assert.Equal("A234-1234-1234", receivedCloudEvent.Id);
            AssertTimestampsEqual("2018-04-05T17:31:00Z", receivedCloudEvent.Time.Value);
            Assert.Equal(new ContentType(MediaTypeNames.Text.Xml), receivedCloudEvent.DataContentType);
            Assert.Equal(Encoding.UTF8.GetBytes("<much wow=\"xml\"/>"), receivedCloudEvent.Data);
            Assert.Equal("hello much wow", receivedCloudEvent.Extension <PartitioningExtension>().PartitioningKeyValue);

            var attr = receivedCloudEvent.GetAttributes();

            Assert.Equal("value", (string)attr["comexampleextension1"]);
        }
Example #6
0
        public static IEvent ToEvent(this CloudEvent cloudEvent, Func <string, Type> typeResolver)
        {
            var eventType     = typeResolver(cloudEvent.Type);
            var stringContent = cloudEvent.Data as string;

            if (stringContent == null)
            {
                throw new ArgumentException("expected cloud event data to by of type string");
            }

            var @event   = JsonSerializer.Deserialize(stringContent, eventType) as EventRecord;
            var envelope = EventEnvelope.Create(cloudEvent.Id, @event);

            envelope.Meta.AddMetaData(cloudEvent.Extension <EventMetaDataExtension>().MetaData);
            return(envelope);
        }
        protected string ExtractPartitionKey(CloudEvent cloudEvent)
        {
            var extension = cloudEvent.Extension <PartitioningExtension>();

            return(extension?.PartitioningKeyValue);
        }