Ejemplo n.º 1
0
        public async Task FindZvsAdapterInvalidIdAsyncTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List <LogEntry>();
            var log        = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return(Task.FromResult(0));
                }
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter>(), dbConnection, log);
            await adapterManager.StartAsync(CancellationToken.None);

            //act
            var adapter = adapterManager.FindZvsAdapter(1);

            //assert
            Assert.IsNull(adapter, "Found a adapter?");
        }
Ejemplo n.º 2
0
        public async Task GetZvsAdaptersTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var log = new StubIFeedback <LogEntry>();
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0)
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);

            //act
            var result = adapterManager.GetZvsAdapters();

            //assert
            Assert.IsTrue(result.Count() == 1, "Expected 1 adapter in the list");
        }
        public async Task LoadAdaptersNameTooLongAsyncTest()
        {
            //Arrange 
            var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "am-LoadAdaptersNameTooLongAsyncTest" };
            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List<LogEntry>();
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return Task.FromResult(0);
                }
            };

            var longNameAdapter = new StubZvsAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet = () => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque magna diam, pellentesque et orci eget, pellentesque iaculis odio. Ut ultrices est sapien, ac pellentesque odio malesuada a. Etiam in neque ullamcorper massa gravida ullamcorper vel a posuere.",
                DescriptionGet = () => "",
                OnSettingsCreatingAdapterSettingBuilder = (s) => Task.FromResult(0),
                OnDeviceTypesCreatingDeviceTypeBuilder = (s) => Task.FromResult(0)
            };

            var adapterManager = new AdapterManager(new List<ZvsAdapter>() { longNameAdapter }, dbConnection, log);
            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert 
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error) == 1, "Expected 1 error");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Event handler for the refresh timer elapsed event.
        /// Refreshes the display.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="e">The <see cref="System.Timers.ElapsedEventArgs"/> instance containing the event data.</param>
        private void refreshDisplayEvent(object source, ElapsedEventArgs e)
        {
            _elapsedTimes++;
            if (AdapterManager != null)
            {
                if (_newMatrix || _elapsedTimes > _maxTickbeforRefresh)
                {
                    //System.Diagnostics.Debug.WriteLine(
                    //    "\t[" + DateTime.UtcNow.ToString("HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture)
                    //    + "]\t[SYNCH]");

                    _elapsedTimes = 0;
                    _newMatrix    = false;
                    AdapterManager.Synchronize(Matrix);
                }
                else if (_elapsedTimes % 30 == 0)
                {
                    if (!pins_locked && stack.Count == 0 && isRenderingNecessary())
                    {
                        RenderDisplay();
                    }
                    //System.Diagnostics.Debug.WriteLine(
                    //    "\t[" + DateTime.UtcNow.ToString("HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture)
                    //    + "]\t[SYNCH ignored]");
                }
            }
        }
Ejemplo n.º 5
0
        public async Task TestPropertyUpdatingOnDatabaseSettingChange()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var log = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    return(Task.FromResult(0));
                }
            };
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder  = s => Task.FromResult(0),
                OnSettingsCreatingAdapterSettingBuilder = s => Task.FromResult(0),
            };

            var adapter = new Adapter
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                Name        = "Unit Testing Adapter",
                Description = ""
            };

            adapter.Settings.Add(new AdapterSetting
            {
                UniqueIdentifier = "PropertyTest",
                Value            = "2",
                ValueType        = DataType.INTEGER
            });

            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);
            await adapterManager.StartAsync(CancellationToken.None);

            //act
            using (var context = new ZvsContext(dbConnection))
            {
                context.AdapterSettings.First().Value = "55";
                await context.SaveChangesAsync(CancellationToken.None);
            }

            //assert
            Assert.IsTrue(unitTestingAdapter.PropertyTest == 55, "The property test property on the zvsAdapter did not properly update when the database value was changed.");
        }
Ejemplo n.º 6
0
        public async Task StartTwiceTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());
            var logEntries = new List <LogEntry>();
            var log        = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    logEntries.Add(e);
                    Console.WriteLine(e.ToString());
                    return(Task.FromResult(0));
                }
            };

            var adapter = new Adapter()
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                IsEnabled   = true
            };

            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }
            var isStartedCount     = 0;
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder  = s => Task.FromResult(0),
                OnSettingsCreatingAdapterSettingBuilder = s => Task.FromResult(0),
                StartAsync01 = () =>
                {
                    isStartedCount++;
                    return(Task.FromResult(0));
                }
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);

            await adapterManager.StartAsync(CancellationToken.None);

            //assert
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Warn) == 1);
            Assert.IsTrue(isStartedCount == 1, "Adapter started too many or too few times");
        }
