コード例 #1
0
        public async Task EncodesPropertiesInTopic()
        {
            var capture            = new SendCapture();
            var connector          = GetConnector(capture);
            var connectionRegistry = GetConnectionRegistry();
            var identityProvider   = new IdentityProvider("hub");
            var identity           = new ModuleIdentity("hub", "device_id", "module_id");
            var message            = new EdgeMessage
                                     .Builder(new byte[] { 0x01, 0x02, 0x03 })
                                     .SetProperties(new Dictionary <string, string>()
            {
                ["prop1"] = "value1", ["prop2"] = "value2"
            })
                                     .SetSystemProperties(new Dictionary <string, string>()
            {
                ["userId"] = "userid", ["cid"] = "corrid", [SystemProperties.LockToken] = "12345"
            })
                                     .Build();

            var sut = new ModuleToModuleMessageHandler(connectionRegistry, identityProvider);

            sut.SetConnector(connector);

            await sut.SendModuleToModuleMessageAsync(message, "some_input", identity, true);

            Assert.StartsWith("$edgehub/device_id/module_id/inputs/some_input/", capture.Topic);
            Assert.Contains("prop1=value1", capture.Topic);
            Assert.Contains("prop2=value2", capture.Topic);
            Assert.Contains("%24.uid=userid", capture.Topic);
            Assert.Contains("%24.cid=corrid", capture.Topic);
        }
コード例 #2
0
        public async Task EncodesPropertiesInTopic()
        {
            var capture            = new SendCapture();
            var connector          = GetConnector(capture);
            var connectionRegistry = GetConnectionRegistry();
            var identity           = new DeviceIdentity("hub", "device_id");
            var message            = new EdgeMessage
                                     .Builder(new byte[] { 0x01, 0x02, 0x03 })
                                     .SetProperties(new Dictionary <string, string>()
            {
                ["prop1"] = "value1", ["prop2"] = "value2"
            })
                                     .SetSystemProperties(new Dictionary <string, string>()
            {
                ["userId"] = "userid", ["cid"] = "corrid"
            })
                                     .Build();

            var sut = new Cloud2DeviceMessageHandler(connectionRegistry);

            sut.SetConnector(connector);

            await sut.SendC2DMessageAsync(message, identity);

            Assert.StartsWith("$edgehub/device_id/messages/c2d/post/", capture.Topic);
            Assert.Contains("prop1=value1", capture.Topic);
            Assert.Contains("prop2=value2", capture.Topic);
            Assert.Contains("%24.uid=userid", capture.Topic);
            Assert.Contains("%24.cid=corrid", capture.Topic);
        }
コード例 #3
0
        protected static Mock <IMqttBrokerConnector> GetConnector(SendCapture sendCapture = null)
        {
            var connector = new Mock <IMqttBrokerConnector>();

            connector
            .Setup(c => c.SendAsync(It.IsAny <string>(), It.IsAny <byte[]>()))
            .Returns((string topic, byte[] content) =>
            {
                sendCapture?.Capture(topic, content);
                return(Task.FromResult(true));
            });

            return(connector);
        }
コード例 #4
0
        protected static IMqttBrokerConnector GetConnector(SendCapture sendCapture = null)
        {
            var connector = Mock.Of <IMqttBrokerConnector>();

            Mock.Get(connector)
            .Setup(c => c.SendAsync(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <bool>()))
            .Returns((string topic, byte[] content, bool retain) =>
            {
                sendCapture?.Caputre(topic, content);
                return(Task.FromResult(true));
            });

            return(connector);
        }
コード例 #5
0
        public async Task SendsMessageDataAsPayload()
        {
            var capture   = new SendCapture();
            var connector = GetConnector(capture);

            var(connectionRegistry, identityProvider) = GetHandlerDependencies();
            var identity = new DeviceIdentity("hub", "device_id");
            var method   = new DirectMethodRequest("12345", "method", new byte[] { 1, 2, 3 }, TimeSpan.FromSeconds(5));

            var sut = new DirectMethodHandler(connectionRegistry, identityProvider);

            sut.SetConnector(connector);

            await sut.CallDirectMethodAsync(method, identity, true);

            Assert.Equal(new byte[] { 1, 2, 3 }, capture.Content);
        }
