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.Exceptions; using Diligent.WebAPI.Data.Entities; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; namespace Diligent.WebAPI.Tests.Services { public class ApplicantServiceTests { private readonly IMapper _mapper; private ILogger _logger = Substitute.For>(); private readonly IUserService _userService = Substitute.For(); private readonly IFileService _fileService = Substitute.For(); private readonly IAdService _adService = Substitute.For(); private readonly List _applicants; private readonly List _ads; private readonly List _users; public ApplicantServiceTests() { // mock data _applicants = MockData.GetListOfApplicants(); _ads = MockData.GetListOfAds(); _users = MockData.GetListOfUsers(); // configure mapper var configuration = new MapperConfiguration(cfg => cfg.AddProfiles( new List { new ApplicantMappingProfile(), new AdMappingProfile(), new SelectionProcessMappingProfile() })); _mapper = new Mapper(configuration); } [Fact] public async Task GetFilteredApplicants_ShouldReturnOneApplicant_Always() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); var filterDto = new ApplicantFilterDto { MinExperience = 2, MaxExperience = 4 }; var result = await applicantService.GetFilteredApplicants(filterDto); Assert.Equal(1,result.Total); } [Fact] public async Task GetAllAdsApplicants_ShouldReturnAllApplicants_Always() { var databaseContext = await Helpers.GetDatabaseContext(_ads); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); var filterDto = MockData.GetApplicantFilters(); var result = await applicantService.GetAllAdsApplicants(filterDto); result.Should().BeEquivalentTo(_mapper.Map>(_ads)); } [Fact] public async Task GetById_ShouldReturnApplicant_WhenApplicantExist() { var fileInBase64Format = "some string"; var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); _fileService.GetCV(Arg.Any()).Returns(fileInBase64Format); var result = await applicantService.GetById(1); _applicants[0].CV = fileInBase64Format; result.Should().BeEquivalentTo(_mapper.Map(_applicants[0])); } [Fact] public async Task GetById_ShouldThrowEntityNotFoundException_WhenApplicantDoesNotExist() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); await Assert.ThrowsAsync(async () => await applicantService.GetById(1000)); } [Fact] public async Task GetApplicantWithSelectionProcessesById_ShouldReturnApplicant_WhenApplicantExists() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); var result = await applicantService.GetApplicantWithSelectionProcessesById(1); var processes = result.SelectionProcesses; processes.Should().HaveCount(3); result.Should().BeEquivalentTo(_mapper.Map(_applicants[0])); } [Fact] public async Task GetApplicantWithSelectionProcessesById_ShouldThrowEntityNotFoundException_WhenApplicantDoesNotExist() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); await Assert.ThrowsAsync(async () => await applicantService.GetApplicantWithSelectionProcessesById(1000)); } [Fact] public async Task ApplyForAd_ShouldThrowEntityNotFooundException_WhenAdIsNotFound() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); ApplyForAdRequestDto applyForAdRequestDto = new() { AdId = 1, BitBucketLink = "", CoverLetter = "saddadas", DateOfBirth = new DateTime(1980, 10, 10), Email = "meris@gmail.com", Experience = 3, FirstName = "Meris", LastName = "Ahmatovic", GithubLink = "", LinkedinLink = "", PdfFile = null, PhoneNumber = "32312321", TechnologiesIds = new int[] { 1 }, }; _fileService.When(x => x.UploadCV(Arg.Any(), Arg.Any())).Do(x => { }); await Assert.ThrowsAsync(async () => await applicantService.ApplyForAd(applyForAdRequestDto)); } [Fact] public async Task ApplyForAd_ApplicantShouldBeCreated_Always() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); ApplyForAdRequestDto applyForAdRequestDto = new() { AdId = 1, BitBucketLink = "", CoverLetter = "saddadas", DateOfBirth = new DateTime(1980, 10, 10), Email = "meris@gmail.com", Experience = 3, FirstName = "Meris", LastName = "Ahmatovic", GithubLink = "", LinkedinLink = "", PdfFile = null, PhoneNumber = "32312321", TechnologiesIds = new int[] { 1 }, }; _fileService.When(x => x.UploadCV(Arg.Any(), Arg.Any())).Do(x => { }); _adService.GetByIdEntityAsync(Arg.Any()).Returns(new Ad { Id = 10, Applicants = new List(), CreatedAt = DateTime.Now, ExpiredAt = DateTime.Now.AddDays(5), MinimumExperience = 1, Title = ".NET Intern", KeyResponsibilities = "dasdadas", Offer = "dsadsada", Requirements = "dsadsadas" }); await applicantService.ApplyForAd(applyForAdRequestDto); var filterDto = MockData.GetApplicantFilters(); var result = await applicantService.GetFilteredApplicants(filterDto); Assert.Equal(3, result.Total); } [Fact] public async Task DeleteApplicant_ShouldDeleteApplicant_WhenApplicantExist() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); await applicantService.DeleteApplicant(1); var filterDto = MockData.GetApplicantFilters(); var applicants = await applicantService.GetFilteredApplicants(filterDto); Assert.Equal(1,applicants.Total); } [Fact] public async Task DeleteApplicant_ShouldThrowEntityNotFooundException_WhenApplicantDoesNotExist() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); await Assert.ThrowsAsync(async () => await applicantService.DeleteApplicant(1000)); } [Fact] public async Task GetOptions_ShouldReturnAllOptions_Always() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); var res = await applicantService.GetOptions(); Assert.Equal(2, res.Count); } [Fact] public async Task InitializeProcess_ShouldReturnError_WhenApplicantDoesNotExist() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); var res = await applicantService.InitializeProcess(new ApplicantProcessRequestDTO { ApplicantId = 1000, Appointment = DateTime.Now, SchedulerId = 1000 }); Assert.True(res.IsError); } [Fact] public async Task InitializeProcess_SelectionProcessShouldBeCreated_WhenApplicantExist() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); var res = await applicantService.InitializeProcess(new ApplicantProcessRequestDTO { ApplicantId = 1, Appointment = DateTime.Now, SchedulerId = 1 }); // before creating new selection process total number of selection proceesses was 6 Assert.Equal(7, databaseContext.SelectionProcesses.Count()); Assert.False(res.IsError); } [Fact] public async Task ImportApplicant_ShouldCreateApplicant_Always() { var databaseContext = await Helpers.GetDatabaseContext(_applicants); _userService.GetFirst().Returns(_users[0]); ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService); var ad = new Ad { Id = 2, Applicants = _applicants, CreatedAt = DateTime.Now, ExpiredAt = DateTime.Now.AddDays(5), MinimumExperience = 1, Title = ".NET Intern", KeyResponsibilities = "dasdadas", Offer = "dsadsada", Requirements = "dsadsadas" }; await applicantService.ImportApplicant(new List { new ApplicantImportDto { Ad = ad, ApplicationChannel = "Facebook", BitBucketLink = "", Comment = "some comment", CV = "name of CV", DateOfApplication = DateTime.Now, Email = "somemail@gmail.com", Experience = 3, FirstName = "Dzenis", LastName = "Hadzifejzovic", GithubLink = "", LinkedlnLink = "", PhoneNumber = "2321312", Position = "some position", TypeOfEmployment = "Intership" } }); Assert.Equal(3, databaseContext.Applicants.Count()); } } }