Ejemplo n.º 7
0
 public InternalPoderosaWorld(PoderosaStartupContext context) {
     _instance = this;
     _startupContext = context;
     _poderosaCulture = new PoderosaCulture();
     _poderosaLog = new PoderosaLog(this);
     _adapterManager = new AdapterManager();
     _stringResource = new StringResource("Poderosa.Plugin.strings", typeof(InternalPoderosaWorld).Assembly);
     _poderosaCulture.AddChangeListener(_stringResource);
     _pluginManager = new PluginManager(this);
     //ルート
     _rootExtension = _pluginManager.CreateExtensionPoint(ExtensionPoint.ROOT, typeof(IRootExtension), null);
 }
Ejemplo n.º 8
0
        public async Task LoadAdaptersAsyncTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List <LogEntry>();
            var log        = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return(Task.FromResult(0));
                }
            };

            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0)
            };

            unitTestingAdapter.OnSettingsCreatingAdapterSettingBuilder = async settingBuilder =>
            {
                var testSetting = new AdapterSetting
                {
                    Name        = "Test setting",
                    Value       = 360.ToString(CultureInfo.InvariantCulture),
                    ValueType   = DataType.INTEGER,
                    Description = "Unit testing only"
                };

                await
                settingBuilder.Adapter(unitTestingAdapter)
                .RegisterAdapterSettingAsync(testSetting, o => o.PropertyTest);
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error) == 0, "Expected 0 errors");
            Assert.IsTrue(unitTestingAdapter.PropertyTest == 360, "Expected TestSetting property to be 360");
        }
Ejemplo n.º 9
0
 public InternalPoderosaWorld(PoderosaStartupContext context)
 {
     _instance        = this;
     _startupContext  = context;
     _poderosaCulture = new PoderosaCulture();
     _poderosaLog     = new PoderosaLog(this);
     _adapterManager  = new AdapterManager();
     _stringResource  = new StringResource("Poderosa.Plugin.strings", typeof(InternalPoderosaWorld).Assembly);
     _poderosaCulture.AddChangeListener(_stringResource);
     _pluginManager = new PluginManager(this);
     //ルート
     _rootExtension = _pluginManager.CreateExtensionPoint(ExtensionPoint.ROOT, typeof(IRootExtension), null);
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Forces all connected adapter devices to recalibrate.
        /// </summary>
        /// <returns><c>true</c> if all adapter are successfully recalibrated</returns>
        public bool RecalibrateAll()
        {
            bool result = true;

            if (AdapterManager != null)
            {
                foreach (var adapter in AdapterManager.GetAdapters())
                {
                    result &= adapter.Recalibrate(0);
                }
            }
            return(result);
        }
        /// <summary>
        /// Tries to sent the actual build matrix to all devices, that are active.
        /// To enable a sending, the pins have to be unlocked (still rendering or maybe locked by the user)
        /// and at an Adapter has to be active.
        /// </summary>
        public void RefreshDisplay(bool rerender = true)
        {
            if (ArePinsLocked())
            {
                return;
            }
            if (AdapterManager != null && AdapterManager.ActiveAdapter != null && Matrix != null)
            {
                if (Matrix == null || rerender)
                {
                    RenderDisplay();
                }

                AdapterManager.Synchronize(this.Matrix.Clone() as bool[, ]);
            }
            //System.GC.Collect();
        }
Ejemplo n.º 12
0
        public async Task DisableAdapterAsyncNotFoundTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var log            = new StubIFeedback <LogEntry>();
            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
            }, dbConnection, log);

            //act
            var result = await adapterManager.DisableAdapterAsync(Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"), CancellationToken.None);

            //assert
            Assert.IsTrue(result.HasError, result.Message);
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Theseus.Core"/> class.
 /// </summary>
 /// <param name="configurationFileName">Configuration file name.</param>
 /// <param name="accountsFileName">Accounts file name.</param>
 public Core(String configurationFileName, String accountsFileName) {
     AppDomain.CurrentDomain.UnhandledException += 
         new UnhandledExceptionEventHandler(delegate(object sender, UnhandledExceptionEventArgs e) {
             Logger.Error(e.ExceptionObject);
         });
     Logger = LogManager.GetLogger("Core");
     Logger.Info("Core initializing...");
     Logger.Info("Configuration initializing...");
     configuration = Configuration.Parse(configurationFileName, cancellationTokenSource.Token).Result;
     Logger.Info("Adapter manager initializing...");
     adapterManager = new AdapterManager(this, configuration.Adapters);
     adapterManager.PluginsDirectory = "./Adapters";
     Logger.Info("Handler manager initializing...");
     handlerManager = new HandlerManager(this, configuration.Handlers);
     handlerManager.PluginsDirectory = "./Handlers";
     Logger.Info("Accounts initializing...");
     accounts = new Accounts(accountsFileName, cancellationTokenSource.Token);
     Logger.Info("Core initialized");
 }
Ejemplo n.º 14
0
        public async Task FindZvsAdapterAsyncTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List <LogEntry>();
            var log        = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return(Task.FromResult(0));
                }
            };

            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder  = s => Task.FromResult(0),
                OnSettingsCreatingAdapterSettingBuilder = s => Task.FromResult(0),
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);
            await adapterManager.StartAsync(CancellationToken.None);

            //act
            var adapter = adapterManager.FindZvsAdapter(1);


            //assert
            Assert.IsNotNull(adapter, "Registered adapter not found!");
            Assert.IsTrue(adapter.AdapterGuid == unitTestingAdapter.AdapterGuid, "Found wrong adapter!");
        }
