Пример #1
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)));
        }
Пример #2
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));
        }
Пример #3
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]);
        }
Пример #4
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));
        }
Пример #5
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));
        }
Пример #6
0
        public void Ctor()
        {
            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny<Func<MqttMessage, bool>>()));

            Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object);

            chMock.VerifyAll();
        }
Пример #7
0
        public void Ctor()
        {
            var chMock = new Mock <IMqttConnectionHandler>();

            chMock.Setup(x => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >()));

            Nmqtt.SubscriptionsManager subs = new Nmqtt.SubscriptionsManager(chMock.Object);

            chMock.VerifyAll();
        }
Пример #8
0
        public void DisposeUnregistersMessageCallback()
        {
            var chMock = new Mock<IMqttConnectionHandler>();
            chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny<Func<MqttMessage, bool>>()));
            chMock.Setup((x) => x.UnRegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny<Func<MqttMessage, bool>>()));

            Nmqtt.SubscriptionsManager subMgr = new Nmqtt.SubscriptionsManager(chMock.Object);
            subMgr.Dispose();

            chMock.VerifyAll();
        }
Пример #9
0
        public void DisposeUnregistersMessageCallback()
        {
            var chMock = new Mock <IMqttConnectionHandler>();

            chMock.Setup((x) => x.RegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >()));
            chMock.Setup((x) => x.UnRegisterForMessage(MqttMessageType.SubscribeAck, It.IsAny <Func <MqttMessage, bool> >()));

            Nmqtt.SubscriptionsManager subMgr = new Nmqtt.SubscriptionsManager(chMock.Object);
            subMgr.Dispose();

            chMock.VerifyAll();
        }
Пример #10
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));
        }
Пример #11
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));
        }
Пример #12
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));
        }
Пример #13
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));
        }
Пример #14
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));
        }
Пример #15
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));
        }
Пример #16
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]);
        }
Пример #17
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));
        }
Пример #18
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));
        }
Пример #19
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));
        }
Пример #20
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"));
        }
Пример #21
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)));
        }