public void ConsequtivePublishOnSameTopicGetsNextMessageId() { var chMock = new Mock<IMqttConnectionHandler>(); chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>())); // publish and save the first id var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return true; }); int firstMsgId = pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test"); int secondMsgId = pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test"); chMock.VerifyAll(); Assert.Equal<int>(firstMsgId + 1, secondMsgId); }
public void ConsequtivePublishOnSameTopicGetsNextMessageId() { var chMock = new Mock <IMqttConnectionHandler>(); chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishMessage>())); // publish and save the first id var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); }); int firstMsgId = pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test"); int secondMsgId = pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test"); chMock.VerifyAll(); Assert.Equal <int>(firstMsgId + 1, secondMsgId); }
/// <summary> /// Publishes a message to the message broker. /// </summary> /// <typeparam name="TDataConverter">The type of the data converter to use for converting the data /// from the object to wire bytes.</typeparam> /// <param name="topic">The topic to publish the message to.</param> /// <param name="qualityOfService">The quality of service to attach to the message.</param> /// <param name="data">The message to publish.</param> /// <returns> /// The message identiier assigned to the message. /// </returns> public short PublishMessage <TDataConverter>(string topic, MqttQos qualityOfService, object data) where TDataConverter : IPublishDataConverter { if (connectionHandler.State != ConnectionState.Connected) { throw new ConnectionException(connectionHandler.State); } return(publishingManager.Publish <TDataConverter>(topic, qualityOfService, data)); }
/// <summary> /// Publishes a message to the message broker. /// </summary> /// <typeparam name="TPayloadConverter">The type of the data converter to use.</typeparam> /// <typeparam name="T">The Type of the data being published</typeparam> /// <param name="topic">The topic to publish the message to.</param> /// <param name="qualityOfService">The quality of service to attach to the message.</param> /// <param name="data">The message to publish.</param> /// <returns> /// The message identiier assigned to the message. /// </returns> public short PublishMessage <T, TPayloadConverter>(string topic, MqttQos qualityOfService, T data) where TPayloadConverter : IPayloadConverter <T>, new() { if (connectionHandler.State != ConnectionState.Connected) { throw new ConnectionException(connectionHandler.State); } return(publishingManager.Publish <T, TPayloadConverter>(topic, qualityOfService, data)); }
public void PublishQos1Or2SavesMessageInStorage() { var chMock = new Mock <IMqttConnectionHandler>(); var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return(true); }); int msgId = pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtLeastOnce, "test"); var msgs = GetPublishedMessages(pm); Assert.True(msgs.ContainsKey(msgId)); }
public void PublishSendsPublishMessageThroughConnectionHandler() { var chMock = new Mock <IMqttConnectionHandler>(); chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishMessage>())); var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return(true); }); pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test"); chMock.VerifyAll(); }
/// <summary> /// Publishes a message to the message broker. /// </summary> /// <typeparam name="TPayloadConverter">The type of the data converter to use.</typeparam> /// <typeparam name="T">The Type of the data being published</typeparam> /// <param name="topic">The topic to publish the message to.</param> /// <param name="qualityOfService">The quality of service to attach to the message.</param> /// <param name="data">The message to publish.</param> /// <returns> /// The message identiier assigned to the message. /// </returns> /// <exception cref="InvalidTopicException">Thrown if the topic supplied violates the MQTT topic format rules.</exception> public short PublishMessage <T, TPayloadConverter>(string topic, MqttQos qualityOfService, T data) where TPayloadConverter : IPayloadConverter <T>, new() { if (connectionHandler.State != ConnectionState.Connected) { throw new ConnectionException(connectionHandler.State); } try { var pubTopic = new PublicationTopic(topic); return(publishingManager.Publish <T, TPayloadConverter>(pubTopic, qualityOfService, data)); } catch (ArgumentException ex) { throw new InvalidTopicException(ex.Message, topic, ex); } }
public void PublishReturnIdMatchesPublishedMessageId() { MqttPublishMessage pubMsg = null; var chMock = new Mock <IMqttConnectionHandler>(); chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishMessage>())) .Callback((MqttMessage msg) => pubMsg = (MqttPublishMessage)msg); var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); }); int retId = pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test"); chMock.VerifyAll(); // check the message message ids match Assert.Equal <int>(pubMsg.VariableHeader.MessageIdentifier, retId); }
public void PublishSendsPublishMessageCorrectPayload() { MqttPublishMessage pubMsg = null; var chMock = new Mock <IMqttConnectionHandler>(); chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishMessage>())) .Callback((MqttMessage msg) => pubMsg = (MqttPublishMessage)msg); var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); }); pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test"); chMock.VerifyAll(); // check the message payload was correct Assert.Equal <string>("test", Encoding.ASCII.GetString(pubMsg.Payload.Message.ToArray <byte>())); }
public void PublishSendsPublishMessageWithCorrectTopic() { MqttPublishMessage pubMsg = null; var chMock = new Mock <IMqttConnectionHandler>(); chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishMessage>())) .Callback((MqttMessage msg) => pubMsg = (MqttPublishMessage)msg); var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); }); pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test"); chMock.VerifyAll(); // check the message topic was correct Assert.Equal <string>("A/Topic", pubMsg.VariableHeader.TopicName); }
public void AcknowledgedQos1MessageRemovedFromStorage() { Func<MqttMessage, bool> ackCallback = null; var chMock = new Mock<IMqttConnectionHandler>(); chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishAck, It.IsAny<Func<MqttMessage, bool>>())) .Callback((MqttMessageType msgtype, Func<MqttMessage, bool> cb) => { ackCallback = cb; }); // send the message, verify we've stored it ok. var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return true; }); var msgId = pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtLeastOnce, "test"); var msgs = GetPublishedMessages(pm); Assert.True(msgs.ContainsKey(msgId)); // now fake an acknowledgement of the message, and ensure it's been removed from storage. ackCallback(new MqttPublishAckMessage().WithMessageIdentifier(msgId)); Assert.False(msgs.ContainsKey(msgId)); }
public void AcknowledgedQos1MessageRemovedFromStorage() { Func <MqttMessage, bool> ackCallback = null; var chMock = new Mock <IMqttConnectionHandler>(); chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishAck, It.IsAny <Func <MqttMessage, bool> >())) .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => { ackCallback = cb; }); // send the message, verify we've stored it ok. var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return(true); }); var msgId = pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.AtLeastOnce, "test"); var msgs = GetPublishedMessages(pm); Assert.True(msgs.ContainsKey(msgId)); // now fake an acknowledgement of the message, and ensure it's been removed from storage. ackCallback(new MqttPublishAckMessage().WithMessageIdentifier(msgId)); Assert.False(msgs.ContainsKey(msgId)); }
public void ReleasedQos2MessageRemovedFromStorage() { Func <MqttMessage, bool> rcvdCallback = null; Func <MqttMessage, bool> compCallback = null; var chMock = new Mock <IMqttConnectionHandler>(); chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishReceived, It.IsAny <Func <MqttMessage, bool> >())) .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => { rcvdCallback = cb; }); chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishRelease, It.IsAny <Func <MqttMessage, bool> >())); chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishComplete, It.IsAny <Func <MqttMessage, bool> >())) .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => { compCallback = cb; }); chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishMessage>())); chMock.Setup(x => x.SendMessage(It.IsAny <MqttPublishReleaseMessage>())); // send the message, verify we've stored it ok. var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return(true); }); var msgId = pm.Publish <AsciiPublishDataConverter>("A/Topic", MqttQos.ExactlyOnce, "test"); var msgs = GetPublishedMessages(pm); Assert.True(msgs.ContainsKey(msgId)); // verify the pub msg has send a publish message. chMock.Verify(x => x.SendMessage(It.IsAny <MqttPublishMessage>())); // fake a response from the other party saying Received, this should initiate a Release to the other party rcvdCallback(new MqttPublishReceivedMessage().WithMessageIdentifier(msgId)); Assert.True(msgs.ContainsKey(msgId)); // verify the pub msg has sent a publish release message. chMock.Verify(x => x.SendMessage(It.IsAny <MqttPublishReleaseMessage>())); // fake a response from the other party saying "Complete", this should remove our copy of the message locally. compCallback(new MqttPublishCompleteMessage().WithMessageIdentifier(msgId)); Assert.False(msgs.ContainsKey(msgId)); }
public void PublishQos1Or2SavesMessageInStorage() { var chMock = new Mock<IMqttConnectionHandler>(); var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return true; }); int msgId = pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtLeastOnce, "test"); var msgs = GetPublishedMessages(pm); Assert.True(msgs.ContainsKey(msgId)); }
public void ReleasedQos2MessageRemovedFromStorage() { Func<MqttMessage, bool> rcvdCallback = null; Func<MqttMessage, bool> compCallback = null; var chMock = new Mock<IMqttConnectionHandler>(); chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishReceived, It.IsAny<Func<MqttMessage, bool>>())) .Callback((MqttMessageType msgtype, Func<MqttMessage, bool> cb) => { rcvdCallback = cb; }); chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishRelease, It.IsAny<Func<MqttMessage, bool>>())); chMock.Setup(x => x.RegisterForMessage(MqttMessageType.PublishComplete, It.IsAny<Func<MqttMessage, bool>>())) .Callback((MqttMessageType msgtype, Func<MqttMessage, bool> cb) => { compCallback = cb; }); chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>())); chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishReleaseMessage>())); // send the message, verify we've stored it ok. var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return true; }); var msgId = pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.ExactlyOnce, "test"); var msgs = GetPublishedMessages(pm); Assert.True(msgs.ContainsKey(msgId)); // verify the pub msg has send a publish message. chMock.Verify(x => x.SendMessage(It.IsAny<MqttPublishMessage>())); // fake a response from the other party saying Received, this should initiate a Release to the other party rcvdCallback(new MqttPublishReceivedMessage().WithMessageIdentifier(msgId)); Assert.True(msgs.ContainsKey(msgId)); // verify the pub msg has sent a publish release message. chMock.Verify(x => x.SendMessage(It.IsAny<MqttPublishReleaseMessage>())); // fake a response from the other party saying "Complete", this should remove our copy of the message locally. compCallback(new MqttPublishCompleteMessage().WithMessageIdentifier(msgId)); Assert.False(msgs.ContainsKey(msgId)); }
public void PublishSendsPublishMessageWithCorrectTopic() { MqttPublishMessage pubMsg = null; var chMock = new Mock<IMqttConnectionHandler>(); chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>())) .Callback((MqttMessage msg) => pubMsg = (MqttPublishMessage)msg); var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return true; }); pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test"); chMock.VerifyAll(); // check the message topic was correct Assert.Equal<string>("A/Topic", pubMsg.VariableHeader.TopicName); }
public void PublishSendsPublishMessageThroughConnectionHandler() { var chMock = new Mock<IMqttConnectionHandler>(); chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>())); var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage pubMsg) => { return true; }); pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test"); chMock.VerifyAll(); }
public void PublishSendsPublishMessageCorrectPayload() { MqttPublishMessage pubMsg = null; var chMock = new Mock<IMqttConnectionHandler>(); chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>())) .Callback((MqttMessage msg) => pubMsg = (MqttPublishMessage)msg); var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return true; }); pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test"); chMock.VerifyAll(); // check the message payload was correct Assert.Equal<string>("test", Encoding.ASCII.GetString(pubMsg.Payload.Message.ToArray<byte>())); }
public void PublishReturnIdMatchesPublishedMessageId() { MqttPublishMessage pubMsg = null; var chMock = new Mock<IMqttConnectionHandler>(); chMock.Setup(x => x.SendMessage(It.IsAny<MqttPublishMessage>())) .Callback((MqttMessage msg) => pubMsg = (MqttPublishMessage)msg); var pm = new Nmqtt.PublishingManager(chMock.Object, (MqttPublishMessage dummyPubMsg) => { return true; }); int retId = pm.Publish<AsciiPublishDataConverter>("A/Topic", MqttQos.AtMostOnce, "test"); chMock.VerifyAll(); // check the message message ids match Assert.Equal<int>(pubMsg.VariableHeader.MessageIdentifier, retId); }