コード例 #6
0
        public async Task EncodesDeviceNameInTopic()
        {
            var capture   = new SendCapture();
            var connector = GetConnector(capture);

            var(connectionRegistry, identityProvider) = GetHandlerDependencies();
            var identity = new DeviceIdentity("hub", "device_id");
            var method   = new DirectMethodRequest("12345", "method", new byte[] { 1, 2, 3 }, TimeSpan.FromSeconds(5));

            var sut = new DirectMethodHandler(connectionRegistry, identityProvider);

            sut.SetConnector(connector);

            await sut.CallDirectMethodAsync(method, identity, true);

            Assert.Equal("$edgehub/device_id/methods/post/method/?$rid=" + method.CorrelationId, capture.Topic);
        }
コード例 #7
0
        public async Task EncodesDeviceNameInTopic()
        {
            var capture            = new SendCapture();
            var connector          = GetConnector(capture);
            var connectionRegistry = GetConnectionRegistry();
            var identity           = new DeviceIdentity("hub", "device_id");
            var message            = new EdgeMessage
                                     .Builder(new byte[] { 0x01, 0x02, 0x03 })
                                     .Build();

            var sut = new Cloud2DeviceMessageHandler(connectionRegistry);

            sut.SetConnector(connector);

            await sut.SendC2DMessageAsync(message, identity);

            Assert.Equal("$edgehub/device_id/messages/c2d/post/", capture.Topic);
        }
コード例 #8
0
        public async Task SendsMessageDataAsPayload()
        {
            var capture            = new SendCapture();
            var connector          = GetConnector(capture);
            var connectionRegistry = GetConnectionRegistry();
            var identity           = new ModuleIdentity("hub", "device_id", "module_id");
            var message            = new EdgeMessage
                                     .Builder(new byte[] { 0x01, 0x02, 0x03 })
                                     .Build();

            var sut = new ModuleToModuleMessageHandler(connectionRegistry);

            sut.SetConnector(connector);

            await sut.SendModuleToModuleMessageAsync(message, "some_input", identity);

            Assert.Equal(new byte[] { 0x01, 0x02, 0x03 }, capture.Content);
        }
コード例 #9
0
        public async Task EncodesModuleNameInTopic()
        {
            var capture            = new SendCapture();
            var connector          = GetConnector(capture);
            var connectionRegistry = GetConnectionRegistry();
            var identity           = new ModuleIdentity("hub", "device_id", "module_id");
            var message            = new EdgeMessage
                                     .Builder(new byte[] { 0x01, 0x02, 0x03 })
                                     .Build();

            var sut = new ModuleToModuleMessageHandler(connectionRegistry);

            sut.SetConnector(connector);

            await sut.SendModuleToModuleMessageAsync(message, "some_input", identity);

            Assert.Equal("$edgehub/device_id/module_id/inputs/some_input/", capture.Topic);
        }
コード例 #10
0
        protected static Mock <IMqttBrokerConnector> GetConnector(SendCapture sendCapture = null, TaskCompletionSource <bool> connectionState = null)
        {
            var connector = new Mock <IMqttBrokerConnector>();

            connector
            .Setup(c => c.SendAsync(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <bool>()))
            .Returns((string topic, byte[] content, bool retain) =>
            {
                sendCapture?.Capture(topic, content);
                return(Task.FromResult(true));
            });

            if (connectionState != null)
            {
                connector
                .SetupGet(c => c.EnsureConnected)
                .Returns(connectionState.Task);
            }

            return(connector);
        }
