forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnDeserializationErrorCallbackTest.cs
More file actions
134 lines (114 loc) · 4.92 KB
/
OnDeserializationErrorCallbackTest.cs
File metadata and controls
134 lines (114 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Runtime.Serialization;
using NUnit.Framework;
namespace ServiceStack.Text.Tests.JsonTests
{
[TestFixture]
public class OnDeserializationErrorTest
{
[Test]
public void Invokes_callback_on_protected_setter()
{
string json = @"{""idBadProt"":""value"", ""idGood"":""2"" }";
AssertThatInvalidJsonInvokesExpectedCallback<TestDto>(json, "idBadProt", "value", typeof(int), "Input string was not in a correct format.");
}
[Test]
public void Invokes_callback_on_incorrect_type()
{
string json = @"{""idBad"":""abc"", ""idGood"":""2"" }";
AssertThatInvalidJsonInvokesExpectedCallback<TestDto>(json, "idBad", "abc", typeof(int), "Input string was not in a correct format.");
}
[Test]
public void Invokes_callback_on_incorrect_type_with_data_set()
{
string json = @"{""idBad"":""abc"", ""idGood"":""2"" }";
AssertThatInvalidJsonInvokesExpectedCallback<TestDto>(json, "idBad", "abc", typeof(int), "Input string was not in a correct format.");
}
[Test]
public void Invokes_callback_on_value_out_of_range()
{
string json = @"{""idBad"":""4700000007"", ""idGood"":""2"" }";
AssertThatInvalidJsonInvokesExpectedCallback<TestDto>(json, "idBad", "4700000007", typeof(int), "Value was either too large or too small for an Int32.");
}
[Test]
public void Does_not_invoke_callback_on_valid_data()
{
JsConfig.Reset();
JsConfig.OnDeserializationError = (o, s, s1, arg3, arg4) => Assert.Fail("For valida data this should not be invoked");
var json = @"{""idBad"":""2"", ""idGood"":""2"" }";
JsonSerializer.DeserializeFromString(json, typeof(TestDto));
}
[Test]
public void TestReset()
{
JsConfig.Reset();
Assert.IsNull(JsConfig.OnDeserializationError);
JsConfig.OnDeserializationError = (o, s, s1, arg3, arg4) => { };
Assert.IsNotNull(JsConfig.OnDeserializationError);
JsConfig.Reset();
Assert.IsNull(JsConfig.OnDeserializationError);
}
[Test]
public void Invokes_callback_deserialization_of_array_with_missing_comma()
{
string json = @"{""Values"": [ { ""Val"": ""a""} { ""Val"": ""b""}] }";
AssertThatInvalidJsonInvokesExpectedCallback<TestDtoWithArray>(json, "Values", @"[ { ""Val"": ""a""} { ""Val"": ""b""}]", typeof(TestChildDto[]), "Type definitions should start with a '{', expecting serialized type 'TestChildDto', got string starting with: Val");
}
[Test]
public void Does_not_invoke_callback_on_valid_data_with_array()
{
JsConfig.Reset();
JsConfig.OnDeserializationError = (o, s, s1, arg3, arg4) => Assert.Fail("For valida data this should not be invoked");
var json = @"{""Values"": [ { ""Val"": ""a""}, { ""Val"": ""b""}] }";
JsonSerializer.DeserializeFromString(json, typeof(TestDtoWithArray));
}
[DataContract]
class TestDtoWithArray
{
[DataMember]
public TestChildDto[] Values { get; set; }
}
[DataContract]
class TestChildDto
{
[DataMember]
public string Val { get; set; }
}
[DataContract]
class TestDto
{
[DataMember(Name = "idBadProt")]
public int protId { get; protected set; }
[DataMember(Name = "idGood")]
public int IdGood { get; set; }
[DataMember(Name = "idBad")]
public int IdBad { get; set; }
}
private static void AssertThatInvalidJsonInvokesExpectedCallback<T>(string json, string expectedProperty, string expectedValue, Type expectedType, string expectedExceptionMessage)
{
string property = null, value = null;
Type type = null;
Exception ex = null;
bool callbackInvoked = false;
object deserialized = null;
JsConfig.Reset();
JsConfig.OnDeserializationError = (o, propertyType, s, s1, arg4) =>
{
deserialized = o;
property = s;
value = s1;
type = propertyType;
ex = arg4;
callbackInvoked = true;
};
JsonSerializer.DeserializeFromString(json, typeof(T));
Assert.IsTrue(callbackInvoked, "Callback should be invoked");
Assert.AreEqual(expectedProperty, property);
Assert.AreEqual(expectedValue, value);
Assert.AreEqual(expectedType, type);
Assert.AreEqual(expectedExceptionMessage, ex.Message);
Assert.IsNotNull(deserialized);
Assert.IsInstanceOf<T>(deserialized);
}
}
}