Search in sources :

Example 1 with DataAccessUserPreferences

use of au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessUserPreferences in project constellation by constellation-app.

the class DataAccessParametersIoProvider method saveParameters.

/**
 * Saves the global and plugin parameters from the passed tabs that belong to
 * the {@link DataAccessPane}. The parameters will be saved to a JSON file as
 * an array with each element representing one tab.
 *
 * @param tabs the tabs to extract the global and plugin parameters from
 * @see JsonIO#saveJsonPreferences(Optional, ObjectMapper, Object)
 */
public static void saveParameters(final TabPane tabs) {
    final List<DataAccessUserPreferences> dataAccessUserPreferenceses = new ArrayList<>();
    String queryName = null;
    for (final Tab step : tabs.getTabs()) {
        // Translate the plugin pane to the JSON format
        final DataAccessUserPreferences preferences = new DataAccessUserPreferences(DataAccessTabPane.getQueryPhasePane(step));
        final Label tabCaption = (Label) step.getGraphic();
        final Label defaultCaption = (Label) tabCaption.getGraphic();
        preferences.setStepCaption(!StringUtils.isBlank(tabCaption.getText()) ? tabCaption.getText() : defaultCaption.getText());
        // Remember the first non-null, non-blank query name.
        if (queryName == null && preferences.getGlobalParameters().containsKey(CoreGlobalParameters.QUERY_NAME_PARAMETER_ID) && StringUtils.isNotBlank(preferences.getGlobalParameters().get(CoreGlobalParameters.QUERY_NAME_PARAMETER_ID))) {
            queryName = preferences.getGlobalParameters().get(CoreGlobalParameters.QUERY_NAME_PARAMETER_ID);
        }
        dataAccessUserPreferenceses.add(preferences);
    }
    // Only save if the query name parameter is present
    if (queryName != null) {
        JsonIO.saveJsonPreferences(Optional.of(DATA_ACCESS_DIR), dataAccessUserPreferenceses);
    }
}
Also used : Tab(javafx.scene.control.Tab) ArrayList(java.util.ArrayList) Label(javafx.scene.control.Label) DataAccessUserPreferences(au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessUserPreferences)

Example 2 with DataAccessUserPreferences

use of au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessUserPreferences in project constellation by constellation-app.

the class DataAccessParametersIoProvider method loadParameters.

/**
 * Loads global and plugin parameters from a JSON file into the passed {@link DataAccessPane}.
 * If the JSON is loaded then all existing tabs will be removed and then new tabs added
 * for each entry in the loaded JSON array.
 *
 * @param dataAccessPane the pane to load the JSON parameter file into
 * @see JsonIO#loadJsonPreferences(Optional, TypeReference)
 */
public static void loadParameters(final DataAccessPane dataAccessPane) {
    final List<DataAccessUserPreferences> loadedParameters = JsonIO.loadJsonPreferences(Optional.of(DATA_ACCESS_DIR), new TypeReference<List<DataAccessUserPreferences>>() {
    });
    if (loadedParameters != null) {
        dataAccessPane.getDataAccessTabPane().removeTabs();
        loadedParameters.forEach(loadedParameter -> {
            final QueryPhasePane pluginPane = dataAccessPane.getDataAccessTabPane().newTab(loadedParameter.getStepCaption());
            // If an existing global parameter is in the JSON then update it,
            // otherwise ignore it
            pluginPane.getGlobalParametersPane().getParams().getParameters().entrySet().stream().filter(param -> loadedParameter.getGlobalParameters().containsKey(param.getKey())).forEach(param -> param.getValue().setStringValue(loadedParameter.getGlobalParameters().get(param.getKey())));
            // Groups all the parameters in to the plugin groups. Common parameters
            // are based on the plugin name that is before the first '.' in the key values
            pluginPane.getDataAccessPanes().stream().filter(pane -> loadedParameter.getPluginParameters().containsKey(pane.getPlugin().getClass().getSimpleName()) && loadedParameter.getPluginParameters().get(pane.getPlugin().getClass().getSimpleName()).containsKey(getEnabledPluginKey(pane)) && Boolean.valueOf(loadedParameter.getPluginParameters().get(pane.getPlugin().getClass().getSimpleName()).get(getEnabledPluginKey(pane)))).forEach(pane -> pane.setParameterValues(loadedParameter.getPluginParameters().get(pane.getPlugin().getClass().getSimpleName())));
        });
    }
}
Also used : CoreGlobalParameters(au.gov.asd.tac.constellation.views.dataaccess.CoreGlobalParameters) JsonIO(au.gov.asd.tac.constellation.utilities.genericjsonio.JsonIO) Label(javafx.scene.control.Label) DataAccessPane(au.gov.asd.tac.constellation.views.dataaccess.panes.DataAccessPane) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataSourceTitledPane(au.gov.asd.tac.constellation.views.dataaccess.panes.DataSourceTitledPane) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) List(java.util.List) TabPane(javafx.scene.control.TabPane) QueryPhasePane(au.gov.asd.tac.constellation.views.dataaccess.panes.QueryPhasePane) Tab(javafx.scene.control.Tab) Optional(java.util.Optional) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DataAccessUserPreferences(au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessUserPreferences) DataAccessTabPane(au.gov.asd.tac.constellation.views.dataaccess.components.DataAccessTabPane) QueryPhasePane(au.gov.asd.tac.constellation.views.dataaccess.panes.QueryPhasePane) ArrayList(java.util.ArrayList) List(java.util.List) DataAccessUserPreferences(au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessUserPreferences)

