Search in sources :

Example 1 with GraphManager

use of au.gov.asd.tac.constellation.graph.manager.GraphManager in project constellation by constellation-app.

the class ExecuteListenerNGTest method setUpMethod.

@BeforeMethod
public void setUpMethod() throws Exception {
    dataAccessViewTopComponent = mock(DataAccessViewTopComponent.class);
    dataAccessPane = mock(DataAccessPane.class);
    dataAccessTabPane = mock(DataAccessTabPane.class);
    tabPane = mock(TabPane.class);
    graphManager = mock(GraphManager.class);
    activeGraph = mock(Graph.class);
    when(dataAccessPane.getParentComponent()).thenReturn(dataAccessViewTopComponent);
    when(dataAccessPane.getDataAccessTabPane()).thenReturn(dataAccessTabPane);
    when(dataAccessTabPane.getTabPane()).thenReturn(tabPane);
    when(dataAccessViewTopComponent.getExecutorService()).thenReturn(Executors.newSingleThreadExecutor());
    executeListener = new ExecuteListener(dataAccessPane);
    graphManagerMockedStatic.when(GraphManager::getDefault).thenReturn(graphManager);
    when(graphManager.getActiveGraph()).thenReturn(activeGraph);
    when(activeGraph.getId()).thenReturn(GRAPH_ID);
    // Intercept the plugin execution run calls and run the plugins manually
    // so that it executes within the same thread and sequentially for the test
    pluginExecution = mock(PluginExecution.class);
    pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(any(SimplePlugin.class))).thenAnswer(iom -> {
        final SimplePlugin plugin = iom.getArgument(0);
        final PluginGraphs graphs = mock(PluginGraphs.class);
        when(graphs.getGraph()).thenReturn(null);
        final PluginInteraction pluginInteraction = mock(PluginInteraction.class);
        final PluginParameters pluginParameters = mock(PluginParameters.class);
        // This will call the execute method of the simple plugin
        plugin.run(graphs, pluginInteraction, pluginParameters);
        return pluginExecution;
    });
    // Intercept calls to start the wait for tasks so that they don't run
    completableFutureMockedStatic.when(() -> CompletableFuture.runAsync(any(WaitForQueriesToCompleteTask.class), any(ExecutorService.class))).thenReturn(null);
    notificationDisplayer = mock(NotificationDisplayer.class);
    notificationDisplayerMockedStatic.when(NotificationDisplayer::getDefault).thenReturn(notificationDisplayer);
    when(notificationDisplayer.notify(anyString(), any(Icon.class), anyString(), isNull())).thenReturn(null);
    statusDisplayer = mock(StatusDisplayer.class);
    statusDisplayerMockedStatic.when(StatusDisplayer::getDefault).thenReturn(statusDisplayer);
    DataAccessPaneState.clearState();
}
Also used : TabPane(javafx.scene.control.TabPane) DataAccessTabPane(au.gov.asd.tac.constellation.views.dataaccess.components.DataAccessTabPane) PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) GraphManager(au.gov.asd.tac.constellation.graph.manager.GraphManager) WaitForQueriesToCompleteTask(au.gov.asd.tac.constellation.views.dataaccess.tasks.WaitForQueriesToCompleteTask) StatusDisplayer(org.openide.awt.StatusDisplayer) DataAccessPane(au.gov.asd.tac.constellation.views.dataaccess.panes.DataAccessPane) PluginGraphs(au.gov.asd.tac.constellation.plugins.PluginGraphs) DataAccessTabPane(au.gov.asd.tac.constellation.views.dataaccess.components.DataAccessTabPane) Graph(au.gov.asd.tac.constellation.graph.Graph) PluginInteraction(au.gov.asd.tac.constellation.plugins.PluginInteraction) ExecutorService(java.util.concurrent.ExecutorService) SimplePlugin(au.gov.asd.tac.constellation.plugins.templates.SimplePlugin) NotificationDisplayer(org.openide.awt.NotificationDisplayer) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) Icon(javax.swing.Icon) DataAccessViewTopComponent(au.gov.asd.tac.constellation.views.dataaccess.DataAccessViewTopComponent) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with GraphManager

use of au.gov.asd.tac.constellation.graph.manager.GraphManager 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 3 with GraphManager

use of au.gov.asd.tac.constellation.graph.manager.GraphManager in project constellation by constellation-app.

the class BasicFindPluginNGTest method getAttributes.

/**
 * Used to convert the string variant of attributes to the Attribute object
 *
 * @return the list of Attribute objects
 */
