package de.superx.dbt.app; import static org.junit.Assert.assertTrue; import java.nio.file.Path; import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import de.superx.BaseDbTest; import de.superx.dbt.DbtAction; import de.superx.dbt.DbtRunRequest; import de.superx.dbt.DbtRunResult; import de.superx.dbt.DbtWrapper; import de.superx.dbt.DbtExecutionState; import de.superx.util.PathAndFileUtils; public class AppApplicationcontentTest extends BaseDbTest { private static final Path dbtProjectDir = Path.of( PathAndFileUtils.getWebinfPath(), "..", "dbt", "projects", "hisinone", "transform"); @Autowired DbtWrapper dbtWrapper; @Before public void before() { setAppContent("('FOO', 'BAR')"); } @Override @After public void after() { setAppContent("('FOO', 'BAR')"); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.update("DELETE FROM sx_repository WHERE id='APP_CONTENT_DELETE'"); super.after(); } @Test public void testEmptyAppContent() { DbtRunRequest request = new DbtRunRequest(DbtAction.BUILD, "hisinone", "+core_applicationcontent"); DbtRunResult result = dbtWrapper.execute(request); assertTrue("DBT run successfull", result.getState() == DbtExecutionState.SUCCESS); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); Integer count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM core.core_applicationcontent", Integer.class); assertTrue("core_applicationcontent table is empty", count.intValue() == 0); } @Test public void testAppContent() { // Test ob zwei Schlüssel übertragen werden setAppContent("('IP', 'BA')"); DbtRunRequest request = new DbtRunRequest(DbtAction.BUILD, "hisinone", "+core_applicationcontent"); DbtRunResult result = dbtWrapper.execute(request); assertTrue("DBT run successfull", result.getState() == DbtExecutionState.SUCCESS); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); Integer count = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM core.core_applicationcontent", Integer.class); assertTrue("core_applicationcontent table has 1 entry", count.intValue() > 1); List uns = jdbcTemplate.queryForList("SELECT DISTINCT content_uniquename FROM core.core_applicationcontent", String.class); assertTrue("core_applicationcontent table has entries with content_uniquename 'IP' and 'BA'", uns.contains("IP") && uns.contains("BA") && uns.size() == 2); // Jetzt einen Schlüssel wieder entfernen und testen, ob trotzdem beide Schlüssel vorhanden sind setAppContent("('IP')"); dbtWrapper.execute(request); uns = jdbcTemplate.queryForList("SELECT DISTINCT content_uniquename FROM core.core_applicationcontent", String.class); assertTrue("core_applicationcontent table has entries with content_uniquename 'IP' and 'BA'", uns.contains("IP") && uns.contains("BA") && uns.size() == 2); // Und schließlich den entfernten Schlüssel in APP_CONTENT_DELETE setzen und testen ob er entfernt wird jdbcTemplate.update("INSERT INTO sx_repository (id, content, aktiv, gueltig_seit, gueltig_bis)" + " VALUES ('APP_CONTENT_DELETE', 'BA' ,1, CURRENT_DATE, CURRENT_DATE)"); dbtWrapper.execute(request); uns = jdbcTemplate.queryForList("SELECT DISTINCT content_uniquename FROM core.core_applicationcontent", String.class); assertTrue("core_applicationcontent table has only entries with content_uniquename 'IP'", uns.contains("IP") && uns.size() == 1); } private void setAppContent(String appContent) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.update("UPDATE unload_params SET param_val = ? WHERE param_id='APP_CONTENT'", appContent); } }