Search in sources :

Example 51 with IExternalResourceInfo

use of org.pentaho.metaverse.api.model.IExternalResourceInfo in project pentaho-metaverse by pentaho.

the class TextFileInputStepAnalyzerTest method testTextFileInputExternalResourceConsumer.

@Test
public void testTextFileInputExternalResourceConsumer() throws Exception {
    TextFileInputExternalResourceConsumer consumer = new TextFileInputExternalResourceConsumer();
    StepMeta spyMeta = spy(new StepMeta("test", meta));
    when(meta.getParentStepMeta()).thenReturn(spyMeta);
    when(spyMeta.getParentTransMeta()).thenReturn(transMeta);
    when(meta.getFileName()).thenReturn(null);
    when(meta.isAcceptingFilenames()).thenReturn(false);
    String[] filePaths = { "/path/to/file1", "/another/path/to/file2" };
    when(meta.getFilePaths(Mockito.any(VariableSpace.class))).thenReturn(filePaths);
    assertFalse(consumer.isDataDriven(meta));
    Collection<IExternalResourceInfo> resources = consumer.getResourcesFromMeta(meta);
    assertFalse(resources.isEmpty());
    assertEquals(2, resources.size());
    when(meta.isAcceptingFilenames()).thenReturn(true);
    assertTrue(consumer.isDataDriven(meta));
    assertTrue(consumer.getResourcesFromMeta(meta).isEmpty());
    when(mockRowMetaInterface.getString(Mockito.any(Object[].class), Mockito.anyString(), Mockito.anyString())).thenReturn("/path/to/row/file");
    when(mockTextFileInput.getStepMetaInterface()).thenReturn(meta);
    resources = consumer.getResourcesFromRow(mockTextFileInput, mockRowMetaInterface, new String[] { "id", "name" });
    assertFalse(resources.isEmpty());
    assertEquals(1, resources.size());
    when(mockRowMetaInterface.getString(Mockito.any(Object[].class), Mockito.anyString(), Mockito.anyString())).thenThrow(KettleException.class);
    resources = consumer.getResourcesFromRow(mockTextFileInput, mockRowMetaInterface, new String[] { "id", "name" });
    assertTrue(resources.isEmpty());
    assertEquals(TextFileInputMeta.class, consumer.getMetaClass());
}
Also used : IExternalResourceInfo(org.pentaho.metaverse.api.model.IExternalResourceInfo) VariableSpace(org.pentaho.di.core.variables.VariableSpace) StepMeta(org.pentaho.di.trans.step.StepMeta) BaseStepMeta(org.pentaho.di.trans.step.BaseStepMeta) Test(org.junit.Test)

Example 52 with IExternalResourceInfo

use of org.pentaho.metaverse.api.model.IExternalResourceInfo in project pentaho-metaverse by pentaho.

the class TextFileOutputExternalResourceConsumerTest method testTextFileOutputExternalResourceConsumer.

@Test
public void testTextFileOutputExternalResourceConsumer() throws Exception {
    StepMeta stepMeta = new StepMeta("test", meta);
    StepMeta spyMeta = spy(stepMeta);
    when(tfo.getStepMeta()).thenReturn(spyMeta);
    when(tfo.getStepDataInterface()).thenReturn(data);
    when(meta.getParentStepMeta()).thenReturn(spyMeta);
    when(spyMeta.getParentTransMeta()).thenReturn(transMeta);
    when(meta.getFileName()).thenReturn(null);
    when(meta.isFileNameInField()).thenReturn(false);
    String[] filePaths = { "/path/to/file1", "/another/path/to/file2" };
    when(meta.getFiles(Mockito.any(VariableSpace.class))).thenReturn(filePaths);
    assertFalse(consumer.isDataDriven(meta));
    Collection<IExternalResourceInfo> resources = consumer.getResourcesFromMeta(meta);
    assertFalse(resources.isEmpty());
    assertEquals(2, resources.size());
    when(meta.isFileNameInField()).thenReturn(true);
    when(meta.getExtension()).thenReturn("txt");
    assertTrue(consumer.isDataDriven(meta));
    assertTrue(consumer.getResourcesFromMeta(meta).isEmpty());
    data.fileName = "/path/to/row/file";
    when(tfo.buildFilename(Mockito.anyString(), Mockito.anyBoolean())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            return (args[0].toString() + ".txt");
        }
    });
    resources = consumer.getResourcesFromRow(tfo, rmi, new String[] { "id", "name" });
    assertFalse(resources.isEmpty());
    assertEquals(1, resources.size());
    when(data.fileName).thenThrow(KettleException.class);
    resources = consumer.getResourcesFromRow(tfo, rmi, new String[] { "id", "name" });
    assertTrue(resources.isEmpty());
    assertEquals(TextFileOutputMeta.class, consumer.getMetaClass());
}
Also used : IExternalResourceInfo(org.pentaho.metaverse.api.model.IExternalResourceInfo) VariableSpace(org.pentaho.di.core.variables.VariableSpace) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StepMeta(org.pentaho.di.trans.step.StepMeta) Test(org.junit.Test)

