3030import io .cdap .delta .plugin .mock .BlockingEventEmitter ;
3131import io .cdap .delta .plugin .mock .MockContext ;
3232import io .cdap .delta .plugin .mock .MockEventEmitter ;
33+ import org .junit .AfterClass ;
3334import org .junit .Assert ;
3435import org .junit .BeforeClass ;
3536import org .junit .Test ;
4142import java .sql .Date ;
4243import java .sql .DriverManager ;
4344import java .sql .PreparedStatement ;
45+ import java .sql .SQLException ;
4446import java .sql .Statement ;
4547import java .time .LocalDate ;
4648import java .util .Collections ;
5759 * are some classloading issues due to copied debezium classes.
5860 */
5961public class MySqlEventReaderIntegrationTest {
60- private static final String DB = "test" ;
62+ private static final int CONSUMER_ID = 13 ;
6163 private static final String CUSTOMERS_TABLE = "customers" ;
6264 private static final Schema CUSTOMERS_SCHEMA = Schema .recordOf (
6365 "customers" ,
@@ -69,9 +71,14 @@ public class MySqlEventReaderIntegrationTest {
6971 BINARYCOL_TABLE ,
7072 Schema .Field .of ("id" , Schema .of (Schema .Type .INT )),
7173 Schema .Field .of ("bincol" , Schema .of (Schema .Type .STRING )));
74+ private static final String HOST = "localhost" ;
75+ private static final String DB = "test" ;
76+ private static final String USER = "root" ;
7277
7378 private static String password ;
7479 private static int port ;
80+ private static Properties connProperties ;
81+ private static String connectionUrl ;
7582
7683
7784 @ BeforeClass
@@ -84,10 +91,10 @@ public static void setupClass() throws Exception {
8491 }
8592 port = Integer .parseInt (properties .getProperty ("mysql.port" ));
8693
87- Properties connProperties = new Properties ();
88- connProperties .put ("user" , "root" );
94+ connProperties = new Properties ();
95+ connProperties .put ("user" , USER );
8996 connProperties .put ("password" , password );
90- String connectionUrl = String .format ("jdbc:mysql://localhost:%d" , port );
97+ connectionUrl = String .format ("jdbc:mysql://localhost:%d" , port );
9198 DriverManager .getDriver (connectionUrl );
9299
93100 // wait until a connection can be established
@@ -146,14 +153,24 @@ public static void setupClass() throws Exception {
146153 }
147154 }
148155
156+ @ AfterClass
157+ public static void tearDown () throws SQLException {
158+ // drop database
159+ try (Connection connection = DriverManager .getConnection (connectionUrl , connProperties )) {
160+ try (Statement statement = connection .createStatement ()) {
161+ statement .execute ("DROP DATABASE " + DB );
162+ }
163+ }
164+ }
165+
149166 @ Test
150167 public void test () throws InterruptedException {
151168 SourceTable sourceTable = new SourceTable (DB , CUSTOMERS_TABLE , null ,
152169 Collections .emptySet (), Collections .emptySet (), Collections .emptySet ());
153170
154171 DeltaSourceContext context = new MockContext (Driver .class );
155- MockEventEmitter eventEmitter = new MockEventEmitter (6 );
156- MySqlConfig config = new MySqlConfig ("localhost" , port , "root" , password , 13 , DB ,
172+ MockEventEmitter eventEmitter = new MockEventEmitter (7 );
173+ MySqlConfig config = new MySqlConfig (HOST , port , USER , password , CONSUMER_ID , DB ,
157174 TimeZone .getDefault ().getID ());
158175
159176 MySqlEventReader eventReader = new MySqlEventReader (Collections .singleton (sourceTable ), config ,
@@ -164,7 +181,7 @@ public void test() throws InterruptedException {
164181 eventEmitter .waitForExpectedEvents (30 , TimeUnit .SECONDS );
165182
166183 Assert .assertEquals (4 , eventEmitter .getDdlEvents ().size ());
167- Assert .assertEquals (2 , eventEmitter .getDmlEvents ().size ());
184+ Assert .assertEquals (3 , eventEmitter .getDmlEvents ().size ());
168185
169186 DDLEvent ddlEvent = eventEmitter .getDdlEvents ().get (0 );
170187 Assert .assertEquals (DDLOperation .Type .DROP_TABLE , ddlEvent .getOperation ().getType ());
@@ -184,14 +201,20 @@ public void test() throws InterruptedException {
184201 Assert .assertEquals (DB , ddlEvent .getOperation ().getDatabaseName ());
185202 Assert .assertEquals (CUSTOMERS_TABLE , ddlEvent .getOperation ().getTableName ());
186203 Assert .assertEquals (Collections .singletonList ("id" ), ddlEvent .getPrimaryKey ());
204+
187205 Assert .assertEquals (CUSTOMERS_SCHEMA , ddlEvent .getSchema ());
188206
189207 DMLEvent dmlEvent = eventEmitter .getDmlEvents ().get (0 );
190208 Assert .assertEquals (DMLOperation .Type .INSERT , dmlEvent .getOperation ().getType ());
191209 Assert .assertEquals (DB , dmlEvent .getOperation ().getDatabaseName ());
192210 Assert .assertEquals (CUSTOMERS_TABLE , dmlEvent .getOperation ().getTableName ());
193211 StructuredRecord row = dmlEvent .getRow ();
194- StructuredRecord expected = StructuredRecord .builder (CUSTOMERS_SCHEMA )
212+
213+ // Take schema name from the row as it is generated by debezium
214+ // so it can be different from the one in our schema but does not impact data
215+ Schema expectedSchema = Schema .recordOf (row .getSchema ().getRecordName (), CUSTOMERS_SCHEMA .getFields ());
216+
217+ StructuredRecord expected = StructuredRecord .builder (expectedSchema )
195218 .set ("id" , 0 )
196219 .set ("name" , "alice" )
197220 .setDate ("bday" , LocalDate .ofEpochDay (0 ))
@@ -203,7 +226,7 @@ public void test() throws InterruptedException {
203226 Assert .assertEquals (DB , dmlEvent .getOperation ().getDatabaseName ());
204227 Assert .assertEquals (CUSTOMERS_TABLE , dmlEvent .getOperation ().getTableName ());
205228 row = dmlEvent .getRow ();
206- expected = StructuredRecord .builder (CUSTOMERS_SCHEMA )
229+ expected = StructuredRecord .builder (expectedSchema )
207230 .set ("id" , 1 )
208231 .set ("name" , "bob" )
209232 .setDate ("bday" , LocalDate .ofEpochDay (365 ))
@@ -215,7 +238,7 @@ public void test() throws InterruptedException {
215238 Assert .assertEquals (DB , dmlEvent .getOperation ().getDatabaseName ());
216239 Assert .assertEquals (CUSTOMERS_TABLE , dmlEvent .getOperation ().getTableName ());
217240 row = dmlEvent .getRow ();
218- expected = StructuredRecord .builder (CUSTOMERS_SCHEMA )
241+ expected = StructuredRecord .builder (expectedSchema )
219242 .set ("id" , 2 )
220243 .set ("name" , "tim" )
221244 .setDate ("bday" , null )
@@ -232,7 +255,7 @@ public void stopReaderTest() throws Exception {
232255 BlockingQueue <DDLEvent > ddlEvents = new ArrayBlockingQueue <>(1 );
233256 BlockingQueue <DMLEvent > dmlEvents = new ArrayBlockingQueue <>(1 );
234257 EventEmitter eventEmitter = new BlockingEventEmitter (ddlEvents , dmlEvents );
235- MySqlConfig config = new MySqlConfig ("localhost" , port , "root" , password , 13 , DB ,
258+ MySqlConfig config = new MySqlConfig (HOST , port , USER , password , CONSUMER_ID , DB ,
236259 TimeZone .getDefault ().getID ());
237260
238261 MySqlEventReader eventReader = new MySqlEventReader (Collections .singleton (sourceTable ), config ,
@@ -264,7 +287,7 @@ public void testBinaryHandlingModebyDebezium() throws InterruptedException {
264287 context .addRuntimeArgument (MySqlEventReader .SOURCE_CONNECTOR_PREFIX + "binary.handling.mode" , "HEX" );
265288
266289 MockEventEmitter eventEmitter = new MockEventEmitter (4 );
267- MySqlConfig config = new MySqlConfig ("localhost" , port , "root" , password , 13 , DB ,
290+ MySqlConfig config = new MySqlConfig (HOST , port , USER , password , CONSUMER_ID , DB ,
268291 TimeZone .getDefault ().getID ());
269292
270293 MySqlEventReader eventReader = new MySqlEventReader (Collections .singleton (sourceTable ), config ,
0 commit comments