Ejemplo n.º 15
0
        public async Task DisableAdapterAsyncTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var log                = new StubIFeedback <LogEntry>();
            var hasStopped         = false;
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0),
                StopAsync01 = async() => hasStopped = true
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);

            var adapter = new Adapter
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
            };

            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }

            //act
            var result = await adapterManager.DisableAdapterAsync(Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"), CancellationToken.None);

            //assert
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsTrue(hasStopped, "Expected adapter StopAsync to be called.");
        }
Ejemplo n.º 16
0
        public async Task LoadAdaptersNameTooLongAsyncTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List <LogEntry>();
            var log        = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return(Task.FromResult(0));
                }
            };

            var longNameAdapter = new StubZvsAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque magna diam, pellentesque et orci eget, pellentesque iaculis odio. Ut ultrices est sapien, ac pellentesque odio malesuada a. Etiam in neque ullamcorper massa gravida ullamcorper vel a posuere.",
                DescriptionGet = () => "",
                OnSettingsCreatingAdapterSettingBuilder = s => Task.FromResult(0),
                OnDeviceTypesCreatingDeviceTypeBuilder  = s => Task.FromResult(0)
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter>()
            {
                longNameAdapter
            }, dbConnection, log);
            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error) == 1, "Expected 1 error");
        }
        public async Task DisableAdapterAsyncTest()
        {
            //Arrange 
            var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "am-DisableAdapterAsyncTest" };
            Database.SetInitializer(new CreateFreshDbInitializer());

            var log = new StubIFeedback<LogEntry>();
            var hasStopped = false;
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = (s) => Task.FromResult(0),
                StopAsync01 = async () => hasStopped = true
            };

            var adapterManager = new AdapterManager(new List<ZvsAdapter> { unitTestingAdapter }, dbConnection, log);

            var adapter = new Adapter
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
            };
            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }

            //act
            var result = await adapterManager.DisableAdapterAsync(Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"), CancellationToken.None);

            //assert 
            Assert.IsFalse(result.HasError, result.Message);
            Assert.IsTrue(hasStopped, "Expected adapter StopAsync to be called.");
        }
