Java Code Examples for org.pentaho.di.core.variables.VariableSpace#setVariable()

The following examples show how to use org.pentaho.di.core.variables.VariableSpace#setVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MongoWrapperUtilTest.java    From pentaho-mongodb-plugin with Apache License 2.0 6 votes vote down vote up
@Test public void testCreatePropertiesBuilder() {
  MongoDbMeta input = new MongoDbInputMeta();
  setSocetAndConnectionTimeouts( input, "${" + CONNECTION_TIMEOUT + "}", "${" + SOCKET_TIMEOUT + "}" );

  MongoDbMeta output = new MongoDbOutputMeta();
  setSocetAndConnectionTimeouts( output, "${" + CONNECTION_TIMEOUT + "}", "${" + SOCKET_TIMEOUT + "}" );

  VariableSpace vars = new Variables();
  vars.setVariable( CONNECTION_TIMEOUT, "200" );
  vars.setVariable( SOCKET_TIMEOUT, "500" );

  MongoProperties inProps = MongoWrapperUtil.createPropertiesBuilder( input, vars ).build();
  MongoProperties outProps = MongoWrapperUtil.createPropertiesBuilder( output, vars ).build();

  checkProps( inProps, "200", "500" );
  checkProps( outProps, "200", "500" );
}
 
Example 2
Source File: MongoDbOutputTest.java    From pentaho-mongodb-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Testing the use of Environment Substitution during initialization of fields.
 * @throws Exception
 */
@Test public void testTopLevelArrayWithEnvironmentSubstitution() throws Exception {
  List<MongoDbOutputMeta.MongoField> paths = asList( mf( "${ENV_FIELD}", true, "[0]" ),
          mf( "field2", true, "${ENV_DOC_PATH}" ) );

  RowMetaInterface rmi = new RowMeta();
  rmi.addValueMeta( new ValueMetaString( "field1" ) );
  rmi.addValueMeta( new ValueMetaInteger( "field2" ) );

  Object[] row = new Object[ 2 ];
  row[ 0 ] = "value1";
  row[ 1 ] = 12L;
  VariableSpace vs = new Variables();
  vs.setVariable( "ENV_FIELD", "field1" );
  vs.setVariable( "ENV_DOC_PATH", "[1]" );

  for ( MongoDbOutputMeta.MongoField f : paths ) {
    f.init( vs );
  }

  DBObject result = kettleRowToMongo( paths, rmi, row, MongoDbOutputData.MongoTopLevel.ARRAY, false );
  assertEquals( JSON.serialize( result ), "[ { \"field1\" : \"value1\"} , { \"field2\" : 12}]" );
}
 
Example 3
Source File: StepWithMappingMetaTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest( StepWithMappingMeta.class )
public void activateParamsTest() throws Exception {
  String childParam = "childParam";
  String childValue = "childValue";
  String paramOverwrite = "paramOverwrite";
  String parentValue = "parentValue";
  String stepValue = "stepValue";

  VariableSpace parent = new Variables();
  parent.setVariable( paramOverwrite, parentValue );

  TransMeta childVariableSpace = new TransMeta();
  childVariableSpace.addParameterDefinition( childParam, "", "" );
  childVariableSpace.setParameterValue( childParam, childValue );

  String[] parameters = childVariableSpace.listParameters();
  StepWithMappingMeta.activateParams( childVariableSpace, childVariableSpace, parent,
    parameters, new String[] { childParam, paramOverwrite }, new String[] { childValue, stepValue }, true );

  Assert.assertEquals( childValue, childVariableSpace.getVariable( childParam ) );
  // the step parameter prevails
  Assert.assertEquals( stepValue, childVariableSpace.getVariable( paramOverwrite ) );
}
 
