|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using api.Database.Context; |
| 5 | +using api.Database.Models; |
| 6 | +using api.Services; |
| 7 | +using Microsoft.AspNetCore.Mvc; |
| 8 | +using Microsoft.EntityFrameworkCore; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Moq; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace api.Controllers.Tests |
| 14 | +{ |
| 15 | + public class AnalysisMappingControllerTest |
| 16 | + { |
| 17 | + private readonly Mock<ILogger<AnalysisMappingController>> _loggerMock; |
| 18 | + private readonly Mock<IAnalysisMappingService> _analysisMappingServiceMock; |
| 19 | + private readonly Mock<IPlantDataService> _plantDataServiceMock; |
| 20 | + private readonly SaraDbContext _dbContext; |
| 21 | + private readonly AnalysisMappingController _analysisMappingController; |
| 22 | + |
| 23 | + public AnalysisMappingControllerTest() |
| 24 | + { |
| 25 | + _loggerMock = new Mock<ILogger<AnalysisMappingController>>(); |
| 26 | + _analysisMappingServiceMock = new Mock<IAnalysisMappingService>(); |
| 27 | + _plantDataServiceMock = new Mock<IPlantDataService>(); |
| 28 | + var options = new DbContextOptionsBuilder<SaraDbContext>() |
| 29 | + .UseInMemoryDatabase(databaseName: "TestDatabase") |
| 30 | + .Options; |
| 31 | + _dbContext = new SaraDbContext(options); |
| 32 | + _analysisMappingController = new AnalysisMappingController(_loggerMock.Object, _analysisMappingServiceMock.Object, _plantDataServiceMock.Object, _dbContext); |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public async Task AddOrCreateAnalysisMapping_ReturnsBadRequest_WhenAnalysisTypeIsInvalid() |
| 38 | + { |
| 39 | + //Arrange |
| 40 | + string tagId = "dummy-tag-id"; |
| 41 | + string analysisType = "InvalidAnalysisType"; |
| 42 | + string inspectionDescription = "Dummy description"; |
| 43 | + //Act |
| 44 | + var result = await _analysisMappingController.AddOrCreateAnalysisMapping(tagId, analysisType, inspectionDescription); |
| 45 | + //Assert |
| 46 | + Assert.IsType<BadRequestObjectResult>(result.Result); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async Task AddOrCreateAnalysisMapping_ReturnsStatusCode500_WhenExceptionIsThrown() |
| 51 | + { |
| 52 | + //Arrange |
| 53 | + string tagId = "dummy-tag-id"; |
| 54 | + string analysisType = "constantleveloiler"; |
| 55 | + string inspectionDescription = "Dummy description"; |
| 56 | + |
| 57 | + _analysisMappingServiceMock |
| 58 | + .Setup(service => service.CreateAnalysisMapping( |
| 59 | + It.IsAny<string>(), |
| 60 | + It.IsAny<string>(), |
| 61 | + It.IsAny<AnalysisType?>() |
| 62 | + )) |
| 63 | + .ThrowsAsync(new Exception("Test exception")); |
| 64 | + |
| 65 | + //Act |
| 66 | + var result = await _analysisMappingController.AddOrCreateAnalysisMapping(tagId, inspectionDescription, analysisType); |
| 67 | + |
| 68 | + //Assert |
| 69 | + var statusCodeResult = Assert.IsType<ObjectResult>(result.Result); |
| 70 | + Assert.Equal(500, statusCodeResult.StatusCode); |
| 71 | + Assert.Equal("An error occurred while creating the analysis mapping", statusCodeResult.Value); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public async Task AddOrCreateAnalysisMapping_CreatesNewMapping_WhenNoneExists() |
| 76 | + { |
| 77 | + // Arrange |
| 78 | + string tagId = "test-tag"; |
| 79 | + string inspectionDescription = "desc"; |
| 80 | + string analysisType = "constantleveloiler"; |
| 81 | + var parsedType = AnalysisType.ConstantLevelOiler; |
| 82 | + var newMapping = new AnalysisMapping(tagId, inspectionDescription) |
| 83 | + { |
| 84 | + AnalysesToBeRun = new List<AnalysisType> { parsedType } |
| 85 | + }; |
| 86 | + |
| 87 | + _analysisMappingServiceMock |
| 88 | + .Setup(s => s.ReadByInspectionDescriptionAndTag(inspectionDescription, tagId)) |
| 89 | + .ReturnsAsync((AnalysisMapping?)null); |
| 90 | + |
| 91 | + _analysisMappingServiceMock |
| 92 | + .Setup(s => s.CreateAnalysisMapping(tagId, inspectionDescription, parsedType)) |
| 93 | + .ReturnsAsync(newMapping); |
| 94 | + |
| 95 | + _plantDataServiceMock |
| 96 | + .Setup(s => s.ReadByTagIdAndInspectionDescription(tagId, inspectionDescription)) |
| 97 | + .ReturnsAsync((List<PlantData>?)null); |
| 98 | + |
| 99 | + // Act |
| 100 | + var result = await _analysisMappingController.AddOrCreateAnalysisMapping(tagId, inspectionDescription, analysisType); |
| 101 | + |
| 102 | + // Assert |
| 103 | + var okResult = Assert.IsType<OkObjectResult>(result.Result); |
| 104 | + Assert.Equal(newMapping, okResult.Value); |
| 105 | + _analysisMappingServiceMock.Verify(s => s.CreateAnalysisMapping(tagId, inspectionDescription, parsedType), Times.Once); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments