diff --git a/CHANGELOG.md b/CHANGELOG.md index 6354607..d260836 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v2.0.0-beta.6 - 2024-02-27 + +- Fix prefix modification algorithm and add tests. + ## v2.0.0-beta.5 - 2024-02-27 - Fix prefix modification algorithm. diff --git a/src/Nexus.Sources.Federation/Federation.cs b/src/Nexus.Sources.Federation/Federation.cs index 6681d07..fe0ff86 100644 --- a/src/Nexus.Sources.Federation/Federation.cs +++ b/src/Nexus.Sources.Federation/Federation.cs @@ -15,7 +15,7 @@ namespace Nexus.Sources public class Federation : IDataSource { private DataSourceContext _context = default!; - private Api.NexusClient _nexusClient = default!; + private Api.INexusClient _nexusClient = default!; private string _sourcePath = "/"; private string _mountPoint = default!; private string _includePattern = default!; @@ -28,6 +28,9 @@ static Federation() _jsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); } + public Func CreateNexusClient { get; set; } + = httpClient => new Api.NexusClient(httpClient); + public Task SetContextAsync(DataSourceContext context, ILogger logger, CancellationToken cancellationToken) { _context = context; @@ -49,13 +52,13 @@ public Task SetContextAsync(DataSourceContext context, ILogger logger, Cancellat httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token); - _nexusClient = new Api.NexusClient(httpClient); + _nexusClient = CreateNexusClient(httpClient); // source-path var sourcePath = _context.SourceConfiguration?.GetStringValue($"source-path"); if (sourcePath is not null) - _sourcePath = sourcePath; + _sourcePath = '/' + sourcePath.Trim('/'); // mount-point var mountPoint = _context.SourceConfiguration?.GetStringValue($"mount-point"); @@ -63,9 +66,9 @@ public Task SetContextAsync(DataSourceContext context, ILogger logger, Cancellat if (mountPoint is null) throw new Exception("The mount-point property is not set."); - _mountPoint = mountPoint; + _mountPoint = '/' + mountPoint.Trim('/'); - // source-path + // include-pattern var includePattern = _context.SourceConfiguration?.GetStringValue($"include-pattern"); _includePattern = includePattern ?? ""; @@ -132,12 +135,14 @@ public async Task ReadAsync(DateTime begin, DateTime end, ReadRequest[] requests private string ToMountPointPrefixedCatalogId(string catalogId) { - return _mountPoint + catalogId.Substring(_sourcePath.Length); + // absolute, relative + return Path.Combine(_mountPoint, catalogId.Substring(_sourcePath.Length).TrimStart('/')); } private string ToSourcePathPrefixedCatalogId(string catalogId) { - return _sourcePath + catalogId.Substring(_mountPoint.Length); + // absolute, relative + return Path.Combine(_sourcePath, catalogId.Substring(_mountPoint.Length).TrimStart('/')); } } } diff --git a/tests/Nexus.Sources.Federation.Tests/FederationTests.cs b/tests/Nexus.Sources.Federation.Tests/FederationTests.cs index 6cf21d2..f74d9fb 100644 --- a/tests/Nexus.Sources.Federation.Tests/FederationTests.cs +++ b/tests/Nexus.Sources.Federation.Tests/FederationTests.cs @@ -1,7 +1,75 @@ -namespace Nexus.Sources.Tests +using System.Text.Json; +using Microsoft.Extensions.Logging.Abstractions; +using Moq; +using Nexus.Api; +using Nexus.Extensibility; +using Xunit; + +namespace Nexus.Sources.Tests; + +public class FederationTests { - public class FederationTests + [Theory] + [InlineData("/", "/", "/TEST_CATALOG")] + [InlineData("/src", "/", "/TEST_CATALOG")] + [InlineData("src", "/", "/TEST_CATALOG")] + [InlineData("/src/", "/", "/TEST_CATALOG")] + [InlineData("/src", "/mnt", "/mnt/TEST_CATALOG")] + [InlineData("/src/", "/mnt", "/mnt/TEST_CATALOG")] + [InlineData("/src", "/mnt/", "/mnt/TEST_CATALOG")] + [InlineData("/", "/mnt", "/mnt/TEST_CATALOG")] + [InlineData("/", "/mnt/", "/mnt/TEST_CATALOG")] + [InlineData("/", "mnt/", "/mnt/TEST_CATALOG")] + [InlineData(default, "mnt", "/mnt/TEST_CATALOG")] + [InlineData("src", "", "/TEST_CATALOG")] + public async Task ProvidesCatalogRegistrations(string? sourcePath, string mountPoint, string expectedCatalogId) { - + // arrange + var catalogsClient = Mock.Of(); + + Mock.Get(catalogsClient) + .Setup(catalog => catalog.GetChildCatalogInfosAsync( + It.IsAny(), + It.IsAny() + )) + .ReturnsAsync((string catalogId, CancellationToken cancellationToken) => + { + Assert.True(catalogId.StartsWith('/')); + + var childCatalogId = catalogId == "/" + ? catalogId + "TEST_CATALOG" + : catalogId + "/" + "TEST_CATALOG"; + + var catalog = new CatalogInfo(childCatalogId, default, default, default, default, default, default, default, default, default, default, default!, default, default); + + return new List() { catalog }; + }); + + var nexusClient = Mock.Of(); + + Mock.Get(nexusClient) + .SetupGet(client => client.Catalogs) + .Returns(catalogsClient); + + var dataSource = new Federation() { CreateNexusClient = _ => nexusClient } as IDataSource; + + var context = new DataSourceContext( + ResourceLocator: new Uri("https://example.com"), + SystemConfiguration: default!, + SourceConfiguration: new Dictionary() + { + ["access-token"] = JsonSerializer.SerializeToElement(""), + ["source-path"] = JsonSerializer.SerializeToElement(sourcePath), + ["mount-point"] = JsonSerializer.SerializeToElement(mountPoint), + }, + RequestConfiguration: default!); + + await dataSource.SetContextAsync(context, NullLogger.Instance, CancellationToken.None); + + // act + var catalogRegistrations = await dataSource.GetCatalogRegistrationsAsync("/", CancellationToken.None); + + // assert + Assert.Equal(expectedCatalogId, catalogRegistrations.First().Path); } } \ No newline at end of file diff --git a/tests/Nexus.Sources.Federation.Tests/Nexus.Sources.Federation.Tests.csproj b/tests/Nexus.Sources.Federation.Tests/Nexus.Sources.Federation.Tests.csproj index 71d4230..f89d76e 100644 --- a/tests/Nexus.Sources.Federation.Tests/Nexus.Sources.Federation.Tests.csproj +++ b/tests/Nexus.Sources.Federation.Tests/Nexus.Sources.Federation.Tests.csproj @@ -7,6 +7,8 @@ + + all @@ -15,13 +17,7 @@ - - - - - - Always - + diff --git a/version.json b/version.json index c8d3709..91c6cff 100644 --- a/version.json +++ b/version.json @@ -1,4 +1,4 @@ { "version": "2.0.0", - "suffix": "beta.5" + "suffix": "beta.6" } \ No newline at end of file