From e8e149ec7fc87fe653eb6b3b8af5164e43004e52 Mon Sep 17 00:00:00 2001 From: Navin Kumar B Date: Mon, 9 Jan 2017 20:10:08 +0530 Subject: [PATCH 1/3] Supporting Multiple TestProperty with the same key value. - Changed the way of deserialization using custom TraitObject. - Added UnitTests for the same. --- .../TestProperty/CustomKeyValueConverter.cs | 21 ++++++++++++++--- .../CustomKeyValueConverterTests.cs | 23 +++++++++++-------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs b/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs index f050104dd0..36777744c8 100644 --- a/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs +++ b/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs @@ -43,14 +43,29 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c { using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(data))) { - var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Dictionary)); - var dict = serializer.ReadObject(stream) as Dictionary; + List> listOfKvps = new List>(); - return dict?.ToArray(); + // Converting Json data to array of KeyValuePairs with duplicate keys. + var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List)); + + var listOfTratiObjects = serializer.ReadObject(stream) as List; + listOfTratiObjects.ForEach(o=>listOfKvps.Add(new KeyValuePair(o.Key, o.Value))); + + return listOfKvps?.ToArray(); } } return base.ConvertFrom(context, culture, value); } + + [System.Runtime.Serialization.DataContract] + private class TraitObject + { + [System.Runtime.Serialization.DataMember(Name = "Key")] + public string Key { get; set; } + + [System.Runtime.Serialization.DataMember(Name = "Value")] + public string Value { get; set; } + } } } \ No newline at end of file diff --git a/test/Microsoft.TestPlatform.ObjectModel.UnitTests/CustomKeyValueConverterTests.cs b/test/Microsoft.TestPlatform.ObjectModel.UnitTests/CustomKeyValueConverterTests.cs index 148866e83f..44acbd3f32 100644 --- a/test/Microsoft.TestPlatform.ObjectModel.UnitTests/CustomKeyValueConverterTests.cs +++ b/test/Microsoft.TestPlatform.ObjectModel.UnitTests/CustomKeyValueConverterTests.cs @@ -60,25 +60,30 @@ public void CustomKeyValueConverterShouldDeserializeEmptyArray() } [TestMethod] - public void CustomKeyValueConverterShouldThrowOnDeserializeNullKeyOrValue() + public void CustomKeyValueConverterShouldDeserializeEmptyKeyOrValue() { - var json = "[{ \"Key\": null, \"Value\": \"\" }]"; + var json = "[{ \"Key\": \"\", \"Value\": \"\" }]"; - var action = new Action(() => this.customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json)); + var data = this.customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair[]; - Assert.ThrowsException(action); + Assert.AreEqual(1, data.Length); + Assert.AreEqual(string.Empty, data[0].Key); + Assert.AreEqual(string.Empty, data[0].Value); } [TestMethod] - public void CustomKeyValueConverterShouldDeserializeEmptyKeyOrValue() + public void CustomKeyValueConverterShouldDeserializeDuplicateKeysKvps() { - var json = "[{ \"Key\": \"\", \"Value\": \"\" }]"; + var json = "[{ \"Key\": \"key1\", \"Value\": \"val1\" }, { \"Key\": \"key1\", \"Value\": \"val2\" }]"; var data = this.customKeyValueConverter.ConvertFrom(null, CultureInfo.InvariantCulture, json) as KeyValuePair[]; - Assert.AreEqual(1, data.Length); - Assert.AreEqual(string.Empty, data[0].Key); - Assert.AreEqual(string.Empty, data[0].Value); + Assert.IsNotNull(data); + Assert.AreEqual(2, data.Length); + Assert.AreEqual("key1", data[0].Key); + Assert.AreEqual("val1", data[0].Value); + Assert.AreEqual("key1", data[1].Key); + Assert.AreEqual("val2", data[1].Value); } } } From 27d118eae3c19031b8ef28fbd6cc9848dd421e7c Mon Sep 17 00:00:00 2001 From: Navin Kumar B Date: Tue, 10 Jan 2017 10:33:02 +0530 Subject: [PATCH 2/3] Resolved PR comments. --- .../TestProperty/CustomKeyValueConverter.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs b/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs index 36777744c8..6724b17836 100644 --- a/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs +++ b/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs @@ -43,15 +43,11 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c { using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(data))) { - List> listOfKvps = new List>(); - // Converting Json data to array of KeyValuePairs with duplicate keys. var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List)); + var listOfTraitObjects = serializer.ReadObject(stream) as List; - var listOfTratiObjects = serializer.ReadObject(stream) as List; - listOfTratiObjects.ForEach(o=>listOfKvps.Add(new KeyValuePair(o.Key, o.Value))); - - return listOfKvps?.ToArray(); + return listOfTraitObjects.Select(i => new KeyValuePair(i.Key, i.Value)).ToArray();; } } From a83b7820a96805f676d99d35e5907bfac38d9e71 Mon Sep 17 00:00:00 2001 From: Navin Kumar B Date: Tue, 10 Jan 2017 11:29:23 +0530 Subject: [PATCH 3/3] Using array instead of list. --- .../TestProperty/CustomKeyValueConverter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs b/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs index 6724b17836..7095630f1b 100644 --- a/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs +++ b/src/Microsoft.TestPlatform.ObjectModel/TestProperty/CustomKeyValueConverter.cs @@ -44,8 +44,8 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(data))) { // Converting Json data to array of KeyValuePairs with duplicate keys. - var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List)); - var listOfTraitObjects = serializer.ReadObject(stream) as List; + var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(TraitObject[])); + var listOfTraitObjects = serializer.ReadObject(stream) as TraitObject[]; return listOfTraitObjects.Select(i => new KeyValuePair(i.Key, i.Value)).ToArray();; }