public async Task TestAddRemoveDeviceConnectionTest() { string deviceId = "id1"; var deviceProxyMock1 = new Mock <IDeviceProxy>(); deviceProxyMock1.SetupGet(dp => dp.IsActive).Returns(true); deviceProxyMock1.Setup(dp => dp.CloseAsync(It.IsAny <Exception>())) .Callback(() => deviceProxyMock1.SetupGet(dp => dp.IsActive).Returns(false)) .Returns(Task.FromResult(true)); var deviceCredentials = new SharedKeyCredentials(new DeviceIdentity("iotHub", deviceId), "dummyConnStr", "abc"); var edgeHub = new Mock <IEdgeHub>(); IClient client = GetDeviceClient(); var messageConverterProvider = Mock.Of <IMessageConverterProvider>(); var deviceClientProvider = new Mock <IClientProvider>(); deviceClientProvider.Setup(d => d.Create(It.IsAny <IIdentity>(), It.IsAny <string>(), It.IsAny <Client.ITransportSettings[]>())) .Returns(client); var cloudConnectionProvider = new CloudConnectionProvider(messageConverterProvider, 1, deviceClientProvider.Object, Option.None <UpstreamProtocol>()); IConnectionManager connectionManager = new ConnectionManager(cloudConnectionProvider); Try <ICloudProxy> cloudProxyTry = await connectionManager.CreateCloudConnectionAsync(deviceCredentials); Assert.True(cloudProxyTry.Success); var deviceListener = new DeviceMessageHandler(deviceCredentials.Identity, edgeHub.Object, connectionManager); Option <ICloudProxy> cloudProxy = connectionManager.GetCloudConnection(deviceId); Assert.True(cloudProxy.HasValue); Assert.True(cloudProxy.OrDefault().IsActive); deviceListener.BindDeviceProxy(deviceProxyMock1.Object); Option <IDeviceProxy> deviceProxy = connectionManager.GetDeviceConnection(deviceId); Assert.True(deviceProxy.HasValue); Assert.True(deviceProxy.OrDefault().IsActive); Assert.True(deviceProxyMock1.Object.IsActive); cloudProxy = connectionManager.GetCloudConnection(deviceId); Assert.True(cloudProxy.HasValue); Assert.True(cloudProxy.OrDefault().IsActive); await deviceListener.CloseAsync(); deviceProxy = connectionManager.GetDeviceConnection(deviceId); Assert.False(deviceProxy.HasValue); Assert.False(deviceProxyMock1.Object.IsActive); cloudProxy = connectionManager.GetCloudConnection(deviceId); Assert.False(cloudProxy.HasValue); Assert.False(client.IsActive); }
public async Task TestAddRemoveDeviceConnectionTest() { string deviceId = "id1"; var deviceProxyMock1 = new Mock <IDeviceProxy>(); deviceProxyMock1.SetupGet(dp => dp.IsActive).Returns(true); deviceProxyMock1.Setup(dp => dp.CloseAsync(It.IsAny <Exception>())) .Callback(() => deviceProxyMock1.SetupGet(dp => dp.IsActive).Returns(false)) .Returns(Task.FromResult(true)); var deviceCredentials = new TokenCredentials(new DeviceIdentity("iotHub", deviceId), "token", "abc", false); var edgeHub = new Mock <IEdgeHub>(); IClient client = GetDeviceClient(); var messageConverterProvider = Mock.Of <IMessageConverterProvider>(); var deviceClientProvider = new Mock <IClientProvider>(); deviceClientProvider.Setup(d => d.Create(It.IsAny <IIdentity>(), It.IsAny <ITokenProvider>(), It.IsAny <ITransportSettings[]>())) .Returns(client); var productInfoStore = Mock.Of <IProductInfoStore>(); var credentialsManager = Mock.Of <ICredentialsCache>(); var edgeHubIdentity = Mock.Of <IIdentity>(i => i.Id == "edgeDevice/$edgeHub"); var cloudConnectionProvider = new CloudConnectionProvider( messageConverterProvider, 1, deviceClientProvider.Object, Option.None <UpstreamProtocol>(), Mock.Of <ITokenProvider>(), Mock.Of <IDeviceScopeIdentitiesCache>(), credentialsManager, edgeHubIdentity, TimeSpan.FromMinutes(60), true, TimeSpan.FromSeconds(20), Option.None <IWebProxy>(), productInfoStore); cloudConnectionProvider.BindEdgeHub(edgeHub.Object); IConnectionManager connectionManager = new ConnectionManager(cloudConnectionProvider, credentialsManager, GetIdentityProvider()); Try <ICloudProxy> cloudProxyTry = await connectionManager.CreateCloudConnectionAsync(deviceCredentials); Assert.True(cloudProxyTry.Success); var deviceListener = new DeviceMessageHandler(deviceCredentials.Identity, edgeHub.Object, connectionManager); Option <ICloudProxy> cloudProxy = await connectionManager.GetCloudConnection(deviceId); Assert.True(cloudProxy.HasValue); Assert.True(cloudProxy.OrDefault().IsActive); deviceListener.BindDeviceProxy(deviceProxyMock1.Object); Option <IDeviceProxy> deviceProxy = connectionManager.GetDeviceConnection(deviceId); Assert.True(deviceProxy.HasValue); Assert.True(deviceProxy.OrDefault().IsActive); Assert.True(deviceProxyMock1.Object.IsActive); cloudProxy = await connectionManager.GetCloudConnection(deviceId); Assert.True(cloudProxy.HasValue); Assert.True(cloudProxy.OrDefault().IsActive); await deviceListener.CloseAsync(); deviceProxy = connectionManager.GetDeviceConnection(deviceId); Assert.False(deviceProxy.HasValue); Assert.False(deviceProxyMock1.Object.IsActive); cloudProxy = await connectionManager.GetCloudConnection(deviceId); Assert.True(cloudProxy.HasValue); Assert.True(client.IsActive); }