You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TechnologyServiceTests.cs 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using AutoMapper;
  2. using Castle.Core.Logging;
  3. using Diligent.WebAPI.Business.MappingProfiles;
  4. using Diligent.WebAPI.Business.Services;
  5. using Diligent.WebAPI.Contracts.DTOs.Technology;
  6. using Diligent.WebAPI.Contracts.Exceptions;
  7. using Diligent.WebAPI.Data.Entities;
  8. using Microsoft.Extensions.Logging;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace Diligent.WebAPI.Tests.Services
  15. {
  16. public class TechnologyServiceTests
  17. {
  18. private readonly IMapper _mapper;
  19. private readonly List<Technology> _technologies = new();
  20. private readonly Technology _technology;
  21. private readonly ILogger<TechnologyService> _logger = Substitute.For<ILogger<TechnologyService>>();
  22. public TechnologyServiceTests()
  23. {
  24. var configuration = new MapperConfiguration(cfg => cfg.AddProfiles(
  25. new List<Profile>
  26. {
  27. new TechnologyMappingProfile()
  28. }));
  29. _mapper = new Mapper(configuration);
  30. _technology = new Technology
  31. {
  32. TechnologyId = 1,
  33. Name = ".NET",
  34. TechnologyType = TechnologyTypes.Backend
  35. };
  36. _technologies.Add(_technology);
  37. }
  38. [Fact]
  39. public async Task GetAll_ShouldReturnListOfTechnologies_Always()
  40. {
  41. var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
  42. TechnologyService tehnologyService = new(_mapper, databaseContext, _logger);
  43. var result = await tehnologyService.GetAllAsync();
  44. Assert.Equal(result.Count, 1);
  45. }
  46. [Fact]
  47. public async Task GetById_ShouldReturnTechnology_WhenTechnologyExists()
  48. {
  49. var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
  50. TechnologyService tehnologyService = new(_mapper, databaseContext, _logger);
  51. var result = await tehnologyService.GetByIdAsync(1);
  52. result.Should().BeEquivalentTo(_mapper.Map<TechnologyResponseDto>(_technology));
  53. }
  54. [Fact]
  55. public async Task GetById_ShouldThrowEntityNotFoundException_WhenTechnologyDoesNotExists()
  56. {
  57. var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
  58. TechnologyService tehnologyService = new(_mapper, databaseContext, _logger);
  59. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await tehnologyService.GetByIdAsync(1000));
  60. }
  61. [Fact]
  62. public async Task GetEntityById_ShouldReturnTechnology_WhenTechnologyExists()
  63. {
  64. var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
  65. TechnologyService tehnologyService = new(_mapper, databaseContext, _logger);
  66. var result = await tehnologyService.GetEntityByIdAsync(1);
  67. result.Should().BeEquivalentTo(_technology);
  68. }
  69. [Fact]
  70. public async Task GetEntityById_ShouldThrowEntityNotFoundException_WhenTechnologyDoesNotExists()
  71. {
  72. var databaseContext = await Helpers<Technology>.GetDatabaseContext(_technologies);
  73. TechnologyService tehnologyService = new(_mapper, databaseContext, _logger);
  74. await Assert.ThrowsAsync<EntityNotFoundException>(async () => await tehnologyService.GetEntityByIdAsync(1000));
  75. }
  76. }
  77. }