Search in sources :

Example 51 with Variables

use of org.pentaho.di.core.variables.Variables in project pentaho-kettle by pentaho.

the class StepErrorMetaTest method testGetErrorRowMeta.

@Test
public void testGetErrorRowMeta() {
    VariableSpace vars = new Variables();
    vars.setVariable("VarNumberErrors", "nbrErrors");
    vars.setVariable("VarErrorDescription", "errorDescription");
    vars.setVariable("VarErrorFields", "errorFields");
    vars.setVariable("VarErrorCodes", "errorCodes");
    StepErrorMeta testObject = new StepErrorMeta(vars, new StepMeta(), new StepMeta(), "${VarNumberErrors}", "${VarErrorDescription}", "${VarErrorFields}", "${VarErrorCodes}");
    RowMetaInterface result = testObject.getErrorRowMeta(10, "some data was bad", "factId", "BAD131");
    assertNotNull(result);
    assertEquals(4, result.size());
    assertEquals(ValueMetaInterface.TYPE_INTEGER, result.getValueMeta(0).getType());
    assertEquals("nbrErrors", result.getValueMeta(0).getName());
    assertEquals(ValueMetaInterface.TYPE_STRING, result.getValueMeta(1).getType());
    assertEquals("errorDescription", result.getValueMeta(1).getName());
    assertEquals(ValueMetaInterface.TYPE_STRING, result.getValueMeta(2).getType());
    assertEquals("errorFields", result.getValueMeta(2).getName());
    assertEquals(ValueMetaInterface.TYPE_STRING, result.getValueMeta(3).getType());
    assertEquals("errorCodes", result.getValueMeta(3).getName());
}
Also used : Variables(org.pentaho.di.core.variables.Variables) VariableSpace(org.pentaho.di.core.variables.VariableSpace) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) Test(org.junit.Test)

Example 52 with Variables

use of org.pentaho.di.core.variables.Variables in project pentaho-kettle by pentaho.

the class UniqueRowsIT method testAllUnique.

@Test
public void testAllUnique() throws Exception {
    KettleEnvironment.init();
    // 
    // Create a new transformation...
    // 
    TransMeta transMeta = new TransMeta();
    transMeta.setName("uniquerowstest");
    PluginRegistry registry = PluginRegistry.getInstance();
    // 
    // create an injector step...
    // 
    String injectorStepname = "injector step";
    InjectorMeta im = new InjectorMeta();
    // Set the information of the injector.
    String injectorPid = registry.getPluginId(StepPluginType.class, im);
    StepMeta injectorStep = new StepMeta(injectorPid, injectorStepname, im);
    transMeta.addStep(injectorStep);
    // 
    // Create a unique rows step
    // 
    String uniqueRowsStepname = "unique rows step";
    UniqueRowsMeta urm = new UniqueRowsMeta();
    urm.setCompareFields(new String[] { "KEY" });
    urm.setCaseInsensitive(new boolean[] { true });
    urm.setRejectDuplicateRow(true);
    String uniqueRowsStepPid = registry.getPluginId(StepPluginType.class, urm);
    StepMeta uniqueRowsStep = new StepMeta(uniqueRowsStepPid, uniqueRowsStepname, urm);
    uniqueRowsStep.setDistributes(false);
    transMeta.addStep(uniqueRowsStep);
    transMeta.addTransHop(new TransHopMeta(injectorStep, uniqueRowsStep));
    // 
    // Create a dummy step to receive the unique rows
    // 
    String dummyStepname1 = "dummy step";
    DummyTransMeta dm1 = new DummyTransMeta();
    String dummyPid1 = registry.getPluginId(StepPluginType.class, dm1);
    StepMeta dummyStep1 = new StepMeta(dummyPid1, dummyStepname1, dm1);
    transMeta.addStep(dummyStep1);
    transMeta.addTransHop(new TransHopMeta(uniqueRowsStep, dummyStep1));
    // 
    // Create a dummy step to receive the duplicate rows (errors)
    // 
    String dummyStepname2 = "dummy step2";
    DummyTransMeta dm2 = new DummyTransMeta();
    String dummyPid2 = registry.getPluginId(StepPluginType.class, dm2);
    StepMeta dummyStep2 = new StepMeta(dummyPid2, dummyStepname2, dm2);
    transMeta.addStep(dummyStep2);
    // Set up error (aka duplicates) handling info
    StepErrorMeta stepErrorMeta = new StepErrorMeta(new Variables(), uniqueRowsStep);
    stepErrorMeta.setTargetStep(dummyStep2);
    stepErrorMeta.setEnabled(true);
    stepErrorMeta.setNrErrorsValuename("numErrors");
    stepErrorMeta.setErrorDescriptionsValuename("duplicates");
    stepErrorMeta.setErrorFieldsValuename("KEY");
    stepErrorMeta.setErrorCodesValuename("errorCodes");
    stepErrorMeta.setMaxErrors("9999");
    stepErrorMeta.setMaxPercentErrors("");
    stepErrorMeta.setMinPercentRows("");
    uniqueRowsStep.setStepErrorMeta(stepErrorMeta);
    transMeta.addTransHop(new TransHopMeta(uniqueRowsStep, dummyStep2));
    // Now execute the transformation...
    Trans trans = new Trans(transMeta);
    trans.prepareExecution(null);
    StepInterface si = trans.getStepInterface(dummyStepname1, 0);
    RowStepCollector dummyRc1 = new RowStepCollector();
    si.addRowListener(dummyRc1);
    StepInterface si2 = trans.getStepInterface(dummyStepname2, 0);
    RowStepCollector dummyRc2 = new RowStepCollector();
    si2.addRowListener(dummyRc2);
    RowProducer rp = trans.addRowProducer(injectorStepname, 0);
    trans.startThreads();
    // add rows
    List<RowMetaAndData> inputList = createDataAllUnique();
    for (RowMetaAndData rm : inputList) {
        rp.putRow(rm.getRowMeta(), rm.getData());
    }
    rp.finished();
    trans.waitUntilFinished();
    List<RowMetaAndData> resultRows = dummyRc1.getRowsWritten();
    checkRows(createResultDataAllUnique(), resultRows);
    List<RowMetaAndData> errorRows = dummyRc2.getRowsWritten();
    // There should be no duplicates for this test
    assertEquals(errorRows.size(), 0);
}
Also used : UniqueRowsMeta(org.pentaho.di.trans.steps.uniquerows.UniqueRowsMeta) RowProducer(org.pentaho.di.trans.RowProducer) RowStepCollector(org.pentaho.di.trans.RowStepCollector) TransMeta(org.pentaho.di.trans.TransMeta) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) InjectorMeta(org.pentaho.di.trans.steps.injector.InjectorMeta) StepErrorMeta(org.pentaho.di.trans.step.StepErrorMeta) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) StepMeta(org.pentaho.di.trans.step.StepMeta) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) Variables(org.pentaho.di.core.variables.Variables) StepInterface(org.pentaho.di.trans.step.StepInterface) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) PluginRegistry(org.pentaho.di.core.plugins.PluginRegistry) TransHopMeta(org.pentaho.di.trans.TransHopMeta) Trans(org.pentaho.di.trans.Trans) Test(org.junit.Test)

