Exemplo n.º 1
0
        public async Task SendLargeMessageThrowsTest()
        {
            // Arrange
            bool disposeMessageCalled = true;
            var  identity             = Mock.Of <IIdentity>(i => i.Id == "d1");

            var deviceListener = Mock.Of <IDeviceListener>();

            Mock.Get(deviceListener).Setup(d => d.ProcessDeviceMessageBatchAsync(It.IsAny <IEnumerable <IMessage> >()))
            .Returns(Task.CompletedTask);

            var connectionHandler = Mock.Of <IConnectionHandler>(c => c.GetDeviceListener() == Task.FromResult(deviceListener));
            var amqpAuthenticator = new Mock <IAmqpAuthenticator>();

            amqpAuthenticator.Setup(c => c.AuthenticateAsync("d1")).ReturnsAsync(true);
            Mock <ICbsNode> cbsNodeMock    = amqpAuthenticator.As <ICbsNode>();
            ICbsNode        cbsNode        = cbsNodeMock.Object;
            var             amqpConnection = Mock.Of <IAmqpConnection>(
                c =>
                c.FindExtension <IConnectionHandler>() == connectionHandler &&
                c.FindExtension <ICbsNode>() == cbsNode);
            var amqpSession = Mock.Of <IAmqpSession>(s => s.Connection == amqpConnection);
            var amqpLink    = Mock.Of <IReceivingAmqpLink>(l => l.Session == amqpSession && l.IsReceiver && l.Settings == new AmqpLinkSettings() && l.State == AmqpObjectState.Opened);

            Action <AmqpMessage> onMessageCallback = null;

            Mock.Get(amqpLink).Setup(l => l.RegisterMessageListener(It.IsAny <Action <AmqpMessage> >())).Callback <Action <AmqpMessage> >(a => onMessageCallback = a);
            Mock.Get(amqpLink).SetupGet(l => l.Settings).Returns(new AmqpLinkSettings());
            Mock.Get(amqpLink).Setup(l => l.SafeAddClosed(It.IsAny <EventHandler>()));
            Mock.Get(amqpLink).Setup(l => l.DisposeMessage(It.IsAny <AmqpMessage>(), It.IsAny <Outcome>(), It.IsAny <bool>(), It.IsAny <bool>()))
            .Callback(() => disposeMessageCalled = true);

            var productInfoStore = Mock.Of <IProductInfoStore>();
            var modelIdStore     = Mock.Of <IModelIdStore>();
            var requestUri       = new Uri("amqps://foo.bar/devices/d1/messages/events");
            var boundVariables   = new Dictionary <string, string> {
                { "deviceid", "d1" }
            };
            var messageConverter = new AmqpMessageConverter();

            using (AmqpMessage amqpMessage = AmqpMessage.Create(new MemoryStream(new byte[800000]), false))
            {
                amqpMessage.ApplicationProperties.Map["LargeProp"] = new int[600000];
                ILinkHandler linkHandler = new EventsLinkHandler(identity, amqpLink, requestUri, boundVariables, connectionHandler, messageConverter, productInfoStore, modelIdStore);

                // Act
                await linkHandler.OpenAsync(TimeSpan.FromSeconds(30));

                // Assert
                Assert.NotNull(onMessageCallback);

                // Act
                onMessageCallback.Invoke(amqpMessage);

                // Assert
                await WaitAndAssert(() => disposeMessageCalled, TimeSpan.FromSeconds(5));
            }
        }
