Ver código fonte

completed unit tests for applicants

pull/151/head
Dzenis Hadzifejzovic 3 anos atrás
pai
commit
91ffa66405

+ 2
- 1
Diligent.WebAPI.Business/Extensions/ApplicantExtensions.cs Ver arquivo

using static Diligent.WebAPI.Data.Entities.Applicant;
using System.Diagnostics.CodeAnalysis;
using static Diligent.WebAPI.Data.Entities.Applicant;


namespace Diligent.WebAPI.Business.Extensions namespace Diligent.WebAPI.Business.Extensions
{ {

+ 3
- 1
Diligent.WebAPI.Business/Extensions/PaginationExtension.cs Ver arquivo

namespace Diligent.WebAPI.Business.Extensions
using System.Diagnostics.CodeAnalysis;

namespace Diligent.WebAPI.Business.Extensions
{ {
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public static class PaginationExtension public static class PaginationExtension

+ 2
- 0
Diligent.WebAPI.Business/MappingProfiles/AdMappingProfile.cs Ver arquivo

 
using System.Diagnostics.CodeAnalysis;

namespace Diligent.WebAPI.Business.MappingProfiles namespace Diligent.WebAPI.Business.MappingProfiles
{ {
public class AdMappingProfile : Profile public class AdMappingProfile : Profile

+ 7
- 2
Diligent.WebAPI.Business/Services/ApplicantService.cs Ver arquivo

private readonly IUserService _userService; private readonly IUserService _userService;
private readonly IFileService _fileService; private readonly IFileService _fileService;
private readonly IAdService _adService; private readonly IAdService _adService;
private readonly ITechnologyService _technologyService;


public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger, public ApplicantService(DatabaseContext context, IMapper mapper, ILogger<ApplicantService> logger,
IUserService userService,IFileService fileService,IAdService adService)
IUserService userService,IFileService fileService,IAdService adService,ITechnologyService technologyService)
{ {
_context = context; _context = context;
_mapper = mapper; _mapper = mapper;
_userService = userService; _userService = userService;
_fileService = fileService; _fileService = fileService;
_adService = adService; _adService = adService;
_technologyService = technologyService;
} }
public async Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto) public async Task<QueryResultDto<ApplicantViewDto>> GetFilteredApplicants(ApplicantFilterDto applicantFilterDto)
{ {


_logger.LogInformation($"Got {adsApplicants.Count} ads"); _logger.LogInformation($"Got {adsApplicants.Count} ads");


_logger.LogInformation("Mapping received Ads to AdApplicantsViewDto");
var result = _mapper.Map<List<AdApplicantsViewDto>>(adsApplicants); var result = _mapper.Map<List<AdApplicantsViewDto>>(adsApplicants);
_logger.LogInformation($"Ads mapped successfully");
return result; return result;
} }


} }


_logger.LogInformation($"Find sent technologies from FE in database"); _logger.LogInformation($"Find sent technologies from FE in database");
var technologies = await _context.Technologies.Where(x => request.TechnologiesIds.Contains(x.TechnologyId)).ToListAsync();
//var technologies = await _context.Technologies.Where(x => request.TechnologiesIds.Contains(x.TechnologyId)).ToListAsync();
var technologies = await _technologyService.GetEntitiesAsync(request.TechnologiesIds);


_logger.LogInformation($"Create applicant instance with sent data"); _logger.LogInformation($"Create applicant instance with sent data");
Applicant applicant = new() Applicant applicant = new()

+ 3
- 1
Diligent.WebAPI.Business/Services/InsuranceCompaniesService.cs Ver arquivo

namespace Diligent.WebAPI.Business.Services
using System.Diagnostics.CodeAnalysis;

namespace Diligent.WebAPI.Business.Services
{ {
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public class InsuranceCompaniesService : IInsuranceCompaniesService public class InsuranceCompaniesService : IInsuranceCompaniesService

+ 3
- 1
Diligent.WebAPI.Business/Services/InsurancePoliciesService.cs Ver arquivo

namespace Diligent.WebAPI.Business.Services
using System.Diagnostics.CodeAnalysis;

namespace Diligent.WebAPI.Business.Services
{ {
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public class InsurancePoliciesService : IInsurancePoliciesService public class InsurancePoliciesService : IInsurancePoliciesService

+ 3
- 1
Diligent.WebAPI.Business/Services/InsurersService.cs Ver arquivo

namespace Diligent.WebAPI.Business.Services
using System.Diagnostics.CodeAnalysis;

namespace Diligent.WebAPI.Business.Services
{ {
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public class InsurersService : IInsurersService public class InsurersService : IInsurersService

+ 1
- 0
Diligent.WebAPI.Business/Services/Interfaces/ITechnologyService.cs Ver arquivo

Task<List<TechnologyResponseDto>> GetAllAsync(); Task<List<TechnologyResponseDto>> GetAllAsync();
Task<TechnologyResponseDto> GetByIdAsync(int id); Task<TechnologyResponseDto> GetByIdAsync(int id);
Task<Technology> GetEntityByIdAsync(int id); Task<Technology> GetEntityByIdAsync(int id);
Task<List<Technology>> GetEntitiesAsync(int [] technologiesIds);
} }
} }

+ 0
- 5
Diligent.WebAPI.Business/Services/PatternService.cs Ver arquivo

using Diligent.WebAPI.Contracts.DTOs.Pattern; using Diligent.WebAPI.Contracts.DTOs.Pattern;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Diligent.WebAPI.Business.Services namespace Diligent.WebAPI.Business.Services
{ {

+ 15
- 1
Diligent.WebAPI.Business/Services/TechnologyService.cs Ver arquivo



public async Task<List<TechnologyResponseDto>> GetAllAsync() public async Task<List<TechnologyResponseDto>> GetAllAsync()
{ {
return _mapper.Map<List<TechnologyResponseDto>>(await _context.Technologies.ToListAsync());
_logger.LogInformation("Start getting all technologies");
var technologies = await _context.Technologies.ToListAsync();
_logger.LogInformation($"Received {technologies.Count} technologies from database");
_logger.LogInformation("Mapping received technologies to TechnologyResponseDto");
var technologiesDto = _mapper.Map<List<TechnologyResponseDto>>(technologies);
_logger.LogInformation($"Technologies mapped successfully");
return technologiesDto;
} }


public async Task<TechnologyResponseDto> GetByIdAsync(int id) public async Task<TechnologyResponseDto> GetByIdAsync(int id)
_logger.LogInformation($"Technology with id = {id} found successfully"); _logger.LogInformation($"Technology with id = {id} found successfully");
return technology; return technology;
} }

public async Task<List<Technology>> GetEntitiesAsync(int[] technologiesIds)
{
_logger.LogInformation("Start getting all technologies");
var technologies = await _context.Technologies.Where(x => technologiesIds.Contains(x.TechnologyId)).ToListAsync();
_logger.LogInformation($"Received {technologies.Count} technologies from database");
return technologies;
}
} }
} }

