Skip to content

Commit

Permalink
Fix prefix modification algorithm and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollo3zehn committed Feb 27, 2024
1 parent 0b1c294 commit 3729413
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
19 changes: 12 additions & 7 deletions src/Nexus.Sources.Federation/Federation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
Expand All @@ -28,6 +28,9 @@ static Federation()
_jsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
}

public Func<HttpClient, Api.INexusClient> CreateNexusClient { get; set; }
= httpClient => new Api.NexusClient(httpClient);

public Task SetContextAsync(DataSourceContext context, ILogger logger, CancellationToken cancellationToken)
{
_context = context;
Expand All @@ -49,23 +52,23 @@ 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");

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 ?? "";

Expand Down Expand Up @@ -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('/'));
}
}
}
74 changes: 71 additions & 3 deletions tests/Nexus.Sources.Federation.Tests/FederationTests.cs
Original file line number Diff line number Diff line change
@@ -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<ICatalogsClient>();

Mock.Get(catalogsClient)
.Setup(catalog => catalog.GetChildCatalogInfosAsync(
It.IsAny<string>(),
It.IsAny<CancellationToken>()
))
.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<CatalogInfo>() { catalog };
});

var nexusClient = Mock.Of<INexusClient>();

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<string, JsonElement>()
{
["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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Nexus.Extensibility" Version="2.0.0-beta.21" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -15,13 +17,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Nexus.Sources.Federation\Nexus.Sources.Federation.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="*Database/**/*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<ProjectReference Include="../../src/Nexus.Sources.Federation/Nexus.Sources.Federation.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "2.0.0",
"suffix": "beta.5"
"suffix": "beta.6"
}

0 comments on commit 3729413

Please sign in to comment.