using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Data.DetectedOperation; using AsbCloudApp.Data.SAUB; using AsbCloudApp.Data.User; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudInfrastructure.Repository; using AsbCloudInfrastructure.Services; using AsbCloudInfrastructure.Services.SAUB; using NSubstitute; using Xunit; namespace AsbCloudWebApi.Tests.Services; public class DataSaubStatServiceTest { private readonly IDataSaubStatRepository dataSaubStatRepositoryMock = Substitute.For(); private readonly ITelemetryDataCache telemetryDataCacheMock = Substitute.For>(); private readonly IDetectedOperationRepository detectedOperationRepositoryMock = Substitute.For(); private readonly ITelemetryDataSaubService dataSaubServiceMock = Substitute.For(); private DataSaubStatService dataSaubStatService; private int[] idTelemetries = [1, 2, 3]; private IEnumerable dataSaubStatDtos = new List() { new DataSaubStatDto { Id = 1, AxialLoad = 1, AxialLoadLimitMax = 1, AxialLoadSp = 1, BlockSpeedSp = 1, DateEnd = DateTime.UtcNow, DateStart = DateTime.UtcNow.AddHours(-1), DepthEnd = 2, DepthStart = 1, EnabledSubsystems = 1, Flow = 1, HasOscillation = true, IdCategory = 1, IdFeedRegulator = 1, IdTelemetry = 1, Pressure = 1, PressureIdle = 1, PressureSp = 1, RotorSpeed = 1, RotorTorque = 1, RotorTorqueLimitMax = 1, RotorTorqueSp = 1, Speed = 1 }, new DataSaubStatDto { Id = 2, AxialLoad = 2, AxialLoadLimitMax = 2, AxialLoadSp = 2, BlockSpeedSp = 2, DateEnd = DateTime.UtcNow, DateStart = DateTime.UtcNow.AddHours(-1), DepthEnd = 3, DepthStart = 2, EnabledSubsystems = 2, Flow = 2, HasOscillation = true, IdCategory = 2, IdFeedRegulator = 2, IdTelemetry = 2, Pressure = 2, PressureIdle = 2, PressureSp = 2, RotorSpeed = 2, RotorTorque = 2, RotorTorqueLimitMax = 2, RotorTorqueSp = 2, Speed = 2 }, new DataSaubStatDto { Id = 3, AxialLoad = 3, AxialLoadLimitMax = 3, AxialLoadSp = 3, BlockSpeedSp = 3, DateEnd = DateTime.UtcNow, DateStart = DateTime.UtcNow.AddHours(-1), DepthEnd = 4, DepthStart = 3, EnabledSubsystems = 3, Flow = 3, HasOscillation = true, IdCategory = 3, IdFeedRegulator = 3, IdTelemetry = 3, Pressure = 3, PressureIdle = 3, PressureSp = 3, RotorSpeed = 3, RotorTorque = 3, RotorTorqueLimitMax = 3, RotorTorqueSp = 3, Speed = 3 }, }; private IEnumerable detectedOperationDtos = new List() { new DetectedOperationDto { Id = 1, DateEnd = DateTimeOffset.UtcNow, DateStart = DateTimeOffset.UtcNow.AddHours(-1), DepthStart = 1, DepthEnd = 2, IdCategory = 5002, IdTelemetry = 1, Value = 1, IdEditor = 1, IdUserAtStart = 1 } }; private IEnumerable telemetryDataSaubDtos1 = new List { new TelemetryDataSaubDto() { IdTelemetry = 1, DateTime = DateTime.UtcNow.AddMinutes(-30), AxialLoad = 1, AxialLoadLimitMax = 1, AxialLoadSp = 1, BitDepth = 1, BlockPosition = 1, BlockPositionMax = 1, BlockPositionMin = 1, BlockSpeed = 1, BlockSpeedSp = 1, BlockSpeedSpDevelop = 1, BlockSpeedSpRotor = 1, BlockSpeedSpSlide = 1, Flow = 1, FlowDeltaLimitMax = 1, FlowIdle = 1, HookWeight = 1, HookWeightIdle = 1, HookWeightLimitMax = 1, HookWeightLimitMin = 1, IdFeedRegulator = 1, IdUser = 1, Mode = 1, Mse = 1, MseState = 1, Pressure = 1, PressureDeltaLimitMax = 1, PressureIdle = 1, PressureSp = 1, PressureSpDevelop = 1, PressureSpRotor = 1, PressureSpSlide = 1, Pump0Flow = 1, Pump1Flow = 1, Pump2Flow = 1, RotorSpeed = 1, RotorTorque = 1, RotorTorqueIdle = 1, RotorTorqueSp = 1, RotorTorqueLimitMax = 1, WellDepth = 1, }, new TelemetryDataSaubDto() { IdTelemetry = 1, DateTime = DateTime.UtcNow.AddMinutes(-20), AxialLoad = 1, AxialLoadLimitMax = 1, AxialLoadSp = 1, BitDepth = 1, BlockPosition = 1, BlockPositionMax = 1, BlockPositionMin = 1, BlockSpeed = 1, BlockSpeedSp = 1, BlockSpeedSpDevelop = 1, BlockSpeedSpRotor = 1, BlockSpeedSpSlide = 1, Flow = 1, FlowDeltaLimitMax = 1, FlowIdle = 1, HookWeight = 1, HookWeightIdle = 1, HookWeightLimitMax = 1, HookWeightLimitMin = 1, IdFeedRegulator = 1, IdUser = 1, Mode = 1, Mse = 1, MseState = 1, Pressure = 1, PressureDeltaLimitMax = 1, PressureIdle = 1, PressureSp = 1, PressureSpDevelop = 1, PressureSpRotor = 1, PressureSpSlide = 1, Pump0Flow = 1, Pump1Flow = 1, Pump2Flow = 1, RotorSpeed = 1, RotorTorque = 1, RotorTorqueIdle = 1, RotorTorqueSp = 1, RotorTorqueLimitMax = 1, WellDepth = 1, }, new TelemetryDataSaubDto() { IdTelemetry = 1, DateTime = DateTime.UtcNow.AddMinutes(-10), AxialLoad = 1, AxialLoadLimitMax = 1, AxialLoadSp = 1, BitDepth = 1, BlockPosition = 1, BlockPositionMax = 1, BlockPositionMin = 1, BlockSpeed = 1, BlockSpeedSp = 1, BlockSpeedSpDevelop = 1, BlockSpeedSpRotor = 1, BlockSpeedSpSlide = 1, Flow = 1, FlowDeltaLimitMax = 1, FlowIdle = 1, HookWeight = 1, HookWeightIdle = 1, HookWeightLimitMax = 1, HookWeightLimitMin = 1, IdFeedRegulator = 1, IdUser = 1, Mode = 1, Mse = 1, MseState = 1, Pressure = 1, PressureDeltaLimitMax = 1, PressureIdle = 1, PressureSp = 1, PressureSpDevelop = 1, PressureSpRotor = 1, PressureSpSlide = 1, Pump0Flow = 1, Pump1Flow = 1, Pump2Flow = 1, RotorSpeed = 1, RotorTorque = 1, RotorTorqueIdle = 1, RotorTorqueSp = 1, RotorTorqueLimitMax = 1, WellDepth = 2, }, new TelemetryDataSaubDto() { IdTelemetry = 1, DateTime = DateTime.UtcNow.AddMinutes(-5), AxialLoad = 1, AxialLoadLimitMax = 1, AxialLoadSp = 1, BitDepth = 1, BlockPosition = 1, BlockPositionMax = 1, BlockPositionMin = 1, BlockSpeed = 1, BlockSpeedSp = 1, BlockSpeedSpDevelop = 1, BlockSpeedSpRotor = 1, BlockSpeedSpSlide = 1, Flow = 1, FlowDeltaLimitMax = 1, FlowIdle = 1, HookWeight = 1, HookWeightIdle = 1, HookWeightLimitMax = 1, HookWeightLimitMin = 1, IdFeedRegulator = 1, IdUser = 1, Mode = 1, Mse = 1, MseState = 1, Pressure = 1, PressureDeltaLimitMax = 1, PressureIdle = 1, PressureSp = 1, PressureSpDevelop = 1, PressureSpRotor = 1, PressureSpSlide = 1, Pump0Flow = 1, Pump1Flow = 1, Pump2Flow = 1, RotorSpeed = 1, RotorTorque = 1, RotorTorqueIdle = 1, RotorTorqueSp = 1, RotorTorqueLimitMax = 1, WellDepth = 3, }, new TelemetryDataSaubDto() { IdTelemetry = 1, DateTime = DateTime.UtcNow.AddMinutes(5), AxialLoad = 1, AxialLoadLimitMax = 1, AxialLoadSp = 1, BitDepth = 1, BlockPosition = 1, BlockPositionMax = 1, BlockPositionMin = 1, BlockSpeed = 1, BlockSpeedSp = 1, BlockSpeedSpDevelop = 1, BlockSpeedSpRotor = 1, BlockSpeedSpSlide = 1, Flow = 1, FlowDeltaLimitMax = 1, FlowIdle = 1, HookWeight = 1, HookWeightIdle = 1, HookWeightLimitMax = 1, HookWeightLimitMin = 1, IdFeedRegulator = 1, IdUser = 1, Mode = 1, Mse = 1, MseState = 1, Pressure = 1, PressureDeltaLimitMax = 1, PressureIdle = 1, PressureSp = 1, PressureSpDevelop = 1, PressureSpRotor = 1, PressureSpSlide = 1, Pump0Flow = 1, Pump1Flow = 1, Pump2Flow = 1, RotorSpeed = 1, RotorTorque = 1, RotorTorqueIdle = 1, RotorTorqueSp = 1, RotorTorqueLimitMax = 1, WellDepth = 1, } }; private IEnumerable telemetryDataSaubDtos2 = new List { new TelemetryDataSaubDto() { IdTelemetry = 2, DateTime = DateTime.Now.AddMinutes(-20), AxialLoad = 1, AxialLoadLimitMax = 1, AxialLoadSp = 1, BitDepth = 1, BlockPosition = 1, BlockPositionMax = 1, BlockPositionMin = 1, BlockSpeed = 1, BlockSpeedSp = 1, BlockSpeedSpDevelop = 1, BlockSpeedSpRotor = 1, BlockSpeedSpSlide = 1, Flow = 1, FlowDeltaLimitMax = 1, FlowIdle = 1, HookWeight = 1, HookWeightIdle = 1, HookWeightLimitMax = 1, HookWeightLimitMin = 1, IdFeedRegulator = 1, IdUser = 1, Mode = 1, Mse = 1, MseState = 1, Pressure = 1, PressureDeltaLimitMax = 1, PressureIdle = 1, PressureSp = 1, PressureSpDevelop = 1, PressureSpRotor = 1, PressureSpSlide = 1, Pump0Flow = 1, Pump1Flow = 1, Pump2Flow = 1, RotorSpeed = 1, RotorTorque = 1, RotorTorqueIdle = 1, RotorTorqueSp = 1, RotorTorqueLimitMax = 1, WellDepth = 1, }, new TelemetryDataSaubDto() { IdTelemetry = 2, DateTime = DateTime.Now.AddMinutes(-20), AxialLoad = 1, AxialLoadLimitMax = 1, AxialLoadSp = 1, BitDepth = 1, BlockPosition = 1, BlockPositionMax = 1, BlockPositionMin = 1, BlockSpeed = 1, BlockSpeedSp = 1, BlockSpeedSpDevelop = 1, BlockSpeedSpRotor = 1, BlockSpeedSpSlide = 1, Flow = 1, FlowDeltaLimitMax = 1, FlowIdle = 1, HookWeight = 1, HookWeightIdle = 1, HookWeightLimitMax = 1, HookWeightLimitMin = 1, IdFeedRegulator = 1, IdUser = 1, Mode = 1, Mse = 1, MseState = 1, Pressure = 1, PressureDeltaLimitMax = 1, PressureIdle = 1, PressureSp = 1, PressureSpDevelop = 1, PressureSpRotor = 1, PressureSpSlide = 1, Pump0Flow = 1, Pump1Flow = 1, Pump2Flow = 1, RotorSpeed = 1, RotorTorque = 1, RotorTorqueIdle = 1, RotorTorqueSp = 1, RotorTorqueLimitMax = 1, WellDepth = 1, } }; public DataSaubStatServiceTest() { telemetryDataCacheMock .GetIds(Arg.Any()) .Returns(idTelemetries); dataSaubStatRepositoryMock .GetLastsAsync(Arg.Any(), Arg.Any()) .Returns(dataSaubStatDtos); detectedOperationRepositoryMock .Get(Arg.Any(), Arg.Any()) .Returns(detectedOperationDtos); dataSaubServiceMock .Get(1, Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) .Returns(telemetryDataSaubDtos1); dataSaubStatRepositoryMock .InsertRangeAsync(Arg.Any>(), Arg.Any()) .Returns(2); dataSaubStatService = new DataSaubStatService( dataSaubStatRepositoryMock, telemetryDataCacheMock, dataSaubServiceMock, detectedOperationRepositoryMock); } [Fact] public async Task GetByMarkId_ShouldReturn_FileInfo() { //act //var data = await fileService.GetByMarkId(idMark, CancellationToken.None); Action action = async (a, b) => { ; }; await dataSaubStatService.CreateStatAsync(5, action, CancellationToken.None); //assert Assert.NotNull("sd"); } }