public async Task ValidParametersWithAppSettings_Succeed(ParameterInfo parameter)
        {
            var nameResolver = new TestNameResolver();

            nameResolver.Values["aDatabase"]   = "myDatabase";
            nameResolver.Values["aCollection"] = "myCollection";

            var config = new ConfigurationBuilder()
                         .AddInMemoryCollection(new Dictionary <string, string>
            {
                { "ConnectionStrings:CosmosDBConnectionString", "AccountEndpoint=https://fromSettings;AccountKey=c29tZV9rZXk=;" }
            })
                         .Build();

            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(config, nameResolver, _options, CreateExtensionConfigProvider(_options), _loggerFactory);

            CosmosDBTriggerBinding binding = (CosmosDBTriggerBinding)await provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None));

            Assert.Equal(typeof(IReadOnlyList <Document>), binding.TriggerValueType);
            Assert.Equal(new Uri("https://fromSettings"), binding.DocumentCollectionLocation.Uri);
            Assert.Equal(new Uri("https://fromSettings"), binding.LeaseCollectionLocation.Uri);
            Assert.Equal("myDatabase", binding.DocumentCollectionLocation.DatabaseName);
            Assert.Equal("myCollection", binding.DocumentCollectionLocation.CollectionName);
            Assert.Equal("myDatabase", binding.LeaseCollectionLocation.DatabaseName);
            Assert.Equal("leases", binding.LeaseCollectionLocation.CollectionName);
        }
        public async Task ValidLeaseHostOptions_Succeed(ParameterInfo parameter)
        {
            var nameResolver = new TestNameResolver();

            nameResolver.Values["dynamicLeasePrefix"] = "someLeasePrefix";

            var config = new ConfigurationBuilder()
                         .AddInMemoryCollection(new Dictionary <string, string>
            {
                // Verify we load from connection strings first
                { "ConnectionStrings:CosmosDBConnectionString", "AccountEndpoint=https://fromSettings;AccountKey=c29tZV9rZXk=;" },
                { "CosmosDBConnectionString", "will not work" },
                { "ConnectionStrings:LeaseConnectionString", "AccountEndpoint=https://overridden;AccountKey=c29tZV9rZXk=;" },
                { "LeaseConnectionString", "will not work" }
            })
                         .Build();

            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(config, nameResolver, _options, CreateExtensionConfigProvider(_options), _loggerFactory);

            CosmosDBTriggerBinding binding = (CosmosDBTriggerBinding)await provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None));

            // This test uses the default for ConnectionStringSetting, but overrides LeaseConnectionStringSetting
            Assert.Equal(typeof(IReadOnlyList <Document>), binding.TriggerValueType);
            Assert.Equal(new Uri("https://fromEnvironment"), binding.DocumentCollectionLocation.Uri);
            Assert.Equal(new Uri("https://overridden"), binding.LeaseCollectionLocation.Uri);
            Assert.Equal("someLeasePrefix", binding.ChangeFeedProcessorOptions.LeasePrefix);
            Assert.Null(binding.ChangeFeedProcessorOptions.MaxItemCount);
            Assert.False(binding.ChangeFeedProcessorOptions.StartFromBeginning);
        }
        public async Task ValidParameters_Succeed(ParameterInfo parameter)
        {
            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider("AccountEndpoint=https://someEndpoint;AccountKey=someKey;", string.Empty);

            ITriggerBinding binding = await provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None));

            Assert.Equal(binding.TriggerValueType, typeof(IReadOnlyList <Document>));
        }
        public async Task ValidCosmosDBTriggerBindigsMultiMasterParameters_Succeed(ParameterInfo parameter)
        {
            var nameResolver = new TestNameResolver();

            nameResolver.Values["regions"] = "East US, North Europe,";

            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(_emptyConfig, nameResolver, _options, CreateExtensionConfigProvider(_options), _loggerFactory);

            CosmosDBTriggerBinding binding = (CosmosDBTriggerBinding)await provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None));

            Assert.False(binding.DocumentCollectionLocation.ConnectionPolicy.UseMultipleWriteLocations);
            Assert.True(binding.LeaseCollectionLocation.ConnectionPolicy.UseMultipleWriteLocations);
        }
        public async Task ValidParametersWithEnvironment_Succeed(ParameterInfo parameter)
        {
            var nameResolver = new TestNameResolver();

            nameResolver.Values[CosmosDBConfiguration.AzureWebJobsCosmosDBConnectionStringName] = "AccountEndpoint=https://fromEnvironment;AccountKey=someKey;";
            nameResolver.Values["CosmosDBConnectionString"] = "AccountEndpoint=https://fromSettings;AccountKey=someKey;";

            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(nameResolver, CreateConfiguration());

            CosmosDBTriggerBinding binding = (CosmosDBTriggerBinding)await provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None));

            Assert.Equal(typeof(IReadOnlyList <Document>), binding.TriggerValueType);
            Assert.Equal(binding.DocumentCollectionLocation.Uri, new Uri("https://fromEnvironment"));
        }
        public async Task ValidChangeFeedOptions_Succeed(ParameterInfo parameter)
        {
            var nameResolver = new TestNameResolver();

            nameResolver.Values[CosmosDBExtensionConfigProvider.AzureWebJobsCosmosDBConnectionStringName] = "AccountEndpoint=https://fromEnvironment;AccountKey=someKey;";
            nameResolver.Values["CosmosDBConnectionString"] = "AccountEndpoint=https://fromSettings;AccountKey=someKey;";

            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(nameResolver, _options, CreateExtensionConfigProvider(_options), _loggerFactory);

            CosmosDBTriggerBinding binding = (CosmosDBTriggerBinding)await provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None));

            Assert.Equal(typeof(IReadOnlyList <Document>), binding.TriggerValueType);
            Assert.Equal(new Uri("https://fromSettings"), binding.DocumentCollectionLocation.Uri);
            Assert.Equal(10, binding.ChangeFeedOptions.MaxItemCount);
            Assert.True(binding.ChangeFeedOptions.StartFromBeginning);
        }
