2023-08-23 20:21:45 +02:00
|
|
|
package com.commafeed;
|
|
|
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
|
import java.sql.SQLException;
|
2023-12-15 08:59:52 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
2023-08-23 20:21:45 +02:00
|
|
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
|
|
|
|
import com.codahale.metrics.MetricRegistry;
|
|
|
|
|
|
|
|
|
|
import io.dropwizard.testing.ResourceHelpers;
|
|
|
|
|
import io.dropwizard.testing.junit5.DropwizardAppExtension;
|
|
|
|
|
|
|
|
|
|
public class CommaFeedDropwizardAppExtension extends DropwizardAppExtension<CommaFeedConfiguration> {
|
|
|
|
|
|
2023-12-15 08:59:52 +01:00
|
|
|
private static final List<String> TABLES = Arrays.asList("FEEDENTRYSTATUSES", "FEEDENTRYTAGS", "FEEDENTRIES", "FEEDENTRYCONTENTS",
|
|
|
|
|
"FEEDSUBSCRIPTIONS", "FEEDS", "FEEDCATEGORIES");
|
|
|
|
|
|
2023-08-23 20:21:45 +02:00
|
|
|
public CommaFeedDropwizardAppExtension() {
|
|
|
|
|
super(CommaFeedApplication.class, ResourceHelpers.resourceFilePath("config.test.yml"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void after() {
|
|
|
|
|
super.after();
|
|
|
|
|
|
|
|
|
|
// clean database after each test
|
|
|
|
|
DataSource dataSource = getConfiguration().getDataSourceFactory().build(new MetricRegistry(), "cleanup");
|
2023-12-15 08:59:52 +01:00
|
|
|
try (Connection connection = dataSource.getConnection()) {
|
|
|
|
|
for (String table : TABLES) {
|
|
|
|
|
PreparedStatement statement = connection.prepareStatement("DELETE FROM " + table);
|
|
|
|
|
statement.executeUpdate();
|
|
|
|
|
}
|
2023-08-23 20:21:45 +02:00
|
|
|
} catch (SQLException e) {
|
|
|
|
|
throw new RuntimeException("could not cleanup database", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|