From a3156c5712aa6ca7813402ad8573efac7c0096b2 Mon Sep 17 00:00:00 2001 From: "Bobryk-Ozaki, Terrence" Date: Mon, 26 Oct 2026 09:53:46 -0700 Subject: [PATCH] fix(sql): harden JDBC connection-string config parsing against null/wrong-type values setConnectionDetailsfromMap/setConnectionDetailsFromSMSS across every QueryUtil cast Map/CaseInsensitiveProperties values straight to String (or coerced booleans via `value + ""`), so a missing key silently became null and surfaced far downstream, and a wrong-typed value threw a confusing ClassCastException instead of naming the bad key. Add getOptionalStringValue/getOptionalBooleanValue/requireNonBlank to AbstractSqlQueryUtil: optional keys get an explicit default instead of implicit string-concatenation coercion, and failures - missing, wrong-typed, or resolved-but-blank - name the offending key via IllegalArgumentException (bad input) or IllegalStateException (invalid accumulated state). These are instance methods, not static - PGVectorDatabaseEngineUnitTests mockStatics AbstractSqlQueryUtil to stub makeConnection(), and unstubbed static calls on a Mockito static mock silently return null/false, which would reintroduce the exact bug this fixes. Applied in full (required-vs-optional judgment per key, now using requireNonBlank) to H2QueryUtil and the AnsiSqlQueryUtil default shared by MySQLQueryUtil/MariaDbQueryUtil/DB2QueryUtil/DerbyQueryUtil/ AsterQueryUtil/CassandraQueryUtil/PhoenixQueryUtil. Applied as a type-safety-only swap (existing required-key checks and messages left untouched) to the 17 other QueryUtil classes with their own override, since each has its own bespoke, already-correct required/optional logic that deserves individual review before consolidating. Added missing-key/wrong-type unit tests to a new H2QueryUtilUnitTests (dev had no existing file by this name - this fix's own additions, without the unrelated H2-2.x-migration-specific tests an IL4-only predecessor commit had added to its own copy of this file, which don't apply since dev is still on H2 1.4.200), AbstractSqlQueryUtilUnitTests, and AnsiSqlQueryUtilUnitTests (via DerbyQueryUtil, proving the shared default works for its inheritors) - merged alongside dev's own independent additions to both of those files. Co-Authored-By: Claude Sonnet 5 --- src/prerna/util/sql/AbstractSqlQueryUtil.java | 117 +++++++++++++++- src/prerna/util/sql/AnsiSqlQueryUtil.java | 37 +++-- src/prerna/util/sql/AthenaQueryUtil.java | 28 ++-- src/prerna/util/sql/BigQueryQueryUtil.java | 52 +++---- src/prerna/util/sql/ClickhouseQueryUtil.java | 20 +-- src/prerna/util/sql/DatabricksQueryUtil.java | 38 +++--- .../util/sql/ElasticSearchQueryUtil.java | 20 +-- src/prerna/util/sql/H2QueryUtil.java | 36 +++-- .../util/sql/MicrosoftSqlServerQueryUtil.java | 32 ++--- src/prerna/util/sql/OpenSearchQueryUtil.java | 20 +-- src/prerna/util/sql/OracleQueryUtil.java | 20 +-- src/prerna/util/sql/PostgresQueryUtil.java | 32 ++--- src/prerna/util/sql/RedshiftQueryUtil.java | 24 ++-- src/prerna/util/sql/SAPHanaQueryUtil.java | 20 +-- src/prerna/util/sql/SEMOSSQueryUtil.java | 36 ++--- src/prerna/util/sql/SQLiteQueryUtil.java | 12 +- src/prerna/util/sql/SnowFlakeQueryUtil.java | 32 ++--- src/prerna/util/sql/TeradataQueryUtil.java | 16 +-- src/prerna/util/sql/TibcoQueryUtil.java | 20 +-- src/prerna/util/sql/TrinoQueryUtil.java | 24 ++-- .../sql/AbstractSqlQueryUtilUnitTests.java | 99 ++++++++++++++ .../util/sql/AnsiSqlQueryUtilUnitTests.java | 57 ++++++++ .../prerna/util/sql/H2QueryUtilUnitTests.java | 129 ++++++++++++++++++ 23 files changed, 652 insertions(+), 269 deletions(-) create mode 100644 test/prerna/util/sql/H2QueryUtilUnitTests.java diff --git a/src/prerna/util/sql/AbstractSqlQueryUtil.java b/src/prerna/util/sql/AbstractSqlQueryUtil.java index 6f36b240a0a..e23209f9262 100644 --- a/src/prerna/util/sql/AbstractSqlQueryUtil.java +++ b/src/prerna/util/sql/AbstractSqlQueryUtil.java @@ -201,14 +201,118 @@ public abstract class AbstractSqlQueryUtil { /** * Build the connection string after the connection details have been set - * + * * @return */ public abstract String buildConnectionString(); + ///////////////////////////////////////////////////////////////////////////////////// + + /* + * Type-safe accessors for reading connection configuration out of a + * Map (FE/JSON input) or a CaseInsensitiveProperties (SMSS + * input). Both inputs are untyped at the API boundary - a key can be absent, + * null, or holding a value of the wrong type - so setConnectionDetailsfromMap + * / setConnectionDetailsFromSMSS implementations should read connection + * config through these instead of casting configMap.get(KEY)/prop.get(KEY) + * directly, which turns a missing or mistyped key into a silent null or a + * ClassCastException far from the actual bad input. + */ + + /** + * Get an optional String value, returning {@code defaultValue} if the key is + * absent. Still fails fast, naming the key, if it is present but not actually + * a String. + * + * @param configMap + * @param key + * @param defaultValue + * @return + */ + protected String getOptionalStringValue(Map configMap, String key, String defaultValue) { + return optionalString(key, configMap.get(key), defaultValue); + } + + /** + * @see #getOptionalStringValue(Map, String, String) + */ + protected String getOptionalStringValue(CaseInsensitiveProperties prop, String key, String defaultValue) { + return optionalString(key, prop.get(key), defaultValue); + } + + /** + * Get an optional boolean value, returning {@code defaultValue} if the key is + * absent. Accepts an actual Boolean, or a String parsed via + * {@link Boolean#parseBoolean(String)}; still fails fast, naming the key, for + * any other type instead of silently coercing it - e.g. via + * {@code value + ""} string concatenation - to false. + * + * @param configMap + * @param key + * @param defaultValue + * @return + */ + protected boolean getOptionalBooleanValue(Map configMap, String key, boolean defaultValue) { + return optionalBoolean(key, configMap.get(key), defaultValue); + } + + /** + * @see #getOptionalBooleanValue(Map, String, boolean) + */ + protected boolean getOptionalBooleanValue(CaseInsensitiveProperties prop, String key, boolean defaultValue) { + return optionalBoolean(key, prop.get(key), defaultValue); + } + + /** + * Assert that a (conditionally) required configuration value was actually + * resolved - e.g. a hostname that is only required when no full connection + * URL was separately provided. Unlike the config-map accessors above, this + * validates accumulated object state rather than a single input argument, so + * it fails with {@link IllegalStateException} rather than + * {@link IllegalArgumentException}. + * + * @param key the configuration key this value came from, used only for the + * error message + * @param value the resolved value to check + * @return value, unchanged, if it is non-null and non-empty + */ + protected String requireNonBlank(String key, String value) { + if (value == null || value.isEmpty()) { + throw new IllegalStateException("Must provide a value for required configuration key '" + key + "'"); + } + return value; + } + + private String optionalString(String key, Object value, String defaultValue) { + if (value == null) { + return defaultValue; + } + if (!(value instanceof String)) { + throw new IllegalArgumentException( + "Configuration key '" + key + "' must be a String, but found " + value.getClass().getName()); + } + return (String) value; + } + + private boolean optionalBoolean(String key, Object value, boolean defaultValue) { + if (value == null) { + return defaultValue; + } + if (value instanceof Boolean) { + return (Boolean) value; + } + if (value instanceof String) { + return Boolean.parseBoolean((String) value); + } + throw new IllegalArgumentException("Configuration key '" + key + "' must be a Boolean or String, but found " + + value.getClass().getName()); + } + + ///////////////////////////////////////////////////////////////////////////////////// + /** * Method to get a connection to an existing RDBMS engine - * + * * @param driverEnum * @param connectionUrl * @param connectionDetails @@ -218,13 +322,13 @@ public abstract class AbstractSqlQueryUtil { public static Connection makeConnection(AbstractSqlQueryUtil util, String connectionUrl, Map connectionDetails) throws SQLException { return AbstractSqlQueryUtil.makeConnection(util.getDbType(), connectionUrl, - (String) connectionDetails.get(util.getConnectionUserKey()), - (String) connectionDetails.get(util.getConnectionPasswordKey())); + util.getOptionalStringValue(connectionDetails, util.getConnectionUserKey(), null), + util.getOptionalStringValue(connectionDetails, util.getConnectionPasswordKey(), null)); } /** * Method to get a connection to an existing RDBMS engine - * + * * @param driverEnum * @param connectionUrl * @param connectionDetails @@ -234,7 +338,8 @@ public static Connection makeConnection(AbstractSqlQueryUtil util, String connec public static Connection makeConnection(AbstractSqlQueryUtil util, String connectionUrl, CaseInsensitiveProperties prop) throws SQLException { return AbstractSqlQueryUtil.makeConnection(util.getDbType(), connectionUrl, - (String) prop.get(util.getConnectionUserKey()), (String) prop.get(util.getConnectionPasswordKey())); + util.getOptionalStringValue(prop, util.getConnectionUserKey(), null), + util.getOptionalStringValue(prop, util.getConnectionPasswordKey(), null)); } /** diff --git a/src/prerna/util/sql/AnsiSqlQueryUtil.java b/src/prerna/util/sql/AnsiSqlQueryUtil.java index 4165e3be463..3d9f727b0d0 100644 --- a/src/prerna/util/sql/AnsiSqlQueryUtil.java +++ b/src/prerna/util/sql/AnsiSqlQueryUtil.java @@ -1913,13 +1913,13 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); - this.schema = (String) configMap.get(AbstractSqlQueryUtil.SCHEMA); - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); - this.username = (String) configMap.get(AbstractSqlQueryUtil.USERNAME); - this.password = (String) configMap.get(AbstractSqlQueryUtil.PASSWORD); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); + this.schema = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SCHEMA, null); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); + this.username = getOptionalStringValue(configMap, AbstractSqlQueryUtil.USERNAME, null); + this.password = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PASSWORD, null); return buildConnectionString(); } @@ -1933,13 +1933,13 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); - this.schema = (String) prop.get(AbstractSqlQueryUtil.SCHEMA); - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); - this.username = (String) prop.get(AbstractSqlQueryUtil.USERNAME); - this.password = (String) prop.get(AbstractSqlQueryUtil.PASSWORD); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); + this.schema = getOptionalStringValue(prop, AbstractSqlQueryUtil.SCHEMA, null); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); + this.username = getOptionalStringValue(prop, AbstractSqlQueryUtil.USERNAME, null); + this.password = getOptionalStringValue(prop, AbstractSqlQueryUtil.PASSWORD, null); return buildConnectionString(); } @@ -1953,13 +1953,8 @@ public String buildConnectionString() { return this.connectionUrl; } - if (this.hostname == null || this.hostname.isEmpty()) { - throw new RuntimeException("Must pass in a hostname"); - } - - if (this.schema == null || this.schema.isEmpty()) { - throw new RuntimeException("Must pass in schema name"); - } + requireNonBlank(AbstractSqlQueryUtil.HOSTNAME, this.hostname); + requireNonBlank(AbstractSqlQueryUtil.SCHEMA, this.schema); String port = getPort(); if (port != null && !port.isEmpty()) { diff --git a/src/prerna/util/sql/AthenaQueryUtil.java b/src/prerna/util/sql/AthenaQueryUtil.java index b58edf13f18..43d356ad535 100644 --- a/src/prerna/util/sql/AthenaQueryUtil.java +++ b/src/prerna/util/sql/AthenaQueryUtil.java @@ -54,16 +54,16 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); - this.region = (String) configMap.get(AbstractSqlQueryUtil.REGION); - this.accessKey = (String) configMap.get(AbstractSqlQueryUtil.ACCESS_KEY); - this.secretKey = (String) configMap.get(AbstractSqlQueryUtil.SECRET_KEY); - this.output = (String) configMap.get(AbstractSqlQueryUtil.OUTPUT); - this.schema = (String) configMap.get(AbstractSqlQueryUtil.SCHEMA); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); + this.region = getOptionalStringValue(configMap, AbstractSqlQueryUtil.REGION, null); + this.accessKey = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ACCESS_KEY, null); + this.secretKey = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SECRET_KEY, null); + this.output = getOptionalStringValue(configMap, AbstractSqlQueryUtil.OUTPUT, null); + this.schema = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SCHEMA, null); if (this.schema == null || this.schema.isEmpty()) { this.schema = "default"; } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); return buildConnectionString(); } @@ -73,16 +73,16 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); - this.region = (String) prop.get(AbstractSqlQueryUtil.REGION); - this.accessKey = (String) prop.get(AbstractSqlQueryUtil.ACCESS_KEY); - this.secretKey = (String) prop.get(AbstractSqlQueryUtil.SECRET_KEY); - this.output = (String) prop.get(AbstractSqlQueryUtil.OUTPUT); - this.schema = (String) prop.get(AbstractSqlQueryUtil.SCHEMA); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); + this.region = getOptionalStringValue(prop, AbstractSqlQueryUtil.REGION, null); + this.accessKey = getOptionalStringValue(prop, AbstractSqlQueryUtil.ACCESS_KEY, null); + this.secretKey = getOptionalStringValue(prop, AbstractSqlQueryUtil.SECRET_KEY, null); + this.output = getOptionalStringValue(prop, AbstractSqlQueryUtil.OUTPUT, null); + this.schema = getOptionalStringValue(prop, AbstractSqlQueryUtil.SCHEMA, null); if (this.schema == null || this.schema.isEmpty()) { this.schema = "default"; } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); return buildConnectionString(); } diff --git a/src/prerna/util/sql/BigQueryQueryUtil.java b/src/prerna/util/sql/BigQueryQueryUtil.java index 243e01331b7..440e85d15c5 100644 --- a/src/prerna/util/sql/BigQueryQueryUtil.java +++ b/src/prerna/util/sql/BigQueryQueryUtil.java @@ -122,39 +122,39 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.hostname == null || this.hostname.isEmpty())) { throw new RuntimeException("Must pass in a host"); } - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); if (this.port == null || this.port.isEmpty()) { this.port = "443"; } - this.projectId = (String) configMap.get(AbstractSqlQueryUtil.PROJECT_ID); + this.projectId = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PROJECT_ID, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.projectId == null || this.projectId.isEmpty())) { throw new RuntimeException("Must pass in a project id"); } - this.oauthType = (String) configMap.get(AbstractSqlQueryUtil.OAUTH_TYPE); + this.oauthType = getOptionalStringValue(configMap, AbstractSqlQueryUtil.OAUTH_TYPE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.oauthType == null || this.oauthType.isEmpty())) { throw new RuntimeException("Must pass in an OAuth Type"); } if (this.oauthType.equals("0")) { - this.oauthServiceAcctEmail = (String) configMap.get(AbstractSqlQueryUtil.OAUTH_SERVICE_ACCT_EMAIL); + this.oauthServiceAcctEmail = getOptionalStringValue(configMap, AbstractSqlQueryUtil.OAUTH_SERVICE_ACCT_EMAIL, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.oauthServiceAcctEmail == null || this.oauthServiceAcctEmail.isEmpty())) { throw new RuntimeException("Must pass in an OAuth Service Account Email"); } - this.oauthPrivateKeyPath = (String) configMap.get(AbstractSqlQueryUtil.OAUTH_PRIVATE_KEY_PATH); + this.oauthPrivateKeyPath = getOptionalStringValue(configMap, AbstractSqlQueryUtil.OAUTH_PRIVATE_KEY_PATH, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.oauthPrivateKeyPath == null || this.oauthPrivateKeyPath.isEmpty())) { throw new RuntimeException("Must pass in an OAuth Private Key Path"); @@ -162,8 +162,8 @@ public String setConnectionDetailsfromMap(Map configMap) throws } else if (this.oauthType.equals("1")) { throw new IllegalArgumentException("This authentication protocol is not currently supported"); } else if (this.oauthType.equals("2")) { - this.oauthAccessToken = (String) configMap.get(AbstractSqlQueryUtil.OAUTH_ACCESS_TOKEN); - this.oauthRefreshToken = (String) configMap.get(AbstractSqlQueryUtil.OAUTH_REFRESH_TOKEN); + this.oauthAccessToken = getOptionalStringValue(configMap, AbstractSqlQueryUtil.OAUTH_ACCESS_TOKEN, null); + this.oauthRefreshToken = getOptionalStringValue(configMap, AbstractSqlQueryUtil.OAUTH_REFRESH_TOKEN, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && ((this.oauthAccessToken == null || this.oauthAccessToken.isEmpty()) @@ -171,13 +171,13 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Must pass in an OAuth Access Token or Refresh Token"); } - this.oauthClientId = (String) configMap.get(AbstractSqlQueryUtil.OAUTH_CLIENT_ID); + this.oauthClientId = getOptionalStringValue(configMap, AbstractSqlQueryUtil.OAUTH_CLIENT_ID, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.oauthClientId == null || this.oauthClientId.isEmpty())) { throw new RuntimeException("Must pass in an OAuth Client ID"); } - this.oauthClientSecret = (String) configMap.get(AbstractSqlQueryUtil.OAUTH_CLIENT_SECRET); + this.oauthClientSecret = getOptionalStringValue(configMap, AbstractSqlQueryUtil.OAUTH_CLIENT_SECRET, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.oauthClientSecret == null || this.oauthClientSecret.isEmpty())) { throw new RuntimeException("Must pass in an OAuth Client Secret"); @@ -189,13 +189,13 @@ public String setConnectionDetailsfromMap(Map configMap) throws "OAuth Type can only contain one of the following values: '0', '1', '2', '3'"); } - this.schema = (String) configMap.get(AbstractSqlQueryUtil.SCHEMA); + this.schema = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SCHEMA, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.schema == null || this.schema.isEmpty())) { throw new RuntimeException("Must pass in the default dataset"); } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -234,39 +234,39 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.hostname == null || this.hostname.isEmpty())) { throw new RuntimeException("Must pass in a host"); } - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); if (this.port == null || this.port.isEmpty()) { this.port = "443"; } - this.projectId = (String) prop.get(AbstractSqlQueryUtil.PROJECT_ID); + this.projectId = getOptionalStringValue(prop, AbstractSqlQueryUtil.PROJECT_ID, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.projectId == null || this.projectId.isEmpty())) { throw new RuntimeException("Must pass in a project id"); } - this.oauthType = (String) prop.get(AbstractSqlQueryUtil.OAUTH_TYPE); + this.oauthType = getOptionalStringValue(prop, AbstractSqlQueryUtil.OAUTH_TYPE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.oauthType == null || this.oauthType.isEmpty())) { throw new RuntimeException("Must pass in an OAuth Type"); } if (this.oauthType.equals("0")) { - this.oauthServiceAcctEmail = (String) prop.get(AbstractSqlQueryUtil.OAUTH_SERVICE_ACCT_EMAIL); + this.oauthServiceAcctEmail = getOptionalStringValue(prop, AbstractSqlQueryUtil.OAUTH_SERVICE_ACCT_EMAIL, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.oauthServiceAcctEmail == null || this.oauthServiceAcctEmail.isEmpty())) { throw new RuntimeException("Must pass in an OAuth Service Account Email"); } - this.oauthPrivateKeyPath = (String) prop.get(AbstractSqlQueryUtil.OAUTH_PRIVATE_KEY_PATH); + this.oauthPrivateKeyPath = getOptionalStringValue(prop, AbstractSqlQueryUtil.OAUTH_PRIVATE_KEY_PATH, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.oauthPrivateKeyPath == null || this.oauthPrivateKeyPath.isEmpty())) { throw new RuntimeException("Must pass in an OAuth Private Key Path"); @@ -274,8 +274,8 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw } else if (this.oauthType.equals("1")) { throw new IllegalArgumentException("This authentication protocol is not currently supported"); } else if (this.oauthType.equals("2")) { - this.oauthAccessToken = (String) prop.get(AbstractSqlQueryUtil.OAUTH_ACCESS_TOKEN); - this.oauthRefreshToken = (String) prop.get(AbstractSqlQueryUtil.OAUTH_REFRESH_TOKEN); + this.oauthAccessToken = getOptionalStringValue(prop, AbstractSqlQueryUtil.OAUTH_ACCESS_TOKEN, null); + this.oauthRefreshToken = getOptionalStringValue(prop, AbstractSqlQueryUtil.OAUTH_REFRESH_TOKEN, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && ((this.oauthAccessToken == null || this.oauthAccessToken.isEmpty()) @@ -283,13 +283,13 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Must pass in an OAuth Access Token or Refresh Token"); } - this.oauthClientId = (String) prop.get(AbstractSqlQueryUtil.OAUTH_CLIENT_ID); + this.oauthClientId = getOptionalStringValue(prop, AbstractSqlQueryUtil.OAUTH_CLIENT_ID, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.oauthClientId == null || this.oauthClientId.isEmpty())) { throw new RuntimeException("Must pass in an OAuth Client ID"); } - this.oauthClientSecret = (String) prop.get(AbstractSqlQueryUtil.OAUTH_CLIENT_SECRET); + this.oauthClientSecret = getOptionalStringValue(prop, AbstractSqlQueryUtil.OAUTH_CLIENT_SECRET, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.oauthClientSecret == null || this.oauthClientSecret.isEmpty())) { throw new RuntimeException("Must pass in an OAuth Client Secret"); @@ -301,13 +301,13 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw "OAuth Type can only contain one of the following values: '0', '1', '2', '3'"); } - this.schema = (String) prop.get(AbstractSqlQueryUtil.SCHEMA); + this.schema = getOptionalStringValue(prop, AbstractSqlQueryUtil.SCHEMA, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.schema == null || this.schema.isEmpty())) { throw new RuntimeException("Must pass in the default dataset"); } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/src/prerna/util/sql/ClickhouseQueryUtil.java b/src/prerna/util/sql/ClickhouseQueryUtil.java index 12a45ac3fb8..79f745bde8a 100644 --- a/src/prerna/util/sql/ClickhouseQueryUtil.java +++ b/src/prerna/util/sql/ClickhouseQueryUtil.java @@ -49,14 +49,14 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -64,13 +64,13 @@ public String setConnectionDetailsfromMap(Map configMap) throws port = ""; } - this.database = (String) configMap.get(AbstractSqlQueryUtil.DATABASE); + this.database = getOptionalStringValue(configMap, AbstractSqlQueryUtil.DATABASE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.database == null || this.database.isEmpty())) { throw new RuntimeException("Must pass in database name"); } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -88,14 +88,14 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -103,13 +103,13 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw port = ""; } - this.database = (String) prop.get(AbstractSqlQueryUtil.DATABASE); + this.database = getOptionalStringValue(prop, AbstractSqlQueryUtil.DATABASE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.database == null || this.database.isEmpty())) { throw new RuntimeException("Must pass in database name"); } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/src/prerna/util/sql/DatabricksQueryUtil.java b/src/prerna/util/sql/DatabricksQueryUtil.java index 5a1fe6a3371..b4e4503275c 100644 --- a/src/prerna/util/sql/DatabricksQueryUtil.java +++ b/src/prerna/util/sql/DatabricksQueryUtil.java @@ -66,16 +66,16 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); - - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); - this.httpPath = (String) configMap.get(AbstractSqlQueryUtil.HTTP_PATH); - this.uid = (String) configMap.get(AbstractSqlQueryUtil.UID); - this.pwd = (String) configMap.get(AbstractSqlQueryUtil.PWD); - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); - this.database = (String) configMap.get(AbstractSqlQueryUtil.DATABASE); - this.schema = (String) configMap.get(AbstractSqlQueryUtil.SCHEMA); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); + + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); + this.httpPath = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HTTP_PATH, null); + this.uid = getOptionalStringValue(configMap, AbstractSqlQueryUtil.UID, null); + this.pwd = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PWD, null); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); + this.database = getOptionalStringValue(configMap, AbstractSqlQueryUtil.DATABASE, null); + this.schema = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SCHEMA, null); // these are not in connection url, but needed if (this.database == null || this.database.isEmpty()) { throw new RuntimeException("Must pass in a database"); @@ -92,16 +92,16 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); - this.httpPath = (String) prop.get(AbstractSqlQueryUtil.HTTP_PATH); - this.uid = (String) prop.get(AbstractSqlQueryUtil.UID); - this.pwd = (String) prop.get(AbstractSqlQueryUtil.PWD); - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); - this.database = (String) prop.get(AbstractSqlQueryUtil.DATABASE); - this.schema = (String) prop.get(AbstractSqlQueryUtil.SCHEMA); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); + this.httpPath = getOptionalStringValue(prop, AbstractSqlQueryUtil.HTTP_PATH, null); + this.uid = getOptionalStringValue(prop, AbstractSqlQueryUtil.UID, null); + this.pwd = getOptionalStringValue(prop, AbstractSqlQueryUtil.PWD, null); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); + this.database = getOptionalStringValue(prop, AbstractSqlQueryUtil.DATABASE, null); + this.schema = getOptionalStringValue(prop, AbstractSqlQueryUtil.SCHEMA, null); // these are not in connection url, but needed if (this.database == null || this.database.isEmpty()) { throw new RuntimeException("Must pass in a database"); diff --git a/src/prerna/util/sql/ElasticSearchQueryUtil.java b/src/prerna/util/sql/ElasticSearchQueryUtil.java index 66fd5c2a3f3..c22072a6bd9 100644 --- a/src/prerna/util/sql/ElasticSearchQueryUtil.java +++ b/src/prerna/util/sql/ElasticSearchQueryUtil.java @@ -51,20 +51,20 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.httpType = (String) configMap.get(AbstractSqlQueryUtil.HTTP_TYPE); + this.httpType = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HTTP_TYPE, null); if (this.httpType == null || this.httpType.isEmpty()) { this.httpType = "https"; } - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.hostname == null || this.hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -72,7 +72,7 @@ public String setConnectionDetailsfromMap(Map configMap) throws port = ""; } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -92,20 +92,20 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.httpType = (String) prop.get(AbstractSqlQueryUtil.HTTP_TYPE); + this.httpType = getOptionalStringValue(prop, AbstractSqlQueryUtil.HTTP_TYPE, null); if (this.httpType == null || this.httpType.isEmpty()) { this.httpType = "https"; } - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.hostname == null || this.hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -113,7 +113,7 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw port = ""; } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/src/prerna/util/sql/H2QueryUtil.java b/src/prerna/util/sql/H2QueryUtil.java index c8a9802df20..e4e9f017ba2 100644 --- a/src/prerna/util/sql/H2QueryUtil.java +++ b/src/prerna/util/sql/H2QueryUtil.java @@ -84,14 +84,14 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.forceFile = Boolean.parseBoolean(configMap.get(AbstractSqlQueryUtil.FORCE_FILE) + ""); - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); - this.schema = (String) configMap.get(AbstractSqlQueryUtil.SCHEMA); - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); - this.username = (String) configMap.get(AbstractSqlQueryUtil.USERNAME); - this.password = (String) configMap.get(AbstractSqlQueryUtil.PASSWORD); + this.forceFile = getOptionalBooleanValue(configMap, AbstractSqlQueryUtil.FORCE_FILE, false); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); + this.schema = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SCHEMA, null); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); + this.username = getOptionalStringValue(configMap, AbstractSqlQueryUtil.USERNAME, null); + this.password = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PASSWORD, null); return buildConnectionString(); } @@ -102,14 +102,14 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.forceFile = Boolean.parseBoolean(prop.get(AbstractSqlQueryUtil.FORCE_FILE) + ""); - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); - this.schema = (String) prop.get(AbstractSqlQueryUtil.SCHEMA); - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); - this.username = (String) prop.get(AbstractSqlQueryUtil.USERNAME); - this.password = (String) prop.get(AbstractSqlQueryUtil.PASSWORD); + this.forceFile = getOptionalBooleanValue(prop, AbstractSqlQueryUtil.FORCE_FILE, false); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); + this.schema = getOptionalStringValue(prop, AbstractSqlQueryUtil.SCHEMA, null); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); + this.username = getOptionalStringValue(prop, AbstractSqlQueryUtil.USERNAME, null); + this.password = getOptionalStringValue(prop, AbstractSqlQueryUtil.PASSWORD, null); return buildConnectionString(); } @@ -122,9 +122,7 @@ public String buildConnectionString() { this.connectionUrl = this.dbType.getUrlPrefix(); - if (this.hostname == null || this.hostname.isEmpty()) { - throw new RuntimeException("Must pass in a hostname"); - } + requireNonBlank(AbstractSqlQueryUtil.HOSTNAME, this.hostname); String port = this.port; if (port != null && !port.isEmpty()) { diff --git a/src/prerna/util/sql/MicrosoftSqlServerQueryUtil.java b/src/prerna/util/sql/MicrosoftSqlServerQueryUtil.java index fee60d7e589..20f43d4d6f0 100644 --- a/src/prerna/util/sql/MicrosoftSqlServerQueryUtil.java +++ b/src/prerna/util/sql/MicrosoftSqlServerQueryUtil.java @@ -83,14 +83,14 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); - this.database = (String) configMap.get(AbstractSqlQueryUtil.DATABASE); - this.schema = (String) configMap.get(AbstractSqlQueryUtil.SCHEMA); - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); - this.username = (String) configMap.get(AbstractSqlQueryUtil.USERNAME); - this.password = (String) configMap.get(AbstractSqlQueryUtil.PASSWORD); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); + this.database = getOptionalStringValue(configMap, AbstractSqlQueryUtil.DATABASE, null); + this.schema = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SCHEMA, null); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); + this.username = getOptionalStringValue(configMap, AbstractSqlQueryUtil.USERNAME, null); + this.password = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PASSWORD, null); return buildConnectionString(); } @@ -101,14 +101,14 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); - this.database = (String) prop.get(AbstractSqlQueryUtil.DATABASE); - this.schema = (String) prop.get(AbstractSqlQueryUtil.SCHEMA); - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); - this.username = (String) prop.get(AbstractSqlQueryUtil.USERNAME); - this.password = (String) prop.get(AbstractSqlQueryUtil.PASSWORD); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); + this.database = getOptionalStringValue(prop, AbstractSqlQueryUtil.DATABASE, null); + this.schema = getOptionalStringValue(prop, AbstractSqlQueryUtil.SCHEMA, null); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); + this.username = getOptionalStringValue(prop, AbstractSqlQueryUtil.USERNAME, null); + this.password = getOptionalStringValue(prop, AbstractSqlQueryUtil.PASSWORD, null); return buildConnectionString(); } diff --git a/src/prerna/util/sql/OpenSearchQueryUtil.java b/src/prerna/util/sql/OpenSearchQueryUtil.java index 3a457894ce4..0be9a3690ba 100644 --- a/src/prerna/util/sql/OpenSearchQueryUtil.java +++ b/src/prerna/util/sql/OpenSearchQueryUtil.java @@ -51,20 +51,20 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.httpType = (String) configMap.get(AbstractSqlQueryUtil.HTTP_TYPE); + this.httpType = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HTTP_TYPE, null); if (this.httpType == null || this.httpType.isEmpty()) { this.httpType = "https"; } - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.hostname == null || this.hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -72,7 +72,7 @@ public String setConnectionDetailsfromMap(Map configMap) throws port = ""; } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -92,20 +92,20 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.httpType = (String) prop.get(AbstractSqlQueryUtil.HTTP_TYPE); + this.httpType = getOptionalStringValue(prop, AbstractSqlQueryUtil.HTTP_TYPE, null); if (this.httpType == null || this.httpType.isEmpty()) { this.httpType = "https"; } - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.hostname == null || this.hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -113,7 +113,7 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw port = ""; } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/src/prerna/util/sql/OracleQueryUtil.java b/src/prerna/util/sql/OracleQueryUtil.java index 0f58e4c257e..098040847a4 100644 --- a/src/prerna/util/sql/OracleQueryUtil.java +++ b/src/prerna/util/sql/OracleQueryUtil.java @@ -52,15 +52,15 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.hostname == null || this.hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -68,13 +68,13 @@ public String setConnectionDetailsfromMap(Map configMap) throws port = ""; } - this.service = (String) configMap.get(AbstractSqlQueryUtil.SERVICE); + this.service = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SERVICE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.service == null || this.service.isEmpty())) { throw new RuntimeException("Must pass in a sid / service name"); } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -92,15 +92,15 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.hostname == null || this.hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -108,13 +108,13 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw port = ""; } - this.service = (String) prop.get(AbstractSqlQueryUtil.SERVICE); + this.service = getOptionalStringValue(prop, AbstractSqlQueryUtil.SERVICE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.service == null || this.service.isEmpty())) { throw new RuntimeException("Must pass in a sid / service name"); } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/src/prerna/util/sql/PostgresQueryUtil.java b/src/prerna/util/sql/PostgresQueryUtil.java index 61aa6064cb6..28a346f69b8 100644 --- a/src/prerna/util/sql/PostgresQueryUtil.java +++ b/src/prerna/util/sql/PostgresQueryUtil.java @@ -231,14 +231,14 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); - this.database = (String) configMap.get(AbstractSqlQueryUtil.DATABASE); - this.schema = (String) configMap.get(AbstractSqlQueryUtil.SCHEMA); - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); - this.username = (String) configMap.get(AbstractSqlQueryUtil.USERNAME); - this.password = (String) configMap.get(AbstractSqlQueryUtil.PASSWORD); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); + this.database = getOptionalStringValue(configMap, AbstractSqlQueryUtil.DATABASE, null); + this.schema = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SCHEMA, null); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); + this.username = getOptionalStringValue(configMap, AbstractSqlQueryUtil.USERNAME, null); + this.password = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PASSWORD, null); return buildConnectionString(); } @@ -249,14 +249,14 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); - this.database = (String) prop.get(AbstractSqlQueryUtil.DATABASE); - this.schema = (String) prop.get(AbstractSqlQueryUtil.SCHEMA); - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); - this.username = (String) prop.get(AbstractSqlQueryUtil.USERNAME); - this.password = (String) prop.get(AbstractSqlQueryUtil.PASSWORD); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); + this.database = getOptionalStringValue(prop, AbstractSqlQueryUtil.DATABASE, null); + this.schema = getOptionalStringValue(prop, AbstractSqlQueryUtil.SCHEMA, null); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); + this.username = getOptionalStringValue(prop, AbstractSqlQueryUtil.USERNAME, null); + this.password = getOptionalStringValue(prop, AbstractSqlQueryUtil.PASSWORD, null); return buildConnectionString(); } diff --git a/src/prerna/util/sql/RedshiftQueryUtil.java b/src/prerna/util/sql/RedshiftQueryUtil.java index f495c47445a..81f89c24e7c 100644 --- a/src/prerna/util/sql/RedshiftQueryUtil.java +++ b/src/prerna/util/sql/RedshiftQueryUtil.java @@ -58,14 +58,14 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -73,19 +73,19 @@ public String setConnectionDetailsfromMap(Map configMap) throws port = ""; } - this.database = (String) configMap.get(AbstractSqlQueryUtil.DATABASE); + this.database = getOptionalStringValue(configMap, AbstractSqlQueryUtil.DATABASE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.database == null || this.database.isEmpty())) { throw new RuntimeException("Must pass in database name"); } - this.schema = (String) configMap.get(AbstractSqlQueryUtil.SCHEMA); + this.schema = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SCHEMA, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.schema == null || this.schema.isEmpty())) { throw new RuntimeException("Must pass in schema name"); } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -103,14 +103,14 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -118,19 +118,19 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw port = ""; } - this.database = (String) prop.get(AbstractSqlQueryUtil.DATABASE); + this.database = getOptionalStringValue(prop, AbstractSqlQueryUtil.DATABASE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.database == null || this.database.isEmpty())) { throw new RuntimeException("Must pass in database name"); } - this.schema = (String) prop.get(AbstractSqlQueryUtil.SCHEMA); + this.schema = getOptionalStringValue(prop, AbstractSqlQueryUtil.SCHEMA, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.schema == null || this.schema.isEmpty())) { throw new RuntimeException("Must pass in schema name"); } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/src/prerna/util/sql/SAPHanaQueryUtil.java b/src/prerna/util/sql/SAPHanaQueryUtil.java index 25a0a82fb72..1134794d721 100644 --- a/src/prerna/util/sql/SAPHanaQueryUtil.java +++ b/src/prerna/util/sql/SAPHanaQueryUtil.java @@ -49,14 +49,14 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -64,13 +64,13 @@ public String setConnectionDetailsfromMap(Map configMap) throws port = ""; } - this.schema = (String) configMap.get(AbstractSqlQueryUtil.SCHEMA); + this.schema = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SCHEMA, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.schema == null || this.schema.isEmpty())) { throw new RuntimeException("Must pass in schema name"); } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -89,14 +89,14 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -104,13 +104,13 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw port = ""; } - this.schema = (String) prop.get(AbstractSqlQueryUtil.SCHEMA); + this.schema = getOptionalStringValue(prop, AbstractSqlQueryUtil.SCHEMA, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.schema == null || this.schema.isEmpty())) { throw new RuntimeException("Must pass in schema name"); } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/src/prerna/util/sql/SEMOSSQueryUtil.java b/src/prerna/util/sql/SEMOSSQueryUtil.java index ed57a22416a..3e6f0b15307 100644 --- a/src/prerna/util/sql/SEMOSSQueryUtil.java +++ b/src/prerna/util/sql/SEMOSSQueryUtil.java @@ -56,14 +56,14 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -71,34 +71,34 @@ public String setConnectionDetailsfromMap(Map configMap) throws port = "443"; } - this.projectId = (String) configMap.get(AbstractSqlQueryUtil.PROJECT); + this.projectId = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PROJECT, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.projectId == null || this.projectId.isEmpty())) { throw new RuntimeException("Must pass in project id"); } - this.insightId = (String) configMap.get(AbstractSqlQueryUtil.INSIGHT); + this.insightId = getOptionalStringValue(configMap, AbstractSqlQueryUtil.INSIGHT, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.insightId == null || this.insightId.isEmpty())) { throw new RuntimeException("Must pass in insight id"); } - this.protocol = (String) configMap.get(AbstractSqlQueryUtil.PROTOCOL); + this.protocol = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PROTOCOL, null); if (this.protocol == null || this.protocol.isEmpty()) { this.protocol = "https"; } - this.endpoint = (String) configMap.get(AbstractSqlQueryUtil.ENDPOINT); + this.endpoint = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ENDPOINT, null); if (this.endpoint == null || this.endpoint.isEmpty()) { this.endpoint = "Monolith"; } - this.subURL = (String) configMap.get(AbstractSqlQueryUtil.SUB_URL); + this.subURL = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SUB_URL, null); if (this.subURL == null) { this.subURL = ""; } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -120,14 +120,14 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -135,34 +135,34 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw port = "443"; } - this.projectId = (String) prop.get(AbstractSqlQueryUtil.PROJECT); + this.projectId = getOptionalStringValue(prop, AbstractSqlQueryUtil.PROJECT, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.projectId == null || this.projectId.isEmpty())) { throw new RuntimeException("Must pass in project id"); } - this.insightId = (String) prop.get(AbstractSqlQueryUtil.INSIGHT); + this.insightId = getOptionalStringValue(prop, AbstractSqlQueryUtil.INSIGHT, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.insightId == null || this.insightId.isEmpty())) { throw new RuntimeException("Must pass in insight id"); } - this.protocol = (String) prop.get(AbstractSqlQueryUtil.PROTOCOL); + this.protocol = getOptionalStringValue(prop, AbstractSqlQueryUtil.PROTOCOL, null); if (this.protocol == null || this.protocol.isEmpty()) { this.protocol = "https"; } - this.endpoint = (String) prop.get(AbstractSqlQueryUtil.ENDPOINT); + this.endpoint = getOptionalStringValue(prop, AbstractSqlQueryUtil.ENDPOINT, null); if (this.endpoint == null || this.endpoint.isEmpty()) { this.endpoint = "Monolith"; } - this.subURL = (String) prop.get(AbstractSqlQueryUtil.SUB_URL); + this.subURL = getOptionalStringValue(prop, AbstractSqlQueryUtil.SUB_URL, null); if (this.subURL == null) { this.subURL = ""; } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/src/prerna/util/sql/SQLiteQueryUtil.java b/src/prerna/util/sql/SQLiteQueryUtil.java index 0977087a78a..3dd6ca68105 100644 --- a/src/prerna/util/sql/SQLiteQueryUtil.java +++ b/src/prerna/util/sql/SQLiteQueryUtil.java @@ -90,9 +90,9 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } @@ -101,7 +101,7 @@ public String setConnectionDetailsfromMap(Map configMap) throws this.hostname = this.hostname.replace(".mv.db", ""); } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -119,9 +119,9 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } @@ -130,7 +130,7 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw this.hostname = this.hostname.replace(".mv.db", ""); } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/src/prerna/util/sql/SnowFlakeQueryUtil.java b/src/prerna/util/sql/SnowFlakeQueryUtil.java index 6555e3d6922..9d173361c76 100644 --- a/src/prerna/util/sql/SnowFlakeQueryUtil.java +++ b/src/prerna/util/sql/SnowFlakeQueryUtil.java @@ -67,14 +67,14 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -82,31 +82,31 @@ public String setConnectionDetailsfromMap(Map configMap) throws port = ""; } - this.warehouse = (String) configMap.get(AbstractSqlQueryUtil.WAREHOUSE); + this.warehouse = getOptionalStringValue(configMap, AbstractSqlQueryUtil.WAREHOUSE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.warehouse == null || this.warehouse.isEmpty())) { throw new RuntimeException("Must pass in the warehouse to compute the queries"); } - this.role = (String) configMap.get(AbstractSqlQueryUtil.ROLE); + this.role = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ROLE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.role == null || this.role.isEmpty())) { this.role = "PUBLIC"; } - this.database = (String) configMap.get(AbstractSqlQueryUtil.DATABASE); + this.database = getOptionalStringValue(configMap, AbstractSqlQueryUtil.DATABASE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.database == null || this.database.isEmpty())) { throw new RuntimeException("Must pass in database name"); } - this.schema = (String) configMap.get(AbstractSqlQueryUtil.SCHEMA); + this.schema = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SCHEMA, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.schema == null || this.schema.isEmpty())) { throw new RuntimeException("Must pass in schema name"); } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -125,14 +125,14 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -140,31 +140,31 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw port = ""; } - this.warehouse = (String) prop.get(AbstractSqlQueryUtil.WAREHOUSE); + this.warehouse = getOptionalStringValue(prop, AbstractSqlQueryUtil.WAREHOUSE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.warehouse == null || this.warehouse.isEmpty())) { throw new RuntimeException("Must pass in the warehouse to compute the queries"); } - this.role = (String) prop.get(AbstractSqlQueryUtil.ROLE); + this.role = getOptionalStringValue(prop, AbstractSqlQueryUtil.ROLE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.role == null || this.role.isEmpty())) { this.role = "PUBLIC"; } - this.database = (String) prop.get(AbstractSqlQueryUtil.DATABASE); + this.database = getOptionalStringValue(prop, AbstractSqlQueryUtil.DATABASE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.database == null || this.database.isEmpty())) { throw new RuntimeException("Must pass in database name"); } - this.schema = (String) prop.get(AbstractSqlQueryUtil.SCHEMA); + this.schema = getOptionalStringValue(prop, AbstractSqlQueryUtil.SCHEMA, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.schema == null || this.schema.isEmpty())) { throw new RuntimeException("Must pass in schema name"); } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/src/prerna/util/sql/TeradataQueryUtil.java b/src/prerna/util/sql/TeradataQueryUtil.java index 6528b0fa81b..6c88682681e 100644 --- a/src/prerna/util/sql/TeradataQueryUtil.java +++ b/src/prerna/util/sql/TeradataQueryUtil.java @@ -63,20 +63,20 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.database = (String) configMap.get(AbstractSqlQueryUtil.DATABASE); + this.database = getOptionalStringValue(configMap, AbstractSqlQueryUtil.DATABASE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.database == null || this.database.isEmpty())) { throw new RuntimeException("Must pass in database name"); } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -94,20 +94,20 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.database = (String) prop.get(AbstractSqlQueryUtil.DATABASE); + this.database = getOptionalStringValue(prop, AbstractSqlQueryUtil.DATABASE, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.database == null || this.database.isEmpty())) { throw new RuntimeException("Must pass in database name"); } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/src/prerna/util/sql/TibcoQueryUtil.java b/src/prerna/util/sql/TibcoQueryUtil.java index 2f570cf11a3..cce8ff3180b 100644 --- a/src/prerna/util/sql/TibcoQueryUtil.java +++ b/src/prerna/util/sql/TibcoQueryUtil.java @@ -49,14 +49,14 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -64,13 +64,13 @@ public String setConnectionDetailsfromMap(Map configMap) throws port = ""; } - this.schema = (String) configMap.get(AbstractSqlQueryUtil.SCHEMA); + this.schema = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SCHEMA, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.schema == null || this.schema.isEmpty())) { throw new RuntimeException("Must pass in schema name"); } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -88,14 +88,14 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -103,13 +103,13 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw port = ""; } - this.schema = (String) prop.get(AbstractSqlQueryUtil.SCHEMA); + this.schema = getOptionalStringValue(prop, AbstractSqlQueryUtil.SCHEMA, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.schema == null || this.schema.isEmpty())) { throw new RuntimeException("Must pass in schema name"); } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/src/prerna/util/sql/TrinoQueryUtil.java b/src/prerna/util/sql/TrinoQueryUtil.java index 29205ba9d3d..4dbd74abe91 100644 --- a/src/prerna/util/sql/TrinoQueryUtil.java +++ b/src/prerna/util/sql/TrinoQueryUtil.java @@ -76,14 +76,14 @@ public String setConnectionDetailsfromMap(Map configMap) throws throw new RuntimeException("Configuration map is null or empty"); } - this.connectionUrl = (String) configMap.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) configMap.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(configMap, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) configMap.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(configMap, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -91,18 +91,18 @@ public String setConnectionDetailsfromMap(Map configMap) throws port = ""; } - this.database = (String) configMap.get(AbstractSqlQueryUtil.CATALOG); + this.database = getOptionalStringValue(configMap, AbstractSqlQueryUtil.CATALOG, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.database == null || this.database.isEmpty())) { throw new RuntimeException("Must pass in the database/catalog"); } - this.schema = (String) configMap.get(AbstractSqlQueryUtil.SCHEMA); + this.schema = getOptionalStringValue(configMap, AbstractSqlQueryUtil.SCHEMA, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (schema == null || schema.isEmpty())) { throw new RuntimeException("Must pass in schema name"); } - this.additionalProps = (String) configMap.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(configMap, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { @@ -121,14 +121,14 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw throw new RuntimeException("Properties object is null or empty"); } - this.connectionUrl = (String) prop.get(AbstractSqlQueryUtil.CONNECTION_URL); + this.connectionUrl = getOptionalStringValue(prop, AbstractSqlQueryUtil.CONNECTION_URL, null); - this.hostname = (String) prop.get(AbstractSqlQueryUtil.HOSTNAME); + this.hostname = getOptionalStringValue(prop, AbstractSqlQueryUtil.HOSTNAME, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (hostname == null || hostname.isEmpty())) { throw new RuntimeException("Must pass in a hostname"); } - this.port = (String) prop.get(AbstractSqlQueryUtil.PORT); + this.port = getOptionalStringValue(prop, AbstractSqlQueryUtil.PORT, null); String port = this.port; if (port != null && !port.isEmpty()) { port = ":" + port; @@ -136,18 +136,18 @@ public String setConnectionDetailsFromSMSS(CaseInsensitiveProperties prop) throw port = ""; } - this.database = (String) prop.get(AbstractSqlQueryUtil.CATALOG); + this.database = getOptionalStringValue(prop, AbstractSqlQueryUtil.CATALOG, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (this.database == null || this.database.isEmpty())) { throw new RuntimeException("Must pass in the database/catalog"); } - this.schema = (String) prop.get(AbstractSqlQueryUtil.SCHEMA); + this.schema = getOptionalStringValue(prop, AbstractSqlQueryUtil.SCHEMA, null); if ((this.connectionUrl == null || this.connectionUrl.isEmpty()) && (schema == null || schema.isEmpty())) { throw new RuntimeException("Must pass in schema name"); } - this.additionalProps = (String) prop.get(AbstractSqlQueryUtil.ADDITIONAL); + this.additionalProps = getOptionalStringValue(prop, AbstractSqlQueryUtil.ADDITIONAL, null); // do we need to make the connection url? if (this.connectionUrl == null || this.connectionUrl.isEmpty()) { diff --git a/test/prerna/util/sql/AbstractSqlQueryUtilUnitTests.java b/test/prerna/util/sql/AbstractSqlQueryUtilUnitTests.java index ee477761f98..d331a1ec788 100644 --- a/test/prerna/util/sql/AbstractSqlQueryUtilUnitTests.java +++ b/test/prerna/util/sql/AbstractSqlQueryUtilUnitTests.java @@ -27,6 +27,10 @@ *******************************************************************************/ package prerna.util.sql; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; @@ -34,6 +38,8 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.Types; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; import org.junit.jupiter.api.Assertions; @@ -45,8 +51,20 @@ import prerna.query.querystruct.SelectQueryStruct; import prerna.query.querystruct.selectors.QueryColumnSelector; +import prerna.engine.impl.CaseInsensitiveProperties; + class AbstractSqlQueryUtilUnitTests { + /** + * These helpers are plain, stateless logic with no instance fields involved - + * any concrete dialect works as the receiver. Deliberately called through an + * instance (not statically): PGVectorDatabaseEngineUnitTests mockStatics + * AbstractSqlQueryUtil to stub makeConnection(), and unstubbed static calls on + * a Mockito static mock silently return null/false - these must stay instance + * methods so that mock can't intercept them. + */ + private final AbstractSqlQueryUtil util = new H2QueryUtil(); + @Test void h2RoundTripsExactValues() throws Exception { try (Connection connection = DriverManager.getConnection("jdbc:h2:mem:binding_" + UUID.randomUUID())) { @@ -95,4 +113,85 @@ void dialectSearchContractProducesBoundPatternsThroughTheInterpreter() { } } + @Test + void optionalStringValueFromMapReturnsDefaultWhenKeyAbsent() { + Map configMap = new HashMap<>(); + assertEquals("fallback", util.getOptionalStringValue(configMap, "missingKey", "fallback")); + } + + @Test + void optionalStringValueFromMapReturnsValueWhenPresent() { + Map configMap = new HashMap<>(); + configMap.put("hostname", "db.example.com"); + assertEquals("db.example.com", util.getOptionalStringValue(configMap, "hostname", null)); + } + + @Test + void optionalStringValueFromMapRejectsWrongTypeNamingTheKey() { + Map configMap = new HashMap<>(); + configMap.put("port", Integer.valueOf(5432)); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> util.getOptionalStringValue(configMap, "port", null)); + assertTrue(ex.getMessage().contains("port"), "message should name the offending key: " + ex.getMessage()); + assertTrue(ex.getMessage().contains("Integer"), "message should name the actual type: " + ex.getMessage()); + } + + @Test + void optionalStringValueFromPropertiesMirrorsMapBehavior() { + CaseInsensitiveProperties prop = new CaseInsensitiveProperties(); + assertEquals("fallback", util.getOptionalStringValue(prop, "missingKey", "fallback")); + + prop.put("hostname", "db.example.com"); + assertEquals("db.example.com", util.getOptionalStringValue(prop, "hostname", null)); + + prop.put("port", Integer.valueOf(5432)); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> util.getOptionalStringValue(prop, "port", null)); + assertTrue(ex.getMessage().contains("port"), "message should name the offending key: " + ex.getMessage()); + } + + @Test + void optionalBooleanValueFromMapReturnsDefaultWhenKeyAbsent() { + Map configMap = new HashMap<>(); + assertTrue(util.getOptionalBooleanValue(configMap, "forceFile", true)); + assertFalse(util.getOptionalBooleanValue(configMap, "forceFile", false)); + } + + @Test + void optionalBooleanValueFromMapAcceptsActualBooleanAndStringForms() { + Map configMap = new HashMap<>(); + configMap.put("forceFile", Boolean.TRUE); + assertTrue(util.getOptionalBooleanValue(configMap, "forceFile", false)); + + configMap.put("forceFile", "true"); + assertTrue(util.getOptionalBooleanValue(configMap, "forceFile", false)); + + configMap.put("forceFile", "false"); + assertFalse(util.getOptionalBooleanValue(configMap, "forceFile", true)); + } + + @Test + void optionalBooleanValueFromMapRejectsWrongTypeNamingTheKey() { + Map configMap = new HashMap<>(); + configMap.put("forceFile", java.util.List.of("not", "a", "boolean")); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> util.getOptionalBooleanValue(configMap, "forceFile", false)); + assertTrue(ex.getMessage().contains("forceFile"), "message should name the offending key: " + ex.getMessage()); + } + + @Test + void requireNonBlankPassesThroughNonEmptyValueUnchanged() { + assertEquals("db.example.com", util.requireNonBlank("hostname", "db.example.com")); + } + + @Test + void requireNonBlankRejectsNullAndEmptyNamingTheKey() { + IllegalStateException nullEx = assertThrows(IllegalStateException.class, + () -> util.requireNonBlank("hostname", null)); + assertTrue(nullEx.getMessage().contains("hostname"), "message should name the key: " + nullEx.getMessage()); + + IllegalStateException emptyEx = assertThrows(IllegalStateException.class, + () -> util.requireNonBlank("hostname", "")); + assertTrue(emptyEx.getMessage().contains("hostname"), "message should name the key: " + emptyEx.getMessage()); + } } diff --git a/test/prerna/util/sql/AnsiSqlQueryUtilUnitTests.java b/test/prerna/util/sql/AnsiSqlQueryUtilUnitTests.java index 7b1fe8228a2..eaf31b5fb61 100644 --- a/test/prerna/util/sql/AnsiSqlQueryUtilUnitTests.java +++ b/test/prerna/util/sql/AnsiSqlQueryUtilUnitTests.java @@ -29,6 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -38,6 +39,7 @@ import java.sql.PreparedStatement; import java.sql.Types; import java.util.Arrays; +import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -97,4 +99,59 @@ void preparedRegexKeepsRawPatternWhileLegacyFilterEscapesIt() { ((QueryConstantSelector) legacy.getFunctionSelector().getInnerSelector().get(1)).getConstant()); } + /** + * DerbyQueryUtil does not override setConnectionDetailsfromMap/ + * buildConnectionString, so it exercises AnsiSqlQueryUtil's shared default - + * also inherited as-is by MySQLQueryUtil, MariaDbQueryUtil, DB2QueryUtil, + * AsterQueryUtil, CassandraQueryUtil and PhoenixQueryUtil. + */ + @Test + void sharedAnsiDefaultFailsFastOnMissingHostnameNamingTheKey() { + Map connDetails = new HashMap<>(); + connDetails.put(AbstractSqlQueryUtil.SCHEMA, "APP"); + + DerbyQueryUtil util = new DerbyQueryUtil(); + IllegalStateException ex = assertThrows(IllegalStateException.class, + () -> util.setConnectionDetailsfromMap(connDetails)); + assertTrue(ex.getMessage().contains(AbstractSqlQueryUtil.HOSTNAME), + "message should name the missing key: " + ex.getMessage()); + } + + @Test + void sharedAnsiDefaultFailsFastOnMissingSchemaNamingTheKey() { + Map connDetails = new HashMap<>(); + connDetails.put(AbstractSqlQueryUtil.HOSTNAME, "db.example.com"); + + DerbyQueryUtil util = new DerbyQueryUtil(); + IllegalStateException ex = assertThrows(IllegalStateException.class, + () -> util.setConnectionDetailsfromMap(connDetails)); + assertTrue(ex.getMessage().contains(AbstractSqlQueryUtil.SCHEMA), + "message should name the missing key: " + ex.getMessage()); + } + + @Test + void sharedAnsiDefaultRejectsWrongTypePortNamingTheKey() { + Map connDetails = new HashMap<>(); + connDetails.put(AbstractSqlQueryUtil.HOSTNAME, "db.example.com"); + connDetails.put(AbstractSqlQueryUtil.SCHEMA, "APP"); + connDetails.put(AbstractSqlQueryUtil.PORT, Integer.valueOf(1527)); + + DerbyQueryUtil util = new DerbyQueryUtil(); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> util.setConnectionDetailsfromMap(connDetails)); + assertTrue(ex.getMessage().contains(AbstractSqlQueryUtil.PORT), + "message should name the offending key: " + ex.getMessage()); + } + + @Test + void sharedAnsiDefaultBuildsExpectedUrlWhenAllRequiredKeysPresent() { + Map connDetails = new HashMap<>(); + connDetails.put(AbstractSqlQueryUtil.HOSTNAME, "db.example.com"); + connDetails.put(AbstractSqlQueryUtil.SCHEMA, "APP"); + connDetails.put(AbstractSqlQueryUtil.PORT, "1527"); + + DerbyQueryUtil util = new DerbyQueryUtil(); + String connectionUrl = util.setConnectionDetailsfromMap(connDetails); + assertEquals(RdbmsTypeEnum.DERBY.getUrlPrefix() + "://db.example.com:1527/APP", connectionUrl); + } } diff --git a/test/prerna/util/sql/H2QueryUtilUnitTests.java b/test/prerna/util/sql/H2QueryUtilUnitTests.java new file mode 100644 index 00000000000..9dec71f7600 --- /dev/null +++ b/test/prerna/util/sql/H2QueryUtilUnitTests.java @@ -0,0 +1,129 @@ +/******************************************************************************* + * Copyright 2015 Defense Health Agency (DHA) + * + * If your use of this software does not include any GPLv2 components: + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ---------------------------------------------------------------------------- + * If your use of this software includes any GPLv2 components: + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + *******************************************************************************/ +package prerna.util.sql; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.junit.jupiter.api.Test; + +import prerna.engine.impl.CaseInsensitiveProperties; + +class H2QueryUtilUnitTests { + + @Test + void missingHostnameWithoutConnectionUrlFailsFastNamingTheKey() { + Map connDetails = new HashMap<>(); + // present but irrelevant key, so this exercises the hostname check and not + // the null-or-empty-map guard + connDetails.put(AbstractSqlQueryUtil.USERNAME, "sa"); + + H2QueryUtil util = new H2QueryUtil(); + IllegalStateException ex = assertThrows(IllegalStateException.class, + () -> util.setConnectionDetailsfromMap(connDetails)); + assertTrue(ex.getMessage().contains(AbstractSqlQueryUtil.HOSTNAME), + "message should name the missing key: " + ex.getMessage()); + } + + @Test + void wrongTypeHostnameFailsFastNamingTheKeyAndType() { + Map connDetails = new HashMap<>(); + connDetails.put(AbstractSqlQueryUtil.HOSTNAME, Integer.valueOf(12345)); + + H2QueryUtil util = new H2QueryUtil(); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> util.setConnectionDetailsfromMap(connDetails)); + assertTrue(ex.getMessage().contains(AbstractSqlQueryUtil.HOSTNAME), + "message should name the offending key: " + ex.getMessage()); + assertTrue(ex.getMessage().contains("Integer"), "message should name the actual type: " + ex.getMessage()); + } + + @Test + void wrongTypeForceFileFailsFastNamingTheKey() { + Map connDetails = new HashMap<>(); + connDetails.put(AbstractSqlQueryUtil.HOSTNAME, "db.example.com"); + connDetails.put(AbstractSqlQueryUtil.FORCE_FILE, java.util.List.of("not", "a", "boolean")); + + H2QueryUtil util = new H2QueryUtil(); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> util.setConnectionDetailsfromMap(connDetails)); + assertTrue(ex.getMessage().contains(AbstractSqlQueryUtil.FORCE_FILE), + "message should name the offending key: " + ex.getMessage()); + } + + @Test + void connectionUrlProvidedDirectlyBypassesHostnameRequirement() { + Map connDetails = new HashMap<>(); + String rawUrl = "jdbc:h2:mem:bypass_" + UUID.randomUUID(); + connDetails.put(AbstractSqlQueryUtil.CONNECTION_URL, rawUrl); + + H2QueryUtil util = new H2QueryUtil(); + String connectionUrl = util.setConnectionDetailsfromMap(connDetails); + assertEquals(rawUrl, connectionUrl); + } + + @Test + void forceFileDefaultsToFalseWhenAbsent() { + Map connDetails = new HashMap<>(); + connDetails.put(AbstractSqlQueryUtil.HOSTNAME, "/path/that/definitely/does/not/exist/" + UUID.randomUUID()); + + H2QueryUtil util = new H2QueryUtil(); + String connectionUrl = util.setConnectionDetailsfromMap(connDetails); + assertTrue(connectionUrl.contains(":tcp://"), + "forceFile should default to false, using tcp mode for a nonexistent path: " + connectionUrl); + } + + @Test + void missingHostnameFromSmssPropertiesFailsFastNamingTheKey() { + CaseInsensitiveProperties prop = new CaseInsensitiveProperties(); + prop.put(AbstractSqlQueryUtil.USERNAME, "sa"); + + H2QueryUtil util = new H2QueryUtil(); + IllegalStateException ex = assertThrows(IllegalStateException.class, + () -> util.setConnectionDetailsFromSMSS(prop)); + assertTrue(ex.getMessage().contains(AbstractSqlQueryUtil.HOSTNAME), + "message should name the missing key: " + ex.getMessage()); + } + + @Test + void wrongTypeHostnameFromSmssPropertiesFailsFastNamingTheKey() { + CaseInsensitiveProperties prop = new CaseInsensitiveProperties(); + prop.put(AbstractSqlQueryUtil.HOSTNAME, Integer.valueOf(12345)); + + H2QueryUtil util = new H2QueryUtil(); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> util.setConnectionDetailsFromSMSS(prop)); + assertTrue(ex.getMessage().contains(AbstractSqlQueryUtil.HOSTNAME), + "message should name the offending key: " + ex.getMessage()); + } +}