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();
        }
Пример #2
0
        public FileTests(IndexingFixture indexingFixture)
        {
            _fileId     = Guid.NewGuid();
            index       = string.Empty;
            type        = string.Empty;
            doc         = null;
            docAsUpsert = null;
            string _someField = Guid.NewGuid().ToString();
            string _someValue = Guid.NewGuid().ToString();

            _fakeIndex = new List <IIndexRequest <dynamic> >();
            _mongoDb   = indexingFixture.MongoDb;
            var files = _mongoDb.GetCollection <BsonDocument>("Files");

            files.InsertOne(
                new BsonDocument("_id", _fileId)
                .Add("Name", "Processed.mol")
                .Add("Status", "Processed")
                .Add(_someField, _someValue));

            var list = new List <BsonDocument>();

            for (int i = 0; i < 10; i++)
            {
                list.Add(new BsonDocument("_id", Guid.NewGuid()).Add("Name", i.ToString()));
            }

            files.InsertMany(list);
            _file = _mongoDb.GetCollection <dynamic>("Files").Find(new BsonDocument("_id", _fileId)).First();
            ((IDictionary <string, object>)_file).Remove("_id");
            ((IDictionary <string, object>)_file).Add("id", _fileId);

            _elasticClientMock = new Mock <IElasticClient>();
            _elasticClientMock
            .Setup(m => m.IndexAsync <object>(It.IsAny <IIndexRequest>(), null, default(CancellationToken)))
            .Returns(Task.FromResult(new Mock <IIndexResponse>().Object))
            .Callback <IIndexRequest <object>, Func <IndexDescriptor <object>, IIndexRequest>, CancellationToken>((a, s, c) => {
                _fakeIndex.Add(a);
                index = a.Index.Name;
                type  = a.Type.Name;
            }).Verifiable();

            _elasticClientMock.Setup(m => m.UpdateAsync <object, object>(It.IsAny <IUpdateRequest <object, object> >(), default(CancellationToken)))
            .Returns(Task.FromResult(new Mock <IUpdateResponse <object> >().Object))
            .Callback <IUpdateRequest <object, object>, CancellationToken>((a, c) =>
            {
                index       = a.Index.Name;
                type        = a.Type.Name;
                docAsUpsert = a.DocAsUpsert;
                doc         = a.Doc as IDictionary <string, object>;
            }).Verifiable();

            _harness  = new InMemoryTestHarness();
            _consumer = _harness.Consumer(() => new FileEventHandler(_elasticClientMock.Object, _mongoDb, _blobStorage));

            _harness.Start().Wait();
        }
Пример #3
0
        public async Task CamelCasedJsonIsPreservedAcrossMassTransitBoundary()
        {
            Harness = new InMemoryTestHarness();

            var messagePackOptions = new MessagePackHubProtocolOptions();

            var jsonOptions = new JsonHubProtocolOptions();

            jsonOptions.PayloadSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            var backplane1Harness = RegisterBusEndpoint("receiveEndpoint1");
            var backplane2Harness = RegisterBusEndpoint("receiveEndpoint2");

            await Harness.Start();

            try
            {
                backplane1Harness.SetHubLifetimeManager(CreateLifetimeManager(messagePackOptions, jsonOptions));
                backplane2Harness.SetHubLifetimeManager(CreateLifetimeManager());
                using (var client1 = new TestClient())
                    using (var client2 = new TestClient())
                    {
                        var manager1 = backplane1Harness.HubLifetimeManager;
                        var manager2 = backplane2Harness.HubLifetimeManager;

                        var connection1 = HubConnectionContextUtils.Create(client1.Connection);
                        var connection2 = HubConnectionContextUtils.Create(client2.Connection);

                        await manager1.OnConnectedAsync(connection1).OrTimeout();

                        await manager2.OnConnectedAsync(connection2).OrTimeout();

                        await manager1.SendAllAsync("Hello", new object[] { new TestObject {
                                                                                TestProperty = "Foo"
                                                                            } });

                        Assert.IsTrue(backplane2Harness.All.Consumed.Select <All <MyHub> >().Any());

                        var message = await client2.ReadAsync().OrTimeout() as InvocationMessage;

                        Assert.NotNull(message);
                        Assert.AreEqual("Hello", message.Target);
                        CollectionAssert.AllItemsAreInstancesOfType(message.Arguments, typeof(JObject));
                        var jObject = message.Arguments[0] as JObject;
                        Assert.NotNull(jObject);
                        var firstProperty = jObject.Properties().First();
                        Assert.AreEqual("testProperty", firstProperty.Name);
                        Assert.AreEqual("Foo", firstProperty.Value.Value <string>());
                    }
            }
            finally
            {
                await Harness.Stop();
            }
        }
