| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- using AutoMapper;
- using Diligent.WebAPI.Business.MappingProfiles;
- using Diligent.WebAPI.Business.Services;
- using Diligent.WebAPI.Contracts.DTOs.Ad;
- using Diligent.WebAPI.Contracts.Exceptions;
- using Diligent.WebAPI.Data.Entities;
- using Microsoft.Extensions.Logging;
-
- namespace Diligent.WebAPI.Tests.Services
- {
- public class AdServiceTests
- {
- private readonly IMapper _mapper;
- private readonly List<Ad> _ads = new List<Ad>();
- private readonly Ad _ad;
- private ITechnologyService _technologyService = Substitute.For<ITechnologyService>();
- private ILogger<AdService> _logger = Substitute.For<ILogger<AdService>>();
- private readonly List<Technology> _technologies = new List<Technology>();
-
- public AdServiceTests()
- {
- var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
- new List<Profile>
- {
- new AdMappingProfile(),
- new TechnologyMappingProfile()
- }));
- _mapper = new Mapper(configuration);
-
- _ad = new Ad
- {
-
- Id = 1,
- Title = "React Developer",
- MinimumExperience = 0,
- CreatedAt = DateTime.Now,
- ExpiredAt = DateTime.Now.AddDays(30),
- KeyResponsibilities = "KR|KR",
- Requirements = "R|R|R",
- Offer = "O|O",
- WorkHour = WorkHours.FullTime,
- EmploymentType = EmploymentTypes.Intership,
- Technologies = new List<Technology>
- {
- new Technology { TechnologyId = 1, Name = "React", TechnologyType = TechnologyTypes.Backend}
- }
- };
-
- _ads.Add(_ad);
- _ads.Add(new Ad
- {
- Id = 2,
- Title = ".NET Developer",
- MinimumExperience = 0,
- CreatedAt = DateTime.Now.AddDays(-2),
- ExpiredAt = DateTime.Now.AddDays(-1),
- KeyResponsibilities = "KR|KR",
- Requirements = "R|R|R",
- Offer = "O|O",
- WorkHour = WorkHours.FullTime,
- EmploymentType = EmploymentTypes.Intership,
- Technologies = new List<Technology>
- {
- new Technology { TechnologyId = 2, Name = ".NET", TechnologyType = TechnologyTypes.Frontend}
- }
- });
-
- _technologies.Add(new Technology
- {
- TechnologyId = 1,
- Name = ".NET"
- });
- }
-
- [Fact]
- public async Task GetAll_ShouldReturnListOfAds_WhenCalled()
- {
- var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
- AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
-
- var result = await adService.GetAllAsync();
-
- //result.Should().BeEquivalentTo(_mapper.Map<List<AdResponseDto>>(_ads));
- Assert.Equal(result.Count, 1);
- }
-
- [Fact]
- public async Task GetById_ShouldReturnAd_WhenAdExists()
- {
- var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
- AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
-
- var result = await adService.GetByIdAsync(1);
-
- result.Should().BeEquivalentTo(_mapper.Map<AdResponseDto>(_ad));
- }
-
- [Fact]
- public async Task GetById_ShouldThrowEntityNotFoundException_WhenAdDontExists()
- {
- var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
- AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.GetByIdAsync(1000));
- }
-
- [Fact]
- public async Task GetAdDetailsById_ShouldReturnAd_WhenAdExists()
- {
- var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
- AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
-
- var result = await adService.GetAdDetailsByIdAsync(1);
-
- result.Should().BeEquivalentTo(_mapper.Map<AdDetailsResponseDto>(_ad));
- }
-
- [Fact]
- public async Task GetAdDetailsById_ShouldThrowEntityNotFoundException_WhenAdDontExists()
- {
- var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
- AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.GetAdDetailsByIdAsync(1000));
- }
-
- [Fact]
- public async Task GetArchiveAds_ShouldReturnListOfAds_WhenCalled()
- {
- var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
- AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
-
- var result = await adService.GetArchiveAds();
-
- //result.Should().BeEquivalentTo(_mapper.Map<List<AdResponseDto>>(_ads));
- Assert.Equal(result.Count, 1);
- }
-
- [Fact]
- public async Task GetFilteredAds_ShouldReturnListOfAds_WhenCalled()
- {
- var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
- AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
-
- var result = await adService.GetFilteredAdsAsync(new AdFilterDto
- {
- MinExperience = 0,
- MaxExperience = 10,
- EmploymentType = "Intership",
- WorkHour = "FullTime",
- Technologies = new string[]
- {
- ".NET"
- }
- });
-
- Assert.Equal(result.Count, 1);
- }
-
- [Fact]
- public async Task CreateAd_ShouldAddAdIntoDatabase_WhenCalled()
- {
- var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
- AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
- _technologyService.GetEntityByIdAsync(Arg.Any<int>()).Returns(new Technology
- {
- TechnologyId = 3,
- Name = "Vue"
- });
-
- AdCreateDto adCreateDto = new AdCreateDto
- {
- Title = "Vue Developer",
- MinimumExperience = 0,
- CreatedAt = DateTime.Now,
- ExpiredAt = DateTime.Now.AddDays(30),
- KeyResponsibilities = "KR|KR",
- Requirements = "R|R|R",
- Offer = "O|O",
- WorkHour = "FullTime",
- EmploymentType = "Intership",
- TechnologiesIds = new List<int> { 3 }
- };
-
- await adService.CreateAsync(adCreateDto);
-
- var ads = await adService.GetAllAsync();
-
- Assert.Equal(2, ads.Count);
- }
-
- [Fact]
- public async Task UpdateAd_ShouldUpdateAd_WhenAdExists()
- {
- var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
- AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
-
- var updateForAd = new AdUpdateDto
- {
- Title = "Vue Developer",
- MinimumExperience = 0,
- CreatedAt = DateTime.Now,
- ExpiredAt = DateTime.Now.AddDays(30),
- KeyResponsibilities = "KR|KR",
- Requirements = "R|R|R",
- Offer = "O|O",
- WorkHour = "FullTime",
- EmploymentType = "Intership",
- };
-
- await adService.UpdateAsync(1, updateForAd);
-
- var ads = await adService.GetAllAsync();
-
- Assert.Equal(ads[0].Title, "Vue Developer");
- }
-
- [Fact]
- public async Task UpdateAd_ShouldThrowEntityNotFoundException_WhenAdDontExists()
- {
- var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
- AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.UpdateAsync(1000, new AdUpdateDto
- {
- Title = "Vue Developer",
- MinimumExperience = 0,
- CreatedAt = DateTime.Now,
- ExpiredAt = DateTime.Now.AddDays(30),
- KeyResponsibilities = "KR|KR",
- Requirements = "R|R|R",
- Offer = "O|O",
- WorkHour = "FullTime",
- EmploymentType = "Intership",
- }));
- }
-
- [Fact]
- public async Task DeleteAd_ShouldDeleteAdFromDatabase_WhenAdExists()
- {
- var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
- AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
-
- await adService.DeleteAsync(1);
-
- var ads = await adService.GetAllAsync();
-
- Assert.Equal(0, ads.Count);
- }
-
- [Fact]
- public async Task DeleteAd_ShouldThrowEntityNotFoundException_WhenAdDontExists()
- {
- var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
- AdService adService = new(databaseContext, _mapper, _technologyService, _logger);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(async () => await adService.DeleteAsync(1000));
- }
- }
- }
|