Example 4
Source File: GetRepositoryNamesTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private void init( Repository repository, String directoryName, boolean includeSubFolders, String nameMask, String exludeNameMask,
    ObjectTypeSelection typeSelection, int itemCount ) throws KettleException {

  VariableSpace vars = new Variables();
  vars.setVariable( "DirName", "/subdir1" );
  vars.setVariable( "IncludeMask", ".*" );
  vars.setVariable( "ExcludeMask", "" );

  GetRepositoryNamesMeta meta = new GetRepositoryNamesMeta();
  meta.setDirectory( new String[] { directoryName } );
  meta.setNameMask( new String[] { nameMask } );
  meta.setExcludeNameMask( new String[] { exludeNameMask } );
  meta.setIncludeSubFolders( new boolean[] { includeSubFolders } );
  meta.setObjectTypeSelection( typeSelection );
  StepMeta stepMeta = new StepMeta( "GetRepoNamesStep", meta );

  TransMeta transMeta = new TransMeta( vars );
  transMeta.setRepository( repository );
  transMeta.addStep( stepMeta );

  GetRepositoryNamesData data = (GetRepositoryNamesData) meta.getStepData();
  GetRepositoryNames step = new GetRepositoryNames( stepMeta, data, 0, transMeta, new Trans( transMeta ) );
  step.init( meta, data );
  assertNotNull( data.list );
  assertEquals( itemCount, data.list.size() );
}
 
Example 5
Source File: JsonInputTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonInputPathResolutionSuccess() {
  JsonInputField inputField = new JsonInputField( "value" );
  final String PATH = "${PARAM_PATH}.price";
  inputField.setPath( PATH );
  inputField.setType( ValueMetaInterface.TYPE_STRING );
  final JsonInputMeta inputMeta = createSimpleMeta( "json", inputField );
  VariableSpace variables = new Variables();
  JsonInput jsonInput = null;
  try {
    jsonInput =
      createJsonInput( "json", inputMeta, variables, new Object[] { getBasicTestJson() } );
    fail( "Without the parameter, this call should fail with an InvalidPathException. If it does not, test fails." );
  } catch ( InvalidPathException pathException ) {
    assertNull( jsonInput );
  }

  variables.setVariable( "PARAM_PATH", "$..book.[*]" );

  try {
    jsonInput = createJsonInput( "json", inputMeta, variables, new Object[] { getBasicTestJson() } );
    assertNotNull( jsonInput );
  } catch ( Exception ex ) {
    fail( "Json Input should be able to resolve the paths with the parameter introduced in the variable space." );
  }
}
 
Example 6
Source File: StepWithMappingMetaTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( StepWithMappingMeta.class )
public void replaceVariablesWithTransInternalVariablesTest()  {
  String variableOverwrite = "paramOverwrite";
  String variableChildOnly = "childValueVariable";
  String [] jobVariables = Const.INTERNAL_TRANS_VARIABLES;
  VariableSpace ChildVariables = new Variables();
  VariableSpace replaceByParentVariables = new Variables();

  for ( String internalVariable : jobVariables ) {
    ChildVariables.setVariable( internalVariable, "childValue" );
    replaceByParentVariables.setVariable( internalVariable, "parentValue" );
  }

  ChildVariables.setVariable( variableChildOnly, "childValueVariable" );
  ChildVariables.setVariable( variableOverwrite, "childNotInternalValue" );
  replaceByParentVariables.setVariable( variableOverwrite, "parentNotInternalValue" );

  StepWithMappingMeta.replaceVariableValues( ChildVariables, replaceByParentVariables );
  // do not replace internal variables
  Assert.assertEquals( "childValue", ChildVariables.getVariable( Const.INTERNAL_VARIABLE_ENTRY_CURRENT_DIRECTORY ) );
  // replace non internal variables
  Assert.assertEquals( "parentNotInternalValue", ChildVariables.getVariable( variableOverwrite ) );
  // keep child only variables
  Assert.assertEquals( variableChildOnly, ChildVariables.getVariable( variableChildOnly ) );

}
 
