From 6954d4fdea9ad5b145764b90e9dfbcfe72433d80 Mon Sep 17 00:00:00 2001 From: Lukas Date: Fri, 19 Jul 2024 11:30:45 +1200 Subject: [PATCH] Add meta change flag for actions triggered by Select Fields Button --- .../steps/jsoninput/JsonInputDialog.java | 7 +- .../jsoninput/getfields/GetFieldsDialog.java | 43 ++++++-- .../getfields/GetFieldsDialogTest.java | 101 ++++++++++++++++++ 3 files changed, 139 insertions(+), 12 deletions(-) create mode 100644 plugins/json/core/src/test/java/org/pentaho/di/ui/trans/steps/jsoninput/getfields/GetFieldsDialogTest.java diff --git a/plugins/json/core/src/main/java/org/pentaho/di/ui/trans/steps/jsoninput/JsonInputDialog.java b/plugins/json/core/src/main/java/org/pentaho/di/ui/trans/steps/jsoninput/JsonInputDialog.java index 76d4dc765ad7..44b200aae313 100644 --- a/plugins/json/core/src/main/java/org/pentaho/di/ui/trans/steps/jsoninput/JsonInputDialog.java +++ b/plugins/json/core/src/main/java/org/pentaho/di/ui/trans/steps/jsoninput/JsonInputDialog.java @@ -1453,10 +1453,13 @@ private void get() { List paths = new ArrayList<>(); for ( int i = 0; i < wFields.table.getItems().length; i++ ) { TableItem item = wFields.table.getItem( i ); - paths.add( item.getText( 2 ) ); + String value = item.getText( 2 ); + if ( !value.trim().isEmpty() ) { + paths.add( value ); + } } - GetFieldsDialog getFieldsDialog = new GetFieldsDialog( shell ); + GetFieldsDialog getFieldsDialog = new GetFieldsDialog( shell, input ); getFieldsDialog.open( filename, paths, wFields ); } diff --git a/plugins/json/core/src/main/java/org/pentaho/di/ui/trans/steps/jsoninput/getfields/GetFieldsDialog.java b/plugins/json/core/src/main/java/org/pentaho/di/ui/trans/steps/jsoninput/getfields/GetFieldsDialog.java index 2656a62f061f..90bba341c3be 100644 --- a/plugins/json/core/src/main/java/org/pentaho/di/ui/trans/steps/jsoninput/getfields/GetFieldsDialog.java +++ b/plugins/json/core/src/main/java/org/pentaho/di/ui/trans/steps/jsoninput/getfields/GetFieldsDialog.java @@ -38,6 +38,7 @@ import org.pentaho.di.core.Const; import org.pentaho.di.core.exception.KettleFileException; import org.pentaho.di.i18n.BaseMessages; +import org.pentaho.di.trans.step.BaseStepMeta; import org.pentaho.di.trans.steps.jsoninput.json.JsonSampler; import org.pentaho.di.ui.core.FormDataBuilder; import org.pentaho.di.ui.core.PropsUI; @@ -47,6 +48,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Objects; @@ -57,21 +59,29 @@ public class GetFieldsDialog extends Dialog { private static final Class PKG = GetFieldsDialog.class; + private List paths = new ArrayList<>(); + private PropsUI props; + protected Button ok; protected Button cancel; protected Button clearSelection; - private PropsUI props; + protected BaseStepMeta meta; + private static final Image LOGO = GUIResource.getInstance().getImageLogoSmall(); - public GetFieldsDialog( Shell parent ) { + + public GetFieldsDialog( Shell parent, BaseStepMeta meta ) { super( parent, SWT.NONE ); this.props = PropsUI.getInstance(); + this.meta = meta; } public void open( String filename, List paths, TableView wFields ) { Shell parent = getParent(); Display display = parent.getDisplay(); + this.paths.addAll( paths ); + if ( StringUtils.isBlank( filename ) ) { MessageBox mb = new MessageBox( parent, SWT.OK | SWT.ICON_ERROR ); mb.setMessage( BaseMessages.getString( PKG, "get-fields.plugin.app.unable-to-view.no-input.message" ) ); @@ -229,6 +239,10 @@ private boolean doSearch( TreeItem item, String data, boolean isSearchInvalid ) return isSearchInvalid; } + private boolean hasChanged( List newPaths ) { + return !( this.paths.size() == newPaths.size() && this.paths.containsAll( newPaths ) && newPaths.containsAll( this.paths ) ); + } + private void setExpanded( TreeItem item ) { item.setExpanded( true ); for ( TreeItem child : item.getItems() ) { @@ -236,28 +250,37 @@ private void setExpanded( TreeItem item ) { } } - private void clearSelection( TreeItem item ) { + protected void clearSelection( TreeItem item ) { item.setChecked( false ); for ( TreeItem child : item.getItems() ) { clearSelection( child ); } + if ( hasChanged( Collections.emptyList() ) ) { + meta.setChanged(); + } } - private void ok( JsonSampler jsonSampler, Tree tree, Shell shell, TableView wFields ) { - paths.clear(); - paths.addAll( jsonSampler.getChecked( tree ) ); + protected void ok( JsonSampler jsonSampler, Tree tree, Shell shell, TableView wFields ) { + List newPaths = jsonSampler.getChecked( tree ); - wFields.table.setItemCount( paths.size() ); - if ( !paths.isEmpty() ) { - for ( int i = 0; i < paths.size(); i++ ) { - String path = paths.get( i ); + List compNewPaths = new ArrayList<>(); + wFields.table.setItemCount( newPaths.size() ); + if ( !newPaths.isEmpty() ) { + for ( int i = 0; i < newPaths.size(); i++ ) { + String path = newPaths.get( i ); String[] values = path.split( ":" ); TableItem item = wFields.table.getItem( i ); item.setText( 1, values[0] ); item.setText( 2, values[1] ); item.setText( 3, values[2] ); + compNewPaths.add( values[1] ); } } + + if ( hasChanged( compNewPaths ) ) { + meta.setChanged(); + } + wFields.removeEmptyRows(); wFields.setRowNums(); wFields.optWidth( true ); diff --git a/plugins/json/core/src/test/java/org/pentaho/di/ui/trans/steps/jsoninput/getfields/GetFieldsDialogTest.java b/plugins/json/core/src/test/java/org/pentaho/di/ui/trans/steps/jsoninput/getfields/GetFieldsDialogTest.java new file mode 100644 index 000000000000..a1ddb07c97c0 --- /dev/null +++ b/plugins/json/core/src/test/java/org/pentaho/di/ui/trans/steps/jsoninput/getfields/GetFieldsDialogTest.java @@ -0,0 +1,101 @@ +package org.pentaho.di.ui.trans.steps.jsoninput.getfields; + +import org.eclipse.swt.widgets.*; +import org.junit.Before; +import org.junit.Test; +import org.mockito.MockedStatic; +import org.pentaho.di.trans.steps.jsoninput.JsonInputMeta; +import org.pentaho.di.trans.steps.jsoninput.json.JsonSampler; +import org.pentaho.di.ui.core.gui.GUIResource; +import org.pentaho.di.ui.core.widget.TableView; + +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.when; + +public class GetFieldsDialogTest { + private GetFieldsDialog mockGetFieldsDialog; + private JsonInputMeta jsonInputMeta; + + private JsonSampler mockJsonSampler; + private Shell mockShell; + private TableView mockTableView; + private Tree mockTree; + private Field pathsField; + + @Before + //mock UI elements + public void setup() { + jsonInputMeta = new JsonInputMeta(); + + mockJsonSampler = mock( JsonSampler.class ); + mockShell = mock( Shell.class ); + mockTableView = mock( TableView.class ); + mockTree = mock( Tree.class ); + mockTableView.table = mock( Table.class ); + + TableItem tableItem = mock( TableItem.class ); + GUIResource guiResource = mock( GUIResource.class ); + + when( guiResource.getImageLogoSmall() ).thenReturn(null); + when( mockTableView.table.getItem( anyInt() ) ).thenReturn( tableItem ); + + try ( MockedStatic mockedStatic = mockStatic( GUIResource.class ) ) { + mockedStatic.when( GUIResource::getInstance ).thenReturn( guiResource ); + mockGetFieldsDialog = mock( GetFieldsDialog.class, CALLS_REAL_METHODS ); + mockGetFieldsDialog.meta = jsonInputMeta; + + pathsField = mockGetFieldsDialog.getClass().getDeclaredField("paths"); + pathsField.setAccessible( true ); + } catch ( NoSuchFieldException e ) { + e.printStackTrace(); + } + } + + @Test + public void testOnOkWithChangesMetaChanged() throws IllegalAccessException { + java.util.List prevPaths = Arrays.asList( "path1", "path2", "path3" ); + java.util.List afterPaths = Arrays.asList( "details1:path3:details2", "details3:path1:details4" ); + + when( mockJsonSampler.getChecked( mockTree ) ).thenReturn( afterPaths ); + + pathsField.set( mockGetFieldsDialog, prevPaths ); + + mockGetFieldsDialog.ok( mockJsonSampler, mockTree, mockShell, mockTableView ); + + assertTrue( jsonInputMeta.hasChanged() ); + } + + @Test + public void testOnOkNoChangesNoMetaChanged() throws IllegalAccessException { + java.util.List prevPaths = Arrays.asList( "path1", "path2", "path3" ); + List afterPaths= Arrays.asList( "details1:path3:details2", "details3:path1:details4", "details5:path2:details6" ); + + when( mockJsonSampler.getChecked( mockTree ) ).thenReturn( afterPaths ); + + pathsField.set( mockGetFieldsDialog, prevPaths ); + + mockGetFieldsDialog.ok( mockJsonSampler, mockTree, mockShell, mockTableView ); + + assertFalse( jsonInputMeta.hasChanged() ); + } + + @Test + public void testOnClearNoMetaChanged() throws IllegalAccessException { + TreeItem treeItem = mock( TreeItem.class ); + java.util.List prevPaths = Arrays.asList( "path1", "path2", "path3" ); + + when( treeItem.getItems() ).thenReturn( new TreeItem[0] ); + pathsField.set( mockGetFieldsDialog, prevPaths ); + + mockGetFieldsDialog.clearSelection( treeItem ); + + assertTrue( jsonInputMeta.hasChanged() ); + } +}