Search in sources :

Example 41 with VariableSpace

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

the class TransMetaTest method testLoadXml.

@Test
public void testLoadXml() throws KettleException {
    String directory = "/home/admin";
    Node jobNode = Mockito.mock(Node.class);
    NodeList nodeList = new NodeList() {

        ArrayList<Node> nodes = new ArrayList<>();

        {
            Node nodeInfo = Mockito.mock(Node.class);
            Mockito.when(nodeInfo.getNodeName()).thenReturn(TransMeta.XML_TAG_INFO);
            Mockito.when(nodeInfo.getChildNodes()).thenReturn(this);
            Node nodeDirectory = Mockito.mock(Node.class);
            Mockito.when(nodeDirectory.getNodeName()).thenReturn("directory");
            Node child = Mockito.mock(Node.class);
            Mockito.when(nodeDirectory.getFirstChild()).thenReturn(child);
            Mockito.when(child.getNodeValue()).thenReturn(directory);
            nodes.add(nodeDirectory);
            nodes.add(nodeInfo);
        }

        @Override
        public Node item(int index) {
            return nodes.get(index);
        }

        @Override
        public int getLength() {
            return nodes.size();
        }
    };
    Mockito.when(jobNode.getChildNodes()).thenReturn(nodeList);
    Repository rep = Mockito.mock(Repository.class);
    RepositoryDirectory repDirectory = new RepositoryDirectory(new RepositoryDirectory(new RepositoryDirectory(), "home"), "admin");
    Mockito.when(rep.findDirectory(Mockito.eq(directory))).thenReturn(repDirectory);
    TransMeta meta = new TransMeta();
    VariableSpace variableSpace = Mockito.mock(VariableSpace.class);
    Mockito.when(variableSpace.listVariables()).thenReturn(new String[0]);
    meta.loadXML(jobNode, null, Mockito.mock(IMetaStore.class), rep, false, variableSpace, Mockito.mock(OverwritePrompter.class));
    meta.setInternalKettleVariables(null);
    assertEquals(repDirectory.getPath(), meta.getVariable(Const.INTERNAL_VARIABLE_TRANSFORMATION_REPOSITORY_DIRECTORY));
}
Also used : Repository(org.pentaho.di.repository.Repository) RepositoryDirectory(org.pentaho.di.repository.RepositoryDirectory) VariableSpace(org.pentaho.di.core.variables.VariableSpace) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) OverwritePrompter(org.pentaho.di.core.gui.OverwritePrompter) IMetaStore(org.pentaho.metastore.api.IMetaStore) Test(org.junit.Test)

Example 42 with VariableSpace

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

the class ReplaceStringMetaTest method testGetFields.

@Test
public void testGetFields() throws KettleStepException {
    ReplaceStringMeta meta = new ReplaceStringMeta();
    meta.setFieldInStream(new String[] { FIELD_NAME });
    meta.setFieldOutStream(new String[] { FIELD_NAME });
    ValueMetaInterface inputFieldMeta = mock(ValueMetaInterface.class);
    when(inputFieldMeta.getStringEncoding()).thenReturn(ENCODING_NAME);
    RowMetaInterface inputRowMeta = mock(RowMetaInterface.class);
    when(inputRowMeta.searchValueMeta(anyString())).thenReturn(inputFieldMeta);
    StepMeta nextStep = mock(StepMeta.class);
    VariableSpace space = mock(VariableSpace.class);
    Repository repository = mock(Repository.class);
    IMetaStore metaStore = mock(IMetaStore.class);
    meta.getFields(inputRowMeta, "test", null, nextStep, space, repository, metaStore);
    ArgumentCaptor<ValueMetaInterface> argument = ArgumentCaptor.forClass(ValueMetaInterface.class);
    verify(inputRowMeta).addValueMeta(argument.capture());
    assertEquals(ENCODING_NAME, argument.getValue().getStringEncoding());
}
Also used : Repository(org.pentaho.di.repository.Repository) VariableSpace(org.pentaho.di.core.variables.VariableSpace) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) StepMeta(org.pentaho.di.trans.step.StepMeta) IMetaStore(org.pentaho.metastore.api.IMetaStore) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) Test(org.junit.Test)

Example 43 with VariableSpace

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

the class TextFileInputMetaTest method setUp.

@Before
public void setUp() throws Exception {
    NamedClusterEmbedManager manager = mock(NamedClusterEmbedManager.class);
    TransMeta parentTransMeta = mock(TransMeta.class);
    doReturn(manager).when(parentTransMeta).getNamedClusterEmbedManager();
    StepMeta parentStepMeta = mock(StepMeta.class);
    doReturn(parentTransMeta).when(parentStepMeta).getParentTransMeta();
    inputMeta = new TextFileInputMeta();
    inputMeta.setParentStepMeta(parentStepMeta);
    inputMeta = spy(inputMeta);
    variableSpace = mock(VariableSpace.class);
    doReturn("<def>").when(variableSpace).environmentSubstitute(anyString());
    doReturn(FILE_NAME_VALID_PATH).when(variableSpace).environmentSubstitute(FILE_NAME_VALID_PATH);
    FileObject mockedFileObject = mock(FileObject.class);
    doReturn(mockedFileObject).when(inputMeta).getFileObject(anyString(), eq(variableSpace));
}
Also used : VariableSpace(org.pentaho.di.core.variables.VariableSpace) NamedClusterEmbedManager(org.pentaho.di.trans.steps.named.cluster.NamedClusterEmbedManager) TransMeta(org.pentaho.di.trans.TransMeta) FileObject(org.apache.commons.vfs2.FileObject) StepMeta(org.pentaho.di.trans.step.StepMeta) Before(org.junit.Before)

