use of au.gov.asd.tac.constellation.plugins.PluginSynchronizer in project constellation by constellation-app.
the class QueryPhasePaneNGTest method testRunPlugins.
@Test
public void testRunPlugins() {
// Initialize the current graph in the state.
DataAccessPaneState.setCurrentGraphId("GraphId");
final QueryPhasePane instance = spy(new QueryPhasePane(new HashMap<>(), null, null));
// Setup the graph manager
final GraphManager graphManager = mock(GraphManager.class);
final Graph graph = mock(Graph.class);
when(graphManager.getActiveGraph()).thenReturn(graph);
// Setup the global parameters
final GlobalParametersPane globalParametersPane = mock(GlobalParametersPane.class);
final PluginParameters globalPluginParameters = mock(PluginParameters.class);
final PluginParameter globalPluginParameter1 = mock(PluginParameter.class);
final PluginParameter globalPluginParameter2 = mock(PluginParameter.class);
doReturn(globalParametersPane).when(instance).getGlobalParametersPane();
when(globalParametersPane.getParams()).thenReturn(globalPluginParameters);
when(globalPluginParameters.getParameters()).thenReturn(Map.of("abc.parameter1", globalPluginParameter1, "global.parameter1", globalPluginParameter2));
when(globalPluginParameter1.getObjectValue()).thenReturn("GLOBAL PARAMETER 1");
// Setup data access panes
final DataSourceTitledPane dataSourceTitledPane1 = mock(DataSourceTitledPane.class);
final DataSourceTitledPane dataSourceTitledPane2 = mock(DataSourceTitledPane.class);
doReturn(List.of(dataSourceTitledPane1, dataSourceTitledPane2)).when(instance).getDataAccessPanes();
// Pane 1 is disabled so will not be run
when(dataSourceTitledPane1.isQueryEnabled()).thenReturn(false);
when(dataSourceTitledPane2.isQueryEnabled()).thenReturn(true);
// Setup the plugin for pane 2
final Plugin plugin = mock(Plugin.class);
when(plugin.getName()).thenReturn("Plugin Name");
when(dataSourceTitledPane2.getPlugin()).thenReturn(plugin);
// Pane 2 has two parameters. One of them matches in name to one of
// the global parameters which means its value will be overriden with
// the global value.
final PluginParameters pluginParameters = mock(PluginParameters.class);
final PluginParameter pluginParameter1 = mock(PluginParameter.class);
final PluginParameter pluginParameter2 = mock(PluginParameter.class);
when(pluginParameters.getParameters()).thenReturn(Map.of("abc.parameter1", pluginParameter1, "abc.parameter2", pluginParameter2));
when(pluginParameters.copy()).thenReturn(pluginParameters);
when(dataSourceTitledPane2.getParameters()).thenReturn(pluginParameters);
try (final MockedStatic<PluginRegistry> pluginRegistry = Mockito.mockStatic(PluginRegistry.class);
final MockedStatic<PluginExecution> pluginExecutionMockedStatic = Mockito.mockStatic(PluginExecution.class);
final MockedStatic<GraphManager> graphManagerMockedStatic = Mockito.mockStatic(GraphManager.class);
final MockedConstruction<PluginSynchronizer> pluginSynchMocks = Mockito.mockConstruction(PluginSynchronizer.class, (pluginSyncMock, cnxt) -> {
assertEquals(cnxt.arguments(), List.of(1));
})) {
// Not sure why this is being done but just retuning the same plugin
// to save creating another mock.
pluginRegistry.when(() -> PluginRegistry.get(plugin.getClass().getName())).thenReturn(plugin);
graphManagerMockedStatic.when(GraphManager::getDefault).thenReturn(graphManager);
// This is the future that will be returned when the plugin begins execution
final Future future = CompletableFuture.completedFuture("Plugin Complete!");
// This is the future of a plugin that was run previously
final Future existingFuture = CompletableFuture.completedFuture("Previous Plugin Complete!");
final PluginExecution pluginExecution = mock(PluginExecution.class);
pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(plugin)).thenReturn(pluginExecution);
// We don't need to verify any of these again (with the exception of the plugin synchronizer)
// because a null pointer will happen if any of the params don't match up.
when(pluginExecution.withParameters(pluginParameters)).thenReturn(pluginExecution);
when(pluginExecution.waitingFor(List.of(existingFuture))).thenReturn(pluginExecution);
when(pluginExecution.synchronizingOn(any(PluginSynchronizer.class))).thenReturn(pluginExecution);
when(pluginExecution.executeLater(graph)).thenReturn(future);
// Verify that the return contains the plugin future as defined above
assertEquals(instance.runPlugins(List.of(existingFuture)), List.of(future));
// Verify the state's running plugin list has been updated
assertEquals(DataAccessPaneState.getRunningPlugins(), Map.of(future, "Plugin Name"));
// Verify the local plugin parameter was updated with the global parameter value
verify(pluginParameter1).setObjectValue("GLOBAL PARAMETER 1");
// Verify the created plugin synchronizer is passed to the plugin execution
verify(pluginExecution).synchronizingOn(pluginSynchMocks.constructed().get(0));
}
}
use of au.gov.asd.tac.constellation.plugins.PluginSynchronizer in project constellation by constellation-app.
the class DefaultPluginEnvironmentNGTest method testExecutePluginLaterWithAsyncThrowsExecutionException.
@Test
public void testExecutePluginLaterWithAsyncThrowsExecutionException() throws ExecutionException, InterruptedException {
System.out.println("executePluginLater");
Graph graph = mock(Graph.class);
Plugin plugin = mock(Plugin.class);
PluginParameters parameters = mock(PluginParameters.class);
boolean interactive = false;
PluginSynchronizer synchronizer = mock(PluginSynchronizer.class);
Future<Object> mockedFuture = mock(Future.class);
List<Future<?>> async = Arrays.asList(mockedFuture);
when(mockedFuture.get()).thenThrow(ExecutionException.class);
final ExecutorService executorService = mock(ExecutorService.class);
DefaultPluginEnvironment instance = spy(new DefaultPluginEnvironment());
doReturn(executorService).when(instance).getPluginExecutor();
when(executorService.submit(any(Callable.class))).thenAnswer(iom -> {
final Callable callable = iom.getArgument(0);
callable.call();
return CompletableFuture.completedFuture(null);
});
Object expResult = null;
Future future = instance.executePluginLater(graph, plugin, parameters, interactive, async, synchronizer);
Object result = future.get();
assertEquals(result, expResult);
}
use of au.gov.asd.tac.constellation.plugins.PluginSynchronizer in project constellation by constellation-app.
the class DefaultPluginEnvironmentNGTest method testExecutePluginLaterThrowsRuntimeException.
@Test
public void testExecutePluginLaterThrowsRuntimeException() throws ExecutionException, InterruptedException, PluginException {
System.out.println("executePluginLater");
Graph graph = mock(Graph.class);
Plugin plugin = mock(Plugin.class);
PluginParameters parameters = mock(PluginParameters.class);
boolean interactive = false;
PluginSynchronizer synchronizer = mock(PluginSynchronizer.class);
List<Future<?>> async = null;
RuntimeException runtimeException = mock(RuntimeException.class);
final ExecutorService executorService = mock(ExecutorService.class);
doThrow(runtimeException).when(plugin).run(any(PluginGraphs.class), any(PluginInteraction.class), any(PluginParameters.class));
DefaultPluginEnvironment instance = spy(new DefaultPluginEnvironment());
doReturn(executorService).when(instance).getPluginExecutor();
when(executorService.submit(any(Callable.class))).thenAnswer(iom -> {
final Callable callable = iom.getArgument(0);
callable.call();
return CompletableFuture.completedFuture(null);
});
Object expResult = null;
Future future = instance.executePluginLater(graph, plugin, parameters, interactive, async, synchronizer);
Object result = future.get();
assertEquals(result, expResult);
}
use of au.gov.asd.tac.constellation.plugins.PluginSynchronizer in project constellation by constellation-app.
the class DefaultPluginEnvironmentNGTest method testExecutePluginLaterWithNullAsync.
/**
* Test of executePluginLater method, of class DefaultPluginEnvironment.
*/
@Test
public void testExecutePluginLaterWithNullAsync() throws ExecutionException, InterruptedException {
System.out.println("executePluginLater");
Graph graph = mock(Graph.class);
Plugin plugin = mock(Plugin.class);
PluginParameters parameters = mock(PluginParameters.class);
boolean interactive = false;
PluginSynchronizer synchronizer = mock(PluginSynchronizer.class);
List<Future<?>> async = null;
final ExecutorService executorService = mock(ExecutorService.class);
DefaultPluginEnvironment instance = spy(new DefaultPluginEnvironment());
doReturn(executorService).when(instance).getPluginExecutor();
when(executorService.submit(any(Callable.class))).thenAnswer(iom -> {
final Callable callable = iom.getArgument(0);
callable.call();
return CompletableFuture.completedFuture(null);
});
Object expResult = null;
Future future = instance.executePluginLater(graph, plugin, parameters, interactive, async, synchronizer);
Object result = future.get();
assertEquals(result, expResult);
}
use of au.gov.asd.tac.constellation.plugins.PluginSynchronizer in project constellation by constellation-app.
the class DefaultPluginInteractionNGTest method setUpMethod.
@BeforeMethod
public void setUpMethod() throws Exception {
environment = new DefaultPluginEnvironment();
plugin = new TestPlugin();
graph = new DualGraph(null);
synchroniser = new PluginSynchronizer(1);
graphReport = new GraphReport(graph.getId());
manager = new PluginManager(environment, plugin, graph, false, synchroniser);
report = new PluginReport(graphReport, plugin);
}
Aggregations