diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index c5fb77e1..e3130b8b 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -24,15 +24,20 @@ namespace AsbCloudInfrastructure return context; } - public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) + public static void MapsterSetup() { TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .MapWith((source) => source.DateTime); TypeAdapterConfig.GlobalSettings.Default.Config .ForType() - .MapWith((source) => source == default ? new DateTime(0,DateTimeKind.Utc) : source ); + .MapWith((source) => source == default ? new DateTime(0, DateTimeKind.Utc) : source); + } + + public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) + { + MapsterSetup(); services.AddDbContext(options => options.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))); diff --git a/AsbCloudWebApi.Tests/ControllersTests/AnalyticsControllerTests.cs b/AsbCloudWebApi.Tests/ControllersTests/AnalyticsControllerTests.cs index fe386b37..53037a74 100644 --- a/AsbCloudWebApi.Tests/ControllersTests/AnalyticsControllerTests.cs +++ b/AsbCloudWebApi.Tests/ControllersTests/AnalyticsControllerTests.cs @@ -45,15 +45,7 @@ namespace AsbCloudWebApi.Tests.ControllersTests controller = new TelemetryAnalyticsController(analyticsService.Object, wellService.Object); - var user = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] - { - new Claim("idCompany", "1"), - }, "mock")); - controller.ControllerContext = new ControllerContext() - { - HttpContext = new DefaultHttpContext() { User = user } - }; } [Fact] diff --git a/AsbCloudWebApi.Tests/ControllersTests/ControllerExtentions.cs b/AsbCloudWebApi.Tests/ControllersTests/ControllerExtentions.cs new file mode 100644 index 00000000..450c145f --- /dev/null +++ b/AsbCloudWebApi.Tests/ControllersTests/ControllerExtentions.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using System.Text; +using System.Threading.Tasks; + +namespace AsbCloudWebApi.Tests.ControllersTests +{ + static class ControllerExtentions + { + public static void AddUser(this ControllerBase controller) + { + var claims = new Claim[] { new Claim("idCompany", "1") }; + controller.AddUser(claims); + } + + public static void AddUser(this ControllerBase controller, Claim[] claims) + { + var identity = new ClaimsIdentity(claims, "mock"); + var user = new ClaimsPrincipal(identity); + + controller.ControllerContext = new ControllerContext() + { + HttpContext = new DefaultHttpContext() { User = user } + }; + } + } +} diff --git a/AsbCloudWebApi.Tests/ControllersTests/TelemetryDataSaubControllerTests.cs b/AsbCloudWebApi.Tests/ControllersTests/TelemetryDataSaubControllerTests.cs new file mode 100644 index 00000000..60a6db17 --- /dev/null +++ b/AsbCloudWebApi.Tests/ControllersTests/TelemetryDataSaubControllerTests.cs @@ -0,0 +1,35 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using AsbCloudWebApi.Controllers; +using AsbCloudWebApi.SignalR; +using Microsoft.AspNetCore.SignalR; +using Microsoft.EntityFrameworkCore; +using Moq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace AsbCloudWebApi.Tests.ControllersTests +{ + public class TelemetryDataSaubControllerTests + { + private readonly Mock telemetryService; + private readonly Mock> telemetryDataService; + private readonly Mock wellService; + private readonly Mock> telemetryHubContext; + + public TelemetryDataSaubControllerTests() + { + telemetryService = new Mock(); + telemetryDataService = new Mock>(); + wellService = new Mock(); + telemetryHubContext = new Mock>(); + } + + + } +} diff --git a/AsbCloudWebApi.Tests/ServicesTests/TelemetryDataSaubServiceTest.cs b/AsbCloudWebApi.Tests/ServicesTests/TelemetryDataSaubServiceTest.cs new file mode 100644 index 00000000..63638083 --- /dev/null +++ b/AsbCloudWebApi.Tests/ServicesTests/TelemetryDataSaubServiceTest.cs @@ -0,0 +1,87 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using AsbCloudInfrastructure.Services; +using AsbCloudInfrastructure.Services.Cache; +using Moq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace AsbCloudWebApi.Tests.ServicesTests +{ + public class TelemetryDataSaubServiceTest + { + private readonly Mock telemetryTracker; + private readonly Mock timezoneService; + private readonly SimpleTimezoneDto timezone; + private readonly AsbCloudDbContext context; + private readonly CacheDb cacheDb; + private readonly TelemetryService telemetryService; + + private readonly DateTime drillingStartDate; + private readonly string uuid; + public TelemetryDataSaubServiceTest() + { + timezone = new() { Hours = 7 }; + drillingStartDate = new DateTime(2022, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); + uuid = drillingStartDate.ToString("yyyyMMdd_HHmmssfff"); + + AsbCloudInfrastructure.DependencyInjection.MapsterSetup(); + + telemetryTracker = new Mock(); + timezoneService = new Mock(); + timezoneService.Setup(s => s.GetByCoordinatesAsync(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(timezone)); + timezoneService.Setup(s => s.GetByCoordinates(It.IsAny(), It.IsAny())) + .Returns(timezone); + + context = TestHelpter.MakeTestContext(); + cacheDb = new CacheDb(); + telemetryService = new TelemetryService(context, telemetryTracker.Object, timezoneService.Object, cacheDb); + + var info = new TelemetryInfoDto + { + TimeZoneOffsetTotalHours = timezone.Hours, + DrillingStartDate = drillingStartDate, + }; + telemetryService.UpdateInfoAsync(uuid, info, CancellationToken.None).Wait(); + + } + ~TelemetryDataSaubServiceTest() + { + var ts = context.Telemetries.Where(t => t.RemoteUid == uuid); + context.Telemetries.RemoveRange(ts); + context.SaveChanges(); + context?.Dispose(); + } + + [Fact] + public async Task UpdateDataAsync() + { + // Arrange + var telemetryDataSaubService = new TelemetryDataSaubService(context, telemetryService, cacheDb); + + var now = DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(timezone.Hours)).DateTime; + var tuser = "Завулон"; + var newData = new List + { + new TelemetryDataSaubDto{ + Date = now, + AxialLoad = 1, + MseState = 1, + User = tuser, + } + }; + + // act + var affected = await telemetryDataSaubService.UpdateDataAsync(uuid, newData, CancellationToken.None); + + // assert + Assert.Equal(1, affected); + } + } +} diff --git a/AsbCloudWebApi.Tests/TestHelpter.cs b/AsbCloudWebApi.Tests/TestHelpter.cs new file mode 100644 index 00000000..7cf359bb --- /dev/null +++ b/AsbCloudWebApi.Tests/TestHelpter.cs @@ -0,0 +1,22 @@ +using AsbCloudDb.Model; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AsbCloudWebApi.Tests +{ + internal static class TestHelpter + { + public static AsbCloudDbContext MakeTestContext() + { + var options = new DbContextOptionsBuilder() + .UseNpgsql("Host=localhost;Database=tests;Username=postgres;Password=q;Persist Security Info=True;Include Error Detail=True") + .Options; + var context = new AsbCloudDbContext(options); + return context; + } + } +}