-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathJsonNetApiClientSerializationTest.cs
318 lines (263 loc) · 12.8 KB
/
JsonNetApiClientSerializationTest.cs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using ArangoDBNetStandard.CursorApi.Models;
using ArangoDBNetStandard.Serialization;
using ArangoDBNetStandard.TransactionApi.Models;
using ArangoDBNetStandardTest.Serialization.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Xunit;
namespace ArangoDBNetStandardTest.Serialization
{
public class JsonNetApiClientSerializationTest
{
[Fact]
public void Serialize_ShouldSucceed()
{
var model = new TestModel()
{
NullProperty = null,
PropertyToCheckIfCamelCase = "myvalue",
EnumToConvert = TestModel.Number.Two
};
var serialization = new JsonNetApiClientSerialization();
// Perform serialize with camel case option
byte[] jsonBytesWithCamelCase = serialization.Serialize(model,
new ApiClientSerializationOptions(true, true, true));
string jsonStringWithCamelCase = Encoding.UTF8.GetString(jsonBytesWithCamelCase);
// Perform serialize without camel case option
byte[] jsonBytesWithoutCamelCase = serialization.Serialize(model,
new ApiClientSerializationOptions(false, true, true));
string jsonStringWithoutCamelCase = Encoding.UTF8.GetString(jsonBytesWithoutCamelCase);
// standard property with and without camelCase
Assert.Contains("propertyToCheckIfCamelCase", jsonStringWithCamelCase);
Assert.Contains("PropertyToCheckIfCamelCase", jsonStringWithoutCamelCase);
// Null property should be ignored in both cases
// (ignore case is important to make sure we don't miss the string
// if it is using different casing than we checked for)
Assert.DoesNotContain(
"nullProperty",
jsonStringWithCamelCase,
System.StringComparison.OrdinalIgnoreCase);
Assert.DoesNotContain("nullProperty",
jsonStringWithoutCamelCase,
System.StringComparison.OrdinalIgnoreCase);
// We expect enum conversion to string, as well as camelCase
Assert.Contains("enumToConvert", jsonStringWithCamelCase);
// We expect enum conversion to string, but not camelCase
Assert.Contains("EnumToConvert", jsonStringWithoutCamelCase);
}
[Fact]
public void Serialize_ShouldSucceed_WhenUsingDefaultOptions()
{
var model = new TestModel()
{
NullProperty = null,
PropertyToCheckIfCamelCase = "myvalue",
EnumToConvert = TestModel.Number.Two,
PropertyWithDifferentJsonName = "aaaaaaa",
MyStringDict = new Dictionary<string, object>()
{
["DictKeyNotCamelCasedAndNotIgnored"] = null,
["moreParams"] = new TestModel()
{
PropertyToCheckIfCamelCase = "fneozirnzgiodedzf",
MyStringDict = new Dictionary<string, object>()
{
["string1"] = "value1"
},
PropertyWithDifferentJsonName = "wow",
AnotherNullProperty = null,
EnumToConvert = TestModel.Number.One,
PropertyWithClassType = new InnerTestModel()
{
InnerTestModel_NullProperty = null,
InnerTestModel_PropertyToCheckIfCamelCase = "fjnzfiuzefnzeb"
}
}
},
PropertyWithClassType = new InnerTestModel()
{
InnerTestModel_NullProperty = null,
InnerTestModel_PropertyToCheckIfCamelCase = "djenfiuzrfiubzeiu"
}
};
var serialization = new JsonNetApiClientSerialization();
// Perform serialize with default options
// i.e. camelCase: false, ignoreNull: true, stringEnum: false
byte[] jsonBytes = serialization.Serialize(model, null);
string jsonString = Encoding.UTF8.GetString(jsonBytes);
Assert.Equal(
"{\"PropertyToCheckIfCamelCase\":\"myvalue\",\"EnumToConvert\":2,\"nameFromJsonProperty\":\"aaaaaaa\",\"MyStringDict\":{\"DictKeyNotCamelCasedAndNotIgnored\":null,\"moreParams\":{\"PropertyToCheckIfCamelCase\":\"fneozirnzgiodedzf\",\"EnumToConvert\":1,\"nameFromJsonProperty\":\"wow\",\"MyStringDict\":{\"string1\":\"value1\"},\"PropertyWithClassType\":{\"InnerTestModel_PropertyToCheckIfCamelCase\":\"fjnzfiuzefnzeb\"}}},\"PropertyWithClassType\":{\"InnerTestModel_PropertyToCheckIfCamelCase\":\"djenfiuzrfiubzeiu\"}}",
jsonString);
// Explicited asserts
Assert.Contains("PropertyToCheckIfCamelCase", jsonString);
Assert.DoesNotContain(
"NullProperty",
jsonString,
System.StringComparison.OrdinalIgnoreCase);
Assert.Contains("EnumToConvert", jsonString);
Assert.DoesNotContain("Two", jsonString);
Assert.Contains("nameFromJsonProperty", jsonString);
Assert.Contains("MyStringDict", jsonString);
Assert.Contains("DictKeyNotCamelCasedAndNotIgnored", jsonString);
Assert.Contains("moreParams", jsonString);
Assert.Contains("string1", jsonString);
Assert.DoesNotContain("PropertyWithDifferentJsonName", jsonString);
Assert.DoesNotContain(
"AnotherNullProperty",
jsonString,
System.StringComparison.OrdinalIgnoreCase);
Assert.Contains("PropertyWithClassType", jsonString);
Assert.Contains("InnerTestModel_PropertyToCheckIfCamelCase", jsonString);
}
[Fact]
public void DefaultOptions_ShouldAllowValuesToBeModifiedInEachInstances()
{
var serialization1 = new JsonNetApiClientSerialization();
var serialization2 = new JsonNetApiClientSerialization();
AssertDefaultOptions(serialization1.DefaultOptions);
serialization1.DefaultOptions.UseCamelCasePropertyNames = true;
serialization1.DefaultOptions.IgnoreNullValues = false;
serialization1.DefaultOptions.UseStringEnumConversion = true;
Assert.True(serialization1.DefaultOptions.UseCamelCasePropertyNames);
Assert.False(serialization1.DefaultOptions.IgnoreNullValues);
Assert.True(serialization1.DefaultOptions.UseStringEnumConversion);
// Ensure options for each instances are independent
AssertDefaultOptions(serialization2.DefaultOptions);
}
[Fact]
public void Serialize_ShouldNotCamelCaseBindVars_WhenSerializingPostCursorBody()
{
var body = new PostCursorBody
{
BindVars = new Dictionary<string, object>
{
["DontCamelCaseKey"] = new { DontCamelCaseMe = true }
}
};
var serialization = new JsonNetApiClientSerialization();
byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(
useCamelCasePropertyNames: true,
ignoreNullValues: true));
string jsonString = Encoding.UTF8.GetString(jsonBytes);
Assert.Contains("DontCamelCaseMe", jsonString);
Assert.Contains("DontCamelCaseKey", jsonString);
Assert.DoesNotContain("dontCamelCaseMe", jsonString);
Assert.DoesNotContain("dontCamelCaseKey", jsonString);
}
[Fact]
public void Serialize_ShouldCamelCaseBindVars_WhenSerializingPostCursorBodyWithDictionaryOption()
{
var body = new PostCursorBody
{
BindVars = new Dictionary<string, object>
{
["CamelCaseKey"] = new { CamelCaseMe = true }
}
};
var serialization = new JsonNetApiClientSerialization();
byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(
useCamelCasePropertyNames: true,
ignoreNullValues: true,
applySerializationOptionsToDictionaryValues: true));
string jsonString = Encoding.UTF8.GetString(jsonBytes);
Assert.Contains("CamelCaseKey", jsonString);
Assert.DoesNotContain("camelCaseKey", jsonString);
Assert.Contains("camelCaseMe", jsonString);
Assert.DoesNotContain("CamelCaseMe", jsonString);
}
[Fact]
[Obsolete]
public void Serialize_ShouldNotCamelCaseParams_WhenSerializingPostTransactionBody()
{
var body = new PostTransactionBody
{
Params = new Dictionary<string, object>
{
["DontCamelCaseKey"] = new { DontCamelCaseMe = true }
}
};
var serialization = new JsonNetApiClientSerialization();
byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(
useCamelCasePropertyNames: true,
ignoreNullValues: true));
string jsonString = Encoding.UTF8.GetString(jsonBytes);
Assert.Contains("DontCamelCaseMe", jsonString);
Assert.Contains("DontCamelCaseKey", jsonString);
Assert.DoesNotContain("dontCamelCaseMe", jsonString);
Assert.DoesNotContain("dontCamelCaseKey", jsonString);
}
[Fact]
[Obsolete]
public void Serialize_ShouldCamelCaseParams_WhenSerializingPostTransactionBodyWithDictionaryOption()
{
var body = new PostTransactionBody
{
Params = new Dictionary<string, object>
{
["CamelCaseKey"] = new { CamelCaseMe = true }
}
};
var serialization = new JsonNetApiClientSerialization();
byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(
useCamelCasePropertyNames: true,
ignoreNullValues: true,
applySerializationOptionsToDictionaryValues: true));
string jsonString = Encoding.UTF8.GetString(jsonBytes);
Assert.Contains("CamelCaseKey", jsonString);
Assert.DoesNotContain("camelCaseKey", jsonString);
Assert.Contains("camelCaseMe", jsonString);
Assert.DoesNotContain("CamelCaseMe", jsonString);
}
[Fact]
public void Serialize_ShouldNotIgnoreNull_WhenSerializingPostCursorBody()
{
var body = new PostCursorBody
{
BindVars = new Dictionary<string, object>
{
["DontCamelCaseKey"] = null
}
};
var serialization = new JsonNetApiClientSerialization();
byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(true, true));
string jsonString = Encoding.UTF8.GetString(jsonBytes);
Assert.Contains("DontCamelCaseKey", jsonString);
}
[Fact]
[Obsolete]
public void Serialize_ShouldNotIgnoreNull_WhenSerializingPostTransactionBody()
{
var body = new PostTransactionBody
{
Params = new Dictionary<string, object>
{
["DontCamelCaseKey"] = null
}
};
var serialization = new JsonNetApiClientSerialization();
byte[] jsonBytes = serialization.Serialize(body, new ApiClientSerializationOptions(true, true));
string jsonString = Encoding.UTF8.GetString(jsonBytes);
Assert.Contains("DontCamelCaseKey", jsonString);
}
[Fact]
public void DeserializeFromStream_ShouldSucceed()
{
// Deserializing should match both "camelCase" and "CamelCase"
byte[] jsonBytes = Encoding.UTF8.GetBytes(
"{\"propertyToCheckIfCamelCase\":\"myvalue\",\"NullProperty\":\"something\"}");
var stream = new MemoryStream(jsonBytes);
var serialization = new JsonNetApiClientSerialization();
TestModel model = serialization.DeserializeFromStream<TestModel>(stream);
Assert.Equal("myvalue", model.PropertyToCheckIfCamelCase);
Assert.Equal("something", model.NullProperty);
}
private void AssertDefaultOptions(ApiClientSerializationOptions options)
{
Assert.False(options.UseCamelCasePropertyNames);
Assert.True(options.IgnoreNullValues);
Assert.False(options.UseStringEnumConversion);
}
}
}