Пример #4
0
        public async Task Send_command_to_parse_valid_cif_should_publish_one_RecordParsed_and_one_FileParsed_event([Frozen] FileParsedEvent expectedEvent)
        {
            try
            {
                await _harness.Start();

                var blobId = await _blobStorage.AddFileAsync("1100110.cif", Resource._1100110, "chemical/x-cif", BUCKET);

                await _harness.InputQueueSendEndpoint.Send <ParseFile>(new
                {
                    expectedEvent.Id,
                    Bucket = BUCKET,
                    BlobId = blobId,
                    expectedEvent.CorrelationId,
                    expectedEvent.UserId
                });

                _consumer.Consumed.Select <ParseFile>().Any();
                _harness.Published.Select <FileParsed>().Any();

                var allEvents = _harness.Published.ToList();

                var parsed = allEvents.Select <RecordParsed>().FirstOrDefault();
                parsed.Should().NotBeNull();
                parsed.ShouldBeEquivalentTo(new
                {
                    FileId = expectedEvent.Id,
                    Bucket = BUCKET,
                    Index  = 0L,
                    expectedEvent.UserId
                },
                                            options => options.ExcludingMissingMembers()
                                            );
                parsed.Fields.Count().Should().Be(22);
            }
            finally
            {
                await _harness.Stop();
            }
        }
Пример #5
0
        public IndexingFixture()
        {
            _harness             = new InMemoryTestHarness();
            _harness.TestTimeout = TimeSpan.FromSeconds(30);

            FakeIndex = new List <IIndexRequest <dynamic> >();

            ElasticClientMock = new Mock <IElasticClient>();
            ElasticClientMock
            .Setup(m => m.IndexAsync <object>(It.IsAny <IIndexRequest>(), null, default(CancellationToken)))
            .Returns(Task.FromResult(new Mock <IIndexResponse>().Object))
            .Callback <IIndexRequest <object>, Func <IndexDescriptor <object>, IIndexRequest>, CancellationToken>((a, s, c) => {
                FakeIndex.Add(a);
            });

            //ElasticClientMock
            //    .Setup(m => m.UpdateAsync<object, object>(It.Is<IUpdateRequest<object, object>>(r =>
            //        r.DocAsUpsert == true && r.Index.Name == "files" && r.Type.Name == "file"), default(CancellationToken)))
            //    .Returns(Task.FromResult(new Mock<IUpdateResponse<object>>().Object))
            //    .Callback<IIndexRequest<object>, Func<IndexDescriptor<object>, IIndexRequest>, CancellationToken>((a, s, c) =>
            //        FakeIndex.Add(a));

            var services = new ServiceCollection();

            UserId = Guid.NewGuid();
            services.AddTransient(x => ElasticClientMock.Object);

            var allAssemblies = new Assembly[]
            {
                Assembly.Load(new AssemblyName("Sds.Indexing"))
            };

            services.AddAllConsumers(allAssemblies);

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

            _harness.OnConfigureBus += cfg =>
            {
                cfg.RegisterConsumers(_serviceProvider, allAssemblies);
            };

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

            _serviceProvider = services.BuildServiceProvider();

            _harness.Start().Wait();
        }
