public async Task when_unsubscribing_existing_subscriptions_then_subscriptions_are_deleted_and_ack_is_sent()
        {
            var sessionRepository = new Mock <IRepository <ClientSession> > ();
            var clientId          = Guid.NewGuid().ToString();
            var packetId          = (ushort)new Random().Next(0, ushort.MaxValue);
            var topic             = "foo/bar/test";
            var qos     = MqttQualityOfService.AtLeastOnce;
            var session = new ClientSession {
                ClientId      = clientId,
                Clean         = false,
                Subscriptions = new List <ClientSubscription> {
                    new ClientSubscription {
                        ClientId = clientId, MaximumQualityOfService = qos, TopicFilter = topic
                    }
                }
            };
            var updatedSession = default(ClientSession);

            sessionRepository.Setup(r => r.Get(It.IsAny <Expression <Func <ClientSession, bool> > > ())).Returns(session);
            sessionRepository.Setup(r => r.Update(It.IsAny <ClientSession> ())).Callback <ClientSession> (s => updatedSession = s);

            var unsubscribe = new Unsubscribe(packetId, topic);

            var channel = new Mock <IMqttChannel <IPacket> > ();

            var response = default(IPacket);

            channel.Setup(c => c.SendAsync(It.IsAny <IPacket> ()))
            .Callback <IPacket> (p => response = p)
            .Returns(Task.Delay(0));

            var connectionProvider = new Mock <IConnectionProvider> ();

            connectionProvider
            .Setup(p => p.GetConnection(It.Is <string> (c => c == clientId)))
            .Returns(channel.Object);

            var flow = new ServerUnsubscribeFlow(sessionRepository.Object);

            await flow.ExecuteAsync(clientId, unsubscribe, channel.Object)
            .ConfigureAwait(continueOnCapturedContext: false);

            Assert.NotNull(response);
            Assert.Equal(0, updatedSession.Subscriptions.Count);

            var unsubscribeAck = response as UnsubscribeAck;

            Assert.NotNull(unsubscribeAck);
            Assert.Equal(packetId, unsubscribeAck.PacketId);
        }
Beispiel #2
0
        public async Task when_unsubscribing_existing_subscriptions_then_subscriptions_are_deleted_and_ack_is_sent()
        {
            Mock <IRepository <ClientSession> > sessionRepository = new Mock <IRepository <ClientSession> >();
            string clientId              = Guid.NewGuid().ToString();
            ushort packetId              = (ushort)new Random().Next(0, ushort.MaxValue);
            string topic                 = "foo/bar/test";
            MqttQualityOfService qos     = MqttQualityOfService.AtLeastOnce;
            ClientSession        session = new ClientSession(clientId, clean: false)
            {
                Subscriptions = new List <ClientSubscription> {
                    new ClientSubscription {
                        ClientId = clientId, MaximumQualityOfService = qos, TopicFilter = topic
                    }
                }
            };
            ClientSession updatedSession = default;

            sessionRepository.Setup(r => r.Read(It.IsAny <string>())).Returns(session);
            sessionRepository.Setup(r => r.Update(It.IsAny <ClientSession>())).Callback <ClientSession>(s => updatedSession = s);

            Unsubscribe unsubscribe = new Unsubscribe(packetId, topic);

            Mock <IMqttChannel <IPacket> > channel = new Mock <IMqttChannel <IPacket> >();

            IPacket response = default;

            channel.Setup(c => c.SendAsync(It.IsAny <IPacket>()))
            .Callback <IPacket>(p => response = p)
            .Returns(Task.Delay(0));

            Mock <IConnectionProvider> connectionProvider = new Mock <IConnectionProvider>();

            connectionProvider
            .Setup(p => p.GetConnection(It.Is <string>(c => c == clientId)))
            .Returns(channel.Object);

            ServerUnsubscribeFlow flow = new ServerUnsubscribeFlow(sessionRepository.Object);

            await flow.ExecuteAsync(clientId, unsubscribe, channel.Object);

            Assert.NotNull(response);
            0.Should().Be(updatedSession.Subscriptions.Count);

            UnsubscribeAck unsubscribeAck = response as UnsubscribeAck;

            Assert.NotNull(unsubscribeAck);
            packetId.Should().Be(unsubscribeAck.PacketId);
        }
        public async Task when_unsubscribing_not_existing_subscriptions_then_ack_is_sent()
        {
            var sessionRepository = new Mock <IRepository <ClientSession> > ();
            var clientId          = Guid.NewGuid().ToString();
            var packetId          = (ushort)new Random().Next(0, ushort.MaxValue);
            var session           = new ClientSession {
                ClientId = clientId,
                Clean    = false
            };

            sessionRepository.Setup(r => r.Get(It.IsAny <Expression <Func <ClientSession, bool> > > ())).Returns(session);

            var unsubscribe = new Unsubscribe(packetId, "foo/bar");

            var channel = new Mock <IMqttChannel <IPacket> > ();

            var response = default(IPacket);

            channel.Setup(c => c.SendAsync(It.IsAny <IPacket> ()))
            .Callback <IPacket> (p => response = p)
            .Returns(Task.Delay(0));

            var connectionProvider = new Mock <IConnectionProvider> ();

            connectionProvider
            .Setup(p => p.GetConnection(It.Is <string> (c => c == clientId)))
            .Returns(channel.Object);

            var flow = new ServerUnsubscribeFlow(sessionRepository.Object);

            await flow.ExecuteAsync(clientId, unsubscribe, channel.Object)
            .ConfigureAwait(continueOnCapturedContext: false);

            sessionRepository.Verify(r => r.Delete(It.IsAny <Expression <Func <ClientSession, bool> > > ()), Times.Never);
            Assert.NotNull(response);

            var unsubscribeAck = response as UnsubscribeAck;

            Assert.NotNull(unsubscribeAck);
            Assert.Equal(packetId, unsubscribeAck.PacketId);
        }
Beispiel #4
0
        public async Task when_unsubscribing_not_existing_subscriptions_then_ack_is_sent()
        {
            Mock <IRepository <ClientSession> > sessionRepository = new Mock <IRepository <ClientSession> >();
            string        clientId = Guid.NewGuid().ToString();
            ushort        packetId = (ushort)new Random().Next(0, ushort.MaxValue);
            ClientSession session  = new ClientSession(clientId, clean: false);

            sessionRepository.Setup(r => r.Read(It.IsAny <string>())).Returns(session);

            Unsubscribe unsubscribe = new Unsubscribe(packetId, "foo/bar");

            Mock <IMqttChannel <IPacket> > channel = new Mock <IMqttChannel <IPacket> >();

            IPacket response = default;

            channel.Setup(c => c.SendAsync(It.IsAny <IPacket>()))
            .Callback <IPacket>(p => response = p)
            .Returns(Task.Delay(0));

            Mock <IConnectionProvider> connectionProvider = new Mock <IConnectionProvider>();

            connectionProvider
            .Setup(p => p.GetConnection(It.Is <string>(c => c == clientId)))
            .Returns(channel.Object);

            ServerUnsubscribeFlow flow = new ServerUnsubscribeFlow(sessionRepository.Object);

            await flow.ExecuteAsync(clientId, unsubscribe, channel.Object);

            sessionRepository.Verify(r => r.Delete(It.IsAny <string>()), Times.Never);
            Assert.NotNull(response);

            UnsubscribeAck unsubscribeAck = response as UnsubscribeAck;

            Assert.NotNull(unsubscribeAck);
            packetId.Should().Be(unsubscribeAck.PacketId);
        }