コード例 #11
0
        public void PublishAddIdentitiesTest()
        {
            // Arrange
            var capture                    = new SendCapture();
            var connectionState            = new TaskCompletionSource <bool>();
            var connector                  = GetConnector(capture, connectionState);
            var deviceScopeIdentitiesCache = new Mock <IDeviceScopeIdentitiesCache>();
            var sut = new ScopeIdentitiesHandler(Task.FromResult(deviceScopeIdentitiesCache.Object));

            sut.SetConnector(connector.Object);
            var serviceIdentity  = new ServiceIdentity("d1", "genId", new List <string>(), new ServiceAuthentication(new SymmetricKeyAuthentication("primKey", "secKey")), ServiceIdentityStatus.Enabled);
            var serviceIdentity2 = new ServiceIdentity("d2", "genId", new List <string>(), new ServiceAuthentication(new SymmetricKeyAuthentication("primKey", "secKey")), ServiceIdentityStatus.Enabled);
            var serviceIdentity3 = new ServiceIdentity("d3", "genId", new List <string>(), new ServiceAuthentication(new SymmetricKeyAuthentication("primKey", "secKey")), ServiceIdentityStatus.Enabled);

            deviceScopeIdentitiesCache.Setup(d => d.GetAuthChain(It.IsAny <string>())).ReturnsAsync(Option.Some("testAuthChain"));
            deviceScopeIdentitiesCache.Setup(d => d.GetAllIds()).ReturnsAsync(new List <string>()
            {
                serviceIdentity.Id, serviceIdentity2.Id, serviceIdentity3.Id
            });
            BrokerServiceIdentity identity  = new BrokerServiceIdentity("d1", Option.Some("testAuthChain"));
            BrokerServiceIdentity identity2 = new BrokerServiceIdentity("d2", Option.Some("testAuthChain"));
            BrokerServiceIdentity identity3 = new BrokerServiceIdentity("d3", Option.Some("testAuthChain"));

            connectionState.SetResult(true);

            // Act
            deviceScopeIdentitiesCache.Raise(d => d.ServiceIdentitiesUpdated += null, null, new List <string> {
                serviceIdentity.Id, serviceIdentity2.Id, serviceIdentity3.Id
            });

            // Assert
            capture.WhenCaptured().Wait();

            Assert.Equal(Topic, capture.Topic);
            Assert.Equal(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new List <BrokerServiceIdentity>()
            {
                identity, identity2, identity3
            })), capture.Content);
        }
コード例 #12
0
        public async Task SendsMessageDataAsPayload()
        {
            var capture            = new SendCapture();
            var connector          = GetConnector(capture);
            var connectionRegistry = GetConnectionRegistry();
            var identityProvider   = new IdentityProvider("hub");
            var identity           = new ModuleIdentity("hub", "device_id", "module_id");
            var message            = new EdgeMessage
                                     .Builder(new byte[] { 0x01, 0x02, 0x03 })
                                     .SetSystemProperties(new Dictionary <string, string>()
            {
                [SystemProperties.LockToken] = "12345"
            })
                                     .Build();

            using var sut = new ModuleToModuleMessageHandler(connectionRegistry, identityProvider, GetAckTimeout());
            sut.SetConnector(connector);

            await sut.SendModuleToModuleMessageAsync(message, "some_input", identity, true);

            Assert.Equal(new byte[] { 0x01, 0x02, 0x03 }, capture.Content);
        }
コード例 #13
0
        public async Task SendsMessageDataAsPayload()
        {
            var capture            = new SendCapture();
            var connector          = GetConnector(capture);
            var connectionRegistry = GetConnectionRegistry();
            var identity           = new DeviceIdentity("hub", "device_id");
            var message            = new EdgeMessage
                                     .Builder(new byte[] { 0x01, 0x02, 0x03 })
                                     .SetSystemProperties(new Dictionary <string, string>()
            {
                [SystemProperties.LockToken] = "12345"
            })
                                     .Build();

            var sut = new Cloud2DeviceMessageHandler(connectionRegistry);

            sut.SetConnector(connector);

            await sut.SendC2DMessageAsync(message, identity, true);

            Assert.Equal(new byte[] { 0x01, 0x02, 0x03 }, capture.Content);
        }
