1010package org .opensearch .dataprepper .plugins .source .rds .stream ;
1111
1212import com .github .shyiko .mysql .binlog .BinaryLogClient ;
13+ import com .github .shyiko .mysql .binlog .network .AuthenticationException ;
1314import io .micrometer .core .instrument .Counter ;
1415import org .junit .jupiter .api .BeforeEach ;
1516import org .junit .jupiter .api .Test ;
1819import org .mockito .Mock ;
1920import org .mockito .junit .jupiter .MockitoExtension ;
2021import org .opensearch .dataprepper .plugins .source .rds .utils .RdsSourceAggregateMetrics ;
21- import com .github .shyiko .mysql .binlog .network .AuthenticationException ;
2222
2323import java .io .IOException ;
2424import java .util .List ;
2929import static org .mockito .Mockito .mock ;
3030import static org .mockito .Mockito .verify ;
3131import static org .mockito .Mockito .when ;
32+ import static org .opensearch .dataprepper .plugins .source .rds .stream .BinlogClientWrapper .ACCESS_DENIED ;
33+ import static org .opensearch .dataprepper .plugins .source .rds .stream .BinlogClientWrapper .CONNECTION_REFUSED ;
34+ import static org .opensearch .dataprepper .plugins .source .rds .stream .BinlogClientWrapper .FAILED_TO_DETERMINE_BINLOG_FILENAME ;
3235
3336
3437@ ExtendWith (MockitoExtension .class )
@@ -48,6 +51,18 @@ class BinlogClientWrapperTest {
4851 @ Mock
4952 private Counter stream5xxErrors ;
5053
54+ @ Mock
55+ private Counter streamAuthErrors ;
56+
57+ @ Mock
58+ private Counter streamServerNotFoundErrors ;
59+
60+ @ Mock
61+ private Counter streamReplicationNotEnabledErrors ;
62+
63+ @ Mock
64+ private Counter streamAccessDeniedErrors ;
65+
5166 private BinlogClientWrapper binlogClientWrapper ;
5267
5368 @ BeforeEach
@@ -56,6 +71,10 @@ void setUp() {
5671 lenient ().when (rdsSourceAggregateMetrics .getStreamApiInvocations ()).thenReturn (streamApiInvocations );
5772 lenient ().when (rdsSourceAggregateMetrics .getStream4xxErrors ()).thenReturn (stream4xxErrors );
5873 lenient ().when (rdsSourceAggregateMetrics .getStream5xxErrors ()).thenReturn (stream5xxErrors );
74+ lenient ().when (rdsSourceAggregateMetrics .getStreamAuthErrors ()).thenReturn (streamAuthErrors );
75+ lenient ().when (rdsSourceAggregateMetrics .getStreamServerNotFoundErrors ()).thenReturn (streamServerNotFoundErrors );
76+ lenient ().when (rdsSourceAggregateMetrics .getStreamReplicationNotEnabledErrors ()).thenReturn (streamReplicationNotEnabledErrors );
77+ lenient ().when (rdsSourceAggregateMetrics .getStreamAccessDeniedErrors ()).thenReturn (streamAccessDeniedErrors );
5978 }
6079
6180 @ Test
@@ -66,14 +85,60 @@ void test_connect_calls_client_connect() throws IOException {
6685 }
6786
6887 @ Test
69- void test_connect_with_4xx_exception () throws IOException {
88+ void test_connect_with_4xx_auth_exception () throws IOException {
7089 doThrow (AuthenticationException .class ).when (binaryLogClient ).connect ();
7190
7291 try {
7392 binlogClientWrapper .connect ();
7493 } catch (Exception e ) {
7594 verify (streamApiInvocations ).increment ();
7695 verify (stream4xxErrors ).increment ();
96+ verify (streamAuthErrors ).increment ();
97+ }
98+ }
99+
100+ @ Test
101+ void test_connect_with_4xx_server_not_found_exception () throws IOException {
102+ Exception connectionRefusedException = new IOException (
103+ "Failed to connect to MySQL server" ,
104+ new Exception (CONNECTION_REFUSED )
105+ );
106+ doThrow (connectionRefusedException ).when (binaryLogClient ).connect ();
107+
108+ try {
109+ binlogClientWrapper .connect ();
110+ } catch (Exception e ) {
111+ verify (streamApiInvocations ).increment ();
112+ verify (stream4xxErrors ).increment ();
113+ verify (streamServerNotFoundErrors ).increment ();
114+ }
115+ }
116+
117+ @ Test
118+ void test_connect_with_4xx_binlog_exception () throws IOException {
119+ final Exception binlogNotEnabledException = new IOException (FAILED_TO_DETERMINE_BINLOG_FILENAME );
120+ doThrow (binlogNotEnabledException ).when (binaryLogClient ).connect ();
121+
122+ try {
123+ binlogClientWrapper .connect ();
124+ } catch (Exception e ) {
125+ verify (streamApiInvocations ).increment ();
126+ verify (stream4xxErrors ).increment ();
127+ verify (streamReplicationNotEnabledErrors ).increment ();
128+ }
129+ }
130+
131+ @ Test
132+ void test_connect_with_4xx_access_exception () throws IOException {
133+ final Exception accessDeniedException = new IOException (ACCESS_DENIED );
134+ doThrow (accessDeniedException ).when (binaryLogClient ).connect ();
135+
136+ try {
137+ binlogClientWrapper .connect ();
138+ } catch (Exception e ) {
139+ verify (streamApiInvocations ).increment ();
140+ verify (stream4xxErrors ).increment ();
141+ verify (streamAccessDeniedErrors ).increment ();
77142 }
78143 }
79144
0 commit comments