Search in sources :

Example 11 with IRowSet

use of org.apache.hop.core.IRowSet in project hop by apache.

the class CalculatorUnitTest method testAddSeconds.

@Test
public void testAddSeconds() throws HopException {
    RowMeta inputRowMeta = new RowMeta();
    ValueMetaDate dayMeta = new ValueMetaDate("Day");
    inputRowMeta.addValueMeta(dayMeta);
    ValueMetaInteger secondsMeta = new ValueMetaInteger("Seconds");
    inputRowMeta.addValueMeta(secondsMeta);
    IRowSet inputRowSet = null;
    try {
        inputRowSet = smh.getMockInputRowSet(new Object[][] { { new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-01-01 00:00:00"), new Long(10) }, { new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-10-31 23:59:50"), new Long(30) } });
    } catch (ParseException pe) {
        pe.printStackTrace();
        fail();
    }
    inputRowSet.setRowMeta(inputRowMeta);
    CalculatorMeta meta = new CalculatorMeta();
    meta.getFunctions().add(new CalculatorMetaFunction("new_day", CalculationType.ADD_SECONDS, "Day", "Seconds", null, "Date", 0, 0, "", "", "", "", false));
    CalculatorData data = new CalculatorData();
    Calculator calculator = new Calculator(smh.transformMeta, meta, data, 0, smh.pipelineMeta, smh.pipeline);
    calculator.addRowSetToInputRowSets(inputRowSet);
    calculator.setInputRowMeta(inputRowMeta);
    calculator.init();
    // Verify output
    try {
        calculator.addRowListener(new RowAdapter() {

            @Override
            public void rowWrittenEvent(IRowMeta rowMeta, Object[] row) throws HopTransformException {
                try {
                    assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2014-01-01 00:00:10"), row[2]);
                } catch (ParseException pe) {
                    throw new HopTransformException(pe);
                }
            }
        });
        calculator.processRow();
    } catch (HopException ke) {
        ke.printStackTrace();
        fail();
    }
}
Also used : RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) HopException(org.apache.hop.core.exception.HopException) IRowMeta(org.apache.hop.core.row.IRowMeta) HopTransformException(org.apache.hop.core.exception.HopTransformException) IRowSet(org.apache.hop.core.IRowSet) RowAdapter(org.apache.hop.pipeline.transform.RowAdapter) ILoggingObject(org.apache.hop.core.logging.ILoggingObject) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 12 with IRowSet

use of org.apache.hop.core.IRowSet in project hop by apache.

the class CalculatorUnitTest method assertRoundGeneral.

