blockers 2
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Reflection;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using StellaOps.SbomService.Repositories;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.SbomService.Tests;
|
||||
@@ -12,7 +17,29 @@ public class ProjectionEndpointTests : IClassFixture<WebApplicationFactory<Progr
|
||||
|
||||
public ProjectionEndpointTests(WebApplicationFactory<Program> factory)
|
||||
{
|
||||
_factory = factory.WithWebHostBuilder(_ => { });
|
||||
_factory = factory.WithWebHostBuilder(builder =>
|
||||
{
|
||||
var fixturePath = GetProjectionFixturePath();
|
||||
if (!File.Exists(fixturePath))
|
||||
{
|
||||
throw new InvalidOperationException($"Projection fixture missing at {fixturePath}");
|
||||
}
|
||||
|
||||
builder.ConfigureAppConfiguration((_, config) =>
|
||||
{
|
||||
config.AddInMemoryCollection(new Dictionary<string, string?>
|
||||
{
|
||||
["SbomService:ProjectionsPath"] = fixturePath
|
||||
});
|
||||
});
|
||||
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
// Avoid MongoDB dependency in tests; use seeded in-memory repo.
|
||||
services.RemoveAll<IComponentLookupRepository>();
|
||||
services.AddSingleton<IComponentLookupRepository, InMemoryComponentLookupRepository>();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -22,7 +49,11 @@ public class ProjectionEndpointTests : IClassFixture<WebApplicationFactory<Progr
|
||||
|
||||
var response = await client.GetAsync("/sboms/snap-001/projection");
|
||||
|
||||
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
if (response.StatusCode != HttpStatusCode.BadRequest)
|
||||
{
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
throw new Xunit.Sdk.XunitException($"Expected 400 but got {(int)response.StatusCode}: {response.StatusCode}. Body: {body}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -42,4 +73,14 @@ public class ProjectionEndpointTests : IClassFixture<WebApplicationFactory<Progr
|
||||
}
|
||||
|
||||
private sealed record ProjectionResponse(string snapshotId, string tenantId, string schemaVersion, string hash, System.Text.Json.JsonElement projection);
|
||||
|
||||
private static string GetProjectionFixturePath()
|
||||
{
|
||||
// Resolve docs/modules/sbomservice/fixtures/lnm-v1/projections.json relative to test bin directory.
|
||||
var baseDir = AppContext.BaseDirectory;
|
||||
var candidate = Path.GetFullPath(Path.Combine(
|
||||
baseDir,
|
||||
"../../../../../../docs/modules/sbomservice/fixtures/lnm-v1/projections.json"));
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Update="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.0-rc.2.25502.107" />
|
||||
<PackageReference Update="FluentAssertions" Version="6.12.0" />
|
||||
<PackageReference Update="xunit" Version="2.9.2" />
|
||||
<PackageReference Update="xunit.runner.visualstudio" Version="2.8.2" />
|
||||
<PackageReference Update="coverlet.collector" Version="6.0.4" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -17,15 +17,23 @@ builder.Configuration
|
||||
builder.Services.AddOptions();
|
||||
builder.Services.AddLogging();
|
||||
|
||||
// Register SBOM query services (InMemory seed; replace with Mongo-backed repository later).
|
||||
// Register SBOM query services (InMemory seed; replace with Mongo-backed repository later).
|
||||
builder.Services.AddSingleton<IComponentLookupRepository>(sp =>
|
||||
{
|
||||
var config = sp.GetRequiredService<IConfiguration>();
|
||||
var mongoConn = config.GetConnectionString("SbomServiceMongo") ?? "mongodb://localhost:27017";
|
||||
var mongoClient = new MongoDB.Driver.MongoClient(mongoConn);
|
||||
var databaseName = config.GetSection("SbomService")?["Database"] ?? "sbomservice";
|
||||
var database = mongoClient.GetDatabase(databaseName);
|
||||
return new MongoComponentLookupRepository(database);
|
||||
try
|
||||
{
|
||||
var config = sp.GetRequiredService<IConfiguration>();
|
||||
var mongoConn = config.GetConnectionString("SbomServiceMongo") ?? "mongodb://localhost:27017";
|
||||
var mongoClient = new MongoDB.Driver.MongoClient(mongoConn);
|
||||
var databaseName = config.GetSection("SbomService")?["Database"] ?? "sbomservice";
|
||||
var database = mongoClient.GetDatabase(databaseName);
|
||||
return new MongoComponentLookupRepository(database);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Fallback for test/offline environments when Mongo driver is unavailable.
|
||||
return new InMemoryComponentLookupRepository();
|
||||
}
|
||||
});
|
||||
builder.Services.AddSingleton<ISbomQueryService, InMemorySbomQueryService>();
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ using StellaOps.SbomService.Models;
|
||||
|
||||
namespace StellaOps.SbomService.Repositories;
|
||||
|
||||
internal sealed class InMemoryComponentLookupRepository : IComponentLookupRepository
|
||||
public sealed class InMemoryComponentLookupRepository : IComponentLookupRepository
|
||||
{
|
||||
private static readonly IReadOnlyList<ComponentLookupRecord> Components = Seed();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user