513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
96 lines
3.9 KiB
C#
96 lines
3.9 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using NSubstitute;
|
|
using ZB.MOM.WW.ScadaBridge.CentralUI.Services;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Schemas;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Design;
|
|
|
|
/// <summary>
|
|
/// Tests for the schema-library read-accessor that the schema-driven
|
|
/// value-entry forms reuse to resolve <c>{"$ref":"lib:Name"}</c> pointers. It projects
|
|
/// <see cref="ISharedSchemaRepository"/> onto a name → schema-JSON map (and a single-name
|
|
/// lookup) over a fresh DI scope. The repository is substituted and registered scoped so
|
|
/// the service's <c>IServiceScopeFactory</c> resolves it exactly as it would at runtime.
|
|
/// </summary>
|
|
public class SchemaLibraryQueryServiceTests
|
|
{
|
|
private static (SchemaLibraryQueryService Service, ISharedSchemaRepository Repo) Build()
|
|
{
|
|
var repo = Substitute.For<ISharedSchemaRepository>();
|
|
var services = new ServiceCollection();
|
|
services.AddScoped(_ => repo);
|
|
var provider = services.BuildServiceProvider();
|
|
var service = new SchemaLibraryQueryService(provider.GetRequiredService<IServiceScopeFactory>());
|
|
return (service, repo);
|
|
}
|
|
|
|
/// <summary>Verifies that the schema map associates each library name with its schema JSON.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task GetSchemaMapAsync_MapsNameToSchemaJson()
|
|
{
|
|
var (service, repo) = Build();
|
|
repo.ListAsync(Arg.Any<CancellationToken>()).Returns(new List<SharedSchema>
|
|
{
|
|
new() { Id = 1, Name = "Address", SchemaJson = "{\"type\":\"object\"}" },
|
|
new() { Id = 2, Name = "GeoPoint", SchemaJson = "{\"type\":\"array\"}" },
|
|
});
|
|
|
|
var map = await service.GetSchemaMapAsync();
|
|
|
|
Assert.Equal(2, map.Count);
|
|
Assert.Equal("{\"type\":\"object\"}", map["Address"]);
|
|
Assert.Equal("{\"type\":\"array\"}", map["GeoPoint"]);
|
|
}
|
|
|
|
/// <summary>Verifies that an empty library yields an empty schema map.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task GetSchemaMapAsync_EmptyLibrary_ReturnsEmptyMap()
|
|
{
|
|
var (service, repo) = Build();
|
|
repo.ListAsync(Arg.Any<CancellationToken>()).Returns(new List<SharedSchema>());
|
|
|
|
var map = await service.GetSchemaMapAsync();
|
|
|
|
Assert.Empty(map);
|
|
}
|
|
|
|
/// <summary>Verifies that resolving a known name returns its schema JSON.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ResolveAsync_KnownName_ReturnsSchemaJson()
|
|
{
|
|
var (service, repo) = Build();
|
|
repo.GetByNameAsync("Address", Arg.Any<CancellationToken>())
|
|
.Returns(new SharedSchema { Id = 1, Name = "Address", SchemaJson = "{\"type\":\"object\"}" });
|
|
|
|
var json = await service.ResolveAsync("Address");
|
|
|
|
Assert.Equal("{\"type\":\"object\"}", json);
|
|
}
|
|
|
|
/// <summary>Verifies that resolving an unknown name returns null.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ResolveAsync_UnknownName_ReturnsNull()
|
|
{
|
|
var (service, repo) = Build();
|
|
repo.GetByNameAsync("Missing", Arg.Any<CancellationToken>()).Returns((SharedSchema?)null);
|
|
|
|
Assert.Null(await service.ResolveAsync("Missing"));
|
|
}
|
|
|
|
/// <summary>Verifies that resolving an empty name returns null without querying the repository.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ResolveAsync_EmptyName_ReturnsNull_WithoutHittingRepo()
|
|
{
|
|
var (service, repo) = Build();
|
|
|
|
Assert.Null(await service.ResolveAsync(""));
|
|
await repo.DidNotReceive().GetByNameAsync(Arg.Any<string>(), Arg.Any<CancellationToken>());
|
|
}
|
|
}
|