Java Code Examples for org.pentaho.di.core.row.RowMeta#addValueMeta()

The following examples show how to use org.pentaho.di.core.row.RowMeta#addValueMeta() . 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: HTTPIT.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void test204Answer() throws Exception {
  startHttpServer( get204AnswerHandler() );
  HTTPData data = new HTTPData();
  int[] index = { 0, 1 };
  RowMeta meta = new RowMeta();
  meta.addValueMeta( new ValueMetaString( "fieldName" ) );
  meta.addValueMeta( new ValueMetaInteger( "codeFieldName" ) );
  Object[] expectedRow = new Object[] { "", 204L };
  HTTP http =
    new HTTPHandler( stepMockHelper.stepMeta, data, 0, stepMockHelper.transMeta, stepMockHelper.trans, false );
  RowMetaInterface inputRowMeta = mock( RowMetaInterface.class );
  http.setInputRowMeta( inputRowMeta );
  when( inputRowMeta.clone() ).thenReturn( inputRowMeta );
  when( stepMockHelper.processRowsStepMetaInterface.getUrl() ).thenReturn( HTTP_LOCALHOST_9998 );
  when( stepMockHelper.processRowsStepMetaInterface.getHeaderField() ).thenReturn( new String[] {} );
  when( stepMockHelper.processRowsStepMetaInterface.getArgumentField() ).thenReturn( new String[] {} );
  when( stepMockHelper.processRowsStepMetaInterface.getResultCodeFieldName() ).thenReturn( "ResultCodeFieldName" );
  when( stepMockHelper.processRowsStepMetaInterface.getFieldName() ).thenReturn( "ResultFieldName" );
  http.init( stepMockHelper.processRowsStepMetaInterface, data );
  Assert.assertTrue( http.processRow( stepMockHelper.processRowsStepMetaInterface, data ) );
  Object[] out = ( (HTTPHandler) http ).getOutputRow();
  Assert.assertTrue( meta.equals( out, expectedRow, index ) );
}
 
Example 2
Source File: SalesforceUpdateTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteToSalesForceForNotNullExtIdField_WithExtIdNO() throws Exception {
  SalesforceUpdate sfInputStep =
    new SalesforceUpdate( smh.stepMeta, smh.stepDataInterface, 0, smh.transMeta, smh.trans );
  SalesforceUpdateMeta meta = generateSalesforceUpdateMeta( new String[] { ACCOUNT_ID }, new Boolean[] { false } );
  SalesforceUpdateData data = generateSalesforceUpdateData();
  sfInputStep.init( meta, data );

  RowMeta rowMeta = new RowMeta();
  ValueMetaBase valueMeta = new ValueMetaString( "AccNoExtId" );
  rowMeta.addValueMeta( valueMeta );
  smh.initStepDataInterface.inputRowMeta = rowMeta;

  sfInputStep.writeToSalesForce( new Object[] { "001i000001c5Nv9AAE" } );
  assertEquals( 0, data.sfBuffer[0].getFieldsToNull().length );
  assertEquals( 1, SalesforceConnection.getChildren( data.sfBuffer[0] ).length );
  assertEquals( Constants.PARTNER_SOBJECT_NS,
    SalesforceConnection.getChildren( data.sfBuffer[0] )[0].getName().getNamespaceURI() );
  assertEquals( ACCOUNT_ID, SalesforceConnection.getChildren( data.sfBuffer[0] )[0].getName().getLocalPart() );
  assertEquals( "001i000001c5Nv9AAE", SalesforceConnection.getChildren( data.sfBuffer[0] )[0].getValue() );
  assertFalse( SalesforceConnection.getChildren( data.sfBuffer[0] )[0].hasChildren() );
}
 
