1818
1919package org .apache .seatunnel .connectors .seatunnel .tdengine .sink ;
2020
21+ import org .apache .seatunnel .shade .com .typesafe .config .Config ;
22+ import org .apache .seatunnel .shade .com .typesafe .config .ConfigFactory ;
23+ import org .apache .seatunnel .shade .com .typesafe .config .ConfigValueFactory ;
2124
22- import lombok .SneakyThrows ;
2325import org .apache .seatunnel .api .table .type .BasicType ;
2426import org .apache .seatunnel .api .table .type .SeaTunnelDataType ;
2527import org .apache .seatunnel .api .table .type .SeaTunnelRowType ;
26- import org .apache .seatunnel .shade .com .typesafe .config .Config ;
27- import org .apache .seatunnel .shade .com .typesafe .config .ConfigFactory ;
28- import org .apache .seatunnel .shade .com .typesafe .config .ConfigValueFactory ;
28+
2929import org .junit .jupiter .api .BeforeEach ;
3030import org .junit .jupiter .api .Test ;
31- import java .lang .reflect .InvocationTargetException ;
32- import java .time .LocalDateTime ;
33-
3431import org .mockito .MockedStatic ;
3532import org .mockito .Mockito ;
33+
34+ import lombok .SneakyThrows ;
35+
3636import java .sql .Connection ;
3737import java .sql .DriverManager ;
3838import java .sql .ResultSet ;
3939import java .sql .Statement ;
40+ import java .time .LocalDateTime ;
4041import java .util .TimeZone ;
4142
42- import static org .junit .jupiter .api .Assertions .*;
43- import static org .mockito .ArgumentMatchers .anyString ;
44- import static org .mockito .Mockito .mockStatic ;
45- import static org .mockito .Mockito .when ;
43+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
4644
47- public class TDengineSinkWriterTest {
45+ static class TDengineSinkWriterTest {
4846 TDengineSinkWriter writer ;
4947
5048 @ SneakyThrows
@@ -53,18 +51,19 @@ public void setup() {
5351 SeaTunnelRowType rowType ;
5452 Config config ;
5553 TimeZone .setDefault (TimeZone .getTimeZone ("UTC" ));
56- String [] fieldNames = new String []{"id" , "name" , "description" , "weight" };
54+ String [] fieldNames = new String [] {"id" , "name" , "description" , "weight" };
5755 SeaTunnelDataType <?>[] dataTypes =
58- new SeaTunnelDataType []{
59- BasicType .LONG_TYPE ,
60- BasicType .STRING_TYPE ,
61- BasicType .STRING_TYPE ,
62- BasicType .STRING_TYPE ,
56+ new SeaTunnelDataType [] {
57+ BasicType .LONG_TYPE ,
58+ BasicType .STRING_TYPE ,
59+ BasicType .STRING_TYPE ,
60+ BasicType .STRING_TYPE ,
6361 };
6462 rowType = new SeaTunnelRowType (fieldNames , dataTypes );
6563 config =
6664 ConfigFactory .empty ()
67- .withValue ("url" , ConfigValueFactory .fromAnyRef ("jdbc:TAOS://localhost:6030/" ))
65+ .withValue (
66+ "url" , ConfigValueFactory .fromAnyRef ("jdbc:TAOS://localhost:6030/" ))
6867 .withValue ("database" , ConfigValueFactory .fromAnyRef ("test_db" ))
6968 .withValue ("stable" , ConfigValueFactory .fromAnyRef ("test_stable" ))
7069 .withValue ("username" , ConfigValueFactory .fromAnyRef ("root" ))
@@ -77,41 +76,44 @@ public void setup() {
7776 ResultSet mockResultSet = Mockito .mock (ResultSet .class );
7877
7978 // Mock ResultSet behavior
80- when (mockResultSet .next ()).thenReturn (true , false ); // First call returns true, second call returns false
81- when (mockResultSet .getString ("note" )).thenReturn ("TAG" );
79+ Mockito .when (mockResultSet .next ())
80+ .thenReturn (true , false ); // First call returns true, second call returns false
81+ Mockito .when (mockResultSet .getString ("note" )).thenReturn ("TAG" );
8282
8383 // Mock Statement behavior
84- when (mockStatement .executeQuery ("desc test_db.test_stable" )).thenReturn (mockResultSet );
84+ Mockito .when (mockStatement .executeQuery ("desc test_db.test_stable" ))
85+ .thenReturn (mockResultSet );
8586
8687 // Mock Connection behavior
87- when (mockConnection .createStatement ()).thenReturn (mockStatement );
88+ Mockito . when (mockConnection .createStatement ()).thenReturn (mockStatement );
8889
89- // Mock DriverManager behavior
90- try ( MockedStatic < DriverManager > mockedStatic = mockStatic (DriverManager .class )) {
91- mockedStatic . when (() -> DriverManager . getConnection ( anyString ())) .thenReturn (mockConnection );
90+ try ( MockedStatic < DriverManager > mockedStatic = Mockito . mockStatic ( DriverManager . class )) {
91+ Mockito . when (DriverManager .getConnection ( Mockito . anyString ()))
92+ .thenReturn (mockConnection );
9293 writer = new TDengineSinkWriter (config , rowType );
9394 }
9495 }
9596
9697 @ Test
97- void testConvertDataTypeWithNull (){
98+ void testConvertDataTypeWithNull () {
9899 // Prepare test data
99100 LocalDateTime dateTime = LocalDateTime .of (2023 , 4 , 14 , 15 , 30 , 45 ); // 2023-04-14 15:30:45
100101 Object [] input = {
101- null , // Test for null value
102- dateTime , // Test for LocalDateTime
103- "test_string" , // Test for String
104- 123 , // Test for other types (Integer)
105- 45.67 // Test for other types (Double)
102+ null , // Test for null value
103+ dateTime , // Test for LocalDateTime
104+ "test_string" , // Test for String
105+ 123 , // Test for other types (Integer)
106+ 45.67 // Test for other types (Double)
106107 };
107108
108109 // Expected output
109110 Object [] expectedOutput = {
110- null , // null remains unchanged
111- "'2023-04-14 15:30:45.000'" , // LocalDateTime is converted to a formatted string with the specified timezone
112- "'test_string'" , // String is wrapped in single quotes
113- 123 , // Integer remains unchanged
114- 45.67 // Double remains unchanged
111+ null , // null remains unchanged
112+ "'2023-04-14 15:30:45.000'" , // LocalDateTime is converted to a formatted string with
113+ // the specified timezone
114+ "'test_string'" , // String is wrapped in single quotes
115+ 123 , // Integer remains unchanged
116+ 45.67 // Double remains unchanged
115117 };
116118
117119 Object [] result = writer .convertDataType (input );
@@ -122,12 +124,14 @@ void testConvertDataTypeWithNull(){
122124 Object [] input1 = {};
123125 Object [] expectedOutput1 = {};
124126 Object [] result1 = writer .convertDataType (input1 );
125- assertArrayEquals (expectedOutput1 , result1 , "Empty input array should return an empty output array." );
127+ assertArrayEquals (
128+ expectedOutput1 , result1 , "Empty input array should return an empty output array." );
126129
127130 // Test for an array containing only null
128131 Object [] input2 = {null };
129132 Object [] expectedOutput2 = {null };
130133 Object [] result2 = writer .convertDataType (input2 );
131- assertArrayEquals (expectedOutput2 , result2 , "Array with only null should return an array with null." );
134+ assertArrayEquals (
135+ expectedOutput2 , result2 , "Array with only null should return an array with null." );
132136 }
133137}
0 commit comments