Exemplo n.º 2
0
        public async Task SetModelIdTest()
        {
            // Arrange
            string modelId       = "com:microsoft:model-id:testModelId";
            string clientVersion = $"TestClientVersion{Guid.NewGuid().ToString()}";
            var    identity      = Mock.Of <IDeviceIdentity>(i => i.Id == "d1" && i.DeviceId == "d1");

            var deviceListener = Mock.Of <IDeviceListener>();

            var connectionHandler = Mock.Of <IConnectionHandler>(c => c.GetDeviceListener() == Task.FromResult(deviceListener));
            var amqpAuthenticator = new Mock <IAmqpAuthenticator>();

            amqpAuthenticator.Setup(c => c.AuthenticateAsync("d1")).ReturnsAsync(true);
            Mock <ICbsNode> cbsNodeMock    = amqpAuthenticator.As <ICbsNode>();
            ICbsNode        cbsNode        = cbsNodeMock.Object;
            var             amqpConnection = Mock.Of <IAmqpConnection>(
                c =>
                c.FindExtension <IConnectionHandler>() == connectionHandler &&
                c.FindExtension <ICbsNode>() == cbsNode);
            var amqpSession = Mock.Of <IAmqpSession>(s => s.Connection == amqpConnection);
            var amqpLink    = Mock.Of <IReceivingAmqpLink>(l => l.Session == amqpSession && l.IsReceiver && l.Settings == new AmqpLinkSettings() && l.State == AmqpObjectState.Opened);

            var linkSettings = new AmqpLinkSettings
            {
                Properties = new Fields()
                {
                    { IotHubAmqpProperty.ClientVersion, clientVersion },
                    { IotHubAmqpProperty.ModelId, modelId }
                }
            };

            Mock.Get(amqpLink).SetupGet(l => l.Settings).Returns(linkSettings);

            var requestUri     = new Uri("amqps://foo.bar/devices/d1/messages/events");
            var boundVariables = new Dictionary <string, string> {
                { "deviceid", "d1" }
            };
            var messageConverter = new AmqpMessageConverter();

            var storeProvider = new StoreProvider(new InMemoryDbStoreProvider());
            IEntityStore <string, string> store = storeProvider.GetEntityStore <string, string>("modelId");
            var productInfoStore = Mock.Of <IProductInfoStore>();
            var modelIdStore     = new ModelIdStore(store);

            ILinkHandler linkHandler = new EventsLinkHandler(identity, amqpLink, requestUri, boundVariables, connectionHandler, messageConverter, productInfoStore, modelIdStore);

            // Act
            await linkHandler.OpenAsync(TimeSpan.FromSeconds(1));

            // Assert
            Option <string> modelIdFromStore = await modelIdStore.GetModelId(identity.Id);

            string deviceEdgeProductInfo = await productInfoStore.GetEdgeProductInfo(identity.Id);

            Assert.True(modelIdFromStore.HasValue);
            modelIdFromStore.ForEach(m => Assert.Equal(modelId, m));
        }
Exemplo n.º 3
0
        public async Task SetProductInfoTest()
        {
            // Arrange
            string edgeProductInfo = "IoTEdge 1.0.7";
            string clientVersion   = $"TestClientVersion{Guid.NewGuid().ToString()}";
            var    identity        = Mock.Of <IDeviceIdentity>(i => i.Id == "d1" && i.DeviceId == "d1");

            var deviceListener = Mock.Of <IDeviceListener>();

            var connectionHandler = Mock.Of <IConnectionHandler>(c => c.GetDeviceListener() == Task.FromResult(deviceListener));
            var amqpAuthenticator = new Mock <IAmqpAuthenticator>();

            amqpAuthenticator.Setup(c => c.AuthenticateAsync("d1", Option.None <string>())).ReturnsAsync(true);
            Mock <ICbsNode> cbsNodeMock    = amqpAuthenticator.As <ICbsNode>();
            ICbsNode        cbsNode        = cbsNodeMock.Object;
            var             amqpConnection = Mock.Of <IAmqpConnection>(
                c =>
                c.FindExtension <IConnectionHandler>() == connectionHandler &&
                c.FindExtension <ICbsNode>() == cbsNode);
            var amqpSession = Mock.Of <IAmqpSession>(s => s.Connection == amqpConnection);
            var amqpLink    = Mock.Of <IReceivingAmqpLink>(l => l.Session == amqpSession && l.IsReceiver && l.Settings == new AmqpLinkSettings() && l.State == AmqpObjectState.Opened);

            var linkSettings = new AmqpLinkSettings
            {
                Properties = new Fields()
                {
                    { IotHubAmqpProperty.ClientVersion, clientVersion }
                }
            };

            Mock.Get(amqpLink).SetupGet(l => l.Settings).Returns(linkSettings);

            var requestUri     = new Uri("amqps://foo.bar/devices/d1/messages/events");
            var boundVariables = new Dictionary <string, string> {
                { "deviceid", "d1" }
            };
            var messageConverter = new AmqpMessageConverter();

            var storeProvider = new StoreProvider(new InMemoryDbStoreProvider());
            IEntityStore <string, string> store = storeProvider.GetEntityStore <string, string>("productInfo");
            var metadataStore = new MetadataStore(store, edgeProductInfo);

            ILinkHandler linkHandler = new EventsLinkHandler(identity, amqpLink, requestUri, boundVariables, connectionHandler, messageConverter, metadataStore);

            // Act
            await linkHandler.OpenAsync(TimeSpan.FromSeconds(1));

            // Assert
            ConnectionMetadata connectionMetadata = await metadataStore.GetMetadata(identity.Id);

            string productInfo           = connectionMetadata.ProductInfo;
            string deviceEdgeProductInfo = connectionMetadata.EdgeProductInfo;

            Assert.Equal(clientVersion, productInfo);
            Assert.Equal($"{clientVersion} {edgeProductInfo}", deviceEdgeProductInfo);
        }
