| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- 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.DTOs.SelectionProcess;
- using Diligent.WebAPI.Contracts.Exceptions;
- using Diligent.WebAPI.Data.Entities;
- using FluentAssertions.Common;
- using Microsoft.Extensions.Logging;
- using NSubstitute.ExceptionExtensions;
- using NSubstitute.Extensions;
- using System.Reflection;
-
- namespace Diligent.WebAPI.Tests.Services
- {
- public class SelectionLevelsServiceTests
- {
- private readonly IMapper _mapper;
- private readonly List<SelectionLevel> _levels;
- private ILogger<SelectionLevelService> _logger = Substitute.For<ILogger<SelectionLevelService>>();
- private readonly SelectionLevel _selectionLevel;
- public SelectionLevelsServiceTests()
- {
- _selectionLevel = new SelectionLevel
- {
- Id = 1,
- Name = "HR intervju",
- SelectionProcesses = new List<SelectionProcess>()
- };
-
- // configure mapper
- var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
- new List<Profile>
- {
- new SelectionLevelMappingProfile(),
- new SelectionProcessMappingProfile(),
- }));
- _mapper = new Mapper(configuration);
- }
-
- [Fact]
- public async Task GetAll_ShouldReturnListOfLevels_Always()
- {
- var databaseContext = await Helpers<SelectionLevel>.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<SelectionLevel>.GetDatabaseContext(_levels);
- databaseContext.SelectionLevels.First().SelectionProcesses = new List<SelectionProcess>
- {
- 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<SelectionLevel>.GetDatabaseContext(_levels);
- SelectionLevelService service = new(databaseContext, _mapper, _logger);
-
- var result = await service.GetByIdAsync(1);
-
- result.Should().BeEquivalentTo(_mapper.Map<SelectionLevelResposneDto>(_selectionLevel));
- }
-
- [Fact]
- public async Task GetById_ShouldThrowEntityNotFooundException_WhenLevelDoesnotExist()
- {
- var databaseContext = await Helpers<SelectionLevel>.GetDatabaseContext(_levels);
- SelectionLevelService service = new(databaseContext, _mapper, _logger);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(async () => await service.GetByIdAsync(1000));
- }
-
- [Fact]
- public async Task GetByIdEntity_ShouldReturnLevel_WhenLevelExist()
- {
- var databaseContext = await Helpers<SelectionLevel>.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<SelectionLevel>.GetDatabaseContext(_levels);
- SelectionLevelService service = new(databaseContext, _mapper, _logger);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(async () => await service.GetByIdEntity(1000));
- }
-
- [Fact]
- public async Task GetCountByLevels_ShouldReturnListOfLevels_Always()
- {
- var databaseContext = await Helpers<SelectionLevel>.GetDatabaseContext(_levels);
- databaseContext.SelectionLevels.First().SelectionProcesses = new List<SelectionProcess>
- {
- 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<string> { "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);
- }
- }
- }
|