Example 3
Source File: SalesforceInsertTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteToSalesForceForNullExtIdField_WithExtIdNO() throws Exception {
  SalesforceInsert sfInputStep =
    new SalesforceInsert( smh.stepMeta, smh.stepDataInterface, 0, smh.transMeta, smh.trans );
  SalesforceInsertMeta meta = generateSalesforceInsertMeta( new String[] { ACCOUNT_ID }, new Boolean[] { false } );
  SalesforceInsertData data = generateSalesforceInsertData();
  sfInputStep.init( meta, data );

  RowMeta rowMeta = new RowMeta();
  ValueMetaBase valueMeta = new ValueMetaString( "AccNoExtId" );
  rowMeta.addValueMeta( valueMeta );
  smh.initStepDataInterface.inputRowMeta = rowMeta;

  sfInputStep.writeToSalesForce( new Object[] { null } );
  assertEquals( 1, data.sfBuffer[0].getFieldsToNull().length );
  assertEquals( ACCOUNT_ID, data.sfBuffer[0].getFieldsToNull()[0] );
  assertNull( SalesforceConnection.getChildren( data.sfBuffer[0] ) );
}
 
Example 4
Source File: CommonFormatShimTest.java    From pentaho-hadoop-shims with Apache License 2.0 6 votes vote down vote up
@Test
public void testAvroReadLocalFileSystem() throws Exception {
  List<String> expectedRows = Arrays.asList( "John;4074549921", "Leslie;4079302194" );
  PentahoAvroInputFormat avroInputFormat = new PentahoAvroInputFormat( mock( NamedCluster.class ) );
  avroInputFormat.setInputSchemaFile( getFilePath( "/sample-schema.avro" ) );
  avroInputFormat.setInputFile( getFilePath( "/sample-data.avro" ) );
  avroInputFormat.setUseFieldAsInputStream( false );
  avroInputFormat.setIsDataBinaryEncoded( true );
  RowMeta outputRowMeta = new RowMeta();
  outputRowMeta.addValueMeta( new ValueMetaString( "foo" ) );
  outputRowMeta.addValueMeta( new ValueMetaString( "bar" ) );
  avroInputFormat.setOutputRowMeta( outputRowMeta );

  List<AvroInputField> inputFields = new ArrayList<>();

  addStringField( inputFields, "FirstName" );
  addStringField( inputFields, "Phone" );
  avroInputFormat.setInputFields( inputFields );

  IPentahoRecordReader recordReader = avroInputFormat.createRecordReader( null );
  assertEquals( expectedRows, generateDataSample( recordReader, inputFields ) );
}
 
Example 5
Source File: SalesforceInsertTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteToSalesForceForNotNullExtIdField_WithExtIdNO() throws Exception {
  SalesforceInsert sfInputStep =
    new SalesforceInsert( smh.stepMeta, smh.stepDataInterface, 0, smh.transMeta, smh.trans );
  SalesforceInsertMeta meta = generateSalesforceInsertMeta( new String[] { ACCOUNT_ID }, new Boolean[] { false } );
  SalesforceInsertData data = generateSalesforceInsertData();
  sfInputStep.init( meta, data );

  RowMeta rowMeta = new RowMeta();
  ValueMetaBase valueMeta = new ValueMetaString( "AccNoExtId" );
  rowMeta.addValueMeta( valueMeta );
  smh.initStepDataInterface.inputRowMeta = rowMeta;

  sfInputStep.writeToSalesForce( new Object[] { "001i000001c5Nv9AAE" } );
  assertEquals( 0, data.sfBuffer[0].getFieldsToNull().length );
  assertEquals( 1, SalesforceConnection.getChildren( data.sfBuffer[0] ).length );
  assertEquals( Constants.PARTNER_SOBJECT_NS,
    SalesforceConnection.getChildren( data.sfBuffer[0] )[0].getName().getNamespaceURI() );
  assertEquals( ACCOUNT_ID, SalesforceConnection.getChildren( data.sfBuffer[0] )[0].getName().getLocalPart() );
  assertEquals( "001i000001c5Nv9AAE", SalesforceConnection.getChildren( data.sfBuffer[0] )[0].getValue() );
  assertFalse( SalesforceConnection.getChildren( data.sfBuffer[0] )[0].hasChildren() );
}
 
