| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- 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<IAdService>();
- private List<AdResponseDto> _ads = new List<AdResponseDto>();
- 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<int>()).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<int>())).Do(x => { throw new EntityNotFoundException(); });
-
- AdsController adsController = new(_adService);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(() => adsController.GetById(1000));
- }
-
- [Fact]
- public async Task GetAdDetailsById_ShouldReturn200OK_WhenAdExists()
- {
- _adService.GetAdDetailsByIdAsync(Arg.Any<int>()).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<int>())).Do(x => { throw new EntityNotFoundException(); });
-
- AdsController adsController = new(_adService);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(() => 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<int>(), adUpdateDto)).Do(x => { throw new EntityNotFoundException(); });
-
- AdsController adsController = new(_adService);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(() => 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<int>())).Do(x => { throw new EntityNotFoundException(); });
-
- AdsController adsController = new(_adService);
-
- await Assert.ThrowsAsync<EntityNotFoundException>(() => adsController.DeleteAd(1000));
- }
- }
- }
|