Example 3 with DataAccessUserPreferences

use of au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessUserPreferences in project constellation by constellation-app.

the class DataAccessParametersIoProviderNGTest method loadParameters.

@Test
public void loadParameters() throws IOException {
    final DataAccessPane dataAccessPane = mock(DataAccessPane.class);
    final DataAccessTabPane dataAccessTabPane = mock(DataAccessTabPane.class);
    when(dataAccessPane.getDataAccessTabPane()).thenReturn(dataAccessTabPane);
    final QueryPhasePane tab1 = mock(QueryPhasePane.class);
    final QueryPhasePane tab2 = mock(QueryPhasePane.class);
    when(dataAccessTabPane.newTab(anyString())).thenReturn(tab1).thenReturn(tab2);
    final GlobalParametersPane globalParametersPane1 = mock(GlobalParametersPane.class);
    final GlobalParametersPane globalParametersPane2 = mock(GlobalParametersPane.class);
    when(tab1.getGlobalParametersPane()).thenReturn(globalParametersPane1);
    when(tab2.getGlobalParametersPane()).thenReturn(globalParametersPane2);
    // By adding the settings bit here, it forces mockito to generate two different
    // classes. Otherwise they would be two different objects but have the same class name
    final Plugin plugin1 = mock(Plugin.class, withSettings().extraInterfaces(Comparable.class));
    final Plugin plugin2 = mock(Plugin.class, withSettings().extraInterfaces(Serializable.class));
    final DataSourceTitledPane dataSourceTitledPane1 = mock(DataSourceTitledPane.class);
    when(dataSourceTitledPane1.getPlugin()).thenReturn(plugin1);
    final DataSourceTitledPane dataSourceTitledPane2 = mock(DataSourceTitledPane.class);
    when(dataSourceTitledPane2.getPlugin()).thenReturn(plugin2);
    when(tab1.getDataAccessPanes()).thenReturn(List.of(dataSourceTitledPane1, dataSourceTitledPane2));
    when(tab2.getDataAccessPanes()).thenReturn(List.of());
    final PluginParameter pluginParameter1 = mock(PluginParameter.class);
    when(pluginParameter1.getId()).thenReturn("param1");
    final PluginParameter pluginParameter2 = mock(PluginParameter.class);
    when(pluginParameter2.getId()).thenReturn("param2");
    final PluginParameter pluginParameter3 = mock(PluginParameter.class);
    when(pluginParameter3.getId()).thenReturn("param3");
    final PluginParameter pluginParameter4 = mock(PluginParameter.class);
    when(pluginParameter4.getId()).thenReturn("param4");
    final PluginParameters globalPluginParameters1 = new PluginParameters();
    globalPluginParameters1.addParameter(pluginParameter1);
    globalPluginParameters1.addParameter(pluginParameter2);
    globalPluginParameters1.addParameter(pluginParameter3);
    final PluginParameters globalPluginParameters2 = new PluginParameters();
    globalPluginParameters2.addParameter(pluginParameter3);
    globalPluginParameters2.addParameter(pluginParameter4);
    when(globalParametersPane1.getParams()).thenReturn(globalPluginParameters1);
    when(globalParametersPane2.getParams()).thenReturn(globalPluginParameters2);
    try (final MockedStatic<JsonIO> jsonIOStaticMock = Mockito.mockStatic(JsonIO.class)) {
        final ObjectMapper objectMapper = new ObjectMapper();
        final String json = IOUtils.toString(new FileInputStream(getClass().getResource("resources/preferences.json").getPath()), StandardCharsets.UTF_8);
        // We do not know the mockito plugin names ahead of time so substitute them in now
        final StringSubstitutor substitutor = new StringSubstitutor(Map.of("INSERT_PLUGIN1_NAME", plugin1.getClass().getSimpleName(), "INSERT_PLUGIN2_NAME", plugin2.getClass().getSimpleName()));
        final List<DataAccessUserPreferences> preferences = objectMapper.readValue(substitutor.replace(json), new TypeReference<List<DataAccessUserPreferences>>() {
        });
        jsonIOStaticMock.when(() -> JsonIO.loadJsonPreferences(eq(Optional.of("DataAccessView")), any(TypeReference.class))).thenReturn(preferences);
        DataAccessParametersIoProvider.loadParameters(dataAccessPane);
    }
    verify(dataAccessTabPane, times(2)).newTab(anyString());
    // tab1 global parameters
    verify(pluginParameter1).setStringValue("tab1_param1_value");
    verify(pluginParameter3).setStringValue(null);
    // tab1 plugin parameters - only plugin1 because plugin2 is disabled
    verify(dataSourceTitledPane1).setParameterValues(Map.of(plugin1.getClass().getSimpleName() + "." + "__is_enabled__", "true", plugin1.getClass().getSimpleName() + "." + "param1", "plugin1_param1_value"));
    // tab2 global parameters
    verify(pluginParameter4).setStringValue("tab2_param4_value");
    // tab2 plugin parameters
    verify(dataSourceTitledPane2, times(0)).setParameterValues(anyMap());
}
Also used : JsonIO(au.gov.asd.tac.constellation.utilities.genericjsonio.JsonIO) Serializable(java.io.Serializable) DataAccessPane(au.gov.asd.tac.constellation.views.dataaccess.panes.DataAccessPane) QueryPhasePane(au.gov.asd.tac.constellation.views.dataaccess.panes.QueryPhasePane) GlobalParametersPane(au.gov.asd.tac.constellation.views.dataaccess.panes.GlobalParametersPane) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FileInputStream(java.io.FileInputStream) DataAccessUserPreferences(au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessUserPreferences) DataSourceTitledPane(au.gov.asd.tac.constellation.views.dataaccess.panes.DataSourceTitledPane) DataAccessTabPane(au.gov.asd.tac.constellation.views.dataaccess.components.DataAccessTabPane) StringSubstitutor(org.apache.commons.text.StringSubstitutor) List(java.util.List) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) TypeReference(com.fasterxml.jackson.core.type.TypeReference) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) Test(org.testng.annotations.Test)

