Skip to content

Commit

Permalink
extract to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
girarda committed Feb 17, 2022
1 parent 80352b6 commit d3a1263
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@
public class MySQLDestination extends AbstractJdbcDestination implements Destination {

private static final Logger LOGGER = LoggerFactory.getLogger(MySQLDestination.class);
public static final List<String> HOST_KEY = List.of("host");
public static final List<String> PORT_KEY = List.of("port");

public static final String DRIVER_CLASS = "com.mysql.cj.jdbc.Driver";

public static final String DATABASE_KEY = "database";
public static final String HOST_KEY = "host";
public static final String JDBC_URL_KEY = "jdbc_url";
public static final String JDBC_URL_PARAMS_KEY = "jdbc_url_params";
public static final String PASSWORD_KEY = "password";
public static final String PORT_KEY = "port";
public static final String SSL_KEY = "ssl";
public static final String USERNAME_KEY = "username";

public static final String DRIVER_CLASS = "com.mysql.cj.jdbc.Driver";

static final Map<String, String> SSL_JDBC_PARAMETERS = ImmutableMap.of(
"useSSL", "true",
"requireSSL", "true",
Expand All @@ -48,15 +53,15 @@ public class MySQLDestination extends AbstractJdbcDestination implements Destina
);

public static Destination sshWrappedDestination() {
return new SshWrappedDestination(new MySQLDestination(), HOST_KEY, PORT_KEY);
return new SshWrappedDestination(new MySQLDestination(), List.of(HOST_KEY), List.of(PORT_KEY));
}

@Override
public AirbyteConnectionStatus check(final JsonNode config) {
try (final JdbcDatabase database = getDatabase(config)) {
final MySQLSqlOperations mySQLSqlOperations = (MySQLSqlOperations) getSqlOperations();

final String outputSchema = getNamingResolver().getIdentifier(config.get("database").asText());
final String outputSchema = getNamingResolver().getIdentifier(config.get(DATABASE_KEY).asText());
attemptSQLCreateAndDropTableOperations(outputSchema, database, getNamingResolver(),
mySQLSqlOperations);

Expand Down Expand Up @@ -87,9 +92,9 @@ protected JdbcDatabase getDatabase(final JsonNode config) {
final JsonNode jdbcConfig = toJdbcConfig(config);

return Databases.createJdbcDatabase(
jdbcConfig.get("username").asText(),
jdbcConfig.has("password") ? jdbcConfig.get("password").asText() : null,
jdbcConfig.get("jdbc_url").asText(),
jdbcConfig.get(USERNAME_KEY).asText(),
jdbcConfig.has(PASSWORD_KEY) ? jdbcConfig.get(PASSWORD_KEY).asText() : null,
jdbcConfig.get(JDBC_URL_KEY).asText(),
getDriverClass(),
"allowLoadLocalInfile=true");
}
Expand All @@ -99,9 +104,9 @@ public JsonNode toJdbcConfig(final JsonNode config) {
final List<String> additionalParameters = getAdditionalParameters(config);

final StringBuilder jdbcUrl = new StringBuilder(String.format("jdbc:mysql://%s:%s/%s",
config.get("host").asText(),
config.get("port").asText(),
config.get("database").asText()));
config.get(HOST_KEY).asText(),
config.get(PORT_KEY).asText(),
config.get(DATABASE_KEY).asText()));
// zero dates by default cannot be parsed into java date objects (they will throw an error)
// in addition, users don't always have agency in fixing them e.g: maybe they don't own the database
// and can't
Expand All @@ -113,11 +118,11 @@ public JsonNode toJdbcConfig(final JsonNode config) {
}

final ImmutableMap.Builder<Object, Object> configBuilder = ImmutableMap.builder()
.put("username", config.get("username").asText())
.put("jdbc_url", jdbcUrl.toString());
.put(USERNAME_KEY, config.get(USERNAME_KEY).asText())
.put(JDBC_URL_KEY, jdbcUrl.toString());

if (config.has("password")) {
configBuilder.put("password", config.get("password").asText());
if (config.has(PASSWORD_KEY)) {
configBuilder.put(PASSWORD_KEY, config.get(PASSWORD_KEY).asText());
}

return Jsons.jsonNode(configBuilder.build());
Expand Down Expand Up @@ -173,7 +178,7 @@ private Map<String, String> getCustomJdbcParameters(final JsonNode config) {
}

private boolean useSSL(final JsonNode config) {
return !config.has("ssl") || config.get("ssl").asBoolean();
return !config.has(SSL_KEY) || config.get(SSL_KEY).asBoolean();
}

static String formatParameter(final String key, final String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
import org.apache.commons.lang3.RandomStringUtils;

/**
* Abstract class that allows us to avoid duplicating testing logic for testing SSH with a key file
* or with a password.
* Abstract class that allows us to avoid duplicating testing logic for testing SSH with a key file or with a password.
*/
public abstract class SshMySQLDestinationAcceptanceTest extends DestinationAcceptanceTest {

private final ExtendedNameTransformer namingResolver = new MySQLNameTransformer();
private final List<String> HOST_KEY = List.of(MySQLDestination.HOST_KEY);
private final List<String> PORT_KEY = List.of(MySQLDestination.PORT_KEY);
private String schemaName;

public abstract Path getConfigFilePath();
Expand Down Expand Up @@ -60,9 +61,9 @@ protected JsonNode getFailCheckConfig() {

@Override
protected List<JsonNode> retrieveRecords(final TestDestinationEnv env,
final String streamName,
final String namespace,
final JsonNode streamSchema)
final String streamName,
final String namespace,
final JsonNode streamSchema)
throws Exception {
return retrieveRecordsFromTable(namingResolver.getRawTableName(streamName), namespace)
.stream()
Expand All @@ -87,8 +88,8 @@ protected boolean implementsNamespaces() {

@Override
protected List<JsonNode> retrieveNormalizedRecords(final TestDestinationEnv env,
final String streamName,
final String namespace)
final String streamName,
final String namespace)
throws Exception {
final var tableName = namingResolver.getIdentifier(streamName);
final String schema = namespace != null ? namingResolver.getIdentifier(namespace) : namingResolver.getIdentifier(schemaName);
Expand Down Expand Up @@ -121,8 +122,8 @@ private List<JsonNode> retrieveRecordsFromTable(final String tableName, final St
final var schema = schemaName == null ? this.schemaName : schemaName;
return SshTunnel.sshWrap(
getConfig(),
MySQLDestination.HOST_KEY,
MySQLDestination.PORT_KEY,
HOST_KEY,
PORT_KEY,
(CheckedFunction<JsonNode, List<JsonNode>, Exception>) mangledConfig -> getDatabaseFromConfig(mangledConfig)
.query(
ctx -> ctx
Expand All @@ -140,8 +141,8 @@ protected void setup(final TestDestinationEnv testEnv) throws Exception {
final var config = getConfig();
SshTunnel.sshWrap(
config,
MySQLDestination.HOST_KEY,
MySQLDestination.PORT_KEY,
HOST_KEY,
PORT_KEY,
mangledConfig -> {
getDatabaseFromConfig(mangledConfig).query(ctx -> ctx.fetch(String.format("CREATE DATABASE %s;", schemaName)));
});
Expand All @@ -151,8 +152,8 @@ protected void setup(final TestDestinationEnv testEnv) throws Exception {
protected void tearDown(final TestDestinationEnv testEnv) throws Exception {
SshTunnel.sshWrap(
getConfig(),
MySQLDestination.HOST_KEY,
MySQLDestination.PORT_KEY,
HOST_KEY,
PORT_KEY,
mangledConfig -> {
getDatabaseFromConfig(mangledConfig).query(ctx -> ctx.fetch(String.format("DROP DATABASE %s", schemaName)));
});
Expand Down

1 comment on commit d3a1263

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SonarQube Report

SonarQube report for Airbyte Connectors Destination Mysql(#10362)

Measures

Name Value Name Value Name Value
Bugs 0 Quality Gate Status ERROR Security Rating A
Duplicated Blocks 0 Lines of Code 318 Code Smells 1
Coverage 0.0 Vulnerabilities 0 Lines to Cover 47
Reliability Rating A Duplicated Lines (%) 0.0 Blocker Issues 0
Critical Issues 2 Major Issues 7 Minor Issues 0

Detected Issues

Rule File Description Message
java:S112 (MAJOR) mysql/MySQLDestination.java Generic exceptions should never be thrown Define and throw a dedicated exception instead of using a generic one.
java:S2864 (MAJOR) mysql/MySQLDestination.java:152 "entrySet()" should be iterated when both the key and value are needed Iterate over the "entrySet" instead of the "keySet".
java:S112 (MAJOR) mysql/MySQLDestination.java Generic exceptions should never be thrown Define and throw a dedicated exception instead of using a generic one.
java:S112 (MAJOR) mysql/MySQLDestination.java Generic exceptions should never be thrown Define and throw a dedicated exception instead of using a generic one.
java:S112 (MAJOR) mysql/MySQLDestination.java Generic exceptions should never be thrown Define and throw a dedicated exception instead of using a generic one.
java:S1134 (MAJOR) mysql/MySQLDestination.java Track uses of "FIXME" tags Take the required action to fix the issue indicated by this comment.
java:S1192 (CRITICAL) mysql/MySQLDestination.java String literals should not be duplicated Define a constant instead of duplicating this literal "username" 3 times.
java:S1192 (CRITICAL) mysql/MySQLDestination.java String literals should not be duplicated Define a constant instead of duplicating this literal "password" 5 times.
java:S112 (MAJOR) mysql/MySQLDestination.java:72 Generic exceptions should never be thrown Define and throw a dedicated exception instead of using a generic one.

Coverage (0.0%)

File Coverage File Coverage
src/main/java/io/airbyte/integrations/destination/mysql/MySQLDestination.java 0.0 src/main/java/io/airbyte/integrations/destination/mysql/MySQLNameTransformer.java 0.0
src/main/java/io/airbyte/integrations/destination/mysql/MySQLSqlOperations.java 0.0

Please sign in to comment.