diff --git a/NucuCar.Domain.Telemetry/Publishers/TelemetryPublisherFirestore.cs b/NucuCar.Domain.Telemetry/Publishers/TelemetryPublisherFirestore.cs index 3d52138..1d11712 100644 --- a/NucuCar.Domain.Telemetry/Publishers/TelemetryPublisherFirestore.cs +++ b/NucuCar.Domain.Telemetry/Publishers/TelemetryPublisherFirestore.cs @@ -1,12 +1,14 @@ using System; using System.Collections.Generic; using System.Net; +using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using NucuCar.Domain.Http; using NucuCar.Domain.Utilities; using NucuCar.Telemetry.Abstractions; +using HttpClient = NucuCar.Domain.Http.HttpClient; namespace NucuCar.Telemetry.Publishers { @@ -103,7 +105,18 @@ namespace NucuCar.Telemetry.Publishers } var data = FirebaseRestTranslator.Translator.Translate(null, GetTelemetry()); - var responseMessage = await HttpClient.PostAsync("", data); + + HttpResponseMessage responseMessage = null; + try + { + responseMessage = await HttpClient.PostAsync("", data); + } + // ArgumentException occurs during json serialization errors. + catch (ArgumentException e) + { + Logger?.LogWarning(e.Message); + } + switch (responseMessage?.StatusCode) { diff --git a/NucuCar.Domain/Http/HttpClient.cs b/NucuCar.Domain/Http/HttpClient.cs index 7e9541e..96004e6 100644 --- a/NucuCar.Domain/Http/HttpClient.cs +++ b/NucuCar.Domain/Http/HttpClient.cs @@ -80,7 +80,6 @@ namespace NucuCar.Domain.Http #endregion - #region Public Methods public void Authorization(string scheme, string token) diff --git a/NucuCar.UnitTests/NucuCar.Domain.Telemetry.Tests/TelemetryPublisherFirestoreTest.cs b/NucuCar.UnitTests/NucuCar.Domain.Telemetry.Tests/TelemetryPublisherFirestoreTest.cs index 6d47c3e..c875358 100644 --- a/NucuCar.UnitTests/NucuCar.Domain.Telemetry.Tests/TelemetryPublisherFirestoreTest.cs +++ b/NucuCar.UnitTests/NucuCar.Domain.Telemetry.Tests/TelemetryPublisherFirestoreTest.cs @@ -93,6 +93,27 @@ namespace NucuCar.UnitTests.NucuCar.Domain.Telemetry.Tests Assert.Equal("{\"fields\":{\"testData\":{\"integerValue\":1}}}", request.Content.ReadAsStringAsync().GetAwaiter().GetResult()); } + + [Fact] + private async Task Test_PublishAsync_InvalidJson() + { + // Setup + var opts = new TelemetryPublisherOptions() + { + ConnectionString = "ProjectId=test;CollectionName=test" + }; + var publisher = new MockTelemetryPublisherFirestore(opts); + var mockHttpClient = new MockHttpClient("http://testing.com"); + mockHttpClient.SendAsyncResponses.Add(new HttpResponseMessage(HttpStatusCode.OK)); + publisher.SetHttpClient(mockHttpClient); + publisher.SetMockData(new Dictionary {["testData"] = double.PositiveInfinity}); + + // Run + await publisher.PublishAsync(CancellationToken.None); + + // Assert no request made. + Assert.Empty(mockHttpClient.SendAsyncArgCalls); + } [Fact] private async Task Test_PublishAsync_Cancel() diff --git a/NucuCar.sln.DotSettings.user b/NucuCar.sln.DotSettings.user index 1046fae..d5d1b87 100644 --- a/NucuCar.sln.DotSettings.user +++ b/NucuCar.sln.DotSettings.user @@ -7,6 +7,11 @@ <TestAncestor> <TestId>xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Telemetry.Tests.TelemetryPublisherFirestoreTest.Test_PublishAsync_Cancel</TestId> </TestAncestor> +</SessionState> + <SessionState ContinuousTestingMode="0" Name="TelemetryPublisherFirestoreTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Domain.Telemetry.Tests.TelemetryPublisherFirestoreTest</TestId> + </TestAncestor> </SessionState> <SessionState ContinuousTestingMode="0" Name="Test_PublishAsync_Authorization_OK" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> <TestAncestor> @@ -24,5 +29,6 @@ <SessionState ContinuousTestingMode="0" Name="Test_PublishAsync_OK" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> <TestAncestor> <TestId>xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Domain.Tests.GuardTest</TestId> + <TestId>xUnit::C6F07921-1052-4945-911E-F328A622F229::.NETCoreApp,Version=v3.1::NucuCar.UnitTests.NucuCar.Domain.Telemetry.Tests.TelemetryPublisherFirestoreTest</TestId> </TestAncestor> </SessionState> \ No newline at end of file diff --git a/Readme.md b/Readme.md index 5a34021..6ad171b 100644 --- a/Readme.md +++ b/Readme.md @@ -40,7 +40,7 @@ Command line utility to play around with the car functionality. You can use it t To build the project and target the Raspberry Pi you can use the following command: ```$xslt -dotnet build --runtime linux-arm -p:PublishSingleFile=true +dotnet build --runtime linux-arm -p:PublishSingleFile=true --configuration Release ``` ---