Add unit tests for SBOM ingestion and transformation
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
- Implement `SbomIngestServiceCollectionExtensionsTests` to verify the SBOM ingestion pipeline exports snapshots correctly. - Create `SbomIngestTransformerTests` to ensure the transformation produces expected nodes and edges, including deduplication of license nodes and normalization of timestamps. - Add `SbomSnapshotExporterTests` to test the export functionality for manifest, adjacency, nodes, and edges. - Introduce `VexOverlayTransformerTests` to validate the transformation of VEX nodes and edges. - Set up project file for the test project with necessary dependencies and configurations. - Include JSON fixture files for testing purposes.
This commit is contained in:
110
tests/Graph/StellaOps.Graph.Indexer.Tests/GraphIdentityTests.cs
Normal file
110
tests/Graph/StellaOps.Graph.Indexer.Tests/GraphIdentityTests.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using FluentAssertions;
|
||||
using StellaOps.Graph.Indexer.Schema;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Graph.Indexer.Tests;
|
||||
|
||||
public sealed class GraphIdentityTests
|
||||
{
|
||||
private static readonly string FixturesRoot =
|
||||
Path.Combine(AppContext.BaseDirectory, "Fixtures", "v1");
|
||||
|
||||
[Fact]
|
||||
public void NodeIds_are_stable()
|
||||
{
|
||||
var nodes = LoadArray("nodes.json");
|
||||
|
||||
foreach (var node in nodes.Cast<JsonObject>())
|
||||
{
|
||||
var tenant = node["tenant"]!.GetValue<string>();
|
||||
var kind = node["kind"]!.GetValue<string>();
|
||||
var canonicalKey = (JsonObject)node["canonical_key"]!;
|
||||
var tuple = GraphIdentity.ExtractIdentityTuple(canonicalKey);
|
||||
|
||||
var expectedId = node["id"]!.GetValue<string>();
|
||||
var actualId = GraphIdentity.ComputeNodeId(tenant, kind, tuple);
|
||||
|
||||
actualId.Should()
|
||||
.Be(expectedId, $"node {kind} with canonical tuple {canonicalKey.ToJsonString()} must have deterministic id");
|
||||
|
||||
var documentClone = JsonNode.Parse(node.ToJsonString())!.AsObject();
|
||||
documentClone.Remove("hash");
|
||||
|
||||
var expectedHash = node["hash"]!.GetValue<string>();
|
||||
var actualHash = GraphIdentity.ComputeDocumentHash(documentClone);
|
||||
|
||||
actualHash.Should()
|
||||
.Be(expectedHash, $"node {kind}:{expectedId} must have deterministic document hash");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EdgeIds_are_stable()
|
||||
{
|
||||
var edges = LoadArray("edges.json");
|
||||
|
||||
foreach (var edge in edges.Cast<JsonObject>())
|
||||
{
|
||||
var tenant = edge["tenant"]!.GetValue<string>();
|
||||
var kind = edge["kind"]!.GetValue<string>();
|
||||
var canonicalKey = (JsonObject)edge["canonical_key"]!;
|
||||
var tuple = GraphIdentity.ExtractIdentityTuple(canonicalKey);
|
||||
|
||||
var expectedId = edge["id"]!.GetValue<string>();
|
||||
var actualId = GraphIdentity.ComputeEdgeId(tenant, kind, tuple);
|
||||
|
||||
actualId.Should()
|
||||
.Be(expectedId, $"edge {kind} with canonical tuple {canonicalKey.ToJsonString()} must have deterministic id");
|
||||
|
||||
var documentClone = JsonNode.Parse(edge.ToJsonString())!.AsObject();
|
||||
documentClone.Remove("hash");
|
||||
|
||||
var expectedHash = edge["hash"]!.GetValue<string>();
|
||||
var actualHash = GraphIdentity.ComputeDocumentHash(documentClone);
|
||||
|
||||
actualHash.Should()
|
||||
.Be(expectedHash, $"edge {kind}:{expectedId} must have deterministic document hash");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AttributeCoverage_matches_matrix()
|
||||
{
|
||||
var matrix = LoadObject("schema-matrix.json");
|
||||
var nodeExpectations = (JsonObject)matrix["nodes"]!;
|
||||
var edgeExpectations = (JsonObject)matrix["edges"]!;
|
||||
|
||||
var nodes = LoadArray("nodes.json");
|
||||
foreach (var node in nodes.Cast<JsonObject>())
|
||||
{
|
||||
var kind = node["kind"]!.GetValue<string>();
|
||||
var expectedAttributes = nodeExpectations[kind]!.AsArray().Select(x => x!.GetValue<string>()).OrderBy(x => x, StringComparer.Ordinal).ToArray();
|
||||
var actualAttributes = ((JsonObject)node["attributes"]!).Select(pair => pair.Key).OrderBy(x => x, StringComparer.Ordinal).ToArray();
|
||||
|
||||
actualAttributes.Should()
|
||||
.Equal(expectedAttributes, $"node kind {kind} must align with schema matrix");
|
||||
}
|
||||
|
||||
var edges = LoadArray("edges.json");
|
||||
foreach (var edge in edges.Cast<JsonObject>())
|
||||
{
|
||||
var kind = edge["kind"]!.GetValue<string>();
|
||||
var expectedAttributes = edgeExpectations[kind]!.AsArray().Select(x => x!.GetValue<string>()).OrderBy(x => x, StringComparer.Ordinal).ToArray();
|
||||
var actualAttributes = ((JsonObject)edge["attributes"]!).Select(pair => pair.Key).OrderBy(x => x, StringComparer.Ordinal).ToArray();
|
||||
|
||||
actualAttributes.Should()
|
||||
.Equal(expectedAttributes, $"edge kind {kind} must align with schema matrix");
|
||||
}
|
||||
}
|
||||
|
||||
private static JsonArray LoadArray(string fileName)
|
||||
=> (JsonArray)JsonNode.Parse(File.ReadAllText(GetFixturePath(fileName)))!;
|
||||
|
||||
private static JsonObject LoadObject(string fileName)
|
||||
=> (JsonObject)JsonNode.Parse(File.ReadAllText(GetFixturePath(fileName)))!;
|
||||
|
||||
private static string GetFixturePath(string fileName)
|
||||
=> Path.Combine(FixturesRoot, fileName);
|
||||
}
|
||||
Reference in New Issue
Block a user