private ArrayList<Attribute> getAttributes() {
    final FindViewTopComponent findViewTopComponent = mock(FindViewTopComponent.class);
    FindViewController instance = FindViewController.getDefault().init(findViewTopComponent);
    final GraphManager gm = Mockito.mock(GraphManager.class);
    when(gm.getAllGraphs()).thenReturn(graphMap);
    ArrayList<Attribute> attributes = new ArrayList<>();
    try (MockedStatic<GraphManager> mockedStatic = Mockito.mockStatic(GraphManager.class)) {
        mockedStatic.when(() -> GraphManager.getDefault()).thenReturn(gm);
        GraphElementType type = GraphElementType.VERTEX;
        List<String> result = instance.populateAttributes(type, attributes, Long.MIN_VALUE);
        ReadableGraph rg = graph.getReadableGraph();
        for (int i = 0; i < result.size(); i++) {
            int attributeInt = rg.getAttribute(type, result.get(i));
            GraphAttribute ga = new GraphAttribute(rg, attributeInt);
            if (ga.getAttributeType().equals("string")) {
                attributes.add(new GraphAttribute(rg, attributeInt));
                System.out.println(attributes.get(i).getName() + " = attribute name");
            }
        }
        rg.close();
    }
    return attributes;
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) GraphManager(au.gov.asd.tac.constellation.graph.manager.GraphManager) Attribute(au.gov.asd.tac.constellation.graph.Attribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) ArrayList(java.util.ArrayList) FindViewTopComponent(au.gov.asd.tac.constellation.views.find2.FindViewTopComponent) GraphElementType(au.gov.asd.tac.constellation.graph.GraphElementType) FindViewController(au.gov.asd.tac.constellation.views.find2.FindViewController)

Example 4 with GraphManager

use of au.gov.asd.tac.constellation.graph.manager.GraphManager in project constellation by constellation-app.

the class ReplacePluginNGTest method getAttributes.

/**
 * Used to convert the string variant of attributes to the Attribute object
 *
 * @return the list of Attribute objects
 */
private ArrayList<Attribute> getAttributes() {
    final FindViewTopComponent findViewTopComponent = mock(FindViewTopComponent.class);
    FindViewController instance = FindViewController.getDefault().init(findViewTopComponent);
    final GraphManager gm = Mockito.mock(GraphManager.class);
    when(gm.getAllGraphs()).thenReturn(graphMap);
    ArrayList<Attribute> attributes = new ArrayList<>();
    try (MockedStatic<GraphManager> mockedStatic = Mockito.mockStatic(GraphManager.class)) {
        mockedStatic.when(() -> GraphManager.getDefault()).thenReturn(gm);
        GraphElementType type = GraphElementType.VERTEX;
        List<String> result = instance.populateAttributes(type, attributes, Long.MIN_VALUE);
        ReadableGraph rg = graph.getReadableGraph();
        for (int i = 0; i < result.size(); i++) {
            int attributeInt = rg.getAttribute(type, result.get(i));
            GraphAttribute ga = new GraphAttribute(rg, attributeInt);
            if (ga.getAttributeType().equals("string")) {
                attributes.add(new GraphAttribute(rg, attributeInt));
            }
        }
        rg.close();
    }
    return attributes;
}
Also used : ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) GraphManager(au.gov.asd.tac.constellation.graph.manager.GraphManager) Attribute(au.gov.asd.tac.constellation.graph.Attribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) GraphAttribute(au.gov.asd.tac.constellation.graph.GraphAttribute) ArrayList(java.util.ArrayList) FindViewTopComponent(au.gov.asd.tac.constellation.views.find2.FindViewTopComponent) GraphElementType(au.gov.asd.tac.constellation.graph.GraphElementType) FindViewController(au.gov.asd.tac.constellation.views.find2.FindViewController)

Example 5 with GraphManager

use of au.gov.asd.tac.constellation.graph.manager.GraphManager in project constellation by constellation-app.

the class FindViewControllerNGTest method testReplaceMatchingElements.

// // Populate the attribute list with the attributes of the given type
// for (Graph graph : gm.getAllGraphs().values()) {
// try {
// System.out.println("in for graph try");
// 
// WritableGraph wg = graph.getWritableGraph("", true);
// for (int i = 0; i < stringAttributeList.size(); i++) {
// int attributeInt = wg.getAttribute(type, stringAttributeList.get(i));
// attributes.add(new GraphAttribute(wg, attributeInt));
// System.out.println(attributes.get(i).getName() + " = attribute name");
// 
// }
// 
// instance.updateBasicFindParameters(parameters);
// 
// System.out.println("before method call");
// 
// instance.retriveMatchingElements(true, false);
// 
// System.out.println("before assert");
// System.out.println(wg.getStringValue(labelV, vxId1));
// 
// System.out.println(wg.getBooleanValue(selectedV, vxId1));
// assertEquals(wg.getBooleanValue(selectedV, vxId1), true);
// System.out.println("after equals");
// 
// wg.commit();
// 
// } catch (final InterruptedException ex) {
// Exceptions.printStackTrace(ex);
// Thread.currentThread().interrupt();
// }
// 
// }
// 
// }
/**
 * Test of replaceMatchingElements method, of class FindViewController.
 */
