-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #433 from ably/feature/422-refactor-message-extras
Refactor MessageExtras
- Loading branch information
Showing
6 changed files
with
309 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 114 additions & 18 deletions
132
src/IO.Ably.Tests.Shared/CustomSerializers/MessageExtrasConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,144 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using IO.Ably.CustomSerialisers; | ||
using IO.Ably.Tests.Shared.Helpers; | ||
using IO.Ably.Types; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using Newtonsoft.Json.Serialization; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace IO.Ably.Tests.DotNetCore20.CustomSerializers | ||
{ | ||
public class MessageExtrasConverterTests | ||
{ | ||
public JsonSerializerSettings JsonSettings | ||
private readonly ITestOutputHelper _testOutputHelper; | ||
public JsonSerializerSettings JsonSettings = JsonHelper.Settings; | ||
|
||
public MessageExtrasConverterTests(ITestOutputHelper testOutputHelper) | ||
{ | ||
get | ||
{ | ||
JsonSerializerSettings res = new JsonSerializerSettings(); | ||
res.Converters = new List<JsonConverter>() | ||
{ | ||
new MessageExtrasConverter(), | ||
}; | ||
res.DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind; | ||
res.NullValueHandling = NullValueHandling.Ignore; | ||
res.ContractResolver = new CamelCasePropertyNamesContractResolver(); | ||
return res; | ||
} | ||
_testOutputHelper = testOutputHelper; | ||
} | ||
|
||
[Fact] | ||
[Trait("spec ", "tm2i")] | ||
public void Should_parse_MessageExtras_json_correctly() | ||
public void ShouldParse_MessageExtrasJson() | ||
{ | ||
var json = "{ \"random\":\"boo\", \"delta\":{ \"From\": \"1\", \"Format\":\"best\" } }"; | ||
var originalJObject = JObject.Parse(json); | ||
var json = @" | ||
{ | ||
'random':'boo', | ||
'delta': { | ||
'From': '1', | ||
'Format':'best' | ||
} | ||
}"; | ||
var originalJToken = JToken.Parse(json); | ||
var messageExtras = JsonConvert.DeserializeObject<MessageExtras>(json, JsonSettings); | ||
|
||
messageExtras.Delta.Should().NotBeNull(); | ||
messageExtras.Delta.From.Should().Be("1"); | ||
messageExtras.Delta.Format.Should().Be("best"); | ||
|
||
((string)messageExtras.ToJson()["random"]).Should().Be("boo"); | ||
|
||
var serialized = JsonConvert.SerializeObject(messageExtras, JsonSettings); | ||
var serializedJToken = JToken.Parse(serialized); | ||
|
||
JAssert.DeepEquals(serializedJToken, originalJToken, _testOutputHelper).Should().Be(true); | ||
} | ||
|
||
[Fact] | ||
[Trait("spec ", "tm2i")] | ||
public void ShouldParse_MessageExtrasJson_WithEmptyDelta() | ||
{ | ||
var json = @"{ | ||
'random':'boo', | ||
'foo':'fooValue', | ||
'bar':'barValue', | ||
'object' : { | ||
'key1': 'value1', | ||
'key2': 'value2' | ||
} | ||
}"; | ||
var originalJToken = JToken.Parse(json); | ||
var messageExtras = JsonConvert.DeserializeObject<MessageExtras>(json, JsonSettings); | ||
((string)messageExtras.ToJson()["random"]).Should().Be("boo"); | ||
((string)messageExtras.ToJson()["foo"]).Should().Be("fooValue"); | ||
((string)messageExtras.ToJson()["bar"]).Should().Be("barValue"); | ||
((string)messageExtras.ToJson()["object"]["key1"]).Should().Be("value1"); | ||
((string)messageExtras.ToJson()["object"]["key2"]).Should().Be("value2"); | ||
|
||
var serialized = JsonConvert.SerializeObject(messageExtras, JsonSettings); | ||
var serializedJToken = JToken.Parse(serialized); | ||
JAssert.DeepEquals(serializedJToken, originalJToken, _testOutputHelper).Should().Be(true); | ||
} | ||
|
||
[Fact] | ||
[Trait("spec ", "tm2i")] | ||
public void ShouldParse_MessageExtrasJson_WithDelta() | ||
{ | ||
var json = @"{ | ||
'delta': { | ||
'From': '1', | ||
'Format':'best' | ||
} | ||
}"; | ||
var originalJToken = JToken.Parse(json); | ||
var messageExtras = JsonConvert.DeserializeObject<MessageExtras>(json, JsonSettings); | ||
|
||
messageExtras.Delta.Should().NotBeNull(); | ||
messageExtras.Delta.From.Should().Be("1"); | ||
messageExtras.Delta.Format.Should().Be("best"); | ||
|
||
var serialized = JsonConvert.SerializeObject(messageExtras, JsonSettings); | ||
var serializedJObject = JObject.Parse(serialized); | ||
JToken.DeepEquals(serializedJObject, originalJObject).Should().BeTrue(); | ||
var serializedJToken = JToken.Parse(serialized); | ||
JAssert.DeepEquals(serializedJToken, originalJToken, _testOutputHelper).Should().Be(true); | ||
} | ||
|
||
[Fact] | ||
[Trait("spec ", "tm2i")] | ||
public void ShouldParse_Message_WithNullMessageExtras() | ||
{ | ||
var json = @"{ | ||
'id':'UniqueId', | ||
'clientId':'clientId', | ||
'connectionId':'connectionId', | ||
'name':'connectionName', | ||
'data':'data', | ||
'encoding':'encoding', | ||
'extras': null | ||
}"; | ||
|
||
var messageObject = JsonConvert.DeserializeObject<Message>(json, JsonSettings); | ||
messageObject.Extras.Should().BeNull(); | ||
var serialized = JsonConvert.SerializeObject(messageObject, JsonSettings); | ||
var serializedJToken = JToken.Parse(serialized); | ||
serializedJToken.Contains("extras").Should().Be(false); | ||
} | ||
|
||
[Fact] | ||
[Trait("spec ", "tm2i")] | ||
public void ShouldParse_Message_WithArbitraryMessageExtras() | ||
{ | ||
var json = @"{ | ||
'id':'UniqueId', | ||
'clientId':'clientId', | ||
'connectionId':'connectionId', | ||
'name':'connectionName', | ||
'data':'data', | ||
'extras': 'extraData', | ||
'encoding':'encoding' | ||
}"; | ||
|
||
var messageObject = JsonConvert.DeserializeObject<Message>(json, JsonSettings); | ||
messageObject.Extras.Delta.Should().BeNull(); | ||
messageObject.Extras.ToJson().ToString().Should().Be("extraData"); | ||
var serialized = JsonConvert.SerializeObject(messageObject, JsonSettings); | ||
var serializedJToken = JToken.Parse(serialized); | ||
serializedJToken.Contains("extras").Should().Be(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit.Abstractions; | ||
|
||
namespace IO.Ably.Tests.Shared.Helpers | ||
{ | ||
internal class JAssert | ||
{ | ||
// todo: upgrade testing library - https://github.com/fluentassertions/fluentassertions.json/issues/7 | ||
// https://stackoverflow.com/questions/52645603/how-to-compare-two-json-objects-using-c-sharp | ||
|
||
public static bool DeepEquals(JToken token1, JToken token2, ITestOutputHelper testOutputHelper) | ||
{ | ||
var areEqual = JToken.DeepEquals(token1, token2); | ||
if (!areEqual) | ||
{ | ||
var diff = JDiff.Differentiate(token1, token2); | ||
testOutputHelper.WriteLine($"Json Difference {diff}"); | ||
} | ||
|
||
return areEqual; | ||
} | ||
} | ||
} |
Oops, something went wrong.