diff --git a/changelog/unreleased/000001-h2-connection.yml b/changelog/unreleased/000001-h2-connection.yml new file mode 100644 index 0000000..771fd24 --- /dev/null +++ b/changelog/unreleased/000001-h2-connection.yml @@ -0,0 +1,12 @@ +# This file is used by logchange tool to generate CHANGELOG.md 🌳 🪓 => 🪵 +# Visit https://github.com/logchange/logchange and leave a star 🌟 +# More info about configuration you can find https://github.com/logchange/logchange#yaml-format ⬅️⬅ ️ +title: Added support for H2 datasource type +authors: + - name: Mateusz Witkowski + nick: witx98 + url: https://github.com/witx98 +merge_requests: + - 56 +type: added + diff --git a/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DataSourceConnectionFactory.java b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DataSourceConnectionFactory.java index d3d0128..6f067e9 100644 --- a/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DataSourceConnectionFactory.java +++ b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DataSourceConnectionFactory.java @@ -1,6 +1,7 @@ package dev.logchange.hofund.connection.spring.datasource; import dev.logchange.hofund.connection.HofundDatabaseConnection; +import dev.logchange.hofund.connection.spring.datasource.h2.H2Connection; import dev.logchange.hofund.connection.spring.datasource.oracle.OracleConnection; import dev.logchange.hofund.connection.spring.datasource.postgresql.PostgreSQLConnection; import lombok.extern.slf4j.Slf4j; @@ -22,6 +23,7 @@ public static HofundDatabaseConnection of(DataSource dataSource) { return switch (dbType) { case POSTGRESQL -> new PostgreSQLConnection(metaData, dataSource).toHofundConnection(); case ORACLE -> new OracleConnection(metaData, dataSource).toHofundConnection(); + case H2 -> new H2Connection(metaData, dataSource).toHofundConnection(); default -> { log.warn("Currently there is no support for DataSource: {} please create issue at: https://github.com/logchange/hofund", productName); yield null; diff --git a/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DatabaseProductName.java b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DatabaseProductName.java index 61d4f9e..b530de4 100644 --- a/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DatabaseProductName.java +++ b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/DatabaseProductName.java @@ -10,6 +10,7 @@ public enum DatabaseProductName { POSTGRESQL("PostgreSQL"), ORACLE("Oracle"), + H2("H2"), NOT_RECOGNIZED("Not recognized"); private final String name; diff --git a/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.java b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.java new file mode 100644 index 0000000..6b0e34b --- /dev/null +++ b/hofund-spring/src/main/java/dev/logchange/hofund/connection/spring/datasource/h2/H2Connection.java @@ -0,0 +1,50 @@ +package dev.logchange.hofund.connection.spring.datasource.h2; + +import dev.logchange.hofund.connection.spring.datasource.DatasourceConnection; +import lombok.extern.slf4j.Slf4j; + +import javax.sql.DataSource; +import java.sql.DatabaseMetaData; +import java.sql.SQLException; +import java.util.Locale; + +@Slf4j +public class H2Connection extends DatasourceConnection { + + private static final String TEST_QUERY = "SELECT 1"; + + private String target; + private String url; + private String dbVendor; + + public H2Connection(DatabaseMetaData metaData, DataSource dataSource) { + super(dataSource, TEST_QUERY); + try { + this.url = metaData.getURL(); + int colonIndex = url.lastIndexOf(':'); + this.target = url.substring(colonIndex + 1).toLowerCase(Locale.ROOT); + this.dbVendor = metaData.getDatabaseProductName(); + } catch (SQLException e) { + log.warn("Error getting db information", e); + this.target = "ERROR"; + this.url = "ERROR"; + this.dbVendor = "ERROR"; + } + } + + @Override + protected String getTarget() { + return target; + } + + @Override + protected String getUrl() { + return url; + } + + @Override + protected String getDbVendor() { + return dbVendor; + } + +} diff --git a/hofund-spring/src/test/java/dev/logchange/hofund/connection/spring/datasource/h2/H2ConnectionTest.java b/hofund-spring/src/test/java/dev/logchange/hofund/connection/spring/datasource/h2/H2ConnectionTest.java new file mode 100644 index 0000000..504c8b3 --- /dev/null +++ b/hofund-spring/src/test/java/dev/logchange/hofund/connection/spring/datasource/h2/H2ConnectionTest.java @@ -0,0 +1,42 @@ +package dev.logchange.hofund.connection.spring.datasource.h2; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import javax.sql.DataSource; +import java.sql.DatabaseMetaData; +import java.sql.SQLException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class H2ConnectionTest { + + @Mock + private DatabaseMetaData databaseMetaData; + + @Mock + private DataSource dataSource; + + + @Test + void givenConnectionUrl_whenGetTarget_databaseNameReturned() throws SQLException { + //given: + String url = "spring.datasource.url=jdbc:h2:mem:17ebc6e8-e833-4157-b8e2-35f113eb404a"; + String productName = "H2"; + when(databaseMetaData.getURL()).thenReturn(url); + when(databaseMetaData.getDatabaseProductName()).thenReturn(productName); + + //when: + H2Connection h2Connection = new H2Connection(databaseMetaData, dataSource); + String resultTarget = h2Connection.getTarget(); + String resultVendor = h2Connection.getDbVendor(); + + //then: + assertThat(resultTarget).isEqualTo("17ebc6e8-e833-4157-b8e2-35f113eb404a"); + assertThat(resultVendor).isEqualTo(productName); + } +}