Example 7
Source File: StepWithMappingMetaTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( StepWithMappingMeta.class )
public void replaceVariablesWithJobInternalVariablesTest()  {
  String variableOverwrite = "paramOverwrite";
  String variableChildOnly = "childValueVariable";
  String [] jobVariables = Const.INTERNAL_JOB_VARIABLES;
  VariableSpace ChildVariables = new Variables();
  VariableSpace replaceByParentVariables = new Variables();

  for ( String internalVariable : jobVariables ) {
    ChildVariables.setVariable( internalVariable, "childValue" );
    replaceByParentVariables.setVariable( internalVariable, "parentValue" );
  }

  ChildVariables.setVariable( variableChildOnly, "childValueVariable" );
  ChildVariables.setVariable( variableOverwrite, "childNotInternalValue" );
  replaceByParentVariables.setVariable( variableOverwrite, "parentNotInternalValue" );

  StepWithMappingMeta.replaceVariableValues( ChildVariables, replaceByParentVariables );
  // do not replace internal variables
  Assert.assertEquals( "childValue", ChildVariables.getVariable( Const.INTERNAL_VARIABLE_ENTRY_CURRENT_DIRECTORY ) );
  // replace non internal variables
  Assert.assertEquals( "parentNotInternalValue", ChildVariables.getVariable( variableOverwrite ) );
  // keep child only variables
  Assert.assertEquals( variableChildOnly, ChildVariables.getVariable( variableChildOnly ) );

}
 
Example 8
Source File: StepWithMappingMetaTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( StepWithMappingMeta.class )
public void activateParamsWithTruePassParametersFlagTest() throws Exception {
  String childParam = "childParam";
  String childValue = "childValue";
  String paramOverwrite = "paramOverwrite";
  String parentValue = "parentValue";
  String stepValue = "stepValue";
  String parentAndChildParameter = "parentAndChildParameter";

  VariableSpace parent = new Variables();
  parent.setVariable( paramOverwrite, parentValue );
  parent.setVariable( parentAndChildParameter, parentValue );

  TransMeta childVariableSpace = new TransMeta();
  childVariableSpace.addParameterDefinition( childParam, "", "" );
  childVariableSpace.setParameterValue( childParam, childValue );
  childVariableSpace.addParameterDefinition( parentAndChildParameter, "", "" );
  childVariableSpace.setParameterValue( parentAndChildParameter, childValue );

  String[] parameters = childVariableSpace.listParameters();

  StepWithMappingMeta.activateParams( childVariableSpace, childVariableSpace, parent,
    parameters, new String[] { childParam, paramOverwrite }, new String[] { childValue, stepValue }, true );

  //childVariableSpace.setVariable( parentAndChildParameter, parentValue);

  Assert.assertEquals( childValue, childVariableSpace.getVariable( childParam ) );
  // the step parameter prevails
  Assert.assertEquals( stepValue, childVariableSpace.getVariable( paramOverwrite ) );

  Assert.assertEquals( parentValue, childVariableSpace.getVariable( parentAndChildParameter ) );
}
 
Example 9
Source File: StepWithMappingMetaTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( StepWithMappingMeta.class )
public void activateParamsWithFalsePassParametersFlagTest() throws Exception {
  String childParam = "childParam";
  String childValue = "childValue";
  String paramOverwrite = "paramOverwrite";
  String parentValue = "parentValue";
  String stepValue = "stepValue";
  String parentAndChildParameter = "parentAndChildParameter";

  VariableSpace parent = new Variables();
  parent.setVariable( paramOverwrite, parentValue );
  parent.setVariable( parentAndChildParameter, parentValue );

  TransMeta childVariableSpace = new TransMeta();
  childVariableSpace.addParameterDefinition( childParam, "", "" );
  childVariableSpace.setParameterValue( childParam, childValue );
  childVariableSpace.addParameterDefinition( parentAndChildParameter, "", "" );
  childVariableSpace.setParameterValue( parentAndChildParameter, childValue );

  String[] parameters = childVariableSpace.listParameters();
  StepWithMappingMeta.activateParams( childVariableSpace, childVariableSpace, parent,
    parameters, new String[] { childParam, paramOverwrite }, new String[] { childValue, stepValue }, false );

  Assert.assertEquals( childValue, childVariableSpace.getVariable( childParam ) );
  // the step parameter prevails
  Assert.assertEquals( stepValue, childVariableSpace.getVariable( paramOverwrite ) );

  Assert.assertEquals( childValue, childVariableSpace.getVariable( parentAndChildParameter ) );
}
 
