diff --git a/Btms.Analytics.Tests/MovementsByMaxVersionTests.cs b/Btms.Analytics.Tests/MovementsByMaxVersionTests.cs index 2f387ae1..94807adc 100644 --- a/Btms.Analytics.Tests/MovementsByMaxVersionTests.cs +++ b/Btms.Analytics.Tests/MovementsByMaxVersionTests.cs @@ -9,10 +9,6 @@ namespace Btms.Analytics.Tests; -// [Collection(nameof(BasicSampleDataTestCollection))] -// public class MovementsByMaxVersionTests( -// BasicSampleDataTestFixture basicSampleDataTestFixture, -// ITestOutputHelper testOutputHelper) public class MovementsByMaxVersionTests(ITestOutputHelper output) : ScenarioGeneratorBaseTest(output) { diff --git a/Btms.Backend.IntegrationTests/DecisionTests/ChedPUpdatedNotificationTests.cs b/Btms.Backend.IntegrationTests/DecisionTests/ChedPUpdatedNotificationTests.cs index 94b9e726..7836251a 100644 --- a/Btms.Backend.IntegrationTests/DecisionTests/ChedPUpdatedNotificationTests.cs +++ b/Btms.Backend.IntegrationTests/DecisionTests/ChedPUpdatedNotificationTests.cs @@ -3,15 +3,20 @@ using Btms.Types.Alvs; using Btms.Types.Ipaffs; using FluentAssertions; +using TestDataGenerator.Scenarios; using TestDataGenerator.Scenarios.ChedP; +using TestGenerator.IntegrationTesting.Backend; using Xunit; using Xunit.Abstractions; namespace Btms.Backend.IntegrationTests.DecisionTests; [Trait("Category", "Integration")] -public class ChedPUpdatedNotificationTests(InMemoryScenarioApplicationFactory factory, ITestOutputHelper testOutputHelper) - : BaseApiTests(factory, testOutputHelper, "DecisionTests"), IClassFixture> +// public class ChedPUpdatedNotificationTests(InMemoryScenarioApplicationFactory factory, ITestOutputHelper testOutputHelper) +// : BaseApiTests(factory, testOutputHelper, "DecisionTests"), IClassFixture> + +public class ChedPUpdatedNotificationTests(ITestOutputHelper output) + : ScenarioGeneratorBaseTest(output) { [Fact(Skip = "We're not currently re-linking when a new version of notification arrives it seems")] @@ -19,16 +24,16 @@ public class ChedPUpdatedNotificationTests(InMemoryScenarioApplicationFactory - d is { generator: MultiStepScenarioGenerator, message: AlvsClearanceRequest }) - .message; + d is { Message: AlvsClearanceRequest }) + .Message; // The scenario has multiple versions of the same notification so we just want one of them. - var chedPNotification = (ImportNotification)loadedData.FirstOrDefault(d => - d is { generator: MultiStepScenarioGenerator, message: ImportNotification }) - .message; + var chedPNotification = (ImportNotification)LoadedData.Single(d => + d is { Message: ImportNotification }) + .Message; // Act var jsonClientResponse = Client.AsJsonApiClient().GetById(chedPMovement!.Header!.EntryReference!, "api/movements"); diff --git a/Btms.Backend.IntegrationTests/DecisionTests/NoMatchManyItemTests.cs b/Btms.Backend.IntegrationTests/DecisionTests/NoMatchManyItemTests.cs index d088cc3d..e61d186c 100644 --- a/Btms.Backend.IntegrationTests/DecisionTests/NoMatchManyItemTests.cs +++ b/Btms.Backend.IntegrationTests/DecisionTests/NoMatchManyItemTests.cs @@ -2,14 +2,15 @@ using Btms.Model; using FluentAssertions; using TestDataGenerator.Scenarios; +using TestGenerator.IntegrationTesting.Backend; using Xunit; using Xunit.Abstractions; namespace Btms.Backend.IntegrationTests.DecisionTests; [Trait("Category", "Integration")] -public class ManyItemTests(InMemoryScenarioApplicationFactory factory, ITestOutputHelper testOutputHelper) - : BaseApiTests(factory, testOutputHelper, "DecisionTests"), IClassFixture> +public class ManyItemTests(ITestOutputHelper output) + : ScenarioGeneratorBaseTest(output) { [Fact] diff --git a/Btms.Backend.IntegrationTests/Helpers/InMemoryScenarioApplicationFactory.cs b/Btms.Backend.IntegrationTests/Helpers/InMemoryScenarioApplicationFactory.cs deleted file mode 100644 index f43d77d8..00000000 --- a/Btms.Backend.IntegrationTests/Helpers/InMemoryScenarioApplicationFactory.cs +++ /dev/null @@ -1,186 +0,0 @@ -using Btms.Analytics; -using Btms.Backend.Data; -using Btms.Backend.Data.Mongo; -using Btms.BlobService; -using Btms.Business.Commands; -using Btms.Common.Extensions; -using Btms.Consumers.Extensions; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.Testing; -using Microsoft.AspNetCore.TestHost; -using Microsoft.EntityFrameworkCore.Diagnostics.Internal; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; -using Microsoft.Extensions.Options; -using MongoDB.Bson.Serialization.Conventions; -using MongoDB.Driver; -using Serilog.Extensions.Logging; -using TestDataGenerator; -using TestDataGenerator.Config; -using TestDataGenerator.Extensions; -using TestDataGenerator.Scenarios; -using TestGenerator.IntegrationTesting.Backend.Fixtures; -using Xunit.Abstractions; - -namespace Btms.Backend.IntegrationTests.Helpers; - - -public class InMemoryApplicationFactory(string databaseName, ITestOutputHelper testOutputHelper ) : WebApplicationFactory -{ - private IMongoDbContext? mongoDbContext; - - protected override void ConfigureWebHost(IWebHostBuilder builder) - { - // Any integration test overrides could be added here - // And we don't want to load the backend ini file - var configurationValues = new Dictionary - { - { "DisableLoadIniFile", "true" }, - { "BlobServiceOptions:CachePath", "Scenarios/Samples" }, - { "BlobServiceOptions:CacheReadEnabled", "true" }, - { "AuthKeyStore:Credentials:IntTest", "Password" } - }; - - var configuration = new ConfigurationBuilder() - .AddInMemoryCollection(configurationValues!) - .Build(); - - builder - .UseConfiguration(configuration) - .ConfigureServices(services => - { - var mongoDatabaseDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(IMongoDatabase))!; - services.Remove(mongoDatabaseDescriptor); - - var blobOptionsValidatorDescriptor = services.SingleOrDefault(d => d.ServiceType == typeof(IValidateOptions))!; - services.Remove(blobOptionsValidatorDescriptor); - - services.AddSingleton(sp => - { - var options = sp.GetService>()!; - var settings = MongoClientSettings.FromConnectionString(options.Value.DatabaseUri); - var client = new MongoClient(settings); - - // _mongoDbContext = client - var camelCaseConvention = new ConventionPack { new CamelCaseElementNameConvention() }; - // convention must be registered before initialising collection - ConventionRegistry.Register("CamelCase", camelCaseConvention, _ => true); - - var dbName = string.IsNullOrEmpty(databaseName) ? Random.Shared.Next().ToString() : databaseName; - var db = client.GetDatabase($"Btms_{dbName}"); - - // TODO : Use our ILoggerFactory - mongoDbContext = new MongoDbContext(db, new SerilogLoggerFactory()); - return db; - }); - - if (testOutputHelper.HasValue()) - { - services.AddLogging(lb => lb.AddXUnit(testOutputHelper)); - } - }); - - builder.UseEnvironment("Development"); - } - - public (IMongoDbContext, BtmsClient) Start() - { - var builder = Host.CreateDefaultBuilder(); - - var host = base - .CreateHost(builder); - - // Creating the client causes the - var client = this.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); - var btmsClient = new BtmsClient(client); - - return (mongoDbContext!, btmsClient); - } - -} - -public class InMemoryScenarioApplicationFactory - : IIntegrationTestsApplicationFactory - where T : ScenarioGenerator -{ - private readonly IMongoDbContext _mongoDbContext; - private readonly BtmsClient _btmsClient; - - public List<( - ScenarioGenerator generator, int scenario, int dateOffset, int count, object message - )> LoadedData; - - private IHost TestGeneratorApp { get; set; } - private InMemoryApplicationFactory WebApp { get; set; } - - public InMemoryScenarioApplicationFactory() - { - // Generate test data - - var generatorBuilder = new HostBuilder(); - generatorBuilder.ConfigureTestDataGenerator("Scenarios/Samples"); - - TestGeneratorApp = generatorBuilder.Build(); - - var dbName = string.IsNullOrEmpty(DatabaseName) ? typeof(T).Name : DatabaseName; - WebApp = new InMemoryApplicationFactory(dbName, TestOutputHelper); - (_mongoDbContext, _btmsClient) = WebApp.Start(); - - LoadedData = GenerateAndLoadTestData().GetAwaiter().GetResult();; - } - - public ITestOutputHelper TestOutputHelper { get; set; } = null!; - - public string DatabaseName { get; set; } = null!; - - public IMongoDbContext GetDbContext() - { - return _mongoDbContext; - } - - public BtmsClient CreateBtmsClient(WebApplicationFactoryClientOptions options) - { - return _btmsClient; - } - - private async Task> GenerateAndLoadTestData() - { - - // TODO : Naive caching implementation, improve - // if (LoadedData.HasValue()) - // { - // return LoadedData; - // } - - await _btmsClient.ClearDb(); - - LoadedData = new List<(ScenarioGenerator generator, int scenario, int dateOffset, int count, object message)>(); - - // TODO: Need a logger - var logger = NullLogger.Instance; - - var scenarioConfig = - TestGeneratorApp.Services.CreateScenarioConfig(1, 1, arrivalDateRange: 0); - - var generatorResults = scenarioConfig.Generate(logger, 0); - foreach (var generatorResult in generatorResults) - { - await WebApp.Services.PushToConsumers(logger, generatorResult); - var output = generatorResult - // ScenarioGenerator generator, int scenario, int dateOffset, int count, object message - .Select(r => (scenarioConfig.Generator, 0, 1, 1, r)) - .ToList(); - - LoadedData.AddRange(output); - } - - - return LoadedData; - } -} \ No newline at end of file diff --git a/Btms.Backend.IntegrationTests/MatchingTests/ChedNoMatchTests.cs b/Btms.Backend.IntegrationTests/MatchingTests/ChedNoMatchTests.cs index 05a82869..868be133 100644 --- a/Btms.Backend.IntegrationTests/MatchingTests/ChedNoMatchTests.cs +++ b/Btms.Backend.IntegrationTests/MatchingTests/ChedNoMatchTests.cs @@ -2,31 +2,29 @@ using Btms.Types.Ipaffs; using FluentAssertions; using TestDataGenerator.Scenarios.SpecificFiles; +using TestGenerator.IntegrationTesting.Backend; using Xunit; using Xunit.Abstractions; namespace Btms.Backend.IntegrationTests.MatchingTests; [Trait("Category", "Integration")] -public class UnmatchedChedTests( - InMemoryScenarioApplicationFactory factory, - ITestOutputHelper testOutputHelper) - : BaseApiTests(factory, testOutputHelper, "MatchingTests"), - IClassFixture> +public class UnmatchedChedTests(ITestOutputHelper output) + : ScenarioGeneratorBaseTest(output) { [Fact] public void ChedsWithNoCR_ShouldNotMatch() { // Arrange - var loadedData = factory.LoadedData; + var loadedData = LoadedData; var chedA = (ImportNotification)loadedData.Single(d => - d.message is ImportNotification { ImportNotificationType: ImportNotificationTypeEnum.Cveda }).message; + d.Message is ImportNotification { ImportNotificationType: ImportNotificationTypeEnum.Cveda }).Message; var chedD = (ImportNotification)loadedData.Single(d => - d.message is ImportNotification { ImportNotificationType: ImportNotificationTypeEnum.Ced }).message; + d.Message is ImportNotification { ImportNotificationType: ImportNotificationTypeEnum.Ced }).Message; var chedP = (ImportNotification)loadedData.Single(d => - d.message is ImportNotification { ImportNotificationType: ImportNotificationTypeEnum.Cvedp }).message; + d.Message is ImportNotification { ImportNotificationType: ImportNotificationTypeEnum.Cvedp }).Message; var chedPP = (ImportNotification)loadedData.Single(d => - d.message is ImportNotification { ImportNotificationType: ImportNotificationTypeEnum.Chedpp }).message; + d.Message is ImportNotification { ImportNotificationType: ImportNotificationTypeEnum.Chedpp }).Message; // // Act diff --git a/Btms.Backend.IntegrationTests/PreprocessingTests/EnsureAuditEntryIsAddedForMovementUpdatesTests.cs b/Btms.Backend.IntegrationTests/PreprocessingTests/EnsureAuditEntryIsAddedForMovementUpdatesTests.cs index 210e2e4b..f428095c 100644 --- a/Btms.Backend.IntegrationTests/PreprocessingTests/EnsureAuditEntryIsAddedForMovementUpdatesTests.cs +++ b/Btms.Backend.IntegrationTests/PreprocessingTests/EnsureAuditEntryIsAddedForMovementUpdatesTests.cs @@ -4,16 +4,16 @@ using FluentAssertions; using TestDataGenerator.Scenarios.ChedP; using TestDataGenerator.Scenarios.SpecificFiles; +using TestGenerator.IntegrationTesting.Backend; using Xunit; using Xunit.Abstractions; namespace Btms.Backend.IntegrationTests.PreprocessingTests; [Trait("Category", "Integration")] -public class EnsureAuditEntryIsAddedForMovementUpdatesTests(InMemoryScenarioApplicationFactory factory, ITestOutputHelper testOutputHelper) - : BaseApiTests(factory, testOutputHelper, "DecisionTests"), IClassFixture> +public class EnsureAuditEntryIsAddedForMovementUpdatesTests(ITestOutputHelper output) + : ScenarioGeneratorBaseTest(output) { - [Fact] public void ShouldHaveUpdatedAuditEntry() { diff --git a/Btms.Backend.IntegrationTests/PreprocessingTests/EnsureDuplicateItemsAreNotCreatedTests.cs b/Btms.Backend.IntegrationTests/PreprocessingTests/EnsureDuplicateItemsAreNotCreatedTests.cs index c6afead1..d916308f 100644 --- a/Btms.Backend.IntegrationTests/PreprocessingTests/EnsureDuplicateItemsAreNotCreatedTests.cs +++ b/Btms.Backend.IntegrationTests/PreprocessingTests/EnsureDuplicateItemsAreNotCreatedTests.cs @@ -3,14 +3,18 @@ using Btms.Types.Alvs; using FluentAssertions; using TestDataGenerator.Scenarios.SpecificFiles; +using TestGenerator.IntegrationTesting.Backend; using Xunit; using Xunit.Abstractions; namespace Btms.Backend.IntegrationTests.PreprocessingTests; [Trait("Category", "Integration")] -public class EnsureDuplicateItemsAreNotCreatedTests(InMemoryScenarioApplicationFactory factory, ITestOutputHelper testOutputHelper) - : BaseApiTests(factory, testOutputHelper, "DecisionTests"), IClassFixture> +public class EnsureDuplicateItemsAreNotCreatedTests(ITestOutputHelper output) + : ScenarioGeneratorBaseTest(output) + +// public class EnsureDuplicateItemsAreNotCreatedTests(InMemoryScenarioApplicationFactory factory, ITestOutputHelper testOutputHelper) +// : BaseApiTests(factory, testOutputHelper, "DecisionTests"), IClassFixture> { [Fact(Skip = "We're ending up with 2 items on the clearance request here.")] @@ -18,11 +22,11 @@ public class EnsureDuplicateItemsAreNotCreatedTests(InMemoryScenarioApplicationF public void ShouldNotCreateDuplicateItems() { // Arrange - var loadedData = factory.LoadedData; + var loadedData = LoadedData; var movementMessage = (AlvsClearanceRequest)loadedData.First(d => - d is { message: AlvsClearanceRequest }) - .message; + d is { Message: AlvsClearanceRequest }) + .Message; // Act diff --git a/TestDataGenerator/Scenarios/CRNoMatch.cs b/TestDataGenerator/Scenarios/CRNoMatch.cs index 61e9df18..72fdabab 100644 --- a/TestDataGenerator/Scenarios/CRNoMatch.cs +++ b/TestDataGenerator/Scenarios/CRNoMatch.cs @@ -10,7 +10,6 @@ namespace TestDataGenerator.Scenarios; public static class NoMatchExtensions { - public static AlvsClearanceRequest CompleteSimpleClearanceRequest(int scenario, int item, DateTime entryDate, ScenarioConfig config) { return NoMatchExtensions @@ -141,7 +140,6 @@ public override GeneratorResult Generate(int scenario, int item, DateTime entryD } } - public class CrNonContiguousDecisionsScenarioGenerator(ILogger logger) : ScenarioGenerator { public override GeneratorResult Generate(int scenario, int item, DateTime entryDate, ScenarioConfig config) @@ -169,7 +167,6 @@ public override GeneratorResult Generate(int scenario, int item, DateTime entryD } } - public class CrDecisionWithoutV1ScenarioGenerator(ILogger logger) : ScenarioGenerator { public override GeneratorResult Generate(int scenario, int item, DateTime entryDate, ScenarioConfig config) diff --git a/TestGenerator.IntegrationTesting.Backend/Fixtures/BackendFixture.cs b/TestGenerator.IntegrationTesting.Backend/Fixtures/BackendFixture.cs index cc2c2c37..8c8b05cd 100644 --- a/TestGenerator.IntegrationTesting.Backend/Fixtures/BackendFixture.cs +++ b/TestGenerator.IntegrationTesting.Backend/Fixtures/BackendFixture.cs @@ -64,7 +64,9 @@ protected override void ConfigureWebHost(IWebHostBuilder builder) { "DisableLoadIniFile", "true" }, { "BlobServiceOptions:CachePath", "Scenarios/Samples" }, { "BlobServiceOptions:CacheReadEnabled", "true" }, - { "AuthKeyStore:Credentials:IntTest", "Password" } + { "AuthKeyStore:Credentials:IntTest", "Password" }, + + { "ConsumerOptions:EnableBlockingPublish", "true" } }; var configuration = new ConfigurationBuilder()