Просмотр исходного кода

Added tests for pattern controller & service

pull/149/head
bronjaermin 3 лет назад
Родитель
Сommit
f22261d9bb

+ 1
- 1
Diligent.WebAPI.Host/Controllers/V1/PatternsController.cs Просмотреть файл

@@ -31,7 +31,7 @@ namespace Diligent.WebAPI.Host.Controllers.V1

[Authorize]
[HttpGet("corresponding-pattern-applicants/{id}")]
public async Task<IActionResult> GetFilteredPatterns([FromRoute] int id) =>
public async Task<IActionResult> GetCorrespondingPatternApplicants([FromRoute] int id) =>
Ok(await _patternService.GetCorrespondingPatternApplicants(id));



+ 249
- 0
Diligent.WebAPI.Tests/Controllers/PatternsControllerTests.cs Просмотреть файл

@@ -0,0 +1,249 @@
using Diligent.WebAPI.Contracts.DTOs.Ad;
using Diligent.WebAPI.Contracts.DTOs.Applicant;
using Diligent.WebAPI.Contracts.DTOs.Pattern;
using Diligent.WebAPI.Contracts.Exceptions;
using Diligent.WebAPI.Data.Entities;
using NSubstitute.ReturnsExtensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Diligent.WebAPI.Tests.Controllers
{
public class PatternsControllerTests
{
private IPatternService _patternService = Substitute.For<IPatternService>();
private List<PatternResponseDto> _patterns = new List<PatternResponseDto>();
private PatternResponseDto _pattern;

public PatternsControllerTests()
{
_pattern = new PatternResponseDto
{
Id = 1205,
CreatedAt = DateTime.Now,
Message = "Poruka",
Title = "Title",
SelectionLevel = new Contracts.DTOs.SelectionLevel.SelectionLevelResposneDto
{
Id = 2345,
Name = "Zakazan termin"
}
};

_patterns.Add(_pattern);
}

[Fact]
public async Task GetAll_ShouldReturn200OK_WhenCalled()
{
_patternService.GetAllAsync().Returns(_patterns);
PatternsController patternsController = new(_patternService);
var result = await patternsController.GetAll();
(result as OkObjectResult).StatusCode.Should().Be(200);
}

[Fact]
public async Task GetById_ShouldReturn200OK_WhenPatternExists()
{
_patternService.GetByIdAsync(Arg.Any<int>()).Returns(_pattern);
PatternsController patternsController = new(_patternService);
var result = await patternsController.GetById(1205);
(result as OkObjectResult).StatusCode.Should().Be(200);
}

[Fact]
public async Task GetById_ShouldThrowEntityNotFoundException_WhenPatternDontExist()
{
_patternService.When(x => x.GetByIdAsync(Arg.Any<int>())).Do(x => { throw new EntityNotFoundException(); });

PatternsController patternsController = new(_patternService);

await Assert.ThrowsAsync<EntityNotFoundException>(() => patternsController.GetById(1000));
}

[Fact]
public async Task GetFilteredPatterns_ShouldReturn200OK_WhenCalled()
{
_patternService.GetFilteredPatternsAsync(new FilterPatternDto
{
FromDate = new DateTime(2022, 11, 30),
ToDate = DateTime.Now,
SelectionLevels = null
}).Returns(_patterns);

PatternsController patternsController = new(_patternService);

var result = await patternsController.GetFilteredPatterns(new FilterPatternDto
{
FromDate = new DateTime(2022, 11, 30),
ToDate = DateTime.Now,
SelectionLevels = null
});

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

[Fact]
public async Task GetCorrespondingPatternApplicants_ShouldReturn200OK_WhenCalled()
{
var patternApplicants = new List<PatternApplicantViewDto>
{
new PatternApplicantViewDto
{
ApplicantId = 1,
Email = "ermin.bronja@dilig.net",
FirstName = "Ermin",
LastName = "Bronja"
}
};

_patternService.GetCorrespondingPatternApplicants(1205).Returns(patternApplicants);

PatternsController patternsController = new(_patternService);

var result = await patternsController.GetCorrespondingPatternApplicants(1205);

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

[Fact]
public async Task GetCorrespondingPatternApplicants_ShouldThrowEntityNotFoundException_WhenPatternDontExist()
{
_patternService.When(x => x.GetCorrespondingPatternApplicants(Arg.Any<int>())).Do(x => { throw new EntityNotFoundException(); });

PatternsController patternsController = new(_patternService);

await Assert.ThrowsAsync<EntityNotFoundException>(() => patternsController.GetCorrespondingPatternApplicants(1000));
}

[Fact]
public async Task CreatePattern_ShouldReturn201Created_WhenPatternCreated()
{
var patternCreateDto = new PatternCreateDto
{
CreatedAt = DateTime.Now,
Message = "Poruka",
SelectionLevelId = 5,
Title = "Čeka na zakazivanju"
};

_patternService.CreateAsync(patternCreateDto);

PatternsController patternsController = new(_patternService);

var result = await patternsController.Create(patternCreateDto);

(result as StatusCodeResult).StatusCode.Should().Be(201);
}

[Fact]
public async Task CreatePattern_ShouldThrowEntityNotFoundException_WhenPatternAlreadyExist()
{
_patternService.When(x => x.CreateAsync(Arg.Any<PatternCreateDto>())).Do(x => { throw new EntityNotFoundException(); });

PatternsController patternsController = new(_patternService);

await Assert.ThrowsAsync<EntityNotFoundException>(() => patternsController.Create(new PatternCreateDto { Title = "Title", CreatedAt = DateTime.Now, Message = "Poruka", SelectionLevelId = 2345}));
}

[Fact]
public async Task ScheduleInterview_ShouldReturn200OK_WhenInterviewScheduled()
{
var scheduleInterviewDto = new ScheduleInterviewDto
{
Emails = new List<string>
{
"ermin.bronja@dilig.net"
},
PatternId = 1205
};

_patternService.ScheduleIntrviewAsync(scheduleInterviewDto).ReturnsNull();

PatternsController patternsController = new(_patternService);

var result = await patternsController.ScheduleInterview(scheduleInterviewDto);

var res = result as StatusCodeResult;
Assert.Equal(res, null);
}

[Fact]
public async Task ScheduleInterview_ShouldThrowEntityNotFoundException_WhenPatternDoesNotExist()
{
_patternService.When(x => x.ScheduleIntrviewAsync(Arg.Any<ScheduleInterviewDto>())).Do(x => { throw new EntityNotFoundException(); });

PatternsController patternsController = new(_patternService);

await Assert.ThrowsAsync<EntityNotFoundException>(() => patternsController.ScheduleInterview(new ScheduleInterviewDto
{
Emails = new List<string>
{
"ermin.bronja@dilig.net"
},
PatternId = 1205
}));
}

[Fact]
public async Task UpdatePattern_ShouldReturn200OK_WhenPatternUpdated()
{
var updatePatternDto = new PatternUpdateDto
{
CreatedAt = DateTime.Now,
Message = "Poruka 1",
SelectionLevelId = 500,
Title = "Naslov"
};

_patternService.UpdateAsync(updatePatternDto, 1205);

PatternsController patternsController = new(_patternService);

var result = await patternsController.Update(updatePatternDto, 1205);

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

[Fact]
public async Task UpdatePattern_ShouldThrowEntityNotFoundException_WhenPatternDoesNotExist()
{
var patternUpdateDto = new PatternUpdateDto
{
CreatedAt = DateTime.Now,
SelectionLevelId = 2345,
Message = "Poruka",
Title = "Title"
};

_patternService.When(x => x.UpdateAsync(Arg.Any<PatternUpdateDto>(), Arg.Any<int>())).Do(x => { throw new EntityNotFoundException(); });

PatternsController patternsController = new(_patternService);

await Assert.ThrowsAsync<EntityNotFoundException>(() => patternsController.Update(patternUpdateDto, 1205));
}

[Fact]
public async Task DeletePattern_ShouldReturn204NoContent_WhenPatternDeleted()
{
PatternsController patternsController = new(_patternService);

var result = await patternsController.DeletePattern(1205);

(result as StatusCodeResult).StatusCode.Should().Be(204);
}

[Fact]
public async Task Delete_ShouldThrowEntityNotFoundException_WhenPatternDoesNotExist()
{
_patternService.When(x => x.DeleteAsync(Arg.Any<int>())).Do(x => { throw new EntityNotFoundException(); });

PatternsController patternsController = new(_patternService);

await Assert.ThrowsAsync<EntityNotFoundException>(() => patternsController.DeletePattern(1205));
}
}
}

+ 355
- 0
Diligent.WebAPI.Tests/Services/PatternServiceTests.cs Просмотреть файл

@@ -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);
}
}
}

Загрузка…
Отмена
Сохранить