コード例 #1
0
        public async Task SerializeAsJson_Action_PayloadSerialized()
        {
            var message = new TestEventOne {
                Content = "Hello MQTT!"
            };
            var serializer = new JsonMessageSerializer <TestEventOne>
            {
                Options = new JsonSerializerOptions
                {
                    WriteIndented = true
                }
            };
            var messageBytes = (await serializer.SerializeAsync(
                                    message,
                                    new MessageHeaderCollection(),
                                    MessageSerializationContext.Empty)).ReadAll();
            var builder = new MqttLastWillMessageBuilder();

            builder
            .ProduceTo("testaments")
            .SerializeAsJson(
                serializerBuilder => serializerBuilder
                .WithOptions(
                    new JsonSerializerOptions
            {
                WriteIndented = true
            }))
            .Message(message);

            var willMessage = builder.Build();

            willMessage.Payload.Should().NotBeNullOrEmpty();
            willMessage.Payload.Should().BeEquivalentTo(messageBytes);
        }
コード例 #2
0
        public async Task SerializeUsing_NewtonsoftJsonSerializer_PayloadSerialized()
        {
            var message = new TestEventOne {
                Content = "Hello MQTT!"
            };
            var serializer = new NewtonsoftJsonMessageSerializer <TestEventOne>
            {
                Encoding = MessageEncoding.Unicode,
                Settings = new JsonSerializerSettings
                {
                    Formatting = Formatting.Indented
                }
            };
            var messageBytes = (await serializer.SerializeAsync(
                                    message,
                                    new MessageHeaderCollection(),
                                    MessageSerializationContext.Empty)).ReadAll();
            var builder = new MqttLastWillMessageBuilder();

            builder
            .ProduceTo("testaments")
            .SerializeUsing(serializer)
            .Message(message);

            var willMessage = builder.Build();

            willMessage.Payload.Should().NotBeNullOrEmpty();
            willMessage.Payload.Should().BeEquivalentTo(messageBytes);
        }
コード例 #3
0
        public void Build_WithoutTopicName_ExceptionThrown()
        {
            var builder = new MqttLastWillMessageBuilder();

            Action act = () => builder.Build();

            act.Should().ThrowExactly <EndpointConfigurationException>();
        }
コード例 #4
0
        public void ProduceTo_TopicName_TopicSet()
        {
            var builder = new MqttLastWillMessageBuilder();

            builder.ProduceTo("testaments");

            var willMessage = builder.Build();

            willMessage.Topic.Should().Be("testaments");
        }
コード例 #5
0
        public void WithAtMostOnceQoS_QosLevelSet()
        {
            var builder = new MqttLastWillMessageBuilder();

            builder
            .ProduceTo("testaments")
            .WithAtMostOnceQoS();

            var willMessage = builder.Build();

            willMessage.QualityOfServiceLevel.Should().Be(MqttQualityOfServiceLevel.AtMostOnce);
        }
コード例 #6
0
        public void WithRetain_RetainFlagSet()
        {
            var builder = new MqttLastWillMessageBuilder();

            builder
            .ProduceTo("testaments")
            .Retain();

            var willMessage = builder.Build();

            willMessage.Retain.Should().BeTrue();
        }
コード例 #7
0
        public async Task Message_SomeMessage_PayloadSet()
        {
            var message = new TestEventOne {
                Content = "Hello MQTT!"
            };
            var builder = new MqttLastWillMessageBuilder();

            builder
            .ProduceTo("testaments")
            .Message(message);

            var willMessage = builder.Build();

            willMessage.Payload.Should().NotBeNullOrEmpty();

            (object?deserializedMessage, Type _) = await new JsonMessageSerializer <TestEventOne>().DeserializeAsync(
                new MemoryStream(willMessage.Payload),
                new MessageHeaderCollection(),
                MessageSerializationContext.Empty);

            deserializedMessage.Should().BeEquivalentTo(message);
        }