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());
}
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"));
}
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"));
}
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"));
}
Aggregations