2018-07-18 02:37:58 +03:00
|
|
|
|
// 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;
|
2018-12-13 02:35:59 +03:00
|
|
|
|
using Microsoft.Azure.IoTSolutions.DeviceSimulation.Services.Runtime;
|
2018-07-18 02:37:58 +03:00
|
|
|
|
using Moq;
|
|
|
|
|
using Services.Test.helpers;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Services.Test
|
|
|
|
|
{
|
|
|
|
|
public class DeviceClientTest
|
|
|
|
|
{
|
|
|
|
|
private readonly DeviceClient target;
|
|
|
|
|
private readonly Mock<IDeviceClientWrapper> client;
|
|
|
|
|
private readonly Mock<IDeviceMethods> deviceMethods;
|
|
|
|
|
private readonly Mock<ILogger> log;
|
2018-12-13 02:35:59 +03:00
|
|
|
|
private readonly Mock<IServicesConfig> servicesConfig;
|
2018-07-18 02:37:58 +03:00
|
|
|
|
|
|
|
|
|
public DeviceClientTest()
|
|
|
|
|
{
|
|
|
|
|
this.client = new Mock<IDeviceClientWrapper>();
|
|
|
|
|
this.deviceMethods = new Mock<IDeviceMethods>();
|
|
|
|
|
this.log = new Mock<ILogger>();
|
2018-12-13 02:35:59 +03:00
|
|
|
|
this.servicesConfig = new Mock<IServicesConfig>();
|
2018-07-18 02:37:58 +03:00
|
|
|
|
|
|
|
|
|
this.target = new DeviceClient(
|
|
|
|
|
"x",
|
|
|
|
|
IoTHubProtocol.AMQP,
|
|
|
|
|
this.client.Object,
|
|
|
|
|
this.deviceMethods.Object,
|
2018-12-13 02:35:59 +03:00
|
|
|
|
this.servicesConfig.Object,
|
2018-07-18 02:37:58 +03:00
|
|
|
|
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
|
2018-10-20 02:58:18 +03:00
|
|
|
|
this.client.Verify(x => x.OpenAsync(), Times.Once);
|
2018-07-18 02:37:58 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)]
|
|
|
|
|
public void AuthFailureOnConnectRaiseException()
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
this.client.Setup(x => x.OpenAsync()).Throws(new UnauthorizedException(""));
|
2018-10-20 02:58:18 +03:00
|
|
|
|
|
2018-07-18 02:37:58 +03:00
|
|
|
|
// Act + Assert
|
|
|
|
|
Assert.ThrowsAsync<DeviceAuthFailedException>(
|
|
|
|
|
async () => await this.target.ConnectAsync()).Wait(Constants.TEST_TIMEOUT);
|
|
|
|
|
}
|
2018-10-20 02:58:18 +03:00
|
|
|
|
|
|
|
|
|
[Fact, Trait(Constants.TYPE, Constants.UNIT_TEST)]
|
|
|
|
|
public void ItDisposesTheClientOnDisconnection()
|
|
|
|
|
{
|
|
|
|
|
// Act
|
|
|
|
|
this.target.DisconnectAsync().CompleteOrTimeout();
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
this.client.Verify(x => x.Dispose(), Times.Once);
|
|
|
|
|
}
|
2018-07-18 02:37:58 +03:00
|
|
|
|
}
|
|
|
|
|
}
|