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 _ads = new List(); private readonly Ad _ad; private ITechnologyService _technologyService = Substitute.For(); private ILogger _logger = Substitute.For>(); private readonly List _technologies = new List(); public AdServiceTests() { var configuration = new MapperConfiguration(cfg => cfg.AddProfiles( new List { 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 { 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 { 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.GetDatabaseContext(_ads); AdService adService = new(databaseContext, _mapper, _technologyService, _logger); var result = await adService.GetAllAsync(); //result.Should().BeEquivalentTo(_mapper.Map>(_ads)); Assert.Equal(result.Count, 1); } [Fact] public async Task GetById_ShouldReturnAd_WhenAdExists() { var databaseContext = await Helpers.GetDatabaseContext(_ads); AdService adService = new(databaseContext, _mapper, _technologyService, _logger); var result = await adService.GetByIdAsync(1); result.Should().BeEquivalentTo(_mapper.Map(_ad)); } [Fact] public async Task GetById_ShouldThrowEntityNotFoundException_WhenAdDontExists() { var databaseContext = await Helpers.GetDatabaseContext(_ads); AdService adService = new(databaseContext, _mapper, _technologyService, _logger); await Assert.ThrowsAsync(async () => await adService.GetByIdAsync(1000)); } [Fact] public async Task GetAdDetailsById_ShouldReturnAd_WhenAdExists() { var databaseContext = await Helpers.GetDatabaseContext(_ads); AdService adService = new(databaseContext, _mapper, _technologyService, _logger); var result = await adService.GetAdDetailsByIdAsync(1); result.Should().BeEquivalentTo(_mapper.Map(_ad)); } [Fact] public async Task GetAdDetailsById_ShouldThrowEntityNotFoundException_WhenAdDontExists() { var databaseContext = await Helpers.GetDatabaseContext(_ads); AdService adService = new(databaseContext, _mapper, _technologyService, _logger); await Assert.ThrowsAsync(async () => await adService.GetAdDetailsByIdAsync(1000)); } [Fact] public async Task GetArchiveAds_ShouldReturnListOfAds_WhenCalled() { var databaseContext = await Helpers.GetDatabaseContext(_ads); AdService adService = new(databaseContext, _mapper, _technologyService, _logger); var result = await adService.GetArchiveAds(); //result.Should().BeEquivalentTo(_mapper.Map>(_ads)); Assert.Equal(result.Count, 1); } [Fact] public async Task GetFilteredAds_ShouldReturnListOfAds_WhenCalled() { var databaseContext = await Helpers.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.GetDatabaseContext(_ads); AdService adService = new(databaseContext, _mapper, _technologyService, _logger); _technologyService.GetEntityByIdAsync(Arg.Any()).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 { 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.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.GetDatabaseContext(_ads); AdService adService = new(databaseContext, _mapper, _technologyService, _logger); await Assert.ThrowsAsync(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.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.GetDatabaseContext(_ads); AdService adService = new(databaseContext, _mapper, _technologyService, _logger); await Assert.ThrowsAsync(async () => await adService.DeleteAsync(1000)); } } }