Exemplo n.º 4
0
        public async Task SendMessageTest()
        {
            // Arrange
            var identity = Mock.Of <IDeviceIdentity>(i => i.Id == "d1" && i.DeviceId == "d1");

            IEnumerable <IMessage> receivedMessages = null;
            var deviceListener = Mock.Of <IDeviceListener>();

            Mock.Get(deviceListener).Setup(d => d.ProcessDeviceMessageBatchAsync(It.IsAny <IEnumerable <IMessage> >())).Callback <IEnumerable <IMessage> >(m => receivedMessages = m);

            var connectionHandler = Mock.Of <IConnectionHandler>(c => c.GetDeviceListener() == Task.FromResult(deviceListener));
            var amqpAuthenticator = new Mock <IAmqpAuthenticator>();

            amqpAuthenticator.Setup(c => c.AuthenticateAsync("d1", Option.None <string>())).ReturnsAsync(true);
            Mock <ICbsNode> cbsNodeMock    = amqpAuthenticator.As <ICbsNode>();
            ICbsNode        cbsNode        = cbsNodeMock.Object;
            var             amqpConnection = Mock.Of <IAmqpConnection>(
                c =>
                c.FindExtension <IConnectionHandler>() == connectionHandler &&
                c.FindExtension <ICbsNode>() == cbsNode);
            var amqpSession = Mock.Of <IAmqpSession>(s => s.Connection == amqpConnection);
            var amqpLink    = Mock.Of <IReceivingAmqpLink>(l => l.Session == amqpSession && l.IsReceiver && l.Settings == new AmqpLinkSettings() && l.State == AmqpObjectState.Opened);

            Action <AmqpMessage> onMessageCallback = null;

            Mock.Get(amqpLink).Setup(l => l.RegisterMessageListener(It.IsAny <Action <AmqpMessage> >())).Callback <Action <AmqpMessage> >(a => onMessageCallback = a);
            Mock.Get(amqpLink).SetupGet(l => l.Settings).Returns(new AmqpLinkSettings());
            Mock.Get(amqpLink).Setup(l => l.SafeAddClosed(It.IsAny <EventHandler>()));
            var metadataStore  = Mock.Of <IMetadataStore>();
            var requestUri     = new Uri("amqps://foo.bar/devices/d1/messages/events");
            var boundVariables = new Dictionary <string, string> {
                { "deviceid", "d1" }
            };
            var messageConverter = new AmqpMessageConverter();

            using (AmqpMessage amqpMessage = AmqpMessage.Create(new MemoryStream(new byte[] { 1, 2, 3, 4 }), false))
            {
                amqpMessage.ApplicationProperties.Map["Prop1"] = "Value1";
                amqpMessage.ApplicationProperties.Map["Prop2"] = "Value2";
                amqpMessage.Properties.ContentType             = "application/json";
                amqpMessage.Properties.ContentEncoding         = "utf-8";

                ILinkHandler linkHandler = new EventsLinkHandler(identity, amqpLink, requestUri, boundVariables, connectionHandler, messageConverter, metadataStore);

                // Act
                await linkHandler.OpenAsync(TimeSpan.FromSeconds(30));

                // Assert
                Assert.NotNull(onMessageCallback);

                // Act
                onMessageCallback.Invoke(amqpMessage);

                // Assert
                await WaitAndAssert(
                    () =>
                {
                    if (receivedMessages == null)
                    {
                        return(false);
                    }

                    IList <IMessage> receivedMessagesList = receivedMessages.ToList();
                    Assert.Equal(1, receivedMessagesList.Count);
                    Assert.Equal("Value1", receivedMessagesList[0].Properties["Prop1"]);
                    Assert.Equal("Value2", receivedMessagesList[0].Properties["Prop2"]);
                    Assert.Equal("utf-8", receivedMessagesList[0].SystemProperties[SystemProperties.ContentEncoding]);
                    Assert.Equal("application/json", receivedMessagesList[0].SystemProperties[SystemProperties.ContentType]);
                    Assert.Equal("d1", receivedMessagesList[0].SystemProperties[SystemProperties.ConnectionDeviceId]);
                    Assert.Equal(receivedMessagesList[0].Body, new byte[] { 1, 2, 3, 4 });
                    return(true);
                },
                    TimeSpan.FromSeconds(5));
            }
        }
