|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using System.Runtime.Serialization; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Newtonsoft.Json; |
| 9 | +using Newtonsoft.Json.Serialization; |
| 10 | +using OneBitSoftware.Utilities.Errors; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace OneBitSoftware.Utilities.OperationResultTests.Serialization |
| 14 | +{ |
| 15 | + public class TypeNameSerializationBinder : ISerializationBinder |
| 16 | + { |
| 17 | + public string TypeFormat { get; private set; } |
| 18 | + |
| 19 | + public TypeNameSerializationBinder(string typeFormat) |
| 20 | + { |
| 21 | + TypeFormat = typeFormat; |
| 22 | + } |
| 23 | + |
| 24 | + public void BindToName(Type serializedType, out string assemblyName, out string typeName) |
| 25 | + { |
| 26 | + assemblyName = null; |
| 27 | + typeName = serializedType.Name; |
| 28 | + } |
| 29 | + |
| 30 | + public Type BindToType(string assemblyName, string typeName) |
| 31 | + { |
| 32 | + var resolvedTypeName = string.Format(TypeFormat, typeName); |
| 33 | + return Type.GetType(resolvedTypeName, true); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public class OperationResultSerializationNewtonsoftJsonTests |
| 38 | + { |
| 39 | + public OperationResultSerializationNewtonsoftJsonTests() |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + private JsonSerializerSettings GetJsonSerializerSettings() |
| 44 | + { |
| 45 | + return new JsonSerializerSettings |
| 46 | + { |
| 47 | + NullValueHandling = NullValueHandling.Ignore, |
| 48 | + TypeNameHandling = TypeNameHandling.Auto, |
| 49 | + SerializationBinder = new TypeNameSerializationBinder("OneBitSoftware.Utilities.Errors.OperationError, OneBitSoftware.Utilities.OperationResult") |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public async Task CanSerializeWithNewtonsoftJson() |
| 55 | + { |
| 56 | + // Arrange |
| 57 | + var testText = "Test details"; |
| 58 | + var operationResult = new OperationResult(); |
| 59 | + operationResult.AppendError(new OperationError(message: "Test") { Code = 123, Details = testText }); |
| 60 | + |
| 61 | + // Act |
| 62 | + string json = JsonConvert.SerializeObject(operationResult, GetJsonSerializerSettings()); |
| 63 | + |
| 64 | + // Assert |
| 65 | + Assert.Contains(testText, json); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public async Task CanSerializeAndDeserializeCustomErrorWithSystemTextJson() |
| 70 | + { |
| 71 | + // TODO: clean up variables and repeated code |
| 72 | + // Arrange |
| 73 | + var testText = "Test details"; |
| 74 | + var customErrorText = "Custom error test details"; |
| 75 | + var serializeStream = new MemoryStream(); |
| 76 | + var deserializeStream = new MemoryStream(); |
| 77 | + var operationResult = new OperationResult(); |
| 78 | + operationResult.AppendError(new OperationError(message: "Test") { Code = 123, Details = testText }); |
| 79 | + operationResult.AppendError(new CustomError() { Message = "Custom Test", Code = 666, Details = testText, CustomProperty = customErrorText }); |
| 80 | + |
| 81 | + var c2 = typeof(IOperationError).IsAssignableFrom(typeof(OperationError)); |
| 82 | + |
| 83 | + // Act |
| 84 | + string json = JsonConvert.SerializeObject(operationResult, GetJsonSerializerSettings()); |
| 85 | + |
| 86 | + var resultObject = JsonConvert.DeserializeObject<OperationResult>(json, GetJsonSerializerSettings()); |
| 87 | + |
| 88 | + //// Assert |
| 89 | + Assert.NotNull(resultObject); |
| 90 | + Assert.Equal(operationResult.Fail, resultObject?.Fail); |
| 91 | + Assert.Equal(operationResult.InitialException, resultObject?.InitialException); |
| 92 | + Assert.Equal(operationResult.Success, resultObject?.Success); |
| 93 | + Assert.Equal(operationResult.SuccessMessages, resultObject?.SuccessMessages); |
| 94 | + Assert.Contains(operationResult.Errors, r => r.GetType() == typeof(CustomError)); |
| 95 | + Assert.Contains(operationResult.Errors, r => r as CustomError != null && (r as CustomError)!.CustomProperty.Equals(customErrorText)); |
| 96 | + Assert.Contains(operationResult.Errors, r => r as CustomError != null && (r as CustomError)!.Code.Equals(666)); |
| 97 | + Assert.Contains(testText, json); |
| 98 | + Assert.Contains(customErrorText, json); |
| 99 | + Assert.Contains("666", json); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments