Search in sources :

Example 11 with ModelInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.

the class CsvDatasourceServiceImplTest method testHasPermissions.

@Test
public void testHasPermissions() throws Exception {
    hasPermissions = true;
    final ISystemSettings systemSettings = mock(ISystemSettings.class);
    when(systemSettings.getSystemSetting("data-access-override", "false")).thenReturn("false");
    PentahoSystem.setSystemSettingsService(systemSettings);
    String filename = "anotherStageFile_CsvFile.csv";
    File file = createTmpCsvFile(filename);
    file.deleteOnExit();
    try {
        ModelInfo modelInfo = service.stageFile(filename, ",", "\n", true, "utf-8");
        CsvFileInfo fileInfo = modelInfo.getFileInfo();
        assertEquals("One header row", 1, fileInfo.getHeaderRows());
        assertEquals("Header + content row", 2, fileInfo.getContents().size());
        assertEquals(filename, fileInfo.getTmpFilename());
        final FileInfo[] stagedFiles = service.getStagedFiles();
        assertNotNull(stagedFiles);
        boolean present = false;
        for (FileInfo info : stagedFiles) {
            if (filename.equals(info.getName())) {
                present = true;
                break;
            }
        }
        assertTrue(present);
        final String encoding = service.getEncoding(filename);
        assertNotNull(encoding);
        final List<String> previewRows = service.getPreviewRows(filename, true, 1, "utf-8");
        assertNotNull(previewRows);
        assertEquals(1, previewRows.size());
        assertEquals("col1,col2", previewRows.get(0));
        final DatasourceDTO datasourceDto = mock(DatasourceDTO.class);
        when(datasourceDto.getCsvModelInfo()).thenReturn(modelInfo);
        try {
            final FileTransformStats fileTransformStats = service.generateDomain(datasourceDto);
        } catch (Exception e) {
        // Testing this logic is not a purpose of this junit
        }
        // Passed permissions check
        verify(datasourceDto, times(1)).getCsvModelInfo();
    } finally {
        file.delete();
    }
}
Also used : CsvFileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo) ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) CsvFileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo) FileInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.FileInfo) ISystemSettings(org.pentaho.platform.api.engine.ISystemSettings) Matchers.anyString(org.mockito.Matchers.anyString) DatasourceDTO(org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO) File(java.io.File) FileTransformStats(org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats) Test(org.junit.Test)

Example 12 with ModelInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.

the class CsvTransformGeneratorIT method testDropNonExistingTable.

/**
 * Given a name of a non-existing table to drop.
 * <br/>
 * When StagingTransformGenerator is called to drop this table,
 * then it shouldn't execute drop statement.
 */
public void testDropNonExistingTable() throws Exception {
    IPentahoSession session = new StandaloneSession("test");
    KettleSystemListener.environmentInit(session);
    DatabaseMeta dbMeta = getDatabaseMeta();
    ModelInfo info = createModel();
    CsvTransformGenerator gen = new CsvTransformGenerator(info, dbMeta);
    String tableName = info.getStageTableName();
    try {
        gen.execSqlStatement(getDropTableStatement(tableName), dbMeta, null);
    } catch (CsvTransformGeneratorException e) {
    // it is OK if the table doesn't exist previously
    }
    // now make sure we do not execute drop statement for non-existing table
    try {
        gen.dropTable(tableName);
    } catch (CsvTransformGeneratorException e) {
        // no need to forward exception, just fail the test
        fail();
    }
}
Also used : ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta)

Example 13 with ModelInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.

the class CsvTransformGeneratorIT method testSemiColonAfterQuoteIsFound.

