package de.superx.kettle; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; 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.junit.AfterClass; 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; /** * Test that {@code ef070=}∅ {@code for ef069=2 OR ef069=3} * * @author hiber * */ public class StudentOfficialStatisticTestSemesterAbroadBeforeNewEnrolment { private static final String SCHEMA_PATH = String.join(File.separator, "test", "resources", "schema_files", "student_statistics", "abroad_before_new_enrolment", "schema"); private static final String VIEWS_PATH = String.join(File.separator, "test", "resources", "schema_files", "student_statistics", "abroad_before_new_enrolment", "views"); private static final String UNLOADS_PATH = String.join(File.separator, "test", "resources", "schema_files", "student_statistics", "abroad_before_new_enrolment", "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|sx_repository)") .setViewPattern("skipeverything"); h2InMemoryCreator.createH2InMemory(); } @Test public void testStudentStatistics() throws Exception { createInMemoryTestDb(); MemoryMetaStore metastore = new MemoryMetaStore(); initKettleEnvironment(metastore); try (Connection conn = this.datasource.getConnection();) { // Server.startWebServer(conn); Job job = runKettleJob(metastore); notifyLoggedErrors(job); validateResult(conn); } } 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", "6"); jobMeta.setParameterValue("stichtagsart_pruef_berichtssem", "6"); jobMeta.setParameterValue("stichtagsart_stud_vorsem", "6"); jobMeta.setParameterValue("stichtagsart_stud_berichtssem", "6"); 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(Connection conn) throws SQLException { Statement statement = conn.createStatement(); // check ef070 = null when ef069 = 2 ResultSet resultSet = statement.executeQuery("SELECT ef069, ef070 FROM " + StatisticsBase.MODULE_PREFIX + "astat WHERE ef069='2'"); while (resultSet.next()) { String ef069 = resultSet.getString("ef069"); String ef070 = resultSet.getString("ef070"); assertNotNull("statistic has an entry for ef069=2", ef069); assertNull("ef070 is null when ef069=2", ef070); } // check ef070 = null when ef069 = 3 resultSet = statement.executeQuery("SELECT ef069, ef070 FROM " + StatisticsBase.MODULE_PREFIX + "astat WHERE ef069 = '3'"); while (resultSet.next()) { String ef069 = resultSet.getString("ef069"); String ef070 = resultSet.getString("ef070"); assertNotNull("statistic has an entry for ef069=3", ef069); assertNull("ef070 is null when ef069=3", ef070); } } @AfterClass public static void after() { KettleEnvironment.shutdown(); } }