Search in sources :

Example 46 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class ConnectionProxyTest method testSetCurrentConnectionWithClosedConnection.

@Test
public void testSetCurrentConnectionWithClosedConnection() throws SQLException {
    when(mockConnection.isClosed()).thenReturn(false);
    final ConnectionUrl conStr = ConnectionUrl.getConnectionUrlInstance(DEFAULT_CONNECTION_STR, new Properties());
    final ConnectionProxy proxy = getConnectionProxy(conStr);
    assertSame(mockConnection, proxy.getCurrentConnection());
    proxy.setCurrentConnection(mockConnection2, mockHostInfo);
    verify(mockConnection).close();
    assertSame(mockConnection2, proxy.getCurrentConnection());
    assertSame(mockHostInfo, proxy.getCurrentHostInfo());
}
Also used : ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 47 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class AWSSecretsManagerPluginTest method testConnectWithNewSecrets.

/**
 * The plugin will attempt to open a connection with a cached secret, but it will fail with an access error.
 * In this case, the plugin will fetch the secret and will retry the connection.
 */
@Test
public void testConnectWithNewSecrets() throws SQLException {
    // Fail initial connection attempt so secrets will be retrieved.
    // Second attempt should be successful.
    final SQLException failedFirstConnectionAccessException = new SQLException(TEST_SQL_ERROR, AWSSecretsManagerPlugin.SQLSTATE_ACCESS_ERROR);
    doThrow(failedFirstConnectionAccessException).doNothing().when(nextPlugin).openInitialConnection(any(ConnectionUrl.class));
    when(this.mockSecretsManagerClient.getSecretValue(this.mockGetValueRequest)).thenReturn(VALID_GET_SECRET_VALUE_RESPONSE);
    this.plugin.openInitialConnection(this.connectionUrl);
    assertEquals(1, AWSSecretsManagerPlugin.SECRET_CACHE.size());
    verify(this.mockSecretsManagerClient).getSecretValue(this.mockGetValueRequest);
    // Verify the openInitialConnection method was called using different ConnectionUrl arguments.
    verify(this.nextPlugin, times(2)).openInitialConnection(this.captor.capture());
    final List<ConnectionUrl> connectionUrls = this.captor.getAllValues();
    Map<String, String> connectionPropsWithCachedSecret = connectionUrls.get(0).getOriginalProperties();
    Map<String, String> connectionPropsWithNewSecret = connectionUrls.get(1).getOriginalProperties();
    assertNotEquals(TEST_USERNAME, connectionPropsWithCachedSecret.get("user"));
    assertNotEquals(TEST_PASSWORD, connectionPropsWithCachedSecret.get("password"));
    assertEquals(TEST_USERNAME, connectionPropsWithNewSecret.get("user"));
    assertEquals(TEST_PASSWORD, connectionPropsWithNewSecret.get("password"));
}
Also used : SQLException(java.sql.SQLException) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Test(org.junit.jupiter.api.Test)

Example 48 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class AWSSecretsManagerPluginTest method testFailedInitialConnectionWithGenericError.

/**
 * The plugin will attempt to open a connection with a cached secret, but it will fail with a generic SQL exception.
 * In this case, the plugin will rethrow the error back to the user.
 */
@Test
public void testFailedInitialConnectionWithGenericError() throws SQLException {
    final SQLException failedFirstConnectionGenericException = new SQLException(TEST_SQL_ERROR, SQL_STATE_FAIL_GENERIC);
    doThrow(failedFirstConnectionGenericException).when(this.nextPlugin).openInitialConnection(any(ConnectionUrl.class));
    final SQLException connectionFailedException = assertThrows(SQLException.class, () -> this.plugin.openInitialConnection(this.connectionUrl));
    assertEquals(TEST_SQL_ERROR, connectionFailedException.getMessage());
    assertEquals(0, AWSSecretsManagerPlugin.SECRET_CACHE.size());
    verify(this.mockSecretsManagerClient, never()).getSecretValue(this.mockGetValueRequest);
    verify(this.nextPlugin).openInitialConnection(this.captor.capture());
    final List<ConnectionUrl> connectionUrls = this.captor.getAllValues();
    final Map<String, String> connectionProperties = connectionUrls.get(0).getOriginalProperties();
    assertNull(connectionProperties.get("user"));
    assertNull(connectionProperties.get("password"));
}
Also used : SQLException(java.sql.SQLException) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Test(org.junit.jupiter.api.Test)

