-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re #18251 Issue_with_logging_of_IDs_as_integers_in_loggly (#9)
* re #18251_Issue_with_logging_of_IDs_as_integers_in_loggly - Have added an overridable SerializerSettings property to the LogglyOptions class with a default value - Have added a JsonConverter that would convert integer Id properties to string and included it in the default SerializerSettings - Changed the serializer settings to include Default Value properties - Have written tests for the FormattedIdJsonConverter * re #18251_Issue_with_logging_of_IDs_as_integers_in_loggly - Fixed the CookieContainer Autofixture issue * re #18251_Issue_with_logging_of_IDs_as_integers_in_loggly - Fixed the CookieContainer Autofixture failing the integration tests * re #18251_Issue_with_logging_of_IDs_as_integers_in_loggly - Deleted a commentary
- Loading branch information
1 parent
00d2285
commit 0386160
Showing
7 changed files
with
267 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
public class FormattedIdConverter : JsonConverter | ||
{ | ||
private readonly HashSet<Type> IdNumericTypes = new HashSet<Type> | ||
{ | ||
typeof(byte), typeof(short), typeof(int), typeof(long), | ||
typeof(sbyte), typeof(ushort), typeof(uint), typeof(ulong), | ||
}; | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return IdNumericTypes.Contains(objectType); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
if (writer.Path.EndsWith("Id", StringComparison.OrdinalIgnoreCase)) | ||
writer.WriteValue(Convert.ToString(value)); | ||
else | ||
writer.WriteValue(value); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
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,19 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace EMG.Extensions.Logging.Loggly | ||
{ | ||
public static class JsonSettings | ||
{ | ||
public static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings | ||
{ | ||
TypeNameHandling = TypeNameHandling.None, | ||
NullValueHandling = NullValueHandling.Ignore, | ||
Formatting = Formatting.None, | ||
DefaultValueHandling = DefaultValueHandling.Include, | ||
DateTimeZoneHandling = DateTimeZoneHandling.Utc, | ||
DateFormatHandling = DateFormatHandling.IsoDateFormat, | ||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore, | ||
Converters = { new FormattedIdConverter() } | ||
}; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using Newtonsoft.Json; | ||
using Moq; | ||
|
||
namespace Tests | ||
{ | ||
[TestFixture] | ||
public class FormattedIdConverterTest | ||
{ | ||
|
||
[Test, AutoMoqData] | ||
public void CanConvert_returns_true_for_integer_types(FormattedIdConverter sut) | ||
{ | ||
// Assert | ||
foreach (var intType in integerTypesList) | ||
{ | ||
Assert.That(sut.CanConvert(intType), Is.EqualTo(true)); | ||
} | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void CanConvert_returns_false_for_non_integer_primitive_types(FormattedIdConverter sut) | ||
{ | ||
// Arrange | ||
var frameworkTypes = typeof(Type).Assembly.GetTypes() | ||
.Where(x => x.IsPrimitive).ToList(); | ||
|
||
var nonIntegerTypesList = frameworkTypes.Except(integerTypesList); | ||
|
||
// Assert | ||
foreach (var intType in nonIntegerTypesList) | ||
{ | ||
Assert.That(sut.CanConvert(intType), Is.EqualTo(false)); | ||
} | ||
} | ||
|
||
readonly List<Type> integerTypesList = new List<Type> | ||
{ | ||
typeof(byte), typeof(short), typeof(int), typeof(long), | ||
typeof(sbyte), typeof(ushort), typeof(uint), typeof(ulong), | ||
}; | ||
|
||
|
||
[Test, AutoMoqData] | ||
public void WriteJson_calls_WriteValue_method_of_JsonWriter_for_single_value_once_only(FormattedIdConverter sut | ||
, JsonWriter writer | ||
, JsonSerializer serializer | ||
, byte value) | ||
{ | ||
|
||
// Act | ||
sut.WriteJson(writer, value, serializer); | ||
|
||
// Assert | ||
Mock.Get(writer).Verify(i => i.WriteValue(It.IsAny<object>()), Times.Once); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void WriteJson_calls_WriteValue_method_of_JsonWriter_for_single_value_with_the_right_value(FormattedIdConverter sut | ||
, JsonWriter writer | ||
, JsonSerializer serializer | ||
, byte value) | ||
{ | ||
|
||
// Act | ||
sut.WriteJson(writer, value, serializer); | ||
|
||
// Assert | ||
Mock.Get(writer).Verify(i => i.WriteValue(value), Times.Once); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void WriteJson_calls_WriteValue_method_of_JsonWriter_for_non_Id_property_once_only(FormattedIdConverter sut | ||
, JsonWriter writer | ||
, JsonSerializer serializer | ||
, string propertyName | ||
, byte value) | ||
{ | ||
Assume.That(propertyName, Does.Not.EndWith("Id")); | ||
|
||
writer.WriteStartObject(); | ||
writer.WritePropertyName(propertyName); | ||
|
||
// Act | ||
sut.WriteJson(writer, value, serializer); | ||
|
||
// Assert | ||
Mock.Get(writer).Verify(i => i.WriteValue(It.IsAny<object>()), Times.Once); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void WriteJson_calls_WriteValue_method_of_JsonWriter_for_non_Id_property_with_the_right_value(FormattedIdConverter sut | ||
, JsonWriter writer | ||
, JsonSerializer serializer | ||
, string propertyName | ||
, byte value) | ||
{ | ||
Assume.That(propertyName, Does.Not.EndWith("Id")); | ||
|
||
writer.WriteStartObject(); | ||
writer.WritePropertyName(propertyName); | ||
|
||
// Act | ||
sut.WriteJson(writer, value, serializer); | ||
|
||
// Assert | ||
Mock.Get(writer).Verify(i => i.WriteValue(value), Times.Once); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void WriteJson_calls_WriteValue_of_JsonWriter_for_Id_Property_once(FormattedIdConverter sut | ||
, JsonWriter writer | ||
, JsonSerializer serializer | ||
, string propertyName | ||
, byte value) | ||
{ | ||
// Arrange | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName($"{propertyName}Id"); | ||
|
||
// Act | ||
sut.WriteJson(writer, value, serializer); | ||
|
||
// Assert | ||
Mock.Get(writer).Verify(i => i.WriteValue(Convert.ToString(value)), Times.Once); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void WriteJson_calls_WriteValue_of_JsonWriter_for_Id_Property_with_string_value(FormattedIdConverter sut | ||
, JsonWriter writer | ||
, JsonSerializer serializer | ||
, string propertyName | ||
, byte value) | ||
{ | ||
// Arrange | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName($"{propertyName}Id"); | ||
|
||
// Act | ||
sut.WriteJson(writer, value, serializer); | ||
|
||
// Assert | ||
Mock.Get(writer).Verify(i => i.WriteValue(Convert.ToString(value)), Times.Once); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void WriteJson_checks_PropertyName_CaseInsensitive_Lower(FormattedIdConverter sut | ||
, JsonWriter writer | ||
, JsonSerializer serializer | ||
, string propertyName | ||
, byte value) | ||
{ | ||
// Arrange | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName($"{propertyName}Id".ToLower()); | ||
|
||
// Act | ||
sut.WriteJson(writer, value, serializer); | ||
|
||
// Assert | ||
Mock.Get(writer).Verify(i => i.WriteValue(Convert.ToString(value)), Times.Once); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void WriteJson_checks_PropertyName_CaseInsensitive_Upper(FormattedIdConverter sut | ||
, JsonWriter writer | ||
, JsonSerializer serializer | ||
, string propertyName | ||
, byte value) | ||
{ | ||
// Arrange | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName($"{propertyName}Id".ToUpper()); | ||
|
||
// Act | ||
sut.WriteJson(writer, value, serializer); | ||
|
||
// Assert | ||
Mock.Get(writer).Verify(i => i.WriteValue(Convert.ToString(value)), Times.Once); | ||
} | ||
|
||
[Test, AutoMoqData] | ||
public void ReadJson_throws_NotImplementedException(FormattedIdConverter sut | ||
, JsonReader reader | ||
, Type objectType | ||
, object existingValue | ||
, JsonSerializer serializer) | ||
{ | ||
// Assert | ||
Assert.Throws<NotImplementedException>(() => sut.ReadJson(reader, objectType, existingValue, serializer)); | ||
} | ||
} | ||
} |
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