using AutoMapper; using Diligent.WebAPI.Business.Extensions; 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.Exceptions; using Diligent.WebAPI.Data.Entities; using Microsoft.AspNetCore.Http; using static Diligent.WebAPI.Data.Entities.Applicant; namespace Diligent.WebAPI.Tests.Services { public class ApplicantServiceTests { private readonly IMapper _mapper; private readonly HttpContext _httpContext; private readonly List _applicants; private readonly List _ads; public ApplicantServiceTests() { _applicants = MockData.GetListOfApplicants(); _ads = MockData.GetListOfAds(); // configure mapper var configuration = new MapperConfiguration(cfg => cfg.AddProfiles( new List { new ApplicantMappingProfile(), new AdMappingProfile(), new SelectionProcessMappingProfile() })); _mapper = new Mapper(configuration); _httpContext = new DefaultHttpContext(); } [Fact] public async Task GetFilteredApplicants_ShouldReturnListOfApplicants_Always() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper); var filterDto = new ApplicantFilterDto(); var result = await applicantService.GetFilteredApplicants(filterDto); Assert.Equal(_applicants.Count,result.Total); } [Fact] public async Task GetById_ShouldReturnApplicant_WhenApplicantExist() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper); var result = await applicantService.GetById(1); result.Should().BeEquivalentTo(_mapper.Map(_applicants[0])); } [Fact] public async Task GetById_ShouldThrowEntityNotFooundException_WhenApplicantDontExist() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper); await Assert.ThrowsAsync(async () => await applicantService.GetById(1000)); } [Fact] public async Task CreateApplicant_ShouldAddEntityIntoDatabase_Always() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper); ApplicantCreateDto applicantCreateDto = new() { ApplicationChannel = "Facebook", BitBucketLink = null, CV = "link", Email = "some@mail.com", Experience = 1, FirstName = "Meris", LastName = "Ahmatovic", GithubLink = null, LinkedlnLink = null, PhoneNumber = "432424", Position = ".NET Developer", TypeOfEmployment = TypesOfEmployment.Intership.ToString() }; await applicantService.CreateApplicant(applicantCreateDto); var filterDto = new ApplicantFilterDto { CurrentPage = 1, PageSize = 4 }; var result = await applicantService.GetFilteredApplicants(filterDto); Assert.Equal(2, result.Total); } [Fact] public async Task DeleteApplicant_ShouldDeleteApplicant_WhenApplicantExist() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper); await applicantService.DeleteApplicant(1); var filterDto = MockData.GetApplicantFilters(); var applicants = await applicantService.GetFilteredApplicants(filterDto); Assert.Empty(applicants.Items); } [Fact] public async Task DeleteApplicant_ShouldThrowEntityNotFooundException_WhenApplicantDontExist() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper); await Assert.ThrowsAsync(async () => await applicantService.DeleteApplicant(1000)); } [Fact] public async Task UpdateApplicant_ShouldUpdateApplicant_WhenApplicantExist() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper); ApplicantUpdateDto applicantUpdateDto = new() { ApplicationChannel = "Instagram", BitBucketLink = null, CV = "link", Email = "some@mail.com", Experience = 1, FirstName = "Dzenis", LastName = "Hadzifejzovic", GithubLink = null, LinkedlnLink = null, PhoneNumber = "432424", Position = "React Developer" }; await applicantService.UpdateApplicant(1, applicantUpdateDto); var applicant = await applicantService.GetById(1); Assert.Equal(applicant.Position, applicantUpdateDto.Position); } [Fact] public async Task GetAllAdsApplicants_ShouldReturnListOfAdApplicants_Always() { var databaseContext = await Helpers.GetDatabaseContext(_ads); ApplicantService applicantService = new(databaseContext, _mapper); var filterDto = MockData.GetApplicantFilters(); var result = await applicantService.GetAllAdsApplicants(filterDto); result.Should().BeEquivalentTo(_mapper.Map>(_ads)); } [Fact] public async Task GetApplicantWithSelectionProcessesById_ShouldReturnApplicant_WhenApplicantExists() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper); var result = await applicantService.GetApplicantWithSelectionProcessesById(1); var processes = result.SelectionProcesses; processes.Should().HaveCount(3); result.Should().BeEquivalentTo(_mapper.Map(_applicants[0])); } } }