Example 44 with VariableSpace

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

the class TextFileOutputTest method doOutput.

private List<Throwable> doOutput(TextFileField[] textFileField, List<Object[]> rows, String pathToFile, String endedLine, Boolean isHeaderEnabled, Boolean isDoNotOpenNewFileInit, Boolean append) throws KettleException {
    TextFileOutputData textFileOutputData = new TextFileOutputData();
    TextFileOutput textFileOutput = new TextFileOutputTestHandler(stepMockHelper.stepMeta, textFileOutputData, 0, stepMockHelper.transMeta, stepMockHelper.trans);
    // init step meta and process step meta should be the same in this case
    Mockito.when(stepMockHelper.processRowsStepMetaInterface.isDoNotOpenNewFileInit()).thenReturn(isDoNotOpenNewFileInit);
    Mockito.when(stepMockHelper.processRowsStepMetaInterface.isFileAppended()).thenReturn(append);
    Mockito.when(stepMockHelper.processRowsStepMetaInterface.isHeaderEnabled()).thenReturn(isHeaderEnabled);
    Mockito.when(stepMockHelper.processRowsStepMetaInterface.getFileName()).thenReturn(pathToFile);
    Mockito.when(stepMockHelper.processRowsStepMetaInterface.buildFilename(Mockito.anyString(), Mockito.anyString(), ((VariableSpace) Mockito.anyObject()), Mockito.anyInt(), Mockito.anyString(), Mockito.anyInt(), Mockito.anyBoolean(), (TextFileOutputMeta) Mockito.anyObject())).thenReturn(pathToFile);
    Mockito.when(stepMockHelper.processRowsStepMetaInterface.getOutputFields()).thenReturn(textFileField);
    textFileOutput.init(stepMockHelper.processRowsStepMetaInterface, textFileOutputData);
    // Process rows
    RowSet rowSet = stepMockHelper.getMockInputRowSet(rows);
    RowMetaInterface inputRowMeta = Mockito.mock(RowMetaInterface.class);
    textFileOutput.setInputRowMeta(inputRowMeta);
    Mockito.when(rowSet.getRowWait(Mockito.anyInt(), (TimeUnit) Mockito.anyObject())).thenReturn(rows.isEmpty() ? null : rows.iterator().next());
    Mockito.when(rowSet.getRowMeta()).thenReturn(inputRowMeta);
    Mockito.when(inputRowMeta.clone()).thenReturn(inputRowMeta);
    for (int i = 0; i < textFileField.length; i++) {
        String name = textFileField[i].getName();
        ValueMetaString valueMetaString = new ValueMetaString(name);
        Mockito.when(inputRowMeta.getValueMeta(i)).thenReturn(valueMetaString);
        Mockito.when(inputRowMeta.indexOfValue(name)).thenReturn(i);
    }
    textFileOutput.addRowSetToInputRowSets(rowSet);
    textFileOutput.addRowSetToOutputRowSets(rowSet);
    Mockito.when(stepMockHelper.processRowsStepMetaInterface.getEndedLine()).thenReturn(endedLine);
    Mockito.when(stepMockHelper.processRowsStepMetaInterface.isFastDump()).thenReturn(true);
    for (int i = 0; i < rows.size(); i++) {
        ((TextFileOutputTestHandler) textFileOutput).setRow(rows.get(i));
        textFileOutput.processRow(stepMockHelper.processRowsStepMetaInterface, textFileOutputData);
    }
    ((TextFileOutputTestHandler) textFileOutput).setRow(null);
    textFileOutput.processRow(stepMockHelper.processRowsStepMetaInterface, textFileOutputData);
    textFileOutput.dispose(stepMockHelper.processRowsStepMetaInterface, textFileOutputData);
    return ((TextFileOutputTestHandler) textFileOutput).errors;
}
Also used : ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) VariableSpace(org.pentaho.di.core.variables.VariableSpace) RowSet(org.pentaho.di.core.RowSet) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString)

Example 45 with VariableSpace

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

the class JobExecutorMetaTest method testLoadJobMeta.

@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));
}
Also used : Variables(org.pentaho.di.core.variables.Variables) Repository(org.pentaho.di.repository.Repository) JobMeta(org.pentaho.di.job.JobMeta) VariableSpace(org.pentaho.di.core.variables.VariableSpace) Test(org.junit.Test)

Aggregations

VariableSpace (org.pentaho.di.core.variables.VariableSpace)49 Test (org.junit.Test)21 Variables (org.pentaho.di.core.variables.Variables)14 KettleException (org.pentaho.di.core.exception.KettleException)12 Repository (org.pentaho.di.repository.Repository)10 TransMeta (org.pentaho.di.trans.TransMeta)10 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)9 RepositoryDirectoryInterface (org.pentaho.di.repository.RepositoryDirectoryInterface)8 StepMeta (org.pentaho.di.trans.step.StepMeta)8 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)7 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)6 IOException (java.io.IOException)5 FileObject (org.apache.commons.vfs2.FileObject)5 CurrentDirectoryResolver (org.pentaho.di.core.util.CurrentDirectoryResolver)5 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4 Matchers.anyString (org.mockito.Matchers.anyString)4 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)4 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)4 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)4