Exemplo n.º 5
0
        public async Task SendMessageBatchTest()
        {
            // Arrange
            var identity = Mock.Of <IIdentity>(i => i.Id == "d1");

            IEnumerable <IMessage> receivedMessages = null;
            var deviceListener = Mock.Of <IDeviceListener>();

            Mock.Get(deviceListener).Setup(d => d.ProcessDeviceMessageBatchAsync(It.IsAny <IEnumerable <IMessage> >()))
            .Callback <IEnumerable <IMessage> >(m => receivedMessages = m)
            .Returns(Task.CompletedTask);

            var connectionHandler = Mock.Of <IConnectionHandler>(c => c.GetDeviceListener() == Task.FromResult(deviceListener));
            var amqpAuthenticator = new Mock <IAmqpAuthenticator>();

            amqpAuthenticator.Setup(c => c.AuthenticateAsync("d1", Option.None <string>())).ReturnsAsync(true);
            Mock <ICbsNode> cbsNodeMock    = amqpAuthenticator.As <ICbsNode>();
            ICbsNode        cbsNode        = cbsNodeMock.Object;
            var             amqpConnection = Mock.Of <IAmqpConnection>(
                c =>
                c.FindExtension <IConnectionHandler>() == connectionHandler &&
                c.FindExtension <ICbsNode>() == cbsNode);
            var amqpSession   = Mock.Of <IAmqpSession>(s => s.Connection == amqpConnection);
            var amqpLink      = Mock.Of <IReceivingAmqpLink>(l => l.Session == amqpSession && l.IsReceiver && l.Settings == new AmqpLinkSettings() && l.State == AmqpObjectState.Opened);
            var metadataStore = Mock.Of <IMetadataStore>();
            Action <AmqpMessage> onMessageCallback = null;

            Mock.Get(amqpLink).Setup(l => l.RegisterMessageListener(It.IsAny <Action <AmqpMessage> >())).Callback <Action <AmqpMessage> >(a => onMessageCallback = a);
            Mock.Get(amqpLink).SetupGet(l => l.Settings).Returns(new AmqpLinkSettings());
            Mock.Get(amqpLink).Setup(l => l.SafeAddClosed(It.IsAny <EventHandler>()));

            var requestUri     = new Uri("amqps://foo.bar/devices/d1/messages/events");
            var boundVariables = new Dictionary <string, string>
            {
                { "deviceid", "d1" }
            };
            var messageConverter = new AmqpMessageConverter();

            string content1 = "Message1 Contents ABC";
            string content2 = "Message2 Contents PQR";
            string content3 = "Message3 Contents XYZ";
            var    contents = new List <string>
            {
                content1,
                content2,
                content3
            };

            using (AmqpMessage amqpMessage = GetBatchedMessage(contents))
            {
                amqpMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat;
                ILinkHandler linkHandler = new EventsLinkHandler(identity, amqpLink, requestUri, boundVariables, connectionHandler, messageConverter, metadataStore);

                // Act
                await linkHandler.OpenAsync(TimeSpan.FromSeconds(30));

                // Assert
                Assert.NotNull(onMessageCallback);

                // Act
                onMessageCallback.Invoke(amqpMessage);

                // Assert
                await WaitAndAssert(
                    () =>
                {
                    if (receivedMessages == null)
                    {
                        return(false);
                    }

                    IList <IMessage> receivedMessagesList = receivedMessages.ToList();
                    Assert.Equal(contents.Count, receivedMessagesList.Count);

                    for (int i = 0; i < receivedMessagesList.Count; i++)
                    {
                        IMessage receivedMessage = receivedMessagesList[i];
                        string actualContents    = Encoding.UTF8.GetString(receivedMessage.Body);

                        Assert.Equal(contents[i], actualContents);
                        Assert.Equal($"{i}", receivedMessage.SystemProperties[SystemProperties.MessageId]);
                        Assert.Equal($"{i}", receivedMessage.Properties["MsgCnt"]);
                        Assert.Equal(contents[i], receivedMessage.Properties["MsgData"]);
                    }

                    return(true);
                },
                    TimeSpan.FromSeconds(5));
            }
        }
