Ejemplo n.º 1
0
        public static void SetupRepositoryMock <T>(this Mock <Repository <T> > repositoryMock, Action <IRepository <T> > options) where T : BaseEntity
        {
            var context = new AppDashTestContext();

            context.Database.EnsureDeleted();

            var constructorArguments = repositoryMock.GetType().GetField("constructorArguments",
                                                                         BindingFlags.NonPublic | BindingFlags.Instance);

            constructorArguments?.SetValue(repositoryMock, new object[] { context });

            repositoryMock.Protected().SetupGet <DbSet <T> >("Entities").Returns(context.Set <T>());
            repositoryMock.Setup(repository => repository.Table).Returns(context.Set <T>());
            repositoryMock.Setup(repository => repository.TableNoTracking).Returns(context.Set <T>().AsNoTracking);

            options.Invoke(repositoryMock.Object);

            context.SaveChanges();
        }
Ejemplo n.º 2
0
        public void TryMatchTest(string pluginKey, string route, bool expectedResult, Type expectedController, string expectedMethodName)
        {
            //Arrange
            AppDashTestContext dbContext = new AppDashTestContext();

            dbContext.Database.EnsureDeleted();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <IRepository <Plugin>, Repository <Plugin> >(e => _pluginRepositoryMock.Object);
            serviceCollection.AddSingleton <IRepository <Tile>, Repository <Tile> >(e => _tileRepositoryMock.Object);
            serviceCollection.AddSingleton <IRepository <PluginSettings>, Repository <PluginSettings> >(e => _pluginSettingsRepositoryMock.Object);
            serviceCollection.AddSingleton <DbContext, AppDashTestContext>(e => dbContext);

            _pluginRepositoryMock.SetupRepositoryMock(dbContext, options =>
            {
                options.InsertNoTracking(new Plugin
                {
                    UniqueIdentifier = typeof(TestPlugin.TestPlugin).FullName,
                    Key  = "TestKey",
                    Name = "TestName"
                });
            });

            _tileRepositoryMock.SetupRepositoryMock(dbContext, options =>
                                                    { });

            _pluginSettingsRepositoryMock.SetupRepositoryMock(dbContext, options =>
            {
                options.InsertNoTracking(new PluginSettings
                {
                    PluginKey = "TestKey",
                    Data      = JsonConvert.SerializeObject(new PluginData
                    {
                        Data = new Dictionary <string, Tuple <Type, object> >(new List <KeyValuePair <string, Tuple <Type, object> > >
                        {
                            new KeyValuePair <string, Tuple <Type, object> >("TestData", new Tuple <Type, object>(typeof(string), "TestValue")),
                            new KeyValuePair <string, Tuple <Type, object> >("TestData2", new Tuple <Type, object>(typeof(string), "TestValue2"))
                        })
                    })
                });
            });

            var serviceProvider = serviceCollection.BuildServiceProvider();

            var pluginResolver = new PluginResolver();

            var pluginSettingsManager = new PluginSettingsManager(serviceProvider);

            PluginLoader pluginLoader = new PluginLoader(pluginResolver, serviceProvider);

            var pluginInitializer = new PluginInitializer(pluginResolver, pluginSettingsManager, serviceProvider);

            var pluginManager = new PluginManager(pluginResolver, pluginLoader, pluginInitializer);

            PluginControllerMatcher pluginControllerMatcher = new PluginControllerMatcher(pluginManager);

            //Act
            pluginLoader.LoadPlugins(Path.Combine(Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath), "plugins"));

            pluginInitializer.InitializePlugins();

            bool result = pluginControllerMatcher.TryMatch(pluginKey, route, out (IPluginController, MethodInfo)controller);

            //Assert
            Assert.Equal(expectedResult, result);

            if (expectedResult)
            {
                Assert.NotNull(controller.Item1);
                Assert.NotNull(controller.Item2);
                Assert.Equal(expectedController.FullName, controller.Item1.GetType().FullName);
                Assert.Equal(expectedMethodName, controller.Item2.Name);
            }
            else
            {
                Assert.Null(controller.Item1);
                Assert.Null(controller.Item2);
            }
        }
