public void AsArray_ReturningOriginal()
        {
            byte[] original = { 1, 2, 3, 4, 5 };
            var    segment  = new ArraySegment <byte>(original);

            Assert.Same(original, BinaryDataUtilities.AsArray(segment));
        }
        public void AsArray_FromPartialSegment(int offset)
        {
            byte[] original = { 1, 2, 3, 4, 5 };
            var    segment  = new ArraySegment <byte>(original, offset, 4);
            var    actual   = BinaryDataUtilities.AsArray(segment);

            Assert.True(actual.SequenceEqual(segment));
            Assert.NotSame(original, actual);
        }
        public void EncodeBinaryModeEventData_NoContentType_LeavesBinaryData()
        {
            var cloudEvent = new CloudEvent
            {
                Data = SampleBinaryData
            }.PopulateRequiredAttributes();

            // EncodeBinaryModeEventData does *not* implicitly encode binary data as JSON.
            var data  = new JsonEventFormatter().EncodeBinaryModeEventData(cloudEvent);
            var array = BinaryDataUtilities.AsArray(data);

            Assert.Equal(array, SampleBinaryData);
        }
Ejemplo n.º 4
0
        // TODO: Update to a newer version of MQTTNet and support both binary and structured mode?
        /// <summary>
        /// Converts a CloudEvent to <see cref="MqttApplicationMessage"/>.
        /// </summary>
        /// <param name="cloudEvent">The CloudEvent to convert. Must not be null, and must be a valid CloudEvent.</param>
        /// <param name="contentMode">Content mode. Currently only structured mode is supported.</param>
        /// <param name="formatter">The formatter to use within the conversion. Must not be null.</param>
        /// <param name="topic">The MQTT topic for the message. May be null.</param>
        public static MqttApplicationMessage ToMqttApplicationMessage(this CloudEvent cloudEvent, ContentMode contentMode, CloudEventFormatter formatter, string?topic)
        {
            Validation.CheckCloudEventArgument(cloudEvent, nameof(cloudEvent));
            Validation.CheckNotNull(formatter, nameof(formatter));

            switch (contentMode)
            {
            case ContentMode.Structured:
                return(new MqttApplicationMessage
                {
                    Topic = topic,
                    Payload = BinaryDataUtilities.AsArray(formatter.EncodeStructuredModeMessage(cloudEvent, out _))
                });

            default:
                throw new ArgumentOutOfRangeException(nameof(contentMode), $"Unsupported content mode: {contentMode}");
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Converts a CloudEvent to <see cref="Message"/>.
        /// </summary>
        /// <param name="cloudEvent">The CloudEvent to convert. Must not be null, and must be a valid CloudEvent.</param>
        /// <param name="contentMode">Content mode. Structured or binary.</param>
        /// <param name="formatter">The formatter to use within the conversion. Must not be null.</param>
        public static Message ToAmqpMessage(this CloudEvent cloudEvent, ContentMode contentMode, CloudEventFormatter formatter)
        {
            Validation.CheckCloudEventArgument(cloudEvent, nameof(cloudEvent));
            Validation.CheckNotNull(formatter, nameof(formatter));

            var applicationProperties = MapHeaders(cloudEvent);
            RestrictedDescribed bodySection;
            Properties          properties;

            switch (contentMode)
            {
            case ContentMode.Structured:
                bodySection = new Data
                {
                    Binary = BinaryDataUtilities.AsArray(formatter.EncodeStructuredModeMessage(cloudEvent, out var contentType))
                };
                // TODO: What about the other parts of the content type?
                properties = new Properties {
                    ContentType = contentType.MediaType
                };
                break;

            case ContentMode.Binary:
                bodySection = new Data {
                    Binary = BinaryDataUtilities.AsArray(formatter.EncodeBinaryModeEventData(cloudEvent))
                };
                properties = new Properties {
                    ContentType = formatter.GetOrInferDataContentType(cloudEvent)
                };
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(contentMode), $"Unsupported content mode: {contentMode}");
            }
            return(new Message
            {
                ApplicationProperties = applicationProperties,
                BodySection = bodySection,
                Properties = properties
            });
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Converts a CloudEvent to a Kafka message.
        /// </summary>
        /// <param name="cloudEvent">The CloudEvent to convert. Must not be null, and must be a valid CloudEvent.</param>
        /// <param name="contentMode">Content mode. Structured or binary.</param>
        /// <param name="formatter">The formatter to use within the conversion. Must not be null.</param>
        public static Message <string?, byte[]> ToKafkaMessage(this CloudEvent cloudEvent, ContentMode contentMode, CloudEventFormatter formatter)
        {
            Validation.CheckCloudEventArgument(cloudEvent, nameof(cloudEvent));
            Validation.CheckNotNull(formatter, nameof(formatter));

            // TODO: Is this appropriate? Why can't we transport a CloudEvent without data in Kafka?
            Validation.CheckArgument(cloudEvent.Data is object, nameof(cloudEvent), "Only CloudEvents with data can be converted to Kafka messages");
            var    headers = MapHeaders(cloudEvent);
            string?key     = (string?)cloudEvent[Partitioning.PartitionKeyAttribute];

            byte[] value;
            string?contentTypeHeaderValue;

            switch (contentMode)
            {
            case ContentMode.Structured:
                value = BinaryDataUtilities.AsArray(formatter.EncodeStructuredModeMessage(cloudEvent, out var contentType));
                // TODO: What about the non-media type parts?
                contentTypeHeaderValue = contentType.MediaType;
                break;

            case ContentMode.Binary:
                value = BinaryDataUtilities.AsArray(formatter.EncodeBinaryModeEventData(cloudEvent));
                contentTypeHeaderValue = formatter.GetOrInferDataContentType(cloudEvent);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(contentMode), $"Unsupported content mode: {contentMode}");
            }
            if (contentTypeHeaderValue is object)
            {
                headers.Add(KafkaContentTypeAttributeName, Encoding.UTF8.GetBytes(contentTypeHeaderValue));
            }
            return(new Message <string?, byte[]>
            {
                Headers = headers,
                Value = value,
                Key = key
            });
        }
        /// <summary>
        /// Converts a CloudEvent to <see cref="Message"/>.
        /// </summary>
        /// <param name="cloudEvent">The CloudEvent to convert. Must not be null, and must be a valid CloudEvent.</param>
        /// <param name="contentMode">Content mode. Structured or binary.</param>
        /// <param name="formatter">The formatter to use within the conversion. Must not be null.</param>
        /// <returns>A <see cref="Message"/>.</returns>
        public static Message ToServiceBusMessage(
            this CloudEvent cloudEvent,
            ContentMode contentMode,
            CloudEventFormatter formatter
            )
        {
            Validation.CheckCloudEventArgument(cloudEvent, nameof(cloudEvent));
            Validation.CheckNotNull(formatter, nameof(formatter));

            Message message;

            switch (contentMode)
            {
            case ContentMode.Structured:
                message = new Message(
                    BinaryDataUtilities.AsArray(formatter.EncodeStructuredModeMessage(cloudEvent, out var contentType))
                    )
                {
                    ContentType = contentType.MediaType,
                };
                break;

            case ContentMode.Binary:
                message = new Message(
                    BinaryDataUtilities.AsArray(formatter.EncodeBinaryModeEventData(cloudEvent))
                    )
                {
                    ContentType = cloudEvent.DataContentType,
                };
                break;

            default:
                throw new ArgumentException($"Unsupported content mode: {contentMode}", nameof(contentMode));
            }

            MapHeaders(cloudEvent, message);

            return(message);
        }