Poorvankbhatia commented on code in PR #35: URL: https://github.com/apache/flink-connector-cassandra/pull/35#discussion_r2264891131
########## flink-connector-cassandra/src/test/java/org/apache/flink/connector/cassandra/source/reader/CassandraRowEmitterTest.java: ########## @@ -0,0 +1,289 @@ +/* + * 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.connector.cassandra.source.reader; + +import org.apache.flink.api.common.eventtime.Watermark; +import org.apache.flink.api.connector.source.SourceOutput; +import org.apache.flink.connector.cassandra.source.reader.converter.CassandraRowToTypeConverter; +import org.apache.flink.connector.cassandra.source.split.CassandraSplit; + +import com.datastax.driver.core.ExecutionInfo; +import com.datastax.driver.core.Row; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +/** Unit tests for {@link CassandraRowEmitter}. */ +class CassandraRowEmitterTest { + + @Mock private CassandraRowToTypeConverter<String> mockConverter; + @Mock private SourceOutput<String> mockOutput; + @Mock private CassandraSplit mockSplit; + @Mock private Row mockRow; + @Mock private ExecutionInfo mockExecutionInfo; + + private CassandraRowEmitter<String> emitter; + private CassandraRow cassandraRow; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + emitter = new CassandraRowEmitter<>(mockConverter); + cassandraRow = new CassandraRow(mockRow, mockExecutionInfo); + } + + @Test + void testEmitRecordSuccessfully() { + // Setup + String expectedResult = "converted-data"; + when(mockConverter.convert(cassandraRow)).thenReturn(expectedResult); + + // Execute + emitter.emitRecord(cassandraRow, mockOutput, mockSplit); + + // Verify + verify(mockConverter).convert(cassandraRow); + verify(mockOutput).collect(expectedResult); + } + + @Test + void testEmitRecordWithNullResult() { + // Setup - converter returns null + when(mockConverter.convert(cassandraRow)).thenReturn(null); + + // Execute + emitter.emitRecord(cassandraRow, mockOutput, mockSplit); + + // Verify + verify(mockConverter).convert(cassandraRow); + verify(mockOutput).collect(null); + } + + @Test + void testEmitRecordWithConversionException() { + // Setup - converter throws exception + RuntimeException expectedException = new RuntimeException("Conversion failed"); + when(mockConverter.convert(cassandraRow)).thenThrow(expectedException); + + // Execute & Verify + assertThatThrownBy(() -> emitter.emitRecord(cassandraRow, mockOutput, mockSplit)) + .isInstanceOf(RuntimeException.class) + .hasMessage("Failed to deserialize Cassandra row") + .hasCause(expectedException); + + verify(mockConverter).convert(cassandraRow); + verifyNoInteractions(mockOutput); + } + + @Test + void testEmitRecordWithDifferentTypes() { + CassandraRowToTypeConverter<Integer> intConverter = + new CassandraRowToTypeConverter<Integer>() { + @Override + public Integer convert(CassandraRow cassandraRow) { + return 42; + } + + @Override + public org.apache.flink.api.common.typeinfo.TypeInformation<Integer> + getProducedType() { + return org.apache.flink.api.common.typeinfo.TypeInformation.of( + Integer.class); + } + }; + CassandraRowEmitter<Integer> intEmitter = new CassandraRowEmitter<>(intConverter); + SourceOutput<Integer> intOutput = + new SourceOutput<Integer>() { + @Override + public void collect(Integer record) {} + + @Override + public void collect(Integer record, long timestamp) {} + + @Override + public void emitWatermark( + org.apache.flink.api.common.eventtime.Watermark watermark) {} + + @Override + public void markIdle() {} + + @Override + public void markActive() {} + }; + + // This should compile and work without issues + assertThat(intEmitter).isNotNull(); Review Comment: Damn. Thanks. Removed it. -- 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. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org