Ejemplo n.º 3
0
        public void InitializePluginsTest()
        {
            //Arrange
            AppDashTestContext dbContext = new AppDashTestContext();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <IRepository <Plugin>, Repository <Plugin> >(e => _pluginRepositoryMock.Object);
            serviceCollection.AddSingleton <IRepository <Tile>, Repository <Tile> >(e => _tileRepositoryMock.Object);
            serviceCollection.AddSingleton <IRepository <PluginSettings>, Repository <PluginSettings> >(e => _pluginSettingsRepositoryMock.Object);
            serviceCollection.AddSingleton <DbContext, AppDashTestContext>(e => dbContext);

            _pluginRepositoryMock.SetupRepositoryMock(dbContext, options =>
            {
                options.InsertNoTracking(new Plugin
                {
                    UniqueIdentifier = typeof(TestPlugin.TestPlugin).FullName,
                    Key  = "TestKey",
                    Name = "TestName"
                });
            });

            _tileRepositoryMock.SetupRepositoryMock(dbContext, options =>
                                                    { });

            _pluginSettingsRepositoryMock.SetupRepositoryMock(dbContext, options =>
            {
                options.InsertNoTracking(new PluginSettings
                {
                    PluginKey = "TestKey",
                    Data      = JsonConvert.SerializeObject(new PluginData
                    {
                        Data = new Dictionary <string, Tuple <Type, object> >(new List <KeyValuePair <string, Tuple <Type, object> > >
                        {
                            new KeyValuePair <string, Tuple <Type, object> >("TestData", new Tuple <Type, object>(typeof(string), "TestValue")),
                            new KeyValuePair <string, Tuple <Type, object> >("TestData2", new Tuple <Type, object>(typeof(string), "TestValue2"))
                        })
                    })
                });
            });

            var serviceProvider = serviceCollection.BuildServiceProvider();

            var pluginResolver        = new PluginResolver();
            var pluginSettingsManager = new PluginSettingsManager(serviceProvider);

            PluginLoader pluginLoader = new PluginLoader(pluginResolver, serviceProvider);

            PluginInitializer pluginInitializer = new PluginInitializer(pluginResolver, pluginSettingsManager, serviceProvider);

            //Act
            pluginLoader.LoadPlugins(Path.Combine(Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath), "plugins"));

            pluginInitializer.InitializePlugins();

            //Assert
            var plugin = pluginResolver.GetPlugin("TestKey");

            //make sure the plugin exists
            Assert.NotNull(plugin);
            //make sure the plugin has the right key
            Assert.Equal("TestKey", plugin.PluginInstance.Key);
            Assert.Equal("TestKey", plugin.PluginKey);

            foreach (ITile pluginTile in pluginResolver.GetPluginTiles())
            {
                //make sure tiles have correct data
                Assert.Equal("TestKey", pluginTile.PluginKey);
                Assert.NotEmpty(pluginTile.TileKey);
                Assert.NotNull(pluginTile.PluginSettings);
                Assert.NotNull(pluginTile.PluginSettings.GetData <string>("TestData"));
                Assert.NotNull(pluginTile.PluginSettings.GetData <string>("TestData2"));
                Assert.Throws <InvalidCastException>(() =>
                {
                    //make sure this throws an InvalidCastException since it should be a string
                    int data = pluginTile.PluginSettings.GetData <int>("TestData");
                });
            }
        }
Ejemplo n.º 4
0
        public void GetPluginSettingsTest(string pluginKey, bool shouldHaveData)
        {
            //Arrange
            AppDashTestContext dbContext = new AppDashTestContext();

            dbContext.Database.EnsureDeleted();

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton <IRepository <Plugin>, Repository <Plugin> >(e => _pluginRepositoryMock.Object);
            serviceCollection.AddSingleton <IRepository <Tile>, Repository <Tile> >(e => _tileRepositoryMock.Object);
            serviceCollection.AddSingleton <IRepository <PluginSettings>, Repository <PluginSettings> >(e => _pluginSettingsRepositoryMock.Object);
            serviceCollection.AddSingleton <DbContext, AppDashTestContext>(e => dbContext);

            _pluginRepositoryMock.SetupRepositoryMock(dbContext, options =>
            {
                options.InsertNoTracking(new Plugin
                {
                    UniqueIdentifier = typeof(TestPlugin.TestPlugin).FullName,
                    Key  = "TestKey",
                    Name = "TestName"
                });
            });

            _tileRepositoryMock.SetupRepositoryMock(dbContext, options =>
                                                    { });

            _pluginSettingsRepositoryMock.SetupRepositoryMock(dbContext, options =>
            {
                options.InsertNoTracking(new PluginSettings
                {
                    PluginKey = "TestKey",
                    Data      = JsonConvert.SerializeObject(new PluginData
                    {
                        Data = new Dictionary <string, Tuple <Type, object> >(new List <KeyValuePair <string, Tuple <Type, object> > >
                        {
                            new KeyValuePair <string, Tuple <Type, object> >("TestData", new Tuple <Type, object>(typeof(string), "TestValue")),
                            new KeyValuePair <string, Tuple <Type, object> >("TestData2", new Tuple <Type, object>(typeof(string), "TestValue2"))
                        })
                    })
                });
            });

            var serviceProvider = serviceCollection.BuildServiceProvider();

            var pluginResolver = new PluginResolver();

            PluginLoader pluginLoader = new PluginLoader(pluginResolver, serviceProvider);

            PluginSettingsManager pluginSettingsManager = new PluginSettingsManager(serviceProvider);

            var pluginInitializer = new PluginInitializer(pluginResolver, pluginSettingsManager, serviceProvider);

            //Act
            pluginLoader.LoadPlugins(Path.Combine(Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath), "plugins"));

            pluginInitializer.InitializePlugins();

            var pluginData = pluginSettingsManager.GetPluginSettings(pluginKey);

            //Assert
            if (shouldHaveData)
            {
                Assert.NotNull(pluginData);

                var testData  = pluginData.GetData <string>("TestData");
                var testData2 = pluginData.GetData <string>("TestData2");

                Assert.NotEmpty(testData);
                Assert.NotEmpty(testData2);
                Assert.Equal(2, pluginData.Data.Count);

                var defaultData = pluginData.GetData <string>("InvalidData");

                Assert.Equal(default, defaultData);