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 IApplicantService _applicantService = Substitute.For(); private readonly ApplicantViewDto _applicant; 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 = ""} } }; } [Fact] public async Task GetById_ShouldReturn_200OK_WhenApplicantExist() { _applicantService.GetById(Arg.Any()).Returns(_applicant); ApplicantsController applicantsController = new(_applicantService); var result = await applicantsController.GetById(1); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task GetProcesses_ShouldReturn_200OK_WhenApplicantExists() { _applicantService.GetApplicantWithSelectionProcessesById(Arg.Any()).Returns(_applicant); ApplicantsController applicantsController = new(_applicantService); var result = await applicantsController.GetProcesses(1); (result as OkObjectResult).StatusCode.Should().Be(200); } [Fact] public async Task GetById_ShouldThrowEntityNotFooundException_WhenApplicantDontExist() { _applicantService.When(x => x.GetById(Arg.Any())).Do(x => { throw new EntityNotFoundException(); }); ApplicantsController applicantsController = new(_applicantService); await Assert.ThrowsAsync(() => applicantsController.GetById(1000)); } [Fact] public async Task GetProcesses_ShouldThrowEntityNotFooundException_WhenApplicantDoesnotExist() { _applicantService.When(x => x.GetApplicantWithSelectionProcessesById(Arg.Any())).Do(x => { throw new EntityNotFoundException(); }); ApplicantsController applicantsController = new(_applicantService); await Assert.ThrowsAsync(() => applicantsController.GetProcesses(1000)); } [Fact] public async Task GetAll_ShouldReturn_200OK_Always() { var applicants = new List { _applicant }; _applicantService.GetAll().Returns(applicants); ApplicantsController applicantsController = new(_applicantService); var result = await applicantsController.GetAll(); (result as OkObjectResult).StatusCode.Should().Be(200); } } }