Skip to content

Commit 8de9ea0

Browse files
committed
Test an exception message
1 parent 859dc67 commit 8de9ea0

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/snowflake/AbstractSnowflakeConnector.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@
4242
import java.util.ArrayList;
4343
import java.util.List;
4444
import java.util.Properties;
45+
import java.util.function.Supplier;
4546
import javax.annotation.Nonnull;
4647
import javax.sql.DataSource;
47-
import org.apache.commons.lang3.StringUtils;
4848
import org.springframework.jdbc.core.JdbcTemplate;
4949
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
5050

@@ -196,16 +196,23 @@ private void setCurrentDatabase(@Nonnull String databaseName, @Nonnull JdbcTempl
196196
String currentDatabase =
197197
jdbcTemplate.queryForObject(String.format("USE DATABASE %s;", databaseName), String.class);
198198
if (currentDatabase == null) {
199-
List<String> dbNames =
200-
jdbcTemplate.query("SHOW DATABASES", (rs, rowNum) -> rs.getString("name"));
201-
throw new MetadataDumperUsageException(
202-
"Database name not found "
203-
+ databaseName
204-
+ ", use one of: "
205-
+ StringUtils.join(dbNames, ", "));
199+
Supplier<List<String>> showQuery =
200+
() -> jdbcTemplate.query("SHOW DATABASES", (rs, rowNum) -> rs.getString("name"));
201+
throw unrecognizedDatabase(databaseName, showQuery);
206202
}
207203
}
208204

205+
@Nonnull
206+
static MetadataDumperUsageException unrecognizedDatabase(
207+
@Nonnull String database, @Nonnull Supplier<List<String>> availableDatabases) {
208+
List<String> names = availableDatabases.get();
209+
String joinedNames = String.join(", ", names);
210+
String message =
211+
String.format("Database name not found %s, use one of: %s", database, joinedNames);
212+
213+
return new MetadataDumperUsageException(message);
214+
}
215+
209216
String sanitizeDatabaseName(@Nonnull String databaseName) throws MetadataDumperUsageException {
210217
int lengthWithQuotes = databaseName.length() + 2;
211218
int maxLength = 255;

dumper/app/src/test/java/com/google/edwmigration/dumper/application/dumper/connector/snowflake/AbstractSnowflakeConnectorTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
*/
1717
package com.google.edwmigration.dumper.application.dumper.connector.snowflake;
1818

19+
import static com.google.edwmigration.dumper.application.dumper.connector.snowflake.AbstractSnowflakeConnector.unrecognizedDatabase;
1920
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNotNull;
2022
import static org.junit.Assert.assertThrows;
2123
import static org.junit.Assert.assertTrue;
2224

@@ -26,6 +28,8 @@
2628
import com.google.edwmigration.dumper.application.dumper.connector.AbstractConnectorTest;
2729
import java.io.IOException;
2830
import java.util.ArrayList;
31+
import java.util.List;
32+
import java.util.function.Supplier;
2933
import org.junit.Assert;
3034
import org.junit.Test;
3135
import org.junit.experimental.theories.Theories;
@@ -127,6 +131,18 @@ public void validate_mixedPrivateKeyAndPassword_fail() throws IOException {
127131
"Private key authentication method can't be used together with user password"));
128132
}
129133

134+
@Test
135+
public void unrecognizedDatabase_success() {
136+
Supplier<List<String>> databases = () -> ImmutableList.of("SNOWFLAKE", "FIRSTDB", "SECONDDB");
137+
138+
String message = unrecognizedDatabase("WRONGNAMEDB", databases).getMessage();
139+
140+
assertNotNull(message);
141+
assertTrue(message, message.contains("WRONGNAMEDB"));
142+
assertTrue(message, message.contains("Database name not found"));
143+
assertTrue(message, message.contains("SNOWFLAKE, FIRSTDB, SECONDDB"));
144+
}
145+
130146
enum TestEnum {
131147
SomeValue
132148
}

0 commit comments

Comments
 (0)