Skip to content

Commit

Permalink
Use assertj-db for asserts in liquibase schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steinarb committed Dec 3, 2024
1 parent d14be6b commit 5151f41
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 68 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ under the License.
</modules>

<properties>
<assertj-db.version>3.0.0</assertj-db.version>
<open-iconic.version>1.1.1</open-iconic.version>
<sonar.cpd.exclusions>
**/SampleappServiceProvider.java,
Expand Down Expand Up @@ -139,6 +140,11 @@ under the License.
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-db</artifactId>
<version>${assertj-db.version}</version>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>open-iconic</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions sampleapp.db.liquibase.production/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ under the License.
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-db</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
package no.priv.bang.sampleapp.db.liquibase.production;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.db.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.assertj.db.type.AssertDbConnectionFactory;
import org.junit.jupiter.api.Test;
import org.ops4j.pax.jdbc.derby.impl.DerbyDataSourceFactory;
import org.osgi.service.jdbc.DataSourceFactory;
Expand All @@ -36,12 +38,16 @@ void testCreateAndVerifySomeDataInSomeTables() throws Exception {
var properties = new Properties();
properties.setProperty(DataSourceFactory.JDBC_URL, "jdbc:derby:memory:sampleapp;create=true");
var datasource = dataSourceFactory.createDataSource(properties);
var assertjConnection = AssertDbConnectionFactory.of(datasource).create();

var runner = new SampleappProductionDbLiquibaseRunner();
runner.activate();
runner.prepare(datasource);
var accounts1 = assertjConnection.table("sampleapp_accounts").build();
assertThat(accounts1).exists().isEmpty();
addAccount(datasource, "jd");
assertAccounts(datasource);
var accounts2 = assertjConnection.table("sampleapp_accounts").build();
assertThat(accounts2).exists().hasNumberOfRows(1).row().column("username").hasValues("jd");
}


Expand Down Expand Up @@ -125,21 +131,6 @@ void testFailWhenUpdatingSchema() throws Exception {
}


private void assertAccounts(DataSource datasource) throws Exception {
try (var connection = datasource.getConnection()) {
try(var statement = connection.prepareStatement("select * from sampleapp_accounts")) {
try (var results = statement.executeQuery()) {
assertAccount(results, "jd");
}
}
}
}

private void assertAccount(ResultSet results, String username) throws Exception {
assertTrue(results.next());
assertEquals(username, results.getString(2)); // column 1 is the id
}