Example 49 with ConnectionUrl

use of com.mysql.cj.conf.ConnectionUrl in project aws-mysql-jdbc by awslabs.

the class AWSSecretsManagerPluginTest method testFailedToReadSecrets.

/**
 * The plugin will attempt to open a connection with a cached secret, but it will fail with an access error.
 * In this case, the plugin will attempt to fetch the secret and retry the connection,
 * but it will fail because the returned secret could not be parsed.
 */
@Test
public void testFailedToReadSecrets() throws SQLException {
    // Fail initial connection attempt so secrets will be retrieved.
    final SQLException failedFirstConnectionAccessException = new SQLException(TEST_SQL_ERROR, AWSSecretsManagerPlugin.SQLSTATE_ACCESS_ERROR);
    doThrow(failedFirstConnectionAccessException).doNothing().when(this.nextPlugin).openInitialConnection(any(ConnectionUrl.class));
    when(this.mockSecretsManagerClient.getSecretValue(this.mockGetValueRequest)).thenReturn(INVALID_GET_SECRET_VALUE_RESPONSE);
    final SQLException readSecretsFailedException = assertThrows(SQLException.class, () -> this.plugin.openInitialConnection(this.connectionUrl));
    assertEquals(readSecretsFailedException.getMessage(), AWSSecretsManagerPlugin.ERROR_GET_SECRETS_FAILED);
    assertEquals(0, AWSSecretsManagerPlugin.SECRET_CACHE.size());
    verify(this.mockSecretsManagerClient).getSecretValue(this.mockGetValueRequest);
    verify(this.nextPlugin).openInitialConnection(this.captor.capture());
    final List<ConnectionUrl> connectionUrls = this.captor.getAllValues();
    final Map<String, String> connectionProperties = connectionUrls.get(0).getOriginalProperties();
    assertNull(connectionProperties.get("user"));
    assertNull(connectionProperties.get("password"));
}
Also used : SQLException(java.sql.SQLException) ConnectionUrl(com.mysql.cj.conf.ConnectionUrl) Test(org.junit.jupiter.api.Test)

Aggregations

ConnectionUrl (com.mysql.cj.conf.ConnectionUrl)49 Test (org.junit.jupiter.api.Test)41 Properties (java.util.Properties)39 HostInfo (com.mysql.cj.conf.HostInfo)21 SQLException (java.sql.SQLException)10 ReplicationConnectionUrl (com.mysql.cj.conf.url.ReplicationConnectionUrl)9 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)9 ResultSet (java.sql.ResultSet)8 Statement (java.sql.Statement)8 ArrayList (java.util.ArrayList)8 FailoverConnectionUrl (com.mysql.cj.conf.url.FailoverConnectionUrl)7 FailoverDnsSrvConnectionUrl (com.mysql.cj.conf.url.FailoverDnsSrvConnectionUrl)7 LoadBalanceConnectionUrl (com.mysql.cj.conf.url.LoadBalanceConnectionUrl)7 LoadBalanceDnsSrvConnectionUrl (com.mysql.cj.conf.url.LoadBalanceDnsSrvConnectionUrl)7 ReplicationDnsSrvConnectionUrl (com.mysql.cj.conf.url.ReplicationDnsSrvConnectionUrl)7 SingleConnectionUrl (com.mysql.cj.conf.url.SingleConnectionUrl)7 XDevApiConnectionUrl (com.mysql.cj.conf.url.XDevApiConnectionUrl)7 XDevApiDnsSrvConnectionUrl (com.mysql.cj.conf.url.XDevApiDnsSrvConnectionUrl)7 ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)6 Connection (java.sql.Connection)6