Example 10
Source File: StepWithMappingMetaTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( StepWithMappingMeta.class )
public void loadMappingMetaTest_PathShouldBeTakenFromParentTrans() throws Exception {

  String fileName = "subtrans-executor-sub.ktr";
  Path parentFolder = Paths.get( getClass().getResource( "subtrans-executor-sub.ktr" ).toURI() ).getParent();

  //we have transformation
  VariableSpace variables = new Variables();
  variables.setVariable( Const.INTERNAL_VARIABLE_ENTRY_CURRENT_DIRECTORY, parentFolder.toString() );
  TransMeta parentTransMeta = new TransMeta( variables );

  //we have step in this transformation
  StepMeta stepMeta = new StepMeta();
  stepMeta.setParentTransMeta( parentTransMeta );

  //attach the executor to step which was described above
  StepWithMappingMeta mappingMetaMock = mock( StepWithMappingMeta.class );
  when( mappingMetaMock.getSpecificationMethod() ).thenReturn( ObjectLocationSpecificationMethod.FILENAME );
  when( mappingMetaMock.getFileName() ).thenReturn( "${" + Const.INTERNAL_VARIABLE_ENTRY_CURRENT_DIRECTORY + "}/" + fileName );
  when( mappingMetaMock.getParentStepMeta() ).thenReturn( stepMeta );

  //we will try to load the subtras which was linked at the step metas
  TransMeta transMeta = StepWithMappingMeta.loadMappingMeta( mappingMetaMock, null, null, variables, true );

  StringBuilder expected = new StringBuilder( parentFolder.toUri().toString() );
  /**
   * we need to remove "/" at the end of expected string because during load the trans from file 
   * internal variables will be replaced by uri from kettle vfs
   * check the follow points
   * {@link org.pentaho.di.trans.TransMeta#setInternalFilenameKettleVariables(VariableSpace)}
   * 
   */
  Assert.assertEquals( expected.deleteCharAt( expected.length() - 1 ).toString(), transMeta.getVariable( Const.INTERNAL_VARIABLE_ENTRY_CURRENT_DIRECTORY ) );
}
 
Example 11
Source File: StepWithMappingMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static void replaceVariableValues( VariableSpace childTransMeta, VariableSpace replaceBy, String type ) {
  if ( replaceBy == null ) {
    return;
  }
  String[] variableNames = replaceBy.listVariables();
  for ( String variableName : variableNames ) {
    if ( childTransMeta.getVariable( variableName ) != null && !isInternalVariable( variableName, type ) ) {
      childTransMeta.setVariable( variableName, replaceBy.getVariable( variableName ) );
    }
  }
}
 
Example 12
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
static void setGrandParentScopeVariable( Trans trans, String variableName, String variableValue ) {
  trans.setVariable( variableName, variableValue );

  VariableSpace parentSpace = trans.getParentVariableSpace();
  if ( parentSpace != null ) {
    parentSpace.setVariable( variableName, variableValue );
    VariableSpace grandParentSpace = parentSpace.getParentVariableSpace();
    if ( grandParentSpace != null ) {
      grandParentSpace.setVariable( variableName, variableValue );
    }
  }
}
 
Example 13
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
static void setParentScopeVariable( Trans trans, String variableName, String variableValue ) {
  trans.setVariable( variableName, variableValue );

  VariableSpace parentSpace = trans.getParentVariableSpace();
  if ( parentSpace != null ) {
    parentSpace.setVariable( variableName, variableValue );
  }
}
 
Example 14
Source File: NamedClusterEmbedManager.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Set the variable that will be used by Vfs to set the FileSystemOptions for the file system.
 * @param nameSpace  The namespace used by the getFileObject method to access VFS.
 * @param embeddedMetastoreProviderKey  The key to the embeddedMetastore from the AbstraceMeta
 */
public void passEmbeddedMetastoreKey( VariableSpace nameSpace, String embeddedMetastoreProviderKey ) {
  if ( nameSpace != null ) {
    if ( embeddedMetastoreProviderKey != null ) {
      nameSpace.setVariable( "vfs.hc.embeddedMetastoreKey", embeddedMetastoreProviderKey );
    }
  }
}
 
