From d9fa31e5d8a9c5d23547e8455d68b28b946c7581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Sun, 14 Jan 2024 16:55:33 +0100 Subject: [PATCH 1/3] refactor(TeslaFleetApiService): extract HandleUnsignedCommands method --- .../Services/Server/TeslaFleetApiService.cs | 19 +++++++++ .../Services/Server/TeslaMateApiService.cs | 7 +--- .../Server/Services/TeslaFleetApiService.cs | 40 +++++++++++-------- 3 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 TeslaSolarCharger.Tests/Services/Server/TeslaFleetApiService.cs diff --git a/TeslaSolarCharger.Tests/Services/Server/TeslaFleetApiService.cs b/TeslaSolarCharger.Tests/Services/Server/TeslaFleetApiService.cs new file mode 100644 index 000000000..03fe8e6e6 --- /dev/null +++ b/TeslaSolarCharger.Tests/Services/Server/TeslaFleetApiService.cs @@ -0,0 +1,19 @@ +using Newtonsoft.Json; +using System.Threading.Tasks; +using TeslaSolarCharger.Server.Dtos.TeslaFleetApi; +using Xunit; +using Xunit.Abstractions; + +namespace TeslaSolarCharger.Tests.Services.Server; + +public class TeslaFleetApiService(ITestOutputHelper outputHelper) : TestBase(outputHelper) +{ + [Fact] + public async Task CanHandleUnsignedCommands() + { + var commandResult = JsonConvert.DeserializeObject>("{\"response\":{\"result\":false,\"reason\":\"unsigned_cmds_hardlocked\"}}"); + Assert.NotNull(commandResult?.Response); + var fleetApiService = Mock.Create(); + await fleetApiService.HandleUnsignedCommands(commandResult.Response).ConfigureAwait(false); + } +} diff --git a/TeslaSolarCharger.Tests/Services/Server/TeslaMateApiService.cs b/TeslaSolarCharger.Tests/Services/Server/TeslaMateApiService.cs index a3ecfbd52..c8b94cf00 100644 --- a/TeslaSolarCharger.Tests/Services/Server/TeslaMateApiService.cs +++ b/TeslaSolarCharger.Tests/Services/Server/TeslaMateApiService.cs @@ -5,13 +5,8 @@ namespace TeslaSolarCharger.Tests.Services.Server; -public class TeslaMateApiService : TestBase +public class TeslaMateApiService(ITestOutputHelper outputHelper) : TestBase(outputHelper) { - public TeslaMateApiService(ITestOutputHelper outputHelper) - : base(outputHelper) - { - } - [Theory] [InlineData(18, null, null, false)] [InlineData(18, null, 19, true)] diff --git a/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs b/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs index 69604ca8b..94dfe5433 100644 --- a/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs +++ b/TeslaSolarCharger/Server/Services/TeslaFleetApiService.cs @@ -327,29 +327,35 @@ await backendApiService.PostErrorInformation(nameof(TeslaFleetApiService), nameo await backendApiService.PostErrorInformation(nameof(TeslaFleetApiService), nameof(SendCommandToTeslaApi), $"Result of command request is false {fleetApiRequest.RequestUrl}, {contentData}. Response string: {responseString}") .ConfigureAwait(false); - if (string.Equals(vehicleCommandResult.Reason, "unsigned_cmds_hardlocked")) - { - settings.FleetApiProxyNeeded = true; - //remove post after a few versions as only used for debugging - await backendApiService.PostErrorInformation(nameof(TeslaFleetApiService), nameof(SendCommandToTeslaApi), - "FleetAPI proxy needed set to true") - .ConfigureAwait(false); - if (!await IsFleetApiProxyNeededInDatabase().ConfigureAwait(false)) - { - teslaSolarChargerContext.TscConfigurations.Add(new TscConfiguration() - { - Key = constants.FleetApiProxyNeeded, - Value = true.ToString(), - }); - } - - } + await HandleUnsignedCommands(vehicleCommandResult).ConfigureAwait(false); } } logger.LogDebug("Response: {responseString}", responseString); return teslaCommandResultResponse; } + internal async Task HandleUnsignedCommands(DtoVehicleCommandResult vehicleCommandResult) + { + if (string.Equals(vehicleCommandResult.Reason, "unsigned_cmds_hardlocked")) + { + settings.FleetApiProxyNeeded = true; + //remove post after a few versions as only used for debugging + await backendApiService.PostErrorInformation(nameof(TeslaFleetApiService), nameof(SendCommandToTeslaApi), + "FleetAPI proxy needed set to true") + .ConfigureAwait(false); + if (!await IsFleetApiProxyNeededInDatabase().ConfigureAwait(false)) + { + teslaSolarChargerContext.TscConfigurations.Add(new TscConfiguration() + { + Key = constants.FleetApiProxyNeeded, + Value = true.ToString(), + }); + await teslaSolarChargerContext.SaveChangesAsync().ConfigureAwait(false); + } + + } + } + public async Task IsFleetApiProxyNeededInDatabase() { return await teslaSolarChargerContext.TscConfigurations.AnyAsync(c => c.Key == constants.FleetApiProxyNeeded).ConfigureAwait(false); From d50cdea31cb8a023dd006a3501a393d20d22d883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Sun, 14 Jan 2024 17:04:05 +0100 Subject: [PATCH 2/3] fix(Tests): fix FleetApiTest for handling unsinged commands --- .../Services/Server/TeslaFleetApiService.cs | 9 ++++++++- TeslaSolarCharger.Tests/TestBase.cs | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/TeslaSolarCharger.Tests/Services/Server/TeslaFleetApiService.cs b/TeslaSolarCharger.Tests/Services/Server/TeslaFleetApiService.cs index 03fe8e6e6..eb2bebb35 100644 --- a/TeslaSolarCharger.Tests/Services/Server/TeslaFleetApiService.cs +++ b/TeslaSolarCharger.Tests/Services/Server/TeslaFleetApiService.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; using TeslaSolarCharger.Server.Dtos.TeslaFleetApi; using Xunit; @@ -6,6 +7,7 @@ namespace TeslaSolarCharger.Tests.Services.Server; +[SuppressMessage("ReSharper", "UseConfigureAwaitFalse")] public class TeslaFleetApiService(ITestOutputHelper outputHelper) : TestBase(outputHelper) { [Fact] @@ -14,6 +16,11 @@ public async Task CanHandleUnsignedCommands() var commandResult = JsonConvert.DeserializeObject>("{\"response\":{\"result\":false,\"reason\":\"unsigned_cmds_hardlocked\"}}"); Assert.NotNull(commandResult?.Response); var fleetApiService = Mock.Create(); - await fleetApiService.HandleUnsignedCommands(commandResult.Response).ConfigureAwait(false); + var fleetApiProxyNeeded = await fleetApiService.IsFleetApiProxyNeededInDatabase(); + Assert.False(fleetApiProxyNeeded); + await fleetApiService.HandleUnsignedCommands(commandResult.Response); + fleetApiProxyNeeded = await fleetApiService.IsFleetApiProxyNeededInDatabase(); + Assert.True(fleetApiProxyNeeded); + } } diff --git a/TeslaSolarCharger.Tests/TestBase.cs b/TeslaSolarCharger.Tests/TestBase.cs index d7898f2be..bd6746ad9 100644 --- a/TeslaSolarCharger.Tests/TestBase.cs +++ b/TeslaSolarCharger.Tests/TestBase.cs @@ -16,7 +16,9 @@ using TeslaSolarCharger.Server.MappingExtensions; using TeslaSolarCharger.Shared.Contracts; using TeslaSolarCharger.Shared.TimeProviding; +using TeslaSolarCharger.SharedBackend.Contracts; using Xunit.Abstractions; +using Constants = TeslaSolarCharger.SharedBackend.Values.Constants; namespace TeslaSolarCharger.Tests; @@ -58,6 +60,7 @@ protected TestBase( _fake = new AutoFake(); _fake.Provide(); + _fake.Provide(); _fake.Provide(new FakeDateTimeProvider(currentFakeTime)); _fake.Provide(configuration); @@ -66,6 +69,7 @@ protected TestBase( { b.Register((_, _) => Context); b.Register((_, _) => _fake.Resolve()); + b.Register((_, _) => _fake.Resolve()); b.Register((_, _) => _fake.Resolve()); b.RegisterType(); //b.Register((_, _) => _fake.Resolve()); From 3a7e9961d7157d4e5af0fecf4471749fe0a850fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20K=C3=BChnel?= Date: Sun, 14 Jan 2024 17:07:52 +0100 Subject: [PATCH 3/3] fix(ChargeTimeCalculationServiceTest): remove mock IConstants --- .../Services/Server/ChargeTimeCalculationService.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/TeslaSolarCharger.Tests/Services/Server/ChargeTimeCalculationService.cs b/TeslaSolarCharger.Tests/Services/Server/ChargeTimeCalculationService.cs index 97d7671ed..08185524e 100644 --- a/TeslaSolarCharger.Tests/Services/Server/ChargeTimeCalculationService.cs +++ b/TeslaSolarCharger.Tests/Services/Server/ChargeTimeCalculationService.cs @@ -50,7 +50,6 @@ public void Calculates_Correct_Full_Speed_Charge_Durations(int minimumSoc, int? }; var chargeTimeCalculationService = Mock.Create(); - Mock.Mock().Setup(c => c.MinimumSocDifference).Returns(2); var chargeDuration = chargeTimeCalculationService.CalculateTimeToReachMinSocAtFullSpeedCharge(car); var expectedTimeSpan = TimeSpan.FromSeconds(expectedTotalSeconds);