Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add meta change flag for actions triggered by Select Fields Button #9486

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1453,10 +1453,13 @@ private void get() {
List<String> 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 );

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,6 +48,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand All @@ -57,21 +59,29 @@
public class GetFieldsDialog extends Dialog {

private static final Class<?> PKG = GetFieldsDialog.class;

private List<String> 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<String> 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" ) );
Expand Down Expand Up @@ -229,35 +239,48 @@ private boolean doSearch( TreeItem item, String data, boolean isSearchInvalid )
return isSearchInvalid;
}

private boolean hasChanged( List<String> 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() ) {
setExpanded( child );
}
}

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<String> 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<String> 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 );
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GUIResource> 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<String> prevPaths = Arrays.asList( "path1", "path2", "path3" );
java.util.List<String> 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<String> prevPaths = Arrays.asList( "path1", "path2", "path3" );
List<String> 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<String> prevPaths = Arrays.asList( "path1", "path2", "path3" );

when( treeItem.getItems() ).thenReturn( new TreeItem[0] );
pathsField.set( mockGetFieldsDialog, prevPaths );

mockGetFieldsDialog.clearSelection( treeItem );

assertTrue( jsonInputMeta.hasChanged() );
}
}