1616
1717package software .amazon .jdbc ;
1818
19+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
1920import static org .junit .jupiter .api .Assertions .assertEquals ;
2021import static org .junit .jupiter .api .Assertions .assertFalse ;
2122import static org .junit .jupiter .api .Assertions .assertNotEquals ;
2223import static org .junit .jupiter .api .Assertions .assertNull ;
2324import static org .junit .jupiter .api .Assertions .assertTrue ;
2425import static org .mockito .ArgumentMatchers .any ;
26+ import static org .mockito .ArgumentMatchers .eq ;
2527import static org .mockito .Mockito .doNothing ;
2628import static org .mockito .Mockito .never ;
2729import static org .mockito .Mockito .spy ;
3032import static org .mockito .Mockito .when ;
3133
3234import java .sql .Connection ;
35+ import java .sql .ResultSet ;
3336import java .sql .SQLException ;
37+ import java .sql .Statement ;
3438import java .util .ArrayList ;
3539import java .util .Arrays ;
3640import java .util .Collections ;
4044import java .util .Map ;
4145import java .util .Properties ;
4246import java .util .Set ;
47+ import java .util .stream .Stream ;
4348import org .junit .jupiter .api .AfterEach ;
4449import org .junit .jupiter .api .BeforeEach ;
4550import org .junit .jupiter .api .Test ;
51+ import org .junit .jupiter .params .ParameterizedTest ;
52+ import org .junit .jupiter .params .provider .Arguments ;
53+ import org .junit .jupiter .params .provider .MethodSource ;
4654import org .mockito .ArgumentCaptor ;
4755import org .mockito .Captor ;
4856import org .mockito .Mock ;
49- import org .mockito .Mockito ;
5057import org .mockito .MockitoAnnotations ;
58+ import software .amazon .jdbc .dialect .AuroraPgDialect ;
59+ import software .amazon .jdbc .dialect .Dialect ;
5160import software .amazon .jdbc .dialect .DialectManager ;
61+ import software .amazon .jdbc .dialect .MysqlDialect ;
5262import software .amazon .jdbc .exceptions .ExceptionManager ;
5363
5464public class PluginServiceImplTests {
@@ -63,6 +73,8 @@ public class PluginServiceImplTests {
6373 @ Mock Connection oldConnection ;
6474 @ Mock HostListProvider hostListProvider ;
6575 @ Mock DialectManager dialectManager ;
76+ @ Mock Statement statement ;
77+ @ Mock ResultSet resultSet ;
6678
6779 @ Captor ArgumentCaptor <EnumSet <NodeChangeOptions >> argumentChanges ;
6880 @ Captor ArgumentCaptor <Map <String , EnumSet <NodeChangeOptions >>> argumentChangesMap ;
@@ -72,6 +84,8 @@ public class PluginServiceImplTests {
7284 void setUp () throws SQLException {
7385 closeable = MockitoAnnotations .openMocks (this );
7486 when (oldConnection .isClosed ()).thenReturn (false );
87+ when (newConnection .createStatement ()).thenReturn (statement );
88+ when (statement .executeQuery (any ())).thenReturn (resultSet );
7589 PluginServiceImpl .hostAvailabilityExpiringCache .clear ();
7690 }
7791
@@ -580,4 +594,73 @@ void testForceRefreshHostList_withCachedHostAvailability() throws SQLException {
580594 target .forceRefreshHostList (newConnection );
581595 assertEquals (expectedHostSpecs2 , newHostSpecs );
582596 }
597+
598+ @ Test
599+ void testIdentifyConnectionWithNoAliases () throws SQLException {
600+ PluginServiceImpl target = spy (
601+ new PluginServiceImpl (
602+ pluginManager , new ExceptionManager (), PROPERTIES , URL , DRIVER_PROTOCOL , dialectManager ));
603+ when (target .getHostListProvider ()).thenReturn (hostListProvider );
604+
605+ when (target .getDialect ()).thenReturn (new MysqlDialect ());
606+ assertNull (target .identifyConnection (newConnection ));
607+ }
608+
609+ @ Test
610+ void testIdentifyConnectionWithAliases () throws SQLException {
611+ final HostSpec expected = new HostSpec ("test" );
612+ PluginServiceImpl target = spy (
613+ new PluginServiceImpl (
614+ pluginManager , new ExceptionManager (), PROPERTIES , URL , DRIVER_PROTOCOL , dialectManager ));
615+ target .hostListProvider = hostListProvider ;
616+ when (target .getHostListProvider ()).thenReturn (hostListProvider );
617+ when (hostListProvider .identifyConnection (eq (newConnection ))).thenReturn (expected );
618+
619+ when (target .getDialect ()).thenReturn (new AuroraPgDialect ());
620+ final HostSpec actual = target .identifyConnection (newConnection );
621+ verify (target , never ()).getCurrentHostSpec ();
622+ verify (hostListProvider ).identifyConnection (newConnection );
623+ assertEquals (expected , actual );
624+ }
625+
626+ @ Test
627+ void testFillAliasesNonEmptyAliases () throws SQLException {
628+ final HostSpec oneAlias = new HostSpec ("foo" );
629+ oneAlias .addAlias (oneAlias .asAlias ());
630+
631+ PluginServiceImpl target = spy (
632+ new PluginServiceImpl (
633+ pluginManager , new ExceptionManager (), PROPERTIES , URL , DRIVER_PROTOCOL , dialectManager ));
634+
635+ assertEquals (1 , oneAlias .getAliases ().size ());
636+ target .fillAliases (newConnection , oneAlias );
637+ // Fill aliases should return directly and no additional aliases should be added.
638+ assertEquals (1 , oneAlias .getAliases ().size ());
639+ }
640+
641+ @ ParameterizedTest
642+ @ MethodSource ("fillAliasesDialects" )
643+ void testFillAliasesWithInstanceEndpoint (Dialect dialect , String [] expectedInstanceAliases ) throws SQLException {
644+ final HostSpec empty = new HostSpec ("foo" );
645+ PluginServiceImpl target = spy (
646+ new PluginServiceImpl (
647+ pluginManager , new ExceptionManager (), PROPERTIES , URL , DRIVER_PROTOCOL , dialectManager ));
648+ target .hostListProvider = hostListProvider ;
649+ when (target .getDialect ()).thenReturn (dialect );
650+ when (resultSet .next ()).thenReturn (true , false ); // Result set contains 1 row.
651+ when (resultSet .getString (eq (1 ))).thenReturn ("ip" );
652+ when (hostListProvider .identifyConnection (eq (newConnection ))).thenReturn (new HostSpec ("instance" ));
653+
654+ target .fillAliases (newConnection , empty );
655+
656+ final String [] aliases = empty .getAliases ().toArray (new String [] {});
657+ assertArrayEquals (expectedInstanceAliases , aliases );
658+ }
659+
660+ private static Stream <Arguments > fillAliasesDialects () {
661+ return Stream .of (
662+ Arguments .of (new AuroraPgDialect (), new String []{"instance" , "foo" , "ip" }),
663+ Arguments .of (new MysqlDialect (), new String []{"foo" , "ip" })
664+ );
665+ }
583666}
0 commit comments