Example #1
0
        public ParseFileTestFixture()
        {
            UserId = NewId.NextGuid();

            var builder = new ContainerBuilder();

            builder.RegisterConsumers(Assembly.Load("Sds.SpectraFileParser.Processing"));

            builder.RegisterType <InMemoryStorage>()
            .As <IBlobStorage>()
            .SingleInstance();

            var settings = ConfigurationManager.AppSettings.TestHarnessSettings();

            _harness             = new InMemoryTestHarness();
            _harness.TestTimeout = TimeSpan.FromSeconds(settings.Timeout);

            _harness.Handler <RecordParsed>(context => { return(Task.CompletedTask); });
            _harness.Handler <FileParsed>(context => { return(Task.CompletedTask); });

            var container = builder.Build();

            _harness.OnConfigureBus += cfg =>
            {
                cfg.ReceiveEndpoint("test", e =>
                {
                    e.LoadFrom(container);
                });
            };

            BlobStorage = container.Resolve <IBlobStorage>();
        }
        public TestHarness()
        {
            var builder = new ConfigurationBuilder()
                          .AddJsonFile("appsettings.bddtests.json", true, true);

            var configuration = builder.Build();

            var services = new ServiceCollection();

            UserId = NewId.NextGuid();

            _harness = new InMemoryTestHarness();

            services.AddOptions();

            services.AddSingleton <IEventStore, InMemoryEventStore>();
            services.AddScoped <IEventPublisher, MassTransitEventPublisher>();
            services.AddScoped <ISession, Session>();
            services.AddScoped <IRepository, Repository>();

            services.AddScoped <CreateRecordConsumer>();
            services.AddScoped <RecordProcessedConsumer>();

            services.AddSingleton <IConsumerScopeProvider, DependencyInjectionConsumerScopeProvider>();

            services.AddSingleton <TestStateMachine>();

            services.AddSingleton <ISagaRepository <TestState> >(new InMemorySagaRepository <TestState>());

            services.AddSingleton((ctx) =>
            {
                return(_harness.Bus as IBusControl);
            });

            _harness.OnConfigureBus += cfg =>
            {
                cfg.UseInMemoryOutbox();

                cfg.ReceiveEndpoint($"receiver_saga_queue", e =>
                {
                    e.StateMachineSaga(_serviceProvider.GetRequiredService <TestStateMachine>(), _serviceProvider.GetRequiredService <ISagaRepository <TestState> >());
                });

                cfg.ReceiveEndpoint($"receiver_queue", e =>
                {
                    e.ScopedConsumer <CreateRecordConsumer>(_serviceProvider, c => c.UseCqrsLite());
                    e.Consumer(_serviceProvider.GetRequiredService <RecordProcessedConsumer>);
                });
            };

            _harness.Handler <RecordProcessed>(async context =>
            {
                await Task.CompletedTask;
            });

            _serviceProvider = services.BuildServiceProvider();

            _harness.Start().Wait();
        }
Example #3
0
        public ParseFileTestFixture()
        {
            UserId = Guid.NewGuid();

            var settings = ConfigurationManager.AppSettings.TestHarnessSettings();

            _harness             = new InMemoryTestHarness();
            _harness.TestTimeout = TimeSpan.FromSeconds(settings.Timeout);

            _harness.Handler <RecordParsed>(context => { return(Task.CompletedTask); });
            _harness.Handler <RecordParseFailed>(context => { return(Task.CompletedTask); });
            _harness.Handler <FileParsed>(context => { return(Task.CompletedTask); });
            _harness.Handler <FileParseFailed>(context => { return(Task.CompletedTask); });

            _harness.OnConfigureBus += cfg =>
            {
                cfg.ReceiveEndpoint("test", e =>
                {
                    e.Consumer(() => new ParseFileCommandHandler(BlobStorage));
                });
            };

            _harness.Start().Wait();
        }
