using AutoMapper; using Castle.Core.Logging; using Diligent.WebAPI.Business.MappingProfiles; using Diligent.WebAPI.Business.Services; using Diligent.WebAPI.Contracts.DTOs.Technology; using Diligent.WebAPI.Contracts.Exceptions; using Diligent.WebAPI.Data.Entities; 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 TechnologyServiceTests { private readonly IMapper _mapper; private readonly List _technologies = new(); private readonly Technology _technology; private readonly ILogger _logger = Substitute.For>(); public TechnologyServiceTests() { var configuration = new MapperConfiguration(cfg => cfg.AddProfiles( new List { new TechnologyMappingProfile() })); _mapper = new Mapper(configuration); _technology = new Technology { TechnologyId = 1, Name = ".NET", TechnologyType = TechnologyTypes.Backend }; _technologies.Add(_technology); } [Fact] public async Task GetAll_ShouldReturnListOfTechnologies_Always() { var databaseContext = await Helpers.GetDatabaseContext(_technologies); TechnologyService tehnologyService = new(_mapper, databaseContext, _logger); var result = await tehnologyService.GetAllAsync(); Assert.Equal(result.Count, 1); } [Fact] public async Task GetById_ShouldReturnTechnology_WhenTechnologyExists() { var databaseContext = await Helpers.GetDatabaseContext(_technologies); TechnologyService tehnologyService = new(_mapper, databaseContext, _logger); var result = await tehnologyService.GetByIdAsync(1); result.Should().BeEquivalentTo(_mapper.Map(_technology)); } [Fact] public async Task GetById_ShouldThrowEntityNotFoundException_WhenTechnologyDoesNotExists() { var databaseContext = await Helpers.GetDatabaseContext(_technologies); TechnologyService tehnologyService = new(_mapper, databaseContext, _logger); await Assert.ThrowsAsync(async () => await tehnologyService.GetByIdAsync(1000)); } [Fact] public async Task GetEntityById_ShouldReturnTechnology_WhenTechnologyExists() { var databaseContext = await Helpers.GetDatabaseContext(_technologies); TechnologyService tehnologyService = new(_mapper, databaseContext, _logger); var result = await tehnologyService.GetEntityByIdAsync(1); result.Should().BeEquivalentTo(_technology); } [Fact] public async Task GetEntityById_ShouldThrowEntityNotFoundException_WhenTechnologyDoesNotExists() { var databaseContext = await Helpers.GetDatabaseContext(_technologies); TechnologyService tehnologyService = new(_mapper, databaseContext, _logger); await Assert.ThrowsAsync(async () => await tehnologyService.GetEntityByIdAsync(1000)); } } }