示例#1
0
        public async Task MessageForOtherTopicThanSubscribedToIsProcessed()
        {
            // Arrange
            var mockMqttConnection            = new Mock <IMqttConnection>();
            var mockTriggeredFunctionExecutor = new Mock <ITriggeredFunctionExecutor>();

            mockMqttConnection
            .Setup(x => x.SubscribeAsync(It.IsAny <TopicFilter[]>()))
            .Returns(Task.CompletedTask);

            mockTriggeredFunctionExecutor
            .Setup(x => x.TryExecuteAsync(It.IsAny <TriggeredFunctionData>(), It.IsAny <CancellationToken>()));

            var topicFilter = new TopicFilter[] { new TopicFilter()
                                                  {
                                                      Topic = "test/topic", QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce
                                                  } };
            var mqttListener = new MqttListener(mockMqttConnection.Object, topicFilter, mockTriggeredFunctionExecutor.Object, _mockLogger.Object);

            // Act
            await mqttListener.StartAsync(_cancellationToken).ConfigureAwait(false);

            var message = new MqttMessage("weird", new byte[] { }, MqttQualityOfServiceLevel.AtLeastOnce, true);
            await mqttListener.OnMessage(new MqttMessageReceivedEventArgs(message));

            // Assert
            mockMqttConnection.VerifyAll();
            mockTriggeredFunctionExecutor.VerifyAll();
        }
示例#2
0
        public async Task StopAsyncUnsubscribesToTopics()
        {
            // Arrange
            var mockMqttConnection            = new Mock <IMqttConnection>();
            var mockTriggeredFunctionExecutor = new Mock <ITriggeredFunctionExecutor>();

            mockMqttConnection
            .Setup(x => x.SubscribeAsync(It.IsAny <TopicFilter[]>()))
            .Returns(Task.CompletedTask);

            mockMqttConnection
            .Setup(x => x.UnubscribeAsync(It.IsAny <string[]>()))
            .Returns(Task.CompletedTask);

            var topicFilter = new TopicFilter[] { new TopicFilter()
                                                  {
                                                      Topic = "test/topic", QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce
                                                  } };
            var mqttListener = new MqttListener(mockMqttConnection.Object, topicFilter, mockTriggeredFunctionExecutor.Object, _mockLogger.Object);

            // Act
            await mqttListener.StartAsync(_cancellationToken).ConfigureAwait(false);

            await mqttListener.StopAsync(_cancellationToken).ConfigureAwait(false);

            // Assert
            mockMqttConnection.VerifyAll();
        }