public void assertRoundGeneral(final Object expectedResult, final CalculationType calcFunction, final Number value, final Long precision, final Long roundingMode, final int valueDataType, final int functionDataType) throws HopException {
    final String msg = getHopTypeName(valueDataType) + "->" + getHopTypeName(functionDataType) + " ";
    final RowMeta inputRowMeta = new RowMeta();
    final List<Object> inputValues = new ArrayList<>(3);
    final String fieldValue = "Value";
    final IValueMeta valueMeta;
    switch(valueDataType) {
        case IValueMeta.TYPE_BIGNUMBER:
            valueMeta = new ValueMetaBigNumber(fieldValue);
            break;
        case IValueMeta.TYPE_NUMBER:
            valueMeta = new ValueMetaNumber(fieldValue);
            break;
        case IValueMeta.TYPE_INTEGER:
            valueMeta = new ValueMetaInteger(fieldValue);
            break;
        default:
            throw new IllegalArgumentException(msg + "Unexpected value dataType: " + value.getClass().getName() + ". Long, Double or BigDecimal expected.");
    }
    inputRowMeta.addValueMeta(valueMeta);
    inputValues.add(value);
    final String fieldPrecision;
    final ValueMetaInteger precisionMeta;
    if (precision == null) {
        fieldPrecision = null;
        precisionMeta = null;
    } else {
        fieldPrecision = "Precision";
        precisionMeta = new ValueMetaInteger(fieldPrecision);
        inputRowMeta.addValueMeta(precisionMeta);
        inputValues.add(precision);
    }
    final String fieldRoundingMode;
    final ValueMetaInteger roundingModeMeta;
    if (roundingMode == null) {
        fieldRoundingMode = null;
        roundingModeMeta = null;
    } else {
        fieldRoundingMode = "RoundingMode";
        roundingModeMeta = new ValueMetaInteger(fieldRoundingMode);
        inputRowMeta.addValueMeta(roundingModeMeta);
        inputValues.add(roundingMode);
    }
    IRowSet inputRowSet = smh.getMockInputRowSet(inputValues.toArray());
    inputRowSet.setRowMeta(inputRowMeta);
    final String fieldA = inputRowMeta.size() > 0 ? inputRowMeta.getValueMetaList().get(0).getName() : null;
    final String fieldB = inputRowMeta.size() > 1 ? inputRowMeta.getValueMetaList().get(1).getName() : null;
    final String fieldC = inputRowMeta.size() > 2 ? inputRowMeta.getValueMetaList().get(2).getName() : null;
    final int resultDataType = functionDataType;
    final String fieldResult = "test";
    final int expectedResultRowSize = inputRowMeta.size() + 1;
    CalculatorMeta meta = new CalculatorMeta();
    meta.getFunctions().add(new CalculatorMetaFunction(fieldResult, calcFunction, fieldA, fieldB, fieldC, ValueMetaFactory.getValueMetaName(resultDataType), 2, 0, "", "", "", "", false));
    CalculatorData data = new CalculatorData();
    Calculator calculator = new Calculator(smh.transformMeta, meta, data, 0, smh.pipelineMeta, smh.pipeline);
    calculator.addRowSetToInputRowSets(inputRowSet);
    calculator.setInputRowMeta(inputRowMeta);
    calculator.init();
    // Verify output
    try {
        calculator.addRowListener(new RowAdapter() {

            @Override
            public void rowWrittenEvent(IRowMeta rowMeta, Object[] row) throws HopTransformException {
                assertEquals(msg + " resultRowSize", expectedResultRowSize, rowMeta.size());
                final int fieldResultIndex = rowMeta.size() - 1;
                assertEquals(msg + " fieldResult", fieldResult, rowMeta.getValueMeta(fieldResultIndex).getName());
                assertEquals(msg, expectedResult, row[fieldResultIndex]);
            }
        });
        calculator.processRow();
    } catch (HopException ke) {
        ke.printStackTrace();
        fail(msg + ke.getMessage());
    }
}
Also used : RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) HopException(org.apache.hop.core.exception.HopException) IRowMeta(org.apache.hop.core.row.IRowMeta) ArrayList(java.util.ArrayList) HopTransformException(org.apache.hop.core.exception.HopTransformException) IValueMeta(org.apache.hop.core.row.IValueMeta) IRowSet(org.apache.hop.core.IRowSet) RowAdapter(org.apache.hop.pipeline.transform.RowAdapter) ILoggingObject(org.apache.hop.core.logging.ILoggingObject)

Example 13 with IRowSet

use of org.apache.hop.core.IRowSet in project hop by apache.

the class CalculatorUnitTest method calculatorShouldClearDataInstance.

@Test
public void calculatorShouldClearDataInstance() throws Exception {
    RowMeta inputRowMeta = new RowMeta();
    ValueMetaInteger valueMeta = new ValueMetaInteger("Value");
    inputRowMeta.addValueMeta(valueMeta);
    IRowSet inputRowSet = smh.getMockInputRowSet(new Object[] { -1L });
    inputRowSet.setRowMeta(inputRowMeta);
    CalculatorMeta meta = new CalculatorMeta();
    meta.getFunctions().add(new CalculatorMetaFunction("test", CalculationType.ABS, "Value", null, null, "String", 0, 0, "", "", "", "", false));
    CalculatorData data = spy(new CalculatorData());
    Calculator calculator = new Calculator(smh.transformMeta, meta, data, 0, smh.pipelineMeta, smh.pipeline);
    calculator.addRowSetToInputRowSets(inputRowSet);
    calculator.setInputRowMeta(inputRowMeta);
    calculator.init();
    calculator.processRow();
    verify(data).getValueMetaFor(eq(valueMeta.getType()), anyString());
    calculator.processRow();
    verify(data).clearValuesMetaMapping();
}
Also used : RowMeta(org.apache.hop.core.row.RowMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) IRowSet(org.apache.hop.core.IRowSet)

