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

Migrating tests to AssertJ #96

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ dependencies {
implementation "org.slf4j:slf4j-api:1.7.36"

testImplementation "org.junit.jupiter:junit-jupiter:5.9.3"
testImplementation "org.hamcrest:hamcrest:2.2"
testImplementation "org.apache.kafka:connect-api:$kafkaVersion"
testImplementation "org.testcontainers:junit-jupiter:$testcontainersVersion"
testImplementation "io.debezium:debezium-api:$debeziumVersion"
testImplementation "org.assertj:assertj-core:3.24.2"

testRuntimeOnly "org.apache.logging.log4j:log4j-slf4j-impl:2.20.0"
testRuntimeOnly "org.apache.logging.log4j:log4j-api:2.20.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

@Testcontainers
final class IntegrationTest {
Expand Down Expand Up @@ -204,9 +204,9 @@ final void checkMessageTopics(final TopicPartition originalTopicPartition, final
final Map<TopicPartition, Long> endOffsets = consumer.endOffsets(
Arrays.asList(originalTopicPartition, newTopicPartition));
// The original topic should be empty.
assertEquals(0, endOffsets.get(originalTopicPartition));
assertThat(endOffsets.get(originalTopicPartition)).isZero();
// The new topic should be non-empty.
assertEquals(TestSourceConnector.MESSAGES_TO_PRODUCE, endOffsets.get(newTopicPartition));
assertThat(endOffsets).containsEntry(newTopicPartition, TestSourceConnector.MESSAGES_TO_PRODUCE);
}

private void waitForCondition(final Supplier<Boolean> conditionChecker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* <p>It just produces a fixed number of struct records.
*/
public class TestSourceConnector extends SourceConnector {
static final int MESSAGES_TO_PRODUCE = 10;
static final long MESSAGES_TO_PRODUCE = 10L;

static final String ORIGINAL_TOPIC = "original-topic";
static final String NEW_TOPIC = "new-topic";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

public class MoneyConverterTest {

Expand All @@ -59,11 +58,11 @@ void teardown() {
@Test
void shouldRegisterCorrectSchema() {
transform.configure(prop);
assertNull(registration.currFieldSchema);
assertThat(registration.currFieldSchema).isNull();
transform.converterFor(new MoneyTestRelationalColumn(), registration);

assertEquals(registration.currFieldSchema.schema().name(), "price");
assertEquals(registration.currFieldSchema.schema().type(), Schema.Type.STRING);
assertThat(registration.currFieldSchema.schema().name()).isEqualTo("price");
assertThat(registration.currFieldSchema.schema().type()).isEqualTo(Schema.Type.STRING);
}

@Test
Expand All @@ -72,40 +71,40 @@ void shouldDoNothingIfColumnIsNotMoney() {

transform.converterFor(new DummyRelationalColumn(), registration);

assertNull(registration.currFieldSchema);
assertNull(registration.currConverter);
assertThat(registration.currFieldSchema).isNull();
assertThat(registration.currConverter).isNull();
}

@Test
void shouldFormatDataToMoneyFormat() {
assertNull(registration.currConverter);
assertThat(registration.currConverter).isNull();
transform.converterFor(new MoneyTestRelationalColumn(), registration);

final String result = (String) registration.currConverter.convert(BigDecimal.valueOf(103.6999));
assertEquals(result, "103.70");
assertThat(result).isEqualTo("103.70");

final String result2 = (String) registration.currConverter.convert((long) 103);
assertEquals(result2, "103.00");
assertThat(result2).isEqualTo("103.00");
}

@Test
void shouldFailIfDataIsNotBigDecimal() {
assertNull(registration.currConverter);
assertThat(registration.currConverter).isNull();
transform.converterFor(new MoneyTestRelationalColumn(), registration);

final Throwable e = assertThrows(IllegalArgumentException.class,
() -> registration.currConverter.convert("103.6999"));
assertEquals(e.getMessage(), "Money type should have BigDecimal type");
assertThatThrownBy(() -> registration.currConverter.convert("103.6999"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Money type should have BigDecimal type");
}

@Test
void shouldFailIfDataIsMissing() {
assertNull(registration.currConverter);
assertThat(registration.currConverter).isNull();
transform.converterFor(new MoneyTestRelationalColumn(), registration);

final Throwable e = assertThrows(IllegalArgumentException.class,
() -> registration.currConverter.convert(null));
assertEquals(e.getMessage(), "Money column is not optional, but data is null");
assertThatThrownBy(() -> registration.currConverter.convert(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Money column is not optional, but data is null");
}

@Test
Expand All @@ -117,7 +116,7 @@ void shouldDoNothingIfColumnIsOptional() {
transform.converterFor(moneyColumn, registration);

final String result = (String) registration.currConverter.convert(null);
assertNull(result);
assertThat(result).isNull();
}

class StubConverterRegistration implements CustomConverter.ConverterRegistration<SchemaBuilder> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@
import static io.aiven.kafka.connect.transforms.ConcatFieldsConfig.FIELD_NAMES_CONFIG;
import static io.aiven.kafka.connect.transforms.ConcatFieldsConfig.FIELD_REPLACE_MISSING_CONFIG;
import static io.aiven.kafka.connect.transforms.ConcatFieldsConfig.OUTPUT_FIELD_NAME_CONFIG;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class ConcatFieldsConfigTest {
@Test
void emptyConfig() {
final Map<String, String> props = new HashMap<>();
final Throwable e = assertThrows(ConfigException.class, () -> new ConcatFieldsConfig(props));
assertEquals("Missing required configuration \"field.names\" which has no default value.",
e.getMessage());
assertThatThrownBy(() -> new ConcatFieldsConfig(props))
.isInstanceOf(ConfigException.class)
.hasMessage("Missing required configuration \"field.names\" which has no default value.");
}

@Test
void emptyFieldName() {
final Map<String, String> props = new HashMap<>();
props.put(FIELD_NAMES_CONFIG, "");
final Throwable e = assertThrows(ConfigException.class, () -> new ConcatFieldsConfig(props));
assertEquals("Missing required configuration \"output.field.name\" which has no default value.",
e.getMessage());
assertThatThrownBy(() -> new ConcatFieldsConfig(props))
.isInstanceOf(ConfigException.class)
.hasMessage("Missing required configuration \"output.field.name\" which has no default value.");
}

@Test
Expand All @@ -57,9 +57,9 @@ void definedFieldName() {
props.put(DELIMITER_CONFIG, "-");
props.put(FIELD_REPLACE_MISSING_CONFIG, "*");
final ConcatFieldsConfig config = new ConcatFieldsConfig(props);
assertEquals(Arrays.asList("test", "foo", "bar"), config.fieldNames());
assertEquals("combined", config.outputFieldName());
assertEquals("-", config.delimiter());
assertEquals("*", config.fieldReplaceMissing());
assertThat(config.fieldNames()).isEqualTo(Arrays.asList("test", "foo", "bar"));
assertThat(config.outputFieldName()).isEqualTo("combined");
assertThat(config.delimiter()).isEqualTo("-");
assertThat(config.fieldReplaceMissing()).isEqualTo("*");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
import static io.aiven.kafka.connect.transforms.ConcatFieldsConfig.FIELD_NAMES_CONFIG;
import static io.aiven.kafka.connect.transforms.ConcatFieldsConfig.FIELD_REPLACE_MISSING_CONFIG;
import static io.aiven.kafka.connect.transforms.ConcatFieldsConfig.OUTPUT_FIELD_NAME_CONFIG;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;

abstract class ConcatFieldsTest {
private static final String FIELD = "combined";
Expand Down Expand Up @@ -65,30 +65,27 @@ abstract class ConcatFieldsTest {

@Test
void recordNotStructOrMap() {
final SinkRecord originalRecord = record(SchemaBuilder.INT8_SCHEMA, (byte) 123);
final Throwable e = assertThrows(DataException.class,
() -> transformation().apply(originalRecord));
assertEquals("Value type must be STRUCT or MAP: " + originalRecord,
e.getMessage());
final SinkRecord originalRecord = record(Schema.INT8_SCHEMA, (byte) 123);
assertThatThrownBy(() -> transformation().apply(originalRecord))
.isInstanceOf(DataException.class)
.hasMessage("Value type must be STRUCT or MAP: " + originalRecord);
}

@Test
void recordStructNull() {
final Schema schema = SchemaBuilder.struct().schema();
final SinkRecord originalRecord = record(schema, null);
final Throwable e = assertThrows(DataException.class,
() -> transformation().apply(originalRecord));
assertEquals(dataPlace() + " Value can't be null: " + originalRecord,
e.getMessage());
assertThatThrownBy(() -> transformation().apply(originalRecord))
.isInstanceOf(DataException.class)
.hasMessage(dataPlace() + " Value can't be null: " + originalRecord);
}

@Test
void recordMapNull() {
final SinkRecord originalRecord = record(null, null);
final Throwable e = assertThrows(DataException.class,
() -> transformation().apply(originalRecord));
assertEquals(dataPlace() + " Value can't be null: " + originalRecord,
e.getMessage());
assertThatThrownBy(() -> transformation().apply(originalRecord))
.isInstanceOf(DataException.class)
.hasMessage(dataPlace() + " Value can't be null: " + originalRecord);
}

@Test
Expand All @@ -100,9 +97,9 @@ void structWithSchemaMissingField() {
.field(AGE_FIELD, Schema.OPTIONAL_INT64_SCHEMA)
.build();
final SinkRecord originalRecord = record(schema, new Struct(schema));
final Throwable e = assertThrows(DataException.class,
() -> transformation().apply(originalRecord));
assertEquals("Invalid value: null used for required field: \"bar\", schema type: STRING", e.getMessage());
assertThatThrownBy(() -> transformation().apply(originalRecord))
.isInstanceOf(DataException.class)
.hasMessage("Invalid value: null used for required field: \"bar\", schema type: STRING");
}

@Test
Expand All @@ -114,26 +111,27 @@ void structWithMissingField() {
.field(AGE_FIELD, Schema.OPTIONAL_INT64_SCHEMA)
.build();
final SinkRecord originalRecord = record(null, new Struct(schema));
final Throwable e = assertThrows(DataException.class,
() -> transformation().apply(originalRecord));
assertEquals("Invalid value: null used for required field: \"bar\", schema type: STRING", e.getMessage());
assertThatThrownBy(() -> transformation().apply(originalRecord))
.isInstanceOf(DataException.class)
.hasMessage("Invalid value: null used for required field: \"bar\", schema type: STRING");
}

@Test
void mapWithMissingField() {
final SinkRecord originalRecord = record(null, new HashMap<>());
assertDoesNotThrow(() -> transformation().apply(originalRecord),
FIELD + " field must be present and its value can't be null: " + originalRecord);
assertThatNoException()
.describedAs(FIELD + " field must be present and its value can't be null: " + originalRecord)
.isThrownBy(() -> transformation().apply(originalRecord));
}

@Test
void mapWithoutSchema() {
final HashMap<Object, Object> valueMap = new HashMap<>();
final Map<Object, Object> valueMap = new HashMap<>();
valueMap.put(BAR_FIELD, BAR_VALUE);
valueMap.put(TEST_FIELD, TEST_VALUE);
valueMap.put(AGE_FIELD, AGE_VALUE);
valueMap.put(FOO_FIELD, FOO_VALUE);
final HashMap<Object, Object> newValueMap = new HashMap<>();
final Map<Object, Object> newValueMap = new HashMap<>();
newValueMap.put(BAR_FIELD, BAR_VALUE);
newValueMap.put(FIELD, TEST_VALUE + "-" + FOO_VALUE + "-" + BAR_VALUE + "-" + AGE_VALUE);
newValueMap.put(TEST_FIELD, TEST_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,41 @@

import org.junit.jupiter.api.Test;

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

class ExtractTimestampConfigTest {
@Test
void emptyConfig() {
final Map<String, String> props = new HashMap<>();
final Throwable e = assertThrows(ConfigException.class, () -> new ExtractTimestampConfig(props));
assertEquals("Missing required configuration \"field.name\" which has no default value.",
e.getMessage());
assertThatThrownBy(() -> new ExtractTimestampConfig(props))
.isInstanceOf(ConfigException.class)
.hasMessage("Missing required configuration \"field.name\" which has no default value.");
}

@Test
void emptyFieldName() {
final Map<String, String> props = new HashMap<>();
props.put("field.name", "");
final Throwable e = assertThrows(ConfigException.class, () -> new ExtractTimestampConfig(props));
assertEquals("Invalid value for configuration field.name: String must be non-empty",
e.getMessage());
assertThatThrownBy(() -> new ExtractTimestampConfig(props))
.isInstanceOf(ConfigException.class)
.hasMessage("Invalid value for configuration field.name: String must be non-empty");
}

@Test
void definedFieldName() {
final Map<String, String> props = new HashMap<>();
props.put("field.name", "test");
final ExtractTimestampConfig config = new ExtractTimestampConfig(props);
assertEquals("test", config.fieldName());
assertThat(config.fieldName()).isEqualTo("test");
}

@Test
void emptyTimestampResolution() {
final var props = new HashMap<>();
props.put("field.name", "test");
final var config = new ExtractTimestampConfig(props);
assertEquals(ExtractTimestampConfig.TimestampResolution.MILLISECONDS, config.timestampResolution());
assertThat(config.timestampResolution()).isEqualTo(ExtractTimestampConfig.TimestampResolution.MILLISECONDS);
}

@Test
Expand All @@ -69,7 +69,7 @@ void definedTimestampResolutionInSeconds() {
ExtractTimestampConfig.TimestampResolution.SECONDS.resolution
);
final var config = new ExtractTimestampConfig(props);
assertEquals(ExtractTimestampConfig.TimestampResolution.SECONDS, config.timestampResolution());
assertThat(config.timestampResolution()).isEqualTo(ExtractTimestampConfig.TimestampResolution.SECONDS);
}

@Test
Expand All @@ -81,7 +81,7 @@ void definedTimestampResolutionInMillis() {
ExtractTimestampConfig.TimestampResolution.MILLISECONDS.resolution
);
final var config = new ExtractTimestampConfig(props);
assertEquals(ExtractTimestampConfig.TimestampResolution.MILLISECONDS, config.timestampResolution());
assertThat(config.timestampResolution()).isEqualTo(ExtractTimestampConfig.TimestampResolution.MILLISECONDS);
}

@Test
Expand All @@ -92,11 +92,10 @@ void wrongTimestampResolution() {
ExtractTimestampConfig.EPOCH_RESOLUTION_CONFIG,
"foo"
);
final var e = assertThrows(ConfigException.class, () -> new ExtractTimestampConfig(props));
assertEquals(
"Invalid value foo for configuration timestamp.resolution: "
+ "Unsupported resolution type 'foo'. Supported are: milliseconds, seconds",
e.getMessage());
assertThatThrownBy(() -> new ExtractTimestampConfig(props))
.isInstanceOf(ConfigException.class)
.hasMessage("Invalid value foo for configuration timestamp.resolution: "
+ "Unsupported resolution type 'foo'. Supported are: milliseconds, seconds");
}

}