示例#3
0
        public async Task StartAsyncSubscribesToTopics()
        {
            // Arrange
            var mockMqttConnection            = new Mock <IMqttConnection>();
            var mockTriggeredFunctionExecutor = new Mock <ITriggeredFunctionExecutor>();

            mockMqttConnection
            .Setup(x => x.SubscribeAsync(It.IsAny <TopicFilter[]>()))
            .Returns(Task.CompletedTask);

            mockTriggeredFunctionExecutor
            .Setup(x => x.TryExecuteAsync(It.IsAny <TriggeredFunctionData>(), It.IsAny <CancellationToken>()));

            var topicFilter = new TopicFilter[] { new TopicFilter()
                                                  {
                                                      Topic = "test/topic", QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce
                                                  } };
            var mqttListener = new MqttListener(mockMqttConnection.Object, topicFilter, mockTriggeredFunctionExecutor.Object, _mockLogger.Object);

            // Act
            await mqttListener.StartAsync(_cancellationToken).ConfigureAwait(false);

            await mqttListener.OnMessage(new MqttMessageReceivedEventArgs(DefaultMessage)); // Shouldnt we be able to raise the IMqttConnection.OnMessageEventHandler??

            // Assert
            mockMqttConnection.VerifyAll();
            mockTriggeredFunctionExecutor.VerifyAll();
        }
        public async Task StartAsyncSubscribesToTopics()
        {
            // Arrange
            var mockManagedMqttClient         = new Mock <IManagedMqttClient>();
            var mockManagedMqttClientOptions  = new Mock <IManagedMqttClientOptions>();
            var mqttConfiguration             = new MqttConfiguration(mockManagedMqttClientOptions.Object, new[] { new TopicFilter("test/topic", MqttQualityOfServiceLevel.AtLeastOnce) });
            var mockMqttClientFactory         = new Mock <IMqttClientFactory>();
            var mockTriggeredFunctionExecutor = new Mock <ITriggeredFunctionExecutor>();

            mockManagedMqttClient
            .Setup(m => m.SubscribeAsync(It.Is <TopicFilter[]>(y => y == mqttConfiguration.Topics)))
            .Returns(Task.CompletedTask);

            mockManagedMqttClient
            .Setup(m => m.StartAsync(It.Is <IManagedMqttClientOptions>(y => y == mqttConfiguration.Options)))
            .Returns(Task.CompletedTask);

            mockMqttClientFactory
            .Setup(m => m.CreateManagedMqttClient())
            .Returns(mockManagedMqttClient.Object);

            var mqttListener = new MqttListener(mockMqttClientFactory.Object, mqttConfiguration, mockTriggeredFunctionExecutor.Object, _mockLogger.Object);

            // Act
            await mqttListener.StartAsync(_cancellationToken).ConfigureAwait(false);

            // Assert
            mockMqttClientFactory.Verify(x => x.CreateManagedMqttClient(), Times.Once());
            mockManagedMqttClient.VerifyAll();
        }
        public async Task NewMessageInvokesFunction()
        {
            // Arrange
            var mockManagedMqttClient        = new Mock <IManagedMqttClient>();
            var mockManagedMqttClientOptions = new Mock <IManagedMqttClientOptions>();

            mockManagedMqttClientOptions.Setup(x => x.ClientOptions.ClientId).Returns(Guid.NewGuid().ToString());
            var mqttConfiguration             = new MqttConfiguration(mockManagedMqttClientOptions.Object, new[] { new TopicFilter("test/topic", MqttQualityOfServiceLevel.AtLeastOnce) });
            var mockMqttClientFactory         = new Mock <IMqttClientFactory>();
            var mockTriggeredFunctionExecutor = new Mock <ITriggeredFunctionExecutor>();

            mockManagedMqttClient
            .Setup(m => m.SubscribeAsync(It.Is <TopicFilter[]>(y => y == mqttConfiguration.Topics)))
            .Returns(Task.CompletedTask);
            mockManagedMqttClient
            .Setup(m => m.StartAsync(It.Is <IManagedMqttClientOptions>(y => y == mqttConfiguration.Options)))
            .Returns(Task.CompletedTask);

            mockMqttClientFactory
            .Setup(m => m.CreateManagedMqttClient())
            .Returns(mockManagedMqttClient.Object);

            var mqttListener = new MqttListener(mockMqttClientFactory.Object, mqttConfiguration, mockTriggeredFunctionExecutor.Object, _mockLogger.Object);
            var testMessage  = new MqttApplicationMessageBuilder()
                               .WithPayload("{}")
                               .WithTopic("123")
                               .WithAtLeastOnceQoS()
                               .Build();

            // Act
            await mqttListener.StartAsync(_cancellationToken).ConfigureAwait(false);

            mockManagedMqttClient.Raise(x => x.ApplicationMessageReceived += null, new MqttApplicationMessageReceivedEventArgs(mqttConfiguration.Options.ClientOptions.ClientId, testMessage));

            // Assert
            mockTriggeredFunctionExecutor.Verify(x => x.TryExecuteAsync(It.IsAny <TriggeredFunctionData>(), It.IsAny <CancellationToken>()), Times.Once());
        }
        public async Task InitializationFailsThrowsRightException()
        {
            // Arrange
            var mockManagedMqttClient         = new Mock <IManagedMqttClient>();
            var mockManagedMqttClientOptions  = new Mock <IManagedMqttClientOptions>();
            var mqttConfiguration             = new MqttConfiguration(mockManagedMqttClientOptions.Object, new[] { new TopicFilter("test/topic", MqttQualityOfServiceLevel.AtLeastOnce) });
            var mockMqttClientFactory         = new Mock <IMqttClientFactory>();
            var mockTriggeredFunctionExecutor = new Mock <ITriggeredFunctionExecutor>();

            mockMqttClientFactory
            .Setup(m => m.CreateManagedMqttClient())
            .Throws <Exception>();

            var mqttListener = new MqttListener(mockMqttClientFactory.Object, mqttConfiguration, mockTriggeredFunctionExecutor.Object, _mockLogger.Object);

            // Act & Assert
            var ex = await Assert.ThrowsAsync <MqttListenerInitializationException>(async() => await mqttListener.StartAsync(_cancellationToken));
        }