public void testSemiColonAfterQuoteIsFound() throws Exception {
    IPentahoSession session = new StandaloneSession("test");
    KettleSystemListener.environmentInit(session);
    DatabaseMeta realDbMeta = getDatabaseMeta();
    ModelInfo info = createModel();
    final DatabaseMeta dbMeta = Mockito.mock(DatabaseMeta.class);
    final Database db = Mockito.mock(Database.class);
    CsvTransformGenerator gen = new CsvTransformGenerator(info, realDbMeta) {

        @Override
        Database getDatabase(final DatabaseMeta databaseMeta) {
            assertSame(dbMeta, databaseMeta);
            return db;
        }
    };
    gen.execSqlStatement("UPDATE \"csv_test4\" SET \"YEAR_ID_KTL\"=\"YEAR_ID\";\nALTER TABLE \"csv_test4\" DROP ( \"YEAR_ID\" )", dbMeta, new StringBuilder());
    Mockito.verify(db).execStatement("UPDATE \"csv_test4\" SET \"YEAR_ID_KTL\"=\"YEAR_ID\"");
    Mockito.verify(db).execStatement("ALTER TABLE \"csv_test4\" DROP ( \"YEAR_ID\" )");
    // again but with single quotes
    gen.execSqlStatement("UPDATE \"csv_test4\" SET \"YEAR_ID_KTL\"=\'YEAR_ID\';\nALTER TABLE \"csv_test4\" DROP ( \'YEAR_ID\' )", dbMeta, new StringBuilder());
    Mockito.verify(db).execStatement("UPDATE \"csv_test4\" SET \"YEAR_ID_KTL\"=\'YEAR_ID\'");
    Mockito.verify(db).execStatement("ALTER TABLE \"csv_test4\" DROP ( \'YEAR_ID\' )");
}
Also used : ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Database(org.pentaho.di.core.database.Database) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta)

Example 14 with ModelInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.

the class CsvTransformGeneratorIT method testLoadTableTruncate.

public void testLoadTableTruncate() throws Exception {
    IPentahoSession session = new StandaloneSession("test");
    KettleSystemListener.environmentInit(session);
    // create the model
    ModelInfo info = createModel();
    CsvTransformGenerator gen = new CsvTransformGenerator(info, getDatabaseMeta());
    String tableName = info.getStageTableName();
    try {
        gen.execSqlStatement(getDropTableStatement(tableName), getDatabaseMeta(), null);
    } catch (CsvTransformGeneratorException e) {
    // table might not be there yet, it is OK
    }
    // generate the database table
    gen.createOrModifyTable(session);
    // load the table
    loadTable(gen, info, true, session);
    // check the results
    long rowCount = this.getRowCount(tableName);
    assertEquals((long) 235, rowCount);
    // load again, no truncate
    loadTable(gen, info, false, session);
    // check the results
    rowCount = this.getRowCount(tableName);
    assertEquals((long) 470, rowCount);
    // load again, with truncate
    loadTable(gen, info, true, session);
    // check the results
    rowCount = this.getRowCount(tableName);
    assertEquals((long) 235, rowCount);
}
Also used : ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession)

Example 15 with ModelInfo

use of org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo in project data-access by pentaho.

the class CsvTransformGeneratorIT method testModifyEmptyTable_RemoveColumn.

public void testModifyEmptyTable_RemoveColumn() throws Exception {
    CsvTransformGenerator gen = getCleanTransformGen();
    IPentahoSession session = new StandaloneSession("test");
    // create the model
    ModelInfo info = gen.getModelInfo();
    // generate the database table initially
    gen.createOrModifyTable(session);
    String removedColumn = info.getColumns()[info.getColumns().length - 1].getId();
    // now, lets update it by changing the model info slightly.. add a column
    removeColumnFromModel(info);
    gen.createOrModifyTable(session);
    // make sure the table has an extra integer column in it
    String tableName = info.getStageTableName();
    String sql = "select " + removedColumn + " from " + tableName + ";";
    try {
        gen.execSqlStatement(sql, getDatabaseMeta(), null);
        fail("Column should have been removed and an error raised");
    } catch (CsvTransformGeneratorException e) {
    // expected, the column should not be there to select
    }
}
Also used : ModelInfo(org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession)

Aggregations

ModelInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ModelInfo)23 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)12 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)11 CsvTransformGeneratorException (org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)10 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)6 CsvFileInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.CsvFileInfo)6 Test (org.junit.Test)4 File (java.io.File)3 ColumnInfo (org.pentaho.platform.dataaccess.datasource.wizard.models.ColumnInfo)3 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)3 XStream (com.thoughtworks.xstream.XStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 Date (java.util.Date)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Database (org.pentaho.di.core.database.Database)2 CsvUtils (org.pentaho.platform.dataaccess.datasource.wizard.csv.CsvUtils)2 DataRow (org.pentaho.platform.dataaccess.datasource.wizard.models.DataRow)2 DatasourceDTO (org.pentaho.platform.dataaccess.datasource.wizard.models.DatasourceDTO)2 FileTransformStats (org.pentaho.platform.dataaccess.datasource.wizard.sources.csv.FileTransformStats)2