using System.Security.Claims; using adas_core.Application.Repositories.Interfaces; using adas_core.Application.Services; using adas_core.Application.Services.Interfaces; using adas_core.Domain.Models; using adas_core.Domain.Models.MongoModels; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using MongoDB.Bson; using Moq; namespace adas_core.Test.Services; [TestFixture] public class RecordingAlertServiceTest { [SetUp] public void Setup() { _patientServiceMock = new Mock(); _patientServiceLazy = new Lazy(() => _patientServiceMock.Object); _configObservationServiceMock = new Mock(); _recordingAlertRepositoryMock = new Mock(); _recordingAlertArchiveRepositoryMock = new Mock(); _clientMessageServiceMock = new Mock(); _subscribersServiceMock = new Mock(); var userClaims = new ClaimsPrincipal(new ClaimsIdentity([ new Claim(ClaimTypes.Name, "TestUser") ], "mock")); var httpContextMock = new DefaultHttpContext { User = userClaims }; _httpContextAccessorMock.Setup(accessor => accessor.HttpContext) .Returns(httpContextMock); //optionsApiSettings = Microsoft.Extensions.Options.Options.Create(apiSettings); _logger = new Mock>(); _recordingAlertService = new RecordingAlertService( _patientServiceLazy, _configObservationServiceMock.Object, _recordingAlertRepositoryMock.Object, _recordingAlertArchiveRepositoryMock.Object, //optionsApiSettings, _logger.Object, _clientMessageServiceMock.Object, _subscribersServiceMock.Object, _httpContextAccessorMock.Object, _auditServiceMock.Object ); } private RecordingAlertService _recordingAlertService = null!; private Mock _patientServiceMock = null!; private Lazy _patientServiceLazy = null!; private Mock _configObservationServiceMock = null!; private Mock _recordingAlertRepositoryMock = null!; private Mock _recordingAlertArchiveRepositoryMock = null!; private Mock _clientMessageServiceMock = null!; private Mock _subscribersServiceMock = null!; private readonly Mock _httpContextAccessorMock = new(); private readonly Mock _auditServiceMock = new(); //private readonly ApiSettings apiSettings = new () //{ //}; //IOptions optionsApiSettings; private Mock> _logger = null!; private static readonly DateTime Now = DateTime.Now; private static readonly ObjectId PatientId = ObjectId.GenerateNewId(); [Test] public async Task SaveRequest_Not_RecordingAlert_Not_ORU_R01_Return_not_insert() { var apiRequest = new ApiRequest(); await _recordingAlertService.SaveRequest(apiRequest); _recordingAlertRepositoryMock.Verify(d => d.InsertOneAsync(It.IsAny()), Times.Never); } [Test] public async Task SaveRequest_patientNumber_and_pointOfCare_null_Return_not_insert() { var apiRequest = new ApiRequest { Type = "ORU_R11", MessageTime = Now }; await _recordingAlertService.SaveRequest(apiRequest); _recordingAlertRepositoryMock.Verify(d => d.InsertOneAsync(It.IsAny()), Times.Never); } [Test] public async Task SaveRequest_Not_Find_Patient_Return_not_insert() { var person = new Person { FirstName = "Miguel", LastName = "Villanueva", Ids = new Dictionary { { "MR", "437537" } } }; var apiRequest = new ApiRequest { Location = new PatientLocation("UCI5C", "Box4"), Patient = person, PatientNumber = "437537", Type = "ORU_R01", PatientId = PatientId.ToString(), MessageTime = Now }; await _recordingAlertService.SaveRequest(apiRequest); _recordingAlertRepositoryMock.Verify(d => d.InsertOneAsync(It.IsAny()), Times.Never); } [Test] public async Task SaveRequest_Alert_Return_insert() { var person = new Person { FirstName = "Miguel", LastName = "Villanueva", Ids = new Dictionary { { "MR", "437537" } } }; var patient = new Patient { Id = ObjectId.GenerateNewId(), UnitString = "UCI5C", Bed = "Box4", PatientNumber = "437537", Person = person }; var recordingAlert = new PatientRecordingAlert { IsRecording = true }; var apiRequest = new ApiRequest { Location = new PatientLocation("UCI5C", "Box4"), Patient = person, PatientNumber = "437537", Type = "ORU_R01", PatientId = PatientId.ToString(), MessageTime = Now, RecordingAlert = recordingAlert }; _patientServiceMock.Setup(p => p.FindPatientByApiRequest(apiRequest)).ReturnsAsync(patient); _configObservationServiceMock.Setup(c => c.RetentionActions(It.IsAny())) .ReturnsAsync((ObservatitonRetentionResult?)null); await _recordingAlertService.SaveRequest(apiRequest); _recordingAlertRepositoryMock.Verify(d => d.InsertOneAsync(It.IsAny()), Times.Once); } }