-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnowledgeBaseOutboxIntegrationTests.cs
More file actions
265 lines (216 loc) · 11.3 KB
/
Copy pathKnowledgeBaseOutboxIntegrationTests.cs
File metadata and controls
265 lines (216 loc) · 11.3 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
using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using BuildingBlocks.Application.Dispatching;
using BuildingBlocks.Infrastructure.IntegrationEvents;
using KnowledgeBase.Api;
using KnowledgeBase.PublicContracts.Events;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Npgsql;
using BuildingBlocks.Testing.Http;
using static BuildingBlocks.Testing.Http.IntegrationTestHttpHelpers;
namespace Integration.Tests.Modules.KnowledgeBase.Events;
public sealed class KnowledgeBaseOutboxIntegrationTests
{
[Xunit.Fact]
public async Task KnowledgeBasePublishCommandReplaysAndSuppressesDuplicateWritesForSameIdempotencyKey()
{
await using var application = await PostgresBackedApiApplication.StartAsync();
var client = application.App.GetTestClient();
client.BaseAddress = new Uri("https://localhost");
var adminSession = await SignInAsync(client, "admin", "LocalOnly!123");
var entryId = await CreateDraftAsync(client, adminSession, "Replay-safe knowledge entry", "repeatable-kb-entry");
const string requestKey = "knowledge-base-publish-request-2";
var first = await SendJsonAsync(
client,
HttpMethod.Put,
$"/api/v1/knowledge-base/manage/entries/{entryId}/publish",
new PublishKnowledgeEntryRequest(ExpectedVersion: 1),
adminSession.Cookies,
adminSession.HeaderName,
adminSession.RequestToken,
requestKey);
var second = await SendJsonAsync(
client,
HttpMethod.Put,
$"/api/v1/knowledge-base/manage/entries/{entryId}/publish",
new PublishKnowledgeEntryRequest(ExpectedVersion: 1),
adminSession.Cookies,
adminSession.HeaderName,
adminSession.RequestToken,
requestKey);
Xunit.Assert.Equal(HttpStatusCode.OK, first.StatusCode);
Xunit.Assert.Equal(HttpStatusCode.OK, second.StatusCode);
using var firstJson = await ReadJsonAsync(first);
using var secondJson = await ReadJsonAsync(second);
Xunit.Assert.Equal(
firstJson.RootElement.GetProperty("entryId").GetGuid(),
secondJson.RootElement.GetProperty("entryId").GetGuid());
Xunit.Assert.Equal("published", firstJson.RootElement.GetProperty("status").GetString());
var configuration = application.App.Services.GetRequiredService<IConfiguration>();
var connectionString = configuration.GetConnectionString("BaselineDatabase")
?? throw new InvalidOperationException("Integration test requires BaselineDatabase connection string.");
Xunit.Assert.Equal(2, await CountKnowledgeEntryOutboxMessagesAsync(connectionString, entryId));
Xunit.Assert.Equal(new[] { 1, 2 }, await ReadKnowledgeEntryOutboxVersionsAsync(connectionString, entryId));
}
[Xunit.Fact]
public async Task KnowledgeBasePublishCommandRejectsConflictingPayloadForSameIdempotencyKey()
{
await using var application = await PostgresBackedApiApplication.StartAsync();
var client = application.App.GetTestClient();
client.BaseAddress = new Uri("https://localhost");
var adminSession = await SignInAsync(client, "admin", "LocalOnly!123");
var entryId = await CreateDraftAsync(client, adminSession, "Conflict knowledge entry", "conflict-kb-entry");
const string requestKey = "knowledge-base-publish-request-3";
var first = await SendJsonAsync(
client,
HttpMethod.Put,
$"/api/v1/knowledge-base/manage/entries/{entryId}/publish",
new PublishKnowledgeEntryRequest(ExpectedVersion: 1),
adminSession.Cookies,
adminSession.HeaderName,
adminSession.RequestToken,
requestKey);
var conflict = await SendJsonAsync(
client,
HttpMethod.Put,
$"/api/v1/knowledge-base/manage/entries/{entryId}/publish",
new PublishKnowledgeEntryRequest(ExpectedVersion: 2),
adminSession.Cookies,
adminSession.HeaderName,
adminSession.RequestToken,
requestKey);
Xunit.Assert.Equal(HttpStatusCode.OK, first.StatusCode);
Xunit.Assert.Equal(HttpStatusCode.Conflict, conflict.StatusCode);
using var conflictJson = await ReadJsonAsync(conflict);
Xunit.Assert.Equal("idempotency.request_conflict", conflictJson.RootElement.GetProperty("code").GetString());
}
[Xunit.Fact]
public async Task KnowledgeBasePublishCommandDualPublishesCompatibilityEventsForTheSameBusinessPublication()
{
var probe = new KnowledgeBaseDeliveryProbe();
await using var application = await PostgresBackedApiApplication.StartAsync(
configureBuilder: builder =>
{
builder.Services.AddSingleton(probe);
builder.Services.AddDispatcher(typeof(KnowledgeBasePublishedEventHandler).Assembly);
builder.Services.AddPostgresIntegrationEventInbox("knowledge-base", "knowledge_base");
},
configureExtraMigrations: static services => services.AddPostgresIntegrationEventInbox("knowledge-base", "knowledge_base"));
var client = application.App.GetTestClient();
client.BaseAddress = new Uri("https://localhost");
var adminSession = await SignInAsync(client, "admin", "LocalOnly!123");
var entryId = await CreateDraftAsync(client, adminSession, "Baseline knowledge entry", "baseline-knowledge-entry");
var response = await SendJsonAsync(
client,
HttpMethod.Put,
$"/api/v1/knowledge-base/manage/entries/{entryId}/publish",
new PublishKnowledgeEntryRequest(ExpectedVersion: 1),
adminSession.Cookies,
adminSession.HeaderName,
adminSession.RequestToken,
"knowledge-base-publish-request-4");
Xunit.Assert.Equal(HttpStatusCode.OK, response.StatusCode);
using var json = await ReadJsonAsync(response);
Xunit.Assert.Equal(entryId, json.RootElement.GetProperty("entryId").GetGuid());
var configuration = application.App.Services.GetRequiredService<IConfiguration>();
var connectionString = configuration.GetConnectionString("BaselineDatabase")
?? throw new InvalidOperationException("Integration test requires BaselineDatabase connection string.");
Xunit.Assert.Equal(2, await CountKnowledgeEntryOutboxMessagesAsync(connectionString, entryId));
Xunit.Assert.Equal(new[] { 1, 2 }, await ReadKnowledgeEntryOutboxVersionsAsync(connectionString, entryId));
using var scope = application.App.Services.CreateScope();
var dispatcher = scope.ServiceProvider.GetRequiredService<IIntegrationEventOutboxDispatcher>();
var dispatched = await dispatcher.DispatchAvailableAsync(CancellationToken.None);
Xunit.Assert.Equal(2, dispatched);
Xunit.Assert.Single(probe.V1Deliveries);
Xunit.Assert.Single(probe.V2Deliveries);
Xunit.Assert.Equal(entryId, probe.V1Deliveries[0].EntryId);
Xunit.Assert.Equal(entryId, probe.V2Deliveries[0].EntryId);
Xunit.Assert.Equal(probe.V1Deliveries[0].EntryId, probe.V2Deliveries[0].EntryId);
Xunit.Assert.NotEqual(probe.V1Deliveries[0].EventId, probe.V2Deliveries[0].EventId);
Xunit.Assert.Equal("baseline-knowledge-entry", probe.V1Deliveries[0].Slug);
Xunit.Assert.Equal("baseline-knowledge-entry", probe.V2Deliveries[0].Slug);
Xunit.Assert.Equal("Baseline knowledge entry", probe.V1Deliveries[0].Title);
Xunit.Assert.Equal("Baseline knowledge entry", probe.V2Deliveries[0].Title);
Xunit.Assert.Equal("Contracts", probe.V2Deliveries[0].Category);
}
private static async Task<Guid> CreateDraftAsync(HttpClient client, AuthenticatedSession session, string title, string slug)
{
var createResponse = await SendJsonAsync(
client,
HttpMethod.Post,
"/api/v1/knowledge-base/manage/entries",
new CreateKnowledgeEntryRequest(
Slug: slug,
Title: title,
Body: "Generated contracts should be refreshed from the backend OpenAPI snapshot.",
Category: "Contracts",
Featured: false,
SortOrder: 20),
session.Cookies,
session.HeaderName,
session.RequestToken);
createResponse.EnsureSuccessStatusCode();
using var createJson = await ReadJsonAsync(createResponse);
return createJson.RootElement.GetProperty("entryId").GetGuid();
}
private static async Task<int> CountKnowledgeEntryOutboxMessagesAsync(string connectionString, Guid entryId)
{
const string sql = "SELECT COUNT(*) FROM knowledge_base.integration_outbox WHERE payload ->> 'entryId' = @entryId;";
await using var connection = new NpgsqlConnection(connectionString);
await connection.OpenAsync();
await using var command = new NpgsqlCommand(sql, connection);
command.Parameters.AddWithValue("entryId", entryId.ToString());
return Convert.ToInt32(await command.ExecuteScalarAsync());
}
private static async Task<int[]> ReadKnowledgeEntryOutboxVersionsAsync(string connectionString, Guid entryId)
{
const string sql = "SELECT event_version FROM knowledge_base.integration_outbox WHERE payload ->> 'entryId' = @entryId ORDER BY event_version;";
await using var connection = new NpgsqlConnection(connectionString);
await connection.OpenAsync();
await using var command = new NpgsqlCommand(sql, connection);
command.Parameters.AddWithValue("entryId", entryId.ToString());
await using var reader = await command.ExecuteReaderAsync();
var versions = new List<int>();
while (await reader.ReadAsync())
{
versions.Add(reader.GetInt32(0));
}
return versions.ToArray();
}
}
internal sealed class KnowledgeBasePublishedEventHandler : IModuleScopedIntegrationEventHandler<KnowledgeEntryPublishedEventV1>
{
private readonly KnowledgeBaseDeliveryProbe _probe;
public KnowledgeBasePublishedEventHandler(KnowledgeBaseDeliveryProbe probe)
{
_probe = probe;
}
public string ModuleKey => "knowledge-base";
public Task Handle(KnowledgeEntryPublishedEventV1 integrationEvent, CancellationToken cancellationToken)
{
_probe.V1Deliveries.Add(integrationEvent);
return Task.CompletedTask;
}
}
internal sealed class KnowledgeBasePublishedEventV2Handler : IModuleScopedIntegrationEventHandler<KnowledgeEntryPublishedEventV2>
{
private readonly KnowledgeBaseDeliveryProbe _probe;
public KnowledgeBasePublishedEventV2Handler(KnowledgeBaseDeliveryProbe probe)
{
_probe = probe;
}
public string ModuleKey => "knowledge-base";
public Task Handle(KnowledgeEntryPublishedEventV2 integrationEvent, CancellationToken cancellationToken)
{
_probe.V2Deliveries.Add(integrationEvent);
return Task.CompletedTask;
}
}
internal sealed class KnowledgeBaseDeliveryProbe
{
public List<KnowledgeEntryPublishedEventV1> V1Deliveries { get; } = [];
public List<KnowledgeEntryPublishedEventV2> V2Deliveries { get; } = [];
}