using Diligent.WebAPI.Contracts.DTOs.Ad; using Diligent.WebAPI.Contracts.Exceptions; using Diligent.WebAPI.Data.Entities; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Diligent.WebAPI.Tests.Controllers { public class AdsControllerTests { private IAdService _adService = Substitute.For(); private List _ads = new List(); private AdResponseDto _ad; public AdsControllerTests() { _ad = new AdResponseDto { Id = 1, Title = "React Developer", MinimumExperience = 0, CreatedAt = DateTime.UtcNow, ExpiredAt = DateTime.UtcNow.AddDays(30), KeyResponsibilities = "KR|KR", Requirements = "R|R|R", Offer = "O|O", WorkHour = nameof(WorkHours.FullTime), EmploymentType = nameof(EmploymentTypes.Intership) }; _ads.Add(_ad); } [Fact] public async Task GetAll_ShouldReturn200OK_WhenCalled() { _adService.GetAllAsync().Returns(_ads); AdsController adsController = new(_adService); var result = await adsController.GetAll(); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task GetById_ShouldReturn200OK_WhenAdExists() { _adService.GetByIdAsync(Arg.Any()).Returns(_ad); AdsController adsController = new(_adService); var result = await adsController.GetById(1); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task GetById_ShouldThrowEntityNotFoundException_WhenAdDontExist() { _adService.When(x => x.GetByIdAsync(Arg.Any())).Do(x => { throw new EntityNotFoundException(); }); AdsController adsController = new(_adService); await Assert.ThrowsAsync(() => adsController.GetById(1000)); } [Fact] public async Task GetAdDetailsById_ShouldReturn200OK_WhenAdExists() { _adService.GetAdDetailsByIdAsync(Arg.Any()).Returns(new AdDetailsResponseDto { Id = 1, Title = "React Developer", MinimumExperience = 0, CreatedAt = DateTime.UtcNow, ExpiredAt = DateTime.UtcNow.AddDays(30), KeyResponsibilities = "KR|KR", Requirements = "R|R|R", Offer = "O|O" }); AdsController adsController = new(_adService); var result = await adsController.GetAdDetailsById(1); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task GetAdDetailsById_ShouldThrowEntityNotFoundException_WhenAdDontExist() { _adService.When(x => x.GetAdDetailsByIdAsync(Arg.Any())).Do(x => { throw new EntityNotFoundException(); }); AdsController adsController = new(_adService); await Assert.ThrowsAsync(() => adsController.GetAdDetailsById(1000)); } [Fact] public async Task GetArchiveAds_ShouldReturn200OK_WhenCalled() { _adService.GetArchiveAds().Returns(_ads); AdsController adsController = new(_adService); var result = await adsController.GetArchiveAds(); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task GetFilteredAds_ShouldReturn200OK_WhenCalled() { var filters = new AdFilterDto { MinExperience = 0, MaxExperience = 10, EmploymentType = "Work", WorkHour = "FullTime", Technologies = new string[] { ".NET" } }; _adService.GetFilteredAdsAsync(filters).Returns(_ads); AdsController adsController = new(_adService); var result = await adsController.GetFilteredAds(filters); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task CreateAd_ShouldReturn201Created_WhenAdCreated() { var adCreateDto = new AdCreateDto { Title = "React Developer", MinimumExperience = 0, CreatedAt = DateTime.UtcNow, ExpiredAt = DateTime.UtcNow.AddDays(30), KeyResponsibilities = "KR|KR", Requirements = "R|R|R", Offer = "O|O" }; _adService.CreateAsync(adCreateDto); AdsController adsController = new(_adService); var result = await adsController.Create(adCreateDto); (result as StatusCodeResult).StatusCode.Should().Be(201); } [Fact] public async Task UpdateAd_ShouldReturn200OK_WhenAdUpdated() { var adUpdateDto = new AdUpdateDto { Title = "React Developer", MinimumExperience = 0, CreatedAt = DateTime.UtcNow, ExpiredAt = DateTime.UtcNow.AddDays(30), KeyResponsibilities = "KR|KR", Requirements = "R|R|R", Offer = "O|O" }; _adService.UpdateAsync(1, adUpdateDto); AdsController adsController = new(_adService); var result = await adsController.Update(adUpdateDto, 1); (result as StatusCodeResult).StatusCode.Should().Be(200); } [Fact] public async Task UpdateAd_ShouldThrowEntityNotFoundException_WhenAdDontExist() { var adUpdateDto = new AdUpdateDto { Title = "React Developer", MinimumExperience = 0, CreatedAt = DateTime.UtcNow, ExpiredAt = DateTime.UtcNow.AddDays(30), KeyResponsibilities = "KR|KR", Requirements = "R|R|R", Offer = "O|O" }; _adService.When(x => x.UpdateAsync(Arg.Any(), adUpdateDto)).Do(x => { throw new EntityNotFoundException(); }); AdsController adsController = new(_adService); await Assert.ThrowsAsync(() => adsController.Update(adUpdateDto, 1000)); } [Fact] public async Task DeleteAd_ShouldReturn204NoContent_WhenAdDeleted() { _adService.DeleteAsync(1); AdsController adsController = new(_adService); var result = await adsController.DeleteAd(1); (result as StatusCodeResult).StatusCode.Should().Be(204); } [Fact] public async Task DeleteAd_ShouldThrowEntityNotFoundException_WhenAdDontExist() { _adService.When(x => x.DeleteAsync(Arg.Any())).Do(x => { throw new EntityNotFoundException(); }); AdsController adsController = new(_adService); await Assert.ThrowsAsync(() => adsController.DeleteAd(1000)); } } }