Example #1
0
        private static IMongoDatabase WaitForMongoDB(ConnectionStringSettings connectionStringSettings, string databaseName)
        {
            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
            {
                while (!cts.IsCancellationRequested)
                {
                    try
                    {
                        var database = MongoDBHelper.Connect(connectionStringSettings, databaseName);
                        database.ListCollections();
                        return(database);
                    }
                    catch (Exception)
                    {
                    }

                    try
                    {
                        Task.Delay(TimeSpan.FromSeconds(1)).Wait(cts.Token);
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            throw new TimeoutException("MongoDB not available");
        }
        public AesEncryptedMongoDBFixture()
        {
            var rng = new Random();

            DatabaseName = $"AesEncryptedMongoDBFixture_{rng.Next(int.MaxValue):X}";

            // docker run -it --rm --name mongodb -p 27027:27017 mongo:3.6.0-jessie
            ConnectionStringSettings = new ConnectionStringSettings
            {
                Name             = DatabaseName,
                ConnectionString = $"mongodb://localhost:27027/{DatabaseName}?maxpoolsize=1000"
            };
#if NET452
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.ConnectionStrings.ConnectionStrings.Remove(ConnectionStringSettings.Name);
            config.ConnectionStrings.ConnectionStrings.Add(ConnectionStringSettings);
            config.Save();
            ConfigurationManager.RefreshSection("connectionStrings");
#endif
#if NETCOREAPP2_0
            ConfigurationManager.ConnectionStrings[ConnectionStringSettings.Name] = ConnectionStringSettings;
#endif

            Database = MongoDBHelper.Connect(ConnectionStringSettings, DatabaseName);

            var encryptionServiceOptions = new AesMessageEncryptionOptions(KeyGenerator.GenerateAesKey());
            var messageQueueingOptions   = new MongoDBMessageQueueingOptions(Database)
            {
                DiagnosticService        = DiagnosticService,
                MessageEncryptionService = new AesMessageEncryptionService(encryptionServiceOptions)
            };
            MessageQueueingService = new MongoDBMessageQueueingService(messageQueueingOptions);
        }
        protected async Task AssertMessageDocumentInserted(string collectionName, string database = null)
        {
            var db     = MongoDBHelper.Connect(ConnectionStringSettings, database);
            var coll   = db.GetCollection <BsonDocument>(collectionName);
            var filter = Builders <BsonDocument> .Filter.Eq("headers.Platibus-MessageId", Message.Headers.MessageId.ToString());

            var msg = await coll.Find(filter).FirstOrDefaultAsync();

            Assert.NotNull(msg);
        }
        protected async Task AssertSubscriptionDocumentInserted(string collectionName, string database = null)
        {
            var db     = MongoDBHelper.Connect(ConnectionStringSettings, database);
            var coll   = db.GetCollection <BsonDocument>(collectionName);
            var filter = Builders <BsonDocument> .Filter.Eq("topic", Topic.ToString())
                         & Builders <BsonDocument> .Filter.Eq("subscriber", Subscriber.ToString());

            var msg = await coll.Find(filter).FirstOrDefaultAsync();

            Assert.NotNull(msg);
        }
        protected async Task AssertEncryptedMessageDocumentInserted(string collectionName, string database = null)
        {
            var db     = MongoDBHelper.Connect(ConnectionStringSettings, database);
            var coll   = db.GetCollection <BsonDocument>(collectionName);
            var filter = Builders <BsonDocument> .Filter.Eq("headers.Platibus-MessageId", Message.Headers.MessageId.ToString());

            var journalEntry = await coll.Find(filter).FirstOrDefaultAsync();

            Assert.NotNull(journalEntry);

            var headers = ReadHeaders(journalEntry);

            Assert.NotNull(headers.IV);
            Assert.NotNull(headers.Signature);
        }
Example #6
0
        public MongoDBFixture()
        {
            var rng = new Random();

            DatabaseName = $"MongoDBFixture_{rng.Next(int.MaxValue):X}";

            // docker run -it --rm --name mongodb -p 27027:27017 mongo:3.6.0-jessie
            ConnectionStringSettings = new ConnectionStringSettings
            {
                Name             = DatabaseName,
                ConnectionString = $"mongodb://localhost:27027/{DatabaseName}?maxpoolsize=1000"
            };
#if NET452
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.ConnectionStrings.ConnectionStrings.Remove(ConnectionStringSettings.Name);
            config.ConnectionStrings.ConnectionStrings.Add(ConnectionStringSettings);
            config.Save();
            ConfigurationManager.RefreshSection("connectionStrings");
#endif
#if NETCOREAPP2_0
            ConfigurationManager.ConnectionStrings[ConnectionStringSettings.Name] = ConnectionStringSettings;
#endif

            Database = MongoDBHelper.Connect(ConnectionStringSettings, DatabaseName);

            var subscriptionTrackingOptions = new MongoDBSubscriptionTrackingOptions(Database)
            {
                DiagnosticService = DiagnosticService
            };
            SubscriptionTrackingService = new MongoDBSubscriptionTrackingService(subscriptionTrackingOptions);

            var messageQueueingOptions = new MongoDBMessageQueueingOptions(Database)
            {
                DiagnosticService = DiagnosticService
            };
            MessageQueueingService = new MongoDBMessageQueueingService(messageQueueingOptions);

            var journalOptions = new MongoDBMessageJournalOptions(Database)
            {
                DiagnosticService = DiagnosticService
            };
            MessageJournal = new MongoDBMessageJournal(journalOptions);
        }