Exemplo n.º 1
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")).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 productInfoStore = new ProductInfoStore(store, edgeProductInfo);
            var modelIdStore     = Mock.Of <IModelIdStore>();

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

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

            // Assert
            string productInfo = await productInfoStore.GetProductInfo(identity.Id);

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

            Assert.Equal(clientVersion, productInfo);
            Assert.Equal($"{clientVersion} {edgeProductInfo}", deviceEdgeProductInfo);
        }
Exemplo n.º 2
0
        public async Task EmptyProductInfoTest()
        {
            // Arrange
            string edgeProductInfo = "IoTEdge 1.0.7";
            var    storeProvider   = new StoreProvider(new InMemoryDbStoreProvider());
            IEntityStore <string, string> store = storeProvider.GetEntityStore <string, string>("productInfo");
            var productInfoStore = new ProductInfoStore(store, edgeProductInfo);

            // Act
            await productInfoStore.SetProductInfo("d1", string.Empty);

            string productInfoValue = await productInfoStore.GetProductInfo("d1");

            // Assert
            Assert.Equal(string.Empty, productInfoValue);
        }
        public async Task SmokeTest()
        {
            // Arrange
            string edgeProductInfo = "IoTEdge 1.0.7";
            var    storeProvider   = new StoreProvider(new InMemoryDbStoreProvider());
            IEntityStore <string, string> store = storeProvider.GetEntityStore <string, string>("productInfo");
            var productInfoStore = new ProductInfoStore(store, edgeProductInfo);

            var deviceProductInfos = new Dictionary <string, string>
            {
                ["d1"]    = Guid.NewGuid().ToString(),
                ["d2"]    = Guid.NewGuid().ToString(),
                ["d3"]    = Guid.NewGuid().ToString(),
                ["d3/m1"] = Guid.NewGuid().ToString(),
                ["d3/m2"] = Guid.NewGuid().ToString()
            };

            // Act
            foreach (KeyValuePair <string, string> kvp in deviceProductInfos)
            {
                await productInfoStore.SetProductInfo(kvp.Key, kvp.Value);
            }

            var receivedDeviceInfos     = new Dictionary <string, string>();
            var receivedEdgeDeviceInfos = new Dictionary <string, string>();

            foreach (string id in deviceProductInfos.Keys)
            {
                string productInfo = await productInfoStore.GetProductInfo(id);

                string deviceEdgeProductInfo = await productInfoStore.GetEdgeProductInfo(id);

                receivedDeviceInfos.Add(id, productInfo);
                receivedEdgeDeviceInfos.Add(id, deviceEdgeProductInfo);
            }

            // Assert
            Assert.Equal(deviceProductInfos.Count, receivedDeviceInfos.Count);
            Assert.Equal(deviceProductInfos.Count, receivedEdgeDeviceInfos.Count);

            foreach (KeyValuePair <string, string> kvp in deviceProductInfos)
            {
                Assert.Equal(kvp.Value, receivedDeviceInfos[kvp.Key]);
                Assert.Equal($"{kvp.Value} {edgeProductInfo}", receivedEdgeDeviceInfos[kvp.Key]);
            }
        }