public void GetAllElements_should_return_all_elements()
        {
            var collectionMock = new Mock <IMongoCollection <DataProtectionKey> >();
            var queryableMock  = new Mock <IMongoQueryable <DataProtectionKey> >();
            var fake           = new List <DataProtectionKey>
            {
                new DataProtectionKey
                {
                    Xml = "<a/>"
                }
            };

            queryableMock.Setup(m => m.GetEnumerator()).Returns(fake.GetEnumerator());

            var provider = new ServiceCollection()
                           .AddLogging()
                           .AddTransient(p => new MongoCollectionWrapper <DataProtectionKey>(collectionMock.Object, queryableMock.Object))
                           .BuildServiceProvider();

            var sut = new MongoDbXmlRepository <DataProtectionKey>(provider, provider.GetRequiredService <ILoggerFactory>());

            var result = sut.GetAllElements();

            Assert.Single(result);
        }
Exemplo n.º 2
0
        public void TestStoreElement(List <XElement> keys)
        {
            var repository = new MongoDbXmlRepository(KeyCollection);

            foreach (var key in keys)
            {
                repository.StoreElement(key, null);
            }
            var allDocuments = KeyCollection.Find(FilterDefinition <MongoDbXmlKey> .Empty).ToList();

            Assert.Equal(keys.Count, allDocuments.Count);
            Assert.Single(allDocuments, document => document.Key == Key);
        }
        public void StoreElement_PushesValueToList()
        {
            const string collectionName = "test3";
            var          repo           = new MongoDbXmlRepository(() => _databaseFixture.Database, collectionName);

            repo.StoreElement(new XElement("Element2"), null);

            var collection = _databaseFixture.Database.GetCollection <KeyElement>(collectionName);
            var keyElement = collection.Find(FilterDefinition <KeyElement> .Empty).FirstOrDefault();

            Assert.NotNull(keyElement);
            Assert.Equal("<Element2 />", keyElement.Xml);
        }
Exemplo n.º 4
0
        public void TestGetAllElements(List <XElement> keys)
        {
            var mongodbKeys = keys.Select(key => new MongoDbXmlKey
            {
                Id    = ObjectId.GenerateNewId(),
                Key   = key.ToString(SaveOptions.DisableFormatting),
                KeyId = key.Attribute(IdName)?.Value
            }).ToList();

            KeyCollection.InsertMany(mongodbKeys);
            var repository  = new MongoDbXmlRepository(KeyCollection);
            var allElements = repository.GetAllElements();

            Assert.Equal(keys.Count, allElements.Count);
            Assert.Single(allElements, element => element.ToString(SaveOptions.DisableFormatting) == Key);
            Assert.Single(KeyCollection.Find(document => document.Id == mongodbKeys.First().Id).ToEnumerable());
        }
        public void StoreElement_should_store_element()
        {
            var collectionMock = new Mock <IMongoCollection <KeyRotationKey> >();

            collectionMock.SetupGet(m => m.Settings).Returns(new MongoCollectionSettings());

            collectionMock.Setup(m => m.InsertOne(It.IsAny <KeyRotationKey>(), null, default)).Verifiable();

            var provider = new ServiceCollection()
                           .AddLogging()
                           .AddTransient(p => new MongoCollectionWrapper <KeyRotationKey>(collectionMock.Object))
                           .BuildServiceProvider();

            var sut = new MongoDbXmlRepository <KeyRotationKey>(provider, provider.GetRequiredService <ILoggerFactory>());

            sut.StoreElement(XElement.Parse("<a/>"), "test");
            collectionMock.Verify();
        }
        public void GetAllElements_ThrowsParsingException()
        {
            const string collectionName = "test2";
            var          collection     = _databaseFixture.Database.GetCollection <KeyElement>(collectionName);

            collection.InsertMany(new[]
            {
                new KeyElement {
                    Xml = "<Element1/>"
                },
                new KeyElement {
                    Xml = "<Element2"
                }
            });
            var repo = new MongoDbXmlRepository(() => _databaseFixture.Database, collectionName);

            Assert.Throws <XmlException>(() => repo.GetAllElements());
        }
        public void GetAllElements_ReturnsAllXmlValuesForGivenKey()
        {
            const string collectionName = "test1";
            var          collection     = _databaseFixture.Database.GetCollection <KeyElement>(collectionName);

            collection.InsertMany(new[]
            {
                new KeyElement {
                    Xml = "<Element1/>"
                },
                new KeyElement {
                    Xml = "<Element2/>"
                }
            });
            var repo     = new MongoDbXmlRepository(() => _databaseFixture.Database, collectionName);
            var elements = repo.GetAllElements().ToArray();

            Assert.Equal(new XElement("Element1").ToString(), elements[0].ToString());
            Assert.Equal(new XElement("Element2").ToString(), elements[1].ToString());
        }