Added database connection, added test and start with Testcontainers.
This commit is contained in:
3
.env.example
Normal file
3
.env.example
Normal file
@@ -0,0 +1,3 @@
|
||||
POSTGRES_PASSWORD=""
|
||||
POSTGRES_USER=""
|
||||
POSTGRES_DB=""
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -37,3 +37,4 @@ build/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
.env
|
||||
@@ -15,5 +15,5 @@
|
||||
|
||||
|
||||
## TODO:
|
||||
- [ ] Fabryka połączenia do bazy danych
|
||||
- [ ] Testy bazy
|
||||
- [x] Fabryka połączenia do bazy danych
|
||||
- [x] Testy bazy
|
||||
18
docker-compose.yaml
Normal file
18
docker-compose.yaml
Normal file
@@ -0,0 +1,18 @@
|
||||
services:
|
||||
|
||||
db:
|
||||
image: postgres:18.3
|
||||
restart: always
|
||||
shm_size: 128mb
|
||||
environment:
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
POSTGRES_DB: ${POSTGRES_DB}
|
||||
ports:
|
||||
- 12900:5432
|
||||
|
||||
adminer:
|
||||
image: adminer
|
||||
restart: always
|
||||
ports:
|
||||
- 12901:8080
|
||||
40
pom.xml
40
pom.xml
@@ -14,4 +14,44 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
<version>7.0.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>42.7.10</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.github.cdimascio</groupId>
|
||||
<artifactId>dotenv-java</artifactId>
|
||||
<version>3.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>1.19.8</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.9.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
33
src/main/java/util/DatabaseConnection.java
Normal file
33
src/main/java/util/DatabaseConnection.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package util;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import io.github.cdimascio.dotenv.Dotenv;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class DatabaseConnection {
|
||||
|
||||
private static Dotenv dotenv = Dotenv.load();
|
||||
|
||||
private static HikariConfig config = new HikariConfig();
|
||||
private static HikariDataSource ds;
|
||||
|
||||
static {
|
||||
config.setJdbcUrl(dotenv.get("JDBC"));
|
||||
config.setUsername(dotenv.get("POSTGRES_USER"));
|
||||
config.setPassword(dotenv.get("POSTGRES_PASSWORD"));
|
||||
config.setDriverClassName("org.postgresql.Driver");
|
||||
config.setMaximumPoolSize(10);
|
||||
config.setMinimumIdle(2);
|
||||
ds = new HikariDataSource(config);
|
||||
}
|
||||
|
||||
private DatabaseConnection() {};
|
||||
|
||||
public static Connection getConnection() throws SQLException {
|
||||
return ds.getConnection();
|
||||
}
|
||||
|
||||
}
|
||||
37
src/test/java/DatabaseConnectionTest.java
Normal file
37
src/test/java/DatabaseConnectionTest.java
Normal file
@@ -0,0 +1,37 @@
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
|
||||
public class DatabaseConnectionTest {
|
||||
|
||||
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
|
||||
"postgres:16-alpine"
|
||||
);
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
postgres.start();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll() {
|
||||
postgres.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldConnect() throws Exception {
|
||||
try (Connection conn = DriverManager.getConnection(
|
||||
postgres.getJdbcUrl(),
|
||||
postgres.getUsername(),
|
||||
postgres.getPassword()
|
||||
)) {
|
||||
assertFalse(conn.isClosed());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user