Ejemplo n.º 18
0
        public async Task LoadAdaptersAsyncAutoStartTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var log = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    return(Task.FromResult(0));
                }
            };

            var adapter = new Adapter()
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                IsEnabled   = true
            };

            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }
            var isStarted          = false;
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0),
                StartAsync01 = () =>
                {
                    isStarted = true;
                    return(Task.FromResult(0));
                }
            };

            unitTestingAdapter.OnSettingsCreatingAdapterSettingBuilder = async settingBuilder =>
            {
                var testSetting = new AdapterSetting
                {
                    Name        = "Test setting",
                    Value       = 360.ToString(CultureInfo.InvariantCulture),
                    ValueType   = DataType.INTEGER,
                    Description = "Unit testing only"
                };

                await
                settingBuilder.Adapter(unitTestingAdapter)
                .RegisterAdapterSettingAsync(testSetting, o => o.PropertyTest);
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert
            Assert.IsTrue(isStarted, "Adapter not started!");
        }
        public async Task LoadAdaptersAsyncTest()
        {
            //Arrange 
            var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "am-LoadAdaptersAsyncTest" };
            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List<LogEntry>();
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return Task.FromResult(0);
                }
            };

            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = (s) => Task.FromResult(0)
            };

            unitTestingAdapter.OnSettingsCreatingAdapterSettingBuilder = async (settingBuilder) =>
            {
                var testSetting = new AdapterSetting
                {
                    Name = "Test setting",
                    Value = 360.ToString(CultureInfo.InvariantCulture),
                    ValueType = DataType.INTEGER,
                    Description = "Unit testing only"
                };

                await
                    settingBuilder.Adapter(unitTestingAdapter)
                        .RegisterAdapterSettingAsync(testSetting, o => o.PropertyTest);
            };

            var adapterManager = new AdapterManager(new List<ZvsAdapter> { unitTestingAdapter }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert 
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error) == 0, "Expected 0 errors");
            Assert.IsTrue(unitTestingAdapter.PropertyTest == 360, "Expected TestSetting property to be 360");
        }
Ejemplo n.º 20
0
 public AdapterLoader(SMAP_State parSmap, DEV9_State parDev9)
 {
     dev9 = parDev9;
     net = new AdapterManager(parSmap);
 }
        public async Task FindZvsAdapterInvalidIdAsyncTest()
        {
            //Arrange 
            var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "am-FindZvsAdapterInvalidIdAsyncTest" };
            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List<LogEntry>();
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return Task.FromResult(0);
                }
            };

            var adapterManager = new AdapterManager(new List<ZvsAdapter>(), dbConnection, log);
            await adapterManager.StartAsync(CancellationToken.None);

            //act
            var adapter = adapterManager.FindZvsAdapter(1);

            //assert 
            Assert.IsNull(adapter, "Found a adapter?");
        }
        public async Task TestPropertyUpdatingOnDatabaseSettingChange()
        {
            //Arrange 
            var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "am-TestPropertyUpdatingOnDatabaseSettingChange" };
            Database.SetInitializer(new CreateFreshDbInitializer());

            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    return Task.FromResult(0);
                }
            };
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = (s) => Task.FromResult(0),
                OnSettingsCreatingAdapterSettingBuilder = (s) => Task.FromResult(0),
            };

            var adapter = new Adapter
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                Name = "Unit Testing Adapter",
                Description = ""
            };
            adapter.Settings.Add(new AdapterSetting
            {
                UniqueIdentifier = "PropertyTest",
                Value = "2",
                ValueType = DataType.INTEGER
            });

            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }

            var adapterManager = new AdapterManager(new List<ZvsAdapter> { unitTestingAdapter }, dbConnection, log);
            await adapterManager.StartAsync(CancellationToken.None);
            //act
            using (var context = new ZvsContext(dbConnection))
            {
                context.AdapterSettings.First().Value = "55";
                await context.SaveChangesAsync(CancellationToken.None);
            }

            //assert 
            Assert.IsTrue(unitTestingAdapter.PropertyTest == 55, "The property test property on the zvsAdapter did not properly update when the database value was changed.");
        }
        public async Task StopWhenNotStartedTest()
        {
            //Arrange 
            var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "am-StopWhenNotStartedTest" };
            Database.SetInitializer(new CreateFreshDbInitializer());
            var logEntries = new List<LogEntry>();
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    logEntries.Add(e);
                    Console.WriteLine(e.ToString());
                    return Task.FromResult(0);
                }
            };

            var adapter = new Adapter()
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                IsEnabled = true
            };
            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = (s) => Task.FromResult(0)
               
            };

            var adapterManager = new AdapterManager(new List<ZvsAdapter> { unitTestingAdapter }, dbConnection, log);

            //act
            await adapterManager.StopAsync(CancellationToken.None);

            //assert 
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Warn) == 1);
        }
        public async Task FindZvsAdapterAsyncTest()
        {
            //Arrange 
            var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "am-FindZvsAdapterAsyncTest" };
            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List<LogEntry>();
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return Task.FromResult(0);
                }
            };

            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = (s) => Task.FromResult(0),
                OnSettingsCreatingAdapterSettingBuilder = (s) => Task.FromResult(0),
            };

            var adapterManager = new AdapterManager(new List<ZvsAdapter> { unitTestingAdapter }, dbConnection, log);
            await adapterManager.StartAsync(CancellationToken.None);

            //act
            var adapter = adapterManager.FindZvsAdapter(1);


            //assert 
            Assert.IsNotNull(adapter, "Registered adapter not found!");
            Assert.IsTrue(adapter.AdapterGuid == unitTestingAdapter.AdapterGuid, "Found wrong adapter!");
        }
        public async Task DisableAdapterAsyncNotFoundTest()
        {
            //Arrange 
            var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "am-DisableAdapterAsyncNotFoundTest" };
            Database.SetInitializer(new CreateFreshDbInitializer());

            var log = new StubIFeedback<LogEntry>();
            var adapterManager = new AdapterManager(new List<ZvsAdapter> { }, dbConnection, log);

            //act
            var result = await adapterManager.DisableAdapterAsync(Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"), CancellationToken.None);

            //assert 
            Assert.IsTrue(result.HasError, result.Message);
        }
        public async Task LoadAdaptersInvalidCastAsyncTest()
        {
            //Arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List<LogEntry>();
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return Task.FromResult(0);
                }
            };

            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0)
            };

            unitTestingAdapter.OnSettingsCreatingAdapterSettingBuilder = async settingBuilder =>
            {
                var testSetting = new AdapterSetting
                {
                    Name = "Test setting",
                    Value = "abc",
                    ValueType = DataType.INTEGER,
                    Description = "Unit testing only"
                };

                await
                    settingBuilder.Adapter(unitTestingAdapter)
                        .RegisterAdapterSettingAsync(testSetting, o => o.PropertyTest);
            };

            var adapterManager = new AdapterManager(new List<ZvsAdapter> { unitTestingAdapter }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert 
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error && o.Message.Contains("Cannot cast value")) == 1, "Expected 1 Cannot cast value on adapter setting error");
        }
