|
1 | | -using System.Net; |
| 1 | +using System.Net; |
2 | 2 |
|
3 | 3 | using AAS.TwinEngine.DataEngine.Infrastructure.Http.Clients; |
4 | 4 | using AAS.TwinEngine.DataEngine.Infrastructure.Monitoring; |
@@ -286,6 +286,245 @@ public async Task CheckHealthAsync_Uses_HealthCheck_Client_Names_Without_Retry_P |
286 | 286 | clientFactory.DidNotReceive().CreateClient($"{HttpClientNames.PluginDataProviderPrefix}Plugin1"); |
287 | 287 | } |
288 | 288 |
|
| 289 | + [Fact] |
| 290 | + public async Task CheckHealthAsync_Uses_Custom_HealthEndpoint_When_Configured() |
| 291 | + { |
| 292 | + string? requestedPath = null; |
| 293 | + |
| 294 | + var clientFactory = Substitute.For<ICreateClient>(); |
| 295 | + |
| 296 | + clientFactory |
| 297 | + .CreateClient(Arg.Any<string>()) |
| 298 | + .Returns(_ => |
| 299 | + { |
| 300 | + var handler = new StubHttpMessageHandler((request, _) => |
| 301 | + { |
| 302 | + requestedPath = request.RequestUri!.ToString(); |
| 303 | + return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)); |
| 304 | + }); |
| 305 | + |
| 306 | + return new HttpClient(handler) |
| 307 | + { |
| 308 | + BaseAddress = new Uri("http://localhost") |
| 309 | + }; |
| 310 | + }); |
| 311 | + |
| 312 | + var pluginConfig = Options.Create(new PluginsConfig |
| 313 | + { |
| 314 | + Instances = |
| 315 | + [ |
| 316 | + new ServiceInstance |
| 317 | + { |
| 318 | + Name = "Plugin1", |
| 319 | + BaseUrl = new Uri("http://localhost"), |
| 320 | + HealthEndpoint = "custom-health" |
| 321 | + } |
| 322 | + ] |
| 323 | + }); |
| 324 | + |
| 325 | + var logger = Substitute.For<ILogger<PluginAvailabilityHealthCheck>>(); |
| 326 | + |
| 327 | + var sut = new PluginAvailabilityHealthCheck(clientFactory, pluginConfig, logger); |
| 328 | + |
| 329 | + await sut.CheckHealthAsync(new HealthCheckContext(), CancellationToken.None); |
| 330 | + |
| 331 | + Assert.Contains("custom-health", requestedPath); |
| 332 | + } |
| 333 | + |
| 334 | + [Fact] |
| 335 | + public async Task CheckHealthAsync_Uses_Default_HealthEndpoint_When_Not_Configured() |
| 336 | + { |
| 337 | + string? requestedPath = null; |
| 338 | + |
| 339 | + var clientFactory = Substitute.For<ICreateClient>(); |
| 340 | + |
| 341 | + clientFactory |
| 342 | + .CreateClient(Arg.Any<string>()) |
| 343 | + .Returns(_ => |
| 344 | + { |
| 345 | + var handler = new StubHttpMessageHandler((request, _) => |
| 346 | + { |
| 347 | + requestedPath = request.RequestUri!.ToString(); |
| 348 | + return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)); |
| 349 | + }); |
| 350 | + |
| 351 | + return new HttpClient(handler) |
| 352 | + { |
| 353 | + BaseAddress = new Uri("http://localhost") |
| 354 | + }; |
| 355 | + }); |
| 356 | + |
| 357 | + var pluginConfig = Options.Create(new PluginsConfig |
| 358 | + { |
| 359 | + Instances = |
| 360 | + [ |
| 361 | + new ServiceInstance |
| 362 | + { |
| 363 | + Name = "Plugin1", |
| 364 | + BaseUrl = new Uri("http://localhost"), |
| 365 | + HealthEndpoint = string.Empty |
| 366 | + } |
| 367 | + ] |
| 368 | + }); |
| 369 | + |
| 370 | + var logger = Substitute.For<ILogger<PluginAvailabilityHealthCheck>>(); |
| 371 | + |
| 372 | + var sut = new PluginAvailabilityHealthCheck(clientFactory, pluginConfig, logger); |
| 373 | + |
| 374 | + await sut.CheckHealthAsync(new HealthCheckContext(), CancellationToken.None); |
| 375 | + |
| 376 | + Assert.Contains("healthz", requestedPath); |
| 377 | + } |
| 378 | + |
| 379 | + [Fact] |
| 380 | + public async Task CheckHealthAsync_Uses_Default_When_HealthEndpoint_Is_Empty() |
| 381 | + { |
| 382 | + string? requestedPath = null; |
| 383 | + |
| 384 | + var clientFactory = Substitute.For<ICreateClient>(); |
| 385 | + |
| 386 | + clientFactory |
| 387 | + .CreateClient(Arg.Any<string>()) |
| 388 | + .Returns(_ => |
| 389 | + { |
| 390 | + var handler = new StubHttpMessageHandler((request, _) => |
| 391 | + { |
| 392 | + requestedPath = request.RequestUri!.ToString(); |
| 393 | + return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)); |
| 394 | + }); |
| 395 | + |
| 396 | + return new HttpClient(handler) |
| 397 | + { |
| 398 | + BaseAddress = new Uri("http://localhost") |
| 399 | + }; |
| 400 | + }); |
| 401 | + |
| 402 | + var pluginConfig = Options.Create(new PluginsConfig |
| 403 | + { |
| 404 | + Instances = |
| 405 | + [ |
| 406 | + new ServiceInstance |
| 407 | + { |
| 408 | + Name = "Plugin1", |
| 409 | + BaseUrl = new Uri("http://localhost"), |
| 410 | + HealthEndpoint = "" |
| 411 | + } |
| 412 | + ] |
| 413 | + }); |
| 414 | + |
| 415 | + var logger = Substitute.For<ILogger<PluginAvailabilityHealthCheck>>(); |
| 416 | + |
| 417 | + var sut = new PluginAvailabilityHealthCheck(clientFactory, pluginConfig, logger); |
| 418 | + |
| 419 | + await sut.CheckHealthAsync(new HealthCheckContext(), CancellationToken.None); |
| 420 | + |
| 421 | + Assert.Contains("healthz", requestedPath); |
| 422 | + } |
| 423 | + |
| 424 | + [Fact] |
| 425 | + public async Task CheckHealthAsync_LogsWarning_When_HealthEndpoint_Is_Blank() |
| 426 | + { |
| 427 | + var clientFactory = Substitute.For<ICreateClient>(); |
| 428 | + |
| 429 | + clientFactory |
| 430 | + .CreateClient(Arg.Any<string>()) |
| 431 | + .Returns(_ => |
| 432 | + { |
| 433 | + var handler = new StubHttpMessageHandler((_, _) => |
| 434 | + Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK))); |
| 435 | + |
| 436 | + return new HttpClient(handler) |
| 437 | + { |
| 438 | + BaseAddress = new Uri("http://localhost") |
| 439 | + }; |
| 440 | + }); |
| 441 | + |
| 442 | + var pluginConfig = Options.Create(new PluginsConfig |
| 443 | + { |
| 444 | + Instances = |
| 445 | + [ |
| 446 | + new ServiceInstance |
| 447 | + { |
| 448 | + Name = "Plugin1", |
| 449 | + BaseUrl = new Uri("http://localhost"), |
| 450 | + HealthEndpoint = "" |
| 451 | + } |
| 452 | + ] |
| 453 | + }); |
| 454 | + |
| 455 | + var logger = Substitute.For<ILogger<PluginAvailabilityHealthCheck>>(); |
| 456 | + |
| 457 | + var sut = new PluginAvailabilityHealthCheck(clientFactory, pluginConfig, logger); |
| 458 | + |
| 459 | + await sut.CheckHealthAsync(new HealthCheckContext(), CancellationToken.None); |
| 460 | + |
| 461 | + logger.Received(1).Log( |
| 462 | + LogLevel.Warning, |
| 463 | + Arg.Any<EventId>(), |
| 464 | + Arg.Any<object>(), |
| 465 | + Arg.Any<Exception>(), |
| 466 | + Arg.Any<Func<object, Exception, string>>()); |
| 467 | + } |
| 468 | + |
| 469 | + [Fact] |
| 470 | + public async Task CheckHealthAsync_WhenMultiplePlugins_OneUsesCustomAndOneUsesDefaultEndpoint() |
| 471 | + { |
| 472 | + // Arrange |
| 473 | + var requestedPaths = new List<string>(); |
| 474 | + |
| 475 | + var clientFactory = Substitute.For<ICreateClient>(); |
| 476 | + |
| 477 | + clientFactory |
| 478 | + .CreateClient(Arg.Any<string>()) |
| 479 | + .Returns(_ => |
| 480 | + { |
| 481 | + var handler = new StubHttpMessageHandler((request, _) => |
| 482 | + { |
| 483 | + requestedPaths.Add(request.RequestUri!.ToString()); |
| 484 | + return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)); |
| 485 | + }); |
| 486 | + |
| 487 | + return new HttpClient(handler) |
| 488 | + { |
| 489 | + BaseAddress = new Uri("http://localhost") |
| 490 | + }; |
| 491 | + }); |
| 492 | + |
| 493 | + var pluginConfig = Options.Create(new PluginsConfig |
| 494 | + { |
| 495 | + Instances = |
| 496 | + [ |
| 497 | + new ServiceInstance |
| 498 | + { |
| 499 | + Name = "Plugin1", |
| 500 | + BaseUrl = new Uri("http://localhost"), |
| 501 | + HealthEndpoint = "custom-health" |
| 502 | + }, |
| 503 | + new ServiceInstance |
| 504 | + { |
| 505 | + Name = "Plugin2", |
| 506 | + BaseUrl = new Uri("http://localhost"), |
| 507 | + HealthEndpoint = null |
| 508 | + } |
| 509 | + ] |
| 510 | + }); |
| 511 | + |
| 512 | + var logger = Substitute.For<ILogger<PluginAvailabilityHealthCheck>>(); |
| 513 | + |
| 514 | + var sut = new PluginAvailabilityHealthCheck(clientFactory, pluginConfig, logger); |
| 515 | + |
| 516 | + // Act |
| 517 | + var result = await sut.CheckHealthAsync(new HealthCheckContext(), CancellationToken.None); |
| 518 | + |
| 519 | + // Assert |
| 520 | + Assert.Equal(HealthStatus.Healthy, result.Status); |
| 521 | + |
| 522 | + Assert.Equal(2, requestedPaths.Count); |
| 523 | + |
| 524 | + Assert.Contains(requestedPaths, p => p.Contains("custom-health")); |
| 525 | + Assert.Contains(requestedPaths, p => p.Contains("healthz")); |
| 526 | + } |
| 527 | + |
289 | 528 | private static HttpClient CreateHttpClient(HttpStatusCode statusCode) |
290 | 529 | { |
291 | 530 | var handler = new StubHttpMessageHandler((_, _) => |
|
0 commit comments