Example 6
Source File: RestIT.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoContent() throws Exception {
  RestData data = new RestData();
  int[] index = { 0, 1 };
  RowMeta meta = new RowMeta();
  meta.addValueMeta( new ValueMetaString( "fieldName" ) );
  meta.addValueMeta( new ValueMetaInteger( "codeFieldName" ) );
  Object[] expectedRow = new Object[] { "", 204L };
  Rest rest =
    new RestHandler( stepMockHelper.stepMeta, data, 0, stepMockHelper.transMeta, stepMockHelper.trans, false );
  RowMetaInterface inputRowMeta = mock( RowMetaInterface.class );
  rest.setInputRowMeta( inputRowMeta );
  when( inputRowMeta.clone() ).thenReturn( inputRowMeta );
  when( stepMockHelper.processRowsStepMetaInterface.getUrl() ).thenReturn(
    HTTP_LOCALHOST_9998 + "restTest/restNoContentAnswer" );
  when( stepMockHelper.processRowsStepMetaInterface.getMethod() ).thenReturn( RestMeta.HTTP_METHOD_GET );
  rest.init( stepMockHelper.processRowsStepMetaInterface, data );
  data.resultFieldName = "ResultFieldName";
  data.resultCodeFieldName = "ResultCodeFieldName";
  Assert.assertTrue( rest.processRow( stepMockHelper.processRowsStepMetaInterface, data ) );
  Object[] out = ( (RestHandler) rest ).getOutputRow();
  Assert.assertTrue( meta.equals( out, expectedRow, index ) );
}
 
Example 7
Source File: EditRowsDialog_EmptyStringVsNull_Test.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private void executeAndAssertResults( String[] expected ) throws Exception {
  EditRowsDialog dialog = mock( EditRowsDialog.class );

  when( dialog.getRowForData( any( TableItem.class ), anyInt() ) ).thenCallRealMethod();
  doCallRealMethod().when( dialog ).setRowMeta( any( RowMetaInterface.class ) );
  doCallRealMethod().when( dialog ).setStringRowMeta( any( RowMetaInterface.class ) );

  when( dialog.isDisplayingNullValue( any( TableItem.class ), anyInt() ) ).thenReturn( false );

  RowMeta meta = new RowMeta();
  meta.addValueMeta( new ValueMetaString( "s1" ) );
  meta.addValueMeta( new ValueMetaString( "s2" ) );
  meta.addValueMeta( new ValueMetaString( "s3" ) );
  dialog.setRowMeta( meta );
  dialog.setStringRowMeta( meta );

  TableItem item = mock( TableItem.class );
  when( item.getText( 1 ) ).thenReturn( " " );
  when( item.getText( 2 ) ).thenReturn( "" );
  when( item.getText( 3 ) ).thenReturn( null );

  Object[] data = dialog.getRowForData( item, 0 );
  TransTestingUtil.assertResult( expected, data );
}
 
Example 8
Source File: SalesforceInsertTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testLogMessageInDetailedModeFotWriteToSalesForce() throws KettleException {
  SalesforceInsert sfInputStep =
    new SalesforceInsert( smh.stepMeta, smh.stepDataInterface, 0, smh.transMeta, smh.trans );
  SalesforceInsertMeta meta = generateSalesforceInsertMeta( new String[] { ACCOUNT_ID }, new Boolean[] { false } );
  SalesforceInsertData data = generateSalesforceInsertData();
  sfInputStep.init( meta, data );
  when( sfInputStep.getLogChannel().isDetailed() ).thenReturn( true );

  RowMeta rowMeta = new RowMeta();
  ValueMetaBase valueMeta = new ValueMetaString( "AccNoExtId" );
  rowMeta.addValueMeta( valueMeta );
  smh.initStepDataInterface.inputRowMeta = rowMeta;

  verify( sfInputStep.getLogChannel(), never() ).logDetailed( anyString() );
  sfInputStep.writeToSalesForce( new Object[] { "001i000001c5Nv9AAE" } );
  verify( sfInputStep.getLogChannel() ).logDetailed( "Called writeToSalesForce with 0 out of 2" );
}
 
