package de.superx.kettle; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import java.io.File; import java.util.List; import java.util.Map; import javax.sql.DataSource; import org.junit.AfterClass; import org.junit.BeforeClass; 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 org.springframework.jdbc.core.JdbcTemplate; import de.superx.db.H2InMemoryCreator; import de.superx.stat.StatisticsBase; import de.superx.util.test.KettleH2TestDataSourceProvider; public class ExaminationOfficialStatisticsTest { private static final String SCHEMA_PATH = String.join(File.separator, "test", "resources", "schema_files", "examination_statistics", "course_of_study_missing", "schema"); private static final String VIEWS_PATH = String.join(File.separator, "test", "resources", "schema_files", "examination_statistics", "course_of_study_missing", "views"); private static final String UNLOADS_PATH = String.join(File.separator, "test", "resources", "schema_files", "examination_statistics", "course_of_study_missing", "unloads"); private static final String KETTLE_PLUGIN_DIR = String.join(File.separator, "superx", "WEB-INF", "kettle-plugins"); private static MemoryMetaStore metastore = new MemoryMetaStore(); private static DataSource datasource = null; @BeforeClass public static void initEnvironment() throws Exception { createInMemoryTestDb(); initKettleEnvironment(); } /** * @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|sx_repository)") .setViewPattern("skipeverything"); h2InMemoryCreator.createH2InMemory(); } @SuppressWarnings("static-method") @Test public void testExamsWithoutCourseOfStudy() throws Exception { Job job = runKettleJob(); notifyLoggedErrors(job); validateResult(); // Server.startWebServer(conn); // uncomment for local (e.g. dev env) debugging } private static void initKettleEnvironment() throws KettleException, MetaStoreException { System.setProperty("BI_KETTLE_PLUGIN_BASE_FOLDERS", KETTLE_PLUGIN_DIR); KettleEnvironment.init(); DataSourceProviderInterface datasourceProvider = new KettleH2TestDataSourceProvider("default", null); DataSourceProviderFactory.setDataSourceProviderInterface(datasourceProvider); datasource = datasourceProvider.getNamedDataSource("eduetl"); DatabaseMeta dbmeta = new DatabaseMeta("eduetl", "POSTGRESQL", "JNDI", null, "eduetl", "1521", null, null); DatabaseMetaStoreUtil.createDatabaseElement(metastore, dbmeta); } private static Job runKettleJob() 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", "2"); jobMeta.setParameterValue("berichtssemester", "20182"); jobMeta.setParameterValue("stichtagsart_pruef_vorsem", "2"); jobMeta.setParameterValue("stichtagsart_pruef_berichtssem", "4"); jobMeta.setParameterValue("stichtagsart_stud_vorsem", "6"); jobMeta.setParameterValue("stichtagsart_stud_berichtssem", "1"); jobMeta.setParameterValue("matrikelnr", "11230911"); Job job = new Job(null, jobMeta); job.setLogLevel(LogLevel.BASIC); 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"); assertFalse("Errors where found in kettle log:\n" + log, errorsOccured); } private static void validateResult() { JdbcTemplate jt = new JdbcTemplate(datasource); List> resultRows = jt.queryForList("select * from " + StatisticsBase.MODULE_PREFIX + "astat"); assertEquals("Result has to be exactly one row", 1, resultRows.size()); Map result = resultRows.get(0); validateStudierendenDaten(result); validateErstePruefungsDaten(result); validateZweitePruefungsDaten(result); } private static void validateStudierendenDaten(Map row) { validateValue(row,"ef001", "03"); validateValue(row,"ef002", "2"); validateValue(row,"ef003", "2018"); validateValue(row,"ef004", "9994"); validateValue(row,"ef005", "000001"); validateValue(row,"ef006", "000011230911"); validateValue(row,"ef007", "1"); validateValue(row,"ef008u1", "22"); validateValue(row,"ef008u2", "07"); validateValue(row,"ef008u3", "1995"); validateValue(row,"ef009", "Flor"); validateValue(row,"ef010", "134"); validateValue(row,"ef017", "9994"); validateValue(row,"ef018", ""); validateValue(row,"ef019", "2"); validateValue(row,"ef020", "2017"); validateValue(row,"ef117", "2013"); validateValue(row,"ef118", "03"); validateValue(row,"ef119u1", "05"); validateValue(row,"ef119u2", "770"); } private static void validateErstePruefungsDaten(Map row) { validateValue(row, "ef126", "1999102"); validateValue(row, "ef127", "03"); validateValue(row, "ef128", "00"); validateValue(row, "ef129", "0"); validateValue(row, "ef130", "0"); validateValue(row, "ef131", "0"); validateValue(row, "ef132", "030"); validateValue(row, "ef133", "000"); validateValue(row, "ef134", "000"); validateValue(row, "ef147", "168"); validateValue(row, "ef148", ""); validateValue(row, "ef149", "04"); validateValue(row, "ef150", "660"); validateValue(row, "ef151", "661"); validateValue(row, "ef153", "662"); validateValue(row, "ef155", "11"); validateValue(row, "ef156", "2018"); validateValue(row, "ef157", "1"); validateValue(row, "ef158", "8"); } private static void validateZweitePruefungsDaten(Map row) { validateValue(row, "ef163", "1999107"); validateValue(row, "ef164", "03"); validateValue(row, "ef165", "00"); validateValue(row, "ef166", "0"); validateValue(row, "ef167", "0"); validateValue(row, "ef168", "0"); validateValue(row, "ef169", "030"); validateValue(row, "ef170", "000"); validateValue(row, "ef171", "000"); validateValue(row, "ef184", "168"); validateValue(row, "ef185", ""); validateValue(row, "ef186", "04"); validateValue(row, "ef187", ""); validateValue(row, "ef188", "664"); validateValue(row, "ef190", "665"); validateValue(row, "ef192", "10"); validateValue(row, "ef193", "2018"); validateValue(row, "ef194", "1"); validateValue(row, "ef195", "8"); } private static void validateValue(Map row, String key, String expected) { Object value = row.get(key); String strValue = value != null ? value.toString() : ""; assertEquals(key, expected, strValue); } @AfterClass public static void after() { KettleEnvironment.shutdown(); } }