Search in sources :

Example 1 with ExportToExcelFilePlugin

use of au.gov.asd.tac.constellation.views.tableview.plugins.ExportToExcelFilePlugin in project constellation by constellation-app.

the class ExportMenuNGTest method verifyExportExcelAction.

/**
 * Verify that the passed event handler exports to Excel either the whole
 * table or just the selected rows.
 *
 * @param eventHandler the handler to test
 * @param expectedCopyOnlySelectedRows true if only the selected rows are
 *     expected to be exported, false otherwise
 * @param userCancelsRequest true if the user is meant to cancel the export when
 *     picking a file in the file chooser
 */
private void verifyExportExcelAction(final EventHandler<ActionEvent> eventHandler, final boolean userCancelsRequest, final boolean expectedExportOnlySelectedRows) throws InterruptedException, PluginException, ExecutionException {
    final ExportMenuItemActionHandler exportActionHandler = (ExportMenuItemActionHandler) eventHandler;
    final ExportMenuItemActionHandler spiedExportActionHandler = spy(exportActionHandler);
    final FileChooserBuilder exportFileChooser = mock(FileChooserBuilder.class);
    final File exportFile = userCancelsRequest ? null : new File("test.xlsx");
    final Optional<File> optionalExportFile = Optional.ofNullable(exportFile);
    doReturn(exportFileChooser).when(spiedExportActionHandler).getExportFileChooser();
    try (final MockedStatic<PluginExecution> pluginExecutionMockedStatic = Mockito.mockStatic(PluginExecution.class);
        final MockedStatic<FileChooser> fileChooserMockedStatic = Mockito.mockStatic(FileChooser.class);
        final MockedStatic<Platform> platformMockedStatic = Mockito.mockStatic(Platform.class)) {
        // This is added so that the mocked static that we would otherwise be
        // trying to run in the fx thread is actually invoked properly
        platformMockedStatic.when(() -> Platform.runLater(any(Runnable.class))).thenAnswer(iom -> {
            ((Runnable) iom.getArgument(0)).run();
            return null;
        });
        fileChooserMockedStatic.when(() -> FileChooser.openSaveDialog(exportFileChooser)).thenReturn(CompletableFuture.completedFuture(optionalExportFile));
        final ActionEvent actionEvent = mock(ActionEvent.class);
        final Pagination pagination = mock(Pagination.class);
        final TableView<ObservableList<String>> tableView = mock(TableView.class);
        final int maxRowsPerPage = 42;
        final UserTablePreferences userTablePreferences = new UserTablePreferences();
        userTablePreferences.setMaxRowsPerPage(maxRowsPerPage);
        when(activeTableReference.getPagination()).thenReturn(pagination);
        when(table.getTableView()).thenReturn(tableView);
        when(activeTableReference.getUserTablePreferences()).thenReturn(userTablePreferences);
        final PluginExecution pluginExecution = mock(PluginExecution.class);
        pluginExecutionMockedStatic.when(() -> PluginExecution.withPlugin(any(Plugin.class))).thenAnswer(mockitoInvocation -> {
            final ExportToExcelFilePlugin plugin = (ExportToExcelFilePlugin) mockitoInvocation.getArgument(0);
            if (exportFile != null) {
                assertEquals(exportFile.getAbsolutePath(), plugin.getFile().getAbsolutePath());
            } else {
                assertEquals(exportFile, plugin.getFile());
            }
            assertEquals(pagination, plugin.getPagination());
            assertEquals(tableView, plugin.getTable());
            assertEquals(42, plugin.getRowsPerPage());
            assertEquals(GRAPH_ID, plugin.getSheetName());
            assertEquals(expectedExportOnlySelectedRows, plugin.isSelectedOnly());
            return pluginExecution;
        });
        spiedExportActionHandler.handle(actionEvent);
        // Wait for the export job to complete
        spiedExportActionHandler.getLastExport().get();
        fileChooserMockedStatic.verify(() -> FileChooser.openSaveDialog(exportFileChooser));
        if (userCancelsRequest) {
            verifyNoInteractions(pluginExecution);
        } else {
            verify(pluginExecution).executeNow((Graph) null);
        }
        verify(actionEvent).consume();
    }
}
Also used : PluginExecution(au.gov.asd.tac.constellation.plugins.PluginExecution) Platform(javafx.application.Platform) ActionEvent(javafx.event.ActionEvent) ExportMenuItemActionHandler(au.gov.asd.tac.constellation.views.tableview.components.ExportMenu.ExportMenuItemActionHandler) ExportToExcelFilePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.ExportToExcelFilePlugin) Pagination(javafx.scene.control.Pagination) UserTablePreferences(au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences) ObservableList(javafx.collections.ObservableList) JFileChooser(javax.swing.JFileChooser) FileChooser(au.gov.asd.tac.constellation.utilities.gui.filechooser.FileChooser) FileChooserBuilder(org.openide.filesystems.FileChooserBuilder) File(java.io.File) ExportToExcelFilePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.ExportToExcelFilePlugin) Plugin(au.gov.asd.tac.constellation.plugins.Plugin) ExportToCsvFilePlugin(au.gov.asd.tac.constellation.views.tableview.plugins.ExportToCsvFilePlugin)

Aggregations

Plugin (au.gov.asd.tac.constellation.plugins.Plugin)1 PluginExecution (au.gov.asd.tac.constellation.plugins.PluginExecution)1 FileChooser (au.gov.asd.tac.constellation.utilities.gui.filechooser.FileChooser)1 UserTablePreferences (au.gov.asd.tac.constellation.views.tableview.api.UserTablePreferences)1 ExportMenuItemActionHandler (au.gov.asd.tac.constellation.views.tableview.components.ExportMenu.ExportMenuItemActionHandler)1 ExportToCsvFilePlugin (au.gov.asd.tac.constellation.views.tableview.plugins.ExportToCsvFilePlugin)1 ExportToExcelFilePlugin (au.gov.asd.tac.constellation.views.tableview.plugins.ExportToExcelFilePlugin)1 File (java.io.File)1 Platform (javafx.application.Platform)1 ObservableList (javafx.collections.ObservableList)1 ActionEvent (javafx.event.ActionEvent)1 Pagination (javafx.scene.control.Pagination)1 JFileChooser (javax.swing.JFileChooser)1 FileChooserBuilder (org.openide.filesystems.FileChooserBuilder)1