Example 9
Source File: ScriptValuesModTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void variableIsSetInScopeOfStep() throws Exception {
  ScriptValuesMod step = StepMockUtil.getStep( ScriptValuesMod.class, ScriptValuesMetaMod.class, "test" );

  RowMeta input = new RowMeta();
  input.addValueMeta( new ValueMetaString( "str" ) );
  step.setInputRowMeta( input );

  step = spy( step );
  doReturn( new Object[] { "" } ).when( step ).getRow();

  ScriptValuesMetaMod meta = new ScriptValuesMetaMod();
  meta.setCompatible( false );
  meta.allocate( 1 );
  meta.setFieldname( new String[] { "str" } );
  meta.setType( new int[] { ValueMetaInterface.TYPE_STRING } );
  meta.setReplace( new boolean[] { true } );

  meta.setJSScripts( new ScriptValuesScript[] {
    new ScriptValuesScript( ScriptValuesScript.TRANSFORM_SCRIPT, "script",
      "setVariable('temp', 'pass', 'r');\nstr = getVariable('temp', 'fail');" )
  } );

  ScriptValuesModData data = new ScriptValuesModData();
  step.init( meta, data );

  Object[] expectedRow = { "pass" };
  Object[] row = TransTestingUtil.execute( step, meta, data, 1, false ).get( 0 );
  TransTestingUtil.assertResult( expectedRow, row );
}
 
Example 10
Source File: DatabaseLookup.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private RowMetaInterface copyValueMetasFrom( RowMetaInterface source, int n ) {
  RowMeta result = new RowMeta();
  for ( int i = 0; i < n; i++ ) {
    // don't need cloning here,
    // because value metas will be cloned during iterating through rows
    result.addValueMeta( source.getValueMeta( i ) );
  }
  return result;
}
 
Example 11
Source File: DatabaseLookupUTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void createsReadDefaultCache_AndUsesOnlyNeededFieldsFromMeta() throws Exception {
  Database db = mock( Database.class );
  when( db.getRows( anyString(), anyInt() ) )
    .thenReturn( Arrays.asList( new Object[] { 1L }, new Object[] { 2L } ) );

  RowMeta returnRowMeta = new RowMeta();
  returnRowMeta.addValueMeta( new ValueMetaInteger() );
  returnRowMeta.addValueMeta( new ValueMetaInteger() );
  when( db.getReturnRowMeta() ).thenReturn( returnRowMeta );

  DatabaseLookupMeta meta = createTestMeta();
  DatabaseLookupData data = new DatabaseLookupData();

  DatabaseLookup step = createSpiedStep( db, mockHelper, meta );
  step.init( meta, data );

  data.db = db;
  data.keytypes = new int[] { ValueMetaInterface.TYPE_INTEGER };
  data.allEquals = true;
  data.conditions = new int[] { DatabaseLookupMeta.CONDITION_EQ };

  step.processRow( meta, data );

  data.lookupMeta = new RowMeta();
  data.lookupMeta.addValueMeta( new ValueMetaInteger() );

  assertNotNull( data.cache.getRowFromCache( data.lookupMeta, new Object[] { 1L } ) );
  assertNotNull( data.cache.getRowFromCache( data.lookupMeta, new Object[] { 2L } ) );
}
 
