Search in sources :

Example 26 with Mockito.when

use of org.mockito.Mockito.when in project pentaho-kettle by pentaho.

the class TextFileInputTest method testFolderFromPreviousStep.

/**
 * This test handles the case where a folder is given for the target of the previous step. Note, that it uses
 * 2 ram files and sets the mock to return "ram://." to trigger the directory logic.
 * @throws Exception - Test exception
 */
@Test
public void testFolderFromPreviousStep() throws Exception {
    final String virtualFile = createVirtualFile("test-file1.txt", "1,\n");
    final String virtualFile2 = createVirtualFile("test-file2.txt", "1,\n");
    BaseFileField col2 = field("col2");
    col2.setIfNullValue("DEFAULT");
    TextFileInputMeta meta = createMetaObject(field("col1"), col2);
    VariableSpace space = new Variables();
    space.initializeVariablesFrom(null);
    meta.inputFiles.passingThruFields = true;
    meta.inputFiles.acceptingFilenames = true;
    TextFileInputData data = createDataObject(virtualFile, ",", "col1", "col2");
    data.files.addFile(KettleVFS.getFileObject(virtualFile2));
    TextFileInput input = Mockito.spy(StepMockUtil.getStep(TextFileInput.class, TextFileInputMeta.class, "test"));
    RowSet rowset = Mockito.mock(RowSet.class);
    RowMetaInterface rwi = Mockito.mock(RowMetaInterface.class);
    Object[] obj1 = new Object[2];
    Object[] obj2 = new Object[2];
    Mockito.doReturn(rowset).when(input).findInputRowSet(null);
    Mockito.doReturn(null).when(input).getRowFrom(rowset);
    Mockito.when(input.getRowFrom(rowset)).thenReturn(obj1, null);
    Mockito.when(input.getTransMeta().listVariables()).thenReturn(space.listVariables());
    Mockito.when(input.getTransMeta().getVariable(anyString())).thenAnswer((Answer<String>) invocation -> space.getVariable((String) invocation.getArguments()[0]));
    Mockito.doReturn(rwi).when(rowset).getRowMeta();
    Mockito.when(rwi.getString(obj2, 0)).thenReturn("ram:///.");
    List<Object[]> output = TransTestingUtil.execute(input, meta, data, 0, false);
    List<String> passThroughKeys = new ArrayList<>(data.passThruFields.keySet());
    Assert.assertNotNull(passThroughKeys);
    // set order is not guaranteed - order alphabetically
    passThroughKeys.sort(String.CASE_INSENSITIVE_ORDER);
    assertEquals(2, passThroughKeys.size());
    Assert.assertNotNull(passThroughKeys.get(0));
    Assert.assertTrue(passThroughKeys.get(0).startsWith("0_ram"));
    Assert.assertTrue(passThroughKeys.get(0).endsWith("test-file1.txt"));
    Assert.assertNotNull(passThroughKeys.get(1));
    Assert.assertTrue(passThroughKeys.get(1).startsWith("1_ram"));
    Assert.assertTrue(passThroughKeys.get(1).endsWith("test-file2.txt"));
    deleteVfsFile(virtualFile);
    deleteVfsFile(virtualFile2);
}
Also used : RestorePDIEngineEnvironment(org.pentaho.di.junit.rules.RestorePDIEngineEnvironment) Trans(org.pentaho.di.trans.Trans) RowSet(org.pentaho.di.core.RowSet) StepDataInterface(org.pentaho.di.trans.step.StepDataInterface) BeforeClass(org.junit.BeforeClass) RowMeta(org.pentaho.di.core.row.RowMeta) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BaseFileField(org.pentaho.di.trans.steps.file.BaseFileField) KettleVFS(org.pentaho.di.core.vfs.KettleVFS) TestUtils(org.pentaho.di.utils.TestUtils) FilePlayListAll(org.pentaho.di.core.playlist.FilePlayListAll) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) Matchers.anyString(org.mockito.Matchers.anyString) ArrayList(java.util.ArrayList) Answer(org.mockito.stubbing.Answer) IBaseFileInputReader(org.pentaho.di.trans.steps.file.IBaseFileInputReader) TransMeta(org.pentaho.di.trans.TransMeta) ByteArrayInputStream(java.io.ByteArrayInputStream) KettleFileException(org.pentaho.di.core.exception.KettleFileException) VariableSpace(org.pentaho.di.core.variables.VariableSpace) FileInputList(org.pentaho.di.core.fileinput.FileInputList) AbstractFileErrorHandler(org.pentaho.di.trans.step.errorhandling.AbstractFileErrorHandler) ClassRule(org.junit.ClassRule) OutputStream(java.io.OutputStream) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Assert(org.pentaho.di.core.util.Assert) StepMeta(org.pentaho.di.trans.step.StepMeta) Variables(org.pentaho.di.core.variables.Variables) Test(org.junit.Test) IBaseFileInputStepControl(org.pentaho.di.trans.steps.file.IBaseFileInputStepControl) Mockito.when(org.mockito.Mockito.when) FileObject(org.apache.commons.vfs2.FileObject) LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface) KettleEnvironment(org.pentaho.di.core.KettleEnvironment) InputStreamReader(java.io.InputStreamReader) StepMockUtil(org.pentaho.di.trans.steps.StepMockUtil) IOUtils(org.apache.commons.io.IOUtils) Mockito(org.mockito.Mockito) List(java.util.List) FileErrorHandler(org.pentaho.di.trans.step.errorhandling.FileErrorHandler) TransTestingUtil(org.pentaho.di.trans.TransTestingUtil) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Assert.assertEquals(org.junit.Assert.assertEquals) FileContent(org.apache.commons.vfs2.FileContent) InputStream(java.io.InputStream) Mockito.mock(org.mockito.Mockito.mock) VariableSpace(org.pentaho.di.core.variables.VariableSpace) BaseFileField(org.pentaho.di.trans.steps.file.BaseFileField) RowSet(org.pentaho.di.core.RowSet) ArrayList(java.util.ArrayList) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) Matchers.anyString(org.mockito.Matchers.anyString) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Variables(org.pentaho.di.core.variables.Variables) FileObject(org.apache.commons.vfs2.FileObject) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)26 Mockito (org.mockito.Mockito)26 Mockito.when (org.mockito.Mockito.when)26 ImmutableSet (com.google.common.collect.ImmutableSet)24 Collections (java.util.Collections)24 Set (java.util.Set)24 Assert.assertEquals (org.junit.Assert.assertEquals)24 ImmutableMap (com.google.common.collect.ImmutableMap)23 HashMap (java.util.HashMap)23 Map (java.util.Map)23 Config (org.apache.samza.config.Config)23 MapConfig (org.apache.samza.config.MapConfig)23 TaskConfig (org.apache.samza.config.TaskConfig)23 Assert.assertNotNull (org.junit.Assert.assertNotNull)23 Assert.assertTrue (org.junit.Assert.assertTrue)23 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)22 ImmutableList (com.google.common.collect.ImmutableList)22 ListMultimap (com.google.common.collect.ListMultimap)22 File (java.io.File)22 Path (java.nio.file.Path)22