コード例 #1
0
ファイル: DaemonAppTests.cs プロジェクト: ludeeus/netdaemon
        [InlineData(100.5)]         //float
        public async Task StorageShouldReturnSameValueAsSet(object data)
        {
            // ARRANGE
            var yamlConfigMock       = new Mock <YamlConfig>(Path.Combine(ConfigFixturePath, "level2", "level3"));
            IEnumerable <Type> types = new List <Type>()
            {
                typeof(AssmeblyDaemonApp)
            };
            var yamlConfig = @"
        app:
            class: AssmeblyDaemonApp
        ";

            using var instance = new YamlAppConfig(types, new StringReader(yamlConfig), yamlConfigMock.Object, "").Instances.FirstOrDefault() as AssmeblyDaemonApp;
            var daemonMock = new Mock <INetDaemon>();

            instance !.Id = "somefake_id";
            instance.InternalStorageObject = new FluentExpandoObject(false, true, daemon: instance);
            // await instance!.StartUpAsync(daemonMock.Object);

            // ACT
            instance !.Storage.Data = data;

            // ASSERT
            Assert.Equal(data, instance.Storage.Data);
            var stateQueueResult = await instance.InternalLazyStoreStateQueue.Reader.WaitToReadAsync();

            Assert.True(stateQueueResult);
        }
コード例 #2
0
ファイル: DaemonAppTests.cs プロジェクト: ludeeus/netdaemon
        public async Task StorageShouldRestoreWithCorrectValues()
        {
            // ARRANGE
            var yamlConfigMock       = new Mock <YamlConfig>(Path.Combine(ConfigFixturePath, "level2", "level3"));
            IEnumerable <Type> types = new List <Type>()
            {
                typeof(AssmeblyDaemonApp)
            };
            var yamlConfig = @"
        app:
            class: AssmeblyDaemonApp
        ";

            using var instance = new YamlAppConfig(types, new StringReader(yamlConfig), yamlConfigMock.Object, "").Instances.FirstOrDefault() as AssmeblyDaemonApp;
            var daemonMock = new Mock <INetDaemon>();

            var storageItem = new FluentExpandoObject();

            storageItem["Data"] = "SomeData";

            daemonMock.Setup(n => n.GetDataAsync <IDictionary <string, object> >(It.IsAny <string>()))
            .ReturnsAsync((IDictionary <string, object>)storageItem);

            await instance !.StartUpAsync(daemonMock.Object);

            // ACT
            await instance.RestoreAppStateAsync();

            // ASSERT
            Assert.Equal("SomeData", instance.Storage.Data);
        }
コード例 #3
0
ファイル: DaemonAppTests.cs プロジェクト: ludeeus/netdaemon
        public void InstanceAppFromConfigShouldHaveCorrectPropertiesCamelCaseConvert()
        {
            // ARRANGE
            var yamlConfigMock       = new Mock <YamlConfig>(Path.Combine(ConfigFixturePath, "level2", "level3"));
            IEnumerable <Type> types = new List <Type>()
            {
                typeof(AssmeblyDaemonApp)
            };
            var yamlConfig = @"
app:
    class: AssmeblyDaemonApp
    string_config: a string
    int_config: 10
    enumerable_config:
        - string 1
        - string 2
";
            // ACT
            var instances = new YamlAppConfig(types, new StringReader(yamlConfig), yamlConfigMock.Object, "").Instances;
            var instance  = instances.FirstOrDefault() as AssmeblyDaemonApp;

            // ASSERT
            Assert.Equal("a string", instance?.StringConfig);
            Assert.Equal(10, instance?.IntConfig);
            Assert.Equal(2, instance?.EnumerableConfig.Count());
        }
コード例 #4
0
ファイル: DaemonAppTests.cs プロジェクト: ludeeus/netdaemon
        public void InstanceAppFromConfigWithSecretsShouldHaveCorrectProperties()
        {
            // ARRANGE
            var yamlConfigMock       = new Mock <YamlConfig>(Path.Combine(ConfigFixturePath, "level2", "level3"));
            IEnumerable <Type> types = new List <Type>()
            {
                typeof(AssmeblyDaemonApp)
            };
            var yamlConfig = @"
app:
    class: AssmeblyDaemonApp
    test_secret_string: !secret a_secret_string
    test_secret_int: !secret a_secret_int
    test_normal_string: not a secret string
    test_normal_int: 0
";
            // ACT
            var instances = new YamlAppConfig(types, new StringReader(yamlConfig), yamlConfigMock.Object,
                                              Path.Combine(ConfigFixturePath, "level2", "level3", "any.cs")).Instances;
            var instance = instances.FirstOrDefault() as AssmeblyDaemonApp;

            // ASSERT
            Assert.Equal("this is a secret string", instance?.TestSecretString);
            Assert.Equal(99, instance?.TestSecretInt);
            Assert.Equal(0, instance?.TestNormalInt);
        }