Esempio n. 7
0
        public async Task ValidCosmosDBTriggerBindigsPreferredLocationsParameters_Succeed(ParameterInfo parameter)
        {
            var nameResolver = new TestNameResolver();

            nameResolver.Values["regions"] = "East US, North Europe,";

            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(_emptyConfig, nameResolver, _options, CreateExtensionConfigProvider(_options), _loggerFactory);

            CosmosDBTriggerBinding binding = (CosmosDBTriggerBinding)await provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None));

            Assert.Equal(2, binding.DocumentCollectionLocation.ConnectionPolicy.PreferredLocations.Count);
            Assert.Equal(2, binding.LeaseCollectionLocation.ConnectionPolicy.PreferredLocations.Count);
            Assert.Equal("East US", binding.DocumentCollectionLocation.ConnectionPolicy.PreferredLocations[0]);
            Assert.Equal("North Europe", binding.DocumentCollectionLocation.ConnectionPolicy.PreferredLocations[1]);
            Assert.Equal("East US", binding.LeaseCollectionLocation.ConnectionPolicy.PreferredLocations[0]);
            Assert.Equal("North Europe", binding.LeaseCollectionLocation.ConnectionPolicy.PreferredLocations[1]);
        }
        public async Task ValidParametersWithEnvironment_ConnectionMode_Succeed(ParameterInfo parameter)
        {
            var nameResolver = new TestNameResolver();

            nameResolver.Values[CosmosDBExtensionConfigProvider.AzureWebJobsCosmosDBConnectionStringName] = "AccountEndpoint=https://fromEnvironment;AccountKey=someKey;";
            nameResolver.Values["CosmosDBConnectionString"] = "AccountEndpoint=https://fromSettings;AccountKey=someKey;";

            _options.ConnectionMode = ConnectionMode.Direct;
            _options.Protocol       = Protocol.Tcp;

            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(nameResolver, _options, CreateExtensionConfigProvider(_options), _loggerFactory);

            CosmosDBTriggerBinding binding = (CosmosDBTriggerBinding)await provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None));

            Assert.Equal(typeof(IReadOnlyList <Document>), binding.TriggerValueType);
            Assert.Equal(binding.DocumentCollectionLocation.Uri, new Uri("https://fromEnvironment"));
            Assert.Equal(ConnectionMode.Direct, binding.DocumentCollectionLocation.ConnectionPolicy.ConnectionMode);
            Assert.Equal(Protocol.Tcp, binding.DocumentCollectionLocation.ConnectionPolicy.ConnectionProtocol);
        }
        public async Task ValidChangeFeedOptions_Succeed(ParameterInfo parameter)
        {
            var config = new ConfigurationBuilder()
                         .AddInMemoryCollection(new Dictionary <string, string>
            {
                // Verify we load from root config
                { "CosmosDBConnectionString", "AccountEndpoint=https://fromSettings;AccountKey=c29tZV9rZXk=;" }
            })
                         .Build();

            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(config, new TestNameResolver(), _options, CreateExtensionConfigProvider(_options), _loggerFactory);

            CosmosDBTriggerBinding binding = (CosmosDBTriggerBinding)await provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None));

            Assert.Equal(typeof(IReadOnlyList <Document>), binding.TriggerValueType);
            Assert.Equal(new Uri("https://fromSettings"), binding.DocumentCollectionLocation.Uri);
            Assert.Equal(new Uri("https://fromSettings"), binding.LeaseCollectionLocation.Uri);
            Assert.Equal(10, binding.ChangeFeedProcessorOptions.MaxItemCount);
            Assert.True(binding.ChangeFeedProcessorOptions.StartFromBeginning);
        }
        public async Task ValidCosmosDBTriggerBindigsWithDatabaseAndCollectionSettings_Succeed(ParameterInfo parameter)
        {
            var nameResolver = new TestNameResolver();

            nameResolver.Values["CosmosDBConnectionString"] = "AccountEndpoint=https://fromSettings;AccountKey=c29tZV9rZXk=;";
            nameResolver.Values["aDatabase"]   = "myDatabase";
            nameResolver.Values["aCollection"] = "myCollection";

            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(_emptyConfig, nameResolver, _options, CreateExtensionConfigProvider(_options), _loggerFactory);

            CosmosDBTriggerBinding binding = (CosmosDBTriggerBinding)await provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None));

            Assert.Equal(typeof(IReadOnlyList <Document>), binding.TriggerValueType);
            Assert.Equal(new Uri("https://fromEnvironment"), binding.DocumentCollectionLocation.Uri);
            Assert.Equal(new Uri("https://fromEnvironment"), binding.LeaseCollectionLocation.Uri);
            Assert.Equal("myDatabase-test", binding.DocumentCollectionLocation.DatabaseName);
            Assert.Equal("myCollection-test", binding.DocumentCollectionLocation.CollectionName);
            Assert.Equal("myDatabase-test", binding.LeaseCollectionLocation.DatabaseName);
            Assert.Equal("leases", binding.LeaseCollectionLocation.CollectionName);
        }
        public async Task ValidCosmosDBTriggerBindingsUseDefaultJsonSerialization_Succeed(ParameterInfo parameter)
        {
            var nameResolver = new TestNameResolver();

            var restoreDefaultSettings = JsonConvert.DefaultSettings;

            var defaultSettingsFetched = false;

            JsonConvert.DefaultSettings = () =>
            {
                defaultSettingsFetched = true;
                return(new JsonSerializerSettings());
            };

            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(_emptyConfig, nameResolver, _options, CreateExtensionConfigProvider(_options), _loggerFactory);

            CosmosDBTriggerBinding binding = (CosmosDBTriggerBinding)await provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None));

            Assert.True(defaultSettingsFetched);

            JsonConvert.DefaultSettings = restoreDefaultSettings;
        }
        public async Task ValidParametersWithEnvironment_ConnectionMode_Succeed(ParameterInfo parameter)
        {
            var nameResolver = new TestNameResolver();
            var config       = new ConfigurationBuilder()
                               .AddInMemoryCollection(new Dictionary <string, string>
            {
                { "ConnectionStrings:CosmosDBConnectionString", "AccountEndpoint=https://fromSettings;AccountKey=c29tZV9rZXk=;" }
            })
                               .Build();

            _options.ConnectionMode = ConnectionMode.Direct;
            _options.Protocol       = Protocol.Tcp;

            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(config, nameResolver, _options, CreateExtensionConfigProvider(_options), _loggerFactory);

            CosmosDBTriggerBinding binding = (CosmosDBTriggerBinding)await provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None));

            Assert.Equal(typeof(IReadOnlyList <Document>), binding.TriggerValueType);
            Assert.Equal(new Uri("https://fromEnvironment"), binding.DocumentCollectionLocation.Uri);
            Assert.Equal(new Uri("https://fromEnvironment"), binding.LeaseCollectionLocation.Uri);
            Assert.Equal(ConnectionMode.Direct, binding.DocumentCollectionLocation.ConnectionPolicy.ConnectionMode);
            Assert.Equal(Protocol.Tcp, binding.DocumentCollectionLocation.ConnectionPolicy.ConnectionProtocol);
        }
        public async Task InvalidParameters_Fail(ParameterInfo parameter)
        {
            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(_emptyConfig, new TestNameResolver(), _options, CreateExtensionConfigProvider(_options), _loggerFactory);

            InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(() => provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None)));

            Assert.NotNull(ex);
        }
        public async Task InvalidParameters_Fail(ParameterInfo parameter)
        {
            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider(new DefaultNameResolver(), CreateConfiguration());

            InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(() => provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None)));

            Assert.NotNull(ex);
        }
        public async Task InvalidParameters_Fail(ParameterInfo parameter)
        {
            CosmosDBTriggerAttributeBindingProvider provider = new CosmosDBTriggerAttributeBindingProvider("notAConnectionString", string.Empty);

            InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(() => provider.TryCreateAsync(new TriggerBindingProviderContext(parameter, CancellationToken.None)));

            Assert.NotNull(ex);
        }