diff --git a/pom.xml b/pom.xml index efc3fb6..c936d13 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,7 @@ under the License. + 3.0.0 1.1.1 **/SampleappServiceProvider.java, @@ -139,6 +140,11 @@ under the License. jaxb-api 2.3.1 + + org.assertj + assertj-db + ${assertj-db.version} + org.webjars.bower open-iconic diff --git a/sampleapp.db.liquibase.production/pom.xml b/sampleapp.db.liquibase.production/pom.xml index 6cf9b28..1ccfe57 100644 --- a/sampleapp.db.liquibase.production/pom.xml +++ b/sampleapp.db.liquibase.production/pom.xml @@ -59,6 +59,11 @@ under the License. assertj-core test + + org.assertj + assertj-db + test + org.mockito mockito-core diff --git a/sampleapp.db.liquibase.production/src/test/java/no/priv/bang/sampleapp/db/liquibase/production/SampleappProductionDbLiquibaseRunnerTest.java b/sampleapp.db.liquibase.production/src/test/java/no/priv/bang/sampleapp/db/liquibase/production/SampleappProductionDbLiquibaseRunnerTest.java index 8d163d4..7595411 100644 --- a/sampleapp.db.liquibase.production/src/test/java/no/priv/bang/sampleapp/db/liquibase/production/SampleappProductionDbLiquibaseRunnerTest.java +++ b/sampleapp.db.liquibase.production/src/test/java/no/priv/bang/sampleapp/db/liquibase/production/SampleappProductionDbLiquibaseRunnerTest.java @@ -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; @@ -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"); } @@ -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 (?)")) { diff --git a/sampleapp.db.liquibase.test/pom.xml b/sampleapp.db.liquibase.test/pom.xml index 367a619..ab07353 100644 --- a/sampleapp.db.liquibase.test/pom.xml +++ b/sampleapp.db.liquibase.test/pom.xml @@ -59,6 +59,11 @@ under the License. assertj-core test + + org.assertj + assertj-db + test + org.mockito mockito-core diff --git a/sampleapp.db.liquibase.test/src/test/java/no/priv/bang/sampleapp/db/liquibase/test/SampleappTestDbLiquibaseRunnerTest.java b/sampleapp.db.liquibase.test/src/test/java/no/priv/bang/sampleapp/db/liquibase/test/SampleappTestDbLiquibaseRunnerTest.java index 9367a3b..ca96862 100644 --- a/sampleapp.db.liquibase.test/src/test/java/no/priv/bang/sampleapp/db/liquibase/test/SampleappTestDbLiquibaseRunnerTest.java +++ b/sampleapp.db.liquibase.test/src/test/java/no/priv/bang/sampleapp/db/liquibase/test/SampleappTestDbLiquibaseRunnerTest.java @@ -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; @@ -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 @@ -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(); diff --git a/sampleapp.db.liquibase/pom.xml b/sampleapp.db.liquibase/pom.xml index 807cadb..3992b89 100644 --- a/sampleapp.db.liquibase/pom.xml +++ b/sampleapp.db.liquibase/pom.xml @@ -41,6 +41,11 @@ under the License. assertj-core test + + org.assertj + assertj-db + test + org.mockito mockito-core diff --git a/sampleapp.db.liquibase/src/test/java/no/priv/bang/sampleapp/db/liquibase/SampleappLiquibaseTest.java b/sampleapp.db.liquibase/src/test/java/no/priv/bang/sampleapp/db/liquibase/SampleappLiquibaseTest.java index d828ab3..80d2912 100644 --- a/sampleapp.db.liquibase/src/test/java/no/priv/bang/sampleapp/db/liquibase/SampleappLiquibaseTest.java +++ b/sampleapp.db.liquibase/src/test/java/no/priv/bang/sampleapp/db/liquibase/SampleappLiquibaseTest.java @@ -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; @@ -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; @@ -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)); } @@ -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)) {