From f465cd4ad9a7cb2f1bc22d75d3deedc9817ef697 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Sat, 7 Mar 2020 16:37:14 +0200 Subject: [PATCH] Extract FirebaseRestTranslator into a nugget package --- NucuCar.Domain/NucuCar.Domain.csproj | 1 + .../Telemetry/TelemetryPublisherFirestore.cs | 2 +- .../Utilities/FirebaseRestTranslator.cs | 228 ---------------- .../Utilities/FirebaseRestTranslatorTests.cs | 257 ------------------ NucuCar.sln.DotSettings.user | 1 + 5 files changed, 3 insertions(+), 486 deletions(-) delete mode 100644 NucuCar.Domain/Utilities/FirebaseRestTranslator.cs delete mode 100644 NucuCar.UnitTests/NucuCar.Domain.Tests/Utilities/FirebaseRestTranslatorTests.cs diff --git a/NucuCar.Domain/NucuCar.Domain.csproj b/NucuCar.Domain/NucuCar.Domain.csproj index e97d6f1..739106f 100644 --- a/NucuCar.Domain/NucuCar.Domain.csproj +++ b/NucuCar.Domain/NucuCar.Domain.csproj @@ -16,6 +16,7 @@ + diff --git a/NucuCar.Domain/Telemetry/TelemetryPublisherFirestore.cs b/NucuCar.Domain/Telemetry/TelemetryPublisherFirestore.cs index 91c7081..edcd916 100644 --- a/NucuCar.Domain/Telemetry/TelemetryPublisherFirestore.cs +++ b/NucuCar.Domain/Telemetry/TelemetryPublisherFirestore.cs @@ -64,7 +64,7 @@ namespace NucuCar.Domain.Telemetry cts.CancelAfter(_timeout); try { - var data = FirebaseRestTranslator.Translate(null, GetTelemetry()); + var data = FirebaseRestTranslator.Translator.Translate(null, GetTelemetry()); var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); await _httpClient.PostAsync("", content, cts.Token); Logger?.LogInformation("Published data to Firestore!"); diff --git a/NucuCar.Domain/Utilities/FirebaseRestTranslator.cs b/NucuCar.Domain/Utilities/FirebaseRestTranslator.cs deleted file mode 100644 index d371a99..0000000 --- a/NucuCar.Domain/Utilities/FirebaseRestTranslator.cs +++ /dev/null @@ -1,228 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace NucuCar.Domain.Utilities -{ - /// - /// This class is used to translate C# dictionaries into firebase json format. - /// - public static class FirebaseRestTranslator - { - public class ReferenceValue - { - private readonly string _value; - - public ReferenceValue(string value) - { - _value = value; - } - - public override string ToString() - { - return _value; - } - } - - public class GeoPointValue - { - private readonly double _latitude; - private readonly double _longitude; - - public GeoPointValue(double latitude, double longitude) - { - _latitude = latitude; - _longitude = longitude; - } - - public Dictionary ToValue() - { - return new Dictionary - { - ["latitude"] = _latitude, - ["longitude"] = _longitude - }; - } - } - - public static Dictionary Translate(string name, Dictionary dict) - { - return BuildRoot(name, dict); - } - - private static Dictionary BuildRoot(string name, Dictionary dict) - { - var root = new Dictionary(); - if (name != null) - { - root["name"] = name; - } - - root["fields"] = new Dictionary(); - // iterate through fields and build leaf - foreach (var entry in dict) - { - var fields = (Dictionary) root["fields"]; - fields[entry.Key] = BuildNode(entry.Value); - } - - return root; - } - - private static Dictionary BuildNode(object value) - { - switch (value) - { - case string v: - { - return BuildString(v); - } - case int v: - { - return BuildInteger(v); - } - case double v: - { - return BuildDouble(v); - } - case bool v: - { - return BuildBool(v); - } - case null: - { - return BuildNull(null); - } - case byte[] v: - { - return BuildBytes(v); - } - case DateTime v: - { - return BuildTimestamp(v); - } - case ReferenceValue v: - { - return BuildReference(v); - } - case GeoPointValue v: - { - return BuildGeoPoint(v); - } - case List> v: - { - return BuildArray(v); - } - case Dictionary[] v: - { - return BuildArray(new List>(v)); - } - case Dictionary v: - { - return BuildMap(v); - } - default: - { - if (value.GetType().IsEnum) - { - return BuildInteger((int) value); - } - - break; - } - } - - throw new ArgumentException($"Can't build leaf! Unknown type for: {value}"); - } - - private static Dictionary BuildSimpleValue(string type, object value) - { - return new Dictionary() - { - [type] = value - }; - } - - private static Dictionary BuildString(string value) - { - return BuildSimpleValue("stringValue", value); - } - - private static Dictionary BuildInteger(int value) - { - return BuildSimpleValue("integerValue", value); - } - - private static Dictionary BuildTimestamp(DateTime value) - { - return BuildSimpleValue("timestampValue", value); - } - - private static Dictionary BuildDouble(double value) - { - return BuildSimpleValue("doubleValue", value); - } - - private static Dictionary BuildBool(bool value) - { - return BuildSimpleValue("booleanValue", value); - } - - private static Dictionary BuildNull(object value) - { - return BuildSimpleValue("nullValue", value); - } - - private static Dictionary BuildBytes(byte[] value) - { - return BuildSimpleValue("bytesValue", Convert.ToBase64String(value)); - } - - private static Dictionary BuildReference(ReferenceValue value) - { - return BuildSimpleValue("referenceValue", value.ToString()); - } - - private static Dictionary BuildGeoPoint(GeoPointValue value) - { - return BuildSimpleValue("geoPointValue", value.ToValue()); - } - - - private static Dictionary BuildArray(List> array) - { - var values = new List>(); - var root = new Dictionary - { - ["arrayValue"] = new Dictionary - { - ["values"] = values - } - }; - - foreach (var entry in array) - { - values.Add(BuildNode(entry)); - } - - return root; - } - - private static Dictionary BuildMap(Dictionary map) - { - var fields = new Dictionary(); - var root = new Dictionary - { - ["mapValue"] = new Dictionary - { - ["fields"] = fields - } - }; - foreach (var entry in map) - { - fields[entry.Key] = BuildNode(entry.Value); - } - - return root; - } - } -} \ No newline at end of file diff --git a/NucuCar.UnitTests/NucuCar.Domain.Tests/Utilities/FirebaseRestTranslatorTests.cs b/NucuCar.UnitTests/NucuCar.Domain.Tests/Utilities/FirebaseRestTranslatorTests.cs deleted file mode 100644 index 94565f9..0000000 --- a/NucuCar.UnitTests/NucuCar.Domain.Tests/Utilities/FirebaseRestTranslatorTests.cs +++ /dev/null @@ -1,257 +0,0 @@ -using System; -using System.Collections.Generic; -using Newtonsoft.Json; -using NucuCar.Domain.Utilities; -using Xunit; -using Xunit.Abstractions; - -namespace NucuCar.UnitTests.NucuCar.Domain.Tests.Utilities -{ - enum Season - { - Spring, - Summer, - Autumn, - Winter - } - - - /* - { - "source":"NucuCar.Sensors", - "timestamp":"2019-12-01T23:26:13.5537227+02:00", - "data":[ - { - "sensor_state":2, - "temperature":32.65558333857916, - "humidity":100.0, - "pressure":62228.49565168124, - "voc":0.0, - "_id":"Bme680-Sensor" - }, - { - "sensor_state":2, - "cpu_temperature":48.849998474121094, - "_id":"CpuTemperature" - } - ] - } - */ - public class FirebaseRestTranslatorTests - { - public FirebaseRestTranslatorTests(ITestOutputHelper testOutputHelper) - { - } - - private Dictionary getBasicTelemetryData() - { - var basicTelemetryDict = new Dictionary - { - ["source"] = "NucuCar.Sensors", - ["timestamp"] = "2019-12-01T23:26:13.5537227+02:00" - }; - var data = new List> - { - new Dictionary() - { - ["sensor_state"] = 2, - ["cpu_temperature"] = 48.849998474121094, - ["_id"] = "CpuTemperature", - }, - new Dictionary() - { - ["sensor_state"] = 2, - ["temperature"] = 32.65, - ["humidity"] = 100.0, - ["pressure"] = 62228.49, - ["voc"] = 0.0, - ["_id"] = "Bme680-Sensor" - } - }; - basicTelemetryDict["data"] = data; - return basicTelemetryDict; - } - - [Fact] - public void Test_FirebaseTranslator_Parse() - { - var expectedJson = - "{\"name\":\"Test\",\"fields\":{\"source\":{\"stringValue\":\"NucuCar.Sensors\"},\"timestamp\":{\"stringValue\":\"2019-12-01T23:26:13.5537227+02:00\"},\"data\":{\"arrayValue\":{\"values\":[{\"mapValue\":{\"fields\":{\"sensor_state\":{\"integerValue\":2},\"cpu_temperature\":{\"doubleValue\":48.849998474121094},\"_id\":{\"stringValue\":\"CpuTemperature\"}}}},{\"mapValue\":{\"fields\":{\"sensor_state\":{\"integerValue\":2},\"temperature\":{\"doubleValue\":32.65},\"humidity\":{\"doubleValue\":100.0},\"pressure\":{\"doubleValue\":62228.49},\"voc\":{\"doubleValue\":0.0},\"_id\":{\"stringValue\":\"Bme680-Sensor\"}}}}]}}}}"; - var basicTelemetryData = getBasicTelemetryData(); - var result = FirebaseRestTranslator.Translate("Test", basicTelemetryData); - var json = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, json); - } - - [Fact] - public void Test_FirebaseTranslator_StringValue() - { - var data = new Dictionary() - { - ["myKey"] = "myValue" - }; - var expectedJson = "{\"name\":\"Test_FirebaseTranslator_StringValue\",\"fields\":{\"myKey\":{\"stringValue\":\"myValue\"}}}"; - var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator_StringValue", data); - var actualJson = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, actualJson); - } - - [Fact] - public void Test_FirebaseTranslator_IntegerValue() - { - var data = new Dictionary() - { - ["myKey"] = 23 - }; - var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"integerValue\":23}}}"; - var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); - var actualJson = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, actualJson); - } - - [Fact] - public void Test_FirebaseTranslator_DoubleValue() - { - var data = new Dictionary() - { - ["myKey"] = 11.20 - }; - var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"doubleValue\":11.2}}}"; - var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); - var actualJson = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, actualJson); - } - - [Fact] - public void Test_FirebaseTranslator_BoolValue() - { - var data = new Dictionary() - { - ["myKey"] = false - }; - var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"booleanValue\":false}}}"; - var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); - var actualJson = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, actualJson); - } - - [Fact] - public void Test_FirebaseTranslator_TimestampValue() - { - var data = new Dictionary() - { - ["myKey"] = new DateTime(2020, 2, 29, 0, 0, 0, 0) - }; - var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"timestampValue\":\"2020-02-29T00:00:00\"}}}"; - var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); - var actualJson = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, actualJson); - } - - [Fact] - public void Test_FirebaseTranslator_EnumValue() - { - var data = new Dictionary() - { - ["myKey"] = Season.Winter - }; - var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"integerValue\":3}}}"; - var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); - var actualJson = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, actualJson); - } - - [Fact] - public void Test_FirebaseTranslator_ArrayValue() - { - var data = new Dictionary() - { - ["myKey"] = new List> - { - new Dictionary - { - ["arrayIndex0"] = 11.20 - }, - new Dictionary - { - ["arrayIndex1"] = "test" - } - } - }; - var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"arrayValue\":{\"values\":[{\"mapValue\":{\"fields\":{\"arrayIndex0\":{\"doubleValue\":11.2}}}},{\"mapValue\":{\"fields\":{\"arrayIndex1\":{\"stringValue\":\"test\"}}}}]}}}}"; - var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); - var actualJson = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, actualJson); - } - - [Fact] - public void Test_FirebaseTranslator_MapValue() - { - var data = new Dictionary() - { - ["myKey"] = new Dictionary - { - ["stringKey"] = "test", - ["boolKey"] = true, - ["intKey"] = 99 - } - }; - var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"mapValue\":{\"fields\":{\"stringKey\":{\"stringValue\":\"test\"},\"boolKey\":{\"booleanValue\":true},\"intKey\":{\"integerValue\":99}}}}}}"; - var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); - var actualJson = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, actualJson); - } - - [Fact] - public void Test_FirebaseTranslator_NullValue() - { - var data = new Dictionary() - { - ["myKey"] = null - }; - var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"nullValue\":null}}}"; - var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); - var actualJson = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, actualJson); - } - - [Fact] - public void Test_FirebaseTranslator_BytesValue() - { - var data = new Dictionary() - { - ["myKey"] = new byte[] {97, 98, 99, 100, 101, 102, 103, 104, 105 } - }; - var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"bytesValue\":\"YWJjZGVmZ2hp\"}}}"; - var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); - var actualJson = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, actualJson); - } - - [Fact] - public void Test_FirebaseTranslator_ReferenceValue() - { - var data = new Dictionary() - { - ["myKey"] = new FirebaseRestTranslator.ReferenceValue("test") - }; - var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"referenceValue\":\"test\"}}}"; - var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); - var actualJson = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, actualJson); - } - - [Fact] - public void Test_FirebaseTranslator_GeoPointvalue() - { - var data = new Dictionary() - { - ["myKey"] = new FirebaseRestTranslator.GeoPointValue(10, 22) - }; - var expectedJson = "{\"name\":\"Test_FirebaseTranslator\",\"fields\":{\"myKey\":{\"geoPointValue\":{\"latitude\":10.0,\"longitude\":22.0}}}}"; - var result = FirebaseRestTranslator.Translate("Test_FirebaseTranslator", data); - var actualJson = JsonConvert.SerializeObject(result); - Assert.Equal(expectedJson, actualJson); - } - } -} \ No newline at end of file diff --git a/NucuCar.sln.DotSettings.user b/NucuCar.sln.DotSettings.user index 35f173d..a20925d 100644 --- a/NucuCar.sln.DotSettings.user +++ b/NucuCar.sln.DotSettings.user @@ -1,4 +1,5 @@  <AssemblyExplorer> <Assembly Path="/home/denis/.nuget/packages/iot.device.bindings/1.0.0/lib/netcoreapp2.1/Iot.Device.Bindings.dll" /> + <Assembly Path="/home/denis/.nuget/packages/firebaseresttranslator/0.1.1/lib/netcoreapp3.0/FirebaseRestTranslator.dll" /> </AssemblyExplorer> \ No newline at end of file