public IActionResult ExecutePluginAction(string pluginKey, string route) { route = "/" + route; if (_pluginControllerMatcher.TryMatch(pluginKey, route, out (IPluginController, MethodInfo)pluginController)) { return(_pluginControllerMatcher.Execute(pluginKey, route, Request.Body, pluginController.Item1, pluginController.Item2)); } return(ApiResult.NotFound()); }
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); } }