Search in sources :

Example 1 with PluginSynchronizer

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));
    }
}
Also used : PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) PluginSynchronizer(au.gov.asd.tac.constellation.plugins.PluginSynchronizer) GraphManager(au.gov.asd.tac.constellation.graph.manager.GraphManager) HashMap(java.util.HashMap) Graph(au.gov.asd.tac.constellation.graph.Graph) PluginRegistry(au.gov.asd.tac.constellation.plugins.PluginRegistry) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) DataAccessPlugin(au.gov.asd.tac.constellation.views.dataaccess.plugins.DataAccessPlugin) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) SimplePlugin(au.gov.asd.tac.constellation.plugins.templates.SimplePlugin) Test(org.testng.annotations.Test)

Example 2 with PluginSynchronizer

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);
}
Also used : PluginSynchronizer(au.gov.asd.tac.constellation.plugins.PluginSynchronizer) Graph(au.gov.asd.tac.constellation.graph.Graph) ExecutorService(java.util.concurrent.ExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) Callable(java.util.concurrent.Callable) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 3 with PluginSynchronizer

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);
}
Also used : PluginSynchronizer(au.gov.asd.tac.constellation.plugins.PluginSynchronizer) PluginGraphs(au.gov.asd.tac.constellation.plugins.PluginGraphs) Callable(java.util.concurrent.Callable) Graph(au.gov.asd.tac.constellation.graph.Graph) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) ExecutorService(java.util.concurrent.ExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 4 with PluginSynchronizer

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);
}
Also used : PluginSynchronizer(au.gov.asd.tac.constellation.plugins.PluginSynchronizer) Graph(au.gov.asd.tac.constellation.graph.Graph) ExecutorService(java.util.concurrent.ExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) Callable(java.util.concurrent.Callable) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 5 with PluginSynchronizer

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);
}
Also used : PluginSynchronizer(au.gov.asd.tac.constellation.plugins.PluginSynchronizer) PluginReport(au.gov.asd.tac.constellation.plugins.reporting.PluginReport) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) GraphReport(au.gov.asd.tac.constellation.plugins.reporting.GraphReport) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

PluginSynchronizer (au.gov.asd.tac.constellation.plugins.PluginSynchronizer)11 Graph (au.gov.asd.tac.constellation.graph.Graph)9 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)9 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)9 Future (java.util.concurrent.Future)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 Test (org.testng.annotations.Test)7 Callable (java.util.concurrent.Callable)6 ExecutorService (java.util.concurrent.ExecutorService)6 PluginGraphs (au.gov.asd.tac.constellation.plugins.PluginGraphs)3 PluginInteraction (au.gov.asd.tac.constellation.plugins.PluginInteraction)3 GraphManager (au.gov.asd.tac.constellation.graph.manager.GraphManager)2 PluginExecution (au.gov.asd.tac.constellation.plugins.PluginExecution)2 PluginRegistry (au.gov.asd.tac.constellation.plugins.PluginRegistry)2 PluginParameter (au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)2 DataAccessPlugin (au.gov.asd.tac.constellation.views.dataaccess.plugins.DataAccessPlugin)2 DuplicateKeyException (au.gov.asd.tac.constellation.graph.DuplicateKeyException)1 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)1 AbstractPlugin (au.gov.asd.tac.constellation.plugins.AbstractPlugin)1 PluginException (au.gov.asd.tac.constellation.plugins.PluginException)1