Skip to content

Commit

Permalink
Added test containers (postgresql) (#72)
Browse files Browse the repository at this point in the history
* added test containers for postgres

Signed-off-by: Alessio <[email protected]>

* restored workflow

Signed-off-by: Alessio <[email protected]>

* Update maven.yaml

* Update maven.yaml

* Update maven.yaml

* Update maven.yaml

---------

Signed-off-by: Alessio <[email protected]>
Co-authored-by: Alessio <[email protected]>
  • Loading branch information
DevStakePool and slux83 authored Feb 22, 2025
1 parent 719ded9 commit 2ae74c0
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 29 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/maven.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ name: Java CI with Maven

on:
push:
branches: [ dev ]
pull_request:
branches: [ dev ]
branches:
- dev
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up JDK 21
uses: actions/setup-java@v4
with:
java-version: 21
distribution: zulu
- name: Build with Maven
run: mvn -DskipTests -B verify --file pom.xml
run: mvn -B verify --file pom.xml
env:
MAVEN_OPTS: -Xmx2g
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<telegram-java-bot-api.version>7.11.0</telegram-java-bot-api.version>
<koios-java.version>1.20.1</koios-java.version>
<emoji-java.version>5.1.1</emoji-java.version>
<testcontainers.version>1.20.5</testcontainers.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -115,6 +116,22 @@
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>

<!-- Test containers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.devpool.thothBot.koios.KoiosFacade;
import com.devpool.thothBot.scheduler.GovernanceSpoVotesCheckerTask;
import com.devpool.thothBot.telegram.TelegramFacade;
import com.devpool.thothBot.util.AbstractIntegrationTest;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.response.SendResponse;
import org.junit.jupiter.api.Assertions;
Expand All @@ -32,7 +33,7 @@
@SpringBootTest
@ActiveProfiles("no-scheduler")
@DirtiesContext
class GovPoolVotesIntegrationTest {
class GovPoolVotesIntegrationTest extends AbstractIntegrationTest {

private static final Logger LOG = LoggerFactory.getLogger(GovPoolVotesIntegrationTest.class);
private static final List<User> TEST_USERS = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.devpool.thothBot.telegram.TelegramFacade;
import com.devpool.thothBot.telegram.command.*;
import com.devpool.thothBot.telegram.command.admin.AdminNotifyAllCmd;
import com.devpool.thothBot.util.AbstractIntegrationTest;
import com.devpool.thothBot.util.TelegramUtils;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.LinkPreviewOptions;
Expand Down Expand Up @@ -42,7 +43,7 @@
@SpringBootTest
@ActiveProfiles("no-scheduler")
@DirtiesContext
class IntegrationNoSchedulerTest {
class IntegrationNoSchedulerTest extends AbstractIntegrationTest {

private static final Logger LOG = LoggerFactory.getLogger(IntegrationNoSchedulerTest.class);
private static List<User> TEST_USERS = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.devpool.thothBot.doubles.koios.BackendServiceDouble;
import com.devpool.thothBot.koios.KoiosFacade;
import com.devpool.thothBot.telegram.TelegramFacade;
import com.devpool.thothBot.util.AbstractIntegrationTest;
import com.pengrad.telegrambot.TelegramBot;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -28,7 +29,7 @@
@SpringBootTest
@ActiveProfiles("subscription")
@DirtiesContext
public class IntegrationSubscriptionSchedulerTest {
public class IntegrationSubscriptionSchedulerTest extends AbstractIntegrationTest {

private static final Logger LOG = LoggerFactory.getLogger(IntegrationSubscriptionSchedulerTest.class);
private static List<User> TEST_USERS = new ArrayList<>();
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/com/devpool/thothBot/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.devpool.thothBot.telegram.command.AddressCmd;
import com.devpool.thothBot.telegram.command.HelpCmd;
import com.devpool.thothBot.telegram.command.SubscribeCmd;
import com.devpool.thothBot.util.AbstractIntegrationTest;
import com.devpool.thothBot.util.TelegramUtils;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.LinkPreviewOptions;
Expand All @@ -26,7 +27,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
Expand All @@ -39,9 +39,8 @@

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@DirtiesContext
public class IntegrationTest {
public class IntegrationTest extends AbstractIntegrationTest {

private static final Logger LOG = LoggerFactory.getLogger(IntegrationTest.class);
private static List<User> TEST_USERS = new ArrayList<>();
Expand Down Expand Up @@ -146,7 +145,7 @@ public void userCommandHelpTest() throws Exception {
Map<String, Object> params = sendMessage.getParameters();
assertEquals(1683539744L, params.get("chat_id"));
assertInstanceOf(LinkPreviewOptions.class, params.get("link_preview_options"));
assertEquals(Boolean.TRUE, ((LinkPreviewOptions)params.get("link_preview_options")).isDisabled());
assertEquals(Boolean.TRUE, ((LinkPreviewOptions) params.get("link_preview_options")).isDisabled());
assertEquals("HTML", params.get("parse_mode"));
assertTrue(params.get("text").toString().contains("THOTH BOT"));
assertTrue(params.get("text").toString().contains("/help or /start"));
Expand All @@ -172,7 +171,8 @@ public void userCommandHelpAsAdminTest() throws Exception {
Map<String, Object> params = sendMessage.getParameters();
assertEquals(1683539744L, params.get("chat_id"));
assertInstanceOf(LinkPreviewOptions.class, params.get("link_preview_options"));
assertEquals(Boolean.TRUE, ((LinkPreviewOptions)params.get("link_preview_options")).isDisabled()); assertEquals("HTML", params.get("parse_mode"));
assertEquals(Boolean.TRUE, ((LinkPreviewOptions) params.get("link_preview_options")).isDisabled());
assertEquals("HTML", params.get("parse_mode"));
assertTrue(params.get("text").toString().contains("THOTH BOT"));
assertTrue(params.get("text").toString().contains("/help or /start"));
// We got an admin commmand
Expand All @@ -197,7 +197,7 @@ public void userCommandInfoStakeAddrTest() throws Exception {
Map<String, Object> params = sendMessage.getParameters();
assertEquals((long) -2, params.get("chat_id"));
assertInstanceOf(LinkPreviewOptions.class, params.get("link_preview_options"));
assertEquals(Boolean.TRUE, ((LinkPreviewOptions)params.get("link_preview_options")).isDisabled());
assertEquals(Boolean.TRUE, ((LinkPreviewOptions) params.get("link_preview_options")).isDisabled());
assertEquals("HTML", params.get("parse_mode"));
assertTrue(params.get("text").toString().contains("[DEV]"));
assertTrue(params.get("text").toString().contains("pool15fxktqvd92sq8plh3rjdrksumt9p8rzsayfk4akv2hng5r8ukha"));
Expand Down Expand Up @@ -225,7 +225,7 @@ public void userCommandInfoNormalAddrTest() throws Exception {
Map<String, Object> params = sendMessage.getParameters();
assertEquals((long) -4, params.get("chat_id"));
assertInstanceOf(LinkPreviewOptions.class, params.get("link_preview_options"));
assertEquals(Boolean.TRUE, ((LinkPreviewOptions)params.get("link_preview_options")).isDisabled());
assertEquals(Boolean.TRUE, ((LinkPreviewOptions) params.get("link_preview_options")).isDisabled());
assertEquals("HTML", params.get("parse_mode"));
assertTrue(params.get("text").toString().contains("$badfriends"));
assertTrue(params.get("text").toString().contains("Balance: 176.00"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.devpool.thothBot.koios.KoiosFacade;
import com.devpool.thothBot.scheduler.RetiredPoolCheckerTask;
import com.devpool.thothBot.telegram.TelegramFacade;
import com.devpool.thothBot.util.AbstractIntegrationTest;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.response.SendResponse;
import org.junit.jupiter.api.Assertions;
Expand All @@ -32,7 +33,7 @@

@SpringBootTest(properties = "thoth.scheduler.initial-delay-secs=999")
@DirtiesContext
class RetiringPoolIntegrationTest {
class RetiringPoolIntegrationTest extends AbstractIntegrationTest {

private static final Logger LOG = LoggerFactory.getLogger(RetiringPoolIntegrationTest.class);
private static final List<User> TEST_USERS = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.devpool.thothBot.util;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertTrue;

@Testcontainers
@SpringBootTest
public abstract class AbstractIntegrationTest {
private static final Logger LOG = LoggerFactory.getLogger(AbstractIntegrationTest.class);

private static final List<String> DB_MIGRATION = List.of(
"db-migration/thoth-v1.0.0-rc1.sql",
"db-migration/alter-1.0.0-rc1-TO-1.5.0.sql",
"db-migration/alter-1.5.0-TO-1.7.0.sql",
"db-migration/alter-1.7.0-TO-1.8.0.sql",
"db-migration/alter-1.8.0-TO-1.9.1.sql",
"db-migration/alter-1.9.1-TO-1.9.2.sql"
);

@Container
private final static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16.6")
.withDatabaseName("testdb")
.withUsername("postgres")
.withPassword("testpass");

@BeforeAll
public static void setupPostgresContainer() throws IOException {
LOG.info("Starting postgresql...");
postgres.start();
assertTrue(postgres.isRunning());
LOG.info("Postgres started!");
migrateDb();
}

private static void migrateDb() throws IOException {
LOG.info("Migrating DB...");
// Create a JDBC DataSource
var dataSource = new SingleConnectionDataSource(
postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword(), true
);
var jdbcTemplate = new JdbcTemplate(dataSource);

for (String sqlFile : DB_MIGRATION) {
LOG.info("Executing {}", sqlFile);
Path scriptPath = Paths.get(
Objects.requireNonNull(
AbstractIntegrationTest.class.getClassLoader()
.getResource(sqlFile)).getPath());
String sql = Files.readString(scriptPath);
jdbcTemplate.execute(sql);
}
}

@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}

@AfterAll
public static void shutdownPostgresContainer() {
if (postgres != null)
postgres.stop();
}
}
6 changes: 1 addition & 5 deletions src/test/resources/application-no-scheduler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ management:
spring:
application:
name: thoth-bot
datasource:
driver-class-name: org.postgresql.Driver
password: postgres
url: jdbc:postgresql://localhost:5432/thoth_dev_itest
username: postgres

telegram:
bot:
token: CHANGE_ME
Expand Down
6 changes: 1 addition & 5 deletions src/test/resources/application-subscription.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ management:
spring:
application:
name: thoth-bot
datasource:
driver-class-name: org.postgresql.Driver
password: postgres
url: jdbc:postgresql://localhost:5432/thoth_dev_itest
username: postgres

telegram:
bot:
token: CHANGE_ME
Expand Down
6 changes: 1 addition & 5 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ management:
spring:
application:
name: thoth-bot
datasource:
driver-class-name: org.postgresql.Driver
password: postgres
url: jdbc:postgresql://localhost:5432/thoth_dev_itest
username: postgres

telegram:
bot:
token: CHANGE_ME
Expand Down

0 comments on commit 2ae74c0

Please sign in to comment.