Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ApplicantServiceTests.cs 12KB

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