private int addAccount(DataSource datasource, String username) throws Exception {
try (var connection = datasource.getConnection()) {
try(var statement = connection.prepareStatement("insert into sampleapp_accounts (username) values (?)")) {
Expand Down
5 changes: 5 additions & 0 deletions sampleapp.db.liquibase.test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ under the License.
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-db</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.db.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.assertj.db.type.AssertDbConnectionFactory;
import org.junit.jupiter.api.Test;
import org.ops4j.pax.jdbc.derby.impl.DerbyDataSourceFactory;
import org.osgi.service.jdbc.DataSourceFactory;
Expand All @@ -31,11 +34,17 @@ class SampleappTestDbLiquibaseRunnerTest {
@Test
void testCreateAndVerifySomeDataInSomeTables() throws Exception {
var datasource = createDataSource("sampleapp");
var assertjConnection = AssertDbConnectionFactory.of(datasource).create();

var runner = new SampleappTestDbLiquibaseRunner();
runner.activate();
runner.prepare(datasource);
assertAccounts(datasource);
var accounts1 = assertjConnection.table("sampleapp_accounts").build();
assertThat(accounts1).exists().isEmpty();
var incrementSteps1 = assertjConnection.table("counter_increment_steps").build();
assertThat(incrementSteps1).exists().isEmpty();
var counters1 = assertjConnection.table("counters").build();
assertThat(counters1).exists().isEmpty();
}

@Test
Expand Down Expand Up @@ -114,20 +123,6 @@ void testFailWhenUpdatingSchema() throws Exception {
assertThat(e.getMessage()).startsWith("Error updating sampleapp test database schema");
}

private void assertAccounts(DataSource datasource) throws Exception {
var resultcount = 0;
try (var connection = datasource.getConnection()) {
try(var statement = connection.prepareStatement("select * from sampleapp_accounts")) {
try (var results = statement.executeQuery()) {
while (results.next()) {
++resultcount;
}
}
}
}
assertEquals(0, resultcount);
}

private DataSource createDataSource(String dbname) throws SQLException {
var dataSourceFactory = new DerbyDataSourceFactory();
var properties = new Properties();
Expand Down
5 changes: 5 additions & 0 deletions sampleapp.db.liquibase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ under the License.
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-db</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.db.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

import java.sql.Connection;
Expand All @@ -25,6 +26,7 @@

import javax.sql.DataSource;

import org.assertj.db.type.AssertDbConnectionFactory;
import org.junit.jupiter.api.Test;
import org.ops4j.pax.jdbc.derby.impl.DerbyDataSourceFactory;
import org.osgi.service.jdbc.DataSourceFactory;
Expand All @@ -38,18 +40,43 @@ class SampleappLiquibaseTest {
void testCreateSchema() throws Exception {
var sampleappLiquibase = new SampleappLiquibase();
var datasource = createDataSource("sampleapp");
var assertjConnection = AssertDbConnectionFactory.of(datasource).create();

sampleappLiquibase.createInitialSchema(datasource.getConnection());

var accounts1 = assertjConnection.table("sampleapp_accounts").build();
assertThat(accounts1).exists().isEmpty();

try(var connection = datasource.getConnection()) {
addAccounts(connection);
assertAccounts(connection);
}

var accounts2 = assertjConnection.table("sampleapp_accounts").build();
assertThat(accounts2).hasNumberOfRows(1);

var incrementSteps1 = assertjConnection.table("counter_increment_steps").build();
assertThat(incrementSteps1).exists().isEmpty();

try(var connection = datasource.getConnection()) {
addCounterIncrementSteps(connection);
assertCounterIncrementSteps(connection);
}

var incrementSteps2 = assertjConnection.table("counter_increment_steps").build();
assertThat(incrementSteps2).hasNumberOfRows(1);

var counters1 = assertjConnection.table("counters").build();
assertThat(counters1).exists().isEmpty();

try(var connection = datasource.getConnection()) {
addCounters(connection);
}

var counters2 = assertjConnection.table("counters").build();
assertThat(counters2).hasNumberOfRows(1);

try(var connection = datasource.getConnection()) {
int accountIdNotMatchingAccount = 375;
assertThrows(SQLException.class,() -> addCounterIncrementStep(connection, accountIdNotMatchingAccount, 10));
addCounters(connection);
assertCounters(connection);
assertThrows(SQLException.class,() -> addCounter(connection, accountIdNotMatchingAccount, 4));
}

Expand Down Expand Up @@ -89,46 +116,14 @@ private void addAccounts(Connection connection) throws Exception {
addAccount(connection, "admin");
}

private void assertAccounts(Connection connection) throws Exception {
var sql = "select count(*) from sampleapp_accounts";
try(var statement = connection.prepareStatement(sql)) {
try(var results = statement.executeQuery()) {
if (results.next()) {
var count = results.getInt(1);
assertEquals(1, count);
}
}
}
}

private void addCounterIncrementSteps(Connection connection) throws Exception {
addCounterIncrementStep(connection, findAccountId(connection, "admin"), 10);
}

private void assertCounterIncrementSteps(Connection connection) throws Exception {
try(var statement = connection.createStatement()) {
try(var results = statement.executeQuery("select * from counter_increment_steps")) {
assertTrue(results.next());
assertEquals(findAccountId(connection, "admin"), results.getInt(2));
assertEquals(10, results.getInt(3));
}
}
}

private void addCounters(Connection connection) throws Exception {
addCounter(connection, findAccountId(connection, "admin"), 3);
}

private void assertCounters(Connection connection) throws Exception {
try(var statement = connection.createStatement()) {
try(var results = statement.executeQuery("select * from counters")) {
assertTrue(results.next());
assertEquals(findAccountId(connection, "admin"), results.getInt(2));
assertEquals(3, results.getInt(3));
}
}
}

private int addAccount(Connection connection, String username) throws Exception {
var sql = "insert into sampleapp_accounts (username) values (?)";
try(var statement = connection.prepareStatement(sql)) {
Expand Down

0 comments on commit 5151f41

Please sign in to comment.