Example 14 with IRowSet

use of org.apache.hop.core.IRowSet in project hop by apache.

the class FuzzyMatchTest method testReadLookupValues.

@Test
public void testReadLookupValues() throws Exception {
    FuzzyMatchData data = spy(new FuzzyMatchData());
    data.indexOfCachedFields = new int[2];
    data.minimalDistance = 0;
    data.maximalDistance = 5;
    FuzzyMatchMeta meta = spy(new FuzzyMatchMeta());
    meta.setOutputMatchField("I don't want NPE here!");
    data.readLookupValues = true;
    fuzzyMatch = new FuzzyMatchHandler(mockHelper.transformMeta, meta, data, 0, mockHelper.pipelineMeta, mockHelper.pipeline);
    fuzzyMatch.init();
    IRowSet lookupRowSet = mockHelper.getMockInputRowSet(binaryLookupRows);
    fuzzyMatch.addRowSetToInputRowSets(mockHelper.getMockInputRowSet(binaryRows));
    fuzzyMatch.addRowSetToInputRowSets(lookupRowSet);
    fuzzyMatch.rowset = lookupRowSet;
    IRowMeta iRowMeta = new RowMeta();
    IValueMeta valueMeta = new ValueMetaString("field1");
    valueMeta.setStorageMetadata(new ValueMetaString("field1"));
    valueMeta.setStorageType(IValueMeta.STORAGE_TYPE_BINARY_STRING);
    iRowMeta.addValueMeta(valueMeta);
    when(lookupRowSet.getRowMeta()).thenReturn(iRowMeta);
    when(meta.getLookupField()).thenReturn("field1");
    when(meta.getMainStreamField()).thenReturn("field1");
    fuzzyMatch.setInputRowMeta(iRowMeta.clone());
    when(meta.getAlgorithmType()).thenReturn(1);
    ITransformIOMeta transformIOMetaInterface = mock(ITransformIOMeta.class);
    when(meta.getTransformIOMeta()).thenReturn(transformIOMetaInterface);
    IStream streamInterface = mock(IStream.class);
    List<IStream> streamInterfaceList = new ArrayList<>();
    streamInterfaceList.add(streamInterface);
    when(streamInterface.getTransformMeta()).thenReturn(mockHelper.transformMeta);
    when(transformIOMetaInterface.getInfoStreams()).thenReturn(streamInterfaceList);
    fuzzyMatch.processRow();
    Assert.assertEquals(iRowMeta.getString(row3B, 0), data.outputRowMeta.getString(fuzzyMatch.resultRow, 1));
}
Also used : IValueMeta(org.apache.hop.core.row.IValueMeta) ValueMetaString(org.apache.hop.core.row.value.ValueMetaString) ITransformIOMeta(org.apache.hop.pipeline.transform.ITransformIOMeta) IRowMeta(org.apache.hop.core.row.IRowMeta) RowMeta(org.apache.hop.core.row.RowMeta) IRowSet(org.apache.hop.core.IRowSet) IRowMeta(org.apache.hop.core.row.IRowMeta) IStream(org.apache.hop.pipeline.transform.stream.IStream) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 15 with IRowSet

use of org.apache.hop.core.IRowSet in project hop by apache.

the class FuzzyMatchTest method testLookupValuesWhenMainFieldIsNull.

