using Diligent.WebAPI.Contracts.DTOs; using Diligent.WebAPI.Contracts.DTOs.Applicant; using Diligent.WebAPI.Contracts.DTOs.SelectionProcess; using Diligent.WebAPI.Contracts.Exceptions; using NSubstitute; namespace Diligent.WebAPI.Tests.Controllers { public class ApplicantsControllerTests { private readonly IApplicantService _applicantService = Substitute.For(); private readonly IFileService _fileService = Substitute.For(); private readonly ApplicantViewDto _applicant; private readonly List _adApplicants; public ApplicantsControllerTests() { _applicant = new ApplicantViewDto { ApplicantId = 1, ApplicationChannel = "Instagram", BitBucketLink = null, CV = "link", DateOfApplication = DateTime.Now, Email = "some@mail.com", Experience = 1, FirstName = "Dzenis", LastName = "Hadzifejzovic", GithubLink = null, LinkedlnLink = null, PhoneNumber = "432424", Position = ".NET Developer", SelectionProcesses = new List { new SelectionProcessResposneWithoutApplicantDto{ Status = ""}, new SelectionProcessResposneWithoutApplicantDto{ Status = ""}, new SelectionProcessResposneWithoutApplicantDto{ Status = ""} } }; _adApplicants = new List { new Contracts.DTOs.Ad.AdApplicantsViewDto { Applicants = new List{ new AdApplicantViewDto { ApplicantId = 1, CV = "link", DateOfApplication = DateTime.Now, Experience = 3, FirstName = "Dzenis", LastName = "Hadzifdejzovic", TechnologyApplicants = new List()} }, Id = 1, Title = "some title" } }; } [Fact] public async Task GetFilteredApplicants_ShouldReturn_200OK_Always() { var applicants = new List { _applicant }; var filterDto = new ApplicantFilterDto { CurrentPage = 1, PageSize = 4 }; _applicantService.GetFilteredApplicants(filterDto).Returns(new QueryResultDto { Items = applicants, Total = applicants.Count }); ApplicantsController applicantsController = new(_applicantService, _fileService); var result = await applicantsController.GetFilteredApplicants(filterDto); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task GetById_ShouldThrowEntityNotFoundException_WhenApplicantDoesNotExist() { _applicantService.When(x => x.GetById(Arg.Any())).Do(x => { throw new EntityNotFoundException(); }); ApplicantsController applicantsController = new(_applicantService, _fileService); await Assert.ThrowsAsync(() => applicantsController.GetById(1000)); } [Fact] public async Task GetById_ShouldReturn_200OK_WhenApplicantExist() { _applicantService.GetById(Arg.Any()).Returns(_applicant); ApplicantsController applicantsController = new(_applicantService, _fileService); var result = await applicantsController.GetById(1); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task GetAllAdsApplicants_ShouldReturn_200OK_Always() { _applicantService.GetAllAdsApplicants(Arg.Any()).Returns(_adApplicants); ApplicantsController applicantsController = new(_applicantService, _fileService); var result = await applicantsController.GetAllAdsApplicants(new ApplicantFilterDto { }); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task DeleteApplicant_ShouldThrowEntityNotFoundException_WhenApplicantDoesNotExist() { _applicantService.When(x => x.DeleteApplicant(Arg.Any())).Do(x => { throw new EntityNotFoundException(); }); ApplicantsController applicantsController = new(_applicantService, _fileService); await Assert.ThrowsAsync(() => applicantsController.DeleteApplicant(1000)); } [Fact] public async Task DeleteApplicant_ShouldReturn_200OK_WhenApplicantExist() { _applicantService.When(x => x.DeleteApplicant(Arg.Any())).Do(x => { }); ApplicantsController applicantsController = new(_applicantService, _fileService); var result = await applicantsController.DeleteApplicant(1000); (result as StatusCodeResult).StatusCode.Should().Be(200); } [Fact] public async Task GetProcesses_ShouldReturn_200OK_WhenApplicantExists() { _applicantService.GetApplicantWithSelectionProcessesById(Arg.Any()).Returns(_applicant); ApplicantsController applicantsController = new(_applicantService,_fileService); var result = await applicantsController.GetProcesses(1); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task GetProcesses_ShouldThrowEntityNotFoundException_WhenApplicantDoesnotExist() { _applicantService.When(x => x.GetApplicantWithSelectionProcessesById(Arg.Any())).Do(x => { throw new EntityNotFoundException(); }); ApplicantsController applicantsController = new(_applicantService, _fileService); await Assert.ThrowsAsync(() => applicantsController.GetProcesses(1000)); } [Fact] public async Task GetOptions_ShouldReturn_200OK_Always() { _applicantService.GetOptions().Returns(new List()); ApplicantsController applicantsController = new(_applicantService, _fileService); var result = await applicantsController.GetOptions(); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task InitSelecetion_ShouldReturnError_WhenApplicantDoesNotExist() { _applicantService.InitializeProcess(Arg.Any()).Returns(new ServiceResponseDTO()); ApplicantsController applicantsController = new(_applicantService, _fileService); var result = await applicantsController.InitSelection(Arg.Any()); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task ApplyForAd_ShouldThrowEntityNotFoundException_WhenAdDoesNotExist() { _applicantService.When(x => x.ApplyForAd(Arg.Any())).Do(x => { throw new EntityNotFoundException(); }); ApplicantsController applicantsController = new(_applicantService, _fileService); await Assert.ThrowsAsync(() => applicantsController.ApplyForAd(new ApplyForAdRequestDto())); } [Fact] public async Task ApplyForAd_ShouldReturn_200OK_WhenAdExist() { _applicantService.When(x => x.ApplyForAd(Arg.Any())).Do(x => { }); ApplicantsController applicantsController = new(_applicantService, _fileService); var result = await applicantsController.ApplyForAd(new ApplyForAdRequestDto()); (result as StatusCodeResult).StatusCode.Should().Be(200); } [Fact] public async Task GetApplicantCV_ShouldReturn_200OK_Always() { _fileService.GetCV(Arg.Any()).Returns("some string"); ApplicantsController applicantsController = new(_applicantService, _fileService); var result = await applicantsController.GetApplicantCV("some string"); (result as OkObjectResult).StatusCode.Should().Be(200); } } }