Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ApplicantServiceTests.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using AutoMapper;
  2. using Diligent.WebAPI.Business.MappingProfiles;
  3. using Diligent.WebAPI.Business.Services;
  4. using Diligent.WebAPI.Contracts.DTOs.Ad;
  5. using Diligent.WebAPI.Contracts.DTOs.Applicant;
  6. using Diligent.WebAPI.Contracts.Exceptions;
  7. using Diligent.WebAPI.Data.Entities;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.Extensions.Logging;
  10. namespace Diligent.WebAPI.Tests.Services
  11. {
  12. public class ApplicantServiceTests
  13. {
  14. private readonly IMapper _mapper;
  15. private ILogger<ApplicantService> _logger = Substitute.For<ILogger<ApplicantService>>();
  16. private readonly IUserService _userService = Substitute.For<IUserService>();
  17. private readonly IFileService _fileService = Substitute.For<IFileService>();
  18. private readonly IAdService _adService = Substitute.For<IAdService>();
  19. private readonly ITechnologyService _technologyService = Substitute.For<ITechnologyService>();
  20. private readonly List<Applicant> _applicants;
  21. private readonly List<Ad> _ads;
  22. private readonly List<User> _users;
  23. private readonly List<Technology> _technologies;
  24. public ApplicantServiceTests()
  25. {
  26. // mock data
  27. _applicants = MockData.GetListOfApplicants();
  28. _ads = MockData.GetListOfAds();
  29. _users = MockData.GetListOfUsers();
  30. _technologies = MockData.GetListOfTechnologies();
  31. // configure mapper
  32. var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
  33. new List<Profile>
  34. {
  35. new ApplicantMappingProfile(),
  36. new AdMappingProfile(),
  37. new SelectionProcessMappingProfile(),
  38. new TechnologyMappingProfile()
  39. }));
  40. _mapper = new Mapper(configuration);
  41. }
  42. [Fact]
  43. public async Task GetFilteredApplicants_ShouldReturnOneApplicant_Always()
  44. {
  45. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  46. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  47. var filterDto = new ApplicantFilterDto
  48. {
  49. MinExperience = 2,
  50. MaxExperience = 4
  51. };
  52. var result = await applicantService.GetFilteredApplicants(filterDto);
  53. Assert.Equal(1,result.Total);
  54. }
  55. [Fact]
  56. public async Task GetAllAdsApplicants_ShouldReturnAllApplicants_Always()
  57. {
  58. var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
  59. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  60. var filterDto = MockData.GetApplicantFilters();
  61. var result = await applicantService.GetAllAdsApplicants(filterDto);
  62. result.Should().BeEquivalentTo(_mapper.Map<List<AdApplicantsViewDto>>(_ads));
  63. }
  64. [Fact]
  65. public async Task GetById_ShouldReturnApplicant_WhenApplicantExist()
  66. {
  67. var fileInBase64Format = "some string";
  68. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  69. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  70. _fileService.GetCV(Arg.Any<string>()).Returns(fileInBase64Format);
  71. var result = await applicantService.GetById(1);
  72. _applicants[0].CV = fileInBase64Format;
  73. result.Should().BeEquivalentTo(_mapper.Map<ApplicantViewDto>(_applicants[0]));
  74. }
  75. [Fact]
  76. public async Task GetById_ShouldThrowEntityNotFoundException_WhenApplicantDoesNotExist()
  77. {
  78. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  79. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  80. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.GetById(1000));
  81. }
  82. [Fact]
  83. public async Task GetApplicantWithSelectionProcessesById_ShouldReturnApplicant_WhenApplicantExists()
  84. {
  85. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  86. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  87. var result = await applicantService.GetApplicantWithSelectionProcessesById(1);
  88. var processes = result.SelectionProcesses;
  89. processes.Should().HaveCount(3);
  90. result.Should().BeEquivalentTo(_mapper.Map<ApplicantViewDto>(_applicants[0]));
  91. }
  92. [Fact]
  93. public async Task GetApplicantWithSelectionProcessesById_ShouldThrowEntityNotFoundException_WhenApplicantDoesNotExist()
  94. {
  95. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  96. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  97. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await
  98. applicantService.GetApplicantWithSelectionProcessesById(1000));
  99. }
  100. [Fact]
  101. public async Task ApplyForAd_ShouldThrowEntityNotFooundException_WhenAdIsNotFound()
  102. {
  103. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  104. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  105. ApplyForAdRequestDto applyForAdRequestDto = new()
  106. {
  107. AdId = 1,
  108. BitBucketLink = "",
  109. CoverLetter = "saddadas",
  110. DateOfBirth = new DateTime(1980, 10, 10),
  111. Email = "meris@gmail.com",
  112. Experience = 3,
  113. FirstName = "Meris",
  114. LastName = "Ahmatovic",
  115. GithubLink = "",
  116. LinkedinLink = "",
  117. PdfFile = null,
  118. PhoneNumber = "32312321",
  119. TechnologiesIds = new int[] { 1 },
  120. };
  121. _fileService.When(x => x.UploadCV(Arg.Any<string>(), Arg.Any<IFormFile>())).Do(x => { });
  122. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await
  123. applicantService.ApplyForAd(applyForAdRequestDto));
  124. }
  125. [Fact]
  126. public async Task ApplyForAd_ApplicantShouldBeCreated_Always()
  127. {
  128. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  129. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  130. ApplyForAdRequestDto applyForAdRequestDto = new()
  131. {
  132. AdId = 1,
  133. BitBucketLink = "",
  134. CoverLetter = "saddadas",
  135. DateOfBirth = new DateTime(1980, 10, 10),
  136. Email = "meris@gmail.com",
  137. Experience = 3,
  138. FirstName = "Meris",
  139. LastName = "Ahmatovic",
  140. GithubLink = "",
  141. LinkedinLink = "",
  142. PdfFile = null,
  143. PhoneNumber = "32312321",
  144. TechnologiesIds = new int[] { 1 },
  145. };
  146. _fileService.When(x => x.UploadCV(Arg.Any<string>(), Arg.Any<IFormFile>())).Do(x => { });
  147. _technologyService.GetEntitiesAsync(Arg.Any<int[]>()).Returns(_technologies);
  148. _adService.GetByIdEntityAsync(Arg.Any<int>()).Returns(new Ad
  149. {
  150. Id = 10,
  151. Applicants = new List<Applicant>(),
  152. CreatedAt = DateTime.Now,
  153. ExpiredAt = DateTime.Now.AddDays(5),
  154. MinimumExperience = 1,
  155. Title = ".NET Intern",
  156. KeyResponsibilities = "dasdadas",
  157. Offer = "dsadsada",
  158. Requirements = "dsadsadas"
  159. });
  160. await applicantService.ApplyForAd(applyForAdRequestDto);
  161. var filterDto = MockData.GetApplicantFilters();
  162. var result = await applicantService.GetFilteredApplicants(filterDto);
  163. Assert.Equal(3, result.Total);
  164. }
  165. [Fact]
  166. public async Task DeleteApplicant_ShouldDeleteApplicant_WhenApplicantExist()
  167. {
  168. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  169. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  170. await applicantService.DeleteApplicant(1);
  171. var filterDto = MockData.GetApplicantFilters();
  172. var applicants = await applicantService.GetFilteredApplicants(filterDto);
  173. Assert.Equal(1,applicants.Total);
  174. }
  175. [Fact]
  176. public async Task DeleteApplicant_ShouldThrowEntityNotFooundException_WhenApplicantDoesNotExist()
  177. {
  178. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  179. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  180. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.DeleteApplicant(1000));
  181. }
  182. [Fact]
  183. public async Task GetOptions_ShouldReturnAllOptions_Always()
  184. {
  185. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  186. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  187. var res = await applicantService.GetOptions();
  188. Assert.Equal(2, res.Count);
  189. }
  190. [Fact]
  191. public async Task InitializeProcess_ShouldReturnError_WhenApplicantDoesNotExist()
  192. {
  193. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  194. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  195. var res = await applicantService.InitializeProcess(new ApplicantProcessRequestDTO
  196. {
  197. ApplicantId = 1000,
  198. Appointment = DateTime.Now,
  199. SchedulerId = 1000
  200. });
  201. Assert.True(res.IsError);
  202. }
  203. [Fact]
  204. public async Task InitializeProcess_SelectionProcessShouldBeCreated_WhenApplicantExist()
  205. {
  206. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  207. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  208. var res = await applicantService.InitializeProcess(new ApplicantProcessRequestDTO
  209. {
  210. ApplicantId = 1,
  211. Appointment = DateTime.Now,
  212. SchedulerId = 1
  213. });
  214. // before creating new selection process total number of selection proceesses was 6
  215. Assert.Equal(7, databaseContext.SelectionProcesses.Count());
  216. Assert.False(res.IsError);
  217. }
  218. [Fact]
  219. public async Task ImportApplicant_ShouldCreateApplicant_Always()
  220. {
  221. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  222. _userService.GetFirst().Returns(_users[0]);
  223. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  224. var ad = new Ad
  225. {
  226. Id = 2,
  227. Applicants = _applicants,
  228. CreatedAt = DateTime.Now,
  229. ExpiredAt = DateTime.Now.AddDays(5),
  230. MinimumExperience = 1,
  231. Title = ".NET Intern",
  232. KeyResponsibilities = "dasdadas",
  233. Offer = "dsadsada",
  234. Requirements = "dsadsadas"
  235. };
  236. await applicantService.ImportApplicant(new List<ApplicantImportDto>
  237. {
  238. new ApplicantImportDto
  239. {
  240. Ad = ad,
  241. ApplicationChannel = "Facebook",
  242. BitBucketLink = "",
  243. Comment = "some comment",
  244. CV = "name of CV",
  245. DateOfApplication = DateTime.Now,
  246. Email = "somemail@gmail.com",
  247. Experience = 3,
  248. FirstName = "Dzenis",
  249. LastName = "Hadzifejzovic",
  250. GithubLink = "",
  251. LinkedlnLink = "",
  252. PhoneNumber = "2321312",
  253. Position = "some position",
  254. TypeOfEmployment = "Intership"
  255. }
  256. });
  257. Assert.Equal(3, databaseContext.Applicants.Count());
  258. }
  259. }
  260. }