DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/ControllersTests/AnalyticsControllerTests.cs

170 lines
6.2 KiB
C#
Raw Normal View History

2021-07-21 15:29:19 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Services;
2022-04-11 18:00:34 +05:00
using AsbCloudWebApi.Controllers.SAUB;
2021-07-21 15:29:19 +05:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using System;
using System.Collections.Generic;
using System.Security.Claims;
2021-12-07 13:44:00 +05:00
using System.Threading;
2022-01-06 11:03:11 +05:00
using System.Threading.Tasks;
using Xunit;
namespace AsbCloudWebApi.Tests.ControllersTests
{
public class AnalyticsControllerTests
{
private readonly Mock<ITelemetryAnalyticsService> analyticsService;
private readonly Mock<IWellService> wellService;
private readonly TelemetryAnalyticsController controller;
private readonly List<WellDepthToDayDto> depthToDayDtos;
// fills class fields with data. Each test inside can change this data themselves for their needs
public AnalyticsControllerTests()
{
analyticsService = new Mock<ITelemetryAnalyticsService>();
wellService = new Mock<IWellService>();
depthToDayDtos = new List<WellDepthToDayDto>()
{
new WellDepthToDayDto { WellDepth = 1000.0, BitDepth = 1000.0, Date = DateTime.Now },
new WellDepthToDayDto { WellDepth = 2000.0, BitDepth = 2000.0, Date = DateTime.Now },
new WellDepthToDayDto { WellDepth = 3000.0, BitDepth = 3000.0, Date = DateTime.Now }
};
2021-12-07 13:44:00 +05:00
analyticsService.Setup(s => s.GetWellDepthToDayAsync(It.IsAny<int>(), CancellationToken.None).Result)
.Returns(depthToDayDtos);
wellService.Setup(s => s.IsCompanyInvolvedInWell(It.IsAny<int>(), It.IsAny<int>()))
.Returns(true);
2022-01-06 11:03:11 +05:00
wellService.Setup(s => s.IsCompanyInvolvedInWellAsync(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(true));
controller = new TelemetryAnalyticsController(analyticsService.Object,
wellService.Object);
2022-01-11 08:50:34 +05:00
controller.AddUser();
}
[Fact]
public void It_should_return_depth_to_day_analytics()
{
2021-12-07 13:44:00 +05:00
var result = controller.GetWellDepthToDayAsync(1).Result;
var okResult = result as OkObjectResult;
2021-07-21 15:29:19 +05:00
Assert.NotNull(okResult);
}
[Fact]
public void It_should_return_correct_count_depth_to_day_analytics()
{
2021-12-07 13:44:00 +05:00
var result = controller.GetWellDepthToDayAsync(1).Result;
var okResult = result as OkObjectResult;
var resultCount = ((List<WellDepthToDayDto>)okResult.Value).Count;
2021-07-21 15:29:19 +05:00
Assert.Equal(3, resultCount);
}
[Fact]
2021-07-21 15:22:58 +05:00
public void It_should_return_403_if_no_idCompany()
{
var emptyUserController = new TelemetryAnalyticsController(analyticsService.Object,
wellService.Object);
2021-07-21 15:29:19 +05:00
var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { }, "mock"));
2021-07-21 15:29:19 +05:00
emptyUserController.ControllerContext = new ControllerContext()
{
HttpContext = new DefaultHttpContext() { User = user }
};
2021-07-21 15:29:19 +05:00
2021-12-07 13:44:00 +05:00
var result = emptyUserController.GetOperationsByWellAsync(1).Result;
var forbidResult = result as ForbidResult;
2021-07-21 15:29:19 +05:00
Assert.NotNull(forbidResult);
}
[Fact]
public void It_should_return_403_if_user_doesnt_own_well()
{
var wellServiceReturnsFalse = new Mock<IWellService>();
wellServiceReturnsFalse.Setup(s => s.IsCompanyInvolvedInWell(It.IsAny<int>(), It.IsAny<int>()))
.Returns(false);
2021-07-21 15:29:19 +05:00
var newControllerInstance = new TelemetryAnalyticsController(analyticsService.Object,
wellServiceReturnsFalse.Object);
2021-07-21 15:29:19 +05:00
var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
{
2021-07-21 15:22:58 +05:00
new Claim("idCompany", "1"),
}, "mock"));
2021-07-21 15:29:19 +05:00
newControllerInstance.ControllerContext = new ControllerContext()
{
HttpContext = new DefaultHttpContext() { User = user }
};
2021-07-21 15:29:19 +05:00
2021-12-07 13:44:00 +05:00
var result = newControllerInstance.GetWellDepthToDayAsync(1).Result;
var forbidResult = result as ForbidResult;
2021-07-21 15:29:19 +05:00
Assert.NotNull(forbidResult);
}
[Fact]
public void It_should_return_204_if_dtos_is_empty()
{
var emptyAnalyticsService = new Mock<ITelemetryAnalyticsService>();
2021-07-21 15:29:19 +05:00
2021-12-07 13:44:00 +05:00
emptyAnalyticsService.Setup(s => s.GetWellDepthToDayAsync(It.IsAny<int>(), CancellationToken.None).Result)
.Returns(new List<WellDepthToDayDto>());
2021-07-21 15:29:19 +05:00
var newControllerInstance = new TelemetryAnalyticsController(emptyAnalyticsService.Object,
wellService.Object);
2021-07-21 15:29:19 +05:00
var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
{
2021-07-21 15:22:58 +05:00
new Claim("idCompany", "1"),
}, "mock"));
2021-07-21 15:29:19 +05:00
newControllerInstance.ControllerContext = new ControllerContext()
{
HttpContext = new DefaultHttpContext() { User = user }
};
2021-07-21 15:29:19 +05:00
2021-12-07 13:44:00 +05:00
var result = newControllerInstance.GetWellDepthToDayAsync(1).Result;
var notFoundResult = result as NoContentResult;
2021-07-21 15:29:19 +05:00
Assert.NotNull(notFoundResult);
}
[Fact]
public void It_should_return_204_if_dtos_is_null()
{
var emptyAnalyticsService = new Mock<ITelemetryAnalyticsService>();
2021-07-21 15:29:19 +05:00
2022-01-06 11:03:11 +05:00
emptyAnalyticsService.Setup(s => s.GetWellDepthToDayAsync(It.IsAny<int>(), CancellationToken.None))
.Returns(Task.FromResult<IEnumerable<WellDepthToDayDto>>(null));
2021-07-21 15:29:19 +05:00
var newControllerInstance = new TelemetryAnalyticsController(emptyAnalyticsService.Object,
wellService.Object);
2021-07-21 15:29:19 +05:00
var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
{
2021-07-21 15:22:58 +05:00
new Claim("idCompany", "1"),
}, "mock"));
2021-07-21 15:29:19 +05:00
newControllerInstance.ControllerContext = new ControllerContext()
{
HttpContext = new DefaultHttpContext() { User = user }
};
2021-07-21 15:29:19 +05:00
2021-12-07 13:44:00 +05:00
var result = newControllerInstance.GetWellDepthToDayAsync(1).Result;
var notFoundResult = result as NoContentResult;
2021-07-21 15:29:19 +05:00
Assert.NotNull(notFoundResult);
}
}
}