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.

ApplicantServiceTests.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. Gender = "Muski",
  121. ProfessionalQualification = "Elektrotehincki fakultet"
  122. };
  123. _fileService.When(x => x.UploadCV(Arg.Any<string>(), Arg.Any<IFormFile>())).Do(x => { });
  124. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await
  125. applicantService.ApplyForAd(applyForAdRequestDto));
  126. }
  127. [Fact]
  128. public async Task ApplyForAd_ApplicantShouldBeCreated_Always()
  129. {
  130. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  131. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  132. ApplyForAdRequestDto applyForAdRequestDto = new()
  133. {
  134. AdId = 1,
  135. BitBucketLink = "",
  136. CoverLetter = "saddadas",
  137. DateOfBirth = new DateTime(1980, 10, 10),
  138. Email = "meris@gmail.com",
  139. Experience = 3,
  140. FirstName = "Meris",
  141. LastName = "Ahmatovic",
  142. GithubLink = "",
  143. LinkedinLink = "",
  144. PdfFile = null,
  145. PhoneNumber = "32312321",
  146. TechnologiesIds = new int[] { 1 },
  147. Gender = "Muski",
  148. ProfessionalQualification = "Elektrotehnicki fakultet"
  149. };
  150. _fileService.When(x => x.UploadCV(Arg.Any<string>(), Arg.Any<IFormFile>())).Do(x => { });
  151. _technologyService.GetEntitiesAsync(Arg.Any<int[]>()).Returns(_technologies);
  152. _adService.GetByIdEntityAsync(Arg.Any<int>()).Returns(new Ad
  153. {
  154. Id = 10,
  155. Applicants = new List<Applicant>(),
  156. CreatedAt = DateTime.Now,
  157. ExpiredAt = DateTime.Now.AddDays(5),
  158. MinimumExperience = 1,
  159. Title = ".NET Intern",
  160. KeyResponsibilities = "dasdadas",
  161. Offer = "dsadsada",
  162. Requirements = "dsadsadas"
  163. });
  164. await applicantService.ApplyForAd(applyForAdRequestDto);
  165. var filterDto = MockData.GetApplicantFilters();
  166. var result = await applicantService.GetFilteredApplicants(filterDto);
  167. Assert.Equal(3, result.Total);
  168. }
  169. [Fact]
  170. public async Task DeleteApplicant_ShouldDeleteApplicant_WhenApplicantExist()
  171. {
  172. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  173. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  174. await applicantService.DeleteApplicant(1);
  175. var filterDto = MockData.GetApplicantFilters();
  176. var applicants = await applicantService.GetFilteredApplicants(filterDto);
  177. Assert.Equal(1,applicants.Total);
  178. }
  179. [Fact]
  180. public async Task DeleteApplicant_ShouldThrowEntityNotFooundException_WhenApplicantDoesNotExist()
  181. {
  182. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  183. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  184. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.DeleteApplicant(1000));
  185. }
  186. [Fact]
  187. public async Task GetOptions_ShouldReturnAllOptions_Always()
  188. {
  189. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  190. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  191. var res = await applicantService.GetOptions();
  192. Assert.Equal(2, res.Count);
  193. }
  194. [Fact]
  195. public async Task InitializeProcess_ShouldReturnError_WhenApplicantDoesNotExist()
  196. {
  197. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  198. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  199. var res = await applicantService.InitializeProcess(new ApplicantProcessRequestDTO
  200. {
  201. ApplicantId = 1000,
  202. Appointment = DateTime.Now,
  203. SchedulerId = 1000
  204. });
  205. Assert.True(res.IsError);
  206. }
  207. [Fact]
  208. public async Task InitializeProcess_SelectionProcessShouldBeCreated_WhenApplicantExist()
  209. {
  210. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  211. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  212. var res = await applicantService.InitializeProcess(new ApplicantProcessRequestDTO
  213. {
  214. ApplicantId = 1,
  215. Appointment = DateTime.Now,
  216. SchedulerId = 1
  217. });
  218. // before creating new selection process total number of selection proceesses was 6
  219. Assert.Equal(7, databaseContext.SelectionProcesses.Count());
  220. Assert.False(res.IsError);
  221. }
  222. [Fact]
  223. public async Task ImportApplicant_ShouldCreateApplicant_Always()
  224. {
  225. var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
  226. _userService.GetFirst().Returns(_users[0]);
  227. ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
  228. var ad = new Ad
  229. {
  230. Id = 2,
  231. Applicants = _applicants,
  232. CreatedAt = DateTime.Now,
  233. ExpiredAt = DateTime.Now.AddDays(5),
  234. MinimumExperience = 1,
  235. Title = ".NET Intern",
  236. KeyResponsibilities = "dasdadas",
  237. Offer = "dsadsada",
  238. Requirements = "dsadsadas"
  239. };
  240. await applicantService.ImportApplicant(new List<ApplicantImportDto>
  241. {
  242. new ApplicantImportDto
  243. {
  244. Ad = ad,
  245. ApplicationChannel = "Facebook",
  246. BitBucketLink = "",
  247. Comment = "some comment",
  248. CV = "name of CV",
  249. DateOfApplication = DateTime.Now,
  250. Email = "somemail@gmail.com",
  251. Experience = 3,
  252. FirstName = "Dzenis",
  253. LastName = "Hadzifejzovic",
  254. GithubLink = "",
  255. LinkedlnLink = "",
  256. PhoneNumber = "2321312",
  257. Position = "some position",
  258. TypeOfEmployment = "Intership"
  259. }
  260. });
  261. Assert.Equal(3, databaseContext.Applicants.Count());
  262. }
  263. }
  264. }