package de.superx.spring; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static java.util.Map.entry; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Map; import java.util.Properties; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import de.superx.util.PathAndFileUtils; // TODO: Test löscht his1_databases.properties. Diese sollte aber min. gesichert // und wieder hergestellt werden! Deshalb erstmal auf Ignore gesetzt. @Ignore public class HIS1Databases_Test { private Logger logger = LoggerFactory.getLogger(HIS1Databases_Test.class); @BeforeClass public static void prepareTests() { Map props = Map.ofEntries( Map.entry("db.hisinone_audit.passwd","sx_des#116#120#-97#-28#-54#-55#11#8"), Map.entry("db.hisinone_audit.user",""), Map.entry("db.hisinone.user","postgres"), Map.entry("db.hisinone.driver","org.postgresql.Driver"), Map.entry("db.hisinone_audit.driver",""), Map.entry("db.eduetl.user","postgres"), Map.entry("db.hisinone.passwd","sx_des#-46#111#51#-31#31#-90#-113#46#116#120#-97#-28#-54#-55#11#8"), Map.entry("db.hisinone.protocol","jdbc\\:postgresql\\:"), Map.entry("db.hisinone.port","5432"), Map.entry("db.hisinone.host","localhost"), Map.entry("db.hisinone.name","hisinone_cust"), Map.entry("db.eduetl.passwd","sx_des#-46#111#51#-31#31#-90#-113#46#116#120#-97#-28#-54#-55#11#8"), Map.entry("dbconnections","eduetl,hisinone,hisinone_audit"), Map.entry("db.eduetl.protocol","jdbc\\:postgresql\\:"), Map.entry("db.eduetl.port","5432"), Map.entry("db.eduetl.host","localhost"), Map.entry("db.eduetl.name","eduetl_cust"), Map.entry("db.eduetl.driver","org.postgresql.Driver"), Map.entry("db.hisinone_audit.url","//\\:/?ApplicationName\\=HIS-BI") ); Properties properties = new Properties(); properties.putAll(props); try (FileOutputStream fos = new FileOutputStream(String.format("%s/classes/his1_databases.properties",PathAndFileUtils.getWebinfPath()));){ properties.store(fos, null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } @AfterClass public static void removeTestArtefacts() { try { Files.delete(Paths.get(String.format("%s/classes/his1_databases.properties",PathAndFileUtils.getWebinfPath()))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void testGetValidConnections() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { Map> connections = DatabaseUtils.getValidConnections(); assertTrue("getValidConnections()", connections != null && connections.size() > 0); } @Test public void testGetAllConnections() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException{ Map> connections = DatabaseUtils.getAllConnections(); assertTrue("getAllConnections()", connections != null && connections.size() > 0); } @Test public void testGetConnection() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { String coName = "eduetl"; Map co = DatabaseUtils.getConnection(coName); assertNotNull(co); assertEquals(coName, co.get("database")); } @Test public void testManualPoolsizeConfig() { int min = 5, max = 10; int maxPoolSizeBefore = -1, minPoolSizeBefore = -1, maxPoolSizeAfter = -1, minPoolSizeAfter = -1; Map map_ps_config = Map.ofEntries( Map.entry("db.eduetl.poolsize.min",""+min), Map.entry("db.eduetl.poolsize.max", ""+max)); try { maxPoolSizeBefore = Integer.parseInt(DatabaseUtils.getConnection("eduetl").get("DBMaximalAnzahl")); minPoolSizeBefore = Integer.parseInt(DatabaseUtils.getConnection("eduetl").get("DBMindestAnzahl")); } catch (NumberFormatException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } Properties poolSizeConfig = new Properties(); poolSizeConfig.putAll(map_ps_config); try(FileOutputStream fos = new FileOutputStream(String.format("%s/classes/his1_db_poolsize.properties", PathAndFileUtils.getWebinfPath()))){ poolSizeConfig.store(fos, null); } catch (IOException e) { // TODO Auto-generated catch block logger.info("Couldn't write manual poolsize config properties file: \n"); e.printStackTrace(); } try { maxPoolSizeAfter = Integer.parseInt(DatabaseUtils.getConnection("eduetl").get("DBMaximalAnzahl")); minPoolSizeAfter = Integer.parseInt(DatabaseUtils.getConnection("eduetl").get("DBMindestAnzahl")); Files.delete(Paths.get(String.format("%s/classes/his1_db_poolsize.properties", PathAndFileUtils.getWebinfPath()))); } catch (NumberFormatException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block logger.info("Couldn't delete Properties File for manual poolsize configuration: \n"); e.printStackTrace(); } assertNotEquals(maxPoolSizeBefore, maxPoolSizeAfter); assertNotEquals(minPoolSizeBefore, minPoolSizeAfter); assertEquals(minPoolSizeAfter, min); assertEquals(maxPoolSizeAfter, max); } @Test public void testAnalyzeJdbcUrlWithParams() { String url = "jdbc:postgresql://localhost:5432/head_eduetl?ApplicationName=superx"; String[] parts = DatabaseUtils.analyzeJdbcUrl(url); assertEquals("Protocol", "jdbc:postgresql:", parts[0]); assertEquals("Host", "localhost", parts[1]); assertEquals("Port", "5432", parts[2]); assertEquals("Database", "head_eduetl", parts[3]); } @Test public void testAnalyzeJdbcUrlWithoutParams() { String url = "jdbc:postgresql://localhost:5432/head_eduetl"; String[] parts = DatabaseUtils.analyzeJdbcUrl(url); assertEquals("Protocol", "jdbc:postgresql:", parts[0]); assertEquals("Host", "localhost", parts[1]); assertEquals("Port", "5432", parts[2]); assertEquals("Database", "head_eduetl", parts[3]); } @Test public void testConvertDbProperties() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { Map config = Map.ofEntries(entry("sslmode", "require"), entry("connectionURL", "jdbc:postgresql://localhost:5432/head_eduetl?ApplicationName=superx"), entry("driverName", "org.postgresql.Driver"), entry("connectionName", "postgres"), entry("connectionPassword", "sx_des#-46#111#51#-31#31#-90#-113#46#116#120#-97#-28#-54#-55#11#8")); Properties dbProperties = new Properties(); dbProperties.putAll(config); Properties his1Properties = DatabaseUtils.convertDbProperties(dbProperties); String prefix = "db.eduetl."; assertEquals("his1 user", "postgres", his1Properties.get(prefix + "user")); assertEquals("his1 password", "postgres", his1Properties.get(prefix + "passwd")); assertEquals("his1 driver", "org.postgresql.Driver", his1Properties.get(prefix + "driver")); assertEquals("his1 ssl", "true", his1Properties.get(prefix + "ssl")); assertEquals("his1 protocol", "jdbc:postgresql:", his1Properties.get(prefix + "protocol")); assertEquals("his1 host", "localhost", his1Properties.get(prefix + "host")); assertEquals("his1 port", "5432", his1Properties.get(prefix + "port")); assertEquals("his1 name", "head_eduetl", his1Properties.get(prefix + "name")); } }