using adas_core.Application.Repositories.Interfaces;
using adas_core.Application.Services;
using adas_core.Application.Services.Interfaces;
using adas_core.Domain.Enums;
using adas_core.Domain.Models;
using adas_core.Domain.Models.AppSettings;
using adas_core.Domain.Models.MongoModels;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using Moq;
using Options = Microsoft.Extensions.Options.Options;
namespace adas_core.Test.Services;
[TestFixture]
public class DiagnosisServiceTest
{
///
/// Initializes the mocked dependencies (patient, diagnosis, unit, audit, subscribers and calculated observations services along with their repositories) and constructs the instance under test.
///
[SetUp]
public void Setup()
{
_patientServiceMock = new Mock();
_patientServiceLazy = new Lazy(() => _patientServiceMock.Object);
//var sectionServiceMock = new Mock();
//sectionServiceLazy = new Lazy(() => sectionServiceMock.Object);
_diagnosisRepositoryMock = new Mock();
_diagnosisArchiveRepositoryMock = new Mock();
_clientMessageServiceMock = new Mock();
_subscribersServiceMock = new Mock();
_unitServiceMock = new Mock();
_httpContextAccessor = new Mock();
_auditService = new Mock();
_calculatedObservationsServiceMock = new Mock();
_calculatedObservationsServiceLazy =
new Lazy(() => _calculatedObservationsServiceMock.Object);
_calculatedObservationsServiceMock.Setup(x => x.Map(It.IsAny()))
.ReturnsAsync((PatientDiagnosis? value) => value);
_optionsApiSettings = Options.Create(_apiSettings);
_logger = new Mock>();
_diagnosisService = new DiagnosisService(
_patientServiceLazy,
//sectionServiceLazy,
_optionsApiSettings,
_diagnosisRepositoryMock.Object,
_diagnosisArchiveRepositoryMock.Object,
_logger.Object,
_clientMessageServiceMock.Object,
_subscribersServiceMock.Object,
_calculatedObservationsServiceLazy,
_httpContextAccessor.Object,
_auditService.Object,
_unitServiceMock.Object
);
// Create a mock of the singleton class subscribers
var mockSingleton = new Mock();
// Set up the mock object to return a specific value when a method is called
mockSingleton.Setup(x => x.Map(It.IsAny())).ReturnsAsync((PatientDiagnosis? value) => value);
}
private DiagnosisService _diagnosisService;
private Lazy _patientServiceLazy;
private Mock _patientServiceMock;
private Mock _diagnosisRepositoryMock;
private Mock _diagnosisArchiveRepositoryMock;
private Mock _clientMessageServiceMock;
private Mock _subscribersServiceMock;
private Mock _unitServiceMock;
private Lazy _calculatedObservationsServiceLazy;
private Mock _calculatedObservationsServiceMock;
private Mock _httpContextAccessor = new();
private Mock _auditService = new();
private readonly ApiSettings _apiSettings = new()
{
DiagnosisSystem = "Snomed-CT",
DiagnosisCode = { "55607006" }
};
private IOptions _optionsApiSettings;
private Mock> _logger;
private static readonly DateTime Now = DateTime.Now;
private static readonly ObjectId PatientId = ObjectId.GenerateNewId();
// [Test]
// public void SaveRequest_patientNumber_and_pointOfCare_null_Return_not_insert()
// {
// var apiRequest = new ApiRequest();
//
// Assert.ThrowsAsync(async () => await _diagnosisService.SaveRequest(apiRequest, null));
// }
// [Test]
// public void SaveRequest_Not_ORU_R01_Return_not_insert()
// {
// var patientObs = 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 = patientObs
//
// };
//
// var apiRequest = new ApiRequest
// {
// Location = new PatientLocation("UCI5C", "Box4"),
// Patient = patientObs,
// PatientNumber = "437537",
// Type = "ORU_R11",
// PatientId = PatientId.ToString(),
// MessageTime = Now
// };
//
// _patientServiceMock.Setup(cm => cm.FindPatientByApiRequest(apiRequest)).ReturnsAsync(patient);
//
// Assert.ThrowsAsync(async () => await _diagnosisService.SaveRequest(apiRequest, null));
// }
///
/// Verifies that SaveRequest does not insert a diagnosis when the patient referenced by the cannot be found.
///
/// A task representing the asynchronous test execution.
[Test]
public async Task SaveRequest_Not_FindPatient_Return_not_insert()
{
var apiRequest = new ApiRequest
{
Location = new PatientLocation("UCI5C", "Box4"),
PatientNumber = "437537",
Type = "ORU_R01",
PatientId = PatientId.ToString(),
MessageTime = Now
};
_patientServiceMock.Setup(cm => cm.FindPatientByApiRequest(apiRequest)).ReturnsAsync((Patient?)null);
await _diagnosisService.SaveRequest(apiRequest, null);
_diagnosisRepositoryMock.Verify(d => d.InsertOneAsync(It.IsAny()), Times.Never);
}
///
/// Verifies that SaveRequest does not insert a when the provided is not considered an observation request, ensuring observations must be present to trigger a database insertion.
///
/// The API request containing the patient and observation data being evaluated.
/// Reserved parameter passed to SaveRequest as a null value.
[Test]
public async Task SaveRequest_Not_Observations_Return_not_insert()
{
var patientObs = 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 = patientObs
};
var apiRequest = new ApiRequest
{
Location = new PatientLocation("UCI5C", "Box4"),
ObservationData = new ObservationData
{
Code = "302147001",
CodingSystem = "SNM",
Value = "Aire; Contacto; Preventivo",
Text = "Aislamiento",
Time = Now
},
Patient = patientObs,
PatientNumber = "437537",
Type = "ORU_R01",
PatientId = PatientId.ToString(),
MessageTime = Now
};
_patientServiceMock.Setup(cm => cm.FindPatientByApiRequest(apiRequest)).ReturnsAsync(patient);
await _diagnosisService.SaveRequest(apiRequest, null);
_diagnosisRepositoryMock.Verify(d => d.InsertOneAsync(It.IsAny()), Times.Never);
}
[Test]
public async Task SaveRequest_Not_DiagnosisCodeSettings_Return_not_insert()
{
var patientObs = 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 = patientObs
};
var apiRequest = new ApiRequest
{
Location = new PatientLocation("UCI5C", "Box4"),
ObservationData = new ObservationData
{
Code = "302147001",
CodingSystem = "SNM",
Value = "Aire; Contacto; Preventivo",
Text = "Aislamiento",
Time = Now
},
Observations =
[
new PatientObservation
{
Code = "263490005",
CodingSystem = "SNM",
Name = "Estado",
Status = StatusEnum.Type.Ok,
Time = Now,
Value = "Sin alergias conocidas"
},
new PatientObservation
{
Code = "300916003",
CodingSystem = "SNM",
Name = "¿Alergia al látex?",
Status = StatusEnum.Type.Ok,
Time = Now,
Value = "No"
}
],
Patient = patientObs,
PatientNumber = "437537",
Type = "ORU_R01",
PatientId = PatientId.ToString(),
MessageTime = Now
};
Options.Create(new ApiSettings());
var diagnosisService = new DiagnosisService(
_patientServiceLazy,
//sectionServiceLazy,
_optionsApiSettings,
_diagnosisRepositoryMock.Object,
_diagnosisArchiveRepositoryMock.Object,
_logger.Object,
_clientMessageServiceMock.Object,
_subscribersServiceMock.Object,
_calculatedObservationsServiceLazy,
_httpContextAccessor.Object,
_auditService.Object,
_unitServiceMock.Object);
_patientServiceMock.Setup(cm => cm.FindPatientByApiRequest(apiRequest)).ReturnsAsync(patient);
await diagnosisService.SaveRequest(apiRequest, null);
_diagnosisRepositoryMock.Verify(d => d.InsertOneAsync(It.IsAny()), Times.Never);
}
// [Test]
// public async Task SaveRequest_DiagnosisCodeSettings_Return_insert()
// {
// var patientObs = 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 = patientObs
// };
//
// var apiRequest = new ApiRequest
// {
// Location = new PatientLocation("UCI5C", "Box4"),
// ObservationData= new ObservationData
// {
// Code = "55607006",
// CodingSystem = "SNM",
// Value = "Diagnosis",
// Time = Now
//
// },
// Observations = new List
// {
// new()
// {
// Code = "272099008",
// CodingSystem = "SNM",
// Name = "description",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = "Daño axonal difuso"
// },
// new()
// {
// Code = "1000000013",
// CodingSystem = "SNM",
// Name = "label",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = "Daño axonal difuso"
// },
// new()
// {
// Code = "1000000014",
// CodingSystem = "SNM",
// Name = "code",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = "262693007"
// },
// new()
// {
// Code = "394731006",
// CodingSystem = "SNM",
// Name = "state",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = "state"
// },
// new()
// {
// Code = "272125009",
// CodingSystem = "SNM",
// Name = "category",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = "category"
// },
// new()
// {
// Code = "398201009",
// CodingSystem = "SNM",
// Name = "starTime",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = Now.AddHours(1)
// },
// new()
// {
// Code = "397898000",
// CodingSystem = "SNM",
// Name = "endTime",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = Now.AddHours(2)
// }
//
// },
// Patient = patientObs,
// PatientNumber = "437537",
// Type = "ORU_R01",
// PatientId = PatientId.ToString(),
// MessageTime = Now
// };
//
// _diagnosisService = new DiagnosisService(
// _patientServiceLazy,
// //sectionServiceLazy,
// _optionsApiSettings,
// _diagnosisRepositoryMock.Object,
// _diagnosisArchiveRepositoryMock.Object,
// _logger.Object,
// _clientMessageServiceMock.Object,
// _subscribersServiceMock.Object,
// _calculatedObservationsServiceLazy,
// _unitServiceMock.Object);
//
// _patientServiceMock.Setup(cm => cm.FindPatientByApiRequest(apiRequest)).ReturnsAsync(patient);
//
// await _diagnosisService.SaveRequest(apiRequest, null);
//
// _diagnosisRepositoryMock.Verify(d => d.InsertOneAsync(It.IsAny()), Times.Once);
// _diagnosisRepositoryMock.Verify(d => d.UpdateOneAsync(It.IsAny(),It.IsAny()), Times.Never);
// }
// [Test]
// public async Task SaveRequest_DiagnosisCodeSettings_Return_Update()
// {
// var patientObs = 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 = patientObs
// };
//
// var apiRequest = new ApiRequest
// {
// Location = new PatientLocation("UCI5C", "Box4"),
// ObservationData= new ObservationData
// {
// Code = "55607006",
// CodingSystem = "SNM",
// Value = "Diagnosis",
// Time = Now
//
// },
// Observations = new List
// {
// new()
// {
// Code = "272099008",
// CodingSystem = "SNM",
// Name = "description",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = "Daño axonal difuso"
// },
// new()
// {
// Code = "1000000013",
// CodingSystem = "SNM",
// Name = "label",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = "Daño axonal difuso"
// },
// new()
// {
// Code = "1000000014",
// CodingSystem = "SNM",
// Name = "code",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = "262693007"
// },
// new()
// {
// Code = "394731006",
// CodingSystem = "SNM",
// Name = "state",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = "state"
// },
// new()
// {
// Code = "272125009",
// CodingSystem = "SNM",
// Name = "category",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = "category"
// },
// new()
// {
// Code = "398201009",
// CodingSystem = "SNM",
// Name = "starTime",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = Now.AddHours(1)
// },
// new()
// {
// Code = "397898000",
// CodingSystem = "SNM",
// Name = "endTime",
// Status = ObservationStatus.Ok,
// Time = Now,
// Value = Now.AddHours(2)
// }
//
// },
// Patient = patientObs,
// PatientNumber = "437537",
// Type = "ORU_R01",
// PatientId = patient.Id.ToString(),
// MessageTime = Now
// };
//
// var patientDiagnosis = new PatientDiagnosis
// {
// Id = ObjectId.GenerateNewId(),
// Time = Now
// };
//
// _diagnosisService = new DiagnosisService(
// _patientServiceLazy,
// //sectionServiceLazy,
// _optionsApiSettings,
// _diagnosisRepositoryMock.Object,
// _diagnosisArchiveRepositoryMock.Object,
// _logger.Object,
// _clientMessageServiceMock.Object,
// _subscribersServiceMock.Object,
// _calculatedObservationsServiceLazy,
// _unitServiceMock.Object);
//
// _patientServiceMock.Setup(cm => cm.FindPatientByApiRequest(apiRequest)).ReturnsAsync(patient);
// _diagnosisRepositoryMock.Setup(d => d.FindByPatientIdAndCode(patient.Id, "262693007", "Snomed-CT")).ReturnsAsync(patientDiagnosis);
//
// await _diagnosisService.SaveRequest(apiRequest, null);
//
// //_diagnosisRepositoryMock.Verify(d => d.InsertOneAsync(It.IsAny()), Times.Never);
// _diagnosisRepositoryMock.Verify(d => d.UpdateOneAsync(It.IsAny(), It.IsAny()), Times.Once);
// }
}