コード例 #5
0
ファイル: DaemonAppTests.cs プロジェクト: jpgdev/netdaemon
        public void InstanceAppFromConfigShouldHaveCorrectProperties()
        {
            // ARRANGE
            var yamlConfigMock = new Mock <IYamlConfig>();

            yamlConfigMock.Setup(x => x.GetAllConfigFilePaths())
            .Returns(new[] { Path.Combine(ConfigFixturePath, "level2", "level3") });

            IEnumerable <Type> types = new List <Type>()
            {
                typeof(AssmeblyDaemonApp)
            };
            var yamlConfig = @"
        app:
            class: AssmeblyDaemonApp
            StringConfig: a string
            IntConfig: 10
            EnumerableConfig:
                - string 1
                - string 2
        ";
            // ACT
            var instances = new YamlAppConfig(types, new StringReader(yamlConfig), yamlConfigMock.Object, "").Instances;
            var instance  = instances.FirstOrDefault() as AssmeblyDaemonApp;

            // ASSERT
            Assert.Equal("a string", instance?.StringConfig);
            Assert.Equal(10, instance?.IntConfig);
            Assert.Equal(2, instance?.EnumerableConfig?.Count());
        }
コード例 #6
0
ファイル: DaemonAppTests.cs プロジェクト: ludeeus/netdaemon
        public void InstanceAppFromConfigNotFoundShouldReturnNull()
        {
            // ARRANGE
            var yamlConfigMock       = new Mock <YamlConfig>(Path.Combine(ConfigFixturePath, "level2", "level3"));
            IEnumerable <Type> types = new List <Type>()
            {
                typeof(AssmeblyDaemonApp)
            };
            var yamlConfig = "app:\n\tclass: NotFoundApp";
            // ACT
            var instances = new YamlAppConfig(types, new StringReader(yamlConfig), yamlConfigMock.Object, "").Instances;

            // ASSERT
            Assert.Empty(instances);
        }
コード例 #7
0
ファイル: DaemonAppTests.cs プロジェクト: ludeeus/netdaemon
        public void InstanceAppFromConfigShouldReturnCorrectType()
        {
            // ARRANGE
            var yamlConfigMock       = new Mock <YamlConfig>(Path.Combine(ConfigFixturePath, "level2", "level3"));
            IEnumerable <Type> types = new List <Type>()
            {
                typeof(AssmeblyDaemonApp)
            };
            var yamlConfig = "app:\n\tclass: AssmeblyDaemonApp";
            // ACT
            var instances = new YamlAppConfig(types, new StringReader(yamlConfig), yamlConfigMock.Object, "").Instances;

            // ASSERT
            Assert.Single(instances);
            // Assert.Equal(1, instances.Count());
            Assert.NotNull(instances.FirstOrDefault() as INetDaemonApp);
        }
コード例 #8
0
        public void YamlAdvancedObjectsShouldReturnCorrectData()
        {
            const string?yaml = @"
            a_string: hello world
            an_int: 10
            a_bool: true
            a_string_list:
            - hi
            - this
            - is
            - cool!
            devices:
            - name: tv
              commands:
                - name: command1
                  data: some code
                - name: command2
                  data: some code2";

            var yamlConfig = new YamlAppConfig(
                new List <Type>(),
                new System.IO.StringReader(yaml),
                new YamlConfig(CreateSettings(ConfigFixturePath)),
                ConfigFixturePath
                );

            var yamlStream = new YamlStream();

            yamlStream.Load(new StringReader(yaml));
            var root = (YamlMappingNode)yamlStream.Documents[0].RootNode;

            var instance =
                (AppComplexConfig?)yamlConfig.InstanceAndSetPropertyConfig(typeof(AppComplexConfig), root, "id");

            Assert.Equal("hello world", instance?.AString);
            Assert.Equal(10, instance?.AnInt);
            Assert.Equal(true, instance?.ABool);
            Assert.NotNull(instance?.Devices);
            Assert.Equal(1, instance?.Devices?.Count());
            Assert.Equal("tv", instance?.Devices?.First().Name);
            Assert.Equal("command1", instance?.Devices?.First()?.Commands?.ElementAt(0).Name);
            Assert.Equal("some code", instance?.Devices?.First()?.Commands?.ElementAt(0).Data);
            Assert.Equal("command2", instance?.Devices?.First()?.Commands?.ElementAt(1).Name);
            Assert.Equal("some code2", instance?.Devices?.First()?.Commands?.ElementAt(1).Data);
        }
