Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AdsControllerTests.cs 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using Diligent.WebAPI.Contracts.DTOs.Ad;
  2. using Diligent.WebAPI.Contracts.Exceptions;
  3. using Diligent.WebAPI.Data.Entities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace Diligent.WebAPI.Tests.Controllers
  10. {
  11. public class AdsControllerTests
  12. {
  13. private IAdService _adService = Substitute.For<IAdService>();
  14. private List<AdResponseDto> _ads = new List<AdResponseDto>();
  15. private AdResponseDto _ad;
  16. public AdsControllerTests()
  17. {
  18. _ad = new AdResponseDto
  19. {
  20. Id = 1,
  21. Title = "React Developer",
  22. MinimumExperience = 0,
  23. CreatedAt = DateTime.Now,
  24. ExpiredAt = DateTime.Now.AddDays(30),
  25. KeyResponsibilities = "KR|KR",
  26. Requirements = "R|R|R",
  27. Offer = "O|O",
  28. WorkHour = nameof(WorkHours.FullTime),
  29. EmploymentType = nameof(EmploymentTypes.Intership)
  30. };
  31. _ads.Add(_ad);
  32. }
  33. [Fact]
  34. public async Task GetAll_ShouldReturn200OK_WhenCalled()
  35. {
  36. _adService.GetAllAsync().Returns(_ads);
  37. AdsController adsController = new(_adService);
  38. var result = await adsController.GetAll();
  39. (result as OkObjectResult).StatusCode.Should().Be(200);
  40. }
  41. [Fact]
  42. public async Task GetById_ShouldReturn200OK_WhenAdExists()
  43. {
  44. _adService.GetByIdAsync(Arg.Any<int>()).Returns(_ad);
  45. AdsController adsController = new(_adService);
  46. var result = await adsController.GetById(1);
  47. (result as OkObjectResult).StatusCode.Should().Be(200);
  48. }
  49. [Fact]
  50. public async Task GetById_ShouldThrowEntityNotFoundException_WhenAdDontExist()
  51. {
  52. _adService.When(x => x.GetByIdAsync(Arg.Any<int>())).Do(x => { throw new EntityNotFoundException(); });
  53. AdsController adsController = new(_adService);
  54. await Assert.ThrowsAsync<EntityNotFoundException>(() => adsController.GetById(1000));
  55. }
  56. [Fact]
  57. public async Task ArchiveActiveAd_ShouldReturn200OK_WhenAdExists()
  58. {
  59. _adService.ArchiveAdAsync(Arg.Any<int>()).Returns(Task.FromResult(1));
  60. AdsController adsController = new(_adService);
  61. var result = await adsController.ArchiveActiveAd(1);
  62. var res = result as StatusCodeResult;
  63. Assert.Equal(res.StatusCode, 200);
  64. }
  65. [Fact]
  66. public async Task ArchiveActiveAd_ShouldThrowEntityNotFoundException_WhenAdDontExist()
  67. {
  68. _adService.When(x => x.ArchiveAdAsync(Arg.Any<int>())).Do(x => { throw new EntityNotFoundException(); });
  69. AdsController adsController = new(_adService);
  70. await Assert.ThrowsAsync<EntityNotFoundException>(() => adsController.ArchiveActiveAd(10));
  71. }
  72. [Fact]
  73. public async Task GetAdDetailsById_ShouldReturn200OK_WhenAdExists()
  74. {
  75. _adService.GetAdDetailsByIdAsync(Arg.Any<int>()).Returns(new AdDetailsResponseDto
  76. {
  77. Id = 1,
  78. Title = "React Developer",
  79. MinimumExperience = 0,
  80. CreatedAt = DateTime.Now,
  81. ExpiredAt = DateTime.Now.AddDays(30),
  82. KeyResponsibilities = "KR|KR",
  83. Requirements = "R|R|R",
  84. Offer = "O|O"
  85. });
  86. AdsController adsController = new(_adService);
  87. var result = await adsController.GetAdDetailsById(1);
  88. (result as OkObjectResult).StatusCode.Should().Be(200);
  89. }
  90. [Fact]
  91. public async Task GetAdDetailsById_ShouldThrowEntityNotFoundException_WhenAdDontExist()
  92. {
  93. _adService.When(x => x.GetAdDetailsByIdAsync(Arg.Any<int>())).Do(x => { throw new EntityNotFoundException(); });
  94. AdsController adsController = new(_adService);
  95. await Assert.ThrowsAsync<EntityNotFoundException>(() => adsController.GetAdDetailsById(1000));
  96. }
  97. [Fact]
  98. public async Task GetArchiveAds_ShouldReturn200OK_WhenCalled()
  99. {
  100. _adService.GetArchiveAds().Returns(_ads);
  101. AdsController adsController = new(_adService);
  102. var result = await adsController.GetArchiveAds();
  103. (result as OkObjectResult).StatusCode.Should().Be(200);
  104. }
  105. [Fact]
  106. public async Task GetFilteredAds_ShouldReturn200OK_WhenCalled()
  107. {
  108. var filters = new AdFilterDto
  109. {
  110. MinExperience = 0,
  111. MaxExperience = 10,
  112. EmploymentType = "Work",
  113. WorkHour = "FullTime",
  114. Technologies = new string[] { ".NET" }
  115. };
  116. _adService.GetFilteredAdsAsync(filters).Returns(_ads);
  117. AdsController adsController = new(_adService);
  118. var result = await adsController.GetFilteredAds(filters);
  119. (result as OkObjectResult).StatusCode.Should().Be(200);
  120. }
  121. [Fact]
  122. public async Task CreateAd_ShouldReturn201Created_WhenAdCreated()
  123. {
  124. var adCreateDto = new AdCreateDto
  125. {
  126. Title = "React Developer",
  127. MinimumExperience = 0,
  128. CreatedAt = DateTime.Now,
  129. ExpiredAt = DateTime.Now.AddDays(30),
  130. KeyResponsibilities = "KR|KR",
  131. Requirements = "R|R|R",
  132. Offer = "O|O"
  133. };
  134. _adService.CreateAsync(adCreateDto);
  135. AdsController adsController = new(_adService);
  136. var result = await adsController.Create(adCreateDto);
  137. (result as StatusCodeResult).StatusCode.Should().Be(201);
  138. }
  139. [Fact]
  140. public async Task UpdateAd_ShouldReturn200OK_WhenAdUpdated()
  141. {
  142. var adUpdateDto = new AdUpdateDto
  143. {
  144. Title = "React Developer",
  145. MinimumExperience = 0,
  146. CreatedAt = DateTime.Now,
  147. ExpiredAt = DateTime.Now.AddDays(30),
  148. KeyResponsibilities = "KR|KR",
  149. Requirements = "R|R|R",
  150. Offer = "O|O"
  151. };
  152. _adService.UpdateAsync(1, adUpdateDto);
  153. AdsController adsController = new(_adService);
  154. var result = await adsController.Update(adUpdateDto, 1);
  155. (result as StatusCodeResult).StatusCode.Should().Be(200);
  156. }
  157. [Fact]
  158. public async Task UpdateAd_ShouldThrowEntityNotFoundException_WhenAdDontExist()
  159. {
  160. var adUpdateDto = new AdUpdateDto
  161. {
  162. Title = "React Developer",
  163. MinimumExperience = 0,
  164. CreatedAt = DateTime.Now,
  165. ExpiredAt = DateTime.Now.AddDays(30),
  166. KeyResponsibilities = "KR|KR",
  167. Requirements = "R|R|R",
  168. Offer = "O|O"
  169. };
  170. _adService.When(x => x.UpdateAsync(Arg.Any<int>(), adUpdateDto)).Do(x => { throw new EntityNotFoundException(); });
  171. AdsController adsController = new(_adService);
  172. await Assert.ThrowsAsync<EntityNotFoundException>(() => adsController.Update(adUpdateDto, 1000));
  173. }
  174. [Fact]
  175. public async Task DeleteAd_ShouldReturn204NoContent_WhenAdDeleted()
  176. {
  177. _adService.DeleteAsync(1);
  178. AdsController adsController = new(_adService);
  179. var result = await adsController.DeleteAd(1);
  180. (result as StatusCodeResult).StatusCode.Should().Be(204);
  181. }
  182. [Fact]
  183. public async Task DeleteAd_ShouldThrowEntityNotFoundException_WhenAdDontExist()
  184. {
  185. _adService.When(x => x.DeleteAsync(Arg.Any<int>())).Do(x => { throw new EntityNotFoundException(); });
  186. AdsController adsController = new(_adService);
  187. await Assert.ThrowsAsync<EntityNotFoundException>(() => adsController.DeleteAd(1000));
  188. }
  189. }
  190. }