Пример #6
0
        public ImagingTestFixture()
        {
            _harness             = new InMemoryTestHarness();
            _harness.TestTimeout = TimeSpan.FromSeconds(30);

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

            _harness.Start().Wait();
        }
Пример #7
0
        public ConverterFixture()
        {
            UserId = Guid.NewGuid();

            _harness             = new InMemoryTestHarness();
            _harness.TestTimeout = TimeSpan.FromSeconds(5);

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

            _harness.Start().Wait();
        }
Пример #8
0
        public ExportTestFixture()
        {
            UserId = Guid.NewGuid();

            _harness             = new InMemoryTestHarness();
            _harness.TestTimeout = TimeSpan.FromSeconds(5);

            provider = new BlobStorageRecordsProvider(BlobStorage);

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

            _harness.Start().Wait();
        }
Пример #9
0
        public async Task Send_command_to_parse_valid_rdf_should_publish_75_RecordParsed_one_FileParsed_event([Frozen] FileParsedEvent expectedEvent)
        {
            try
            {
                await _harness.Start();

                var blobId = await _blobStorage.AddFileAsync("ccr0401.rdf", Resource.ccr0401, "chemical/x-mdl-rdfile", BUCKET);

                await _harness.InputQueueSendEndpoint.Send <ParseFile>(new
                {
                    expectedEvent.Id,
                    Bucket = BUCKET,
                    BlobId = blobId,
                    expectedEvent.CorrelationId,
                    expectedEvent.UserId
                });

                var res = _consumer.Consumed.Select <ParseFile>().Any();
                res.Should().BeTrue();

                var allEvents = _harness.Published.ToList();

                allEvents.Where(e => e.MessageType == typeof(RecordParsed)).Count().Should().Be(75);

                var parsed = allEvents.Select <FileParsed>().FirstOrDefault();
                parsed.Should().NotBeNull();
                parsed.Should().BeEquivalentTo(expectedEvent,
                                               options => options
                                               .Excluding(p => p.TimeStamp)
                                               .Excluding(p => p.Fields)
                                               .Excluding(p => p.TotalRecords)
                                               );
                parsed.TotalRecords.Should().Be(75);
                parsed.Fields.Count().Should().Be(6);
            }
            finally
            {
                await _harness.Stop();
            }
        }
Пример #10
0
        public RecordTests(IndexingFixture fixture)
        {
            _fixture  = fixture;
            _recordId = Guid.NewGuid();
            _fixture.FakeIndex.Clear();

            string _someField = Guid.NewGuid().ToString();
            string _someValue = Guid.NewGuid().ToString();

            var records = _fixture.MongoDb.GetCollection <BsonDocument>("Records");


            records.InsertOne(
                new BsonDocument("_id", _recordId)
                .Add("Name", "TestName1")
                .Add("Status", "Processed")
                .Add(_someField, _someValue)
                );



            var list = new List <BsonDocument>();

            for (int i = 0; i < 10; i++)
            {
                list.Add(new BsonDocument("_id", Guid.NewGuid()).Add("name", i.ToString()));
            }

            records.InsertMany(list);
            _record = _fixture.MongoDb.GetCollection <dynamic>("Records").Find(new BsonDocument("_id", _recordId)).First();
            ((IDictionary <string, object>)_record).Remove("_id");
            ((IDictionary <string, object>)_record).Add("id", _recordId);



            _harness  = new InMemoryTestHarness();
            _consumer = _harness.Consumer(() => new RecordEventHandler(fixture.ElasticClientMock.Object, fixture.MongoDb));
            _harness.Start().Wait();
        }
Пример #11
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();
        }
Пример #12
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>()
                                                    );
        }
Пример #13
0
        async Task StartTestHarness()
        {
            using var source = new CancellationTokenSource(TimeSpan.FromSeconds(30));

            await Harness.Start(source.Token);
        }