public async Task MultipleTriggersCustomConfigReceiveOwnMessages() { using (var mqttServer = await MqttServerHelper.Get(_logger)) using (var jobHost = await JobHostHelper <MultipleTriggersCustomConfigTestFunction> .RunFor(_testLoggerProvider)) { var firstMessage = new MqttApplicationMessageBuilder() .WithTopic("test/asd/1") .WithPayload("{ \"test\":\"case\" }") .WithAtLeastOnceQoS() .Build(); await mqttServer.PublishAsync(firstMessage); var secondMessage = new MqttApplicationMessageBuilder() .WithTopic("test/asd/2") .WithPayload("{ \"test\":\"case\" }") .WithAtLeastOnceQoS() .Build(); await mqttServer.PublishAsync(secondMessage); await WaitFor(() => MultipleTriggersCustomConfigTestFunction.CallCountFunction1 >= 1 && MultipleTriggersCustomConfigTestFunction.CallCountFunction2 >= 1); } Assert.Equal(1, MultipleTriggersCustomConfigTestFunction.CallCountFunction1); Assert.Equal(1, MultipleTriggersCustomConfigTestFunction.CallCountFunction2); Assert.Equal("test/asd/1", MultipleTriggersCustomConfigTestFunction.LastReceivedMessageFunction1.Topic); Assert.Equal("test/asd/2", MultipleTriggersCustomConfigTestFunction.LastReceivedMessageFunction2.Topic); }
public async Task ICollectorOutputsArePublished() { var mqttApplicationMessages = new List <MqttApplicationMessage>();; using (var mqttServer = await MqttServerHelper.Get(_logger)) using (var mqttClient = await MqttClientHelper.Get(_logger)) using (var jobHost = await JobHostHelper <ICollectorOutputIsPublishedTestFunction> .RunFor(_testLoggerProvider)) { await mqttClient.SubscribeAsync("test/outtopic"); await mqttClient.SubscribeAsync("test/outtopic2"); mqttClient.OnMessage += (object sender, OnMessageEventArgs e) => mqttApplicationMessages.Add(e.ApplicationMessage); await jobHost.CallAsync(nameof(ICollectorOutputIsPublishedTestFunction.Testert)); await WaitFor(() => ICollectorOutputIsPublishedTestFunction.CallCount >= 1); await WaitFor(() => mqttApplicationMessages.Count > 0); } Assert.Equal(1, ICollectorOutputIsPublishedTestFunction.CallCount); Assert.Equal(2, mqttApplicationMessages.Count); Assert.Contains(mqttApplicationMessages, x => x.Topic == "test/outtopic"); Assert.Contains(mqttApplicationMessages, x => x.Topic == "test/outtopic2"); var bodyString = Encoding.UTF8.GetString(mqttApplicationMessages.First().Payload); Assert.Equal("", bodyString); }
public async Task UsernameAndPasswordAreValidated() { var validated = false; var options = new MqttServerOptionsBuilder() .WithConnectionValidator(x => { validated = true; x.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; if (x.Username == "admin" && x.Password == "Welkom123") { x.ReturnCode = MqttConnectReturnCode.ConnectionAccepted; } }) .Build(); using (var mqttServer = await MqttServerHelper.Get(_logger, options)) using (var jobHost = await JobHostHelper <UsernameAndPasswordTestFunction> .RunFor(_testLoggerProvider)) { await mqttServer.PublishAsync(DefaultMessage); await WaitFor(() => UsernameAndPasswordTestFunction.CallCount >= 1); } Assert.Equal(1, UsernameAndPasswordTestFunction.CallCount); Assert.True(validated, "Username and Password are not validated by the Mqtt server"); }
public async Task MultipleJobHostsOnlyOneIsActive() { var clientIdsSeen = new List <string>(); var options = new MqttServerOptionsBuilder() .WithConnectionValidator((x) => { if (x.ClientId != "IntegrationTest" && !clientIdsSeen.Any(y => y == x.ClientId)) { clientIdsSeen.Add(x.ClientId); } }) .Build(); using (var mqttServer = await MqttServerHelper.Get(_logger, options)) using (var jobHost1 = await JobHostHelper <MultipleJobHostsTestFunction> .RunFor(_loggerFactory, true)) using (var jobHost2 = await JobHostHelper <MultipleJobHostsTestFunction> .RunFor(_loggerFactory, false)) { for (int i = 0; i < 30; i++) { await mqttServer.PublishAsync(DefaultMessage); } await WaitFor(() => MultipleJobHostsTestFunction.CallCount >= 30); } Assert.Equal(30, MultipleJobHostsTestFunction.CallCount); Assert.Single(clientIdsSeen); Assert.Equal("test/topic", MultipleJobHostsTestFunction.LastReceivedMessage.Topic); var messageBody = Encoding.UTF8.GetString(MultipleJobHostsTestFunction.LastReceivedMessage.GetMessage()); Assert.Equal("{ \"test\":\"case\" }", messageBody); }
public async Task TriggerAndOutputReuseConnection() { var mqttApplicationMessages = new List <MqttApplicationMessage>(); var counnections = 0; var options = new MqttServerOptionsBuilder() .WithConnectionValidator(x => { counnections += (x.ClientId != "IntegrationTest") ? 1 : 0; Debug.WriteLine($"ClientId:{x.ClientId}"); }) .Build(); using (var mqttServer = await MqttServerHelper.Get(_logger, options)) using (var mqttClient = await MqttClientHelper.Get(_logger)) using (var jobHost = await JobHostHelper <TriggerAndOutputWithSameConnectionTestFunction> .RunFor(_testLoggerProvider)) { await mqttClient.SubscribeAsync("test/outtopic"); await mqttClient.SubscribeAsync("test/outtopic2"); await mqttServer.PublishAsync(DefaultMessage); var firstMessage = await mqttClient.WaitForMessage(); if (firstMessage != null) { mqttApplicationMessages.Add(firstMessage); var secondMessage = await mqttClient.WaitForMessage(); if (secondMessage != null) { mqttApplicationMessages.Add(secondMessage); } } await WaitFor(() => TriggerAndOutputWithSameConnectionTestFunction.CallCount >= 1); } Assert.Equal(1, TriggerAndOutputWithSameConnectionTestFunction.CallCount); Assert.Equal(1, counnections); Assert.Equal(2, mqttApplicationMessages.Count); Assert.Contains(mqttApplicationMessages, x => x.Topic == "test/outtopic"); Assert.Contains(mqttApplicationMessages, x => x.Topic == "test/outtopic2"); var bodyString = Encoding.UTF8.GetString(mqttApplicationMessages.First().Payload); Assert.Equal("{\"test\":\"message\"}", bodyString); }
public async Task SimpleMessageIsReceived() { using (var mqttServer = await MqttServerHelper.Get(_logger)) using (var jobHost = await JobHostHelper <SimpleMessageIsReceivedTestFunction> .RunFor(_testLoggerProvider)) { await mqttServer.PublishAsync(DefaultMessage); await WaitFor(() => SimpleMessageIsReceivedTestFunction.CallCount >= 1); } Assert.Equal(1, SimpleMessageIsReceivedTestFunction.CallCount); Assert.Equal("test/topic", SimpleMessageIsReceivedTestFunction.LastReceivedMessage.Topic); var messageBody = Encoding.UTF8.GetString(SimpleMessageIsReceivedTestFunction.LastReceivedMessage.GetMessage()); Assert.Equal("{ \"test\":\"case\" }", messageBody); }
public async Task MqttServerOnOtherPortReceivesMessage() { var options = new MqttServerOptionsBuilder() .WithDefaultEndpointPort(1337) .Build(); using (var mqttServer = await MqttServerHelper.Get(_logger, options)) using (var jobHost = await JobHostHelper <SimpleMessageAnotherPortTestFunction> .RunFor(_testLoggerProvider)) { await mqttServer.PublishAsync(DefaultMessage); await WaitFor(() => SimpleMessageAnotherPortTestFunction.CallCount >= 1); } Assert.Equal(1, SimpleMessageAnotherPortTestFunction.CallCount); }
public async Task SimpleMessageIsPublished() { MqttApplicationMessage mqttApplicationMessage = null; using (var mqttServer = await MqttServerHelper.Get(_logger)) using (var mqttClient = await MqttClientHelper.Get(_logger)) using (var jobHost = await JobHostHelper <SimpleOutputIsPublishedTestFunction> .RunFor(_testLoggerProvider)) { await mqttClient.SubscribeAsync("test/topic"); await jobHost.CallAsync(nameof(SimpleOutputIsPublishedTestFunction.Testert)); mqttApplicationMessage = await mqttClient.WaitForMessage(); } Assert.NotNull(mqttApplicationMessage); }
public async Task ComplexTopicFilterIsUsed() { using (var mqttServer = await MqttServerHelper.Get(_logger)) using (var jobHost = await JobHostHelper <ComplexTopicFilterIsUsedFunction> .RunFor(_testLoggerProvider)) { await mqttServer.PublishAsync(DefaultMessage); await WaitFor(() => ComplexTopicFilterIsUsedFunction.CallCount >= 1); } Assert.Equal(1, ComplexTopicFilterIsUsedFunction.CallCount); Assert.Equal("test/topic", ComplexTopicFilterIsUsedFunction.LastReceivedMessage.Topic); Assert.Equal(Messaging.MqttQualityOfServiceLevel.AtMostOnce, ComplexTopicFilterIsUsedFunction.LastReceivedMessage.QosLevel); var messageBody = Encoding.UTF8.GetString(ComplexTopicFilterIsUsedFunction.LastReceivedMessage.GetMessage()); Assert.Equal("{ \"test\":\"case\" }", messageBody); }
public async Task TriggerAndOutputUseDifferentConnection() { MqttApplicationMessage mqttApplicationMessage = null; var connectionsCountServer1 = 0; var optionsServer1 = new MqttServerOptionsBuilder() .WithDefaultEndpointPort(1337) .WithConnectionValidator(x => { connectionsCountServer1 += (x.ClientId != "IntegrationTest") ? 1 : 0; }) .Build(); var connectionsCountServer2 = 0; var optionsServer2 = new MqttServerOptionsBuilder() .WithConnectionValidator(x => { connectionsCountServer2 += (x.ClientId != "IntegrationTest") ? 1 : 0; }) .Build(); using (var mqttServer1 = await MqttServerHelper.Get(_logger, optionsServer1)) using (var mqttServer2 = await MqttServerHelper.Get(_logger, optionsServer2)) using (var mqttClientForServer2 = await MqttClientHelper.Get(_logger)) using (var jobHost = await JobHostHelper <TriggerAndOutputWithDifferentConnectionTestFunction> .RunFor(_testLoggerProvider)) { await mqttClientForServer2.SubscribeAsync("test/outtopic"); await mqttServer1.PublishAsync(DefaultMessage); mqttApplicationMessage = await mqttClientForServer2.WaitForMessage(); await WaitFor(() => TriggerAndOutputWithDifferentConnectionTestFunction.CallCount >= 1); } Assert.Equal(1, TriggerAndOutputWithDifferentConnectionTestFunction.CallCount); Assert.Equal(1, connectionsCountServer1); Assert.Equal(1, connectionsCountServer2); Assert.NotNull(mqttApplicationMessage); Assert.Equal("test/outtopic", mqttApplicationMessage.Topic); var bodyString = Encoding.UTF8.GetString(mqttApplicationMessage.Payload); Assert.Equal("{\"test\":\"message\"}", bodyString); }
public async Task CustomConnectionWithClientIdIsReceived() { string clientId = string.Empty; var options = new MqttServerOptionsBuilder() .WithConnectionValidator(x => clientId = x.ClientId) .Build(); using (var mqttServer = await MqttServerHelper.Get(_logger, options)) using (var jobHost = await JobHostHelper <CustomConnectionStringWithClientIdTestFunction> .RunFor(_testLoggerProvider)) { await mqttServer.PublishAsync(DefaultMessage); await WaitFor(() => CustomConnectionStringWithClientIdTestFunction.CallCount >= 1); } Assert.Equal(1, CustomConnectionStringWithClientIdTestFunction.CallCount); Assert.Equal("Custom", clientId); }
public async Task WhenTlsIsSetToTrueASecureConnectionIsMade() { var options = new MqttServerOptionsBuilder() .WithEncryptedEndpoint() .WithEncryptionCertificate(new X509Certificate2(@"Certificates/myRootCA.pfx", "12345", X509KeyStorageFlags.Exportable)) .WithoutDefaultEndpoint() .Build(); using (var mqttServer = await MqttServerHelper.Get(_logger, options)) using (var jobHost = await JobHostHelper <FunctionConnectingWithTlsEnabledTestFunction> .RunFor(_testLoggerProvider)) { await mqttServer.PublishAsync(DefaultMessage); await WaitFor(() => FunctionConnectingWithTlsEnabledTestFunction.CallCount >= 1, 20); } Assert.Equal(1, FunctionConnectingWithTlsEnabledTestFunction.CallCount); }
public async Task SimpleMessageIsPublished() { MqttApplicationMessage mqttApplicationMessage = null; using (var mqttServer = await MqttServerHelper.Get(_logger)) using (var mqttClient = await MqttClientHelper.Get(_logger)) using (var jobHost = await JobHostHelper <SimpleOutputIsPublishedTestFunction> .RunFor(_loggerFactory)) { await mqttClient.SubscribeAsync("test/topic"); mqttClient.OnMessage += (object sender, OnMessageEventArgs e) => mqttApplicationMessage = e.ApplicationMessage; await jobHost.CallAsync(nameof(SimpleOutputIsPublishedTestFunction.Testert)); await WaitFor(() => mqttApplicationMessage != null); } Assert.NotNull(mqttApplicationMessage); }
public async Task CustomMqttConfigProviderGetsTriggered() { var message = new MqttApplicationMessageBuilder() .WithTopic("TestTopic/random") .WithPayload("{ \"test\":\"case\" }") .WithAtLeastOnceQoS() .Build(); using (var mqttServer = await MqttServerHelper.Get(_logger)) using (var jobHost = await JobHostHelper <CustomMqttConfigProviderTestFunction> .RunFor(_testLoggerProvider)) { await mqttServer.PublishAsync(message); await WaitFor(() => CustomMqttConfigProviderTestFunction.CallCount >= 1); } Assert.Equal(1, CustomMqttConfigProviderTestFunction.CallCount); Assert.Equal("TestTopic/random", CustomMqttConfigProviderTestFunction.LastReceivedMessage.Topic); var messageBody = Encoding.UTF8.GetString(CustomMqttConfigProviderTestFunction.LastReceivedMessage.GetMessage()); Assert.Equal("{ \"test\":\"case\" }", messageBody); }
public async Task MultipleTriggersWithSameConnectionThrowsExceptiojn() { using (var mqttServer = await MqttServerHelper.Get(_logger)) { JobHostHelper <MultipleTriggersSameConnectionTestFunction> jobHostHelper = null; var ex = Assert.ThrowsAsync <Exception>(async() => jobHostHelper = await JobHostHelper <MultipleTriggersSameConnectionTestFunction> .RunFor(_testLoggerProvider)); jobHostHelper?.Dispose(); } }