Skip to content

Commit

Permalink
Merge pull request #288 from Aiven-Open/anatolii/assertj-migration
Browse files Browse the repository at this point in the history
Migration to AssertJ
  • Loading branch information
tvainika authored Mar 5, 2024
2 parents 80350d9 + 2597d15 commit 2a418bb
Show file tree
Hide file tree
Showing 26 changed files with 585 additions and 731 deletions.
91 changes: 46 additions & 45 deletions src/test/java/io/aiven/connect/jdbc/JdbcSourceConnectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import io.aiven.connect.jdbc.util.TableId;

import org.easymock.EasyMock;
import org.easymock.Mock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -48,9 +47,8 @@
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@RunWith(PowerMockRunner.class)
@PrepareForTest({JdbcSourceConnector.class, DatabaseDialect.class})
Expand All @@ -61,9 +59,6 @@ public class JdbcSourceConnectorTest {
private EmbeddedDerby db;
private Map<String, String> connProps;

@Mock
private DatabaseDialect dialect;

@Before
public void setup() {
connector = new JdbcSourceConnector();
Expand All @@ -82,27 +77,26 @@ public void tearDown() throws Exception {

@Test
public void testTaskClass() {
assertEquals(JdbcSourceTask.class, connector.taskClass());
assertThat(connector.taskClass()).isEqualTo(JdbcSourceTask.class);
}

@Test(expected = ConnectException.class)
public void testMissingUrlConfig() throws Exception {
@Test
public void testMissingUrlConfig() {
final HashMap<String, String> connProps = new HashMap<>();
connProps.put(JdbcSourceConnectorConfig.MODE_CONFIG, JdbcSourceConnectorConfig.MODE_BULK);
connector.start(connProps);
assertThatThrownBy(() -> connector.start(connProps)).isInstanceOf(ConnectException.class);
}

@Test(expected = ConnectException.class)
public void testMissingModeConfig() throws Exception {
final HashMap<String, String> connProps = new HashMap<>();
connProps.put(JdbcConfig.CONNECTION_URL_CONFIG, db.getUrl());
connector.start(Collections.<String, String>emptyMap());
@Test
public void testMissingModeConfig() {
assertThatThrownBy(() -> connector.start(Collections.emptyMap())).isInstanceOf(ConnectException.class);
}

@Test(expected = ConnectException.class)
public void testStartConnectionFailure() throws Exception {
@Test
public void testStartConnectionFailure() {
// Invalid URL
connector.start(Collections.singletonMap(JdbcConfig.CONNECTION_URL_CONFIG, "jdbc:foo"));
final Map<String, String> connProps = Collections.singletonMap(JdbcConfig.CONNECTION_URL_CONFIG, "jdbc:foo");
assertThatThrownBy(() -> connector.start(connProps)).isInstanceOf(ConnectException.class);
}

@Test
Expand Down Expand Up @@ -138,15 +132,16 @@ public void testStartStop() throws Exception {

@Test
public void testPartitioningOneTable() throws Exception {
// Tests simplest case where we have exactly 1 table and also ensures we return fewer tasks
// Tests the simplest case where we have exactly 1 table and also ensures we return fewer tasks
// if there aren't enough tables for the max # of tasks
db.createTable("test", "id", "INT NOT NULL");
connector.start(connProps);
final List<Map<String, String>> configs = connector.taskConfigs(10);
assertEquals(1, configs.size());
assertThat(configs).hasSize(1);
assertTaskConfigsHaveParentConfigs(configs);
assertEquals(tables("test"), configs.get(0).get(JdbcSourceTaskConfig.TABLES_CONFIG));
assertNull(configs.get(0).get(JdbcSourceTaskConfig.QUERY_CONFIG));
assertThat(configs.get(0))
.containsEntry(JdbcSourceTaskConfig.TABLES_CONFIG, tables("test"))
.doesNotContainKey(JdbcSourceTaskConfig.QUERY_CONFIG);
connector.stop();
}

Expand All @@ -159,15 +154,18 @@ public void testPartitioningManyTables() throws Exception {
db.createTable("test4", "id", "INT NOT NULL");
connector.start(connProps);
final List<Map<String, String>> configs = connector.taskConfigs(3);
assertEquals(3, configs.size());
assertThat(configs).hasSize(3);
assertTaskConfigsHaveParentConfigs(configs);

assertEquals(tables("test1", "test2"), configs.get(0).get(JdbcSourceTaskConfig.TABLES_CONFIG));
assertNull(configs.get(0).get(JdbcSourceTaskConfig.QUERY_CONFIG));
assertEquals(tables("test3"), configs.get(1).get(JdbcSourceTaskConfig.TABLES_CONFIG));
assertNull(configs.get(1).get(JdbcSourceTaskConfig.QUERY_CONFIG));
assertEquals(tables("test4"), configs.get(2).get(JdbcSourceTaskConfig.TABLES_CONFIG));
assertNull(configs.get(2).get(JdbcSourceTaskConfig.QUERY_CONFIG));
assertThat(configs.get(0))
.containsEntry(JdbcSourceTaskConfig.TABLES_CONFIG, tables("test1", "test2"))
.doesNotContainKey(JdbcSourceTaskConfig.QUERY_CONFIG);
assertThat(configs.get(1))
.containsEntry(JdbcSourceTaskConfig.TABLES_CONFIG, tables("test3"))
.doesNotContainKey(JdbcSourceTaskConfig.QUERY_CONFIG);
assertThat(configs.get(2))
.containsEntry(JdbcSourceTaskConfig.TABLES_CONFIG, tables("test4"))
.doesNotContainKey(JdbcSourceTaskConfig.QUERY_CONFIG);

connector.stop();
}
Expand All @@ -182,15 +180,18 @@ public void testPartitioningUnqualifiedTables() throws Exception {
db.createTable("test4", "id", "INT NOT NULL");
connector.start(connProps);
final List<Map<String, String>> configs = connector.taskConfigs(3);
assertEquals(3, configs.size());
assertThat(configs).hasSize(3);
assertTaskConfigsHaveParentConfigs(configs);

assertEquals(unqualifiedTables("test1", "test2"), configs.get(0).get(JdbcSourceTaskConfig.TABLES_CONFIG));
assertNull(configs.get(0).get(JdbcSourceTaskConfig.QUERY_CONFIG));
assertEquals(unqualifiedTables("test3"), configs.get(1).get(JdbcSourceTaskConfig.TABLES_CONFIG));
assertNull(configs.get(1).get(JdbcSourceTaskConfig.QUERY_CONFIG));
assertEquals(unqualifiedTables("test4"), configs.get(2).get(JdbcSourceTaskConfig.TABLES_CONFIG));
assertNull(configs.get(2).get(JdbcSourceTaskConfig.QUERY_CONFIG));
assertThat(configs.get(0))
.containsEntry(JdbcSourceTaskConfig.TABLES_CONFIG, unqualifiedTables("test1", "test2"))
.doesNotContainKey(JdbcSourceTaskConfig.QUERY_CONFIG);
assertThat(configs.get(1))
.containsEntry(JdbcSourceTaskConfig.TABLES_CONFIG, unqualifiedTables("test3"))
.doesNotContainKey(JdbcSourceTaskConfig.QUERY_CONFIG);
assertThat(configs.get(2))
.containsEntry(JdbcSourceTaskConfig.TABLES_CONFIG, unqualifiedTables("test4"))
.doesNotContainKey(JdbcSourceTaskConfig.QUERY_CONFIG);

connector.stop();
}
Expand All @@ -204,11 +205,12 @@ public void testPartitioningQuery() throws Exception {
connProps.put(JdbcSourceConnectorConfig.QUERY_CONFIG, sampleQuery);
connector.start(connProps);
final List<Map<String, String>> configs = connector.taskConfigs(3);
assertEquals(1, configs.size());
assertThat(configs).hasSize(1);
assertTaskConfigsHaveParentConfigs(configs);

assertEquals("", configs.get(0).get(JdbcSourceTaskConfig.TABLES_CONFIG));
assertEquals(sampleQuery, configs.get(0).get(JdbcSourceTaskConfig.QUERY_CONFIG));
assertThat(configs.get(0))
.containsEntry(JdbcSourceTaskConfig.TABLES_CONFIG, "")
.containsEntry(JdbcSourceTaskConfig.QUERY_CONFIG, sampleQuery);

connector.stop();
}
Expand All @@ -218,23 +220,22 @@ public void testConflictingQueryTableSettings() {
final String sampleQuery = "SELECT foo, bar FROM sample_table";
connProps.put(JdbcSourceConnectorConfig.QUERY_CONFIG, sampleQuery);
connProps.put(JdbcSourceConnectorConfig.TABLE_WHITELIST_CONFIG, "foo,bar");
assertThrows(ConnectException.class, () -> connector.start(connProps));
assertThatThrownBy(() -> connector.start(connProps)).isInstanceOf(ConnectException.class);

connector = new JdbcSourceConnector();
connProps.remove(JdbcSourceConnectorConfig.QUERY_CONFIG);
connProps.put(JdbcSourceConnectorConfig.TABLE_NAMES_QUALIFY_CONFIG, "false");
connProps.put(JdbcSourceConnectorConfig.MODE_CONFIG, JdbcSourceConnectorConfig.MODE_INCREMENTING);
assertThrows(ConnectException.class, () -> connector.start(connProps));
assertThatThrownBy(() -> connector.start(connProps)).isInstanceOf(ConnectException.class);

connector = new JdbcSourceConnector();
connProps.put(JdbcSourceConnectorConfig.MODE_CONFIG, JdbcSourceConnectorConfig.MODE_TIMESTAMP_INCREMENTING);
assertThrows(ConnectException.class, () -> connector.start(connProps));
assertThatThrownBy(() -> connector.start(connProps)).isInstanceOf(ConnectException.class);
}

private void assertTaskConfigsHaveParentConfigs(final List<Map<String, String>> configs) {
for (final Map<String, String> config : configs) {
assertEquals(this.db.getUrl(),
config.get(JdbcConfig.CONNECTION_URL_CONFIG));
assertThat(config).containsEntry(JdbcConfig.CONNECTION_URL_CONFIG, this.db.getUrl());
}
}

Expand Down
30 changes: 15 additions & 15 deletions src/test/java/io/aiven/connect/jdbc/dialect/BaseDialectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -254,7 +253,7 @@ protected void assertMapping(
}
final SinkRecordField field = new SinkRecordField(schemaBuilder.build(), schemaName, false);
final String sqlType = dialect.getSqlType(field);
assertEquals(expectedSqlType, sqlType);
assertThat(sqlType).isEqualTo(expectedSqlType);
}

protected void assertMapping(
Expand All @@ -270,8 +269,9 @@ protected void assertMapping(

protected Map<String, String> propertiesFromPairs(final String... pairs) {
final Map<String, String> props = new HashMap<>();
assertEquals("Expecting even number of properties but found " + pairs.length, 0,
pairs.length % 2);
assertThat(pairs.length % 2)
.as("Expecting even number of properties but found " + pairs.length)
.isZero();
for (int i = 0; i != pairs.length; ++i) {
final String key = pairs[i];
final String value = pairs[++i];
Expand All @@ -282,7 +282,7 @@ protected Map<String, String> propertiesFromPairs(final String... pairs) {

protected void assertStatements(final String[] expected, final List<String> actual) {
// TODO: Remove
assertEquals(expected.length, actual.size());
assertThat(actual).hasSameSizeAs(expected);
for (int i = 0; i != expected.length; ++i) {
assertQueryEquals(expected[i], actual.get(i));
}
Expand All @@ -302,17 +302,17 @@ protected Collection<ColumnId> columns(final TableId id, final String... names)

protected void verifyDataTypeMapping(final String expected, final Schema schema) {
final SinkRecordField field = new SinkRecordField(schema, schema.name(), schema.isOptional());
assertEquals(expected, dialect.getSqlType(field));
assertThat(dialect.getSqlType(field)).isEqualTo(expected);
}

protected void verifyCreateOneColNoPk(final String expected) {
assertQueryEquals(expected, dialect.buildCreateTableStatement(tableId, Arrays.asList(
assertQueryEquals(expected, dialect.buildCreateTableStatement(tableId, List.of(
new SinkRecordField(Schema.INT32_SCHEMA, "col1", false)
)));
}

protected void verifyCreateOneColOnePk(final String expected) {
assertQueryEquals(expected, dialect.buildCreateTableStatement(tableId, Arrays.asList(
assertQueryEquals(expected, dialect.buildCreateTableStatement(tableId, List.of(
new SinkRecordField(Schema.INT32_SCHEMA, "pk1", true)
)));
}
Expand All @@ -326,16 +326,16 @@ protected void verifyCreateThreeColTwoPk(final String expected) {
}

protected void verifyAlterAddOneCol(final String... expected) {
assertArrayEquals(expected, dialect.buildAlterTable(tableId, Arrays.asList(
assertThat(dialect.buildAlterTable(tableId, List.of(
new SinkRecordField(Schema.OPTIONAL_INT32_SCHEMA, "newcol1", false)
)).toArray());
))).containsExactly(expected);
}

protected void verifyAlterAddTwoCols(final String... expected) {
assertArrayEquals(expected, dialect.buildAlterTable(tableId, Arrays.asList(
assertThat(dialect.buildAlterTable(tableId, Arrays.asList(
new SinkRecordField(Schema.OPTIONAL_INT32_SCHEMA, "newcol1", false),
new SinkRecordField(SchemaBuilder.int32().defaultValue(42).build(), "newcol2", false)
)).toArray());
))).containsExactly(expected);
}

@Test
Expand Down Expand Up @@ -419,11 +419,11 @@ public void bindFieldMapUnsupported() throws SQLException {
}

protected void assertSanitizedUrl(final String url, final String expectedSanitizedUrl) {
assertEquals(expectedSanitizedUrl, dialect.sanitizedUrl(url));
assertThat(dialect.sanitizedUrl(url)).isEqualTo(expectedSanitizedUrl);
}

protected void assertQueryEquals(final String expected, final String actual) {
assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

protected PreparedStatement verifyBindField(final int index, final Schema schema, final Object value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
import org.junit.runners.Parameterized;
import org.mockito.Mock;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.Offset.offset;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -113,9 +114,9 @@ public void testValueConversionOnNumeric() throws Exception {
dialect.addFieldToSchema(columnDefn, schemaBuilder);
final Schema schema = schemaBuilder.build();
final List<Field> fields = schema.fields();
assertEquals(1, fields.size());
assertThat(fields).hasSize(1);
final Field field = fields.get(0);
assertEquals(expectedType, field.schema().type());
assertThat(field.schema().type()).isEqualTo(expectedType);

// Set up the ResultSet
when(resultSet.getBigDecimal(1, scale)).thenReturn(BIG_DECIMAL);
Expand All @@ -137,9 +138,10 @@ public void testValueConversionOnNumeric() throws Exception {
);
final Object value = converter.convert(resultSet);
if (value instanceof Number && expectedValue instanceof Number) {
assertEquals(((Number) expectedValue).floatValue(), ((Number) value).floatValue(), 0.01d);
assertThat(((Number) value).floatValue())
.isCloseTo(((Number) expectedValue).floatValue(), offset(0.01f));
} else {
assertEquals(expectedValue, value);
assertThat(value).isEqualTo(expectedValue);
}
}

Expand Down Expand Up @@ -172,8 +174,8 @@ protected JdbcSourceConnectorConfig sourceConfigWithUrl(

protected Map<String, String> propertiesFromPairs(final String... pairs) {
final Map<String, String> props = new HashMap<>();
assertEquals("Expecting even number of properties but found " + pairs.length, 0,
pairs.length % 2);
assertThat(pairs.length % 2).as("Expecting even number of properties but found " + pairs.length)
.isZero();
for (int i = 0; i != pairs.length; ++i) {
final String key = pairs[i];
final String value = pairs[++i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,25 @@
import org.junit.Test;

import static junit.framework.TestCase.assertSame;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThat;

public class DatabaseDialectsTest {

@Test
public void shouldLoadAllBuiltInDialects() {
final Collection<? extends DatabaseDialectProvider> providers = DatabaseDialects
.registeredDialectProviders();
assertContainsInstanceOf(providers, GenericDatabaseDialect.Provider.class);
assertContainsInstanceOf(providers, DerbyDatabaseDialect.Provider.class);
assertContainsInstanceOf(providers, OracleDatabaseDialect.Provider.class);
assertContainsInstanceOf(providers, SqliteDatabaseDialect.Provider.class);
assertContainsInstanceOf(providers, PostgreSqlDatabaseDialect.Provider.class);
assertContainsInstanceOf(providers, MySqlDatabaseDialect.Provider.class);
assertContainsInstanceOf(providers, SqlServerDatabaseDialect.Provider.class);
assertContainsInstanceOf(providers, SapHanaDatabaseDialect.Provider.class);
assertContainsInstanceOf(providers, VerticaDatabaseDialect.Provider.class);
assertContainsInstanceOf(providers, MockDatabaseDialect.Provider.class);
assertThat(providers)
.hasAtLeastOneElementOfType(GenericDatabaseDialect.Provider.class)
.hasAtLeastOneElementOfType(DerbyDatabaseDialect.Provider.class)
.hasAtLeastOneElementOfType(OracleDatabaseDialect.Provider.class)
.hasAtLeastOneElementOfType(SqliteDatabaseDialect.Provider.class)
.hasAtLeastOneElementOfType(PostgreSqlDatabaseDialect.Provider.class)
.hasAtLeastOneElementOfType(MySqlDatabaseDialect.Provider.class)
.hasAtLeastOneElementOfType(SqlServerDatabaseDialect.Provider.class)
.hasAtLeastOneElementOfType(SapHanaDatabaseDialect.Provider.class)
.hasAtLeastOneElementOfType(VerticaDatabaseDialect.Provider.class)
.hasAtLeastOneElementOfType(MockDatabaseDialect.Provider.class);
}

@Test
Expand Down Expand Up @@ -89,9 +90,6 @@ public void shouldFindSqlServerDialect() {

@Test
public void shouldFindSapDialect() {
try {
} finally {
}
assertDialect(SapHanaDatabaseDialect.class, "jdbc:sap://myServer:30015/?autocommit=false");
}

Expand Down Expand Up @@ -128,17 +126,4 @@ private void assertDialect(
final DatabaseDialect dialect = DatabaseDialects.findBestFor(url, config);
assertSame(dialect.getClass(), clazz);
}

private void assertContainsInstanceOf(
final Collection<? extends DatabaseDialectProvider> providers,
final Class<? extends DatabaseDialectProvider> clazz
) {
for (final DatabaseDialectProvider provider : providers) {
if (provider.getClass().equals(clazz)) {
return;
}
}
fail("Missing " + clazz.getName());
}

}
Loading

0 comments on commit 2a418bb

Please sign in to comment.