// Copyright (c) Microsoft. All rights reserved. using Microsoft.Azure.Devices.Client.Exceptions; using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services; using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Diagnostics; using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Exceptions; using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.IotHub; using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Models; using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Runtime; using Moq; using Services.Test.helpers; using Xunit; namespace Services.Test { public class DeviceClientTest { private readonly DeviceClient target; private readonly Mock client; private readonly Mock deviceMethods; private readonly Mock log; private readonly Mock servicesConfig; public DeviceClientTest() { this.client = new Mock(); this.deviceMethods = new Mock(); this.log = new Mock(); this.servicesConfig = new Mock(); this.target = new DeviceClient( "x", IoTHubProtocol.AMQP, this.client.Object, this.deviceMethods.Object, this.servicesConfig.Object, this.log.Object); } [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public void ConnectsToIoTHub() { // Act (connect twice, the second call should be ignored) this.target.ConnectAsync().Wait(Constants.TEST_TIMEOUT); this.target.ConnectAsync().Wait(Constants.TEST_TIMEOUT); // Assert this.client.Verify(x => x.OpenAsync(), Times.Once); } [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public void AuthFailureOnConnectRaiseException() { // Arrange this.client.Setup(x => x.OpenAsync()).Throws(new UnauthorizedException("")); // Act + Assert Assert.ThrowsAsync( async () => await this.target.ConnectAsync()).Wait(Constants.TEST_TIMEOUT); } [Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)] public void ItDisposesTheClientOnDisconnection() { // Act this.target.DisconnectAsync().CompleteOrTimeout(); // Assert this.client.Verify(x => x.Dispose(), Times.Once); } } }