Example 53 with Variables

use of org.pentaho.di.core.variables.Variables in project pentaho-kettle by pentaho.

the class BlackBoxIT method runTrans.

public Result runTrans(String fileName, LogChannelInterface log) throws KettleException {
    // Bootstrap the Kettle API...
    // 
    KettleEnvironment.init();
    TransMeta transMeta = new TransMeta(fileName);
    Trans trans = new Trans(transMeta);
    Result result;
    try {
        trans.setLogLevel(LogLevel.ERROR);
        result = trans.getResult();
    } catch (Exception e) {
        result = trans.getResult();
        String message = "Processing has stopped because of an error: " + getPath(fileName);
        addFailure(message);
        log.logError(message, e);
        fail(message);
        return result;
    }
    try {
        trans.initializeVariablesFrom(null);
        trans.getTransMeta().setInternalKettleVariables(trans);
        trans.setSafeModeEnabled(true);
        // see if the transformation checks ok
        List<CheckResultInterface> remarks = new ArrayList<CheckResultInterface>();
        trans.getTransMeta().checkSteps(remarks, false, null, new Variables(), null, null);
        for (CheckResultInterface remark : remarks) {
            if (remark.getType() == CheckResultInterface.TYPE_RESULT_ERROR) {
                // add this to the log
                addFailure("Check error: " + getPath(fileName) + ", " + remark.getErrorCode());
                log.logError("BlackBoxTest", "Check error: " + getPath(fileName) + ", " + remark.getErrorCode());
            }
        }
        // allocate & run the required sub-threads
        try {
            trans.execute(null);
        } catch (Exception e) {
            addFailure("Unable to prepare and initialize this transformation: " + getPath(fileName));
            log.logError("BlackBoxTest", "Unable to prepare and initialize this transformation: " + getPath(fileName));
            fail("Unable to prepare and initialize this transformation: " + getPath(fileName));
            return null;
        }
        trans.waitUntilFinished();
        result = trans.getResult();
        // The result flag is not set to true by a transformation - set it to true if got no errors
        // FIXME: Find out if there is a better way to check if a transformation has thrown an error
        result.setResult(result.getNrErrors() == 0);
        return result;
    } catch (Exception e) {
        addFailure("Unexpected error occurred: " + getPath(fileName));
        log.logError("BlackBoxTest", "Unexpected error occurred: " + getPath(fileName), e);
        result.setResult(false);
        result.setNrErrors(1);
        fail("Unexpected error occurred: " + getPath(fileName));
        return result;
    }
}
Also used : Variables(org.pentaho.di.core.variables.Variables) TransMeta(org.pentaho.di.trans.TransMeta) ArrayList(java.util.ArrayList) CheckResultInterface(org.pentaho.di.core.CheckResultInterface) Trans(org.pentaho.di.trans.Trans) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) Result(org.pentaho.di.core.Result)

