|
|
|
@@ -0,0 +1,355 @@ |
|
|
|
using AutoMapper; |
|
|
|
using Castle.Core.Logging; |
|
|
|
using Diligent.WebAPI.Business.MappingProfiles; |
|
|
|
using Diligent.WebAPI.Business.Services; |
|
|
|
using Diligent.WebAPI.Business.Services.Interfaces; |
|
|
|
using Diligent.WebAPI.Contracts.DTOs.Ad; |
|
|
|
using Diligent.WebAPI.Contracts.DTOs.Pattern; |
|
|
|
using Diligent.WebAPI.Contracts.Exceptions; |
|
|
|
using Diligent.WebAPI.Data.Entities; |
|
|
|
using Microsoft.AspNetCore.Http; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Text; |
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
namespace Diligent.WebAPI.Tests.Services |
|
|
|
{ |
|
|
|
public class PatternServiceTests |
|
|
|
{ |
|
|
|
private readonly IMapper _mapper; |
|
|
|
private readonly List<Pattern> _patterns = new(); |
|
|
|
private readonly Pattern _pattern; |
|
|
|
private ISelectionLevelService _selectionLevelService = Substitute.For<ISelectionLevelService>(); |
|
|
|
private ILogger<PatternService> _logger = Substitute.For<ILogger<PatternService>>(); |
|
|
|
private IEmailer _emailService = Substitute.For<IEmailer>(); |
|
|
|
private ISelectionProcessService _selectionProcessService = Substitute.For<ISelectionProcessService>(); |
|
|
|
|
|
|
|
public PatternServiceTests() |
|
|
|
{ |
|
|
|
var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(new List<Profile>() |
|
|
|
{ |
|
|
|
new PatternMappingProfile(), |
|
|
|
new SelectionLevelMappingProfile(), |
|
|
|
new SelectionProcessMappingProfile(), |
|
|
|
new ApplicantMappingProfile() |
|
|
|
})); |
|
|
|
_mapper = new Mapper(configuration); |
|
|
|
|
|
|
|
_pattern = new Pattern |
|
|
|
{ |
|
|
|
Id = 1, |
|
|
|
Title = "Zakazivanje termina", |
|
|
|
CreatedAt = DateTime.Now, |
|
|
|
SelectionLevelId = 2000, |
|
|
|
SelectionLevel = new SelectionLevel |
|
|
|
{ |
|
|
|
Id = 2000, |
|
|
|
Name = "SelLevel", |
|
|
|
SelectionProcesses = new List<SelectionProcess> |
|
|
|
{ |
|
|
|
new SelectionProcess |
|
|
|
{ |
|
|
|
Id = 1234, |
|
|
|
Name = "Zakazan termin", |
|
|
|
Status = "Čeka na zakazivanje", |
|
|
|
SelectionLevelId = 4321, |
|
|
|
ApplicantId = 2321, |
|
|
|
Applicant = new Applicant |
|
|
|
{ |
|
|
|
ApplicantId= 2321, |
|
|
|
FirstName = "Ermin", |
|
|
|
LastName = "Bronja", |
|
|
|
Position = "Developer", |
|
|
|
DateOfApplication = DateTime.Now, |
|
|
|
CV = "", |
|
|
|
Email = "ermin.bronja@dilig.net", |
|
|
|
PhoneNumber = "", |
|
|
|
LinkedlnLink = "", |
|
|
|
GithubLink = "", |
|
|
|
BitBucketLink = "", |
|
|
|
Experience = 1, |
|
|
|
ApplicationChannel = "", |
|
|
|
TypeOfEmployment = Applicant.TypesOfEmployment.Posao, |
|
|
|
}, |
|
|
|
Comment = "Komentar" |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
Message = "Poruka" |
|
|
|
}; |
|
|
|
|
|
|
|
_patterns.Add(_pattern); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task GetAll_ShouldReturnListOfPatterns_WhenCalled() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
var result = await patternService.GetAllAsync(); |
|
|
|
|
|
|
|
Assert.Equal(result.Count, 1); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task GetById_ShouldReturnPattern_WhenAdExists() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
var result = await patternService.GetByIdAsync(1); |
|
|
|
|
|
|
|
result.Should().BeEquivalentTo(_mapper.Map<PatternResponseDto>(_pattern)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task GetById_ShouldThrowEntityNotFoundException_WhenPatternDontExists() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.GetByIdAsync(1000)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task GetFilteredPatterns_ShouldReturnListOfPatterns_WhenCalled() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
var result = await patternService.GetFilteredPatternsAsync(new FilterPatternDto |
|
|
|
{ |
|
|
|
FromDate = new DateTime(2022, 11, 30), |
|
|
|
ToDate = DateTime.Now, |
|
|
|
SelectionLevels = null |
|
|
|
}); |
|
|
|
|
|
|
|
Assert.Equal(result.Count, 1); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task GetCorrespondingPatternApplicants_ShouldReturnListOfApplicants_WhenCalled() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
var result = await patternService.GetCorrespondingPatternApplicants(1); |
|
|
|
|
|
|
|
Assert.Equal(result.Count, 1); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task GetCorrespondingPatternApplicants_ShouldThrowEntityNotFoundException_WhenPatternDontExists() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.GetCorrespondingPatternApplicants(2)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task CreatePattern_ShouldThrowEntityNotFoundException_WhenPatternExist() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.CreateAsync(new PatternCreateDto |
|
|
|
{ |
|
|
|
Title = "Zakazivanje termina", |
|
|
|
CreatedAt = DateTime.Now, |
|
|
|
SelectionLevelId = 2000, |
|
|
|
Message = "Poruka", |
|
|
|
})); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task CreatePattern_ShouldThrowEntityNotFoundException_WhenSelectionLevelIsNull() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.CreateAsync(new PatternCreateDto |
|
|
|
{ |
|
|
|
Title = "Zakazan termin", |
|
|
|
CreatedAt = DateTime.Now, |
|
|
|
SelectionLevelId = 2323, |
|
|
|
Message = "Poruka", |
|
|
|
})); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task CreatePattern_ShouldCreatePattern_WhenConditionsAreFullfiled() |
|
|
|
{ |
|
|
|
_selectionLevelService.GetByIdEntity(Arg.Any<int>()).Returns(new SelectionLevel |
|
|
|
{ |
|
|
|
Id = 1122, |
|
|
|
Name = "Test", |
|
|
|
SelectionProcesses = new List<SelectionProcess>() |
|
|
|
}); |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
await patternService.CreateAsync(new PatternCreateDto |
|
|
|
{ |
|
|
|
Title = "Zakazan termin", |
|
|
|
CreatedAt = DateTime.Now, |
|
|
|
SelectionLevelId = 1122, |
|
|
|
Message = "Poruka", |
|
|
|
}); |
|
|
|
|
|
|
|
var result = await patternService.GetAllAsync(); |
|
|
|
|
|
|
|
Assert.Equal(2, result.Count); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task ScheduleInterview_ShouldThrowEntityNotFoundException_WhenWhenPatternDoesNotExist() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.ScheduleIntrviewAsync(new ScheduleInterviewDto |
|
|
|
{ |
|
|
|
Emails = new List<string> |
|
|
|
{ |
|
|
|
"ermin.bronja@dilig.net" |
|
|
|
}, |
|
|
|
PatternId = 2 |
|
|
|
})); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task ScheduleInterview_ShouldScheduleInterview_WhenConditionsAreFullfiled() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
var result = await patternService.ScheduleIntrviewAsync(new ScheduleInterviewDto |
|
|
|
{ |
|
|
|
Emails = new List<string> |
|
|
|
{ |
|
|
|
"ermin.bronja@dilig.net" |
|
|
|
}, |
|
|
|
PatternId = 1 |
|
|
|
}); |
|
|
|
|
|
|
|
Assert.Equal(null, result); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task ScheduleInterview_ShouldReturnListOfNotSentEmails_WhenEmailsDontExistOrStatusAreNotCorrectInProcesses() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
var result = await patternService.ScheduleIntrviewAsync(new ScheduleInterviewDto |
|
|
|
{ |
|
|
|
Emails = new List<string> |
|
|
|
{ |
|
|
|
"meris.ahmatovic@dilig.net" |
|
|
|
}, |
|
|
|
PatternId = 1 |
|
|
|
}); |
|
|
|
|
|
|
|
Assert.NotEqual(null, result); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task UpdatePattern_ShouldThrowEntityNotFoundException_WhenWhenPatternDoesNotExist() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.UpdateAsync(new PatternUpdateDto |
|
|
|
{ |
|
|
|
CreatedAt = DateTime.Now, |
|
|
|
Message = "Poruka 1", |
|
|
|
SelectionLevelId = 2000, |
|
|
|
Title = "Naslov" |
|
|
|
}, 2)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task UpdatePattern_ShouldThrowEntityNotFoundException_WhenTitleAndPatternSelectionLevelAreNotChanged() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.UpdateAsync(new PatternUpdateDto |
|
|
|
{ |
|
|
|
CreatedAt = DateTime.Now, |
|
|
|
Message = "Poruka 1", |
|
|
|
SelectionLevelId = 2000, |
|
|
|
Title = "Zakazivanje termina" |
|
|
|
}, 1)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task UpdatePattern_ShouldThrowEntityNotFoundException_WhenSelectionLevelDoesNotExistInDatabase() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.UpdateAsync(new PatternUpdateDto |
|
|
|
{ |
|
|
|
CreatedAt = DateTime.Now, |
|
|
|
Message = "Poruka 1", |
|
|
|
SelectionLevelId = 2001, |
|
|
|
Title = "Zakazan Termin" |
|
|
|
}, 1)); |
|
|
|
} |
|
|
|
|
|
|
|
//[Fact] |
|
|
|
//public async Task UpdatePattern_ShouldUpdatePattern_WhenConditionsAreFullfiled() |
|
|
|
//{ |
|
|
|
// _selectionLevelService.GetByIdEntity(Arg.Any<int>()).Returns(new SelectionLevel |
|
|
|
// { |
|
|
|
// Id = 1122, |
|
|
|
// Name = "Test 1", |
|
|
|
// SelectionProcesses = new List<SelectionProcess>() |
|
|
|
// }); |
|
|
|
// var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
// PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
// var updatePatternDto = new PatternUpdateDto |
|
|
|
// { |
|
|
|
// CreatedAt = DateTime.Now, |
|
|
|
// Message = "Message", |
|
|
|
// SelectionLevelId = 1122, |
|
|
|
// Title = "Zakazan termin" |
|
|
|
// }; |
|
|
|
|
|
|
|
// await patternService.UpdateAsync(updatePatternDto, 1); |
|
|
|
|
|
|
|
// var result = await patternService.GetAllAsync(); |
|
|
|
|
|
|
|
// Assert.Equal(1, result.Count); |
|
|
|
//} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task DeletePattern_ShouldThrowEntityNotFoundException_WhenPatternDoesNotExist() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
await Assert.ThrowsAsync<EntityNotFoundException>(async () => await patternService.DeleteAsync(2)); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task DeletePattern_ShouldDeletePattern_WhenPatternExist() |
|
|
|
{ |
|
|
|
var databaseContext = await Helpers<Pattern>.GetDatabaseContext(_patterns); |
|
|
|
PatternService patternService = new(databaseContext, _mapper, _selectionLevelService, _logger, _emailService, _selectionProcessService); |
|
|
|
|
|
|
|
await patternService.DeleteAsync(1); |
|
|
|
|
|
|
|
var result = await patternService.GetAllAsync(); |
|
|
|
|
|
|
|
Assert.Equal(0, result.Count); |
|
|
|
} |
|
|
|
} |
|
|
|
} |