Skip to content

Commit e48e098

Browse files
authored
Merge pull request #83 from andrei-punko/82-add-int-tests-for-db-versioning-modules
Fix #82: add integration tests for `db-versioning` modules
2 parents 5eef396 + ef814a1 commit e48e098

File tree

10 files changed

+369
-37
lines changed

10 files changed

+369
-37
lines changed

db-versioning/flyway-db/pom.xml

Lines changed: 100 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<name>DB versioning : Flyway</name>
1616

1717
<properties>
18-
<junit.vintage.version>5.10.1</junit.vintage.version>
19-
<junit.platform.version>1.10.1</junit.platform.version>
18+
<junit.vintage.version>5.10.2</junit.vintage.version>
19+
<junit.platform.version>1.10.2</junit.platform.version>
2020
</properties>
2121

2222
<dependencies>
@@ -39,47 +39,130 @@
3939
<scope>test</scope>
4040
</dependency>
4141
<dependency>
42-
<groupId>com.h2database</groupId>
43-
<artifactId>h2</artifactId>
44-
<version>${h2.db.version}</version>
45-
<scope>test</scope>
42+
<groupId>org.hibernate</groupId>
43+
<artifactId>hibernate-core</artifactId>
44+
<version>5.6.15.Final</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>mysql</groupId>
48+
<artifactId>mysql-connector-java</artifactId>
49+
<version>${mysql.version}</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.flywaydb</groupId>
53+
<artifactId>flyway-mysql</artifactId>
54+
<version>${flyway-maven-plugin.version}</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.projectlombok</groupId>
58+
<artifactId>lombok</artifactId>
4659
</dependency>
4760
</dependencies>
4861

4962
<build>
5063
<plugins>
64+
<plugin>
65+
<groupId>io.fabric8</groupId>
66+
<artifactId>docker-maven-plugin</artifactId>
67+
<version>${docker-maven-plugin.version}</version>
68+
<configuration>
69+
<showLogs>true</showLogs>
70+
<images>
71+
<image>
72+
<name>mysql:${mysql.version}</name>
73+
<run>
74+
<ports>
75+
<port>3306:3306</port>
76+
</ports>
77+
<env>
78+
<MYSQL_DATABASE>db</MYSQL_DATABASE>
79+
<MYSQL_ROOT_PASSWORD>some_password</MYSQL_ROOT_PASSWORD>
80+
<MYSQL_USER>user</MYSQL_USER>
81+
<MYSQL_PASSWORD>password</MYSQL_PASSWORD>
82+
</env>
83+
<wait>
84+
<log>starting as process 1</log>
85+
<time>20000</time>
86+
</wait>
87+
</run>
88+
</image>
89+
</images>
90+
</configuration>
91+
92+
<!-- Connect start/stop to pre- and post- integration-test phase respectively
93+
if you want to start your docker containers during integration tests -->
94+
<executions>
95+
<execution>
96+
<id>start</id>
97+
<phase>pre-integration-test</phase>
98+
<goals>
99+
<goal>stop</goal>
100+
<goal>start</goal>
101+
</goals>
102+
</execution>
103+
<execution>
104+
<id>stop</id>
105+
<phase>post-integration-test</phase>
106+
<goals>
107+
<goal>stop</goal>
108+
</goals>
109+
</execution>
110+
</executions>
111+
</plugin>
112+
51113
<plugin>
52114
<groupId>org.flywaydb</groupId>
53115
<artifactId>flyway-maven-plugin</artifactId>
54116
<version>${flyway-maven-plugin.version}</version>
55-
56117
<configuration>
57-
<url>jdbc:h2:mem:flywaydb</url>
118+
<url>${jdbc.url}</url>
58119
<user>${jdbc.username}</user>
59120
<password>${jdbc.password}</password>
121+
<encoding>UTF-8</encoding>
122+
<cleanDisabled>false</cleanDisabled>
60123
<locations>
61124
<location>some_folder/db/migration</location>
62125
</locations>
63-
<cleanDisabled>false</cleanDisabled>
64126
</configuration>
65127

66128
<executions>
67129
<execution>
68-
<phase>process-test-resources</phase>
130+
<id>execution</id>
131+
<phase>integration-test</phase>
69132
<goals>
70133
<goal>clean</goal>
71134
<goal>migrate</goal>
72135
</goals>
73136
</execution>
74137
</executions>
138+
</plugin>
75139

