public void MultipleSubscriptionsForAcknowledgedSubscriptionThrowsException() { Func <MqttMessage, bool> theCallback = null; MqttSubscribeMessage subMsg = null; var chMock = new Mock <IMqttConnectionHandler>(); // mock the call to register and save the callback for later. chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >())) .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => theCallback = cb); // mock the call to Send(), which should occur when the subscription manager tries to subscribe chMock.Setup(x => x.SendMessage(It.IsAny <MqttSubscribeMessage>())) .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg); string topic = "testtopic"; MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object); short subid = subs.RegisterSubscription <AsciiPublishDataConverter>(topic, qos); chMock.VerifyAll(); // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys); Assert.Equal <MqttQos>(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]); // execute the callback that would normally be initiated by the connection handler when a sub ack message arrived. theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(subid).AddQosGrant(MqttQos.AtMostOnce)); Assert.Equal <SubscriptionStatus>(SubscriptionStatus.Active, subs.GetSubscriptionsStatus(topic)); // NOW THE IMPORTANT PART - Try and subscribe agin to the same topic. Assert.Throws <ArgumentException>(() => subs.RegisterSubscription <AsciiPublishDataConverter>(topic, qos)); }
public void SubscriptionRequestInvokesSend() { MqttSubscribeMessage subMsg = null; var pubMock = new Mock <IPublishingManager>(); var chMock = new Mock <IMqttConnectionHandler>(); // mock the call to register and save the callback for later. chMock.Setup(x => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >())); // mock the call to Send(), which should occur when the subscription manager tries to subscribe chMock.Setup(x => x.SendMessage(It.IsAny <MqttSubscribeMessage>())) .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg); const string topic = "testtopic"; const MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object); subs.RegisterSubscription <string, AsciiPayloadConverter>(topic, qos); chMock.VerifyAll(); // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys); Assert.Equal(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]); }
public void AcknowledgedSubscriptionRequestCreatesActiveSubscription() { Func <MqttMessage, bool> theCallback = null; MqttSubscribeMessage subMsg = null; var pubMock = new Mock <IPublishingManager>(); var chMock = new Mock <IMqttConnectionHandler>(); // mock the call to register and save the callback for later. chMock.Setup(x => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >())) .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => theCallback = cb); // mock the call to Send(), which should occur when the subscription manager tries to subscribe chMock.Setup(x => x.SendMessage(It.IsAny <MqttSubscribeMessage>())) .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg); const string topic = "testtopic"; const MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object); subs.RegisterSubscription <string, AsciiPayloadConverter>(topic, qos); chMock.VerifyAll(); // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys); Assert.Equal(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]); // execute the callback that would normally be initiated by the connection handler when a sub ack message arrived. theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(1).AddQosGrant(MqttQos.AtMostOnce)); Assert.Equal(SubscriptionStatus.Active, subs.GetSubscriptionsStatus(topic)); }
public void AcknowledgedSubscriptionRequestCreatesActiveSubscription() { Func<MqttMessage, bool> theCallback = null; MqttSubscribeMessage subMsg = null; var chMock = new Mock<IMqttConnectionHandler>(); // mock the call to register and save the callback for later. chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny<Func<MqttMessage, bool>>())) .Callback((MqttMessageType msgtype, Func<MqttMessage, bool> cb) => theCallback = cb); // mock the call to Send(), which should occur when the subscription manager tries to subscribe chMock.Setup(x => x.SendMessage(It.IsAny<MqttSubscribeMessage>())) .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg); string topic = "testtopic"; MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object); short subid = subs.RegisterSubscription<AsciiPublishDataConverter>(topic, qos); chMock.VerifyAll(); // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys); Assert.Equal<MqttQos>(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]); // execute the callback that would normally be initiated by the connection handler when a sub ack message arrived. theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(subid).AddQosGrant(MqttQos.AtMostOnce)); Assert.Equal<SubscriptionStatus>(SubscriptionStatus.Active, subs.GetSubscriptionsStatus(topic)); }
public void SubscriptionAckForNonPendingSubscriptionThrowsException() { Func <MqttMessage, bool> theCallback = null; MqttSubscribeMessage subMsg = null; var pubMock = new Mock <IPublishingManager>(); var chMock = new Mock <IMqttConnectionHandler>(); // mock the call to register and save the callback for later. chMock.Setup(x => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >())) .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => theCallback = cb); // mock the call to Send(), which should occur when the subscription manager tries to subscribe chMock.Setup(x => x.SendMessage(It.IsAny <MqttSubscribeMessage>())) .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg); const string topic = "testtopic"; const MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object); subs.RegisterSubscription <string, AsciiPayloadConverter>(topic, qos); chMock.VerifyAll(); // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys); Assert.Equal(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]); // execute the callback with a bogus message identifier. Assert.Throws <ArgumentException>(() => theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(999).AddQosGrant(MqttQos.AtMostOnce))); }
public void MultipleSubscriptionsForSamePendingThrowsException() { var chMock = new Mock <IMqttConnectionHandler>(); // mock the call to register and save the callback for later. chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >())); string topic = "testtopic"; MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object); subs.RegisterSubscription <AsciiPublishDataConverter>(topic, qos); chMock.VerifyAll(); Assert.Throws <ArgumentException>(() => subs.RegisterSubscription <AsciiPublishDataConverter>(topic, qos)); }
public IObservable <MqttReceivedMessage <T> > ListenTo <T, TPayloadConverter>(string topic, MqttQos qosLevel) where TPayloadConverter : IPayloadConverter <T>, new() { if (connectionHandler.State != ConnectionState.Connected) { throw new ConnectionException(connectionHandler.State); } return(subscriptionsManager.RegisterSubscription <T, TPayloadConverter>(topic, qosLevel)); }
public short Subscribe <TPublishDataConverter>(string topic, MqttQos qosLevel) where TPublishDataConverter : IPublishDataConverter { if (connectionHandler.State != ConnectionState.Connected) { throw new ConnectionException(connectionHandler.State); } short messageIdentifier = subscriptionsManager.RegisterSubscription <TPublishDataConverter>(topic, qosLevel); return(messageIdentifier); }
public void SubscriptionAgainstInvalidTopicThrowsInvalidTopicException() { var chMock = new Mock <IMqttConnectionHandler>(); var pubMock = new Mock <IPublishingManager>(); const string badTopic = "finance/ibm#"; const MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object); Assert.Throws <InvalidTopicException>(() => subs.RegisterSubscription <byte[], PassThroughPayloadConverter>(badTopic, qos)); }
public void SubscriptionRequestCreatesPendingSubscription() { var chMock = new Mock <IMqttConnectionHandler>(); string topic = "testtopic"; MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object); subs.RegisterSubscription <AsciiPublishDataConverter>(topic, qos); Assert.Equal <SubscriptionStatus>(SubscriptionStatus.Pending, subs.GetSubscriptionsStatus(topic)); }
public void GetSubscriptionForPendingSubscriptionReturnsNull() { var chMock = new Mock <IMqttConnectionHandler>(); string topic = "testtopic"; MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object); var subid = subs.RegisterSubscription <AsciiPublishDataConverter>(topic, qos); Assert.Null(subs.GetSubscription(topic)); }
public void SubscriptionRequestCreatesPendingSubscription() { var chMock = new Mock<IMqttConnectionHandler>(); var pubMock = new Mock<IPublishingManager>(); const string topic = "testtopic"; const MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object); subs.RegisterSubscription<string, AsciiPayloadConverter>(topic, qos); Assert.Equal(SubscriptionStatus.Pending, subs.GetSubscriptionsStatus(topic)); }
public void GetSubscriptionWithValidTopicReturnsSubscription() { Func <MqttMessage, bool> theCallback = null; var chMock = new Mock <IMqttConnectionHandler>(); chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >())) .Callback((MqttMessageType msgtype, Func <MqttMessage, bool> cb) => theCallback = cb); string topic = "testtopic"; MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object); var subid = subs.RegisterSubscription <AsciiPublishDataConverter>(topic, qos); // execute the callback that would normally be initiated by the connection handler when a sub ack message arrived. theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(subid).AddQosGrant(MqttQos.AtMostOnce)); Assert.NotNull(subs.GetSubscription(topic)); }
public void SubscriptionRequestInvokesSend() { MqttSubscribeMessage subMsg = null; var pubMock = new Mock<IPublishingManager>(); var chMock = new Mock<IMqttConnectionHandler>(); // mock the call to register and save the callback for later. chMock.Setup(x => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny<Func<MqttMessage, bool>>())); // mock the call to Send(), which should occur when the subscription manager tries to subscribe chMock.Setup(x => x.SendMessage(It.IsAny<MqttSubscribeMessage>())) .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg); const string topic = "testtopic"; const MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object); subs.RegisterSubscription<string, AsciiPayloadConverter>(topic, qos); chMock.VerifyAll(); // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys); Assert.Equal(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]); }
public void GetSubscriptionForPendingSubscriptionReturnsNull() { var chMock = new Mock<IMqttConnectionHandler>(); string topic = "testtopic"; MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object); var subid = subs.RegisterSubscription<AsciiPublishDataConverter>(topic, qos); Assert.Null(subs.GetSubscription(topic)); }
public void SubscriptionAgainstInvalidTopicThrowsInvalidTopicException() { var chMock = new Mock<IMqttConnectionHandler>(); var pubMock = new Mock<IPublishingManager>(); const string badTopic = "finance/ibm#"; const MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object); Assert.Throws<InvalidTopicException>(() => subs.RegisterSubscription<byte[], PassThroughPayloadConverter>(badTopic, qos)); }
public void GetSubscriptionWithInvalidTopicReturnsNull() { Func<MqttMessage, bool> theCallback = null; var pubMock = new Mock<IPublishingManager>(); var chMock = new Mock<IMqttConnectionHandler>(); chMock.Setup(x => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny<Func<MqttMessage, bool>>())) .Callback((MqttMessageType msgtype, Func<MqttMessage, bool> cb) => theCallback = cb); const string topic = "testtopic"; const MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object); subs.RegisterSubscription<string, AsciiPayloadConverter>(topic, qos); // execute the callback that would normally be initiated by the connection handler when a sub ack message arrived. theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(1).AddQosGrant(MqttQos.AtMostOnce)); Assert.Null(subs.GetSubscription("abc_badTopic")); }
public void SubscriptionAckForNonPendingSubscriptionThrowsException() { Func<MqttMessage, bool> theCallback = null; MqttSubscribeMessage subMsg = null; var pubMock = new Mock<IPublishingManager>(); var chMock = new Mock<IMqttConnectionHandler>(); // mock the call to register and save the callback for later. chMock.Setup(x => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny<Func<MqttMessage, bool>>())) .Callback((MqttMessageType msgtype, Func<MqttMessage, bool> cb) => theCallback = cb); // mock the call to Send(), which should occur when the subscription manager tries to subscribe chMock.Setup(x => x.SendMessage(It.IsAny<MqttSubscribeMessage>())) .Callback((MqttMessage msg) => subMsg = (MqttSubscribeMessage)msg); const string topic = "testtopic"; const MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. var subs = new Nmqtt.SubscriptionsManager(chMock.Object, pubMock.Object); subs.RegisterSubscription<string, AsciiPayloadConverter>(topic, qos); chMock.VerifyAll(); // now check the message generated by the subscription manager was good - ie contain the topic at the specified qos Assert.Contains(topic, subMsg.Payload.Subscriptions.Keys); Assert.Equal(MqttQos.AtMostOnce, subMsg.Payload.Subscriptions[topic]); // execute the callback with a bogus message identifier. Assert.Throws<ArgumentException>(() => theCallback(new MqttSubscribeAckMessage().WithMessageIdentifier(999).AddQosGrant(MqttQos.AtMostOnce))); }
public void MultipleSubscriptionsForSamePendingThrowsException() { var chMock = new Mock<IMqttConnectionHandler>(); // mock the call to register and save the callback for later. chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny<Func<MqttMessage, bool>>())); string topic = "testtopic"; MqttQos qos = MqttQos.AtMostOnce; // run and verify the mocks were called. Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object); subs.RegisterSubscription<AsciiPublishDataConverter>(topic, qos); chMock.VerifyAll(); Assert.Throws<ArgumentException>(() => subs.RegisterSubscription<AsciiPublishDataConverter>(topic, qos)); }