-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathOneOfJsonConverterTests.cs
More file actions
254 lines (222 loc) · 8.77 KB
/
OneOfJsonConverterTests.cs
File metadata and controls
254 lines (222 loc) · 8.77 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
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
using System.Text.Json;
using FluentAssertions;
using OneOf;
using TrueLayer.Payments.Model;
using TrueLayer.Serialization;
using Xunit;
namespace TrueLayer.Tests.Serialization
{
public class OneOfJsonConverterTests
{
private readonly JsonSerializerOptions _options = new JsonSerializerOptions
{
Converters = { new OneOfJsonConverterFactory() }
};
[Fact]
public void Throws_if_not_a_valid_json_object()
{
string json = "[]";
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<OneOf<Foo, Bar>>(json, _options));
}
[Fact]
public void Throws_if_discriminator_and_status_field_missing()
{
string json = @"{
""field1"": ""Bar"",
""field2"": 10
}
";
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<OneOf<Foo, Bar>>(json, _options));
}
[Fact]
public void Can_read_from_type_discriminator()
{
string json = @"{
""type"": ""Bar"",
""BarProp"": 10
}
";
var oneOf = JsonSerializer.Deserialize<OneOf<Foo, Bar>>(json, _options);
oneOf.AsT1.BarProp.Should().Be(10);
oneOf.Value.Should().BeOfType<Bar>();
}
[Fact]
public void Can_read_from_status_discriminator()
{
string json = @"{
""status"": ""Bar"",
""BarProp"": 10
}
";
var oneOf = JsonSerializer.Deserialize<OneOf<Foo, Bar>>(json, _options);
oneOf.AsT1.BarProp.Should().Be(10);
oneOf.Value.Should().BeOfType<Bar>();
}
[Fact]
public void Can_fallback_to_status_discriminator_when_type_discriminator_does_not_match()
{
string json = @"{
""type"": ""NotMatched"",
""status"": ""Bar"",
""BarProp"": 10
}
";
var oneOf = JsonSerializer.Deserialize<OneOf<Foo, Bar>>(json, _options);
oneOf.AsT1.BarProp.Should().Be(10);
oneOf.Value.Should().BeOfType<Bar>();
}
[Fact]
public void Can_read_from_status_discriminator_Refund_Failed()
{
string json = @"{
""status"": ""failed"",
""AmountInMinor"": 1000,
""CreatedAt"": ""2021-01-01T00:00:00Z"",
""FailedAt"": ""2021-01-01T00:02:00Z"",
""FailureReason"": ""Something bad happened""
}
";
var oneOf = JsonSerializer.Deserialize<OneOf<RefundPending, RefundAuthorized, RefundExecuted, RefundFailed>>(json, _options);
oneOf.Value.Should().BeOfType<RefundFailed>();
oneOf.AsT3.AmountInMinor.Should().Be(1000);
oneOf.AsT3.Status.Should().Be("failed");
oneOf.AsT3.CreatedAt.Should().Be(new System.DateTime(2021, 1, 1, 0, 0, 0, System.DateTimeKind.Utc));
oneOf.AsT3.FailedAt.Should().Be(new System.DateTime(2021, 1, 1, 0, 2, 0, System.DateTimeKind.Utc));
oneOf.AsT3.FailureReason.Should().Be("Something bad happened");
}
[Fact]
public void Can_read_from_status_discriminator_Refund_Executed()
{
string json = @"{
""status"": ""executed"",
""AmountInMinor"": 2000,
""CreatedAt"": ""2021-01-01T00:00:00Z"",
""ExecutedAt"": ""2021-01-01T00:05:00Z""
}
";
var oneOf = JsonSerializer.Deserialize<OneOf<RefundPending, RefundAuthorized, RefundExecuted, RefundFailed>>(json, _options);
oneOf.Value.Should().BeOfType<RefundExecuted>();
oneOf.AsT2.AmountInMinor.Should().Be(2000);
oneOf.AsT2.Status.Should().Be("executed");
oneOf.AsT2.CreatedAt.Should().Be(new System.DateTime(2021, 1, 1, 0, 0, 0, System.DateTimeKind.Utc));
oneOf.AsT2.ExecutedAt.Should().Be(new System.DateTime(2021, 1, 1, 0, 5, 0, System.DateTimeKind.Utc));
}
[Fact]
public void Can_deserialize_ListPaymentRefundsResponse_with_all_refund_statuses()
{
string json = @"{
""Items"": [
{
""status"": ""pending"",
""Id"": ""refund-1"",
""Reference"": ""ref-1"",
""AmountInMinor"": 1000,
""Currency"": ""GBP"",
""Metadata"": {},
""CreatedAt"": ""2021-01-01T00:00:00Z""
},
{
""status"": ""authorized"",
""Id"": ""refund-2"",
""Reference"": ""ref-2"",
""AmountInMinor"": 1500,
""Currency"": ""GBP"",
""Metadata"": {},
""CreatedAt"": ""2021-01-01T00:01:00Z""
},
{
""status"": ""executed"",
""Id"": ""refund-3"",
""Reference"": ""ref-3"",
""AmountInMinor"": 2000,
""Currency"": ""GBP"",
""Metadata"": {},
""CreatedAt"": ""2021-01-01T00:02:00Z"",
""ExecutedAt"": ""2021-01-01T00:05:00Z""
},
{
""status"": ""failed"",
""Id"": ""refund-4"",
""Reference"": ""TUOYAP"",
""AmountInMinor"": 500,
""Currency"": ""GBP"",
""Metadata"": {},
""CreatedAt"": ""2021-01-01T00:03:00Z"",
""FailedAt"": ""2021-01-01T00:04:00Z"",
""FailureReason"": ""Insufficient funds""
}
]
}";
var response = JsonSerializer.Deserialize<ListPaymentRefundsResponse>(json, _options);
response.Should().NotBeNull();
response!.Items.Should().HaveCount(4);
response.Items[0].Value.Should().BeOfType<RefundPending>();
response.Items[0].AsT0.Reference.Should().Be("ref-1");
response.Items[1].Value.Should().BeOfType<RefundAuthorized>();
response.Items[1].AsT1.Reference.Should().Be("ref-2");
response.Items[2].Value.Should().BeOfType<RefundExecuted>();
response.Items[2].AsT2.Reference.Should().Be("ref-3");
response.Items[3].Value.Should().BeOfType<RefundFailed>();
response.Items[3].AsT3.Reference.Should().Be("TUOYAP");
response.Items[3].AsT3.FailureReason.Should().Be("Insufficient funds");
}
[Fact]
public void Can_read_nested()
{
string json = @"{
""Name"": ""Nested"",
""OneOf"": {
""type"": ""Foo"",
""FooProp"": ""test""
}
}
";
var wrapper = JsonSerializer.Deserialize<Wrapper>(json, _options);
wrapper.Should().NotBeNull();
wrapper!.Name.Should().Be("Nested");
wrapper.OneOf.AsT0.FooProp.Should().Be("test");
}
[Fact]
public void Can_override_discriminator()
{
string json = @"{
""type"": ""_other_""
}
";
var oneOf = JsonSerializer.Deserialize<OneOf<Foo, Other>>(json, _options);
oneOf.Value.Should().BeOfType<Other>();
}
[Fact]
public void Returns_default_value_for_empty_objects()
{
string json = @"{
}
";
var oneOf = JsonSerializer.Deserialize<OneOf<Foo, Other>>(json, _options);
oneOf.Value.Should().Be(default);
}
[Fact]
public void Can_write_one_of_type()
{
OneOf<Foo, Bar> obj = new Foo();
JsonSerializer.Serialize(obj, _options).Should().NotBeNullOrWhiteSpace();
}
public class Foo
{
public string? FooProp { get; set; }
}
public class Bar
{
public int BarProp { get; set; }
}
[JsonDiscriminator("_other_")]
public class Other
{
}
public class Wrapper
{
public string? Name { get; set; }
public OneOf<Foo, Bar> OneOf { get; set; }
}
}
}