public void CreateContext_AttributeUri_Wins(string attributeConnection, string expectedConnection)
        {
            // Arrange
            var attribute = new DocumentDBAttribute
            {
                ConnectionString = attributeConnection
            };

            var mockFactory = new Mock <IDocumentDBServiceFactory>();

            mockFactory
            .Setup(f => f.CreateService(expectedConnection))
            .Returns <IDocumentDBService>(null);

            // Default ConnecitonString will come from app.config
            var config = new DocumentDBConfiguration
            {
                DocumentDBServiceFactory = mockFactory.Object
            };

            // Act
            DocumentDBAttributeBindingProvider.CreateContext(config, attribute, null);

            // Assert
            mockFactory.VerifyAll();
        }
        public async Task CreateIfNotExists_Creates_IfTrue()
        {
            // Arrange
            string databaseName   = "TestDB";
            string collectionName = "TestCollection";

            var databaseUri = UriFactory.CreateDatabaseUri(databaseName);

            var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict);

            mockService
            .Setup(m => m.CreateDatabaseAsync(It.Is <Database>(d => d.Id == databaseName)))
            .ReturnsAsync(new Database());

            mockService
            .Setup(m => m.CreateDocumentCollectionAsync(databaseUri, It.Is <DocumentCollection>(d => d.Id == collectionName)))
            .ReturnsAsync(new DocumentCollection());

            var config = new DocumentDBConfiguration
            {
                ConnectionString         = "AccountEndpoint=http://someuri;AccountKey=some_key",
                DocumentDBServiceFactory = new TestDocumentDBServiceFactory(mockService.Object)
            };

            var provider = new DocumentDBAttributeBindingProvider(new JobHostConfiguration(), config);

            // Act
            await provider.TryCreateAsync(new BindingProviderContext(GetCreateIfNotExistsParameters().Last(), null, CancellationToken.None));

            // Assert
            mockService.VerifyAll();
        }
        private static Task <IBinding> CreateProviderAndTryCreateAsync(ParameterInfo parameter)
        {
            var jobConfig   = new JobHostConfiguration();
            var docDBConfig = new DocumentDBConfiguration
            {
                ConnectionString = "AccountEndpoint=https://someuri;AccountKey=my_key"
            };
            var provider = new DocumentDBAttributeBindingProvider(jobConfig, docDBConfig);

            var context = new BindingProviderContext(parameter, null, CancellationToken.None);

            return(provider.TryCreateAsync(context));
        }
        public async Task CreateIfNotExist_Throws_IfExceptionIsNotConflict()
        {
            // Arrange
            var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict);

            mockService
            .Setup(m => m.CreateDatabaseAsync(It.Is <Database>(d => d.Id == DatabaseName)))
            .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.BadRequest));

            // Act
            await Assert.ThrowsAsync <DocumentClientException>(
                () => DocumentDBAttributeBindingProvider.CreateIfNotExistAsync(mockService.Object, DatabaseName, CollectionName));

            // Assert
            mockService.VerifyAll();
        }
        public async Task CreateIfNotExist_Succeeds_IfCollectionDoesNotExist()
        {
            // Arrange
            var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict);

            mockService
            .Setup(m => m.CreateDatabaseAsync(It.Is <Database>(d => d.Id == DatabaseName)))
            .ThrowsAsync(DocumentDBTestUtility.CreateDocumentClientException(HttpStatusCode.Conflict));

            mockService
            .Setup(m => m.CreateDocumentCollectionAsync(databaseUri, It.Is <DocumentCollection>(d => d.Id == CollectionName)))
            .ReturnsAsync(new DocumentCollection());

            // Act
            await DocumentDBAttributeBindingProvider.CreateIfNotExistAsync(mockService.Object, DatabaseName, CollectionName);

            // Assert
            mockService.VerifyAll();
        }
        public async Task CreateIfNotExist_Succeeds_IfDbAndCollectionDoNotExist()
        {
            // Arrange
            var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict);

            mockService
            .Setup(m => m.CreateDatabaseAsync(It.Is <Database>(d => d.Id == DatabaseName)))
            .ReturnsAsync(new Database());

            mockService
            .Setup(m => m.CreateDocumentCollectionAsync(databaseUri, It.Is <DocumentCollection>(d => d.Id == CollectionName)))
            .ReturnsAsync(new DocumentCollection());

            // Act
            await DocumentDBAttributeBindingProvider.CreateIfNotExistAsync(mockService.Object, DatabaseName, CollectionName);

            // Assert
            mockService.VerifyAll();
        }
        public async Task CreateIfNotExists_DoesNotCreate_IfFalse()
        {
            // Arrange
            var mockService = new Mock <IDocumentDBService>(MockBehavior.Strict);
            var config      = new DocumentDBConfiguration
            {
                ConnectionString         = "AccountEndpoint=http://someuri;AccountKey=some_key",
                DocumentDBServiceFactory = new TestDocumentDBServiceFactory(mockService.Object)
            };
            var attribute = new DocumentDBAttribute {
                CreateIfNotExists = false
            };
            var provider = new DocumentDBAttributeBindingProvider(new JobHostConfiguration(), config);

            // Act
            await provider.TryCreateAsync(new BindingProviderContext(GetCreateIfNotExistsParameters().First(), null, CancellationToken.None));

            // Assert
            // Nothing to assert. Since the service was null, it was never called.
        }
        public void CreateContext_ResolvesNames()
        {
            // Arrange
            var resolver = new TestNameResolver();

            resolver.Values.Add("MyDatabase", "123abc");
            resolver.Values.Add("MyCollection", "abc123");

            var attribute = new DocumentDBAttribute("%MyDatabase%", "%MyCollection%");

            var config = new DocumentDBConfiguration
            {
                ConnectionString = "AccountEndpoint=https://someuri;AccountKey=some_key"
            };

            // Act
            var context = DocumentDBAttributeBindingProvider.CreateContext(config, attribute, resolver);

            // Assert
            Assert.Equal("123abc", context.ResolvedDatabaseName);
            Assert.Equal("abc123", context.ResolvedCollectionName);
        }