コード例 #14
0
ファイル: TwinHandlerTest.cs プロジェクト: chzawist/iotedge
        public async Task DesiredUpdateEncodesVersionAndIdentityInTopic()
        {
            var sendCapture = new SendCapture();

            var(connectionRegistry, identityProvider) = GetHandlerDependencies();
            var connector = GetConnector(sendCapture);

            var identity = new DeviceIdentity("hub", "device_id");
            var twin     = new EdgeMessage.Builder(new byte[] { 1, 2, 3 })
                           .SetSystemProperties(new Dictionary <string, string>()
            {
                [SystemProperties.Version] = "123"
            })
                           .Build();

            var sut = new TwinHandler(connectionRegistry, identityProvider);

            sut.SetConnector(connector);

            await sut.SendDesiredPropertiesUpdate(twin, identity, true);

            Assert.Equal("$edgehub/device_id/twin/desired/?$version=123", sendCapture.Topic);
        }
コード例 #15
0
        public async Task EncodesModuleNameInTopic()
        {
            var capture            = new SendCapture();
            var connector          = GetConnector(capture);
            var connectionRegistry = GetConnectionRegistry();
            var identityProvider   = new IdentityProvider("hub");
            var identity           = new ModuleIdentity("hub", "device_id", "module_id");
            var message            = new EdgeMessage
                                     .Builder(new byte[] { 0x01, 0x02, 0x03 })
                                     .SetSystemProperties(new Dictionary <string, string>()
            {
                [SystemProperties.LockToken] = "12345"
            })
                                     .Build();

            var sut = new ModuleToModuleMessageHandler(connectionRegistry, identityProvider);

            sut.SetConnector(connector);

            await sut.SendModuleToModuleMessageAsync(message, "some_input", identity, true);

            Assert.Equal("$edgehub/device_id/module_id/inputs/some_input/", capture.Topic);
        }
コード例 #16
0
ファイル: TwinHandlerTest.cs プロジェクト: chzawist/iotedge
        public async Task TwinUpdateSendsMessageBody()
        {
            var sendCapture = new SendCapture();

            var(connectionRegistry, identityProvider) = GetHandlerDependencies();
            var connector = GetConnector(sendCapture);

            var identity = new DeviceIdentity("hub", "device_id");
            var twin     = new EdgeMessage.Builder(new byte[] { 1, 2, 3 })
                           .SetSystemProperties(new Dictionary <string, string>()
            {
                [SystemProperties.StatusCode]    = "200",
                [SystemProperties.CorrelationId] = "123"
            })
                           .Build();

            var sut = new TwinHandler(connectionRegistry, identityProvider);

            sut.SetConnector(connector);

            await sut.SendTwinUpdate(twin, identity, true);

            Assert.Equal(new byte[] { 1, 2, 3 }, sendCapture.Content);
        }
コード例 #17
0
ファイル: TwinHandlerTest.cs プロジェクト: vrvlive/iotedge
        public async Task TwinUpdateEncodesPropertiesAndIdentityInTopic()
        {
            var sendCapture = new SendCapture();

            var(connectionRegistry, identityProvider) = GetHandlerDependencies();
            var connector = GetConnector(sendCapture);

            var identity = new DeviceIdentity("hub", "device_id");
            var twin     = new EdgeMessage.Builder(new byte[] { 1, 2, 3 })
                           .SetSystemProperties(new Dictionary <string, string>()
            {
                [SystemProperties.StatusCode]    = "200",
                [SystemProperties.CorrelationId] = "123"
            })
                           .Build();

            var sut = new TwinHandler(connectionRegistry, identityProvider);

            sut.SetConnector(connector);

            await sut.SendTwinUpdate(twin, identity);

            Assert.Equal("$edgehub/device_id/twin/res/200/?$rid=123", sendCapture.Topic);
        }
