83 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			83 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.Linq;
 | 
						|
using System.Threading;
 | 
						|
using System.Threading.Tasks;
 | 
						|
using MongoDB.Bson;
 | 
						|
using MongoDB.Driver;
 | 
						|
using StellaOps.Concelier.Storage.Mongo;
 | 
						|
using StellaOps.Concelier.Storage.Mongo.Conflicts;
 | 
						|
using StellaOps.Concelier.Testing;
 | 
						|
using Xunit;
 | 
						|
 | 
						|
namespace StellaOps.Concelier.Storage.Mongo.Tests;
 | 
						|
 | 
						|
[Collection("mongo-fixture")]
 | 
						|
public sealed class AdvisoryConflictStoreTests
 | 
						|
{
 | 
						|
    private readonly IMongoDatabase _database;
 | 
						|
 | 
						|
    public AdvisoryConflictStoreTests(MongoIntegrationFixture fixture)
 | 
						|
    {
 | 
						|
        _database = fixture.Database ?? throw new ArgumentNullException(nameof(fixture.Database));
 | 
						|
    }
 | 
						|
 | 
						|
    [Fact]
 | 
						|
    public async Task InsertAndRetrieve_PersistsConflicts()
 | 
						|
    {
 | 
						|
        var store = new AdvisoryConflictStore(_database);
 | 
						|
        var vulnerabilityKey = $"CVE-{Guid.NewGuid():N}";
 | 
						|
        var baseTime = DateTimeOffset.UtcNow;
 | 
						|
        var statementIds = new[] { Guid.NewGuid(), Guid.NewGuid() };
 | 
						|
 | 
						|
        var conflict = new AdvisoryConflictRecord(
 | 
						|
            Guid.NewGuid(),
 | 
						|
            vulnerabilityKey,
 | 
						|
            new byte[] { 0x10, 0x20 },
 | 
						|
            baseTime,
 | 
						|
            baseTime.AddSeconds(30),
 | 
						|
            statementIds,
 | 
						|
            new BsonDocument("explanation", "first-pass"));
 | 
						|
 | 
						|
        await store.InsertAsync(new[] { conflict }, CancellationToken.None);
 | 
						|
 | 
						|
        var results = await store.GetConflictsAsync(vulnerabilityKey, null, CancellationToken.None);
 | 
						|
 | 
						|
        Assert.Single(results);
 | 
						|
        Assert.Equal(conflict.Id, results[0].Id);
 | 
						|
        Assert.Equal(statementIds, results[0].StatementIds);
 | 
						|
    }
 | 
						|
 | 
						|
    [Fact]
 | 
						|
    public async Task GetConflicts_AsOfFilters()
 | 
						|
    {
 | 
						|
        var store = new AdvisoryConflictStore(_database);
 | 
						|
        var vulnerabilityKey = $"CVE-{Guid.NewGuid():N}";
 | 
						|
        var baseTime = DateTimeOffset.UtcNow;
 | 
						|
 | 
						|
        var earlyConflict = new AdvisoryConflictRecord(
 | 
						|
            Guid.NewGuid(),
 | 
						|
            vulnerabilityKey,
 | 
						|
            new byte[] { 0x01 },
 | 
						|
            baseTime,
 | 
						|
            baseTime.AddSeconds(10),
 | 
						|
            new[] { Guid.NewGuid() },
 | 
						|
            new BsonDocument("stage", "early"));
 | 
						|
 | 
						|
        var lateConflict = new AdvisoryConflictRecord(
 | 
						|
            Guid.NewGuid(),
 | 
						|
            vulnerabilityKey,
 | 
						|
            new byte[] { 0x02 },
 | 
						|
            baseTime.AddMinutes(10),
 | 
						|
            baseTime.AddMinutes(10).AddSeconds(15),
 | 
						|
            new[] { Guid.NewGuid() },
 | 
						|
            new BsonDocument("stage", "late"));
 | 
						|
 | 
						|
        await store.InsertAsync(new[] { earlyConflict, lateConflict }, CancellationToken.None);
 | 
						|
 | 
						|
        var results = await store.GetConflictsAsync(vulnerabilityKey, baseTime.AddMinutes(1), CancellationToken.None);
 | 
						|
 | 
						|
        Assert.Single(results);
 | 
						|
        Assert.Equal("early", results[0].Details["stage"].AsString);
 | 
						|
    }
 | 
						|
}
 |