Пример #1
0
        public void JsonContentTypeTests_LoadInvalidJsonValueAsStringValue()
        {
            List <ConfigurationSetting> _kvCollection = new List <ConfigurationSetting>
            {
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "TestKey1",
                    value: "True",
                    contentType: "application/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "TestKey2",
                    value: "[abc,def,ghi]",
                    contentType: "application/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "TestKey3",
                    value: "{\"Name\": Foo}",
                    contentType: "APPLICATION/JSON"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "TestKey4",
                    value: null,
                    contentType: "APPLICATION/JSON")
            };
            var mockClient = GetMockConfigurationClient(_kvCollection);

            var config = new ConfigurationBuilder()
                         .AddAzureAppConfiguration(options => options.Client = mockClient.Object)
                         .Build();

            Assert.Equal("True", config["TestKey1"]);
            Assert.Equal("[abc,def,ghi]", config["TestKey2"]);
            Assert.Equal("{\"Name\": Foo}", config["TestKey3"]);
            Assert.Null(config["TestKey4"]);
        }
        public async Task GetAppConfigStatusReturnsUnhealthyOnExceptionTest()
        {
            Response <ConfigurationSetting> response = Response.FromValue(ConfigurationModelFactory.ConfigurationSetting(string.Empty, string.Empty), this.mockResponse.Object);

            this.client.Setup(c => c.GetConfigurationSettingAsync(string.Empty, It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(response);

            StatusResultServiceModel result = await this.appConfigClient.StatusAsync();

            Assert.False(result.IsHealthy);
        }
Пример #3
0
        public void JsonContentTypeTests_JsonKeyValueAdapterCannotProcessFeatureFlags()
        {
            var compactJsonValue         = "{\"id\":\"Beta\",\"description\":\"\",\"enabled\":true,\"conditions\":{\"client_filters\":[{\"name\":\"Browser\",\"parameters\":{\"AllowedBrowsers\":[\"Firefox\",\"Safari\"]}}]}}";
            ConfigurationSetting setting = ConfigurationModelFactory.ConfigurationSetting(
                key: FeatureManagementConstants.FeatureFlagMarker + "Beta",
                value: compactJsonValue,
                contentType: FeatureManagementConstants.ContentType + ";charset=utf-8");

            var jsonKeyValueAdapter = new JsonKeyValueAdapter();

            Assert.False(jsonKeyValueAdapter.CanProcess(setting));
        }
        public async Task GetAppConfigStatusReturnsHealthyTest()
        {
            this.mockConfig.Setup(x => x.Global.Location).Returns("eastus");

            Response <ConfigurationSetting> response = Response.FromValue(ConfigurationModelFactory.ConfigurationSetting("test", "test"), this.mockResponse.Object);

            this.client.Setup(c => c.GetConfigurationSetting(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Returns(response);

            StatusResultServiceModel result = await this.appConfigClient.StatusAsync();

            Assert.True(result.IsHealthy);
        }
        public void GetAppConfigValueByKeyTest()
        {
            string key   = this.rand.NextString();
            string value = this.rand.NextString();
            Response <ConfigurationSetting> response = Response.FromValue(ConfigurationModelFactory.ConfigurationSetting("test", "test"), this.mockResponse.Object);

            this.client.Setup(c => c.GetConfigurationSetting(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Returns(response);

            string result = this.appConfigClient.GetValue(key);

            Assert.Equal(result, response.Value.Value);
        }
        public async Task SetAppConfigByKeyAndValueTest()
        {
            string key   = this.rand.NextString();
            string value = this.rand.NextString();
            Response <ConfigurationSetting> response = Response.FromValue(ConfigurationModelFactory.ConfigurationSetting("test", "test"), this.mockResponse.Object);

            this.client.Setup(c => c.SetConfigurationSettingAsync(It.IsAny <ConfigurationSetting>(), true, It.IsAny <CancellationToken>()))
            .Returns(Task <Response> .FromResult(response));

            await this.appConfigClient.SetValueAsync(key, value);

            Assert.True(true);
        }
Пример #7
0
        public void JsonContentTypeTests_JsonKeyValueAdapterCannotProcessKeyVaultReferences()
        {
            ConfigurationSetting setting = ConfigurationModelFactory.ConfigurationSetting(
                key: "TK1",
                value: @"
                    {
                        ""uri"":""https://keyvault-theclassics.vault.azure.net/secrets/TheTrialSecret""
                    }
                   ",
                contentType: KeyVaultConstants.ContentType + "; charset=utf-8");

            var jsonKeyValueAdapter = new JsonKeyValueAdapter();

            Assert.False(jsonKeyValueAdapter.CanProcess(setting));
        }
Пример #8
0
        public void JsonContentTypeTests_LoadSettingsWithInvalidJsonContentTypeAsString()
        {
            List <ConfigurationSetting> _kvCollection = new List <ConfigurationSetting>
            {
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "TestKey1",
                    value: "true",
                    contentType: "application/notjson"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "TestKey2",
                    value: "[1,2,3]",
                    contentType: "text/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "TestKey3",
                    value: "{\"Name\": \"Foo\"}",
                    contentType: null),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "TestKey4",
                    value: "99",
                    contentType: ""),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "TestKey5",
                    value: "null",
                    contentType: "json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "TestKey6",
                    value: null,
                    contentType: "/"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "TestKey7",
                    value: "{}",
                    contentType: "application/")
            };

            var mockClient = GetMockConfigurationClient(_kvCollection);

            var config = new ConfigurationBuilder()
                         .AddAzureAppConfiguration(options => options.Client = mockClient.Object)
                         .Build();

            Assert.Equal("true", config["TestKey1"]);
            Assert.Equal("[1,2,3]", config["TestKey2"]);
            Assert.Equal("{\"Name\": \"Foo\"}", config["TestKey3"]);
            Assert.Equal("99", config["TestKey4"]);
            Assert.Equal("null", config["TestKey5"]);
            Assert.Null(config["TestKey6"]);
            Assert.Equal("{}", config["TestKey7"]);
        }
Пример #9
0
        public static List <ConfigurationSetting> LoadJsonSettingsFromFile(string path)
        {
            List <ConfigurationSetting> _kvCollection = new List <ConfigurationSetting>();
            var valueArray = JsonSerializer.Deserialize <JsonElement>(File.ReadAllText(path)).EnumerateArray();

            foreach (var setting in valueArray)
            {
                ConfigurationSetting kv = ConfigurationModelFactory
                                          .ConfigurationSetting(
                    key: setting.GetProperty("key").ToString(),
                    value: setting.GetProperty("value").GetRawText(),
                    contentType: setting.GetProperty("contentType").ToString());
                _kvCollection.Add(kv);
            }
            return(_kvCollection);
        }
Пример #10
0
        public void MockClient()
        {
            // Create a mock response
            var mockResponse = new Mock <Response>();
            // Create a client mock
            var mock = new Mock <ConfigurationClient>();

            // Setup client method
            mock.Setup(c => c.Get("Key", It.IsAny <string>(), It.IsAny <DateTimeOffset>(), It.IsAny <CancellationToken>()))
            .Returns(new Response <ConfigurationSetting>(mockResponse.Object, ConfigurationModelFactory.ConfigurationSetting("Key", "Value")));

            // Use the client mock
            ConfigurationClient  client  = mock.Object;
            ConfigurationSetting setting = client.Get("Key");

            Assert.AreEqual("Value", setting.Value);
        }
Пример #11
0
        /*
         * This sample illustrates how to use Moq to create a unit test that
         * mocks the reponse from a ConfigurationClient method.  For more
         * examples of mocking, see the Azure.Data.AppConfiguration.Tests project.
         */
        public void MockClient()
        {
            // Create a mock response.
            var mockResponse = new Mock <Response>();

            // Create a mock client.
            var mockClient = new Mock <ConfigurationClient>();

            // Set up a client method that will be called when GetConfigurationSetting is called on the mock client.
            mockClient.Setup(c => c.GetConfigurationSetting("Key", It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Returns(Response.FromValue(ConfigurationModelFactory.ConfigurationSetting("Key", "Value"), mockResponse.Object));

            // Use the mock client to validate client functionality without making a network call.
            ConfigurationClient  client  = mockClient.Object;
            ConfigurationSetting setting = client.GetConfigurationSetting("Key");

            Assert.AreEqual("Value", setting.Value);
        }
Пример #12
0
        public void JsonContentTypeTests_DontFlattenFeatureFlagAsJsonObject()
        {
            var compactJsonValue = "{\"id\":\"Beta\",\"description\":\"\",\"enabled\":true,\"conditions\":{\"client_filters\":[{\"name\":\"Browser\",\"parameters\":{\"AllowedBrowsers\":[\"Firefox\",\"Safari\"]}}]}}";
            List <ConfigurationSetting> _kvCollection = new List <ConfigurationSetting>
            {
                ConfigurationModelFactory.ConfigurationSetting(
                    key: FeatureManagementConstants.FeatureFlagMarker + "Beta",
                    value: compactJsonValue,
                    contentType: FeatureManagementConstants.ContentType + ";charset=utf-8")
            };

            var mockClient = GetMockConfigurationClient(_kvCollection);

            var config = new ConfigurationBuilder()
                         .AddAzureAppConfiguration(options => options.Client = mockClient.Object)
                         .Build();

            Assert.Equal(compactJsonValue, config[FeatureManagementConstants.FeatureFlagMarker + "Beta"]);
        }
        public async Task MockClient()
        {
            #region Snippet:AzConfigSample7_CreateMocks
            var mockResponse = new Mock <Response>();
            var mockClient   = new Mock <ConfigurationClient>();
            #endregion

            #region Snippet:AzConfigSample7_SetupMocks

            Response <ConfigurationSetting> response = Response.FromValue(ConfigurationModelFactory.ConfigurationSetting("available_vms", "10"), mockResponse.Object);
            mockClient.Setup(c => c.GetConfigurationSettingAsync("available_vms", It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(response));
            mockClient.Setup(c => c.SetConfigurationSettingAsync(It.IsAny <ConfigurationSetting>(), true, It.IsAny <CancellationToken>()))
            .Returns((ConfigurationSetting cs, bool onlyIfUnchanged, CancellationToken ct) => Task.FromResult(Response.FromValue(cs, new Mock <Response>().Object)));
            #endregion

            #region Snippet:AzConfigSample7_UseMocks
            ConfigurationClient client = mockClient.Object;
            int availableVms           = await UpdateAvailableVmsAsync(client, 2, default);

            Assert.AreEqual(12, availableVms);
            #endregion
        }
Пример #14
0
        public void JsonContentTypeTests_FlattenFeatureFlagWhenContentTypeIsNotFeatureManagementContentType()
        {
            var compactJsonValue = "{\"id\":\"Beta\",\"description\":\"\",\"enabled\":true,\"conditions\":{\"client_filters\":[{\"name\":\"Browser\",\"parameters\":{\"AllowedBrowsers\":[\"Firefox\",\"Safari\"]}}]}}";
            List <ConfigurationSetting> _kvCollection = new List <ConfigurationSetting>
            {
                ConfigurationModelFactory.ConfigurationSetting(
                    key: FeatureManagementConstants.FeatureFlagMarker + "Beta",
                    value: compactJsonValue,
                    contentType: "application/json")
            };

            var mockClient = GetMockConfigurationClient(_kvCollection);

            var config = new ConfigurationBuilder()
                         .AddAzureAppConfiguration(options => options.Client = mockClient.Object)
                         .Build();

            Assert.Equal("Beta", config[FeatureManagementConstants.FeatureFlagMarker + "Beta:id"]);
            Assert.Equal("", config[FeatureManagementConstants.FeatureFlagMarker + "Beta:description"]);
            Assert.Equal("True", config[FeatureManagementConstants.FeatureFlagMarker + "Beta:enabled"]);
            Assert.Equal("Browser", config[FeatureManagementConstants.FeatureFlagMarker + "Beta:conditions:client_filters:0:name"]);
            Assert.Equal("Firefox", config[FeatureManagementConstants.FeatureFlagMarker + "Beta:conditions:client_filters:0:parameters:AllowedBrowsers:0"]);
            Assert.Equal("Safari", config[FeatureManagementConstants.FeatureFlagMarker + "Beta:conditions:client_filters:0:parameters:AllowedBrowsers:1"]);
        }
Пример #15
0
        public void JsonContentTypeTests_OverwriteValuesForDuplicateKeysAfterFlatteningJson()
        {
            List <ConfigurationSetting> _kvCollection = new List <ConfigurationSetting>
            {
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "MyNumberList",
                    value:  "[10, 20, 30, 40]",
                    contentType: "application/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "MyNumberList:0",
                    value: "11",
                    contentType: "application/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "MyNumberList:1",
                    value: "22",
                    contentType: "application/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "MyNumberList:2",
                    value: "33",
                    contentType: "application/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "MyNumberList:3",
                    value: "44",
                    contentType: "application/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "MyObject",
                    value: "{\"ObjectSetting\": {\"Logging\": {\"LogLevel\": \"Information\", \"Default\": \"Debug\"}}}",
                    contentType: "application/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "MyObject:ObjectSetting:Logging:Default",
                    value: "\"Debug2\"",
                    contentType: "application/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "MyObject:ObjectSetting:Logging:LogLevel",
                    value: "\"Information2\"",
                    contentType: "application/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "MyObjectWithDuplicateProperties",
                    value: "{\"Name\": \"Value1\", \"Name\": \"Value2\"}",
                    contentType: "application/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "CaseSensitiveKey",
                    value: "\"foobar\"",
                    contentType: "application/json"),
                ConfigurationModelFactory.ConfigurationSetting(
                    key: "casesensitivekey",
                    value: "\"foobar-overwritten\"",
                    contentType: "application/json")
            };

            var mockClient = GetMockConfigurationClient(_kvCollection);

            var config = new ConfigurationBuilder()
                         .AddAzureAppConfiguration(options => options.Client = mockClient.Object)
                         .Build();

            Assert.Null(config["MyNumberList"]);
            Assert.Equal("11", config["MyNumberList:0"]);
            Assert.Equal("22", config["MyNumberList:1"]);
            Assert.Equal("33", config["MyNumberList:2"]);
            Assert.Equal("44", config["MyNumberList:3"]);

            Assert.Null(config["MyObject"]);
            Assert.Equal("Debug2", config["MyObject:ObjectSetting:Logging:Default"]);
            Assert.Equal("Information2", config["MyObject:ObjectSetting:Logging:LogLevel"]);

            Assert.Null(config["MyObjectWithDuplicateProperties"]);
            Assert.Equal("Value2", config["MyObjectWithDuplicateProperties:Name"]);

            Assert.Equal("foobar-overwritten", config["CaseSensitiveKey"]);
            Assert.Equal("foobar-overwritten", config["casesensitivekey"]);
        }
Пример #16
0
 public static ConfigurationSetting CloneSetting(ConfigurationSetting setting)
 {
     return(ConfigurationModelFactory.ConfigurationSetting(setting.Key, setting.Value, setting.Label, setting.ContentType, setting.ETag, setting.LastModified));
 }