Fix interface and restore gRPC tests

This commit is contained in:
Denis-Cosmin Nutiu 2019-11-30 18:55:55 +02:00
parent f13a918af3
commit beeb6d22a2
7 changed files with 74 additions and 43 deletions

View file

@ -15,7 +15,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
/// Abstraction for the BME680 sensor.
/// See: https://www.bosch-sensortec.com/bst/products/all_products/bme680
/// </summary>
public class Bme680Sensor : IDisposable, ITelemeter
public class Bme680Sensor : IDisposable, ITelemeter, ISensor<Bme680Sensor>
{
private readonly ILogger _logger;
private I2cConnectionSettings _i2CSettings;
@ -42,15 +42,16 @@ namespace NucuCar.Sensors.EnvironmentSensor
_logger?.LogInformation("BME680 Sensor is disabled!");
_sensorStateEnum = SensorStateEnum.Disabled;
}
Object = this;
}
// TODO Make more generic, Add interface and remove virtual
// TODO Make more generic
public virtual EnvironmentSensorMeasurement GetMeasurement()
{
return _lastMeasurement;
}
// TODO: Add interface and remove virtual
public virtual SensorStateEnum GetState()
{
return _sensorStateEnum;
@ -135,5 +136,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
return returnValue;
}
public Bme680Sensor Object { get; }
}
}

View file

@ -36,7 +36,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (_serviceEnabled)
if (!_serviceEnabled)
{
return;
}

View file

@ -2,7 +2,7 @@ using NucuCar.Domain.Telemetry;
namespace NucuCar.Sensors
{
public interface ISensor<out TSensor> where TSensor : class, new()
public interface ISensor<out TSensor> where TSensor : class, ITelemeter
{
TSensor Object { get; }
}

View file

@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NucuCar.Sensors.EnvironmentSensor;
@ -23,9 +22,8 @@ namespace NucuCar.Sensors
// Singletons
services.AddSingleton<SensorTelemetry>();
services.AddSingleton<Bme680Sensor>();
services.AddSingleton<ISensor<Bme680Sensor>>();
services.AddSingleton<ISensor<Bme680Sensor>, Bme680Sensor>();
// Workers
services.AddHostedService<TelemetryWorker>();
services.AddHostedService<Bme680Worker>();

View file

@ -0,0 +1,9 @@
using NucuCar.Domain.Telemetry;
namespace NucuCar.Sensors
{
public class Sensor<T> : ISensor<T> where T : class, ITelemeter
{
public T Object { get; }
}
}

View file

@ -1,7 +1,6 @@
using System;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using Moq;
using NucuCar.Sensors;
using NucuCar.Sensors.EnvironmentSensor;
using NucuCarSensorsProto;
using Xunit;
@ -9,47 +8,47 @@ using Xunit.Abstractions;
namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
{
public class Bme680GrpcServiceTest
public partial class Bme680GrpcServiceTest
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly Mock<ILogger<Bme680GrpcService>> _mockLogger;
private readonly Mock<Bme680Sensor> _mockSensor;
private readonly Mock<ISensor<Bme680Sensor>> _mockSensor;
public Bme680GrpcServiceTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
_mockLogger = new Mock<ILogger<Bme680GrpcService>>();
_mockSensor = new Mock<Bme680Sensor>();
_mockSensor = new Mock<ISensor<Bme680Sensor>>();
_mockSensor.Setup(ms => ms.Object).Returns(new Mock<TestBme680Sensor>().Object);
}
//
// [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());
//
// }
[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.Object.GetState(), Times.AtLeastOnce());
_mockSensor.Setup(s => s.Object.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.Object.GetMeasurement(), Times.AtLeastOnce());
}
}
}

View file

@ -0,0 +1,22 @@
using NucuCar.Sensors.EnvironmentSensor;
using NucuCarSensorsProto;
namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
{
public partial class Bme680GrpcServiceTest
{
public class TestBme680Sensor : Bme680Sensor
{
// TODO Make more generic
public override EnvironmentSensorMeasurement GetMeasurement()
{
return new EnvironmentSensorMeasurement();
}
public override SensorStateEnum GetState()
{
return SensorStateEnum.Error;
}
}
}
}