@Test
public void testLookupValuesWhenMainFieldIsNull() throws Exception {
    FuzzyMatchData data = spy(new FuzzyMatchData());
    FuzzyMatchMeta meta = spy(new FuzzyMatchMeta());
    data.readLookupValues = false;
    fuzzyMatch = new FuzzyMatchHandler(mockHelper.transformMeta, meta, data, 0, mockHelper.pipelineMeta, mockHelper.pipeline);
    fuzzyMatch.init();
    fuzzyMatch.first = false;
    data.indexOfMainField = 1;
    Object[] inputRow = { "test input", null };
    IRowSet lookupRowSet = mockHelper.getMockInputRowSet(new Object[] { "test lookup" });
    fuzzyMatch.addRowSetToInputRowSets(mockHelper.getMockInputRowSet(inputRow));
    fuzzyMatch.addRowSetToInputRowSets(lookupRowSet);
    fuzzyMatch.rowset = lookupRowSet;
    IRowMeta iRowMeta = new RowMeta();
    IValueMeta valueMeta = new ValueMetaString("field1");
    valueMeta.setStorageMetadata(new ValueMetaString("field1"));
    valueMeta.setStorageType(IValueMeta.TYPE_STRING);
    iRowMeta.addValueMeta(valueMeta);
    when(lookupRowSet.getRowMeta()).thenReturn(iRowMeta);
    fuzzyMatch.setInputRowMeta(iRowMeta.clone());
    data.outputRowMeta = iRowMeta.clone();
    fuzzyMatch.processRow();
    Assert.assertEquals(inputRow[0], fuzzyMatch.resultRow[0]);
    Assert.assertNull(fuzzyMatch.resultRow[1]);
    Assert.assertTrue(Arrays.stream(fuzzyMatch.resultRow, 3, fuzzyMatch.resultRow.length).allMatch(val -> val == null));
}
Also used : InjectMocks(org.mockito.InjectMocks) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ITransformIOMeta(org.apache.hop.pipeline.transform.ITransformIOMeta) IStream(org.apache.hop.pipeline.transform.stream.IStream) IRowMeta(org.apache.hop.core.row.IRowMeta) Arrays(java.util.Arrays) RowMeta(org.apache.hop.core.row.RowMeta) ILoggingObject(org.apache.hop.core.logging.ILoggingObject) Test(org.junit.Test) TransformMeta(org.apache.hop.pipeline.transform.TransformMeta) ArrayList(java.util.ArrayList) HopTransformException(org.apache.hop.core.exception.HopTransformException) HashSet(java.util.HashSet) IValueMeta(org.apache.hop.core.row.IValueMeta) Mockito(org.mockito.Mockito) List(java.util.List) IRowSet(org.apache.hop.core.IRowSet) Pipeline(org.apache.hop.pipeline.Pipeline) After(org.junit.After) ValueMetaString(org.apache.hop.core.row.value.ValueMetaString) TransformMockHelper(org.apache.hop.pipeline.transforms.mock.TransformMockHelper) Assert(org.junit.Assert) PipelineMeta(org.apache.hop.pipeline.PipelineMeta) Before(org.junit.Before) IValueMeta(org.apache.hop.core.row.IValueMeta) ValueMetaString(org.apache.hop.core.row.value.ValueMetaString) IRowMeta(org.apache.hop.core.row.IRowMeta) RowMeta(org.apache.hop.core.row.RowMeta) IRowSet(org.apache.hop.core.IRowSet) IRowMeta(org.apache.hop.core.row.IRowMeta) ILoggingObject(org.apache.hop.core.logging.ILoggingObject) Test(org.junit.Test)

Aggregations

IRowSet (org.apache.hop.core.IRowSet)62 IRowMeta (org.apache.hop.core.row.IRowMeta)34 RowMeta (org.apache.hop.core.row.RowMeta)22 ILoggingObject (org.apache.hop.core.logging.ILoggingObject)17 ValueMetaString (org.apache.hop.core.row.value.ValueMetaString)15 HopException (org.apache.hop.core.exception.HopException)14 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)11 QueueRowSet (org.apache.hop.core.QueueRowSet)11 HopTransformException (org.apache.hop.core.exception.HopTransformException)10 IValueMeta (org.apache.hop.core.row.IValueMeta)6 IStream (org.apache.hop.pipeline.transform.stream.IStream)6 RowAdapter (org.apache.hop.pipeline.transform.RowAdapter)5 File (java.io.File)4 FileObject (org.apache.commons.vfs2.FileObject)4 SingleRowRowSet (org.apache.hop.core.SingleRowRowSet)4 HopFileException (org.apache.hop.core.exception.HopFileException)4 ITransformIOMeta (org.apache.hop.pipeline.transform.ITransformIOMeta)4 TransformMeta (org.apache.hop.pipeline.transform.TransformMeta)4 ParseException (java.text.ParseException)3