Exemplo n.º 6
0
        public async Task SendMessageBatchTest()
        {
            // Arrange
            var identity = Mock.Of <IIdentity>(i => i.Id == "d1");
            var amqpAuth = new AmqpAuthentication(true, Option.Some(Mock.Of <IClientCredentials>(c => c.Identity == identity)));

            IEnumerable <IMessage> receivedMessages = null;
            var deviceListener = Mock.Of <IDeviceListener>();

            Mock.Get(deviceListener).Setup(d => d.ProcessDeviceMessageBatchAsync(It.IsAny <IEnumerable <IMessage> >()))
            .Callback <IEnumerable <IMessage> >(m => receivedMessages = m)
            .Returns(Task.CompletedTask);

            var connectionHandler = Mock.Of <IConnectionHandler>(c => c.GetAmqpAuthentication() == Task.FromResult(amqpAuth) && c.GetDeviceListener() == Task.FromResult(deviceListener));
            var amqpConnection    = Mock.Of <IAmqpConnection>(c => c.FindExtension <IConnectionHandler>() == connectionHandler);
            var amqpSession       = Mock.Of <IAmqpSession>(s => s.Connection == amqpConnection);
            var amqpLink          = Mock.Of <IReceivingAmqpLink>(l => l.Session == amqpSession && l.IsReceiver && l.Settings == new AmqpLinkSettings() && l.State == AmqpObjectState.Opened);

            Action <AmqpMessage> onMessageCallback = null;

            Mock.Get(amqpLink).Setup(l => l.RegisterMessageListener(It.IsAny <Action <AmqpMessage> >())).Callback <Action <AmqpMessage> >(a => onMessageCallback = a);
            Mock.Get(amqpLink).SetupGet(l => l.Settings).Returns(new AmqpLinkSettings());
            Mock.Get(amqpLink).Setup(l => l.SafeAddClosed(It.IsAny <EventHandler>()));

            var requestUri     = new Uri("amqps://foo.bar/devices/d1/messages/events");
            var boundVariables = new Dictionary <string, string>
            {
                { "deviceid", "d1" }
            };
            var messageConverter = new AmqpMessageConverter();

            using (AmqpMessage amqpMessage = AmqpMessage.Create(
                       new[]
            {
                new Data
                {
                    Value = new ArraySegment <byte>(new byte[80000])
                },
                new Data
                {
                    Value = new ArraySegment <byte>(new byte[90000])
                },
                new Data
                {
                    Value = new ArraySegment <byte>(new byte[100000])
                }
            }))
            {
                amqpMessage.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat;
                ILinkHandler linkHandler = new EventsLinkHandler(amqpLink, requestUri, boundVariables, messageConverter);

                // Act
                await linkHandler.OpenAsync(TimeSpan.FromSeconds(30));

                // Assert
                Assert.NotNull(onMessageCallback);

                // Act
                onMessageCallback.Invoke(amqpMessage);

                // Assert
                await WaitAndAssert(
                    () =>
                {
                    if (receivedMessages == null)
                    {
                        return(false);
                    }
                    IList <IMessage> receivedMessagesList = receivedMessages.ToList();
                    Assert.Equal(3, receivedMessagesList.Count);
                    return(true);
                },
                    TimeSpan.FromSeconds(5));
            }
        }