예제 #1
0
        private T LoadConfig()
        {
            string configContents       = ConfigSource.GetContents();
            string configSchemaContents = ConfigSchemaSource.GetContents();
            OperationResult <IList <string> > validationResult = ConfigService <T> .Validate(
                configContents, configSchemaContents
                );

            if (!validationResult.Result)
            {
                var exception = new WrongConfigException($"File {ConfigSource.Name} has a wrong structure.");
                for (int i = 0; i < validationResult.Data.Count; i++)
                {
                    exception.Data.Add(i.ToString(), validationResult.Data[i]);
                }
                throw exception;
            }
            using var stream = new MemoryStream(Encoding.UTF8.GetBytes(configContents));
            T data = new ConfigurationBuilder()
                     .AddJsonStream(stream)
                     .Build()
                     .Get <T>();

            Config = data;

            return(Config);
        }
예제 #2
0
        public void Init_WrongConfiguration_ThrowsWrongConfigException(
            string configPath,
            string schemaPath,
            string expectedErrorMsg,
            Dictionary <string, string> expectedExceptionDetails)
        {
            // Arrange
            var configSource       = new EmbeddedSource(configPath, Assembly.GetExecutingAssembly());
            var configSchemaSource = new EmbeddedSource(schemaPath, Assembly.GetExecutingAssembly());

            // Act
            void action() => _ = new ConfigService <Settings>(configSource, configSchemaSource);

            // Assert
            WrongConfigException ex = Assert.Throws <WrongConfigException>(action);

            Assert.Equal(expectedErrorMsg, ex.Message);
            Assert.Equal(expectedExceptionDetails.Count, ex.Data.Count);
            foreach (KeyValuePair <string, string> el in expectedExceptionDetails)
            {
                Assert.True(ex.Data.Contains(el.Key));
                Assert.Equal(el.Value, ex.Data[el.Key]);
            }
        }