+ 3
- 1
Diligent.WebAPI.Business/Services/WebhookDefinitionService.cs Ver arquivo

namespace Diligent.WebAPI.Business.Services
using System.Diagnostics.CodeAnalysis;

namespace Diligent.WebAPI.Business.Services
{ {
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public class WebhookDefinitionService : IWebhookDefinitionService public class WebhookDefinitionService : IWebhookDefinitionService

+ 3
- 1
Diligent.WebAPI.Business/Services/WebhookPublisherService.cs Ver arquivo

namespace Diligent.WebAPI.Business.Services
using System.Diagnostics.CodeAnalysis;

namespace Diligent.WebAPI.Business.Services
{ {
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public class WebhookPublisherService : IWebhookPublisherService public class WebhookPublisherService : IWebhookPublisherService

+ 3
- 1
Diligent.WebAPI.Business/Services/WebhookSubscriptionService.cs Ver arquivo

namespace Diligent.WebAPI.Business.Services
using System.Diagnostics.CodeAnalysis;

namespace Diligent.WebAPI.Business.Services
{ {
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public class WebhookSubscriptionService : IWebhookSubscriptionService public class WebhookSubscriptionService : IWebhookSubscriptionService

+ 3
- 1
Diligent.WebAPI.Host/Extensions/BusinessConfigurationExtension.cs Ver arquivo

namespace Diligent.WebAPI.Host.Extensions
using System.Diagnostics.CodeAnalysis;

namespace Diligent.WebAPI.Host.Extensions
{ {
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public static class BusinessConfigurationExtension public static class BusinessConfigurationExtension

+ 1
- 1
Diligent.WebAPI.Tests/Controllers/ApplicantsControllerTests.cs Ver arquivo

_fileService.GetCV(Arg.Any<string>()).Returns("some string"); _fileService.GetCV(Arg.Any<string>()).Returns("some string");
ApplicantsController applicantsController = new(_applicantService, _fileService); ApplicantsController applicantsController = new(_applicantService, _fileService);


var result = await applicantsController.GetOptions();
var result = await applicantsController.GetApplicantCV("some string");


(result as OkObjectResult).StatusCode.Should().Be(200); (result as OkObjectResult).StatusCode.Should().Be(200);
} }

+ 20
- 15
Diligent.WebAPI.Tests/Services/ApplicantServiceTests.cs Ver arquivo

private readonly IUserService _userService = Substitute.For<IUserService>(); private readonly IUserService _userService = Substitute.For<IUserService>();
private readonly IFileService _fileService = Substitute.For<IFileService>(); private readonly IFileService _fileService = Substitute.For<IFileService>();
private readonly IAdService _adService = Substitute.For<IAdService>(); private readonly IAdService _adService = Substitute.For<IAdService>();
private readonly ITechnologyService _technologyService = Substitute.For<ITechnologyService>();
private readonly List<Applicant> _applicants; private readonly List<Applicant> _applicants;
private readonly List<Ad> _ads; private readonly List<Ad> _ads;
private readonly List<User> _users; private readonly List<User> _users;
private readonly List<Technology> _technologies;
public ApplicantServiceTests() public ApplicantServiceTests()
{ {
// mock data // mock data
_applicants = MockData.GetListOfApplicants(); _applicants = MockData.GetListOfApplicants();
_ads = MockData.GetListOfAds(); _ads = MockData.GetListOfAds();
_users = MockData.GetListOfUsers(); _users = MockData.GetListOfUsers();
_technologies = MockData.GetListOfTechnologies();


// configure mapper // configure mapper
var configuration = new MapperConfiguration(cfg => cfg.AddProfiles( var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
{ {
new ApplicantMappingProfile(), new ApplicantMappingProfile(),
new AdMappingProfile(), new AdMappingProfile(),
new SelectionProcessMappingProfile()
new SelectionProcessMappingProfile(),
new TechnologyMappingProfile()
})); }));
_mapper = new Mapper(configuration); _mapper = new Mapper(configuration);
} }
{ {
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);


ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);


var filterDto = new ApplicantFilterDto var filterDto = new ApplicantFilterDto
{ {
public async Task GetAllAdsApplicants_ShouldReturnAllApplicants_Always() public async Task GetAllAdsApplicants_ShouldReturnAllApplicants_Always()
{ {
var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads); var databaseContext = await Helpers<Ad>.GetDatabaseContext(_ads);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);


var filterDto = MockData.GetApplicantFilters(); var filterDto = MockData.GetApplicantFilters();


{ {
var fileInBase64Format = "some string"; var fileInBase64Format = "some string";
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
_fileService.GetCV(Arg.Any<string>()).Returns(fileInBase64Format); _fileService.GetCV(Arg.Any<string>()).Returns(fileInBase64Format);


var result = await applicantService.GetById(1); var result = await applicantService.GetById(1);
public async Task GetById_ShouldThrowEntityNotFoundException_WhenApplicantDoesNotExist() public async Task GetById_ShouldThrowEntityNotFoundException_WhenApplicantDoesNotExist()
{ {
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);


await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.GetById(1000)); await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.GetById(1000));
} }
public async Task GetApplicantWithSelectionProcessesById_ShouldReturnApplicant_WhenApplicantExists() public async Task GetApplicantWithSelectionProcessesById_ShouldReturnApplicant_WhenApplicantExists()
{ {
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);


var result = await applicantService.GetApplicantWithSelectionProcessesById(1); var result = await applicantService.GetApplicantWithSelectionProcessesById(1);
var processes = result.SelectionProcesses; var processes = result.SelectionProcesses;
public async Task GetApplicantWithSelectionProcessesById_ShouldThrowEntityNotFoundException_WhenApplicantDoesNotExist() public async Task GetApplicantWithSelectionProcessesById_ShouldThrowEntityNotFoundException_WhenApplicantDoesNotExist()
{ {
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);


await Assert.ThrowsAsync<EntityNotFoundException>(async () => await await Assert.ThrowsAsync<EntityNotFoundException>(async () => await
applicantService.GetApplicantWithSelectionProcessesById(1000)); applicantService.GetApplicantWithSelectionProcessesById(1000));
public async Task ApplyForAd_ShouldThrowEntityNotFooundException_WhenAdIsNotFound() public async Task ApplyForAd_ShouldThrowEntityNotFooundException_WhenAdIsNotFound()
{ {
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
ApplyForAdRequestDto applyForAdRequestDto = new() ApplyForAdRequestDto applyForAdRequestDto = new()
{ {
AdId = 1, AdId = 1,
public async Task ApplyForAd_ApplicantShouldBeCreated_Always() public async Task ApplyForAd_ApplicantShouldBeCreated_Always()
{ {
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
ApplyForAdRequestDto applyForAdRequestDto = new() ApplyForAdRequestDto applyForAdRequestDto = new()
{ {
AdId = 1, AdId = 1,
TechnologiesIds = new int[] { 1 }, TechnologiesIds = new int[] { 1 },
}; };
_fileService.When(x => x.UploadCV(Arg.Any<string>(), Arg.Any<IFormFile>())).Do(x => { }); _fileService.When(x => x.UploadCV(Arg.Any<string>(), Arg.Any<IFormFile>())).Do(x => { });
_technologyService.GetEntitiesAsync(Arg.Any<int[]>()).Returns(_technologies);
_adService.GetByIdEntityAsync(Arg.Any<int>()).Returns(new Ad _adService.GetByIdEntityAsync(Arg.Any<int>()).Returns(new Ad
{ {
Id = 10, Id = 10,
public async Task DeleteApplicant_ShouldDeleteApplicant_WhenApplicantExist() public async Task DeleteApplicant_ShouldDeleteApplicant_WhenApplicantExist()
{ {
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);


await applicantService.DeleteApplicant(1); await applicantService.DeleteApplicant(1);


public async Task DeleteApplicant_ShouldThrowEntityNotFooundException_WhenApplicantDoesNotExist() public async Task DeleteApplicant_ShouldThrowEntityNotFooundException_WhenApplicantDoesNotExist()
{ {
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);


await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.DeleteApplicant(1000)); await Assert.ThrowsAsync<EntityNotFoundException>(async () => await applicantService.DeleteApplicant(1000));
} }
public async Task GetOptions_ShouldReturnAllOptions_Always() public async Task GetOptions_ShouldReturnAllOptions_Always()
{ {
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);


var res = await applicantService.GetOptions(); var res = await applicantService.GetOptions();
Assert.Equal(2, res.Count); Assert.Equal(2, res.Count);
public async Task InitializeProcess_ShouldReturnError_WhenApplicantDoesNotExist() public async Task InitializeProcess_ShouldReturnError_WhenApplicantDoesNotExist()
{ {
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
var res = await applicantService.InitializeProcess(new ApplicantProcessRequestDTO var res = await applicantService.InitializeProcess(new ApplicantProcessRequestDTO
{ {
ApplicantId = 1000, ApplicantId = 1000,
public async Task InitializeProcess_SelectionProcessShouldBeCreated_WhenApplicantExist() public async Task InitializeProcess_SelectionProcessShouldBeCreated_WhenApplicantExist()
{ {
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);
var res = await applicantService.InitializeProcess(new ApplicantProcessRequestDTO var res = await applicantService.InitializeProcess(new ApplicantProcessRequestDTO
{ {
{ {
var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants); var databaseContext = await Helpers<Applicant>.GetDatabaseContext(_applicants);
_userService.GetFirst().Returns(_users[0]); _userService.GetFirst().Returns(_users[0]);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService);
ApplicantService applicantService = new(databaseContext, _mapper, _logger, _userService, _fileService, _adService, _technologyService);


var ad = new Ad var ad = new Ad
{ {

Carregando…
Cancelar
Salvar