76-
<dependencies>
77-
<dependency>
78-
<groupId>com.h2database</groupId>
79-
<artifactId>h2</artifactId>
80-
<version>${h2.db.version}</version>
81-
</dependency>
82-
</dependencies>
140+
<plugin>
141+
<artifactId>maven-compiler-plugin</artifactId>
142+
<version>${maven-compiler-plugin.version}</version>
143+
<configuration>
144+
<annotationProcessorPaths>
145+
<path>
146+
<groupId>org.projectlombok</groupId>
147+
<artifactId>lombok</artifactId>
148+
<version>${lombok.version}</version>
149+
</path>
150+
</annotationProcessorPaths>
151+
</configuration>
152+
</plugin>
153+
154+
<!--This plugin used for running integration tests in integration-test build phase-->
155+
<plugin>
156+
<artifactId>maven-failsafe-plugin</artifactId>
157+
<version>${maven-failsafe-plugin.version}</version>
158+
<executions>
159+
<execution>
160+
<goals>
161+
<goal>integration-test</goal>
162+
<goal>verify</goal>
163+
</goals>
164+
</execution>
165+
</executions>
83166
</plugin>
84167
</plugins>
85168
</build>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package by.andd3dfx.model;
2+
3+
import lombok.Data;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
import javax.persistence.Table;
10+
11+
@Data
12+
@Entity
13+
@Table(name = "PERSON")
14+
public class Person {
15+
16+
@Id
17+
@GeneratedValue
18+
private Long id;
19+
20+
@Column
21+
private String name;
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!DOCTYPE hibernate-configuration PUBLIC
3+
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4+
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
5+
6+
<hibernate-configuration>
7+
<session-factory>
8+
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
9+
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/db?allowPublicKeyRetrieval=true&amp;useSSL=false</property>
10+
<property name="hibernate.connection.username">root</property>
11+
<property name="hibernate.connection.password">some_password</property>
12+
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
13+
<property name="show_sql">true</property>
14+
<property name="hibernate.id.new_generator_mappings">false</property>
15+
16+
<mapping class="by.andd3dfx.model.Person"/>
17+
</session-factory>
18+
</hibernate-configuration>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package by.andd3dfx;
2+
3+
import by.andd3dfx.model.Person;
4+
import lombok.SneakyThrows;
5+
import org.hibernate.cfg.Configuration;
6+
import org.junit.Test;
7+
8+
import javax.persistence.EntityManager;
9+
import javax.persistence.criteria.CriteriaBuilder;
10+
import javax.persistence.criteria.CriteriaQuery;
11+
import javax.persistence.criteria.Root;
12+
13+
import static org.hamcrest.CoreMatchers.is;
14+
import static org.hamcrest.MatcherAssert.assertThat;
15+
16+
public class MigrationIT {
17+
18+
@SneakyThrows
19+
@Test
20+
public void checkRecordsAmount() {
21+
try (var sessionFactory = new Configuration().configure().buildSessionFactory()) {
22+
try (var session = sessionFactory.openSession()) {
23+
var emFactory = session.getEntityManagerFactory();
24+
var em = emFactory.createEntityManager();
25+
26+
getFromDbAndCheck(em, Person.class, 3);
27+
}
28+
}
29+
}
30+
31+
private <T> void getFromDbAndCheck(EntityManager em, Class<T> clazz, int expectedItemsCount) {
32+
CriteriaBuilder builder = em.getCriteriaBuilder();
33+
CriteriaQuery<T> query = builder.createQuery(clazz);
34+
Root<T> variableRoot = query.from(clazz);
35+
query.select(variableRoot);
36+
var items = em.createQuery(query).getResultList();
37+
38+
System.out.println("Retrieved next items: " + items);
39+
assertThat("Wrong items count", items.size(), is(expectedItemsCount));
40+
}
41+
}

db-versioning/liquibase-db/pom.xml

Lines changed: 101 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,115 @@
1919
<liquibase.version>4.21.1</liquibase.version>
2020
</properties>
2121

22+
<dependencies>
23+
<dependency>
24+
<groupId>org.hibernate</groupId>
25+
<artifactId>hibernate-core</artifactId>
26+
<version>5.6.15.Final</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>mysql</groupId>
30+
<artifactId>mysql-connector-java</artifactId>
31+
<version>${mysql.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.projectlombok</groupId>
35+
<artifactId>lombok</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.liquibase</groupId>
39+
<artifactId>liquibase-core</artifactId>
40+
<version>${liquibase.version}</version>
41+
</dependency>
42+
</dependencies>
43+
2244
<build>
2345
<plugins>
46+
<plugin>
47+
<groupId>io.fabric8</groupId>
48+
<artifactId>docker-maven-plugin</artifactId>
49+
<version>${docker-maven-plugin.version}</version>
50+
<configuration>
51+
<showLogs>true</showLogs>
52+
<images>
53+
<image>
54+
<name>mysql:${mysql.version}</name>
55+
<run>
56+
<ports>
57+
<port>3306:3306</port>
58+
</ports>
59+
<env>
60+
<MYSQL_DATABASE>db</MYSQL_DATABASE>
61+
<MYSQL_ROOT_PASSWORD>some_password</MYSQL_ROOT_PASSWORD>
62+
<MYSQL_USER>user</MYSQL_USER>
63+
<MYSQL_PASSWORD>password</MYSQL_PASSWORD>
64+
</env>
65+
<wait>
66+
<log>starting as process 1</log>
67+
<time>20000</time>
68+
</wait>
69+
</run>
70+
</image>
71+
</images>
72+
</configuration>
73+
74+
<!-- Connect start/stop to pre- and post- integration-test phase respectively
75+
if you want to start your docker containers during integration tests -->
76+
<executions>
77+
<execution>
78+
<id>start</id>
79+
<phase>pre-integration-test</phase>
80+
<goals>
81+
<goal>stop</goal>
82+
<goal>start</goal>
83+
</goals>
84+
</execution>
85+
<execution>
86+
<id>stop</id>
87+
<phase>post-integration-test</phase>
88+
<goals>
89+
<goal>stop</goal>
90+
</goals>
91+
</execution>
92+
</executions>
93+
</plugin>
94+
95+
<plugin>
96+
<artifactId>maven-compiler-plugin</artifactId>
97+
<version>${maven-compiler-plugin.version}</version>
98+
<configuration>
99+
<annotationProcessorPaths>
100+
<path>
101+
<groupId>org.projectlombok</groupId>
102+
<artifactId>lombok</artifactId>
103+
<version>${lombok.version}</version>
104+
</path>
105+
</annotationProcessorPaths>
106+
</configuration>
107+
</plugin>
108+
109+
<!--This plugin used for running integration tests in integration-test build phase-->
110+
<plugin>
111+
<artifactId>maven-failsafe-plugin</artifactId>
112+
<version>${maven-failsafe-plugin.version}</version>
113+
<executions>
114+
<execution>
115+
<goals>
116+
<goal>integration-test</goal>
117+
<goal>verify</goal>
118+
</goals>
119+
</execution>
120+
</executions>
121+
</plugin>
122+
24123
<plugin>
25124
<groupId>org.liquibase</groupId>
26125
<artifactId>liquibase-maven-plugin</artifactId>
27126
<version>${liquibase.version}</version>
28127
<configuration>
29128
<changeLogFile>src/main/db-migration/changelog.xml</changeLogFile>
30129
<driver>${jdbc.driverClassName}</driver>
31-
<url>jdbc:h2:file:${project.build.directory}/liquibasedb</url>
130+
<url>${jdbc.url}</url>
32131
<username>${jdbc.username}</username>
33132
<password>${jdbc.password}</password>
34133
<outputFileEncoding>UTF-8</outputFileEncoding>
@@ -37,25 +136,12 @@
37136

38137
<executions>
39138
<execution>
40-
<phase>process-test-resources</phase>
139+
<phase>pre-integration-test</phase>
41140
<goals>
42141
<goal>update</goal>
43142
</goals>
44143
</execution>
45144
</executions>
46-
47-
<dependencies>
48-
<dependency>
49-
<groupId>org.liquibase</groupId>
50-
<artifactId>liquibase-core</artifactId>
51-
<version>${liquibase.version}</version>
52-
</dependency>
53-
<dependency>
54-
<groupId>com.h2database</groupId>
55-
<artifactId>h2</artifactId>
56-
<version>${h2.db.version}</version>
57-
</dependency>
58-
</dependencies>
59145
</plugin>
60146
</plugins>
61147
</build>

0 commit comments

Comments
 (0)