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

change embeddedpg to testcontainers #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Project exclude paths
/target/
/target/
.idea
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.opentable.components</groupId>
<artifactId>otj-pg-embedded</artifactId>
<version>0.13.0</version>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.17.6</version>
<scope>test</scope>
</dependency>

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/ru/hh/school/DbFactory.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ru.hh.school;

import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Environment;
import ru.hh.school.entity.Resume;
import ru.hh.school.entity.Area;
import ru.hh.school.entity.Employer;
Expand All @@ -19,9 +21,10 @@ public class DbFactory {
Area.class
);

public static SessionFactory createSessionFactory() {
public static SessionFactory createSessionFactory(DataSource dataSource) {
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.loadProperties("hibernate.properties")
.applySetting(Environment.DATASOURCE, dataSource)
.build();

MetadataSources metadataSources = new MetadataSources(serviceRegistry);
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/hibernate.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# datasource
hibernate.connection.driver_class = org.postgresql.Driver
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.url = jdbc:postgresql://localhost:5433/postgres
hibernate.connection.username = postgres
hibernate.connection.password = postgres
hibernate.current_session_context_class=thread
Expand Down
22 changes: 18 additions & 4 deletions src/test/java/ru/hh/school/BaseTest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
package ru.hh.school;

import com.opentable.db.postgres.embedded.EmbeddedPostgres;
import javax.sql.DataSource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Before;
import org.junit.BeforeClass;
import org.postgresql.ds.PGSimpleDataSource;
import org.testcontainers.containers.PostgreSQLContainer;
import ru.hh.school.util.QueryInfoHolder;
import java.io.IOException;
import java.util.Optional;
import java.util.function.Supplier;

public abstract class BaseTest {

protected static EmbeddedPostgres pg;
protected static PostgreSQLContainer pgContainer;
protected static DataSource dataSource;
protected static SessionFactory sessionFactory;

@BeforeClass
public static void setupSessionFactory() throws IOException {
pg = EmbeddedPostgres.builder().setPort(5433).start();
sessionFactory = DbFactory.createSessionFactory();
pgContainer = new PostgreSQLContainer<>("postgres")
.withUsername("postgres")
.withPassword("postgres");
pgContainer.start();

PGSimpleDataSource pgDataSource = new PGSimpleDataSource();
String jdbcUrl = pgContainer.getJdbcUrl();
pgDataSource.setUrl(jdbcUrl);
pgDataSource.setUser(pgContainer.getUsername());
pgDataSource.setPassword(pgContainer.getPassword());
dataSource = pgDataSource;

sessionFactory = DbFactory.createSessionFactory(dataSource);
}

@Before
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/ru/hh/school/batching/BatchingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class BatchingTest extends BaseTest {

@BeforeClass
public static void createTable() {
TestHelper.executeScript(pg.getPostgresDatabase(), "create_resume.sql");
TestHelper.executeScript(dataSource, "create_resume.sql");
}

@Before
public void clearTable() {
TestHelper.execute(pg.getPostgresDatabase(), "delete from resume");
TestHelper.execute(dataSource, "delete from resume");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void setupService() {
new EmployerDao(sessionFactory),
new TransactionHelper(sessionFactory)
);
TestHelper.executeScript(pg.getPostgresDatabase(), "create_employers.sql");
TestHelper.executeScript(dataSource, "create_employers.sql");
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/ru/hh/school/service/VacancyServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public static void setupService() {
new VacancyDao(sessionFactory),
new TransactionHelper(sessionFactory)
);
TestHelper.executeScript(pg.getPostgresDatabase(), "create_employers.sql");
TestHelper.executeScript(dataSource, "create_employers.sql");
}

@Test
public void getSalaryStatistics() {
TestHelper.executeScript(pg.getPostgresDatabase(), "clear_data.sql");
TestHelper.executeScript(dataSource, "clear_data.sql");

Employer employer = new Employer();
employer.setCompanyName("HH");
Expand Down