You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PatternServiceTests.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. },
  74. Comment = "Komentar"
  75. }
  76. }
  77. },
  78. Message = "Poruka"
  79. };
  80. _patterns.Add(_pattern);
  81. }
  82. [Fact]
  83. public async Task GetAll_ShouldReturnListOfPatterns_WhenCalled()
  84. {
  85. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  86. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  87. var result = await patternService.GetAllAsync();
  88. Assert.Equal(result.Count, 1);
  89. }
  90. [Fact]
  91. public async Task GetById_ShouldReturnPattern_WhenAdExists()
  92. {
  93. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  94. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  95. var result = await patternService.GetByIdAsync(1);
  96. result.Should().BeEquivalentTo(_mapper.Map<PatternResponseDto>(_pattern));
  97. }
  98. [Fact]
  99. public async Task GetById_ShouldThrowEntityNotFoundException_WhenPatternDontExists()
  100. {
  101. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  102. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  103. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.GetByIdAsync(1000));
  104. }
  105. [Fact]
  106. public async Task GetFilteredPatterns_ShouldReturnListOfPatterns_WhenCalled()
  107. {
  108. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  109. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  110. var result = await patternService.GetFilteredPatternsAsync(new FilterPatternDto
  111. {
  112. FromDate = new DateTime(2022, 11, 30),
  113. ToDate = DateTime.Now,
  114. SelectionLevels = null
  115. });
  116. Assert.Equal(result.Count, 1);
  117. }
  118. [Fact]
  119. public async Task GetCorrespondingPatternApplicants_ShouldReturnListOfApplicants_WhenCalled()
  120. {
  121. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  122. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  123. var result = await patternService.GetCorrespondingPatternApplicants(1);
  124. Assert.Equal(result.Count, 1);
  125. }
  126. [Fact]
  127. public async Task GetCorrespondingPatternApplicants_ShouldThrowEntityNotFoundException_WhenPatternDontExists()
  128. {
  129. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  130. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  131. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.GetCorrespondingPatternApplicants(2));
  132. }
  133. [Fact]
  134. public async Task CreatePattern_ShouldThrowEntityNotFoundException_WhenPatternExist()
  135. {
  136. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  137. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  138. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.CreateAsync(new PatternCreateDto
  139. {
  140. Title = "Zakazivanje termina",
  141. CreatedAt = DateTime.Now,
  142. SelectionLevelId = 2000,
  143. Message = "Poruka",
  144. }));
  145. }
  146. [Fact]
  147. public async Task CreatePattern_ShouldThrowEntityNotFoundException_WhenSelectionLevelIsNull()
  148. {
  149. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  150. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  151. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.CreateAsync(new PatternCreateDto
  152. {
  153. Title = "Zakazan termin",
  154. CreatedAt = DateTime.Now,
  155. SelectionLevelId = 2323,
  156. Message = "Poruka",
  157. }));
  158. }
  159. [Fact]
  160. public async Task CreatePattern_ShouldCreatePattern_WhenConditionsAreFullfiled()
  161. {
  162. _selectionLevelService.GetByIdEntity(Arg.Any<int>()).Returns(new SelectionLevel
  163. {
  164. Id = 1122,
  165. Name = "Test",
  166. SelectionProcesses = new List<SelectionProcess>()
  167. });
  168. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  169. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  170. await patternService.CreateAsync(new PatternCreateDto
  171. {
  172. Title = "Zakazan termin",
  173. CreatedAt = DateTime.Now,
  174. SelectionLevelId = 1122,
  175. Message = "Poruka",
  176. });
  177. var result = await patternService.GetAllAsync();
  178. Assert.Equal(2, result.Count);
  179. }
  180. [Fact]
  181. public async Task ScheduleInterview_ShouldThrowEntityNotFoundException_WhenWhenPatternDoesNotExist()
  182. {
  183. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  184. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  185. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.ScheduleIntrviewAsync(new ScheduleInterviewDto
  186. {
  187. Emails = new List<string>
  188. {
  189. "ermin.bronja@dilig.net"
  190. },
  191. PatternId = 2
  192. }));
  193. }
  194. [Fact]
  195. public async Task ScheduleInterview_ShouldScheduleInterview_WhenConditionsAreFullfiled()
  196. {
  197. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  198. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  199. var result = await patternService.ScheduleIntrviewAsync(new ScheduleInterviewDto
  200. {
  201. Emails = new List<string>
  202. {
  203. "ermin.bronja@dilig.net"
  204. },
  205. PatternId = 1
  206. });
  207. Assert.Equal(null, result);
  208. }
  209. [Fact]
  210. public async Task ScheduleInterview_ShouldReturnListOfNotSentEmails_WhenEmailsDontExistOrStatusAreNotCorrectInProcesses()
  211. {
  212. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  213. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  214. var result = await patternService.ScheduleIntrviewAsync(new ScheduleInterviewDto
  215. {
  216. Emails = new List<string>
  217. {
  218. "meris.ahmatovic@dilig.net"
  219. },
  220. PatternId = 1
  221. });
  222. Assert.NotEqual(null, result);
  223. }
  224. [Fact]
  225. public async Task UpdatePattern_ShouldThrowEntityNotFoundException_WhenWhenPatternDoesNotExist()
  226. {
  227. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  228. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  229. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.UpdateAsync(new PatternUpdateDto
  230. {
  231. CreatedAt = DateTime.Now,
  232. Message = "Poruka 1",
  233. SelectionLevelId = 2000,
  234. Title = "Naslov"
  235. }, 2));
  236. }
  237. [Fact]
  238. public async Task UpdatePattern_ShouldThrowEntityNotFoundException_WhenTitleAndPatternSelectionLevelAreNotChanged()
  239. {
  240. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  241. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  242. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.UpdateAsync(new PatternUpdateDto
  243. {
  244. CreatedAt = DateTime.Now,
  245. Message = "Poruka 1",
  246. SelectionLevelId = 2000,
  247. Title = "Zakazivanje termina"
  248. }, 1));
  249. }
  250. [Fact]
  251. public async Task UpdatePattern_ShouldThrowEntityNotFoundException_WhenSelectionLevelDoesNotExistInDatabase()
  252. {
  253. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  254. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  255. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.UpdateAsync(new PatternUpdateDto
  256. {
  257. CreatedAt = DateTime.Now,
  258. Message = "Poruka 1",
  259. SelectionLevelId = 2001,
  260. Title = "Zakazan Termin"
  261. }, 1));
  262. }
  263. //[Fact]
  264. //public async Task UpdatePattern_ShouldUpdatePattern_WhenConditionsAreFullfiled()
  265. //{
  266. // _selectionLevelService.GetByIdEntity(Arg.Any<int>()).Returns(new SelectionLevel
  267. // {
  268. // Id = 1122,
  269. // Name = "Test 1",
  270. // SelectionProcesses = new List<SelectionProcess>()
  271. // });
  272. // var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  273. // PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  274. // var updatePatternDto = new PatternUpdateDto
  275. // {
  276. // CreatedAt = DateTime.Now,
  277. // Message = "Message",
  278. // SelectionLevelId = 1122,
  279. // Title = "Zakazan termin"
  280. // };
  281. // await patternService.UpdateAsync(updatePatternDto, 1);
  282. // var result = await patternService.GetAllAsync();
  283. // Assert.Equal(1, result.Count);
  284. //}
  285. [Fact]
  286. public async Task DeletePattern_ShouldThrowEntityNotFoundException_WhenPatternDoesNotExist()
  287. {
  288. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  289. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  290. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.DeleteAsync(2));
  291. }
  292. [Fact]
  293. public async Task DeletePattern_ShouldDeletePattern_WhenPatternExist()
  294. {
  295. var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns);
  296. PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService);
  297. await patternService.DeleteAsync(1);
  298. var result = await patternService.GetAllAsync();
  299. Assert.Equal(0, result.Count);
  300. }
  301. }
  302. }