Ejemplo n.º 27
0
        public async Task LoadAdaptersAsyncBadPropertyTest()
        {
            //Arrange
            var dbConnection = new UnitTestDbConnection();

            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List <LogEntry>();
            var log        = new StubIFeedback <LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return(Task.FromResult(0));
                }
            };

            var badSettings = new AdapterSetting
            {
                Name             = "Test setting2",
                Value            = 360.ToString(CultureInfo.InvariantCulture),
                ValueType        = DataType.STRING,
                Description      = "Unit testing only",
                UniqueIdentifier = "NotExistantPropertyName"
            };

            var adapter = new Adapter()
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
            };

            adapter.Settings.Add(badSettings);
            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }

            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet        = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0)
            };

            unitTestingAdapter.OnSettingsCreatingAdapterSettingBuilder = async settingBuilder =>
            {
                var testSetting = new AdapterSetting
                {
                    Name        = "Test setting",
                    Value       = 360.ToString(CultureInfo.InvariantCulture),
                    ValueType   = DataType.INTEGER,
                    Description = "Unit testing only"
                };

                await
                settingBuilder.Adapter(unitTestingAdapter)
                .RegisterAdapterSettingAsync(testSetting, o => o.PropertyTest);
            };

            var adapterManager = new AdapterManager(new List <ZvsAdapter> {
                unitTestingAdapter
            }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert
            Assert.IsTrue(unitTestingAdapter.PropertyTest == 360, "Expected TestSetting property to be 360");
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error && o.Message.Contains("Cannot find property")) == 1, "Expected 1 Cannot find property error");
        }
        public async Task LoadAdaptersAsyncAutoStartTest()
        {
            //Arrange 
            var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "am-LoadAdaptersAsyncAutoStartTest" };
            Database.SetInitializer(new CreateFreshDbInitializer());

            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    return Task.FromResult(0);
                }
            };

            var adapter = new Adapter()
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                IsEnabled = true
            };
            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }
            var isStarted = false;
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = (s) => Task.FromResult(0),
                StartAsync01 = () =>
                {
                    isStarted = true;
                    return Task.FromResult(0);
                }
            };
            unitTestingAdapter.OnSettingsCreatingAdapterSettingBuilder = async (settingBuilder) =>
            {
                var testSetting = new AdapterSetting
                {
                    Name = "Test setting",
                    Value = 360.ToString(CultureInfo.InvariantCulture),
                    ValueType = DataType.INTEGER,
                    Description = "Unit testing only"
                };

                await
                    settingBuilder.Adapter(unitTestingAdapter)
                        .RegisterAdapterSettingAsync(testSetting, o => o.PropertyTest);
            };

            var adapterManager = new AdapterManager(new List<ZvsAdapter> { unitTestingAdapter }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert 
            Assert.IsTrue(isStarted, "Adapter not started!");
        }
        public async Task GetZvsAdaptersTest()
        {
            //Arrange 
            var dbConnection = new StubIEntityContextConnection { NameOrConnectionStringGet = () => "am-GetZvsAdapters" };
            Database.SetInitializer(new CreateFreshDbInitializer());

            var log = new StubIFeedback<LogEntry>();
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = (s) => Task.FromResult(0)
            };

            var adapterManager = new AdapterManager(new List<ZvsAdapter> { unitTestingAdapter }, dbConnection, log);

            //act
            var result = adapterManager.GetZvsAdapters();

            //assert 
            Assert.IsTrue(result.Count() == 1, "Expected 1 adapter in the list");
        }
        public async Task StopTest()
        {
            //Arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());
            var logEntries = new List<LogEntry>();
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    logEntries.Add(e);
                    Console.WriteLine(e.ToString());
                    return Task.FromResult(0);
                }
            };

            var adapter = new Adapter()
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                IsEnabled = true
            };
            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }
            var isStartedCount = 0;
            var isStoppedCount = 0;
            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0),
                OnSettingsCreatingAdapterSettingBuilder = s => Task.FromResult(0),
                StartAsync01 = () =>
                {
                    isStartedCount++;
                    return Task.FromResult(0);
                },
                StopAsync01 = () =>
                {
                    isStoppedCount++;
                    return Task.FromResult(0);
                }
            };

            var adapterManager = new AdapterManager(new List<ZvsAdapter> { unitTestingAdapter }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);
            await adapterManager.StopAsync(CancellationToken.None);

            //assert 
            Assert.IsTrue(logEntries.All(o => o.Level == LogEntryLevel.Info), "Not all log entries are info level");
            Assert.IsTrue(isStartedCount == 1, "Plugin started too many or too few times");
            Assert.IsTrue(isStoppedCount == 1, "Plugin stopped too many or too few times");
        }
        public async Task LoadAdaptersAsyncBadPropertyTest()
        {
            //Arrange 
            var dbConnection = new UnitTestDbConnection();
            Database.SetInitializer(new CreateFreshDbInitializer());

            var logEntries = new List<LogEntry>();
            var log = new StubIFeedback<LogEntry>
            {
                ReportAsyncT0CancellationToken = (e, c) =>
                {
                    Console.WriteLine(e.ToString());
                    logEntries.Add(e);
                    return Task.FromResult(0);
                }
            };

            var badSettings = new AdapterSetting
            {
                Name = "Test setting2",
                Value = 360.ToString(CultureInfo.InvariantCulture),
                ValueType = DataType.STRING,
                Description = "Unit testing only",
                UniqueIdentifier = "NotExistantPropertyName"

            };

            var adapter = new Adapter()
            {
                AdapterGuid = Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
            };
            adapter.Settings.Add(badSettings);
            using (var context = new ZvsContext(dbConnection))
            {
                context.Adapters.Add(adapter);
                await context.SaveChangesAsync(CancellationToken.None);
            }

            var unitTestingAdapter = new StubUnitTestAdapter
            {
                AdapterGuidGet = () => Guid.Parse("a0f912a6-b8bb-406a-360f-1eb13f50aae4"),
                NameGet = () => "Unit Testing Adapter",
                DescriptionGet = () => "",
                OnDeviceTypesCreatingDeviceTypeBuilder = s => Task.FromResult(0)
            };

            unitTestingAdapter.OnSettingsCreatingAdapterSettingBuilder = async settingBuilder =>
            {
                var testSetting = new AdapterSetting
                {
                    Name = "Test setting",
                    Value = 360.ToString(CultureInfo.InvariantCulture),
                    ValueType = DataType.INTEGER,
                    Description = "Unit testing only"
                };

                await
                    settingBuilder.Adapter(unitTestingAdapter)
                        .RegisterAdapterSettingAsync(testSetting, o => o.PropertyTest);
            };

            var adapterManager = new AdapterManager(new List<ZvsAdapter> { unitTestingAdapter }, dbConnection, log);

            //act
            await adapterManager.StartAsync(CancellationToken.None);

            //assert 
            Assert.IsTrue(unitTestingAdapter.PropertyTest == 360, "Expected TestSetting property to be 360");
            Assert.IsTrue(logEntries.Count(o => o.Level == LogEntryLevel.Error && o.Message.Contains("Cannot find property")) == 1, "Expected 1 Cannot find property error");
        }