Example 4 with DataAccessUserPreferences

use of au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessUserPreferences in project constellation by constellation-app.

the class DataAccessParametersIoProviderNGTest method saveParameters.

/**
 * Verifies the save parameter method saves only if the query name parameter
 * is present in the global parameters and is not blank. This will simulate two
 * tabs and verify the correct calls are made to serialize the data.
 *
 * @param tab1GlobalParams the global parameters to be populated in tab 1
 * @param tab2GlobalParams the global parameters to be populated in tab 2
 * @param isSaveExpected true if a call to save the JSON is expected to be made, false otherwise
 */
public void saveParameters(final Map<String, String> tab1GlobalParams, final Map<String, String> tab2GlobalParams, final boolean isSaveExpected) {
    final TabPane tabPane = mock(TabPane.class);
    final Tab tab1 = mock(Tab.class);
    final Tab tab2 = mock(Tab.class);
    when(tabPane.getTabs()).thenReturn(FXCollections.observableArrayList(tab1, tab2));
    final ScrollPane scrollPane1 = mock(ScrollPane.class);
    final ScrollPane scrollPane2 = mock(ScrollPane.class);
    when(tab1.getContent()).thenReturn(scrollPane1);
    when(tab2.getContent()).thenReturn(scrollPane2);
    when(tab1.getGraphic()).thenReturn(new Label("Step caption 1"));
    when(tab2.getGraphic()).thenReturn(new Label("Step caption 2"));
    final QueryPhasePane queryPhasePane1 = mock(QueryPhasePane.class);
    final QueryPhasePane queryPhasePane2 = mock(QueryPhasePane.class);
    when(scrollPane1.getContent()).thenReturn(queryPhasePane1);
    when(scrollPane2.getContent()).thenReturn(queryPhasePane2);
    // This will apply the correct stubbing to the created mocks. Uses the passed
    // query phase pane to determine what stubs to set.
    final MockedConstruction.MockInitializer<DataAccessUserPreferences> mockInitializer = (mock, cntxt) -> {
        final QueryPhasePane pane = (QueryPhasePane) cntxt.arguments().get(0);
        if (pane.equals(queryPhasePane1)) {
            when(mock.getGlobalParameters()).thenReturn(tab1GlobalParams);
            return;
        } else if (pane.equals(queryPhasePane2)) {
            when(mock.getGlobalParameters()).thenReturn(tab2GlobalParams);
            return;
        }
        throw new RuntimeException("Unknown QueryPhasePane passed to DataAccessUserPreferences constructor");
    };
    try (final MockedStatic<JsonIO> jsonIOStaticMock = Mockito.mockStatic(JsonIO.class);
        final MockedConstruction<DataAccessUserPreferences> mockedPrefConstruction = Mockito.mockConstruction(DataAccessUserPreferences.class, mockInitializer)) {
        DataAccessParametersIoProvider.saveParameters(tabPane);
        if (isSaveExpected) {
            jsonIOStaticMock.verify(() -> JsonIO.saveJsonPreferences(eq(Optional.of("DataAccessView")), eq(mockedPrefConstruction.constructed())));
        } else {
            jsonIOStaticMock.verifyNoInteractions();
        }
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) GlobalParametersPane(au.gov.asd.tac.constellation.views.dataaccess.panes.GlobalParametersPane) JsonIO(au.gov.asd.tac.constellation.utilities.genericjsonio.JsonIO) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TimeoutException(java.util.concurrent.TimeoutException) FXCollections(javafx.collections.FXCollections) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Test(org.testng.annotations.Test) DataSourceTitledPane(au.gov.asd.tac.constellation.views.dataaccess.panes.DataSourceTitledPane) FxToolkit(org.testfx.api.FxToolkit) Level(java.util.logging.Level) StringSubstitutor(org.apache.commons.text.StringSubstitutor) ScrollPane(javafx.scene.control.ScrollPane) TabPane(javafx.scene.control.TabPane) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) PluginParameter(au.gov.asd.tac.constellation.plugins.parameters.PluginParameter) Map(java.util.Map) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DataAccessUserPreferences(au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessUserPreferences) MockedConstruction(org.mockito.MockedConstruction) AfterClass(org.testng.annotations.AfterClass) PluginParameters(au.gov.asd.tac.constellation.plugins.parameters.PluginParameters) Label(javafx.scene.control.Label) DataAccessPane(au.gov.asd.tac.constellation.views.dataaccess.panes.DataAccessPane) BeforeClass(org.testng.annotations.BeforeClass) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Mockito.times(org.mockito.Mockito.times) FileInputStream(java.io.FileInputStream) Mockito.when(org.mockito.Mockito.when) Logger(java.util.logging.Logger) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) Mockito.verify(org.mockito.Mockito.verify) IOUtils(org.apache.commons.io.IOUtils) Mockito(org.mockito.Mockito) List(java.util.List) MockedStatic(org.mockito.MockedStatic) QueryPhasePane(au.gov.asd.tac.constellation.views.dataaccess.panes.QueryPhasePane) Tab(javafx.scene.control.Tab) Optional(java.util.Optional) Mockito.withSettings(org.mockito.Mockito.withSettings) DataAccessTabPane(au.gov.asd.tac.constellation.views.dataaccess.components.DataAccessTabPane) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) TabPane(javafx.scene.control.TabPane) DataAccessTabPane(au.gov.asd.tac.constellation.views.dataaccess.components.DataAccessTabPane) JsonIO(au.gov.asd.tac.constellation.utilities.genericjsonio.JsonIO) QueryPhasePane(au.gov.asd.tac.constellation.views.dataaccess.panes.QueryPhasePane) Label(javafx.scene.control.Label) DataAccessUserPreferences(au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessUserPreferences) MockedConstruction(org.mockito.MockedConstruction) Tab(javafx.scene.control.Tab) ScrollPane(javafx.scene.control.ScrollPane)