Example 54 with Variables

use of org.pentaho.di.core.variables.Variables in project pentaho-kettle by pentaho.

the class JaninoStepIT method testIntegerReturnType.

@Test
public void testIntegerReturnType() throws KettleException {
    String STEP_NAME = "JaninoStep";
    JaninoMeta meta = new JaninoMeta();
    meta.setDefault();
    JaninoMetaFunction[] formulas = { new JaninoMetaFunction("intretval", "2", ValueMetaInterface.TYPE_INTEGER, -1, -1, null) };
    meta.setFormula(formulas);
    TransMeta transMeta = TransTestFactory.generateTestTransformation(new Variables(), meta, STEP_NAME);
    List<RowMetaAndData> inputData = new ArrayList<RowMetaAndData>();
    inputData.add(new RowMetaAndData(new RowMeta(), new Object[0]));
    List<RowMetaAndData> resultData = TransTestFactory.executeTestTransformation(transMeta, TransTestFactory.INJECTOR_STEPNAME, STEP_NAME, TransTestFactory.DUMMY_STEPNAME, inputData);
    assertNotNull(resultData);
    assertEquals(1, resultData.size());
    assertEquals(1, resultData.get(0).size());
    assertEquals("intretval", resultData.get(0).getValueMeta(0).getName());
    assertEquals(ValueMetaInterface.TYPE_INTEGER, resultData.get(0).getValueMeta(0).getType());
    assertEquals(Long.valueOf(2), resultData.get(0).getInteger(0));
}
Also used : Variables(org.pentaho.di.core.variables.Variables) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) RowMeta(org.pentaho.di.core.row.RowMeta) TransMeta(org.pentaho.di.trans.TransMeta) DummyTransMeta(org.pentaho.di.trans.steps.dummytrans.DummyTransMeta) ArrayList(java.util.ArrayList) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Test(org.junit.Test)

Example 55 with Variables

use of org.pentaho.di.core.variables.Variables in project pentaho-kettle by pentaho.

the class JavaFilterIT method testVariableCondition.

@Test
public void testVariableCondition() throws KettleException {
    Variables varSpace = new Variables();
    varSpace.setVariable("myVarCondition", realCondition);
    JavaFilterMeta meta = new JavaFilterMeta();
    meta.setCondition("${myVarCondition}");
    TransMeta trans = TransTestFactory.generateTestTransformation(varSpace, meta, stepName);
    List<RowMetaAndData> input = getInputData();
    List<RowMetaAndData> result = TransTestFactory.executeTestTransformation(trans, TransTestFactory.INJECTOR_STEPNAME, stepName, TransTestFactory.DUMMY_STEPNAME, input);
    // Only the "Car" row should be returned, based upon the Java Filter condition
    assertTrue(result.size() == 1);
    assertTrue(result.get(0).getString(fieldName, null).equals("Car"));
}
Also used : Variables(org.pentaho.di.core.variables.Variables) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) TransMeta(org.pentaho.di.trans.TransMeta) Test(org.junit.Test)

Aggregations

Variables (org.pentaho.di.core.variables.Variables)119 Test (org.junit.Test)67 TransMeta (org.pentaho.di.trans.TransMeta)31 ArrayList (java.util.ArrayList)25 CheckResultInterface (org.pentaho.di.core.CheckResultInterface)20 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)20 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)20 StepMeta (org.pentaho.di.trans.step.StepMeta)18 RowMeta (org.pentaho.di.core.row.RowMeta)17 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)16 VariableSpace (org.pentaho.di.core.variables.VariableSpace)14 Repository (org.pentaho.di.repository.Repository)12 TableView (org.pentaho.di.ui.core.widget.TableView)10 FormAttachment (org.eclipse.swt.layout.FormAttachment)9 FormData (org.eclipse.swt.layout.FormData)9 ColumnInfo (org.pentaho.di.ui.core.widget.ColumnInfo)9 Label (org.eclipse.swt.widgets.Label)8 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)7 SelectionEvent (org.eclipse.swt.events.SelectionEvent)7 DummyTransMeta (org.pentaho.di.trans.steps.dummytrans.DummyTransMeta)7