Example 12
Source File: CheckSumTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Create, execute, and return the row listener attached to the output step with complete results from the execution.
 *
 * @param checkSumType
 *          Type of checksum to use (the array index of {@link CheckSumMeta#checksumtypeCodes})
 * @param compatibilityMode
 *          Use compatibility mode for CheckSum
 * @param fieldSeparatorString
 *          The string separate multiple fields with
 * @param inputs
 *          Array of objects representing row data
 * @param inputValueMetas
 *          metas to be processed
 * @return RowListener with results.
 */
private MockRowListener executeHexTest( int checkSumType, boolean compatibilityMode, boolean oldChecksumBehaviour,
    String fieldSeparatorString, Object[] inputs, ValueMetaInterface... inputValueMetas ) throws Exception {

  String[] fieldNames = new String[inputValueMetas.length];
  RowMeta inputRowMeta = new RowMeta();
  for ( int i = 0; i < inputValueMetas.length; i++ ) {
    inputRowMeta.addValueMeta( inputValueMetas[i] );
    fieldNames[i] = inputValueMetas[i].getName();
  }

  Trans trans =
      buildHexadecimalChecksumTrans( checkSumType, compatibilityMode, oldChecksumBehaviour, fieldSeparatorString,
          fieldNames );

  trans.prepareExecution( null );

  StepInterface output = trans.getRunThread( "Output", 0 );
  MockRowListener listener = new MockRowListener();
  output.addRowListener( listener );

  RowProducer rp = trans.addRowProducer( "CheckSum", 0 );

  ( (BaseStep) trans.getRunThread( "CheckSum", 0 ) ).setInputRowMeta( inputRowMeta );

  trans.startThreads();

  rp.putRow( inputRowMeta, inputs );
  rp.finished();

  trans.waitUntilFinished();
  trans.stopAll();
  trans.cleanup();
  return listener;
}
 
Example 13
Source File: DatabaseLookupUTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private DatabaseLookup createSpiedStep( Database db,
                                        StepMockHelper<DatabaseLookupMeta, DatabaseLookupData> mockHelper,
                                        DatabaseLookupMeta meta ) throws KettleException {
  DatabaseLookup step = spyLookup( mockHelper, db, meta.getDatabaseMeta() );
  doNothing().when( step ).determineFieldsTypesQueryingDb();
  doReturn( null ).when( step ).lookupValues( any( RowMetaInterface.class ), any( Object[].class ) );

  RowMeta input = new RowMeta();
  input.addValueMeta( new ValueMetaInteger( "Test" ) );
  step.setInputRowMeta( input );
  return step;
}
 
Example 14
Source File: DatabaseMetaTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void testQuoteReservedWords() {
  DatabaseMeta databaseMeta = mock( DatabaseMeta.class );
  doCallRealMethod().when( databaseMeta ).quoteReservedWords( any( RowMetaInterface.class ) );
  doCallRealMethod().when( databaseMeta ).quoteField( anyString() );
  doCallRealMethod().when( databaseMeta ).setDatabaseInterface( any( DatabaseInterface.class ) );
  doReturn( "\"" ).when( databaseMeta ).getStartQuote();
  doReturn( "\"" ).when( databaseMeta ).getEndQuote();
  final DatabaseInterface databaseInterface = mock( DatabaseInterface.class );
  doReturn( true ).when( databaseInterface ).isQuoteAllFields();
  databaseMeta.setDatabaseInterface( databaseInterface );

  final RowMeta fields = new RowMeta();
  for ( int i = 0; i < 10; i++ ) {
    final ValueMetaInterface valueMeta = new ValueMetaNone( "test_" + i );
    fields.addValueMeta( valueMeta );
  }

  for ( int i = 0; i < 10; i++ ) {
    databaseMeta.quoteReservedWords( fields );
  }

  for ( int i = 0; i < 10; i++ ) {
    databaseMeta.quoteReservedWords( fields );
    final String name = fields.getValueMeta( i ).getName();
    // check valueMeta index in list
    assertTrue( name.contains( "test_" + i ) );
    // check valueMeta is found by quoted name
    assertNotNull( fields.searchValueMeta( name ) );
  }
}
 
Example 15
Source File: PDI5436Test.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private RowMeta mockInputRowMeta() {
  RowMeta inputRowMeta = new RowMeta();
  ValueMetaString nameMeta = new ValueMetaString( "name" );
  nameMeta.setStorageType( ValueMetaInterface.STORAGE_TYPE_BINARY_STRING );
  nameMeta.setStorageMetadata( new ValueMetaString( "name" ) );
  inputRowMeta.addValueMeta( nameMeta );
  ValueMetaString idMeta = new ValueMetaString( "id" );
  idMeta.setStorageType( ValueMetaInterface.STORAGE_TYPE_BINARY_STRING );
  idMeta.setStorageMetadata( new ValueMetaString( "id" ) );
  inputRowMeta.addValueMeta( idMeta );

  return inputRowMeta;
}
 
Example 16
Source File: HTTPPOSTIT.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void testDuplicateNamesInHeader() throws Exception {
  startHttpServer( getDuplicateHeadersHandler() );
  HTTPPOSTData data = new HTTPPOSTData();
  RowMeta meta = new RowMeta();
  meta.addValueMeta( new ValueMetaString( "headerFieldName" ) );
  HTTPPOST HTTPPOST = new HTTPPOSTHandler(
    stepMockHelper.stepMeta, data, 0, stepMockHelper.transMeta, stepMockHelper.trans, false );
  RowMetaInterface inputRowMeta = mock( RowMetaInterface.class );
  HTTPPOST.setInputRowMeta( inputRowMeta );
  when( inputRowMeta.clone() ).thenReturn( inputRowMeta );
  when( stepMockHelper.processRowsStepMetaInterface.getUrl() ).thenReturn( HTTP_LOCALHOST_9998 );
  when( stepMockHelper.processRowsStepMetaInterface.getQueryField() ).thenReturn( new String[] {} );
  when( stepMockHelper.processRowsStepMetaInterface.getArgumentField() ).thenReturn( new String[] {} );
  when( stepMockHelper.processRowsStepMetaInterface.getEncoding() ).thenReturn( "UTF-8" );
  when( stepMockHelper.processRowsStepMetaInterface.getResponseHeaderFieldName() ).thenReturn(
    "ResponseHeaderFieldName" );
  HTTPPOST.init( stepMockHelper.processRowsStepMetaInterface, data );
  Assert.assertTrue( HTTPPOST.processRow( stepMockHelper.processRowsStepMetaInterface, data ) );
  Object[] out = ( (HTTPPOSTHandler) HTTPPOST ).getOutputRow();
  Assert.assertTrue( out.length == 1 );
  JSONParser parser = new JSONParser();
  JSONObject json = (JSONObject) parser.parse( (String) out[ 0 ] );
  Object userAgent = json.get( "User-agent" );
  Assert.assertTrue( "HTTPTool/1.0".equals( userAgent ) );
  Object cookies = json.get( "Set-cookie" );
  Assert.assertTrue( cookies instanceof JSONArray );
  for ( int i = 0; i < 3; i++ ) {
    String cookie = ( (String) ( (JSONArray) cookies ).get( i ) );
    Assert.assertTrue( cookie.startsWith( "cookie" + i ) );
  }
}
 
Example 17
Source File: VariablesTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void testFieldSubstitution() throws KettleValueException {
  Object[] rowData = new Object[]{ "DataOne", "DataTwo" };
  RowMeta rm = new RowMeta();
  rm.addValueMeta( new ValueMetaString( "FieldOne" ) );
  rm.addValueMeta( new ValueMetaString( "FieldTwo" ) );

  Variables vars = new Variables();
  assertNull( vars.fieldSubstitute( null, rm, rowData ) );
  assertEquals( "", vars.fieldSubstitute( "", rm, rowData ) );
  assertEquals( "DataOne", vars.fieldSubstitute( "?{FieldOne}", rm, rowData ) );
  assertEquals( "TheDataOne", vars.fieldSubstitute( "The?{FieldOne}", rm, rowData ) );
}
 
Example 18
Source File: XmlJoinMetaGetFieldsTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetFieldsReturnTargetStepFieldsWithDuplicates() throws Exception {
  // Source Step
  String sourceXmlStep = "source xml step name";
  String sourceStepField1 = "a";
  String sourceStepField2 = "b";

  // Target Step
  String targetXmlStep = "target xml step name";
  String targetStepField1 = "b";
  String targetStepField2 = "c";

  // XML Join Result
  String resultXmlFieldName = "result xml field name";

  // Source Row Meta
  RowMeta rowMetaPreviousSourceStep = new RowMeta();
  rowMetaPreviousSourceStep.addValueMeta( new ValueMeta( sourceStepField1, ValueMetaInterface.TYPE_STRING ) );
  rowMetaPreviousSourceStep.addValueMeta( new ValueMeta( sourceStepField2, ValueMetaInterface.TYPE_STRING ) );

  // Set source step in XML Join step.
  xmlJoinMeta.setSourceXMLstep( sourceXmlStep );
  StepMeta sourceStepMeta = new StepMeta();
  sourceStepMeta.setName( sourceXmlStep );

  doReturn( sourceStepMeta ).when( transMeta ).findStep( sourceXmlStep );
  doReturn( rowMetaPreviousSourceStep ).when( transMeta ).getStepFields( sourceStepMeta, null, null );

  // Target Row Meta
  RowMeta rowMetaPreviousTargetStep = new RowMeta();
  rowMetaPreviousTargetStep.addValueMeta( new ValueMeta( targetStepField1, ValueMetaInterface.TYPE_STRING ) );
  rowMetaPreviousTargetStep.addValueMeta( new ValueMeta( targetStepField2, ValueMetaInterface.TYPE_STRING ) );

  // Set target step in XML Join step.
  xmlJoinMeta.setTargetXMLstep( targetXmlStep );
  StepMeta targetStepMeta = new StepMeta();
  targetStepMeta.setName( targetXmlStep );

  doReturn( targetStepMeta ).when( transMeta ).findStep( targetXmlStep );
  doReturn( rowMetaPreviousTargetStep ).when( transMeta ).getStepFields( targetStepMeta, null, null );

  // Set result field name
  xmlJoinMeta.setValueXMLfield( resultXmlFieldName );

  RowMeta rowMeta = new RowMeta();
  ValueMetaString removeValueMeta1 = new ValueMetaString( "a" );
  rowMeta.addValueMeta( removeValueMeta1 );
  ValueMetaString keepValueMeta1 = new ValueMetaString( "b" );
  rowMeta.addValueMeta( keepValueMeta1 );
  ValueMetaString keepValueMeta2 = new ValueMetaString( "c" );
  rowMeta.addValueMeta( keepValueMeta2 );

  // Get output fields
  xmlJoinMeta.getFields( rowMeta, "testStepName", null, null, transMeta, null, null );
  assertEquals( 3, rowMeta.size() );
  String[] strings = rowMeta.getFieldNames();
  assertEquals( "b", strings[0] );
  assertEquals( "c", strings[1] );
  assertEquals( "result xml field name", strings[2] );
}
 
Example 19
Source File: GroupByTransform.java    From kettle-beam with Apache License 2.0 4 votes vote down vote up
@Override public PCollection<KettleRow> expand( PCollection<KettleRow> input ) {
  try {
    if ( inputRowMeta == null ) {
      BeamKettle.init(stepPluginClasses, xpPluginClasses);

      inputRowMeta = JsonRowMeta.fromJson( rowMetaJson );

      groupRowMeta = new RowMeta();
      for (int i=0;i<groupFields.length;i++) {
        groupRowMeta.addValueMeta( inputRowMeta.searchValueMeta( groupFields[i] ) );
      }
      subjectRowMeta = new RowMeta();
      for (int i=0;i<subjects.length;i++) {
        subjectRowMeta.addValueMeta( inputRowMeta.searchValueMeta( subjects[i] ) );
      }
    }

    // Split the KettleRow into GroupFields-KettleRow and SubjectFields-KettleRow
    //
    PCollection<KV<KettleRow, KettleRow>> groupSubjects = input.apply( ParDo.of(
      new KettleKeyValueFn( rowMetaJson, stepPluginClasses, xpPluginClasses, groupFields, subjects, stepname )
    ) );

    // Now we need to aggregate the groups with a Combine
    GroupByKey<KettleRow, KettleRow> byKey = GroupByKey.<KettleRow, KettleRow>create();
    PCollection<KV<KettleRow, Iterable<KettleRow>>> grouped = groupSubjects.apply( byKey );

    // Aggregate the rows in the grouped PCollection
    //   Input: KV<KettleRow>, Iterable<KettleRow>>
    //   This means that The group rows is in KettleRow.  For every one of these, you get a list of subject rows.
    //   We need to calculate the aggregation of these subject lists
    //   Then we output group values with result values behind it.
    //
    String counterName = stepname+" AGG";
    PCollection<KettleRow> output = grouped.apply( ParDo.of(
      new GroupByFn(counterName, JsonRowMeta.toJson(groupRowMeta), stepPluginClasses, xpPluginClasses,
        JsonRowMeta.toJson(subjectRowMeta), aggregations ) ) );

    return output;
  } catch(Exception e) {
    numErrors.inc();
    LOG.error( "Error in group by transform", e );
    throw new RuntimeException( "Error in group by transform", e );
  }
}
 
Example 20
Source File: RestIT.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
public void testResponseHeader() throws Exception {
  try {
    RestData data = new RestData();
    int[] index = { 0, 1, 3 };
    RowMeta meta = new RowMeta();
    meta.addValueMeta( new ValueMetaString( "fieldName" ) );
    meta.addValueMeta( new ValueMetaInteger( "codeFieldName" ) );
    meta.addValueMeta( new ValueMetaInteger( "responseTimeFieldName" ) );
    meta.addValueMeta( new ValueMetaString( "headerFieldName" ) );
    Object[] expectedRow =
      new Object[] { "", 204L, 0L, "{\"host\":\"localhost\"}" };
    Rest rest =
      new RestHandler( stepMockHelper.stepMeta, data, 0, stepMockHelper.transMeta, stepMockHelper.trans, true );
    RowMetaInterface inputRowMeta = mock( RowMetaInterface.class );
    rest.setInputRowMeta( inputRowMeta );
    when( inputRowMeta.clone() ).thenReturn( inputRowMeta );
    when( stepMockHelper.processRowsStepMetaInterface.getUrl() ).thenReturn(
      HTTP_LOCALHOST_9998 + "restTest/restNoContentAnswer" );
    when( stepMockHelper.processRowsStepMetaInterface.getMethod() ).thenReturn( RestMeta.HTTP_METHOD_GET );
    when( stepMockHelper.processRowsStepMetaInterface.getResponseTimeFieldName() ).thenReturn(
      "ResponseTimeFieldName" );
    when( stepMockHelper.processRowsStepMetaInterface.getResponseHeaderFieldName() ).thenReturn(
      "ResponseHeaderFieldName" );
    rest.init( stepMockHelper.processRowsStepMetaInterface, data );
    data.resultFieldName = "ResultFieldName";
    data.resultCodeFieldName = "ResultCodeFieldName";
    Assert.assertTrue( rest.processRow( stepMockHelper.processRowsStepMetaInterface, data ) );
    Object[] out = ( (RestHandler) rest ).getOutputRow();
    Assert.assertTrue( meta.equals( out, expectedRow, index ) );
  } catch ( ArrayIndexOutOfBoundsException ex ) {
    // This is only here because everything blows up due to the version
    // of Java we're running -vs- the version of ASM we rely on thanks to the
    // version of Jetty we need. When we upgrade to Jetty 8, this NPE will go away.
    // I'm only catching the NPE to allow the test to work as much as it can with
    // the version of Jetty we use.
    // Makes me wonder if we can change the version of jetty used in test cases to
    // the later one to avoid this before we have to go and change the version of
    // Jetty for the rest of the platform.
    //
    // MB - 5/2016
    org.junit.Assert.assertTrue( System.getProperty( "java.version" ).startsWith( "1.8" ) );
  }
}