-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathJsonMessageSerializerTests.cs
More file actions
90 lines (73 loc) · 3.11 KB
/
JsonMessageSerializerTests.cs
File metadata and controls
90 lines (73 loc) · 3.11 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
namespace SlimMessageBus.Host.Serialization.SystemTextJson.Test;
public class JsonMessageSerializerTests
{
public static TheoryData<object, object> Data => new()
{
{ null, null},
{ 10, 10},
{ false, false},
{ true, true},
{ "string", "string"},
{ DateTime.Now.Date, DateTime.Now.Date},
{ Guid.Empty, "00000000-0000-0000-0000-000000000000"},
};
[Theory]
[MemberData(nameof(Data))]
public void When_SerializeAndDeserialize_Given_TypeObjectAndBytesPayload_Then_TriesToInferPrimitiveTypes(object value, object expectedValue)
{
// arrange
var subject = new JsonMessageSerializer();
// act
var bytes = subject.Serialize(typeof(object), null, value, null);
var deserializedValue = subject.Deserialize(typeof(object), null, bytes, null);
// assert
deserializedValue.Should().Be(expectedValue);
}
[Theory]
[MemberData(nameof(Data))]
public void When_SerializeAndDeserialize_Given_TypeObjectAndStringPayload_Then_TriesToInferPrimitiveTypes(object value, object expectedValue)
{
// arrange
var subject = new JsonMessageSerializer() as IMessageSerializer<string>;
// act
var json = subject.Serialize(typeof(object), null, value, null);
var deserializedValue = subject.Deserialize(typeof(object), null, json, null);
// assert
deserializedValue.Should().Be(expectedValue);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void When_RegisterSerializer_Then_UsesOptionsFromContainerIfAvailable(bool jsonOptionComeFromContainer)
{
// arrange
var mbb = MessageBusBuilder.Create();
mbb.AddJsonSerializer();
var services = new ServiceCollection();
var jsonOptions = new JsonSerializerOptions();
// Simulate the options have been used in an serializer already (see https://github.com/zarusz/SlimMessageBus/issues/252)
// Modifying options (adding converters) when the options are already in use by a serializer will throw an exception: System.InvalidOperationException : This JsonSerializerOptions instance is read-only or has already been used in serialization or deserialization.
JsonSerializer.SerializeToUtf8Bytes(new Dictionary<string, object>(), jsonOptions);
if (jsonOptionComeFromContainer)
{
services.AddSingleton(jsonOptions);
}
foreach (var action in mbb.PostConfigurationActions)
{
action(services);
}
var serviceProvider = services.BuildServiceProvider();
var subject = serviceProvider.GetRequiredService<JsonMessageSerializer>();
// act
if (jsonOptionComeFromContainer)
{
subject.Options.Should().BeSameAs(jsonOptions);
}
else
{
subject.Options.Should().NotBeSameAs(jsonOptions);
}
// assert
serviceProvider.GetService<IMessageSerializerProvider>().Should().BeSameAs(subject);
}
}