Creado apartir del commit b888de6c92056de294456f581a56e25e734d4ab7 de develop
This commit is contained in:
@@ -0,0 +1,241 @@
|
||||
using System.Net;
|
||||
using adas_core.Application.Repositories.Interfaces;
|
||||
using adas_core.Application.Services.Interfaces;
|
||||
using adas_core.Domain.Enums;
|
||||
using adas_core.Domain.Models.MongoModels;
|
||||
using adas_core.Infrastructure.Services;
|
||||
using adas_core.module.Relays.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using Moq.Protected;
|
||||
using Options = Microsoft.Extensions.Options.Options;
|
||||
|
||||
namespace adas_core.Test.Services;
|
||||
|
||||
[TestFixture]
|
||||
public class RelayServiceTest
|
||||
{
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_logger = new Mock<ILogger<RelayService>>();
|
||||
_httpClientFactoryMock = new Mock<IHttpClientFactory>();
|
||||
_httpMessageHandlerMock = new Mock<HttpMessageHandler>();
|
||||
_relaySettings.RecordingOrApiUrl = "http://localhost:8080";
|
||||
_relaySettings.Cache = true;
|
||||
_optionsRelaySettings = Options.Create(_relaySettings);
|
||||
_pocServiceMock = new Mock<IPointOfCareService>();
|
||||
_relayRepositoryMock = new Mock<IRelayRepository>();
|
||||
|
||||
_relayService = new RelayService(
|
||||
//optionsApiSettings,
|
||||
_logger.Object,
|
||||
_httpClientFactoryMock.Object,
|
||||
_optionsRelaySettings,
|
||||
_pocServiceMock.Object,
|
||||
_relayRepositoryMock.Object);
|
||||
}
|
||||
|
||||
private RelayService _relayService = null!;
|
||||
private Mock<IHttpClientFactory> _httpClientFactoryMock = null!;
|
||||
private Mock<HttpMessageHandler> _httpMessageHandlerMock = null!;
|
||||
private readonly RelaySettings _relaySettings = new();
|
||||
private IOptions<RelaySettings> _optionsRelaySettings = null!;
|
||||
private Mock<IPointOfCareService> _pocServiceMock = null!;
|
||||
private Mock<IRelayRepository> _relayRepositoryMock = null!;
|
||||
|
||||
//readonly ApiSettings apiSettings = new ()
|
||||
//{
|
||||
//};
|
||||
//IOptions<ApiSettings> optionsApiSettings;
|
||||
|
||||
private Mock<ILogger<RelayService>> _logger = null!;
|
||||
|
||||
[Test]
|
||||
public async Task PowerOffAsync()
|
||||
{
|
||||
var relay = new Relay
|
||||
{
|
||||
Driver = "KMTronicV2",
|
||||
Ip = "10.133.10.169",
|
||||
Port = 80,
|
||||
RelayNumber = 1,
|
||||
RelayName = "Relay1",
|
||||
Total = 8,
|
||||
Username = "adas",
|
||||
Password = "!HULPM22"
|
||||
};
|
||||
|
||||
|
||||
var httpResponseMessage = new HttpResponseMessage
|
||||
{
|
||||
StatusCode = HttpStatusCode.OK,
|
||||
Content = new StringContent("Test content")
|
||||
};
|
||||
|
||||
_httpMessageHandlerMock.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(),
|
||||
ItExpr.IsAny<CancellationToken>())
|
||||
.ReturnsAsync(httpResponseMessage);
|
||||
|
||||
|
||||
var client = new HttpClient(_httpMessageHandlerMock.Object);
|
||||
|
||||
_httpClientFactoryMock.Setup(c => c.CreateClient(It.IsAny<string>())).Returns(client);
|
||||
|
||||
await _relayService.PowerOff(relay);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PowerOnAsync()
|
||||
{
|
||||
var relay = new Relay
|
||||
{
|
||||
Driver = "KMTronicV2",
|
||||
Ip = "10.133.10.169",
|
||||
Port = 80,
|
||||
RelayNumber = 1,
|
||||
RelayName = "Relay1",
|
||||
Total = 8,
|
||||
Username = "adas",
|
||||
Password = "!HULPM22"
|
||||
};
|
||||
|
||||
|
||||
var httpResponseMessage = new HttpResponseMessage
|
||||
{
|
||||
StatusCode = HttpStatusCode.OK,
|
||||
Content = new StringContent("Test content")
|
||||
};
|
||||
|
||||
_httpMessageHandlerMock.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(),
|
||||
ItExpr.IsAny<CancellationToken>())
|
||||
.ReturnsAsync(httpResponseMessage);
|
||||
|
||||
|
||||
var client = new HttpClient(_httpMessageHandlerMock.Object);
|
||||
|
||||
_httpClientFactoryMock.Setup(c => c.CreateClient(It.IsAny<string>())).Returns(client);
|
||||
|
||||
await _relayService.PowerOn(relay);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CheckRelayStatus_Return_On()
|
||||
{
|
||||
var relay = new Relay
|
||||
{
|
||||
Driver = "KMTronicV2",
|
||||
Ip = "10.133.10.169",
|
||||
Port = 80,
|
||||
RelayNumber = 1,
|
||||
RelayName = "Relay1",
|
||||
Total = 8,
|
||||
Username = "adas",
|
||||
Password = "!HULPM22"
|
||||
};
|
||||
|
||||
var httpResponseMessage = new HttpResponseMessage
|
||||
{
|
||||
StatusCode = HttpStatusCode.OK,
|
||||
Content = new StringContent("\"On")
|
||||
};
|
||||
|
||||
_httpMessageHandlerMock.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(),
|
||||
ItExpr.IsAny<CancellationToken>())
|
||||
.ReturnsAsync(httpResponseMessage);
|
||||
|
||||
//httpMessageHandlerMock.Protected()
|
||||
// .Setup<Task<string>>("ReadAsStringAsync", ItExpr.IsAny<string>(), ItExpr.IsAny<CancellationToken>())
|
||||
// .ReturnsAsync("\\On");
|
||||
|
||||
|
||||
var client = new HttpClient(_httpMessageHandlerMock.Object);
|
||||
|
||||
_httpClientFactoryMock.Setup(c => c.CreateClient(It.IsAny<string>())).Returns(client);
|
||||
|
||||
await _relayService.PowerOn(relay);
|
||||
var result = await _relayService.CheckRelayStatus(relay);
|
||||
|
||||
//Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.EqualTo(RelayEnum.Status.On));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CheckRelayStatus_Return_Off()
|
||||
{
|
||||
var relay = new Relay
|
||||
{
|
||||
Driver = "KMTronicV2",
|
||||
Ip = "10.133.10.169",
|
||||
Port = 80,
|
||||
RelayNumber = 1,
|
||||
RelayName = "Relay1",
|
||||
Total = 8,
|
||||
Username = "adas",
|
||||
Password = "!HULPM22"
|
||||
};
|
||||
|
||||
var httpResponseMessage = new HttpResponseMessage
|
||||
{
|
||||
StatusCode = HttpStatusCode.OK,
|
||||
Content = new StringContent("\"Off")
|
||||
};
|
||||
|
||||
_httpMessageHandlerMock.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(),
|
||||
ItExpr.IsAny<CancellationToken>())
|
||||
.ReturnsAsync(httpResponseMessage);
|
||||
|
||||
|
||||
var client = new HttpClient(_httpMessageHandlerMock.Object);
|
||||
|
||||
_httpClientFactoryMock.Setup(c => c.CreateClient(It.IsAny<string>())).Returns(client);
|
||||
await _relayService.PowerOff(relay);
|
||||
var result = await _relayService.CheckRelayStatus(relay);
|
||||
|
||||
//Assert.That(result, Is.Not.Null); //result nunca es null aquí
|
||||
Assert.That(result, Is.EqualTo(RelayEnum.Status.Off));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CheckRelayStatus_Return_Unknown()
|
||||
{
|
||||
var relay = new Relay
|
||||
{
|
||||
Driver = "KMTronicV2",
|
||||
Ip = "10.133.10.169",
|
||||
Port = 80,
|
||||
RelayNumber = 1,
|
||||
RelayName = "Relay1",
|
||||
Total = 8,
|
||||
Username = "adas",
|
||||
Password = "!HULPM22"
|
||||
};
|
||||
|
||||
|
||||
var httpResponseMessage = new HttpResponseMessage
|
||||
{
|
||||
StatusCode = HttpStatusCode.OK,
|
||||
Content = new StringContent("\\Off")
|
||||
};
|
||||
|
||||
_httpMessageHandlerMock.Protected()
|
||||
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(),
|
||||
ItExpr.IsAny<CancellationToken>())
|
||||
.ReturnsAsync(httpResponseMessage);
|
||||
|
||||
|
||||
var client = new HttpClient(_httpMessageHandlerMock.Object);
|
||||
|
||||
_httpClientFactoryMock.Setup(c => c.CreateClient(It.IsAny<string>())).Returns(client);
|
||||
|
||||
var result = await _relayService.CheckRelayStatus(relay);
|
||||
|
||||
//Assert.That(result, Is.Not.Null);
|
||||
Assert.That(result, Is.EqualTo(RelayEnum.Status.Unknown));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user