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.

PatternServiceTests.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using AutoMapper;
  2. using Castle.Core.Logging;
  3. using Diligent.WebAPI.Business.MappingProfiles;
  4. using Diligent.WebAPI.Business.Services;
  5. using Diligent.WebAPI.Business.Services.Interfaces;
  6. using Diligent.WebAPI.Contracts.DTOs.Ad;
  7. using Diligent.WebAPI.Contracts.DTOs.Pattern;
  8. using Diligent.WebAPI.Contracts.Exceptions;
  9. using Diligent.WebAPI.Data.Entities;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.Extensions.Logging;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. namespace Diligent.WebAPI.Tests.Services
  18. {
  19. public class PatternServiceTests
  20. {
  21. private readonly IMapper _mapper;
  22. private readonly List<Pattern> _patterns = new();
  23. private readonly Pattern _pattern;
  24. private ISelectionLevelService _selectionLevelService = Substitute.For<ISelectionLevelService>();
  25. private ILogger<PatternService> _logger = Substitute.For<ILogger<PatternService>>();
  26. private IEmailer _emailService = Substitute.For<IEmailer>();
  27. private ISelectionProcessService _selectionProcessService = Substitute.For<ISelectionProcessService>();
  28. public PatternServiceTests()
  29. {
  30. var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(new List<Profile>()
  31. {
  32. new PatternMappingProfile(),
  33. new SelectionLevelMappingProfile(),
  34. new SelectionProcessMappingProfile(),
  35. new ApplicantMappingProfile()
  36. }));
  37. _mapper = new Mapper(configuration);
  38. _pattern = new Pattern
  39. {
  40. Id = 1,
  41. Title = "Zakazivanje termina",
  42. CreatedAt = DateTime.Now,
  43. SelectionLevelId = 2000,
  44. SelectionLevel = new SelectionLevel
  45. {
  46. Id = 2000,
  47. Name = "SelLevel",
  48. SelectionProcesses = new List<SelectionProcess>
  49. {
  50. new SelectionProcess
  51. {
  52. Id = 1234,
  53. Name = "Zakazan termin",
  54. Status = "Čeka na zakazivanje",
  55. SelectionLevelId = 4321,
  56. ApplicantId = 2321,
  57. Applicant = new Applicant
  58. {
  59. ApplicantId= 2321,
  60. FirstName = "Ermin",
  61. LastName = "Bronja",
  62. Position = "Developer",
  63. DateOfApplication = DateTime.Now,
  64. CV = "",
  65. Email = "ermin.bronja@dilig.net",
  66. PhoneNumber = "",
  67. LinkedlnLink = "",
  68. GithubLink = "",
  69. BitBucketLink = "",
  70. Experience = 1,
  71. ApplicationChannel = "",
  72. TypeOfEmployment = Applicant.TypesOfEmployment.Posao,
  73. Gender = Applicant.Genders.M,
  74. ProfessionalQualification = "Elektrotehnicki fakultet"
  75. },
  76. Comment = "Komentar"
  77. }
  78. }
  79. },
  80. Message = "Poruka"
  81. };
  82. _patterns.Add(_pattern);
  83. }
  84. [Fact]
  85. public async Task GetAll_ShouldReturnListOfPatterns_WhenCalled()
  86. {
  87. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  88. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  89. var result = await patternService.GetAllAsync();
  90. Assert.Equal(result.Count, 1);
  91. }
  92. [Fact]
  93. public async Task GetById_ShouldReturnPattern_WhenAdExists()
  94. {
  95. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  96. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  97. var result = await patternService.GetByIdAsync(1);
  98. result.Should().BeEquivalentTo(_mapper.Map<PatternResponseDto>(_pattern));
  99. }
  100. [Fact]
  101. public async Task GetById_ShouldThrowEntityNotFoundException_WhenPatternDontExists()
  102. {
  103. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  104. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  105. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.GetByIdAsync(1000));
  106. }
  107. [Fact]
  108. public async Task GetFilteredPatterns_ShouldReturnListOfPatterns_WhenCalled()
  109. {
  110. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  111. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  112. var result = await patternService.GetFilteredPatternsAsync(new FilterPatternDto
  113. {
  114. FromDate = new DateTime(2022, 11, 30),
  115. ToDate = DateTime.Now,
  116. SelectionLevels = null
  117. });
  118. Assert.Equal(result.Count, 1);
  119. }
  120. [Fact]
  121. public async Task GetCorrespondingPatternApplicants_ShouldReturnListOfApplicants_WhenCalled()
  122. {
  123. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  124. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  125. var result = await patternService.GetCorrespondingPatternApplicants(1);
  126. Assert.Equal(result.Count, 1);
  127. }
  128. [Fact]
  129. public async Task GetCorrespondingPatternApplicants_ShouldThrowEntityNotFoundException_WhenPatternDontExists()
  130. {
  131. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  132. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  133. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.GetCorrespondingPatternApplicants(2));
  134. }
  135. [Fact]
  136. public async Task CreatePattern_ShouldThrowEntityNotFoundException_WhenPatternExist()
  137. {
  138. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  139. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  140. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.CreateAsync(new PatternCreateDto
  141. {
  142. Title = "Zakazivanje termina",
  143. CreatedAt = DateTime.Now,
  144. SelectionLevelId = 2000,
  145. Message = "Poruka",
  146. }));
  147. }
  148. [Fact]
  149. public async Task CreatePattern_ShouldThrowEntityNotFoundException_WhenSelectionLevelIsNull()
  150. {
  151. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  152. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  153. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.CreateAsync(new PatternCreateDto
  154. {
  155. Title = "Zakazan termin",
  156. CreatedAt = DateTime.Now,
  157. SelectionLevelId = 2323,
  158. Message = "Poruka",
  159. }));
  160. }
  161. [Fact]
  162. public async Task CreatePattern_ShouldCreatePattern_WhenConditionsAreFullfiled()
  163. {
  164. _selectionLevelService.GetByIdEntity(Arg.Any<int>()).Returns(new SelectionLevel
  165. {
  166. Id = 1122,
  167. Name = "Test",
  168. SelectionProcesses = new List<SelectionProcess>()
  169. });
  170. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  171. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  172. await patternService.CreateAsync(new PatternCreateDto
  173. {
  174. Title = "Zakazan termin",
  175. CreatedAt = DateTime.Now,
  176. SelectionLevelId = 1122,
  177. Message = "Poruka",
  178. });
  179. var result = await patternService.GetAllAsync();
  180. Assert.Equal(2, result.Count);
  181. }
  182. [Fact]
  183. public async Task ScheduleInterview_ShouldThrowEntityNotFoundException_WhenWhenPatternDoesNotExist()
  184. {
  185. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  186. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  187. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.ScheduleIntrviewAsync(new ScheduleInterviewDto
  188. {
  189. Emails = new List<string>
  190. {
  191. "ermin.bronja@dilig.net"
  192. },
  193. PatternId = 2
  194. }));
  195. }
  196. [Fact]
  197. public async Task ScheduleInterview_ShouldScheduleInterview_WhenConditionsAreFullfiled()
  198. {
  199. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  200. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  201. var result = await patternService.ScheduleIntrviewAsync(new ScheduleInterviewDto
  202. {
  203. Emails = new List<string>
  204. {
  205. "ermin.bronja@dilig.net"
  206. },
  207. PatternId = 1
  208. });
  209. Assert.Equal(null, result);
  210. }
  211. [Fact]
  212. public async Task ScheduleInterview_ShouldReturnListOfNotSentEmails_WhenEmailsDontExistOrStatusAreNotCorrectInProcesses()
  213. {
  214. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  215. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  216. var result = await patternService.ScheduleIntrviewAsync(new ScheduleInterviewDto
  217. {
  218. Emails = new List<string>
  219. {
  220. "meris.ahmatovic@dilig.net"
  221. },
  222. PatternId = 1
  223. });
  224. Assert.NotEqual(null, result);
  225. }
  226. [Fact]
  227. public async Task UpdatePattern_ShouldThrowEntityNotFoundException_WhenWhenPatternDoesNotExist()
  228. {
  229. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  230. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  231. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.UpdateAsync(new PatternUpdateDto
  232. {
  233. CreatedAt = DateTime.Now,
  234. Message = "Poruka 1",
  235. SelectionLevelId = 2000,
  236. Title = "Naslov"
  237. }, 2));
  238. }
  239. [Fact]
  240. public async Task UpdatePattern_ShouldThrowEntityNotFoundException_WhenTitleAndPatternSelectionLevelAreNotChanged()
  241. {
  242. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  243. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  244. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.UpdateAsync(new PatternUpdateDto
  245. {
  246. CreatedAt = DateTime.Now,
  247. Message = "Poruka 1",
  248. SelectionLevelId = 2000,
  249. Title = "Zakazivanje termina"
  250. }, 1));
  251. }
  252. [Fact]
  253. public async Task UpdatePattern_ShouldThrowEntityNotFoundException_WhenSelectionLevelDoesNotExistInDatabase()
  254. {
  255. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  256. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  257. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.UpdateAsync(new PatternUpdateDto
  258. {
  259. CreatedAt = DateTime.Now,
  260. Message = "Poruka 1",
  261. SelectionLevelId = 2001,
  262. Title = "Zakazan Termin"
  263. }, 1));
  264. }
  265. [Fact]
  266. public async Task UpdatePattern_ShouldUpdatePattern_WhenConditionsAreFullfiled()
  267. {
  268. _selectionLevelService.GetByIdEntity(Arg.Any<int>()).Returns(new SelectionLevel
  269. {
  270. Id = 1,
  271. Name = "Test 1",
  272. SelectionProcesses = new List<SelectionProcess>()
  273. });
  274. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  275. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  276. var updatePatternDto = new PatternUpdateDto
  277. {
  278. CreatedAt = DateTime.Now,
  279. Message = "Message",
  280. SelectionLevelId = 1122,
  281. Title = "Zakazan termin"
  282. };
  283. await patternService.UpdateAsync(updatePatternDto, 1);
  284. var result = await patternService.GetAllAsync();
  285. Assert.Equal(1, result.Count);
  286. }
  287. [Fact]
  288. public async Task DeletePattern_ShouldThrowEntityNotFoundException_WhenPatternDoesNotExist()
  289. {
  290. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  291. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  292. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.DeleteAsync(2));
  293. }
  294. [Fact]
  295. public async Task DeletePattern_ShouldDeletePattern_WhenPatternExist()
  296. {
  297. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  298. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  299. await patternService.DeleteAsync(1);
  300. var result = await patternService.GetAllAsync();
  301. Assert.Equal(0, result.Count);
  302. }
  303. }
  304. }