Example 53 with IExternalResourceInfo

use of org.pentaho.metaverse.api.model.IExternalResourceInfo in project pentaho-metaverse by pentaho.

the class TextFileOutputStepAnalyzerTest method testCreateResourceNode.

@Test
public void testCreateResourceNode() throws Exception {
    IExternalResourceInfo res = mock(IExternalResourceInfo.class);
    when(res.getName()).thenReturn("file:///Users/home/tmp/xyz.ktr");
    IMetaverseNode resourceNode = analyzer.createResourceNode(res);
    assertNotNull(resourceNode);
    assertEquals(DictionaryConst.NODE_TYPE_FILE, resourceNode.getType());
}
Also used : IExternalResourceInfo(org.pentaho.metaverse.api.model.IExternalResourceInfo) IMetaverseNode(org.pentaho.metaverse.api.IMetaverseNode) Test(org.junit.Test)

Example 54 with IExternalResourceInfo

use of org.pentaho.metaverse.api.model.IExternalResourceInfo in project pentaho-metaverse by pentaho.

the class ExecutionDataTest method testAddExternalResource.

@Test
public void testAddExternalResource() {
    IExternalResourceInfo externalResource = new BaseResourceInfo();
    assertEquals(executionData.getExternalResources().size(), 0);
    executionData.addExternalResource("testStep", externalResource);
    assertEquals(executionData.getExternalResources().get("testStep").get(0), externalResource);
}
Also used : IExternalResourceInfo(org.pentaho.metaverse.api.model.IExternalResourceInfo) BaseResourceInfo(org.pentaho.metaverse.api.model.BaseResourceInfo) Test(org.junit.Test)

Example 55 with IExternalResourceInfo

use of org.pentaho.metaverse.api.model.IExternalResourceInfo in project pentaho-metaverse by pentaho.

the class KettleAnalyzerUtil method getResourcesFromRow.

public static Collection<IExternalResourceInfo> getResourcesFromRow(BaseFileInputStep step, RowMetaInterface rowMeta, Object[] row) {
    Collection<IExternalResourceInfo> resources = new LinkedList<>();
    // For some reason the step doesn't return the StepMetaInterface directly, so go around it
    BaseFileInputMeta meta = (BaseFileInputMeta) step.getStepMetaInterface();
    if (meta == null) {
        meta = (BaseFileInputMeta) step.getStepMeta().getStepMetaInterface();
    }
    try {
        String filename = meta == null ? null : step.environmentSubstitute(rowMeta.getString(row, meta.getAcceptingField(), null));
        if (!Const.isEmpty(filename)) {
            FileObject fileObject = KettleVFS.getFileObject(filename, step);
            resources.add(ExternalResourceInfoFactory.createFileResource(fileObject, true));
        }
    } catch (KettleException kve) {
    // TODO throw exception or ignore?
    }
    return resources;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) IExternalResourceInfo(org.pentaho.metaverse.api.model.IExternalResourceInfo) BaseFileInputMeta(org.pentaho.di.trans.steps.file.BaseFileInputMeta) FileObject(org.apache.commons.vfs2.FileObject) LinkedList(java.util.LinkedList)

Aggregations

IExternalResourceInfo (org.pentaho.metaverse.api.model.IExternalResourceInfo)64 Test (org.junit.Test)38 BaseStepMeta (org.pentaho.di.trans.step.BaseStepMeta)16 StepMeta (org.pentaho.di.trans.step.StepMeta)16 LinkedList (java.util.LinkedList)12 IMetaverseNode (org.pentaho.metaverse.api.IMetaverseNode)12 HashSet (java.util.HashSet)10 KettleException (org.pentaho.di.core.exception.KettleException)10 ArrayList (java.util.ArrayList)8 VariableSpace (org.pentaho.di.core.variables.VariableSpace)8 FileObject (org.apache.commons.vfs2.FileObject)7 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)5 IExecutionProfile (org.pentaho.metaverse.api.model.IExecutionProfile)5 List (java.util.List)4 IAnalysisContext (org.pentaho.metaverse.api.IAnalysisContext)4 IStepExternalResourceConsumer (org.pentaho.metaverse.api.analyzer.kettle.step.IStepExternalResourceConsumer)3 WebServiceResourceInfo (org.pentaho.metaverse.api.model.WebServiceResourceInfo)3 Before (org.junit.Before)2 Matchers.anyString (org.mockito.Matchers.anyString)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2