Example 15
Source File: JobExecutorMetaTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadJobMeta() throws KettleException {
  String param1 = "param1";
  String param2 = "param2";
  String param3 = "param3";
  String parentValue1 = "parentValue1";
  String parentValue2 = "parentValue2";
  String childValue3 = "childValue3";

  JobExecutorMeta jobExecutorMeta = spy( new JobExecutorMeta() );
  Repository repository = Mockito.mock( Repository.class );

  JobMeta meta = new JobMeta();
  meta.setVariable( param2, "childValue2 should be override" );
  meta.setVariable( param3, childValue3 );

  Mockito.doReturn( meta ).when( repository )
    .loadJob( Mockito.eq( "test.kjb" ), Mockito.anyObject(), Mockito.anyObject(), Mockito.anyObject() );

  VariableSpace parentSpace = new Variables();
  parentSpace.setVariable( param1, parentValue1 );
  parentSpace.setVariable( param2, parentValue2 );

  jobExecutorMeta.setSpecificationMethod( ObjectLocationSpecificationMethod.FILENAME );
  jobExecutorMeta.setFileName( "/home/admin/test.kjb" );

  JobMeta jobMeta;

  jobExecutorMeta.getParameters().setInheritingAllVariables( false );
  jobMeta = JobExecutorMeta.loadJobMeta( jobExecutorMeta, repository, parentSpace );
  Assert.assertEquals( null, jobMeta.getVariable( param1 ) );
  Assert.assertEquals( parentValue2, jobMeta.getVariable( param2 ) );
  Assert.assertEquals( childValue3, jobMeta.getVariable( param3 ) );

  jobExecutorMeta.getParameters().setInheritingAllVariables( true );
  jobMeta = JobExecutorMeta.loadJobMeta( jobExecutorMeta, repository, parentSpace );
  Assert.assertEquals( parentValue1, jobMeta.getVariable( param1 ) );
  Assert.assertEquals( parentValue2, jobMeta.getVariable( param2 ) );
  Assert.assertEquals( childValue3, jobMeta.getVariable( param3 ) );
}
 
Example 16
Source File: TextFileOutputMetaTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void testVarReplaceSplit() throws Exception {
  TextFileOutputMeta meta = new TextFileOutputMeta();
  meta.setDefault();
  meta.setSplitEveryRows( "${splitVar}" );
  VariableSpace varSpace = new Variables();
  assertEquals( 0, meta.getSplitEvery( varSpace ) );
  String fileName = meta.buildFilename( "foo", "txt2", varSpace, 0, null, 3, false, meta );
  assertEquals( "foo.txt2", fileName );
  varSpace.setVariable( "splitVar", "2" );
  assertEquals( 2, meta.getSplitEvery( varSpace ) );
  fileName = meta.buildFilename( "foo", "txt2", varSpace, 0, null, 5, false, meta );
  assertEquals( "foo_5.txt2", fileName );
}
 
Example 17
Source File: JobEntryTransTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetTransMeta() throws KettleException {
  String param1 = "param1";
  String param2 = "param2";
  String param3 = "param3";
  String parentValue1 = "parentValue1";
  String parentValue2 = "parentValue2";
  String childValue3 = "childValue3";

  JobEntryTrans jobEntryTrans = spy( getJobEntryTrans() );
  Repository rep = Mockito.mock( Repository.class );
  RepositoryDirectory repositoryDirectory = Mockito.mock( RepositoryDirectory.class );
  RepositoryDirectoryInterface repositoryDirectoryInterface = Mockito.mock( RepositoryDirectoryInterface.class );
  Mockito.doReturn( repositoryDirectoryInterface ).when( rep ).loadRepositoryDirectoryTree();
  Mockito.doReturn( repositoryDirectory ).when( repositoryDirectoryInterface ).findDirectory( "/home/admin" );

  TransMeta meta = new TransMeta();
  meta.setVariable( param2, "childValue2 should be override" );
  meta.setVariable( param3, childValue3 );

  Mockito.doReturn( meta ).when( rep )
    .loadTransformation( Mockito.eq( "test.ktr" ), Mockito.anyObject(), Mockito.anyObject(), Mockito.anyBoolean(),
      Mockito.anyObject() );

  VariableSpace parentSpace = new Variables();
  parentSpace.setVariable( param1, parentValue1 );
  parentSpace.setVariable( param2, parentValue2 );

  jobEntryTrans.setFileName( "/home/admin/test.ktr" );

  Mockito.doNothing().when( jobEntryTrans ).logBasic( Mockito.anyString() );
  jobEntryTrans.setSpecificationMethod( ObjectLocationSpecificationMethod.FILENAME );

  TransMeta transMeta;
  jobEntryTrans.setPassingAllParameters( false );
  transMeta = jobEntryTrans.getTransMeta( rep, null, parentSpace );
  Assert.assertEquals( null, transMeta.getVariable( param1 ) );
  Assert.assertEquals( parentValue2, transMeta.getVariable( param2 ) );
  Assert.assertEquals( childValue3, transMeta.getVariable( param3 ) );

  jobEntryTrans.setPassingAllParameters( true );
  transMeta = jobEntryTrans.getTransMeta( rep, null, parentSpace );
  Assert.assertEquals( parentValue1, transMeta.getVariable( param1 ) );
  Assert.assertEquals( parentValue2, transMeta.getVariable( param2 ) );
  Assert.assertEquals( childValue3, transMeta.getVariable( param3 ) );
}
 
