Add tests for NucuCar.Sensor environment sensor's grpc services
This commit is contained in:
parent
73bae4046f
commit
a208492e0c
7 changed files with 115 additions and 6 deletions
|
@ -11,6 +11,13 @@ namespace NucuCar.Domain.Telemetry
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class TelemetryPublisher : IDisposable
|
public abstract class TelemetryPublisher : IDisposable
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Parameter less constructor, mainly used for testing.
|
||||||
|
/// </summary>
|
||||||
|
public TelemetryPublisher()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Raw connection string that is used to connect to the cloud service. Should be parsed if required.
|
/// Raw connection string that is used to connect to the cloud service. Should be parsed if required.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -24,6 +24,11 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
private EnvironmentSensorMeasurement _lastMeasurement;
|
private EnvironmentSensorMeasurement _lastMeasurement;
|
||||||
private SensorStateEnum _sensorStateEnum;
|
private SensorStateEnum _sensorStateEnum;
|
||||||
|
|
||||||
|
public Bme680Sensor()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public Bme680Sensor(ILogger<Bme680Sensor> logger, IOptions<Bme680Config> options)
|
public Bme680Sensor(ILogger<Bme680Sensor> logger, IOptions<Bme680Config> options)
|
||||||
{
|
{
|
||||||
_sensorStateEnum = SensorStateEnum.Uninitialized;
|
_sensorStateEnum = SensorStateEnum.Uninitialized;
|
||||||
|
@ -39,12 +44,14 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public EnvironmentSensorMeasurement GetMeasurement()
|
// TODO Make more generic, Add interface and remove virtual
|
||||||
|
public virtual EnvironmentSensorMeasurement GetMeasurement()
|
||||||
{
|
{
|
||||||
return _lastMeasurement;
|
return _lastMeasurement;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SensorStateEnum GetState()
|
// TODO: Add interface and remove virtual
|
||||||
|
public virtual SensorStateEnum GetState()
|
||||||
{
|
{
|
||||||
return _sensorStateEnum;
|
return _sensorStateEnum;
|
||||||
}
|
}
|
||||||
|
@ -54,7 +61,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
_bme680?.Dispose();
|
_bme680?.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void InitializeSensor()
|
public void InitializeSensor()
|
||||||
{
|
{
|
||||||
if (_sensorStateEnum == SensorStateEnum.Initialized)
|
if (_sensorStateEnum == SensorStateEnum.Initialized)
|
||||||
{
|
{
|
||||||
|
@ -85,8 +92,8 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
_sensorStateEnum = SensorStateEnum.Error;
|
_sensorStateEnum = SensorStateEnum.Error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal async Task TakeMeasurement()
|
public async Task TakeMeasurement()
|
||||||
{
|
{
|
||||||
if (_sensorStateEnum != SensorStateEnum.Initialized)
|
if (_sensorStateEnum != SensorStateEnum.Initialized)
|
||||||
{
|
{
|
||||||
|
|
|
@ -16,6 +16,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
public class Bme680Worker : BackgroundService
|
public class Bme680Worker : BackgroundService
|
||||||
{
|
{
|
||||||
private readonly bool _telemetryEnabled;
|
private readonly bool _telemetryEnabled;
|
||||||
|
private readonly bool _serviceEnabled;
|
||||||
private readonly int _measurementInterval;
|
private readonly int _measurementInterval;
|
||||||
private readonly ILogger<Bme680Worker> _logger;
|
private readonly ILogger<Bme680Worker> _logger;
|
||||||
private readonly TelemetryPublisher _telemetryPublisher;
|
private readonly TelemetryPublisher _telemetryPublisher;
|
||||||
|
@ -27,6 +28,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_telemetryEnabled = options.Value.TelemetryEnabled;
|
_telemetryEnabled = options.Value.TelemetryEnabled;
|
||||||
|
_serviceEnabled = options.Value.ServiceEnabled;
|
||||||
_measurementInterval = options.Value.MeasurementInterval;
|
_measurementInterval = options.Value.MeasurementInterval;
|
||||||
_telemetryPublisher = sensorTelemetry.Publisher;
|
_telemetryPublisher = sensorTelemetry.Publisher;
|
||||||
_bme680Sensor = bme680Sensor;
|
_bme680Sensor = bme680Sensor;
|
||||||
|
@ -34,6 +36,10 @@ namespace NucuCar.Sensors.EnvironmentSensor
|
||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
|
if (_serviceEnabled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (_telemetryEnabled)
|
if (_telemetryEnabled)
|
||||||
{
|
{
|
||||||
_telemetryPublisher?.RegisterTelemeter(_bme680Sensor);
|
_telemetryPublisher?.RegisterTelemeter(_bme680Sensor);
|
||||||
|
|
|
@ -1,13 +1,19 @@
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using NucuCar.Domain.Telemetry;
|
using NucuCar.Domain.Telemetry;
|
||||||
|
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global
|
||||||
|
|
||||||
namespace NucuCar.Sensors.Telemetry
|
namespace NucuCar.Sensors.Telemetry
|
||||||
{
|
{
|
||||||
public class SensorTelemetry
|
public class SensorTelemetry
|
||||||
{
|
{
|
||||||
public TelemetryPublisher Publisher { get; }
|
public TelemetryPublisher Publisher { get; set; }
|
||||||
|
|
||||||
|
public SensorTelemetry()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public SensorTelemetry(ILogger<SensorTelemetry> logger, IOptions<TelemetryConfig> options)
|
public SensorTelemetry(ILogger<SensorTelemetry> logger, IOptions<TelemetryConfig> options)
|
||||||
{
|
{
|
||||||
if (options.Value.ServiceEnabled)
|
if (options.Value.ServiceEnabled)
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
using System;
|
||||||
|
using Grpc.Core;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using NucuCar.Sensors.EnvironmentSensor;
|
||||||
|
using NucuCarSensorsProto;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
|
||||||
|
{
|
||||||
|
public class Bme680GrpcServiceTest
|
||||||
|
{
|
||||||
|
private readonly ITestOutputHelper _testOutputHelper;
|
||||||
|
private readonly Mock<ILogger<Bme680GrpcService>> _mockLogger;
|
||||||
|
private readonly Mock<Bme680Sensor> _mockSensor;
|
||||||
|
|
||||||
|
|
||||||
|
public Bme680GrpcServiceTest(ITestOutputHelper testOutputHelper)
|
||||||
|
{
|
||||||
|
_testOutputHelper = testOutputHelper;
|
||||||
|
_mockLogger = new Mock<ILogger<Bme680GrpcService>>();
|
||||||
|
_mockSensor = new Mock<Bme680Sensor>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_GetSensorState()
|
||||||
|
{
|
||||||
|
var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object);
|
||||||
|
var result = service.GetSensorState(null, null).Result;
|
||||||
|
|
||||||
|
// Default sensor state is error
|
||||||
|
Assert.Equal(SensorStateEnum.Error, result.State);
|
||||||
|
|
||||||
|
// Verify that the sensor get state method is called.
|
||||||
|
_mockSensor.Verify(s => s.GetState(), Times.AtLeastOnce());
|
||||||
|
|
||||||
|
_mockSensor.Setup(s => s.GetState()).Returns(SensorStateEnum.Initialized);
|
||||||
|
result = service.GetSensorState(null, null).Result;
|
||||||
|
Assert.Equal(SensorStateEnum.Initialized, result.State);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Test_GetSensorMeasurement()
|
||||||
|
{
|
||||||
|
var service = new Bme680GrpcService(_mockLogger.Object, _mockSensor.Object);
|
||||||
|
service.GetSensorMeasurement(null, null);
|
||||||
|
|
||||||
|
// Verify that the sensor get measurement method is called.
|
||||||
|
_mockSensor.Verify(s => s.GetMeasurement(), Times.AtLeastOnce());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
NucuCar.UnitTests/NucuCar.UnitTests.csproj
Normal file
22
NucuCar.UnitTests/NucuCar.UnitTests.csproj
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.0</TargetFramework>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
|
||||||
|
<PackageReference Include="Moq" Version="4.13.1" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.0" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||||
|
<PackageReference Include="coverlet.collector" Version="1.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\NucuCar.Domain\NucuCar.Domain.csproj" />
|
||||||
|
<ProjectReference Include="..\NucuCar.Sensors\NucuCar.Sensors.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -6,6 +6,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NucuCar.TestClient", "NucuC
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NucuCar.Domain", "NucuCar.Domain\NucuCar.Domain.csproj", "{36BDA186-4C90-43C6-8991-A16DE245F91A}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NucuCar.Domain", "NucuCar.Domain\NucuCar.Domain.csproj", "{36BDA186-4C90-43C6-8991-A16DE245F91A}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NucuCar.UnitTests", "NucuCar.UnitTests\NucuCar.UnitTests.csproj", "{C6F07921-1052-4945-911E-F328A622F229}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
@ -24,5 +26,9 @@ Global
|
||||||
{36BDA186-4C90-43C6-8991-A16DE245F91A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{36BDA186-4C90-43C6-8991-A16DE245F91A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{36BDA186-4C90-43C6-8991-A16DE245F91A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{36BDA186-4C90-43C6-8991-A16DE245F91A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{36BDA186-4C90-43C6-8991-A16DE245F91A}.Release|Any CPU.Build.0 = Release|Any CPU
|
{36BDA186-4C90-43C6-8991-A16DE245F91A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{C6F07921-1052-4945-911E-F328A622F229}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{C6F07921-1052-4945-911E-F328A622F229}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{C6F07921-1052-4945-911E-F328A622F229}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{C6F07921-1052-4945-911E-F328A622F229}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
Loading…
Reference in a new issue