-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommandSerializationTests.cs
More file actions
347 lines (304 loc) · 12.8 KB
/
CommandSerializationTests.cs
File metadata and controls
347 lines (304 loc) · 12.8 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Shouldly;
using ManagedCode.Communication.Commands;
using ManagedCode.Communication.Helpers;
using ManagedCode.Communication.Tests.Orleans.Fixtures;
using ManagedCode.Communication.Tests.Orleans.Grains;
using ManagedCode.Communication.Tests.Orleans.Models;
using Orleans;
using Xunit;
using ManagedCode.Communication.Tests.TestHelpers;
namespace ManagedCode.Communication.Tests.Orleans.Serialization;
/// <summary>
/// Tests for Command serialization through Orleans grain calls
/// </summary>
public class CommandSerializationTests : IClassFixture<OrleansClusterFixture>
{
private readonly IGrainFactory _grainFactory;
public CommandSerializationTests(OrleansClusterFixture fixture)
{
_grainFactory = fixture.Cluster.GrainFactory;
}
[Fact]
public async Task Command_WithAllFields_ShouldSerializeCorrectly()
{
// Arrange
var grain = _grainFactory.GetGrain<ITestSerializationGrain>(Guid.NewGuid());
var metadata = new CommandMetadata
{
InitiatedBy = "user123",
Source = "web-app",
Target = "payment-service",
IpAddress = "192.168.1.1",
UserAgent = "Mozilla/5.0",
SessionId = "session-456",
TraceId = "trace-789",
SpanId = "span-012",
Version = 1,
Priority = CommandPriority.High,
TimeToLiveSeconds = 300,
Tags = new Dictionary<string, string>
{
["environment"] = "production",
["region"] = "us-west"
},
Extensions = new Dictionary<string, object?>
{
["customField"] = "customValue",
["retryCount"] = 3
}
};
var command = Command.Create("ProcessPayment")
.WithCorrelationId("correlation-123")
.WithCausationId("causation-456")
.WithMetadata(m =>
{
m.InitiatedBy = metadata.InitiatedBy;
m.Source = metadata.Source;
m.Target = metadata.Target;
m.IpAddress = metadata.IpAddress;
m.UserAgent = metadata.UserAgent;
m.SessionId = metadata.SessionId;
m.TraceId = metadata.TraceId;
m.SpanId = metadata.SpanId;
m.Version = metadata.Version;
m.Priority = metadata.Priority;
m.TimeToLiveSeconds = metadata.TimeToLiveSeconds;
m.Tags = metadata.Tags;
m.Extensions = metadata.Extensions;
});
// Act
var result = await grain.EchoCommandAsync(command);
// Assert
result.ShouldNotBeNull();
result.CommandId.ShouldBe(command.CommandId);
result.CommandType.ShouldBe(command.CommandType);
result.Timestamp.ShouldBeCloseTo(command.Timestamp, TimeSpan.FromSeconds(1));
result.CorrelationId.ShouldBe(command.CorrelationId);
result.CausationId.ShouldBe(command.CausationId);
// Verify metadata
result.Metadata.ShouldNotBeNull();
result.Metadata!.InitiatedBy.ShouldBe(metadata.InitiatedBy);
result.Metadata.Source.ShouldBe(metadata.Source);
result.Metadata.Target.ShouldBe(metadata.Target);
result.Metadata.IpAddress.ShouldBe(metadata.IpAddress);
result.Metadata.UserAgent.ShouldBe(metadata.UserAgent);
result.Metadata.SessionId.ShouldBe(metadata.SessionId);
result.Metadata.TraceId.ShouldBe(metadata.TraceId);
result.Metadata.SpanId.ShouldBe(metadata.SpanId);
result.Metadata.Version.ShouldBe(metadata.Version);
result.Metadata.Priority.ShouldBe(metadata.Priority);
result.Metadata.TimeToLiveSeconds.ShouldBe(metadata.TimeToLiveSeconds);
result.Metadata.Tags.ShouldNotBeNull();
result.Metadata.Tags.ShouldHaveCount(2);
result.Metadata.Tags!["environment"].ShouldBe("production");
result.Metadata.Tags!["region"].ShouldBe("us-west");
result.Metadata.Extensions.ShouldNotBeNull();
result.Metadata.Extensions.ShouldHaveCount(2);
result.Metadata.Extensions!["customField"].ShouldBe("customValue");
result.Metadata.Extensions!["retryCount"].ShouldBe(3);
}
[Fact]
public async Task Command_WithMinimalFields_ShouldSerializeCorrectly()
{
// Arrange
var grain = _grainFactory.GetGrain<ITestSerializationGrain>(Guid.NewGuid());
var command = Command.Create("SimpleCommand");
// Act
var result = await grain.EchoCommandAsync(command);
// Assert
result.ShouldNotBeNull();
result.CommandId.ShouldBe(command.CommandId);
result.CommandType.ShouldBe("SimpleCommand");
result.Timestamp.ShouldBeCloseTo(command.Timestamp, TimeSpan.FromSeconds(1));
result.CorrelationId.ShouldBeNull();
result.CausationId.ShouldBeNull();
result.Metadata.ShouldBeNull();
}
[Fact]
public async Task CommandT_WithComplexPayload_ShouldSerializeCorrectly()
{
// Arrange
var grain = _grainFactory.GetGrain<ITestSerializationGrain>(Guid.NewGuid());
var payload = new PaymentRequest
{
OrderId = "order-123",
Amount = 99.99m,
Currency = "USD",
Items = new List<OrderItem>
{
new() { ProductId = "prod-1", Quantity = 2, Price = 25.50m },
new() { ProductId = "prod-2", Quantity = 1, Price = 48.99m }
},
Metadata = new Dictionary<string, string>
{
["customer"] = "cust-456",
["promotion"] = "SUMMER20"
}
};
var command = Command<PaymentRequest>.From(GuidHelper.CreateVersion7(), payload);
command.CommandType = "ProcessPayment";
command.CorrelationId = "correlation-789";
command.CausationId = "causation-012";
command.Metadata = new CommandMetadata
{
InitiatedBy = "system",
Priority = CommandPriority.Critical,
Tags = new Dictionary<string, string> { ["urgent"] = "true" }
};
// Act
var result = await grain.EchoCommandAsync(command);
// Assert
result.ShouldNotBeNull();
result.CommandId.ShouldBe(command.CommandId);
result.CommandType.ShouldBe("ProcessPayment");
result.CorrelationId.ShouldBe("correlation-789");
result.CausationId.ShouldBe("causation-012");
result.Value.ShouldNotBeNull();
result.Value!.OrderId.ShouldBe(payload.OrderId);
result.Value.Amount.ShouldBe(payload.Amount);
result.Value.Currency.ShouldBe(payload.Currency);
result.Value!.Items.ShouldNotBeNull();
result.Value.Items.ShouldHaveCount(2);
result.Value.Items[0].ProductId.ShouldBe("prod-1");
result.Value.Items[0].Quantity.ShouldBe(2);
result.Value.Items[0].Price.ShouldBe(25.50m);
result.Value.Metadata.ShouldNotBeNull();
result.Value.Metadata["customer"].ShouldBe("cust-456");
result.Value.Metadata["promotion"].ShouldBe("SUMMER20");
result.Metadata!.InitiatedBy.ShouldBe("system");
result.Metadata.Priority.ShouldBe(CommandPriority.Critical);
result.Metadata.Tags!["urgent"].ShouldBe("true");
}
[Fact]
public async Task PaginationCommand_ShouldSerializeCorrectly()
{
// Arrange
var grain = _grainFactory.GetGrain<ITestSerializationGrain>(Guid.NewGuid());
var options = new PaginationOptions(defaultPageSize: 25, maxPageSize: 50, minPageSize: 10);
var command = PaginationCommand.Create(GuidHelper.CreateVersion7(), skip: 25, take: 5, options);
command.CorrelationId = "pagination-correlation";
command.Metadata = new CommandMetadata
{
InitiatedBy = "tests",
Priority = CommandPriority.Low,
Tags = new Dictionary<string, string> { ["scope"] = "pagination" }
};
// Act
var pagination = await grain.EchoPaginationCommandAsync(command);
// Assert
pagination.CommandId.ShouldBe(command.CommandId);
pagination.CommandType.ShouldBe("Pagination");
pagination.Timestamp.ShouldBeCloseTo(command.Timestamp, TimeSpan.FromSeconds(1));
pagination.CorrelationId.ShouldBe("pagination-correlation");
pagination.Skip.ShouldBe(25);
pagination.Take.ShouldBe(10); // normalized to minimum page size 10
pagination.PageSize.ShouldBe(10);
pagination.PageNumber.ShouldBe(3);
pagination.Value.ShouldNotBeNull();
pagination.Value!.Skip.ShouldBe(25);
pagination.Value.Take.ShouldBe(10);
pagination.Metadata.ShouldNotBeNull();
pagination.Metadata!.InitiatedBy.ShouldBe("tests");
pagination.Metadata.Priority.ShouldBe(CommandPriority.Low);
pagination.Metadata.Tags.ShouldNotBeNull();
pagination.Metadata.Tags!["scope"].ShouldBe("pagination");
}
[Fact]
public async Task CommandT_WithEnumType_ShouldSerializeCorrectly()
{
// Arrange
var grain = _grainFactory.GetGrain<ITestSerializationGrain>(Guid.NewGuid());
var command = Command<string>.From("test-data");
command.CommandType = "CreateUser";
command.Metadata = new CommandMetadata
{
InitiatedBy = "admin",
TimeToLiveSeconds = 60
};
// Act
var result = await grain.EchoCommandAsync(command);
// Assert
result.ShouldNotBeNull();
result.Value.ShouldBe("test-data");
result.CommandType.ShouldBe("CreateUser");
result.GetCommandTypeAsEnum<TestCommandType>().Value.ShouldBe(TestCommandType.CreateUser);
result.Metadata!.InitiatedBy.ShouldBe("admin");
result.Metadata.TimeToLiveSeconds.ShouldBe(60);
}
[Fact]
public async Task CommandT_WithNullPayload_ShouldSerializeCorrectly()
{
// Arrange
var grain = _grainFactory.GetGrain<ITestSerializationGrain>(Guid.NewGuid());
var command = Command.From<string?>(null);
// Act
var result = await grain.EchoCommandAsync(command);
// Assert
result.ShouldNotBeNull();
result.Value.ShouldBeNull();
result.IsEmpty.ShouldBeTrue();
}
[Fact]
public async Task CommandMetadata_WithAllFields_ShouldSerializeCorrectly()
{
// Arrange
var grain = _grainFactory.GetGrain<ITestSerializationGrain>(Guid.NewGuid());
var metadata = new CommandMetadata
{
InitiatedBy = "user@example.com",
Source = "mobile-app",
Target = "user-service",
IpAddress = "10.0.0.1",
UserAgent = "CustomApp/1.0",
SessionId = Guid.NewGuid().ToString(),
TraceId = Guid.NewGuid().ToString(),
SpanId = Guid.NewGuid().ToString(),
Version = 2,
Priority = CommandPriority.Critical,
TimeToLiveSeconds = 600,
Tags = new Dictionary<string, string>
{
["feature"] = "payments",
["client"] = "ios",
["version"] = "14.5"
},
Extensions = new Dictionary<string, object?>
{
["rateLimitRemaining"] = 50,
["beta"] = true,
["metadata"] = new Dictionary<string, string>
{
["nested"] = "value"
}
}
};
// Act
var result = await grain.EchoMetadataAsync(metadata);
// Assert
result.ShouldNotBeNull();
result.InitiatedBy.ShouldBe(metadata.InitiatedBy);
result.Source.ShouldBe(metadata.Source);
result.Target.ShouldBe(metadata.Target);
result.IpAddress.ShouldBe(metadata.IpAddress);
result.UserAgent.ShouldBe(metadata.UserAgent);
result.SessionId.ShouldBe(metadata.SessionId);
result.TraceId.ShouldBe(metadata.TraceId);
result.SpanId.ShouldBe(metadata.SpanId);
result.Version.ShouldBe(metadata.Version);
result.Priority.ShouldBe(metadata.Priority);
result.TimeToLiveSeconds.ShouldBe(metadata.TimeToLiveSeconds);
result.Tags.ShouldNotBeNull();
result.Tags.ShouldHaveCount(3);
result.Tags!["feature"].ShouldBe("payments");
result.Tags!["client"].ShouldBe("ios");
result.Tags!["version"].ShouldBe("14.5");
result.Extensions.ShouldNotBeNull();
result.Extensions.ShouldHaveCount(3);
result.Extensions!["rateLimitRemaining"].ShouldBe(50);
result.Extensions!["beta"].ShouldBe(true);
result.Extensions!["metadata"].ShouldNotBeNull();
}
}