1
1
package com .snowflake .kafka .connector ;
2
2
3
- import static com .snowflake .kafka .connector .SnowflakeSinkConnectorConfig .BUFFER_COUNT_RECORDS ;
4
3
import static com .snowflake .kafka .connector .SnowflakeSinkConnectorConfig .BUFFER_FLUSH_TIME_SEC ;
4
+ import static com .snowflake .kafka .connector .SnowflakeSinkConnectorConfig .INGESTION_METHOD_OPT ;
5
5
import static com .snowflake .kafka .connector .SnowflakeSinkConnectorConfig .NAME ;
6
- import static com .snowflake .kafka .connector .SnowflakeSinkConnectorConfig .SNOWFLAKE_DATABASE ;
7
- import static com .snowflake .kafka .connector .SnowflakeSinkConnectorConfig .SNOWFLAKE_PRIVATE_KEY ;
8
- import static com .snowflake .kafka .connector .SnowflakeSinkConnectorConfig .SNOWFLAKE_SCHEMA ;
9
- import static com .snowflake .kafka .connector .SnowflakeSinkConnectorConfig .SNOWFLAKE_URL ;
10
- import static com .snowflake .kafka .connector .SnowflakeSinkConnectorConfig .SNOWFLAKE_USER ;
11
6
import static org .apache .kafka .connect .runtime .ConnectorConfig .CONNECTOR_CLASS_CONFIG ;
12
7
import static org .apache .kafka .connect .runtime .ConnectorConfig .KEY_CONVERTER_CLASS_CONFIG ;
13
8
import static org .apache .kafka .connect .runtime .ConnectorConfig .TASKS_MAX_CONFIG ;
14
9
import static org .apache .kafka .connect .runtime .ConnectorConfig .VALUE_CONVERTER_CLASS_CONFIG ;
15
10
import static org .apache .kafka .connect .sink .SinkConnector .TOPICS_CONFIG ;
16
- import static org .assertj .core .api .Assertions .assertThat ;
17
- import static org .awaitility .Awaitility .await ;
18
11
19
- import com .snowflake .kafka .connector .fake .SnowflakeFakeSinkConnector ;
20
- import com .snowflake .kafka .connector .fake .SnowflakeFakeSinkTask ;
21
- import java .time .Duration ;
22
- import java .util .HashMap ;
23
- import java .util .List ;
12
+ import com .snowflake .kafka .connector .internal .TestUtils ;
13
+ import com .snowflake .kafka .connector .internal .streaming .FakeStreamingClientHandler ;
14
+ import com .snowflake .kafka .connector .internal .streaming .IngestionMethodConfig ;
15
+ import com .snowflake .kafka .connector .internal .streaming .StreamingClientProvider ;
24
16
import java .util .Map ;
25
- import org .apache .kafka .connect .runtime .AbstractStatus ;
26
- import org .apache .kafka .connect .runtime .rest .entities .ConnectorStateInfo ;
27
- import org .apache .kafka .connect .sink .SinkRecord ;
28
17
import org .apache .kafka .connect .storage .StringConverter ;
29
18
import org .apache .kafka .connect .util .clusters .EmbeddedConnectCluster ;
30
19
import org .junit .jupiter .api .AfterAll ;
31
20
import org .junit .jupiter .api .AfterEach ;
32
21
import org .junit .jupiter .api .BeforeAll ;
33
22
import org .junit .jupiter .api .BeforeEach ;
34
- import org .junit .jupiter .api .Test ;
35
23
import org .junit .jupiter .api .TestInstance ;
36
24
37
25
@ TestInstance (TestInstance .Lifecycle .PER_CLASS )
38
- public class ConnectClusterBaseIT {
26
+ class ConnectClusterBaseIT {
39
27
40
- protected EmbeddedConnectCluster connectCluster ;
28
+ EmbeddedConnectCluster connectCluster ;
41
29
42
- protected static final String TEST_TOPIC = "kafka-int-test" ;
43
- protected static final String TEST_CONNECTOR_NAME = "test-connector" ;
44
- protected static final Integer TASK_NUMBER = 1 ;
45
- private static final Duration CONNECTOR_MAX_STARTUP_TIME = Duration .ofSeconds (20 );
30
+ FakeStreamingClientHandler fakeStreamingClientHandler ;
31
+
32
+ static final Integer TASK_NUMBER = 1 ;
46
33
47
34
@ BeforeAll
48
35
public void beforeAll () {
@@ -52,14 +39,18 @@ public void beforeAll() {
52
39
.numWorkers (3 )
53
40
.build ();
54
41
connectCluster .start ();
55
- connectCluster .kafka ().createTopic (TEST_TOPIC );
56
- connectCluster .configureConnector (TEST_CONNECTOR_NAME , createProperties ());
57
- await ().timeout (CONNECTOR_MAX_STARTUP_TIME ).until (this ::isConnectorRunning );
58
42
}
59
43
60
44
@ BeforeEach
61
- public void before () {
62
- SnowflakeFakeSinkTask .resetRecords ();
45
+ public void beforeEach () {
46
+ StreamingClientProvider .reset ();
47
+ fakeStreamingClientHandler = new FakeStreamingClientHandler ();
48
+ StreamingClientProvider .overrideStreamingClientHandler (fakeStreamingClientHandler );
49
+ }
50
+
51
+ @ AfterEach
52
+ public void afterEach () {
53
+ StreamingClientProvider .reset ();
63
54
}
64
55
65
56
@ AfterAll
@@ -70,55 +61,30 @@ public void afterAll() {
70
61
}
71
62
}
72
63
73
- @ AfterEach
74
- public void after () {
75
- SnowflakeFakeSinkTask .resetRecords ();
76
- }
77
-
78
- @ Test
79
- public void connectorShouldConsumeMessagesFromTopic () {
80
- connectCluster .kafka ().produce (TEST_TOPIC , "test1" );
81
- connectCluster .kafka ().produce (TEST_TOPIC , "test2" );
64
+ final Map <String , String > defaultProperties (String topicName , String connectorName ) {
65
+ Map <String , String > config = TestUtils .getConf ();
82
66
83
- await ()
84
- .untilAsserted (
85
- () -> {
86
- List <SinkRecord > records = SnowflakeFakeSinkTask .getRecords ();
87
- assertThat (records ).hasSize (2 );
88
- assertThat (records .stream ().map (SinkRecord ::value )).containsExactly ("test1" , "test2" );
89
- });
90
- }
91
-
92
- protected Map <String , String > createProperties () {
93
- Map <String , String > config = new HashMap <>();
94
-
95
- // kafka connect specific
96
- // real connector will be specified with SNOW-1055561
97
- config .put (CONNECTOR_CLASS_CONFIG , SnowflakeFakeSinkConnector .class .getName ());
98
- config .put (TOPICS_CONFIG , TEST_TOPIC );
67
+ config .put (CONNECTOR_CLASS_CONFIG , SnowflakeSinkConnector .class .getName ());
68
+ config .put (NAME , connectorName );
69
+ config .put (TOPICS_CONFIG , topicName );
70
+ config .put (INGESTION_METHOD_OPT , IngestionMethodConfig .SNOWPIPE_STREAMING .toString ());
71
+ config .put (Utils .SF_ROLE , "testrole_kafka" );
72
+ config .put (BUFFER_FLUSH_TIME_SEC , "1" );
99
73
config .put (TASKS_MAX_CONFIG , TASK_NUMBER .toString ());
100
74
config .put (KEY_CONVERTER_CLASS_CONFIG , StringConverter .class .getName ());
101
75
config .put (VALUE_CONVERTER_CLASS_CONFIG , StringConverter .class .getName ());
102
76
103
- // kafka push specific
104
- config .put (NAME , TEST_CONNECTOR_NAME );
105
- config .put (SNOWFLAKE_URL , "https://test.testregion.snowflakecomputing.com:443" );
106
- config .put (SNOWFLAKE_USER , "testName" );
107
- config .put (SNOWFLAKE_PRIVATE_KEY , "testPrivateKey" );
108
- config .put (SNOWFLAKE_DATABASE , "testDbName" );
109
- config .put (SNOWFLAKE_SCHEMA , "testSchema" );
110
- config .put (BUFFER_COUNT_RECORDS , "1000000" );
111
- config .put (BUFFER_FLUSH_TIME_SEC , "1" );
112
-
113
77
return config ;
114
78
}
115
79
116
- private boolean isConnectorRunning () {
117
- ConnectorStateInfo status = connectCluster .connectorStatus (TEST_CONNECTOR_NAME );
118
- return status != null
119
- && status .connector ().state ().equals (AbstractStatus .State .RUNNING .toString ())
120
- && status .tasks ().size () >= TASK_NUMBER
121
- && status .tasks ().stream ()
122
- .allMatch (state -> state .state ().equals (AbstractStatus .State .RUNNING .toString ()));
80
+ final void waitForConnectorRunning (String connectorName ) {
81
+ try {
82
+ connectCluster
83
+ .assertions ()
84
+ .assertConnectorAndAtLeastNumTasksAreRunning (
85
+ connectorName , 1 , "The connector did not start." );
86
+ } catch (InterruptedException e ) {
87
+ throw new IllegalStateException ("The connector is not running" );
88
+ }
123
89
}
124
90
}
0 commit comments