@Test
public void testReplaceMatchingElements() {
    System.out.println("replaceMatchingElements");
    /**
     * Set up the graph with 4 vertexs, 4 transactions, 3 vertex attributes
     * (2 of type string), 3 transaction attributes (2 of type string)
     */
    setupGraph();
    /**
     * Create a mock of the top component, get an instance of the
     * controller, create a mock of the graph manager, when getAllGraphs is
     * called return the graphMap created in this class
     */
    final FindViewTopComponent findViewTopComponent = mock(FindViewTopComponent.class);
    FindViewController instance = FindViewController.getDefault().init(findViewTopComponent);
    final GraphManager gm = Mockito.mock(GraphManager.class);
    when(gm.getAllGraphs()).thenReturn(graphMap);
    when(gm.getActiveGraph()).thenReturn(graph);
    System.out.println("before try");
    try (MockedStatic<GraphManager> mockedStatic = Mockito.mockStatic(GraphManager.class)) {
        mockedStatic.when(() -> GraphManager.getDefault()).thenReturn(gm);
        System.out.println("in try");
        try (MockedStatic<PluginExecution> mockedStaticPlugin = Mockito.mockStatic(PluginExecution.class)) {
            PluginExecution pluginExecution = mock(PluginExecution.class);
            /**
             * The first test should execute the plugin once on graph as the
             * parameters are not set to look at all graphs
             */
            when(pluginExecution.executeLater(Mockito.eq(graph))).thenReturn(null);
            mockedStaticPlugin.when(() -> PluginExecution.withPlugin(Mockito.any(Plugin.class))).thenReturn(pluginExecution);
            instance.replaceMatchingElements(true, false);
            verify(pluginExecution).executeLater(Mockito.eq(graph));
            /**
             * Set the parameters to find in all graphs and repeat the same
             * process. The plugin should be executed on graph a second
             * time, and should be executed on graph2 for the first time.
             */
            instance.updateBasicReplaceParameters(parametersAllGraphs);
            when(pluginExecution.executeLater(Mockito.any(Graph.class))).thenReturn(null);
            mockedStaticPlugin.when(() -> PluginExecution.withPlugin(Mockito.any(Plugin.class))).thenReturn(pluginExecution);
            instance.replaceMatchingElements(true, false);
            verify(pluginExecution, times(2)).executeLater(Mockito.eq(graph));
            verify(pluginExecution).executeLater(Mockito.eq(graph2));
        }
    }
}
Also used : PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) GraphManager(au.gov.asd.tac.constellation.graph.manager.GraphManager) WritableGraph(au.gov.asd.tac.constellation.graph.WritableGraph) Graph(au.gov.asd.tac.constellation.graph.Graph) DualGraph(au.gov.asd.tac.constellation.graph.locking.DualGraph) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Aggregations

GraphManager (au.gov.asd.tac.constellation.graph.manager.GraphManager)20 Test (org.testng.annotations.Test)15 Graph (au.gov.asd.tac.constellation.graph.Graph)11 GraphElementType (au.gov.asd.tac.constellation.graph.GraphElementType)4 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)4 PluginExecution (au.gov.asd.tac.constellation.plugins.PluginExecution)4 ArrayList (java.util.ArrayList)4 Attribute (au.gov.asd.tac.constellation.graph.Attribute)3 WritableGraph (au.gov.asd.tac.constellation.graph.WritableGraph)3 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)3 GraphAttribute (au.gov.asd.tac.constellation.graph.GraphAttribute)2 DualGraph (au.gov.asd.tac.constellation.graph.locking.DualGraph)2 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)2 SimplePlugin (au.gov.asd.tac.constellation.plugins.templates.SimplePlugin)2 WaitForQueriesToCompleteTask (au.gov.asd.tac.constellation.views.dataaccess.tasks.WaitForQueriesToCompleteTask)2 FindViewController (au.gov.asd.tac.constellation.views.find2.FindViewController)2 FindViewTopComponent (au.gov.asd.tac.constellation.views.find2.FindViewTopComponent)2 UserTablePreferences (au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutorService (java.util.concurrent.ExecutorService)2