diff --git a/src/main/java/io/ivyteam/devops/db/Database.java b/src/main/java/io/ivyteam/devops/db/Database.java index 1afa0df..0307ef2 100644 --- a/src/main/java/io/ivyteam/devops/db/Database.java +++ b/src/main/java/io/ivyteam/devops/db/Database.java @@ -14,7 +14,15 @@ public class Database { private static final String VERSION = "14"; - private static final Path PATH = Path.of("data", "devopsV" + VERSION + ".db"); + private final Path path; + + public Database() { + this(Path.of("data", "devopsV" + VERSION + ".db")); + } + + public Database(Path path) { + this.path = path; + } public Connection connection() { if (!exists()) { @@ -25,9 +33,9 @@ public Connection connection() { private Connection dbConnection() { try { - Files.createDirectories(PATH.getParent()); + Files.createDirectories(path.getParent()); Class.forName("org.sqlite.JDBC"); - var connection = DriverManager.getConnection("jdbc:sqlite:" + PATH); + var connection = DriverManager.getConnection("jdbc:sqlite:" + path); try (var stmt = connection.createStatement()) { stmt.execute("PRAGMA foreign_keys = ON"); } @@ -39,7 +47,7 @@ private Connection dbConnection() { } private boolean exists() { - return Files.exists(PATH); + return Files.exists(path); } private void create() { diff --git a/src/main/java/io/ivyteam/devops/user/UserRepository.java b/src/main/java/io/ivyteam/devops/user/UserRepository.java index 9f82e81..4afac07 100644 --- a/src/main/java/io/ivyteam/devops/user/UserRepository.java +++ b/src/main/java/io/ivyteam/devops/user/UserRepository.java @@ -17,6 +17,10 @@ public class UserRepository { @Autowired private Database db; + public UserRepository(Database db) { + this.db = db; + } + public List all() { try (var connection = db.connection()) { try (var stmt = connection.prepareStatement("SELECT * FROM user ORDER BY name")) { @@ -29,8 +33,9 @@ public List all() { public void create(User user) { try (var connection = db.connection()) { - try (var stmt = connection.prepareStatement("INSERT INTO user (name) VALUES (?)")) { + try (var stmt = connection.prepareStatement("INSERT INTO user (name, avatarUrl) VALUES (?, ?)")) { stmt.setString(1, user.name()); + stmt.setString(2, user.avatarUrl()); stmt.execute(); } } catch (SQLException ex) { diff --git a/src/test/java/io/ivyteam/devops/user/TestUserRepository.java b/src/test/java/io/ivyteam/devops/user/TestUserRepository.java new file mode 100644 index 0000000..869d9ea --- /dev/null +++ b/src/test/java/io/ivyteam/devops/user/TestUserRepository.java @@ -0,0 +1,57 @@ +package io.ivyteam.devops.user; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.nio.file.Path; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import io.ivyteam.devops.db.Database; + +class TestUserRepository { + + private static final User ALEX = new User("alex", "https://example.com/alex.png"); + private static final User LOUIS = new User("louis", "https://example.com/louis.png"); + + @TempDir + Path tempDir; + + UserRepository users; + + @BeforeEach + void beforeEach() { + var db = new Database(tempDir.resolve("test.db")); + users = new UserRepository(db); + } + + @Test + void create() { + users.create(ALEX); + assertThat(users.all()).containsExactly(ALEX); + } + + @Test + void exists() { + users.create(ALEX); + assertThat(users.exists(ALEX)).isTrue(); + } + + @Test + void doesNotExists() { + assertThat(users.exists(LOUIS)).isFalse(); + } + + @Test + void all_ordered() { + users.create(LOUIS); + users.create(ALEX); + assertThat(users.all()).containsExactly(ALEX, LOUIS); + } + + @Test + void all_empty() { + assertThat(users.all()).isEmpty(); + } +}