-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added support to serialize and deserialize custom erros
- Loading branch information
Showing
6 changed files
with
231 additions
and
5 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
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
57 changes: 57 additions & 0 deletions
57
tests/PowerUtils.Results.Tests/CommonUtilsTests/GetErrorTypeTests.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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using FluentAssertions; | ||
using PowerUtils.Results.Tests.Fakes; | ||
using Xunit; | ||
|
||
namespace PowerUtils.Results.Tests.CommonUtilsTests | ||
{ | ||
public class GetErrorTypeTests | ||
{ | ||
[Fact] | ||
public void BuiltInError_GetErrorType_Type() | ||
{ | ||
// Arrange | ||
var errorType = typeof(NotFoundError); | ||
var fullName = errorType.FullName; | ||
|
||
|
||
// Act | ||
var act = CommonUtils.GetErrorType(fullName); | ||
|
||
|
||
// Assert | ||
act.Should().Be(errorType); | ||
} | ||
|
||
[Fact] | ||
public void CustomError_GetErrorType_Type() | ||
{ | ||
// Arrange | ||
var errorType = typeof(CustomError); | ||
var fullName = errorType.FullName; | ||
|
||
|
||
// Act | ||
var act = CommonUtils.GetErrorType(fullName); | ||
|
||
|
||
// Assert | ||
act.Should().Be(errorType); | ||
} | ||
|
||
[Fact] | ||
public void ExistentTypeNotIError_GetErrorType_TypeLoadException() | ||
{ | ||
// Arrange | ||
var fullName = typeof(FakeCustomer).FullName; | ||
|
||
|
||
// Act | ||
var act = Record.Exception(() => CommonUtils.GetErrorType(fullName)); | ||
|
||
|
||
// Assert | ||
act.Should().BeOfType<TypeLoadException>(); | ||
} | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
tests/PowerUtils.Results.Tests/Errors/CustomErrors/JsonSerializerDeserializeTests.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 |
---|---|---|
@@ -0,0 +1,147 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using FluentAssertions; | ||
using FluentAssertions.Execution; | ||
using PowerUtils.Results.Tests.Fakes; | ||
using Xunit; | ||
|
||
namespace PowerUtils.Results.Tests.Errors.CustomErrors | ||
{ | ||
public class JsonSerializerDeserializeTests | ||
{ | ||
[Fact] | ||
public void EmptyArrayString_DeserializeToIErrorList_EmptyCustomErrorList() | ||
{ | ||
// Arrange | ||
var serialization = "[]"; | ||
|
||
|
||
// Act | ||
var act = JsonSerializer.Deserialize<List<CustomError>>(serialization); | ||
|
||
|
||
// Assert | ||
using(new AssertionScope()) | ||
{ | ||
act.Should().BeEmpty(); | ||
act.Should().BeOfType<List<CustomError>>(); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void EmptyArrayString_DeserializeToIErrorArray_EmptyCustomErrorArray() | ||
{ | ||
// Arrange | ||
var serialization = "[]"; | ||
|
||
|
||
// Act | ||
var act = JsonSerializer.Deserialize<CustomError[]>(serialization); | ||
|
||
|
||
// Assert | ||
using(new AssertionScope()) | ||
{ | ||
act.Should().BeEmpty(); | ||
act.Should().BeOfType<CustomError[]>(); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void String_Deserialize_CustomError() | ||
{ | ||
// Arrange | ||
var json = @"{ | ||
""_type"": ""PowerUtils.Results.Tests.Fakes.CustomError"", | ||
""Property"": ""prop"", | ||
""Code"": ""CODE"", | ||
""Description"": ""Disc"" | ||
}"; | ||
|
||
|
||
// Act | ||
var act = JsonSerializer.Deserialize<CustomError>(json); | ||
|
||
|
||
// Assert | ||
act.Should().IsError<CustomError>( | ||
"prop", | ||
"CODE", | ||
"Disc"); | ||
} | ||
|
||
[Fact] | ||
public void CustomError_Serialize_SerializationWithTypePropertyCodeAndDescription() | ||
{ | ||
// Arrange | ||
var property = "prop"; | ||
var code = "cod"; | ||
var description = "disc"; | ||
|
||
var error = new CustomError(property, code, description); | ||
|
||
var type = error.GetType().FullName; | ||
var expected = $"{{\"_type\":\"{type}\",\"Property\":\"{property}\",\"Code\":\"{code}\",\"Description\":\"{description}\"}}"; | ||
|
||
|
||
// Act | ||
var act = JsonSerializer.Serialize(error); | ||
|
||
|
||
// Assert | ||
act.Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void CustomError_SerializeDeserialize_SameOriginalError() | ||
{ | ||
// Arrange | ||
var property = "prop"; | ||
var code = "cod"; | ||
var description = "disc"; | ||
|
||
var error = new CustomError(property, code, description); | ||
|
||
|
||
// Act | ||
var serialization = JsonSerializer.Serialize(error); | ||
var act = JsonSerializer.Deserialize<CustomError>(serialization); | ||
|
||
|
||
// Assert | ||
act.Should().IsError<CustomError>( | ||
property, | ||
code, | ||
description); | ||
} | ||
|
||
#if NET6_0_OR_GREATER | ||
[Fact] | ||
public void CustomError_DeserializeUsingIError_SameOriginalError() | ||
{ | ||
// The System.Text.Json serializer does not support deserialization of interface types | ||
// Exception returned -> 'System.NotSupportedException : Deserialization of interface types is not supported...' | ||
|
||
|
||
// Arrange | ||
var property = "prop"; | ||
var code = "cod"; | ||
var description = "disc"; | ||
|
||
var serialization = JsonSerializer.Serialize( | ||
new CustomError(property, code, description)); | ||
|
||
|
||
// Act | ||
var act = JsonSerializer.Deserialize<IError>(serialization); | ||
|
||
|
||
// Assert | ||
act.Should().IsError<CustomError>( | ||
property, | ||
code, | ||
description); | ||
} | ||
#endif | ||
} | ||
} |
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