コード例 #9
0
        public void InstanceAppFromConfigNotFoundShouldReturnNull()
        {
            // ARRANGE
            var yamlConfigMock = new Mock <IYamlConfig>();

            yamlConfigMock.Setup(x => x.GetAllConfigFilePaths())
            .Returns(new[] { Path.Combine(ConfigFixturePath, "level2", "level3") });

            IEnumerable <Type> types = new List <Type>()
            {
                typeof(AssemblyDaemonApp)
            };
            const string?yamlConfig = "app:\n  class: NotFoundApp";

            // ACT
            var instances = new YamlAppConfig(types, new StringReader(yamlConfig), yamlConfigMock.Object, "").Instances;

            // ASSERT
            Assert.Empty(instances);
        }
コード例 #10
0
        public IEnumerable <INetDaemonAppBase> InstanceDaemonApps()
        {
            var result = new List <INetDaemonAppBase>(50);

            // No loaded, just return an empty list
            if (_loadedDaemonApps?.Any() != true)
            {
                return(result);
            }

            // Get all yaml config file paths
            var allConfigFilePaths = _yamlConfig.GetAllConfigFilePaths();

            if (!allConfigFilePaths.Any())
            {
                _logger.LogWarning("No yaml configuration files found, please add yaml configuration to insance apps!");
                return(result);
            }

            foreach (string file in allConfigFilePaths)
            {
                try
                {
                    using var fileReader = File.OpenText(file);
                    var yamlAppConfig = new YamlAppConfig(_loadedDaemonApps, fileReader, _yamlConfig, file);

                    foreach (var appInstance in yamlAppConfig.Instances)
                    {
                        result.Add(appInstance);
                    }
                }
                catch (Exception e)
                {
                    _logger.LogTrace(e, "Error instance the app from the file {file}", file);
                    _logger.LogError("Error instance the app from the file {file}, use trace flag for details", file);
                    throw new NetDaemonException($"Error instance the app from the file {file}", e);
                }
            }
            return(result);
        }
コード例 #11
0
        public async Task StorageShouldRestoreWithCorrectValues()
        {
            // ARRANGE
            var yamlConfigMock = new Mock <IYamlConfig>();

            yamlConfigMock.Setup(x => x.GetAllConfigFilePaths())
            .Returns(new[] { Path.Combine(ConfigFixturePath, "level2", "level3") });

            IEnumerable <Type> types = new List <Type>()
            {
                typeof(AssemblyDaemonApp)
            };
            const string?yamlConfig = @"
                app:
                    class: NetDaemon.Daemon.Tests.DaemonRunner.App.AssemblyDaemonApp
                ";

            await using var instance = new YamlAppConfig(types, new StringReader(yamlConfig), yamlConfigMock.Object, "").Instances.FirstOrDefault() as AssemblyDaemonApp;
            var daemonMock = new Mock <INetDaemon>();

            daemonMock.SetupGet(x => x.Logger).Returns(new Mock <ILogger>().Object);

            var storageItem = new FluentExpandoObject
            {
                ["Data"] = "SomeData"
            };

            daemonMock.SetupGet(x => x.Logger).Returns(new Mock <ILogger>().Object);
            daemonMock.Setup(n => n.GetDataAsync <IDictionary <string, object> >(It.IsAny <string>()))
            .ReturnsAsync((IDictionary <string, object>)storageItem);

            await instance !.StartUpAsync(daemonMock.Object).ConfigureAwait(false);

            // ACT
            await instance.RestoreAppStateAsync().ConfigureAwait(false);

            // ASSERT
            Assert.Equal("SomeData", instance.Storage.Data);
        }