Beispiel #1
0
        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));
        }
Beispiel #2
0
        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]);
        }
Beispiel #3
0
        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));
        }
Beispiel #4
0
        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));
        }
Beispiel #5
0
        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)));
        }
Beispiel #6
0
        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));
        }
Beispiel #7
0
        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));
        }
Beispiel #8
0
        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);
        }
Beispiel #9
0
        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));
        }
Beispiel #10
0
        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));
        }
Beispiel #11
0
        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));
        }
Beispiel #12
0
        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));
        }
Beispiel #13
0
        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));
        }
Beispiel #14
0
        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]);
        }
Beispiel #15
0
        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));
        }
Beispiel #16
0
        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));
        }
Beispiel #17
0
        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"));
        }
Beispiel #18
0
        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)));
        }
Beispiel #19
0
        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));
        }