public async Task ProcessRemoveDesiredPropertiesUpdateSubscription() { // Arrange var edgeHub = new Mock <IEdgeHub>(); edgeHub.Setup(e => e.RemoveSubscription("d1", DeviceSubscription.DesiredPropertyUpdates)) .Returns(Task.CompletedTask); var connMgr = Mock.Of <IConnectionManager>(); var identity = Mock.Of <IDeviceIdentity>(i => i.Id == "d1"); var deviceProxy = new Mock <IDeviceProxy>(); IMessage sentMessage = null; deviceProxy.Setup(d => d.SendTwinUpdate(It.IsAny <IMessage>())) .Callback <IMessage>(m => sentMessage = m) .Returns(Task.CompletedTask); var listener = new DeviceMessageHandler(identity, edgeHub.Object, connMgr, DefaultMessageAckTimeout, Option.None <string>()); listener.BindDeviceProxy(deviceProxy.Object); string correlationId = Guid.NewGuid().ToString(); // Act await listener.RemoveDesiredPropertyUpdatesSubscription(correlationId); // Assert Assert.NotNull(sentMessage); Assert.Equal(correlationId, sentMessage.SystemProperties[SystemProperties.CorrelationId]); Assert.Equal("200", sentMessage.SystemProperties[SystemProperties.StatusCode]); edgeHub.VerifyAll(); }
public async Task MessageCompletionMismatchedResponseTest() { var connMgr = new Mock <IConnectionManager>(); connMgr.Setup(c => c.AddDeviceConnection(It.IsAny <IIdentity>(), It.IsAny <IDeviceProxy>())); var identity = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.ModuleId == "module1" && m.Id == "device1/module1"); var cloudProxy = new Mock <ICloudProxy>(); var edgeHub = Mock.Of <IEdgeHub>(); var underlyingDeviceProxy = new Mock <IDeviceProxy>(); underlyingDeviceProxy.Setup(d => d.SendMessageAsync(It.IsAny <IMessage>(), It.IsAny <string>())) .Returns(Task.CompletedTask); connMgr.Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Task.FromResult(Option.Some(cloudProxy.Object))); var deviceMessageHandler = new DeviceMessageHandler(identity, edgeHub, connMgr.Object, DefaultMessageAckTimeout, Option.None <string>()); deviceMessageHandler.BindDeviceProxy(underlyingDeviceProxy.Object); IMessage message = new EdgeMessage.Builder(new byte[0]).Build(); Task sendMessageTask = deviceMessageHandler.SendMessageAsync(message, "input1"); Assert.False(sendMessageTask.IsCompleted); await deviceMessageHandler.ProcessMessageFeedbackAsync(Guid.NewGuid().ToString(), FeedbackStatus.Complete); await Task.Delay(TimeSpan.FromSeconds(1)); Assert.False(sendMessageTask.IsCompleted); }
public async Task X509DeviceCanSendMessageTest() { var connMgr = new Mock <IConnectionManager>(); connMgr.Setup(c => c.AddDeviceConnection(It.IsAny <IIdentity>(), It.IsAny <IDeviceProxy>())); var identity = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.ModuleId == "module1" && m.Id == "device1/module1"); var edgeHub = Mock.Of <IEdgeHub>(); Option <ICloudProxy> cloudProxy = Option.None <ICloudProxy>(); bool messageReceived = false; string lockToken = null; var underlyingDeviceProxy = new Mock <IDeviceProxy>(); underlyingDeviceProxy.Setup(d => d.SendMessageAsync(It.IsAny <IMessage>(), It.IsAny <string>())) .Callback <IMessage, string>( (m, i) => { messageReceived = true; lockToken = m.SystemProperties[SystemProperties.LockToken]; }) .Returns(Task.CompletedTask); connMgr.Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Task.FromResult(cloudProxy)); var deviceMessageHandler = new DeviceMessageHandler(identity, edgeHub, connMgr.Object, DefaultMessageAckTimeout, Option.None <string>()); deviceMessageHandler.BindDeviceProxy(underlyingDeviceProxy.Object); IMessage message = new EdgeMessage.Builder(new byte[0]).Build(); // send message to x509 device Task sendMessageTask = deviceMessageHandler.SendMessageAsync(message, "input1"); await deviceMessageHandler.ProcessMessageFeedbackAsync(lockToken, FeedbackStatus.Complete); await Task.Delay(TimeSpan.FromSeconds(1)); Assert.True(messageReceived); Assert.True(sendMessageTask.IsCompletedSuccessfully); }
public async Task ForwardsGetTwinOperationToEdgeHub() { var edgeHub = new Mock <IEdgeHub>(); var connMgr = Mock.Of <IConnectionManager>(); var identity = Mock.Of <IDeviceIdentity>(i => i.Id == "d1"); var cloudProxy = Mock.Of <ICloudProxy>(); var deviceProxy = new Mock <IDeviceProxy>(); IMessage actualMessage = null; deviceProxy.Setup(d => d.SendTwinUpdate(It.IsAny <IMessage>())) .Callback <IMessage>(m => actualMessage = m) .Returns(Task.CompletedTask); IMessage expectedMessage = new EdgeMessage.Builder(new byte[0]).Build(); edgeHub.Setup(e => e.GetTwinAsync(It.IsAny <string>())).Returns(Task.FromResult(expectedMessage)); Mock.Get(connMgr).Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Task.FromResult(Option.Some(cloudProxy))); var listener = new DeviceMessageHandler(identity, edgeHub.Object, connMgr, DefaultMessageAckTimeout, Option.None <string>()); listener.BindDeviceProxy(deviceProxy.Object); await listener.SendGetTwinRequest("cid"); edgeHub.Verify(x => x.GetTwinAsync(identity.Id), Times.Once); Assert.Same(expectedMessage, actualMessage); }
public async Task MessageCompletionLongAckTimeoutTest() { var connMgr = new Mock <IConnectionManager>(); connMgr.Setup(c => c.AddDeviceConnection(It.IsAny <IIdentity>(), It.IsAny <IDeviceProxy>())); var identity = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.ModuleId == "module1" && m.Id == "device1/module1"); var cloudProxy = new Mock <ICloudProxy>(); var edgeHub = Mock.Of <IEdgeHub>(); var underlyingDeviceProxy = new Mock <IDeviceProxy>(); IMessage receivedMessage = null; underlyingDeviceProxy.Setup(d => d.SendMessageAsync(It.IsAny <IMessage>(), It.IsAny <string>())) .Callback <IMessage, string>((m, s) => receivedMessage = m) .Returns(Task.CompletedTask); connMgr.Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Task.FromResult(Option.Some(cloudProxy.Object))); TimeSpan messageAckTimeout = TimeSpan.FromSeconds(15); var deviceMessageHandler = new DeviceMessageHandler(identity, edgeHub, connMgr.Object, messageAckTimeout, Option.None <string>()); deviceMessageHandler.BindDeviceProxy(underlyingDeviceProxy.Object); IMessage message = new EdgeMessage.Builder(new byte[0]).Build(); Task sendMessageTask = deviceMessageHandler.SendMessageAsync(message, "input1"); Assert.False(sendMessageTask.IsCompleted); await Task.Delay(TimeSpan.FromSeconds(10)); string messageId = receivedMessage.SystemProperties[SystemProperties.LockToken]; await deviceMessageHandler.ProcessMessageFeedbackAsync(messageId, FeedbackStatus.Complete); await Task.Delay(TimeSpan.FromSeconds(1)); Assert.True(sendMessageTask.IsCompleted); }
public async Task MessageCompletionTimeoutTest() { var connMgr = new Mock <IConnectionManager>(); connMgr.Setup(c => c.AddDeviceConnection(It.IsAny <IIdentity>(), It.IsAny <IDeviceProxy>())); var identity = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.ModuleId == "module1" && m.Id == "device1/module1"); var cloudProxy = new Mock <ICloudProxy>(); cloudProxy.Setup(c => c.BindCloudListener(It.IsAny <ICloudListener>())); var edgeHub = Mock.Of <IEdgeHub>(); var underlyingDeviceProxy = new Mock <IDeviceProxy>(); underlyingDeviceProxy.Setup(d => d.SendMessageAsync(It.IsAny <IMessage>(), It.IsAny <string>())) .Returns(Task.CompletedTask); connMgr.Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Option.Some(cloudProxy.Object)); var deviceMessageHandler = new DeviceMessageHandler(identity, edgeHub, connMgr.Object); deviceMessageHandler.BindDeviceProxy(underlyingDeviceProxy.Object); IMessage message = new EdgeMessage.Builder(new byte[0]).Build(); Task sendMessageTask = deviceMessageHandler.SendMessageAsync(message, "input1"); Assert.False(sendMessageTask.IsCompleted); await Assert.ThrowsAsync <TimeoutException>(async() => await sendMessageTask); }
public async Task ForwardsTwinPatchOperationToTheCloudProxy() { var connMgr = Mock.Of <IConnectionManager>(); var identity = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.ModuleId == "module1" && m.Id == "device1/module1"); var cloudProxy = Mock.Of <ICloudProxy>(); IMessage receivedMessage = null; var edgeHub = new Mock <IEdgeHub>(); edgeHub.Setup(e => e.UpdateReportedPropertiesAsync(It.IsAny <IIdentity>(), It.IsAny <IMessage>())) .Callback <IIdentity, IMessage>((id, m) => receivedMessage = m) .Returns(Task.CompletedTask); Mock.Get(connMgr).Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Option.Some(cloudProxy)); var listener = new DeviceMessageHandler(identity, edgeHub.Object, connMgr); var underlyingDeviceProxy = new Mock <IDeviceProxy>(); bool updateSent = false; underlyingDeviceProxy.Setup(d => d.SendTwinUpdate(It.IsAny <IMessage>())) .Callback(() => updateSent = true) .Returns(Task.CompletedTask); listener.BindDeviceProxy(underlyingDeviceProxy.Object); IMessage message = new EdgeMessage.Builder(Encoding.UTF8.GetBytes("don't care")).Build(); await listener.UpdateReportedPropertiesAsync(message, Guid.NewGuid().ToString()); edgeHub.VerifyAll(); Assert.True(updateSent); Assert.NotNull(receivedMessage); Assert.Equal(Constants.TwinChangeNotificationMessageSchema, receivedMessage.SystemProperties[SystemProperties.MessageSchema]); Assert.Equal(Constants.TwinChangeNotificationMessageType, receivedMessage.SystemProperties[SystemProperties.MessageType]); Assert.Equal("device1", receivedMessage.SystemProperties[SystemProperties.ConnectionDeviceId]); Assert.Equal("module1", receivedMessage.SystemProperties[SystemProperties.ConnectionModuleId]); Assert.True(receivedMessage.SystemProperties.ContainsKey(SystemProperties.EnqueuedTime)); }
public static async Task <TestModule> Create(string deviceId, string moduleId, string outputEndpointId, List <string> inputEndpointIds, IEdgeHub edgeHub, IConnectionManager connectionManager) { IClientCredentials moduleCredentials = SetupModuleCredentials(moduleId, deviceId); Try <ICloudProxy> cloudProxy = await connectionManager.CreateCloudConnectionAsync(moduleCredentials); Assert.True(cloudProxy.Success); var deviceListener = new DeviceMessageHandler(moduleCredentials.Identity, edgeHub, connectionManager, DefaultMessageAckTimeout); var receivedMessages = new List <IMessage>(); var deviceProxy = new Mock <IDeviceProxy>(); deviceProxy.Setup(d => d.SendMessageAsync(It.IsAny <IMessage>(), It.Is <string>(e => inputEndpointIds.Contains(e)))) .Callback <IMessage, string>( (m, e) => { receivedMessages.Add(m); deviceListener.ProcessMessageFeedbackAsync(m.SystemProperties[SystemProperties.LockToken], FeedbackStatus.Complete).Wait(); }) .Returns(Task.CompletedTask); deviceProxy.SetupGet(d => d.IsActive).Returns(true); deviceListener.BindDeviceProxy(deviceProxy.Object); await deviceListener.AddSubscription(DeviceSubscription.ModuleMessages); return(new TestModule(moduleCredentials.Identity as IModuleIdentity, outputEndpointId, deviceListener, receivedMessages)); }
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 ProcessDuplicateC2DMessageTest() { // Arrange var identity = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.Id == "device1"); var cloudProxy = new Mock <ICloudProxy>(); cloudProxy.Setup(c => c.SendFeedbackMessageAsync(It.IsAny <string>(), It.IsAny <FeedbackStatus>())) .Returns(Task.CompletedTask); var edgeHub = Mock.Of <IEdgeHub>(); var underlyingDeviceProxy = new Mock <IDeviceProxy>(); underlyingDeviceProxy.Setup(d => d.SendC2DMessageAsync(It.IsAny <IMessage>())).Returns(Task.CompletedTask); var connMgr = new Mock <IConnectionManager>(); connMgr.Setup(c => c.AddDeviceConnection(It.IsAny <IIdentity>(), It.IsAny <IDeviceProxy>())); connMgr.Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Task.FromResult(Option.Some(cloudProxy.Object))); var deviceMessageHandler = new DeviceMessageHandler(identity, edgeHub, connMgr.Object, DefaultMessageAckTimeout, Option.None <string>()); deviceMessageHandler.BindDeviceProxy(underlyingDeviceProxy.Object); string lockToken = Guid.NewGuid().ToString(); var systemProperties1 = new Dictionary <string, string> { [SystemProperties.LockToken] = lockToken }; var message1 = Mock.Of <IMessage>(m => m.SystemProperties == systemProperties1); var systemProperties2 = new Dictionary <string, string> { [SystemProperties.LockToken] = lockToken }; var message2 = Mock.Of <IMessage>(m => m.SystemProperties == systemProperties2); // Act await deviceMessageHandler.SendC2DMessageAsync(message1); // Assert underlyingDeviceProxy.Verify(d => d.SendC2DMessageAsync(It.IsAny <IMessage>()), Times.Once); // Act await deviceMessageHandler.SendC2DMessageAsync(message2); // Assert underlyingDeviceProxy.Verify(d => d.SendC2DMessageAsync(It.IsAny <IMessage>()), Times.Once); // Act await deviceMessageHandler.ProcessMessageFeedbackAsync(lockToken, FeedbackStatus.Complete); // Assert cloudProxy.Verify(c => c.SendFeedbackMessageAsync(It.IsAny <string>(), It.IsAny <FeedbackStatus>()), Times.Once); }
public static async Task <TestDevice> Create(string deviceId, IEdgeHub edgeHub, IConnectionManager connectionManager) { IClientCredentials deviceCredentials = SetupDeviceIdentity(deviceId); Try <ICloudProxy> cloudProxy = await connectionManager.CreateCloudConnectionAsync(deviceCredentials); Assert.True(cloudProxy.Success); var deviceProxy = Mock.Of <IDeviceProxy>(); var deviceListener = new DeviceMessageHandler(deviceCredentials.Identity, edgeHub, connectionManager); deviceListener.BindDeviceProxy(deviceProxy); return(new TestDevice(deviceCredentials.Identity as IDeviceIdentity, deviceListener)); }
DeviceMessageHandler GetDeviceMessageHandler() { var identity = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.ModuleId == "module1" && m.Id == "device1/module1"); var cloudProxy = new Mock <ICloudProxy>(); var edgeHub = Mock.Of <IEdgeHub>(); var underlyingDeviceProxy = new Mock <IDeviceProxy>(); underlyingDeviceProxy.Setup(d => d.InvokeMethodAsync(It.IsAny <DirectMethodRequest>())).ReturnsAsync(default(DirectMethodResponse)); underlyingDeviceProxy.Setup(d => d.SendC2DMessageAsync(It.IsAny <IMessage>())).Returns(Task.CompletedTask); var connMgr = new Mock <IConnectionManager>(); connMgr.Setup(c => c.AddDeviceConnection(It.IsAny <IIdentity>(), It.IsAny <IDeviceProxy>())); connMgr.Setup(c => c.GetCloudConnection(It.IsAny <string>())).Returns(Task.FromResult(Option.Some(cloudProxy.Object))); var deviceMessageHandler = new DeviceMessageHandler(identity, edgeHub, connMgr.Object, DefaultMessageAckTimeout, Option.None <string>()); deviceMessageHandler.BindDeviceProxy(underlyingDeviceProxy.Object); return(deviceMessageHandler); }
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); }
public async Task InvokeMethodLateSubscriptionTest() { // Create a mock endpoint capable of returning a mock processor var processor = Mock.Of <IProcessor>(); var endpoint = new Mock <Endpoint>("myId"); endpoint.Setup(ep => ep.CreateProcessor()).Returns(processor); endpoint.SetupGet(ep => ep.Id).Returns("myId"); // Create a mock endpoint executor factory to create the endpoint executor to verify invocation var endpointExecutor = Mock.Of <IEndpointExecutor>(); Mock.Get(endpointExecutor).SetupGet(ee => ee.Endpoint).Returns(() => endpoint.Object); var endpointExecutorFactory = Mock.Of <IEndpointExecutorFactory>(); Mock.Get(endpointExecutorFactory).Setup(eef => eef.CreateAsync(It.IsAny <Endpoint>())).ReturnsAsync(endpointExecutor); // Create a route to map to the message var endpoints = new HashSet <Endpoint> { endpoint.Object }; var route = new Route("myRoute", "true", "myIotHub", TelemetryMessageSource.Instance, endpoints); // Create a router var routerConfig = new RouterConfig(new[] { route }); Router router = await Router.CreateAsync("myRouter", "myIotHub", routerConfig, endpointExecutorFactory); // Create mock message converter to generate a message with source matching the route var messageConverter = Mock.Of <Core.IMessageConverter <Devices.Routing.Core.IMessage> >(); var methodRequest = new DirectMethodRequest("device1/module1", "shutdown", null, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(20)); // Mock of twin manager var twinManager = Mock.Of <ITwinManager>(); // DeviceListener var identity = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.ModuleId == "module1" && m.Id == "device1/module1"); var cloudProxy = new Mock <ICloudProxy>(); // ICloudConnectionProvider var cloudConnection = Mock.Of <ICloudConnection>(c => c.IsActive && c.CloudProxy == Option.Some(cloudProxy.Object)); var cloudConnectionProvider = new Mock <ICloudConnectionProvider>(); cloudConnectionProvider.Setup(c => c.Connect(It.IsAny <IIdentity>(), It.IsAny <Action <string, CloudConnectionStatus> >())) .ReturnsAsync(Try.Success(cloudConnection)); var deviceConnectivitymanager = Mock.Of <IDeviceConnectivityManager>(); var connectionManager = new ConnectionManager(cloudConnectionProvider.Object, Mock.Of <ICredentialsCache>(), new IdentityProvider("myIotHub"), deviceConnectivitymanager); IInvokeMethodHandler invokeMethodHandler = new InvokeMethodHandler(connectionManager); var subscriptionProcessor = new SubscriptionProcessor(connectionManager, invokeMethodHandler, Mock.Of <IDeviceConnectivityManager>()); // RoutingEdgeHub var routingEdgeHub = new RoutingEdgeHub( router, messageConverter, connectionManager, twinManager, "testEdgeDevice", invokeMethodHandler, subscriptionProcessor); var deviceMessageHandler = new DeviceMessageHandler(identity, routingEdgeHub, connectionManager); var underlyingDeviceProxy = new Mock <IDeviceProxy>(); // Arrange Message message = new EdgeMessage.Builder(new byte[0]).Build(); message.Properties[SystemProperties.CorrelationId] = methodRequest.CorrelationId; message.Properties[SystemProperties.StatusCode] = "200"; underlyingDeviceProxy.Setup(d => d.InvokeMethodAsync(It.IsAny <DirectMethodRequest>())) .Callback(() => deviceMessageHandler.ProcessMethodResponseAsync(message)) .ReturnsAsync(default(DirectMethodResponse)); underlyingDeviceProxy.SetupGet(d => d.IsActive).Returns(true); // Act deviceMessageHandler.BindDeviceProxy(underlyingDeviceProxy.Object); Task <DirectMethodResponse> responseTask = routingEdgeHub.InvokeMethodAsync(identity.Id, methodRequest); // Assert Assert.False(responseTask.IsCompleted); // Act await routingEdgeHub.AddSubscription(identity.Id, DeviceSubscription.Methods); await Task.Delay(TimeSpan.FromSeconds(5)); // Assert Assert.True(responseTask.IsCompleted); Assert.Equal(methodRequest.CorrelationId, responseTask.Result.CorrelationId); Assert.Equal(200, responseTask.Result.Status); Assert.False(responseTask.Result.Exception.HasValue); Assert.Equal(HttpStatusCode.OK, responseTask.Result.HttpStatusCode); }
public async Task InvokeMethodNoSubscriptionTest() { // Create a mock endpoint capable of returning a mock processor var processor = Mock.Of <IProcessor>(); var endpoint = new Mock <Endpoint>("myId"); endpoint.Setup(ep => ep.CreateProcessor()).Returns(processor); endpoint.SetupGet(ep => ep.Id).Returns("myId"); // Create a mock endpoint executor factory to create the endpoint executor to verify invocation var endpointExecutor = Mock.Of <IEndpointExecutor>(); Mock.Get(endpointExecutor).SetupGet(ee => ee.Endpoint).Returns(() => endpoint.Object); var endpointExecutorFactory = Mock.Of <IEndpointExecutorFactory>(); Mock.Get(endpointExecutorFactory).Setup(eef => eef.CreateAsync(It.IsAny <Endpoint>())).ReturnsAsync(endpointExecutor); // Create a route to map to the message var endpoints = new HashSet <Endpoint> { endpoint.Object }; var route = new Route("myRoute", "true", "myIotHub", TelemetryMessageSource.Instance, endpoints); // Create a router var routerConfig = new RouterConfig(new[] { route }); Router router = await Router.CreateAsync("myRouter", "myIotHub", routerConfig, endpointExecutorFactory); // Create mock message converter to generate a message with source matching the route var messageConverter = Mock.Of <Core.IMessageConverter <IMessage> >(); // Mock of twin manager var twinManager = Mock.Of <ITwinManager>(); // DeviceListener var identity = Mock.Of <IModuleIdentity>(m => m.DeviceId == "device1" && m.ModuleId == "module1" && m.Id == "device1/module1"); var cloudProxy = new Mock <ICloudProxy>(); cloudProxy.Setup(c => c.BindCloudListener(It.IsAny <ICloudListener>())); var underlyingDeviceProxy = new Mock <IDeviceProxy>(); underlyingDeviceProxy.Setup(d => d.InvokeMethodAsync(It.IsAny <DirectMethodRequest>())).ReturnsAsync(default(DirectMethodResponse)); underlyingDeviceProxy.SetupGet(d => d.IsActive).Returns(true); // ICloudConnectionProvider var cloudConnection = Mock.Of <ICloudConnection>(c => c.IsActive && c.CloudProxy == Option.Some(cloudProxy.Object)); var cloudConnectionProvider = new Mock <ICloudConnectionProvider>(); cloudConnectionProvider.Setup(c => c.Connect(It.IsAny <IClientCredentials>(), It.IsAny <Action <string, CloudConnectionStatus> >())) .ReturnsAsync(Try.Success(cloudConnection)); var connectionManager = new ConnectionManager(cloudConnectionProvider.Object); // RoutingEdgeHub var routingEdgeHub = new RoutingEdgeHub(router, messageConverter, connectionManager, twinManager, "testEdgeDevice"); var deviceMessageHandler = new DeviceMessageHandler(identity, routingEdgeHub, connectionManager); var methodRequest = new DirectMethodRequest("device1/module1", "shutdown", null, TimeSpan.FromSeconds(2), TimeSpan.FromMilliseconds(10)); // Act deviceMessageHandler.BindDeviceProxy(underlyingDeviceProxy.Object); Task <DirectMethodResponse> responseTask = routingEdgeHub.InvokeMethodAsync(identity.Id, methodRequest); // Assert Assert.True(responseTask.IsCompleted); Assert.Equal(404, responseTask.Result.Status); }