Example 18
Source File: JobEntryTransTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetTransMetaRepo() throws KettleException {
  String param1 = "dir";
  String param2 = "file";
  String parentValue1 = "/home/admin";
  String parentValue2 = "test";

  JobEntryTrans jobEntryTrans = spy( getJobEntryTrans() );
  Repository rep = Mockito.mock( Repository.class );
  RepositoryDirectory repositoryDirectory = Mockito.mock( RepositoryDirectory.class );
  RepositoryDirectoryInterface repositoryDirectoryInterface = Mockito.mock( RepositoryDirectoryInterface.class );
  Mockito.doReturn( repositoryDirectoryInterface ).when( rep ).loadRepositoryDirectoryTree();
  Mockito.doReturn( repositoryDirectory ).when( repositoryDirectoryInterface ).findDirectory( parentValue1 );

  TransMeta meta = new TransMeta();
  meta.setVariable( param2, "childValue2 should be override" );

  Mockito.doReturn( meta ).when( rep )
          .loadTransformation( Mockito.eq( "test" ), Mockito.anyObject(), Mockito.anyObject(), Mockito.anyBoolean(),
                  Mockito.anyObject() );

  VariableSpace parentSpace = new Variables();
  parentSpace.setVariable( param1, parentValue1 );
  parentSpace.setVariable( param2, parentValue2 );

  jobEntryTrans.setDirectory( "${dir}" );
  jobEntryTrans.setTransname( "${file}" );

  Mockito.doNothing().when( jobEntryTrans ).logBasic( Mockito.anyString() );
  jobEntryTrans.setSpecificationMethod( ObjectLocationSpecificationMethod.REPOSITORY_BY_NAME );

  TransMeta transMeta;
  jobEntryTrans.setPassingAllParameters( false );
  transMeta = jobEntryTrans.getTransMeta( rep, null, parentSpace );
  Assert.assertEquals( null, transMeta.getVariable( param1 ) );
  Assert.assertEquals( parentValue2, transMeta.getVariable( param2 ) );

  jobEntryTrans.setPassingAllParameters( true );
  transMeta = jobEntryTrans.getTransMeta( rep, null, parentSpace );
  Assert.assertEquals( parentValue1, transMeta.getVariable( param1 ) );
  Assert.assertEquals( parentValue2, transMeta.getVariable( param2 ) );
}
 
