package de.superx.kettle; import java.io.File; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import org.h2.tools.Server; import org.junit.AfterClass; import org.junit.Ignore; import org.junit.Test; import org.pentaho.di.core.KettleEnvironment; import org.pentaho.di.core.Result; import org.pentaho.di.core.database.DataSourceProviderFactory; import org.pentaho.di.core.database.DataSourceProviderInterface; import org.pentaho.di.core.database.DatabaseMeta; import org.pentaho.di.core.exception.KettleException; import org.pentaho.di.core.exception.KettleXMLException; import org.pentaho.di.core.logging.LogLevel; import org.pentaho.di.core.parameters.UnknownParamException; import org.pentaho.di.job.Job; import org.pentaho.di.job.JobMeta; import org.pentaho.di.metastore.DatabaseMetaStoreUtil; import org.pentaho.metastore.api.exceptions.MetaStoreException; import org.pentaho.metastore.stores.memory.MemoryMetaStore; import de.superx.db.H2InMemoryCreator; import de.superx.stat.StatisticsBase; import de.superx.util.test.KettleH2TestDataSourceProvider; import junit.framework.Assert; /** * @author brummer * */ public class StudentOfficialStatisticsTest { private static final String SCHEMA_PATH = String.join(File.separator, "test", "resources", "schema_files", "student_statistics", "schema"); private static final String VIEWS_PATH = String.join(File.separator, "test", "resources", "schema_files", "student_statistics", "views"); private static final String UNLOADS_PATH = String.join(File.separator, "test", "resources", "schema_files", "student_statistics", "unloads"); private static final String KETTLE_PLUGIN_DIR = String.join(File.separator, "superx", "WEB-INF", "kettle-plugins"); private DataSource datasource = null; /** * @throws java.lang.Exception */ private static void createInMemoryTestDb() throws Exception { H2InMemoryCreator h2InMemoryCreator = new H2InMemoryCreator(); String schemaPath = new File(SCHEMA_PATH).getAbsolutePath(); String viewsPath = new File(VIEWS_PATH).getAbsolutePath(); String unloadsPath = new File(UNLOADS_PATH).getAbsolutePath(); h2InMemoryCreator.setSchemaDir(schemaPath) .setViewDir(viewsPath) .setUnlDir(unloadsPath) .setUserName("sa") .setTablePattern("(astat_.*|sos_.*|trans_cifx|cif.*|dim_studiengang|hochschulinfo|semester|konstanten|accredited_ects)") .setViewPattern("skipeverything"); h2InMemoryCreator.createH2InMemory(); } @Ignore @Test public void testStudentStatistics() throws Exception { createInMemoryTestDb(); MemoryMetaStore metastore = new MemoryMetaStore(); initKettleEnvironment(metastore); // attention: it seems that in memory datasource is destroyed by kettle processing if we do not keep an open connection!? try ( Connection conn = this.datasource.getConnection(); ) { // Job job = runKettleJob(metastore); // notifyLoggedErrors(job); // validateResult(conn); Server.startWebServer(conn); // uncomment for local (e.g. dev env) debugging } } private void initKettleEnvironment(MemoryMetaStore metastore) throws KettleException, MetaStoreException { System.setProperty("BI_KETTLE_PLUGIN_BASE_FOLDERS", KETTLE_PLUGIN_DIR); KettleEnvironment.init(); DataSourceProviderInterface providerinterface = new KettleH2TestDataSourceProvider("default", null); DataSourceProviderFactory.setDataSourceProviderInterface(providerinterface); this.datasource = providerinterface.getNamedDataSource("eduetl"); DatabaseMeta dbmeta = new DatabaseMeta("eduetl", "POSTGRESQL", "JNDI", null, "eduetl", "1521", null, null); DatabaseMetaStoreUtil.createDatabaseElement(metastore, dbmeta); } private static Job runKettleJob(MemoryMetaStore metastore) throws KettleXMLException, UnknownParamException { JobMeta jobMeta = new JobMeta(null, "superx/WEB-INF/conf/edustore/db/module/astat/etl/statistik2016/estatistik.kjb", null, metastore, null); jobMeta.setParameterValue("lieferung", "1"); jobMeta.setParameterValue("berichtssemester", "20181"); jobMeta.setParameterValue("stichtagsart_pruef_vorsem", "2"); jobMeta.setParameterValue("stichtagsart_pruef_berichtssem", "4"); jobMeta.setParameterValue("stichtagsart_stud_vorsem", "6"); jobMeta.setParameterValue("stichtagsart_stud_berichtssem", "1"); Job job = new Job(null, jobMeta); job.setLogLevel(LogLevel.DETAILED); job.start(); job.waitUntilFinished(); return job; } private static void notifyLoggedErrors(Job job) { Result result = job.getResult(); String log = result.getLogText(); boolean errorsOccured = log.contains("ERROR"); Assert.assertFalse("Errors where found in kettle log:\n" + log, errorsOccured); } private static void validateResult(Connection conn) throws SQLException { Statement statement = conn.createStatement(); ResultSet resultSet = statement.executeQuery("select * from " + StatisticsBase.MODULE_PREFIX + "astat"); resultSet.next(); String ef007_name = resultSet.getString("ef007"); String ef023_pruefungBestanden = resultSet.getString("ef023"); Assert.assertEquals("'ef007' (name) differs from expected value", "Anne", ef007_name); Assert.assertEquals("'ef023' (pruefungBestanden) differs from expected value", "0", ef023_pruefungBestanden); } @AfterClass public static void after() { KettleEnvironment.shutdown(); } }