using AutoMapper; using Diligent.WebAPI.Business.MappingProfiles; using Diligent.WebAPI.Business.Services; using Diligent.WebAPI.Contracts.DTOs.Ad; using Diligent.WebAPI.Contracts.DTOs.Applicant; using Diligent.WebAPI.Contracts.DTOs.SelectionLevel; using Diligent.WebAPI.Contracts.Exceptions; using Diligent.WebAPI.Data.Entities; using Microsoft.Extensions.Logging; namespace Diligent.WebAPI.Tests.Services { public class SelectionLevelsServiceTests { private readonly IMapper _mapper; private readonly List _levels; private ILogger _logger = Substitute.For>(); private readonly SelectionLevel _selectionLevel; public SelectionLevelsServiceTests() { _selectionLevel = new SelectionLevel { Id = 1, Name = "HR intervju", SelectionProcesses = new List() }; // configure mapper var configuration = new MapperConfiguration(cfg => cfg.AddProfiles( new List { new SelectionLevelMappingProfile(), new SelectionProcessMappingProfile(), })); _mapper = new Mapper(configuration); } [Fact] public async Task GetAll_ShouldReturnListOfLevels_Always() { var databaseContext = await Helpers.GetDatabaseContext(_levels); SelectionLevelService service = new(databaseContext, _mapper, _logger); var result = await service.GetAllAsync(); result.Should().HaveCount(4); } [Fact] public async Task GetFilteredData_ShouldReturnListOfLevels_Always() { var databaseContext = await Helpers.GetDatabaseContext(_levels); databaseContext.SelectionLevels.First().SelectionProcesses = new List { new SelectionProcess{ Id = 1, Status = "Obrađen", Date = DateTime.Now}, new SelectionProcess{ Id = 2, Status = "Obrađen", Date = DateTime.Now.AddMonths(-1)}, new SelectionProcess{ Id = 3, Status = "Čeka na zakazivanje", Date = DateTime.Now}, new SelectionProcess{ Id = 4, Status = "Čeka na zakazivanje", Date = DateTime.Now.AddMonths(-1)}, }; SelectionLevelService service = new(databaseContext, _mapper, _logger); var filter = new SelectionProcessFilterDto { DateStart = DateTime.Now.AddDays(-1), DateEnd = DateTime.Now.AddDays(1), Statuses = new string[]{ "Obrađen", "Zakazan" } }; var result = service.GetFilteredLevelsAsync(filter); result.Should().HaveCount(4); result.First().SelectionProcesses.Should().HaveCount(1); } [Fact] public async Task GetById_ShouldReturnLevel_WhenLevelExist() { var databaseContext = await Helpers.GetDatabaseContext(_levels); SelectionLevelService service = new(databaseContext, _mapper, _logger); var result = await service.GetByIdAsync(1); result.Should().BeEquivalentTo(_mapper.Map(_selectionLevel)); } [Fact] public async Task GetById_ShouldThrowEntityNotFooundException_WhenLevelDoesnotExist() { var databaseContext = await Helpers.GetDatabaseContext(_levels); SelectionLevelService service = new(databaseContext, _mapper, _logger); await Assert.ThrowsAsync(async () => await service.GetByIdAsync(1000)); } [Fact] public async Task GetByIdEntity_ShouldReturnLevel_WhenLevelExist() { var databaseContext = await Helpers.GetDatabaseContext(_levels); SelectionLevelService service = new(databaseContext, _mapper, _logger); var result = await service.GetByIdEntity(1); result.Id.Should().Be(_selectionLevel.Id); result.Name.Should().Be(_selectionLevel.Name); } [Fact] public async Task GetByIdEntity_ShouldThrowEntityNotFooundException_WhenLevelDoesnotExist() { var databaseContext = await Helpers.GetDatabaseContext(_levels); SelectionLevelService service = new(databaseContext, _mapper, _logger); await Assert.ThrowsAsync(async () => await service.GetByIdEntity(1000)); } [Fact] public async Task GetCountByLevels_ShouldReturnListOfLevels_Always() { var databaseContext = await Helpers.GetDatabaseContext(_levels); databaseContext.SelectionLevels.First().SelectionProcesses = new List { new SelectionProcess{ Id = 1, Status = "Obrađen", Date = DateTime.Now}, new SelectionProcess{ Id = 2, Status = "Zakazan", Date = DateTime.Now.AddMonths(-1)}, new SelectionProcess{ Id = 3, Status = "Čeka na zakazivanje", Date = DateTime.Now}, new SelectionProcess{ Id = 4, Status = "Čeka na zakazivanje", Date = DateTime.Now.AddMonths(-1)}, }; SelectionLevelService service = new(databaseContext, _mapper, _logger); var statues = new List { "Obrađen", "Zakazan" }; var result = await service.GetCountByLevels(statues); result.Should().HaveCount(4); result.First().CountAll.Should().Be(4); result.First().CountDone.Should().Be(2); } } }