Example 19
Source File: StepWithMappingMetaTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
@PrepareForTest( StepWithMappingMeta.class )
public void loadMappingMetaTest() throws Exception {

  String childParam = "childParam";
  String childValue = "childValue";
  String paramOverwrite = "paramOverwrite";
  String parentParam = "parentParam";
  String parentValue = "parentValue";

  String variablePath = "Internal.Entry.Current.Directory";
  String virtualDir = "/testFolder/CDA-91";
  String fileName = "testTrans.ktr";

  VariableSpace variables = new Variables();
  variables.setVariable( parentParam, parentValue );
  variables.setVariable( paramOverwrite, parentValue );

  StepMeta stepMeta = new StepMeta();
  TransMeta parentTransMeta = new TransMeta();
  stepMeta.setParentTransMeta( parentTransMeta );

  RepositoryDirectoryInterface repositoryDirectory = Mockito.mock( RepositoryDirectoryInterface.class );
  when( repositoryDirectory.toString() ).thenReturn( virtualDir );
  stepMeta.getParentTransMeta().setRepositoryDirectory( repositoryDirectory );

  StepWithMappingMeta mappingMetaMock = mock( StepWithMappingMeta.class );
  when( mappingMetaMock.getSpecificationMethod() ).thenReturn( ObjectLocationSpecificationMethod.FILENAME );
  when( mappingMetaMock.getFileName() ).thenReturn( "${" + variablePath + "}/" + fileName );
  when( mappingMetaMock.getParentStepMeta() ).thenReturn( stepMeta );


  Repository rep = mock( Repository.class );
  Mockito.doReturn( Mockito.mock( RepositoryDirectoryInterface.class ) ).when( rep ).findDirectory( anyString() );

  TransMeta child = new TransMeta();
  child.setVariable( childParam, childValue );
  child.setVariable( paramOverwrite, childValue );
  Mockito.doReturn( child ).when( rep ).loadTransformation( anyString(), any(), any(), anyBoolean(), any() );

  TransMeta transMeta = StepWithMappingMeta.loadMappingMeta( mappingMetaMock, rep, null, variables, true );

  Assert.assertNotNull( transMeta );

  //When the child parameter does exist in the parent parameters, overwrite the child parameter by the parent parameter.
  Assert.assertEquals( parentValue, transMeta.getVariable( paramOverwrite ) );

  //When the child parameter does not exist in the parent parameters, keep it.
  Assert.assertEquals( childValue, transMeta.getVariable( childParam ) );

  //All other parent parameters need to get copied into the child parameters  (when the 'Inherit all
  //variables from the transformation?' option is checked)
  Assert.assertEquals( parentValue, transMeta.getVariable( parentParam ) );
}
 
Example 20
Source File: MongoDbOutputTest.java    From pentaho-mongodb-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Testing the use of Environment Substitution during initialization of fields.
 * @throws Exception
 */
@Test public void testModifierPushObjectWithEnvironmentSubstitution() throws Exception {
  List<MongoDbOutputMeta.MongoField> paths = new ArrayList<MongoDbOutputMeta.MongoField>( 2 );
  MongoDbOutputData data = new MongoDbOutputData();

  VariableSpace vars = new Variables();
  vars.setVariable( "ENV_FIELD", "field1" );
  vars.setVariable( "ENV_DOC_PATH", "bob.fred[].george" );
  vars.setVariable( "ENV_UPDATE_OP", "$push" );

  MongoDbOutputMeta.MongoField mf = mf( "${ENV_FIELD}", true, "${ENV_DOC_PATH}" );
  mf.m_modifierUpdateOperation = "${ENV_UPDATE_OP}";
  mf.m_modifierOperationApplyPolicy = "Insert&Update";
  mf.init( vars );
  paths.add( mf );

  mf = mf( "field2", true, "bob.fred[].george" );
  mf.m_modifierUpdateOperation = "${ENV_UPDATE_OP}";
  mf.m_modifierOperationApplyPolicy = "Insert&Update";
  mf.init( vars );
  paths.add( mf );

  RowMetaInterface rm = new RowMeta();
  rm.addValueMeta( new ValueMetaString( "field1" ) );
  rm.addValueMeta( new ValueMetaString( "field2" ) );


  Object[] dummyRow = new Object[] { "value1", "value2" };

  DBObject
          modifierUpdate =
          data.getModifierUpdateObject( paths, rm, dummyRow, vars, MongoDbOutputData.MongoTopLevel.RECORD );

  assertTrue( modifierUpdate != null );
  assertTrue( modifierUpdate.get( "$push" ) != null );
  DBObject setOpp = (DBObject) modifierUpdate.get( "$push" );

  // in this case, we have the same path up to the array (bob.fred). The
  // remaining
  // terminal fields should be grouped into one record "george" for $push to
  // append
  // to the end of the array
  assertEquals( setOpp.keySet().size(), 1 );

  assertEquals( JSON.serialize( modifierUpdate ),
          "{ \"$push\" : { \"bob.fred\" : { \"george\" : { \"field1\" : \"value1\" , \"field2\" : \"value2\"}}}}" );
}