Example #4
0
        public OsdrTestHarness()
        {
            var builder = new ConfigurationBuilder()
                          .AddJsonFile("appsettings.bddtests.json", true, true);

            var configuration = builder.Build();

            var services = new ServiceCollection();

            JohnId = NewId.NextGuid();
            JaneId = NewId.NextGuid();

            var testHarnessSettings = configuration.GetSection("TestHarness").Get <TestHarnessSettings>();

            _harness             = new InMemoryTestHarness();
            _harness.TestTimeout = TimeSpan.FromSeconds(testHarnessSettings.Timeout);

            services.AddSingleton <IBlobStorage, InMemoryStorage>();

            services.AddOptions();

            services.AddSingleton <EventStore.InMemoryEventStore>();
            services.AddSingleton <IEventStore>(c => c.GetService <EventStore.InMemoryEventStore>());
            services.AddSingleton <EventStore.IEventStore>(c => c.GetService <EventStore.InMemoryEventStore>());
            services.AddScoped <IEventPublisher, CqrsLite.MassTransit.MassTransitEventPublisher>();
            services.AddScoped <ISession, Session>();
            services.AddScoped <IRepository, Repository>();
            services.AddScoped <IKeyValueRepository, InMemoryKeyValueRepository>();

            services.AddSingleton <IConsumerScopeProvider, DependencyInjectionConsumerScopeProvider>();

            services.Configure <Infrastructure.AccessControl.AccessControl>(configuration.GetSection("AccessControl"));
            services.AddSingleton <IAccessControl, Infrastructure.AccessControl.AppSettingsAccessControl>();

            services.AddSingleton(new MongoClient(Environment.ExpandEnvironmentVariables("%OSDR_MONGO_DB%")));
            services.AddScoped(service => service.GetService <MongoClient>().GetDatabase(MongoDatabaseName));

            var allAssembly = new Assembly[]
            {
                Assembly.LoadFrom("Sds.Osdr.Domain.BddTests.dll"),
                //Assembly.LoadFrom("Sds.Osdr.Domain.BackEnd.dll")
            };

            services.AddAllConsumers(allAssembly);

            services.AddScoped <Domain.BackEnd.EventHandlers.MachineLearningEventHandlers>();
            services.AddScoped <Domain.BackEnd.EventHandlers.MicroServiceEventHandlers>();

            var moduleAssemblies = new Assembly[]
            {
                Assembly.LoadFrom("Sds.Osdr.Generic.dll"),
                Assembly.LoadFrom("Sds.Osdr.RecordsFile.dll"),
                Assembly.LoadFrom("Sds.Osdr.Chemicals.dll"),
                Assembly.LoadFrom("Sds.Osdr.Crystals.dll"),
                Assembly.LoadFrom("Sds.Osdr.Reactions.dll"),
                Assembly.LoadFrom("Sds.Osdr.Spectra.dll"),
                Assembly.LoadFrom("Sds.Osdr.Pdf.dll"),
                Assembly.LoadFrom("Sds.Osdr.Images.dll"),
                Assembly.LoadFrom("Sds.Osdr.Office.dll"),
                Assembly.LoadFrom("Sds.Osdr.Tabular.dll"),
                Assembly.LoadFrom("Sds.Osdr.MachineLearning.dll"),
                Assembly.LoadFrom("Sds.Osdr.WebPage.dll"),
            };

            services.UseInMemoryOsdrModules(moduleAssemblies);

            services.AddSingleton((ctx) =>
            {
                return(_harness.Bus as IBusControl);
            });

            _harness.OnConfigureBus += cfg =>
            {
                cfg.UseInMemoryOutbox();

                cfg.RegisterScopedConsumer <Domain.BackEnd.EventHandlers.MachineLearningEventHandlers>(_serviceProvider, null, c => c.UseCqrsLite());
                cfg.RegisterScopedConsumer <Domain.BackEnd.EventHandlers.MicroServiceEventHandlers>(_serviceProvider, null, c => c.UseCqrsLite());

                cfg.RegisterInMemoryOsdrModules(_serviceProvider, moduleAssemblies);

                cfg.RegisterConsumers(_serviceProvider, allAssembly);

                cfg.UseRetry(r =>
                {
                    r.Interval(100, TimeSpan.FromMilliseconds(10));
                    r.Handle <MongoWriteException>();
                    r.Handle <UnhandledEventException>();
                    r.Handle <ConcurrencyException>();
                });
            };

            _harness.Handler <RecordsFile.Sagas.Events.RecordProcessed>(async context =>
            {
                lock (ProcessedRecords)
                {
                    var recordId = context.Message.Id;
                    var fileId   = context.Message.FileId;

                    if (!ProcessedRecords.ContainsKey(fileId))
                    {
                        ProcessedRecords[fileId] = new List <Guid>();
                    }

                    ProcessedRecords[fileId].Add(recordId);
                }

                await Task.CompletedTask;
            });

            _harness.Handler <Generic.Sagas.Events.FileProcessed>(async context =>
            {
                lock (ProcessedRecords)
                {
                    var recordId = context.Message.Id;
                    var parentId = context.Message.ParentId;

                    if (!DependentFiles.ContainsKey(parentId))
                    {
                        DependentFiles[parentId] = new List <Guid>();
                    }

                    DependentFiles[parentId].Add(recordId);
                }

                await Task.CompletedTask;
            });

            _harness.Handler <ModelTrainingFinished>(async context =>
            {
                lock (ProcessedRecords)
                {
                    var recordId = context.Message.Id;
                    var parentId = context.Message.ParentId;

                    if (!DependentFiles.ContainsKey(parentId))
                    {
                        DependentFiles[parentId] = new List <Guid>();
                    }

                    DependentFiles[parentId].Add(recordId);
                }

                await Task.CompletedTask;
            });

            _harness.Handler <RecordsFile.Sagas.Events.InvalidRecordProcessed>(async context =>
            {
                lock (InvalidRecords)
                {
                    var recordId = context.Message.Id;
                    var fileId   = context.Message.FileId;

                    if (!InvalidRecords.ContainsKey(fileId))
                    {
                        InvalidRecords[fileId] = new List <Guid>();
                    }

                    InvalidRecords[fileId].Add(recordId);
                }

                await Task.CompletedTask;
            });

            _harness.Handler <RecordsFile.Domain.Events.Records.StatusPersisted>(async context =>
            {
                if (context.Message.Status == RecordsFile.Domain.RecordStatus.Processed)
                {
                    lock (PersistedRecords)
                    {
                        if (!PersistedRecords.ContainsKey(context.Message.Id))
                        {
                            PersistedRecords[context.Message.Id] = 1;
                        }
                        else
                        {
                            PersistedRecords[context.Message.Id] = PersistedRecords[context.Message.Id] + 1;
                        }
                    }
                }

                await Task.CompletedTask;
            });

            _harness.Handler <Fault>(async context =>
            {
                Faults.AddRange(context.Message.Exceptions.Where(ex => !ex.ExceptionType.Equals("System.InvalidOperationException")));

                await Task.CompletedTask;
            });

            _serviceProvider = services.BuildServiceProvider();

            _harness.Start().Wait();

            Seed(JohnId, "John Doe", "John", "Doe", "john", "*****@*****.**", null).Wait();
            Seed(JaneId, "Jane Doe", "Jane", "Doe", "jane", "*****@*****.**", null).Wait();

            //  Specify how to compare DateTimes inside FluentAssertions
            AssertionOptions.AssertEquivalencyUsing(options =>
                                                    options
                                                    .Using <DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, (int)_harness.TestTimeout.TotalMilliseconds)).WhenTypeIs <DateTime>()
                                                    .Using <DateTimeOffset>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, (int)_harness.TestTimeout.TotalMilliseconds)).WhenTypeIs <DateTimeOffset>()
                                                    );
        }