Exemplo n.º 1
0
        public void Catches_validation_error_for_Test1_file()
        {
            var services      = new ServiceCollection();
            var configuration = ConfigurationTestBuilder
                                .BuildFromEmbeddedResource(JsonIndex.AttributeValidated.Test1);
            var sectionName = SettingsSectionNameAttribute
                              .GetSettingsSectionName <AttributeValidatedSettings>();

            // the next call will throw an exception
            Action act = () =>
            {
                services.AddEagerlyValidatedSettings <AttributeValidatedSettings>(configuration, out _);
            };

            var exception = Assert.Throws <OptionsValidationException>(act);

            Assert.StartsWith("Validation failed for members:", exception.Message);

            /* Full validation message looks like
             *
             * Validation failed for members: 'IntegerA' with the error: 'The field IntegerA must be between 1
             * and 100.'.; Validation failed for members: 'BooleanB' with the error: 'The BooleanB field is
             * required.'.
             */
        }
Exemplo n.º 2
0
        public void Wires_up_SimpleSettings_from_json_files(string filename, int integerA)
        {
            var services      = new ServiceCollection();
            var configuration = ConfigurationTestBuilder.BuildFromEmbeddedResource(filename);

            services.AddValidatedSettings <SimpleSettings>(configuration);

            var serviceProvider = services.BuildServiceProvider();
            var result          = serviceProvider.GetRequiredService <IOptions <SimpleSettings> >().Value;

            Assert.NotNull(result);
            Assert.Equal(integerA, result.IntegerA);
        }
Exemplo n.º 3
0
        public void Wires_up_SimpleSettings_from_json_files_ReferenceEquals(string filename, int integerA)
        {
            var services      = new ServiceCollection();
            var configuration = ConfigurationTestBuilder.BuildFromEmbeddedResource(filename);

            services.AddEagerlyValidatedSettings <SimpleSettings>(configuration, out var initialResult);

            var serviceProvider = services.BuildServiceProvider();
            var result          = serviceProvider.GetRequiredService <IOptions <SimpleSettings> >().Value;

            Assert.Equal(integerA, initialResult.IntegerA);
            Assert.Equal(integerA, result.IntegerA);

            Assert.True(object.ReferenceEquals(initialResult, result));
        }
Exemplo n.º 4
0
        public void Wires_up_settings_from_Test2_file()
        {
            var services = new ServiceCollection();

            var configuration = ConfigurationTestBuilder.BuildFromEmbeddedResource(
                JsonIndex.AttributeValidated.Test2);

            services.AddValidatedSettings <AttributeValidatedSettings>(configuration);

            var serviceProvider = services.BuildServiceProvider();
            var result          = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value;

            Assert.NotNull(result);
            Assert.Equal(92, result.IntegerA);
            Assert.False(result.BooleanB);
        }
Exemplo n.º 5
0
        public void Wires_up_settings_from_Test2_file_ReferenceEquals()
        {
            var services = new ServiceCollection();

            var configuration = ConfigurationTestBuilder
                                .BuildFromEmbeddedResource(JsonIndex.AttributeValidated.Test2);

            services.AddEagerlyValidatedSettings <AttributeValidatedSettings>(configuration, out var initialResult);

            var serviceProvider = services.BuildServiceProvider();
            var result          = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value;

            Assert.Equal(92, initialResult.IntegerA);
            Assert.Equal(92, result.IntegerA);

            Assert.True(object.ReferenceEquals(initialResult, result));
        }
Exemplo n.º 6
0
        public void Test_reference_equals_1(string filename)
        {
            var services = new ServiceCollection();

            var configuration        = ConfigurationTestBuilder.BuildFromEmbeddedResource(filename);
            var sectionName          = SettingsSectionNameAttribute.GetSettingsSectionName <AttributeValidatedSettings>();
            var configurationSection = configuration.GetSection(sectionName);

            // This is the standard way, it results in lazy validation
            services.AddOptions <AttributeValidatedSettings>()
            .Bind(configurationSection)
            .RecursivelyValidateDataAnnotations();

            var serviceProvider = services.BuildServiceProvider();

            var result1 = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value;
            var result2 = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value;

            Assert.True(object.ReferenceEquals(result1, result2));
        }
Exemplo n.º 7
0
        public void Catches_validation_error_for_Test1_file()
        {
            var services = new ServiceCollection();

            var configuration = ConfigurationTestBuilder.BuildFromEmbeddedResource(
                JsonIndex.AttributeValidated.Test1);

            services.AddValidatedSettings <AttributeValidatedSettings>(configuration);

            var serviceProvider = services.BuildServiceProvider();

            // this call will work, because we're just getting the accessor singleton
            var optionsAccessor = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >();
            // the next call will throw an exception because we're accessing the .Value instance
            Action act = () =>
            {
                var _ = optionsAccessor.Value;
            };

            var exception = Assert.Throws <OptionsValidationException>(act);

            Assert.StartsWith("Validation failed for members: 'IntegerA'", exception.Message);
        }
Exemplo n.º 8
0
        public void Test_reference_equals_2(string filename)
        {
            var services = new ServiceCollection();

            var configuration  = ConfigurationTestBuilder.BuildFromEmbeddedResource(filename);
            var sectionName    = SettingsSectionNameAttribute.GetSettingsSectionName <AttributeValidatedSettings>();
            var simpleSettings = configuration.GetSection(sectionName).Get <AttributeValidatedSettings>();

            // This approach gives eager validation
            // Downside is that I think IOptionsMonitor<T> will not inform of changes

            var options = Options.Create <AttributeValidatedSettings>(simpleSettings);

            services.AddSingleton <IOptions <AttributeValidatedSettings> >(options);
            services.AddSingleton <IValidateOptions <AttributeValidatedSettings> >(
                new RecursiveDataAnnotationValidateOptions <AttributeValidatedSettings>(
                    null
                    ));

            var serviceProvider = services.BuildServiceProvider();

            var validator             = serviceProvider.GetRequiredService <IValidateOptions <AttributeValidatedSettings> >();
            var validateOptionsResult = validator.Validate(null, options.Value);

            if (validateOptionsResult.Failed)
            {
                throw new Exception();
            }

            var validatedSettings = options.Value;

            var result1 = validatedSettings;
            var result2 = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value;

            Assert.True(object.ReferenceEquals(result1, result2));
        }