Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump org.assertj:assertj-db from 2.0.2 to 3.0.0 #935

Merged
merged 6 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jetbrains-annotations-version = "26.0.1"
websocket-version = "1.5.7"
junitVersion = "5.11.3"
assertj-core-version = "3.26.3"
assertj-db-version = "2.0.2"
assertj-db-version = "3.0.0"
mockito-version = "5.14.2"
awaitility-version = "4.2.2"
json-unit-version = "4.1.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.rakambda.channelpointsminer.miner.database;

import com.zaxxer.hikari.HikariDataSource;
import fr.rakambda.channelpointsminer.miner.api.ws.data.message.subtype.Event;
import fr.rakambda.channelpointsminer.miner.database.converter.Converters;
import fr.rakambda.channelpointsminer.miner.database.model.prediction.OutcomeStatistic;
Expand All @@ -10,7 +9,9 @@
import org.flywaydb.core.Flyway;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.sql.DataSource;
import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Expand All @@ -27,7 +28,7 @@
@RequiredArgsConstructor
@Log4j2
public abstract class BaseDatabase implements IDatabase{
private final HikariDataSource dataSource;
private final DataSource dataSource;
private final Lock[] getOrCreatePredictionUserIdLocks = new Lock[]{
new ReentrantLock(),
new ReentrantLock(),
Expand Down Expand Up @@ -120,14 +121,14 @@
}

try(var addUserStatement = conn.prepareStatement("""
INSERT INTO `PredictionUser`(`Username`, `ChannelID`) VALUES (?, ?) RETURNING `ID`;""")) {
INSERT INTO `PredictionUser`(`Username`, `ChannelID`) VALUES (?, ?) RETURNING `ID`;""")){
addUserStatement.setString(1, username);
addUserStatement.setString(2, channelId);
try (var generatedKeys = addUserStatement.executeQuery()) {
if (!generatedKeys.next()) {
try(var generatedKeys = addUserStatement.executeQuery()){
if(!generatedKeys.next()){
throw new SQLException("Failed to get new prediction user id");
}

var userId = generatedKeys.getInt(1);
log.debug("Added new prediction user '{}' for channel '{}' : {}", username, channelId, userId);
return userId;
Expand Down Expand Up @@ -270,8 +271,10 @@
}

@Override
public void close(){
dataSource.close();
public void close() throws IOException{
if(dataSource instanceof Closeable closeable){
closeable.close();

Check warning on line 276 in miner/src/main/java/fr/rakambda/channelpointsminer/miner/database/BaseDatabase.java

View check run for this annotation

Codecov / codecov/patch

miner/src/main/java/fr/rakambda/channelpointsminer/miner/database/BaseDatabase.java#L276

Added line #L276 was not covered by tests
}
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.extern.log4j.Log4j2;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.sql.SQLException;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -155,7 +156,7 @@ private void updateBalance(@NotNull IStreamerEvent event, @NotNull Balance balan
}

@Override
public void close(){
public void close() throws IOException{
database.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.rakambda.channelpointsminer.miner.api.ws.data.message.subtype.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.sql.SQLException;
import java.time.Instant;
import java.util.Collection;
Expand All @@ -13,7 +14,7 @@ public interface IDatabase extends AutoCloseable{
void initDatabase() throws SQLException;

@Override
void close();
void close() throws IOException;

void createChannel(@NotNull String channelId, @NotNull String username) throws SQLException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package fr.rakambda.channelpointsminer.miner.database;

import com.zaxxer.hikari.HikariDataSource;
import fr.rakambda.channelpointsminer.miner.database.converter.Converters;
import org.jetbrains.annotations.NotNull;
import javax.sql.DataSource;
import java.sql.SQLException;

public class MariaDBDatabase extends BaseDatabase{
public MariaDBDatabase(HikariDataSource dataSource){
public MariaDBDatabase(DataSource dataSource){
super(dataSource);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package fr.rakambda.channelpointsminer.miner.database;

import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;

public class MysqlDatabase extends MariaDBDatabase{
public MysqlDatabase(HikariDataSource dataSource){
public MysqlDatabase(DataSource dataSource){
super(dataSource);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package fr.rakambda.channelpointsminer.miner.database;

import com.zaxxer.hikari.HikariDataSource;
import fr.rakambda.channelpointsminer.miner.database.converter.Converters;
import fr.rakambda.channelpointsminer.miner.factory.TimeFactory;
import org.jetbrains.annotations.NotNull;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.sql.Timestamp;

public class SQLiteDatabase extends BaseDatabase{
public SQLiteDatabase(HikariDataSource dataSource){
public SQLiteDatabase(DataSource dataSource){
super(dataSource);
}

@Override
public void initDatabase() throws SQLException{
public void initDatabase(){
applyFlyway("db/migrations/sqlite");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package fr.rakambda.channelpointsminer.miner.event;

import java.io.IOException;

public interface IEventHandler extends AutoCloseable{
void onEvent(IEvent event);

@Override
default void close(){}
default void close() throws IOException{}

Check warning on line 9 in miner/src/main/java/fr/rakambda/channelpointsminer/miner/event/IEventHandler.java

View check run for this annotation

Codecov / codecov/patch

miner/src/main/java/fr/rakambda/channelpointsminer/miner/event/IEventHandler.java#L9

Added line #L9 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,58 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.ThreadContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.annotations.VisibleForTesting;
import java.io.IOException;
import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;

@Log4j2
@RequiredArgsConstructor
public class EventManager implements IEventManager{
@Getter(value = AccessLevel.PUBLIC, onMethod_ = {
@TestOnly,
@VisibleForTesting
})
private final Collection<IEventHandler> eventHandlers = new ConcurrentLinkedQueue<>();

private final ExecutorService handlerExecutor;

@Setter
private IMiner miner;

@Override
public void addEventHandler(@NotNull IEventHandler handler){
eventHandlers.add(handler);
}

@Override
public void onEvent(@NotNull IEvent event){
event.setMiner(miner);

var values = ThreadContext.getImmutableContext();
var messages = ThreadContext.getImmutableStack().asList();

eventHandlers.forEach(listener -> handlerExecutor.submit(() -> {
try(var ignored = LogContext.restore(values, messages)){
listener.onEvent(event);
}
}));
}

@Override
public void close(){
for(var listener : eventHandlers){
listener.close();
try{
listener.close();

Check warning on line 58 in miner/src/main/java/fr/rakambda/channelpointsminer/miner/event/manager/EventManager.java

View check run for this annotation

Codecov / codecov/patch

miner/src/main/java/fr/rakambda/channelpointsminer/miner/event/manager/EventManager.java#L58

Added line #L58 was not covered by tests
}
catch(IOException e){
log.error("Failed to close event handler {}", listener, e);
}

Check warning on line 62 in miner/src/main/java/fr/rakambda/channelpointsminer/miner/event/manager/EventManager.java

View check run for this annotation

Codecov / codecov/patch

miner/src/main/java/fr/rakambda/channelpointsminer/miner/event/manager/EventManager.java#L60-L62

Added lines #L60 - L62 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.IOException;
import java.sql.SQLException;
import java.time.Instant;
import java.util.List;
Expand Down Expand Up @@ -363,7 +364,7 @@ void onPredictionResultDatabaseException() throws SQLException{
}

@Test
void closeClosesDatabase(){
void closeClosesDatabase() throws IOException{
tested.close();

verify(database).close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package fr.rakambda.channelpointsminer.miner.database;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import fr.rakambda.channelpointsminer.miner.api.ws.data.message.subtype.Event;
import fr.rakambda.channelpointsminer.miner.database.model.prediction.OutcomeStatistic;
import fr.rakambda.channelpointsminer.miner.factory.TimeFactory;
import org.assertj.core.api.Assertions;
import org.assertj.db.type.AssertDbConnection;
import org.assertj.db.type.AssertDbConnectionFactory;
import org.assertj.db.type.Changes;
import org.assertj.db.type.Table;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.sqlite.SQLiteDataSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.file.Path;
import java.sql.SQLException;
import java.time.Instant;
Expand Down Expand Up @@ -69,8 +70,8 @@ class SQLiteDatabaseTest{
private Event event;

private SQLiteDatabase tested;
private HikariDataSource dataSource;

private AssertDbConnection assertDbConnection;
private Supplier<Changes> changesBalance;
private Supplier<Changes> changesChannel;
private Supplier<Changes> changesPrediction;
Expand All @@ -79,23 +80,22 @@ class SQLiteDatabaseTest{
private Supplier<Changes> changesResolvedPrediction;

@BeforeEach
void setUp() throws SQLException{
var poolConfiguration = new HikariConfig();
poolConfiguration.setJdbcUrl("jdbc:sqlite:" + tempPath.resolve(System.currentTimeMillis() + "_test.db").toAbsolutePath());
poolConfiguration.setDriverClassName("org.sqlite.JDBC");
poolConfiguration.setMaximumPoolSize(1);
void setUp(){
var dataSource = new SQLiteDataSource();
dataSource.setUrl("jdbc:sqlite:" + tempPath.resolve(System.currentTimeMillis() + "_test.db").toAbsolutePath());

assertDbConnection = AssertDbConnectionFactory.of(dataSource).create();;

dataSource = new HikariDataSource(poolConfiguration);
tested = new SQLiteDatabase(dataSource);

tested.initDatabase();

changesBalance = () -> new Changes(new Table(dataSource, "Balance"));
changesChannel = () -> new Changes(new Table(dataSource, "Channel"));
changesPrediction = () -> new Changes(new Table(dataSource, "Prediction"));
changesPredictionUser = () -> new Changes(new Table(dataSource, "PredictionUser"));
changesUserPrediction = () -> new Changes(new Table(dataSource, "UserPrediction"));
changesResolvedPrediction = () -> new Changes(new Table(dataSource, "ResolvedPrediction"));
changesBalance = () -> assertDbConnection.changes().table("Balance").build();
changesChannel = () -> assertDbConnection.changes().table("Channel").build();
changesPrediction = () -> assertDbConnection.changes().table("Prediction").build();
changesPredictionUser = () -> assertDbConnection.changes().table("PredictionUser").build();
changesUserPrediction = () -> assertDbConnection.changes().table("UserPrediction").build();
changesResolvedPrediction = () -> assertDbConnection.changes().table("ResolvedPrediction").build();

lenient().when(event.getId()).thenReturn(EVENT_ID);
lenient().when(event.getChannelId()).thenReturn(CHANNEL_ID);
Expand All @@ -105,17 +105,17 @@ void setUp() throws SQLException{
}

@AfterEach
void tearDown(){
void tearDown() throws IOException{
tested.close();
}

@Test
void tablesAreCreated(){
assertThat(new Table(dataSource, "Balance")).exists();
assertThat(new Table(dataSource, "Channel")).exists();
assertThat(new Table(dataSource, "Prediction")).exists();
assertThat(new Table(dataSource, "PredictionUser")).exists();
assertThat(new Table(dataSource, "UserPrediction")).exists();
assertThat(assertDbConnection.table("Balance").build()).exists();
assertThat(assertDbConnection.table("Channel").build()).exists();
assertThat(assertDbConnection.table("Prediction").build()).exists();
assertThat(assertDbConnection.table("PredictionUser").build()).exists();
assertThat(assertDbConnection.table("UserPrediction").build()).exists();
}

@Test
Expand Down
Loading