Skip to content

Commit

Permalink
Fix(device): Fix issue with .NET standard 5 preview -> Not supported …
Browse files Browse the repository at this point in the history
…exceptions. Fixes #1592 (#1594)
  • Loading branch information
azabbasi authored Oct 3, 2020
1 parent f9b7bec commit e41a277
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 156 deletions.
154 changes: 0 additions & 154 deletions iothub/device/src/Common/ReadOnlyMergeDictionary.cs

This file was deleted.

18 changes: 18 additions & 0 deletions iothub/device/src/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Azure.Devices.Client.Extensions;
using Newtonsoft.Json;
Expand Down Expand Up @@ -106,5 +109,20 @@ public static void ValidateDataIsEmptyOrJson(byte[] data)
}
}
}

public static IReadOnlyDictionary<TKey, TValue> MergeDictionaries<TKey, TValue>(IDictionary<TKey, TValue>[] dictionaries)
{
// No item in the array should be null.
if (dictionaries == null || dictionaries.Any(item => item == null))
{
throw new ArgumentNullException(nameof(dictionaries), "Provided dictionaries should not be null");
}

Dictionary<TKey, TValue> result = dictionaries.SelectMany(dict => dict)
.ToLookup(pair => pair.Key, pair => pair.Value)
.ToDictionary(group => group.Key, group => group.First());

return new ReadOnlyDictionary<TKey, TValue>(result);
}
}
}
4 changes: 2 additions & 2 deletions iothub/device/src/Transport/Mqtt/MqttIotHubAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ public static async Task WriteMessageAsync(IChannelHandlerContext context, objec
}
}

internal string PopulateMessagePropertiesFromMessage(string topicName, Message message)
internal static string PopulateMessagePropertiesFromMessage(string topicName, Message message)
{
var systemProperties = new Dictionary<string, string>();
foreach (KeyValuePair<string, object> property in message.SystemProperties)
Expand All @@ -1247,7 +1247,7 @@ internal string PopulateMessagePropertiesFromMessage(string topicName, Message m
systemProperties[propertyName] = ConvertFromSystemProperties(property.Value);
}
}
string properties = UrlEncodedDictionarySerializer.Serialize(new ReadOnlyMergeDictionary<string, string>(systemProperties, message.Properties));
string properties = UrlEncodedDictionarySerializer.Serialize(Utils.MergeDictionaries(new IDictionary<string, string>[] { systemProperties, message.Properties }));

string msg = properties.Length != 0
? topicName.EndsWith(SegmentSeparator, StringComparison.Ordinal) ? topicName + properties + SegmentSeparator : topicName + SegmentSeparator + properties
Expand Down
60 changes: 60 additions & 0 deletions iothub/device/tests/UtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Azure.Devices.Client.Test
Expand Down Expand Up @@ -35,5 +36,64 @@ public void ConvertDeliveryAckTypeToStringInvalidValueFail()
{
Utils.ConvertDeliveryAckTypeToString((DeliveryAcknowledgement)100500);
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void MergeDictionaries_OneItemInListIsNull()
{
var dict1 = new Dictionary<int, string> { { 1, "One" }, { 2, "Two" }, { 3, "Three" } };

Utils.MergeDictionaries(new Dictionary<int, string>[] { dict1, null });
}

[TestMethod]
public void MergeDictionaries_TwoDictionaries_NoOverlap()
{
var dict1 = new Dictionary<int, string> { { 1, "One" }, { 2, "Two" }, { 3, "Three" } };
var dict2 = new Dictionary<int, string> { { 4, "Four" }, { 5, "Five" }, { 6, "Six" } };

var result = Utils.MergeDictionaries(new Dictionary<int, string>[] { dict1, dict2 });

Assert.AreEqual(6, result.Count, $"Number of items in the merged dictionary should be equal to {dict1.Count + dict2.Count}");
}

[TestMethod]
public void MergeDictionaries_ThreeDictionaries_NoOverlap()
{
var dict1 = new Dictionary<int, string> { { 1, "One" }, { 2, "Two" }, { 3, "Three" } };
var dict2 = new Dictionary<int, string> { { 4, "Four" }, { 5, "Five" }, { 6, "Six" } };
var dict3 = new Dictionary<int, string> { { 7, "Seven" }, { 8, "Eight" }, { 9, "Nine" } };

var result = Utils.MergeDictionaries(new Dictionary<int, string>[] { dict1, dict2, dict3 });

Assert.AreEqual(9, result.Count, $"Number of items in the merged dictionary should be equal to {dict1.Count + dict2.Count + dict3.Count}");
}

[TestMethod]
public void MergeDictionaries_TwoDictionaries_OneOverLap_PicksFirst()
{
var dict1 = new Dictionary<int, string> { { 1, "One" }, { 2, "Two" }, { 3, "Three" } };
var dict2 = new Dictionary<int, string> { { 1, "One1" }, { 4, "Four" }, { 5, "Five" } };

var result = Utils.MergeDictionaries(new Dictionary<int, string>[] { dict1, dict2 });

// There is one overlapping pair, we should pick the first one.
Assert.AreEqual(5, result.Count, $"Number of items in the merged dictionary should be equal to {dict1.Count + dict2.Count - 1}");
Assert.AreEqual(dict1[1], result[1], $"The first item in the list takes priority");
}

[TestMethod]
public void MergeDictionaries_ThreeDictionaries_OneOverLap_PicksFirst()
{
var dict1 = new Dictionary<int, string> { { 1, "One" }, { 2, "Two" }, { 3, "Three" } };
var dict2 = new Dictionary<int, string> { { 1, "One1" }, { 4, "Four" }, { 5, "Five" } };
var dict3 = new Dictionary<int, string> { { 1, "One2" }, { 6, "Six" }, { 7, "Seven" } };

var result = Utils.MergeDictionaries(new Dictionary<int, string>[] { dict1, dict2, dict3 });

// There is one overlapping pair, we should pick the first one.
Assert.AreEqual(7, result.Count, $"Number of items in the merged dictionary should be equal to {dict1.Count + dict2.Count + dict3.Count - 2}");
Assert.AreEqual(dict1[1], result[1], $"The first item in the list takes priority");
}
}
}

0 comments on commit e41a277

Please sign in to comment.