using System.Net; using System.Text; using adas_core.Application.Services; using adas_core.Application.Services.Interfaces; using adas_core.Domain.Models; using adas_core.Domain.Models.AppSettings; using adas_core.Domain.Models.Recording; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Moq; using Moq.Protected; using Newtonsoft.Json; using Error = EasyNetQ.SystemMessages.Error; using Options = Microsoft.Extensions.Options.Options; namespace adas_core.Test.Services; [TestFixture] public class RecordingServiceTest { [SetUp] public void Setup() { _optionsApiSettings = Options.Create(_apiSettings); _optionsRabbitMqSettings = Options.Create(_rabbitMqSettings); _optionsRecordingSettings = Options.Create(_recordingSettings); _publisherServiceMock = new Mock(); _logger = new Mock>(); _authServiceMock = new Mock(); _httpClientFactoryMock = new Mock(); _httpMessageHandlerMock = new Mock(); _clientMessageServiceMock = new Mock(); _subscribersServiceMock = new Mock(); _patientServiceMock = new Mock>(); var client = new HttpClient(_httpMessageHandlerMock.Object); _httpClientFactoryMock.Setup(c => c.CreateClient(It.IsAny())).Returns(client); _recordingService = new RecordingService( _optionsRabbitMqSettings, _optionsRecordingSettings, _logger.Object, _httpClientFactoryMock.Object, _publisherServiceMock.Object, _authServiceMock.Object, _optionsApiSettings, _clientMessageServiceMock.Object, _subscribersServiceMock.Object, _patientServiceMock.Object ); // Set up the mock object to return a specific value when a method is called _publisherServiceMock.Setup(x => x.SendMessage(It.IsAny(), It.IsAny())).ReturnsAsync(true); _publisherServiceMock.Setup(x => x.SendMessageError(It.IsAny(), It.IsAny())).ReturnsAsync(true); } private RecordingService _recordingService = null!; //Mock mockSingleton = null!; private Mock _httpClientFactoryMock = null!; private Mock _httpMessageHandlerMock = null!; private Mock _publisherServiceMock = null!; private Mock _authServiceMock = null!; private Mock> _patientServiceMock = null!; private readonly RabbitMqSettings _rabbitMqSettings = new() { RecordingQueue = "recordings" }; private IOptions _optionsRabbitMqSettings = null!; private readonly RecordingSettings _recordingSettings = new() { RecordingApiUrl = "http://localhost:8082" }; private IOptions _optionsRecordingSettings = null!; private readonly ApiSettings _apiSettings = new() { StartRecordingWithoutPatientNumber = false }; private IOptions _optionsApiSettings = null!; private Mock> _logger = null!; private Mock _clientMessageServiceMock = null!; private Mock _subscribersServiceMock = null!; [Test] public async Task GetRecordings_Return_Empty() { var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.Unauthorized, Content = new StringContent("Content text.") }; var recordingData = new List(); _authServiceMock.Setup(s => s.GetToken()).ReturnsAsync("abc"); _httpMessageHandlerMock.Protected() .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) .ReturnsAsync(httpResponseMessage); var result = await _recordingService.GetRecordings(4); Assert.That(result, Is.Not.Null); //Assert.AreEqual(result, recordingData); Assert.That(result, Is.EqualTo(recordingData)); } [Test] public async Task GetRecordings_Return_RecordingData() { var patient = new Patient { Id = "id", FirstName = "firstName", LastName = "lastName" }; var recordingData = new List { new() { Patient = patient, RoomId = 4, Status = "INITIALIZED" } }; var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(JsonConvert.SerializeObject(recordingData), Encoding.UTF8, "application/json") }; _httpMessageHandlerMock.Protected() .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) .ReturnsAsync(httpResponseMessage); _authServiceMock.Setup(s => s.GetToken()).ReturnsAsync("abc"); var result = await _recordingService.GetRecordings(4); Assert.That(result, Is.Not.Null); using (Assert.EnterMultipleScope()) { Assert.That(recordingData[0].Status, Is.EqualTo(result![0].Status)); Assert.That(recordingData[0].Patient?.Id, Is.EqualTo(result[0].Patient?.Id)); Assert.That(recordingData[0].Patient?.LastName, Is.EqualTo(result[0].Patient?.LastName)); Assert.That(recordingData[0].Patient?.FirstName, Is.EqualTo(result[0].Patient?.FirstName)); Assert.That(recordingData[0].RoomId, Is.EqualTo(result[0].RoomId)); } } [Test] public Task SaveRequest() { ApiRequest apiRequest = new(); var httpResponseMessage = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent("Content text.") }; _httpMessageHandlerMock.Protected() .Setup>("SendAsync", ItExpr.IsAny(), ItExpr.IsAny()) .ReturnsAsync(httpResponseMessage); Func act = async () => await _recordingService.SaveRequest(apiRequest); Assert.ThrowsAsync(act); return Task.CompletedTask; } }