コード例 #18
0
        public void UpdateIdentitiesBeforeConnectTest()
        {
            // Arrange
            var capture   = new SendCapture();
            var connector = GetConnector(capture);
            var deviceScopeIdentitiesCache = new Mock <IDeviceScopeIdentitiesCache>();
            var serviceIdentity            = new ServiceIdentity("d1", "genId", new List <string>(), new ServiceAuthentication(new SymmetricKeyAuthentication("primKey", "secKey")), ServiceIdentityStatus.Enabled);
            var serviceIdentity2           = new ServiceIdentity("d2", "genId", new List <string>(), new ServiceAuthentication(new SymmetricKeyAuthentication("primKey", "secKey")), ServiceIdentityStatus.Enabled);
            var serviceIdentity3           = new ServiceIdentity("d3", "genId", new List <string>(), new ServiceAuthentication(new SymmetricKeyAuthentication("primKey", "secKey")), ServiceIdentityStatus.Enabled);

            BrokerServiceIdentity identity  = new BrokerServiceIdentity("d1", Option.Some("testAuthChain"));
            BrokerServiceIdentity identity2 = new BrokerServiceIdentity("d2", Option.Some("testAuthChain2"));
            BrokerServiceIdentity identity3 = new BrokerServiceIdentity("d3", Option.Some("testAuthChain3"));

            deviceScopeIdentitiesCache.Setup(d => d.GetAuthChain("d1")).ReturnsAsync(Option.Some("testAuthChain"));
            deviceScopeIdentitiesCache.Setup(d => d.GetAuthChain("d2")).ReturnsAsync(Option.Some("testAuthChain2"));
            deviceScopeIdentitiesCache.Setup(d => d.GetAuthChain("d3")).ReturnsAsync(Option.Some("testAuthChain3"));
            deviceScopeIdentitiesCache.Setup(d => d.GetAllIds()).ReturnsAsync(new List <string>()
            {
                serviceIdentity.Id, serviceIdentity2.Id, serviceIdentity3.Id
            });

            var sut = new ScopeIdentitiesHandler(Task.FromResult(deviceScopeIdentitiesCache.Object));

            sut.SetConnector(connector.Object);

            // Act
            deviceScopeIdentitiesCache.Raise(d => d.ServiceIdentitiesUpdated += null, null, new List <string>()
            {
                serviceIdentity.Id, serviceIdentity2.Id, serviceIdentity3.Id
            });
            deviceScopeIdentitiesCache.Raise(d => d.ServiceIdentitiesUpdated += null, null, new List <string>()
            {
                serviceIdentity.Id, serviceIdentity2.Id
            });

            // Assert
            Assert.Null(capture.Topic);
            Assert.Null(capture.Content);

            // Act
            connector.Raise(c => c.OnConnected += null, null, null);

            // Assert
            Assert.Equal(Topic, capture.Topic);
            IList <BrokerServiceIdentity> brokerServiceIdentities = JsonConvert.DeserializeObject <IList <BrokerServiceIdentity> >(Encoding.UTF8.GetString(capture.Content));

            Assert.Equal(3, brokerServiceIdentities.Count);
            Assert.Contains(identity, brokerServiceIdentities);
            Assert.Contains(identity2, brokerServiceIdentities);
            Assert.Contains(identity3, brokerServiceIdentities);

            // Act
            deviceScopeIdentitiesCache.Raise(d => d.ServiceIdentitiesUpdated += null, null, new List <string>()
            {
                serviceIdentity.Id, serviceIdentity2.Id
            });

            // Assert
            Assert.Equal(Topic, capture.Topic);
            brokerServiceIdentities = JsonConvert.DeserializeObject <IList <BrokerServiceIdentity> >(Encoding.UTF8.GetString(capture.Content));
            Assert.Equal(2, brokerServiceIdentities.Count);
            Assert.Contains(identity, brokerServiceIdentities);
            Assert.Contains(identity2, brokerServiceIdentities);
        }