| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- 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<ApplicantService> _logger = Substitute.For<ILogger<ApplicantService>>();
- private readonly IUserService _userService = Substitute.For<IUserService>();
- private readonly IFileService _fileService = Substitute.For<IFileService>();
- private readonly IAdService _adService = Substitute.For<IAdService>();
- private readonly List<Applicant> _applicants;
- private readonly List<Ad> _ads;
- private readonly List<User> _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<Profile>
- {
- new ApplicantMappingProfile(),
- new AdMappingProfile(),
- new SelectionProcessMappingProfile()
- }));
- _mapper = new Mapper(configuration);
- }
-
- [Fact]
- public async Task GetFilteredApplicants_ShouldReturnOneApplicant_Always()
- {
- var databaseContext = await Helpers<Applicant>.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<Ad>.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<List<AdApplicantsViewDto>>(_ads));
- }
-
- [Fact]
- public async Task GetById_ShouldReturnApplicant_WhenApplicantExist()
- {
- var fileInBase64Format = "some string";
- var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
- ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
- _fileService.GetCV(Arg.Any<string>()).Returns(fileInBase64Format);
-
- var result = await applicantService.GetById(1);
- _applicants[0].CV = fileInBase64Format;
-
- result.Should().BeEquivalentTo(_mapper.Map<ApplicantViewDto>(_applicants[0]));
- }
-
- [Fact]
- public async Task GetById_ShouldThrowEntityNotFoundException_WhenApplicantDoesNotExist()
- {
- var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
- ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.GetById(1000));
- }
-
- [Fact]
- public async Task GetApplicantWithSelectionProcessesById_ShouldReturnApplicant_WhenApplicantExists()
- {
- var databaseContext = await Helpers<Applicant>.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<ApplicantViewDto>(_applicants[0]));
- }
-
- [Fact]
- public async Task GetApplicantWithSelectionProcessesById_ShouldThrowEntityNotFoundException_WhenApplicantDoesNotExist()
- {
- var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
- ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(async () => await
- applicantService.GetApplicantWithSelectionProcessesById(1000));
- }
-
- [Fact]
- public async Task ApplyForAd_ShouldThrowEntityNotFooundException_WhenAdIsNotFound()
- {
- var databaseContext = await Helpers<Applicant>.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<string>(), Arg.Any<IFormFile>())).Do(x => { });
-
- await Assert.ThrowsAsync<EntityNotFoundException>(async () => await
- applicantService.ApplyForAd(applyForAdRequestDto));
- }
-
- [Fact]
- public async Task ApplyForAd_ApplicantShouldBeCreated_Always()
- {
- var databaseContext = await Helpers<Applicant>.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<string>(), Arg.Any<IFormFile>())).Do(x => { });
- _adService.GetByIdEntityAsync(Arg.Any<int>()).Returns(new Ad
- {
- Id = 10,
- Applicants = new List<Applicant>(),
- 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<Applicant>.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<Applicant>.GetDatabaseContext(_applicants);
- ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.DeleteApplicant(1000));
- }
-
- [Fact]
- public async Task GetOptions_ShouldReturnAllOptions_Always()
- {
- var databaseContext = await Helpers<Applicant>.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<Applicant>.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<Applicant>.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<Applicant>.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<ApplicantImportDto>
- {
- 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());
-
- }
-
- }
- }
|