From 2bde0beaec7617b3e7c3ffbb3ecfb0d2aeadce57 Mon Sep 17 00:00:00 2001 From: Qingsheng Ren Date: Thu, 30 Nov 2023 17:31:20 +0800 Subject: [PATCH] [cdc-common] Introduce AssertJ-style assertion system for Flink CDC data structures --- .../assertions/AddColumnEventAssert.java | 41 +++++++++++ .../AlterColumnTypeEventAssert.java | 44 ++++++++++++ .../assertions/ChangeEventAssert.java | 45 ++++++++++++ .../assertions/CreateTableEventAssert.java | 44 ++++++++++++ .../assertions/DataChangeEventAssert.java | 60 ++++++++++++++++ .../assertions/DropColumnEventAssert.java | 43 ++++++++++++ .../testutils/assertions/EventAssert.java | 43 ++++++++++++ .../testutils/assertions/EventAssertions.java | 69 +++++++++++++++++++ .../assertions/RecordDataAssert.java | 47 +++++++++++++ .../RecordDataWithSchemaAssert.java | 49 +++++++++++++ .../assertions/RenameColumnEventAssert.java | 42 +++++++++++ .../assertions/SchemaChangeEventAssert.java | 63 +++++++++++++++++ 12 files changed, 590 insertions(+) create mode 100644 flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/AddColumnEventAssert.java create mode 100644 flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/AlterColumnTypeEventAssert.java create mode 100644 flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/ChangeEventAssert.java create mode 100644 flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/CreateTableEventAssert.java create mode 100644 flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/DataChangeEventAssert.java create mode 100644 flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/DropColumnEventAssert.java create mode 100644 flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/EventAssert.java create mode 100644 flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/EventAssertions.java create mode 100644 flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/RecordDataAssert.java create mode 100644 flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/RecordDataWithSchemaAssert.java create mode 100644 flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/RenameColumnEventAssert.java create mode 100644 flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/SchemaChangeEventAssert.java diff --git a/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/AddColumnEventAssert.java b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/AddColumnEventAssert.java new file mode 100644 index 000000000..2b5505241 --- /dev/null +++ b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/AddColumnEventAssert.java @@ -0,0 +1,41 @@ +/* + * Copyright 2023 Ververica Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ververica.cdc.common.testutils.assertions; + +import com.ververica.cdc.common.event.AddColumnEvent; +import org.assertj.core.internal.Iterables; + +import java.util.List; + +/** Assertions for {@link AddColumnEvent}. */ +public class AddColumnEventAssert extends ChangeEventAssert { + private final Iterables iterables = Iterables.instance(); + + public static AddColumnEventAssert assertThatAddColumnEvent(AddColumnEvent event) { + return new AddColumnEventAssert(event); + } + + private AddColumnEventAssert(AddColumnEvent addColumnEvent) { + super(addColumnEvent, AddColumnEventAssert.class); + } + + public AddColumnEventAssert containsAddedColumns(AddColumnEvent.ColumnWithPosition... columns) { + List actualAddedColumns = actual.getAddedColumns(); + iterables.assertContainsExactlyInAnyOrder(info, actualAddedColumns, columns); + return myself; + } +} diff --git a/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/AlterColumnTypeEventAssert.java b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/AlterColumnTypeEventAssert.java new file mode 100644 index 000000000..55915d855 --- /dev/null +++ b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/AlterColumnTypeEventAssert.java @@ -0,0 +1,44 @@ +/* + * Copyright 2023 Ververica Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ververica.cdc.common.testutils.assertions; + +import com.ververica.cdc.common.event.AlterColumnTypeEvent; +import com.ververica.cdc.common.types.DataType; +import org.assertj.core.internal.Maps; + +import java.util.Map; + +/** Assertions for {@link AlterColumnTypeEvent}. */ +public class AlterColumnTypeEventAssert + extends ChangeEventAssert { + private final Maps maps = Maps.instance(); + + public static AlterColumnTypeEventAssert assertThatAlterColumnTypeEvent( + AlterColumnTypeEvent event) { + return new AlterColumnTypeEventAssert(event); + } + + private AlterColumnTypeEventAssert(AlterColumnTypeEvent event) { + super(event, AlterColumnTypeEventAssert.class); + } + + public AlterColumnTypeEventAssert containsTypeMapping(Map typeMapping) { + Map actualTypeMapping = actual.getTypeMapping(); + maps.assertContainsAllEntriesOf(info, actualTypeMapping, typeMapping); + return myself; + } +} diff --git a/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/ChangeEventAssert.java b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/ChangeEventAssert.java new file mode 100644 index 000000000..84461d2ad --- /dev/null +++ b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/ChangeEventAssert.java @@ -0,0 +1,45 @@ +/* + * Copyright 2023 Ververica Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ververica.cdc.common.testutils.assertions; + +import com.ververica.cdc.common.event.ChangeEvent; +import com.ververica.cdc.common.event.TableId; +import org.assertj.core.api.AbstractAssert; + +/** Assertions for {@link ChangeEvent}. */ +public class ChangeEventAssert, EVENT extends ChangeEvent> + extends AbstractAssert { + + public static > + ChangeEventAssert assertThatChangeEvent(ChangeEvent changeEvent) { + return new ChangeEventAssert<>(changeEvent, ChangeEventAssert.class); + } + + protected ChangeEventAssert(EVENT event, Class selfType) { + super(event, selfType); + } + + public SELF hasTableId(TableId tableId) { + if (!actual.tableId().equals(tableId)) { + failWithActualExpectedAndMessage( + actual.tableId(), + tableId, + "Table ID of the DataChangeEvent is not as expected"); + } + return myself; + } +} diff --git a/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/CreateTableEventAssert.java b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/CreateTableEventAssert.java new file mode 100644 index 000000000..5c51a8707 --- /dev/null +++ b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/CreateTableEventAssert.java @@ -0,0 +1,44 @@ +/* + * Copyright 2023 Ververica Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ververica.cdc.common.testutils.assertions; + +import com.ververica.cdc.common.event.CreateTableEvent; +import com.ververica.cdc.common.schema.Schema; + +/** Assertions for {@link CreateTableEvent}. */ +public class CreateTableEventAssert + extends ChangeEventAssert { + + public static CreateTableEventAssert assertThatCreateTableEvent(CreateTableEvent event) { + return new CreateTableEventAssert(event); + } + + protected CreateTableEventAssert(CreateTableEvent event) { + super(event, CreateTableEventAssert.class); + } + + public CreateTableEventAssert hasSchema(Schema schema) { + isNotNull(); + if (!actual.getSchema().equals(schema)) { + failWithActualExpectedAndMessage( + actual.getSchema(), + schema, + "The schema of the CreateTableEvent is not as expected"); + } + return myself; + } +} diff --git a/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/DataChangeEventAssert.java b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/DataChangeEventAssert.java new file mode 100644 index 000000000..bc09f9518 --- /dev/null +++ b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/DataChangeEventAssert.java @@ -0,0 +1,60 @@ +/* + * Copyright 2023 Ververica Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ververica.cdc.common.testutils.assertions; + +import com.ververica.cdc.common.event.DataChangeEvent; +import com.ververica.cdc.common.event.OperationType; + +/** Assertions for {@link DataChangeEvent}. */ +public class DataChangeEventAssert + extends ChangeEventAssert { + + public static DataChangeEventAssert assertThatDataChangeEvent(DataChangeEvent event) { + return new DataChangeEventAssert(event); + } + + protected DataChangeEventAssert(DataChangeEvent dataChangeEvent) { + super(dataChangeEvent, DataChangeEventAssert.class); + } + + public DataChangeEventAssert hasOperationType(OperationType operationType) { + if (!actual.op().equals(operationType)) { + failWithMessage( + "Expect DataChangeEvent to have operation type \"%s\", but was \"%s\"", + operationType, actual.op()); + } + return this; + } + + public RecordDataAssert withBeforeRecordData() { + if (actual.before() == null) { + failWithMessage( + "DataChangeEvent with operation type \"%s\" does not have \"before\" field", + actual.op()); + } + return RecordDataAssert.assertThatRecordData(actual.before()); + } + + public RecordDataAssert withAfterRecordData() { + if (actual.after() == null) { + failWithMessage( + "DataChangeEvent with operation type \"%s\" does not have \"after\" field", + actual.op()); + } + return RecordDataAssert.assertThatRecordData(actual.after()); + } +} diff --git a/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/DropColumnEventAssert.java b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/DropColumnEventAssert.java new file mode 100644 index 000000000..633b47997 --- /dev/null +++ b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/DropColumnEventAssert.java @@ -0,0 +1,43 @@ +/* + * Copyright 2023 Ververica Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ververica.cdc.common.testutils.assertions; + +import com.ververica.cdc.common.event.DropColumnEvent; +import com.ververica.cdc.common.schema.Column; +import org.assertj.core.internal.Iterables; + +import java.util.List; + +/** Assertions for {@link DropColumnEvent}. */ +public class DropColumnEventAssert + extends ChangeEventAssert { + private final Iterables iterables = Iterables.instance(); + + public static DropColumnEventAssert assertThatDropColumnEvent(DropColumnEvent event) { + return new DropColumnEventAssert(event); + } + + private DropColumnEventAssert(DropColumnEvent event) { + super(event, DropColumnEventAssert.class); + } + + public DropColumnEventAssert containsDroppedColumns(Column... droppedColumns) { + List actualDroppedColumns = actual.getDroppedColumns(); + iterables.assertContainsExactlyInAnyOrder(info, actualDroppedColumns, droppedColumns); + return myself; + } +} diff --git a/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/EventAssert.java b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/EventAssert.java new file mode 100644 index 000000000..9370307c7 --- /dev/null +++ b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/EventAssert.java @@ -0,0 +1,43 @@ +/* + * Copyright 2023 Ververica Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ververica.cdc.common.testutils.assertions; + +import com.ververica.cdc.common.event.DataChangeEvent; +import com.ververica.cdc.common.event.Event; +import com.ververica.cdc.common.event.SchemaChangeEvent; +import org.assertj.core.api.AbstractAssert; + +/** Assertions for {@link EventAssert}. */ +public class EventAssert extends AbstractAssert { + public static EventAssert assertThatEvent(Event event) { + return new EventAssert(event); + } + + protected EventAssert(Event event) { + super(event, EventAssert.class); + } + + public DataChangeEventAssert asDataChangeEvent() { + isInstanceOf(DataChangeEvent.class); + return DataChangeEventAssert.assertThatDataChangeEvent((DataChangeEvent) actual); + } + + public SchemaChangeEventAssert asSchemaChangeEvent() { + isInstanceOf(SchemaChangeEvent.class); + return SchemaChangeEventAssert.assertThatSchemaChangeEvent((SchemaChangeEvent) actual); + } +} diff --git a/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/EventAssertions.java b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/EventAssertions.java new file mode 100644 index 000000000..d7692ebc5 --- /dev/null +++ b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/EventAssertions.java @@ -0,0 +1,69 @@ +/* + * Copyright 2023 Ververica Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ververica.cdc.common.testutils.assertions; + +import com.ververica.cdc.common.data.RecordData; +import com.ververica.cdc.common.event.AddColumnEvent; +import com.ververica.cdc.common.event.ChangeEvent; +import com.ververica.cdc.common.event.CreateTableEvent; +import com.ververica.cdc.common.event.DataChangeEvent; +import com.ververica.cdc.common.event.DropColumnEvent; +import com.ververica.cdc.common.event.Event; +import com.ververica.cdc.common.event.RenameColumnEvent; +import com.ververica.cdc.common.event.SchemaChangeEvent; +import org.assertj.core.api.Assertions; + +/** Collection for entries of customized event-related assertions. */ +public class EventAssertions extends Assertions { + public static EventAssert assertThat(Event event) { + return EventAssert.assertThatEvent(event); + } + + public static > + ChangeEventAssert assertThat(ChangeEvent changeEvent) { + return ChangeEventAssert.assertThatChangeEvent(changeEvent); + } + + public static DataChangeEventAssert assertThat(DataChangeEvent event) { + return DataChangeEventAssert.assertThatDataChangeEvent(event); + } + + public static > RecordDataAssert assertThat( + RecordData recordData) { + return RecordDataAssert.assertThatRecordData(recordData); + } + + public static SchemaChangeEventAssert assertThat(SchemaChangeEvent event) { + return SchemaChangeEventAssert.assertThatSchemaChangeEvent(event); + } + + public static CreateTableEventAssert assertThat(CreateTableEvent event) { + return CreateTableEventAssert.assertThatCreateTableEvent(event); + } + + public static AddColumnEventAssert assertThat(AddColumnEvent event) { + return AddColumnEventAssert.assertThatAddColumnEvent(event); + } + + public static DropColumnEventAssert assertThat(DropColumnEvent event) { + return DropColumnEventAssert.assertThatDropColumnEvent(event); + } + + public static RenameColumnEventAssert assertThat(RenameColumnEvent event) { + return RenameColumnEventAssert.assertThatRenameColumnEvent(event); + } +} diff --git a/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/RecordDataAssert.java b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/RecordDataAssert.java new file mode 100644 index 000000000..c3fc81bc7 --- /dev/null +++ b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/RecordDataAssert.java @@ -0,0 +1,47 @@ +/* + * Copyright 2023 Ververica Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ververica.cdc.common.testutils.assertions; + +import com.ververica.cdc.common.data.RecordData; +import com.ververica.cdc.common.schema.Schema; +import org.assertj.core.api.AbstractAssert; + +/** Assertions for {@link RecordData}. */ +public class RecordDataAssert> + extends AbstractAssert { + + public static > RecordDataAssert assertThatRecordData( + RecordData recordData) { + return new RecordDataAssert<>(recordData, RecordDataAssert.class); + } + + public RecordDataAssert(RecordData recordData, Class selfType) { + super(recordData, selfType); + } + + public RecordDataWithSchemaAssert withSchema(Schema schema) { + return new RecordDataWithSchemaAssert(actual, schema); + } + + public SELF hasArity(int arity) { + if (actual.getArity() != arity) { + failWithActualExpectedAndMessage( + actual.getArity(), arity, "The RecordData has unexpected arity"); + } + return myself; + } +} diff --git a/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/RecordDataWithSchemaAssert.java b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/RecordDataWithSchemaAssert.java new file mode 100644 index 000000000..388c10f79 --- /dev/null +++ b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/RecordDataWithSchemaAssert.java @@ -0,0 +1,49 @@ +/* + * Copyright 2023 Ververica Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ververica.cdc.common.testutils.assertions; + +import com.ververica.cdc.common.data.RecordData; +import com.ververica.cdc.common.schema.Schema; +import com.ververica.cdc.common.utils.SchemaUtils; +import org.assertj.core.internal.Iterables; + +import java.util.ArrayList; +import java.util.List; + +/** + * Assertions for {@link RecordData} with schema so that fields in the RecordData can be checked. + */ +public class RecordDataWithSchemaAssert extends RecordDataAssert { + private final Schema schema; + private final Iterables iterables = Iterables.instance(); + + protected RecordDataWithSchemaAssert(RecordData recordData, Schema schema) { + super(recordData, RecordDataWithSchemaAssert.class); + this.schema = schema; + } + + public RecordDataWithSchemaAssert hasFields(Object... fields) { + objects.assertNotNull(info, schema); + List fieldGetters = SchemaUtils.createFieldGetters(schema); + List actualFields = new ArrayList<>(); + for (RecordData.FieldGetter fieldGetter : fieldGetters) { + actualFields.add(fieldGetter.getFieldOrNull(actual)); + } + iterables.assertContainsExactly(info, actualFields, fields); + return myself; + } +} diff --git a/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/RenameColumnEventAssert.java b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/RenameColumnEventAssert.java new file mode 100644 index 000000000..dc1922da4 --- /dev/null +++ b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/RenameColumnEventAssert.java @@ -0,0 +1,42 @@ +/* + * Copyright 2023 Ververica Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ververica.cdc.common.testutils.assertions; + +import com.ververica.cdc.common.event.RenameColumnEvent; +import org.assertj.core.internal.Maps; + +import java.util.Map; + +/** Assertions for {@link RenameColumnEvent}. */ +public class RenameColumnEventAssert + extends ChangeEventAssert { + private final Maps maps = Maps.instance(); + + public static RenameColumnEventAssert assertThatRenameColumnEvent(RenameColumnEvent event) { + return new RenameColumnEventAssert(event); + } + + private RenameColumnEventAssert(RenameColumnEvent event) { + super(event, RenameColumnEventAssert.class); + } + + public RenameColumnEventAssert containsNameMapping(Map nameMapping) { + Map actualNameMapping = actual.getNameMapping(); + maps.assertContainsAllEntriesOf(info, actualNameMapping, nameMapping); + return myself; + } +} diff --git a/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/SchemaChangeEventAssert.java b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/SchemaChangeEventAssert.java new file mode 100644 index 000000000..38a33377e --- /dev/null +++ b/flink-cdc-common/src/test/java/com/ververica/cdc/common/testutils/assertions/SchemaChangeEventAssert.java @@ -0,0 +1,63 @@ +/* + * Copyright 2023 Ververica Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ververica.cdc.common.testutils.assertions; + +import com.ververica.cdc.common.event.AddColumnEvent; +import com.ververica.cdc.common.event.AlterColumnTypeEvent; +import com.ververica.cdc.common.event.CreateTableEvent; +import com.ververica.cdc.common.event.DropColumnEvent; +import com.ververica.cdc.common.event.RenameColumnEvent; +import com.ververica.cdc.common.event.SchemaChangeEvent; + +/** Assertions for {@link SchemaChangeEvent}. */ +public class SchemaChangeEventAssert + extends ChangeEventAssert { + + public static SchemaChangeEventAssert assertThatSchemaChangeEvent(SchemaChangeEvent event) { + return new SchemaChangeEventAssert(event); + } + + protected SchemaChangeEventAssert(SchemaChangeEvent event) { + super(event, SchemaChangeEventAssert.class); + } + + public CreateTableEventAssert asCreateTableEvent() { + isInstanceOf(CreateTableEvent.class); + return CreateTableEventAssert.assertThatCreateTableEvent((CreateTableEvent) actual); + } + + public AddColumnEventAssert asAddColumnEvent() { + isInstanceOf(AddColumnEvent.class); + return AddColumnEventAssert.assertThatAddColumnEvent((AddColumnEvent) actual); + } + + public DropColumnEventAssert asDropColumnEvent() { + isInstanceOf(DropColumnEvent.class); + return DropColumnEventAssert.assertThatDropColumnEvent((DropColumnEvent) actual); + } + + public RenameColumnEventAssert asRenameColumnEvent() { + isInstanceOf(RenameColumnEvent.class); + return RenameColumnEventAssert.assertThatRenameColumnEvent(((RenameColumnEvent) actual)); + } + + public AlterColumnTypeEventAssert asAlterColumnTypeEvent() { + isInstanceOf(AlterColumnTypeEvent.class); + return AlterColumnTypeEventAssert.assertThatAlterColumnTypeEvent( + ((AlterColumnTypeEvent) actual)); + } +}