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 ExecuteTest(string pluginKey, string route, object expectedResult) { //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); //the test controllers return a ObjectResult, we cast it so we can easily check the value below var actionResult = (ObjectResult)pluginControllerMatcher.Execute(pluginKey, route, null, controller.Item1, controller.Item2); //Assert Assert.True(result); Assert.Equal(expectedResult, actionResult.Value); }