Aggregations

DataAccessUserPreferences (au.gov.asd.tac.constellation.views.dataaccess.api.DataAccessUserPreferences)4 JsonIO (au.gov.asd.tac.constellation.utilities.genericjsonio.JsonIO)3 DataAccessTabPane (au.gov.asd.tac.constellation.views.dataaccess.components.DataAccessTabPane)3 DataAccessPane (au.gov.asd.tac.constellation.views.dataaccess.panes.DataAccessPane)3 DataSourceTitledPane (au.gov.asd.tac.constellation.views.dataaccess.panes.DataSourceTitledPane)3 QueryPhasePane (au.gov.asd.tac.constellation.views.dataaccess.panes.QueryPhasePane)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 List (java.util.List)3 Label (javafx.scene.control.Label)3 Tab (javafx.scene.control.Tab)3 Plugin (au.gov.asd.tac.constellation.plugins.Plugin)2 PluginParameter (au.gov.asd.tac.constellation.plugins.parameters.PluginParameter)2 PluginParameters (au.gov.asd.tac.constellation.plugins.parameters.PluginParameters)2 GlobalParametersPane (au.gov.asd.tac.constellation.views.dataaccess.panes.GlobalParametersPane)2 FileInputStream (java.io.FileInputStream)2 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 Optional (java.util.Optional)2 TabPane (javafx.scene.control.TabPane)2