77
88import com .github .shyiko .mysql .binlog .BinaryLogClient ;
99import org .junit .jupiter .api .BeforeEach ;
10+ import org .junit .jupiter .api .Nested ;
1011import org .junit .jupiter .api .Test ;
1112import org .junit .jupiter .api .extension .ExtendWith ;
1213import org .mockito .Mock ;
1516import org .opensearch .dataprepper .model .source .coordinator .enhanced .EnhancedSourceCoordinator ;
1617import org .opensearch .dataprepper .plugins .source .rds .coordination .partition .StreamPartition ;
1718import org .opensearch .dataprepper .plugins .source .rds .coordination .state .MySqlStreamState ;
19+ import org .opensearch .dataprepper .plugins .source .rds .coordination .state .PostgresStreamState ;
1820import org .opensearch .dataprepper .plugins .source .rds .coordination .state .StreamProgressState ;
1921import org .opensearch .dataprepper .plugins .source .rds .model .BinlogCoordinate ;
22+ import org .postgresql .replication .LogSequenceNumber ;
2023
2124import java .io .IOException ;
2225import java .util .Optional ;
2326import java .util .UUID ;
2427
28+ import static org .mockito .ArgumentMatchers .any ;
2529import static org .mockito .Mockito .mock ;
2630import static org .mockito .Mockito .never ;
2731import static org .mockito .Mockito .verify ;
@@ -33,12 +37,6 @@ class StreamWorkerTest {
3337 @ Mock
3438 private EnhancedSourceCoordinator sourceCoordinator ;
3539
36- @ Mock
37- private BinlogClientWrapper binlogClientWrapper ;
38-
39- @ Mock
40- private BinaryLogClient binaryLogClient ;
41-
4240 @ Mock
4341 private PluginMetrics pluginMetrics ;
4442
@@ -47,55 +45,119 @@ class StreamWorkerTest {
4745
4846 private StreamWorker streamWorker ;
4947
50- @ BeforeEach
51- void setUp () {
52- streamWorker = createObjectUnderTest ();
53- }
54-
55- @ Test
56- void test_processStream_with_given_binlog_coordinates () throws IOException {
57- final StreamProgressState streamProgressState = mock (StreamProgressState .class );
58- final MySqlStreamState mySqlStreamState = mock (MySqlStreamState .class );
59- final String binlogFilename = UUID .randomUUID ().toString ();
60- final long binlogPosition = 100L ;
61- when (streamPartition .getProgressState ()).thenReturn (Optional .of (streamProgressState ));
62- when (streamProgressState .getMySqlStreamState ()).thenReturn (mySqlStreamState );
63- when (mySqlStreamState .getCurrentPosition ()).thenReturn (new BinlogCoordinate (binlogFilename , binlogPosition ));
64- when (streamProgressState .shouldWaitForExport ()).thenReturn (false );
65- when (binlogClientWrapper .getBinlogClient ()).thenReturn (binaryLogClient );
66-
67- streamWorker .processStream (streamPartition );
68-
69- verify (binaryLogClient ).setBinlogFilename (binlogFilename );
70- verify (binaryLogClient ).setBinlogPosition (binlogPosition );
71- verify (binlogClientWrapper ).connect ();
72- }
48+ @ Nested
49+ class TestForMySql {
50+ @ Mock
51+ private BinlogClientWrapper binlogClientWrapper ;
52+
53+ @ Mock
54+ private BinaryLogClient binaryLogClient ;
55+
56+ @ BeforeEach
57+ void setUp () {
58+ streamWorker = createObjectUnderTest ();
59+ }
60+
61+ @ Test
62+ void test_processStream_with_given_binlog_coordinates () throws IOException {
63+ final StreamProgressState streamProgressState = mock (StreamProgressState .class );
64+ final MySqlStreamState mySqlStreamState = mock (MySqlStreamState .class );
65+ final String binlogFilename = UUID .randomUUID ().toString ();
66+ final long binlogPosition = 100L ;
67+ when (streamPartition .getProgressState ()).thenReturn (Optional .of (streamProgressState ));
68+ when (streamProgressState .getMySqlStreamState ()).thenReturn (mySqlStreamState );
69+ when (mySqlStreamState .getCurrentPosition ()).thenReturn (new BinlogCoordinate (binlogFilename , binlogPosition ));
70+ when (streamProgressState .shouldWaitForExport ()).thenReturn (false );
71+ when (binlogClientWrapper .getBinlogClient ()).thenReturn (binaryLogClient );
72+
73+ streamWorker .processStream (streamPartition );
74+
75+ verify (binaryLogClient ).setBinlogFilename (binlogFilename );
76+ verify (binaryLogClient ).setBinlogPosition (binlogPosition );
77+ verify (binlogClientWrapper ).connect ();
78+ }
79+
80+ @ Test
81+ void test_processStream_without_current_binlog_coordinates () throws IOException {
82+ final StreamProgressState streamProgressState = mock (StreamProgressState .class );
83+ final MySqlStreamState mySqlStreamState = mock (MySqlStreamState .class );
84+ when (streamPartition .getProgressState ()).thenReturn (Optional .of (streamProgressState ));
85+ final String binlogFilename = "binlog-001" ;
86+ final long binlogPosition = 100L ;
87+ when (streamProgressState .getMySqlStreamState ()).thenReturn (mySqlStreamState );
88+ when (mySqlStreamState .getCurrentPosition ()).thenReturn (null );
89+ when (streamProgressState .shouldWaitForExport ()).thenReturn (false );
90+ when (binlogClientWrapper .getBinlogClient ()).thenReturn (binaryLogClient );
91+
92+ streamWorker .processStream (streamPartition );
93+
94+ verify (binaryLogClient , never ()).setBinlogFilename (binlogFilename );
95+ verify (binaryLogClient , never ()).setBinlogPosition (binlogPosition );
96+ verify (binlogClientWrapper ).connect ();
97+ }
98+
99+ @ Test
100+ void test_shutdown () throws IOException {
101+ streamWorker .shutdown ();
102+ verify (binlogClientWrapper ).disconnect ();
103+ }
104+
105+ private StreamWorker createObjectUnderTest () {
106+ return new StreamWorker (sourceCoordinator , binlogClientWrapper , pluginMetrics );
107+ }
73108
74- @ Test
75- void test_processStream_without_current_binlog_coordinates () throws IOException {
76- final StreamProgressState streamProgressState = mock (StreamProgressState .class );
77- final MySqlStreamState mySqlStreamState = mock (MySqlStreamState .class );
78- when (streamPartition .getProgressState ()).thenReturn (Optional .of (streamProgressState ));
79- final String binlogFilename = "binlog-001" ;
80- final long binlogPosition = 100L ;
81- when (streamProgressState .getMySqlStreamState ()).thenReturn (mySqlStreamState );
82- when (mySqlStreamState .getCurrentPosition ()).thenReturn (null );
83- when (streamProgressState .shouldWaitForExport ()).thenReturn (false );
84-
85- streamWorker .processStream (streamPartition );
86-
87- verify (binaryLogClient , never ()).setBinlogFilename (binlogFilename );
88- verify (binaryLogClient , never ()).setBinlogPosition (binlogPosition );
89- verify (binlogClientWrapper ).connect ();
90109 }
91110
92- @ Test
93- void test_shutdown () throws IOException {
94- streamWorker .shutdown ();
95- verify (binlogClientWrapper ).disconnect ();
96- }
111+ @ Nested
112+ class TestForPostgres {
113+ @ Mock
114+ private LogicalReplicationClient logicalReplicationClient ;
115+
116+ @ BeforeEach
117+ void setUp () {
118+ streamWorker = createObjectUnderTest ();
119+ }
120+
121+ @ Test
122+ void test_processStream_with_given_currentLsn () {
123+ final StreamProgressState streamProgressState = mock (StreamProgressState .class );
124+ final PostgresStreamState postgresStreamState = mock (PostgresStreamState .class );
125+ final String currentLsn = UUID .randomUUID ().toString ();
126+ when (streamPartition .getProgressState ()).thenReturn (Optional .of (streamProgressState ));
127+ when (streamProgressState .getPostgresStreamState ()).thenReturn (postgresStreamState );
128+ when (postgresStreamState .getCurrentLsn ()).thenReturn (currentLsn );
129+ when (streamProgressState .shouldWaitForExport ()).thenReturn (false );
130+
131+ streamWorker .processStream (streamPartition );
132+
133+ verify (logicalReplicationClient ).setStartLsn (LogSequenceNumber .valueOf (currentLsn ));
134+ verify (logicalReplicationClient ).connect ();
135+ }
136+
137+ @ Test
138+ void test_processStream_without_currentLsn () {
139+ final StreamProgressState streamProgressState = mock (StreamProgressState .class );
140+ final PostgresStreamState postgresStreamState = mock (PostgresStreamState .class );
141+ when (streamPartition .getProgressState ()).thenReturn (Optional .of (streamProgressState ));
142+ when (streamProgressState .getPostgresStreamState ()).thenReturn (postgresStreamState );
143+ when (postgresStreamState .getCurrentLsn ()).thenReturn (null );
144+ when (streamProgressState .shouldWaitForExport ()).thenReturn (false );
145+
146+ streamWorker .processStream (streamPartition );
147+
148+ verify (logicalReplicationClient , never ()).setStartLsn (any ());
149+ verify (logicalReplicationClient ).connect ();
150+ }
151+
152+ @ Test
153+ void test_shutdown () throws IOException {
154+ streamWorker .shutdown ();
155+ verify (logicalReplicationClient ).disconnect ();
156+ }
157+
158+ private StreamWorker createObjectUnderTest () {
159+ return new StreamWorker (sourceCoordinator , logicalReplicationClient , pluginMetrics );
160+ }
97161
98- private StreamWorker createObjectUnderTest () {
99- return new StreamWorker (sourceCoordinator , binlogClientWrapper , pluginMetrics );
100162 }
101- }
163+ }
0 commit comments