twalthr commented on a change in pull request #13770: URL: https://github.com/apache/flink/pull/13770#discussion_r517378013
########## File path: flink-connectors/flink-connector-kinesis/src/test/java/org/apache/flink/streaming/connectors/kinesis/table/KinesisDynamicTableFactoryTest.java ########## @@ -0,0 +1,358 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.flink.streaming.connectors.kinesis.table; + +import org.apache.flink.configuration.Configuration; +import org.apache.flink.streaming.api.functions.sink.SinkFunction; +import org.apache.flink.streaming.api.functions.source.SourceFunction; +import org.apache.flink.streaming.connectors.kinesis.FlinkKinesisConsumer; +import org.apache.flink.streaming.connectors.kinesis.FlinkKinesisProducer; +import org.apache.flink.streaming.connectors.kinesis.RandomKinesisPartitioner; +import org.apache.flink.table.api.DataTypes; +import org.apache.flink.table.api.TableColumn; +import org.apache.flink.table.api.TableSchema; +import org.apache.flink.table.api.ValidationException; +import org.apache.flink.table.catalog.CatalogTable; +import org.apache.flink.table.catalog.CatalogTableImpl; +import org.apache.flink.table.catalog.ObjectIdentifier; +import org.apache.flink.table.connector.sink.DynamicTableSink; +import org.apache.flink.table.connector.sink.SinkFunctionProvider; +import org.apache.flink.table.connector.source.ScanTableSource; +import org.apache.flink.table.connector.source.SourceFunctionProvider; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.factories.FactoryUtil; +import org.apache.flink.table.factories.TableOptionsBuilder; +import org.apache.flink.table.factories.TestFormatFactory; +import org.apache.flink.table.runtime.connector.sink.SinkRuntimeProviderContext; +import org.apache.flink.table.runtime.connector.source.ScanRuntimeProviderContext; +import org.apache.flink.table.types.DataType; +import org.apache.flink.util.TestLogger; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import static org.apache.flink.core.testutils.FlinkMatchers.containsCause; +import static org.apache.flink.streaming.connectors.kinesis.table.RowDataKinesisDeserializationSchema.Metadata; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +/** + * Test for {@link KinesisDynamicSource} and {@link KinesisDynamicSink} created + * by {@link KinesisDynamicTableFactory}. + */ +public class KinesisDynamicTableFactoryTest extends TestLogger { + + private static final String SOURCE_STREAM = "sourceStream"; + private static final String SINK_STREAM = "targetStream"; + + private static final String SOURCE_TABLE = "sourceTable"; + private static final String SINK_TABLE = "sinkTable"; + + private static final Properties KINESIS_PROPERTIES = new Properties() {{ + setProperty("aws.region", "us-west-2"); + }}; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + // -------------------------------------------------------------------------------------------- + // Positive tests + // -------------------------------------------------------------------------------------------- + + @Test + public void testGoodTableSource() { + TableSchema sourceSchema = defaultSourceSchema().build(); + Map<String, String> sourceOptions = defaultSourceOptions().build(); + + // Construct actual DynamicTableSource using FactoryUtil + KinesisDynamicSource actualSource = (KinesisDynamicSource) + FactoryUtil.createTableSource( + null, + ObjectIdentifier.of("default", "default", SOURCE_TABLE), + createSourceTable(sourceSchema, sourceOptions, Collections.emptyList()), + new Configuration(), + Thread.currentThread().getContextClassLoader(), + false); + + // Construct expected DynamicTableSink using factory under test + KinesisDynamicSource expectedSource = new KinesisDynamicSource( + sourceSchema.toPhysicalRowDataType(), + SOURCE_STREAM, + KINESIS_PROPERTIES, + new TestFormatFactory.DecodingFormatMock(",", true)); + + // verify that the constructed DynamicTableSink is as expected + assertEquals(expectedSource, actualSource); + + // verify that the copy of the constructed DynamicTableSink is as expected + assertEquals(expectedSource, actualSource.copy()); + + // verify produced sink + ScanTableSource.ScanRuntimeProvider functionProvider = + actualSource.getScanRuntimeProvider(ScanRuntimeProviderContext.INSTANCE); + SourceFunction<RowData> sourceFunction = + as(functionProvider, SourceFunctionProvider.class).createSourceFunction(); + assertThat(sourceFunction, instanceOf(FlinkKinesisConsumer.class)); + } + + @Test + public void testGoodTableSourceWithMetadataFields() { + TableSchema sourceSchema = defaultSourceSchema().build(); + Map<String, String> sourceOptions = defaultSourceOptions().build(); + + // Construct actual DynamicTableSource using FactoryUtil + KinesisDynamicSource actualSource = (KinesisDynamicSource) + FactoryUtil.createTableSource( + null, + ObjectIdentifier.of("default", "default", SOURCE_TABLE), + createSourceTable(sourceSchema, sourceOptions, Collections.emptyList()), + new Configuration(), + Thread.currentThread().getContextClassLoader(), + false); + + // Construct expected DynamicTableSink using factory under test + KinesisDynamicSource expectedSource = new KinesisDynamicSource( + sourceSchema.toPhysicalRowDataType(), + SOURCE_STREAM, + KINESIS_PROPERTIES, + new TestFormatFactory.DecodingFormatMock(",", true)); + + List<String> metadataKeys = Arrays.asList("ShardId", "ApproximateArrivalTimestamp"); + DataType producedDataType = getProducedType(sourceSchema, Metadata.values()); + + expectedSource.applyReadableMetadata(metadataKeys, producedDataType); Review comment: We had this discussions before. Personally, I was also more in favor of immutable interfaces. But after lots of online and offline discussion we decided for mutability by considering `DynamicTableSource` and `DynamicTableSink` as another level of factories or builders that the planner uses to get the runtime implementation in the end. Regarding this test, the `KinesisDynamicSource::equals` is correct but we should rather test the contents of the member variables instead of comparing the result of performing the same computation twice. Btw I wouldn't say that the current implementation goes against the coding guidelines. The mutable attributes must be included in the hashCode/equals and it might be the case that the planner is using them. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
