Skip to content

Commit 6159422

Browse files
authored
Group exception-building methods together (google#969)
* Add a snowflake-specific usage exception * Improve coverage for Lite
1 parent 40e70f1 commit 6159422

File tree

8 files changed

+136
-48
lines changed

8 files changed

+136
-48
lines changed

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,7 @@ public final Handle open(@Nonnull ConnectorArguments arguments)
104104
@Override
105105
public final void validate(@Nonnull ConnectorArguments arguments) {
106106
if (arguments.isPasswordFlagProvided() && arguments.isPrivateKeyFileProvided()) {
107-
String inconsistentAuth =
108-
"Private key authentication method can't be used together with user password. "
109-
+ "If the private key file is encrypted, please use --"
110-
+ ConnectorArguments.OPT_PRIVATE_KEY_PASSWORD
111-
+ " to specify the key password.";
112-
throw new MetadataDumperUsageException(inconsistentAuth);
107+
throw SnowflakeUsageException.mixedAuthentication();
113108
}
114109
validateForConnector(arguments);
115110
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
*/
1717
package com.google.edwmigration.dumper.application.dumper.connector.snowflake;
1818

19-
import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_ASSESSMENT;
2019
import static com.google.edwmigration.dumper.application.dumper.connector.snowflake.SnowflakeInput.USAGE_ONLY_SOURCE;
2120

2221
import com.google.auto.service.AutoService;
2322
import com.google.edwmigration.dumper.application.dumper.ConnectorArguments;
24-
import com.google.edwmigration.dumper.application.dumper.MetadataDumperUsageException;
2523
import com.google.edwmigration.dumper.application.dumper.connector.Connector;
2624
import java.io.IOException;
2725
import javax.annotation.Nonnull;
@@ -50,8 +48,7 @@ public void printHelp(Appendable out) throws IOException {
5048
@Override
5149
public final void validateForConnector(ConnectorArguments arguments) {
5250
if (arguments.isAssessment()) {
53-
String message = String.format("The --%s flag is not supported.", OPT_ASSESSMENT);
54-
throw new MetadataDumperUsageException(message);
51+
throw SnowflakeUsageException.unsupportedAssessment();
5552
}
5653
}
5754
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
*/
1717
package com.google.edwmigration.dumper.application.dumper.connector.snowflake;
1818

19-
import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_ASSESSMENT;
2019
import static com.google.edwmigration.dumper.application.dumper.connector.snowflake.SnowflakeInput.SCHEMA_ONLY_SOURCE;
2120

2221
import com.google.auto.service.AutoService;
2322
import com.google.edwmigration.dumper.application.dumper.ConnectorArguments;
24-
import com.google.edwmigration.dumper.application.dumper.MetadataDumperUsageException;
2523
import com.google.edwmigration.dumper.application.dumper.connector.Connector;
2624
import java.io.IOException;
2725
import javax.annotation.Nonnull;
@@ -50,8 +48,7 @@ public void printHelp(@Nonnull Appendable out) throws IOException {
5048
@Override
5149
public final void validateForConnector(ConnectorArguments arguments) {
5250
if (arguments.isAssessment()) {
53-
String message = String.format("The --%s flag is not supported.", OPT_ASSESSMENT);
54-
throw new MetadataDumperUsageException(message);
51+
throw SnowflakeUsageException.unsupportedAssessment();
5552
}
5653
}
5754
}

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.google.auto.service.AutoService;
2020
import com.google.edwmigration.dumper.application.dumper.ConnectorArguments;
21-
import com.google.edwmigration.dumper.application.dumper.MetadataDumperUsageException;
2221
import com.google.edwmigration.dumper.application.dumper.annotations.RespectsArgumentAssessment;
2322
import com.google.edwmigration.dumper.application.dumper.connector.Connector;
2423
import com.google.edwmigration.dumper.application.dumper.task.DumpMetadataTask;
@@ -67,16 +66,10 @@ public final void addTasksTo(List<? super Task<?>> out, ConnectorArguments argum
6766
@Override
6867
protected void validateForConnector(ConnectorArguments arguments) {
6968
if (!arguments.isAssessment()) {
70-
throw noAssessmentException();
69+
throw SnowflakeUsageException.missingAssessmentException(NAME);
70+
}
71+
if (!arguments.getDatabases().isEmpty()) {
72+
throw SnowflakeUsageException.unsupportedFilter();
7173
}
72-
}
73-
74-
private static MetadataDumperUsageException noAssessmentException() {
75-
String message =
76-
String.format(
77-
"The %s connector only supports extraction for Assessment."
78-
+ " Provide the '--%s' flag to use this connector.",
79-
NAME, ConnectorArguments.OPT_ASSESSMENT);
80-
return new MetadataDumperUsageException(message);
8174
}
8275
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private TaskDescription(
105105
@Override
106106
protected final void validateForConnector(@Nonnull ConnectorArguments arguments) {
107107
if (arguments.isAssessment() && arguments.hasQueryLogEarliestTimestamp()) {
108-
throw unsupportedOption(ConnectorArguments.OPT_QUERY_LOG_EARLIEST_TIMESTAMP);
108+
throw SnowflakeUsageException.unsupportedEarliestTimestamp();
109109
}
110110
}
111111

@@ -354,13 +354,6 @@ private static void addJdbcTask(
354354
out.add(new JdbcSelectTask(file, query, task.taskCategory).withHeaderClass(task.headerClass));
355355
}
356356

357-
private static MetadataDumperUsageException unsupportedOption(String option) {
358-
String assessment = ConnectorArguments.OPT_ASSESSMENT;
359-
String message =
360-
String.format("Unsupported option used with --%s: please remove --%s", assessment, option);
361-
return new MetadataDumperUsageException(message);
362-
}
363-
364357
static String overrideableQuery(
365358
@Nullable String override, @Nonnull String defaultSql, @Nonnull String whereField) {
366359
String start = whereField + " >= to_timestamp_ltz('%s')";

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.common.annotations.VisibleForTesting;
2424
import com.google.common.collect.ImmutableList;
2525
import com.google.edwmigration.dumper.application.dumper.ConnectorArguments;
26-
import com.google.edwmigration.dumper.application.dumper.MetadataDumperUsageException;
2726
import com.google.edwmigration.dumper.application.dumper.annotations.RespectsArgumentAssessment;
2827
import com.google.edwmigration.dumper.application.dumper.annotations.RespectsArgumentDatabaseForConnection;
2928
import com.google.edwmigration.dumper.application.dumper.annotations.RespectsArgumentDatabasePredicate;
@@ -131,15 +130,7 @@ public Iterable<ConnectorProperty> getPropertyConstants() {
131130
protected void validateForConnector(@Nonnull ConnectorArguments arguments) {
132131
boolean hasDatabases = !arguments.getDatabases().isEmpty();
133132
if (arguments.isAssessment() && hasDatabases) {
134-
String unsupportedFilter =
135-
"Trying to filter by database with the --"
136-
+ ConnectorArguments.OPT_ASSESSMENT
137-
+ " flag. This is unsupported in Assessment. Remove either the --"
138-
+ ConnectorArguments.OPT_ASSESSMENT
139-
+ " or the --"
140-
+ ConnectorArguments.OPT_DATABASE
141-
+ " flag.";
142-
throw new MetadataDumperUsageException(unsupportedFilter);
133+
throw SnowflakeUsageException.unsupportedFilter();
143134
}
144135
}
145136

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2022-2025 Google LLC
3+
* Copyright 2013-2021 CompilerWorks
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.google.edwmigration.dumper.application.dumper.connector.snowflake;
18+
19+
import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_ASSESSMENT;
20+
import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_DATABASE;
21+
import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_PRIVATE_KEY_PASSWORD;
22+
import static com.google.edwmigration.dumper.application.dumper.ConnectorArguments.OPT_QUERY_LOG_EARLIEST_TIMESTAMP;
23+
import static java.util.stream.Collectors.joining;
24+
25+
import com.google.common.collect.ImmutableList;
26+
import com.google.edwmigration.dumper.application.dumper.MetadataDumperUsageException;
27+
import java.util.stream.Stream;
28+
import javax.annotation.Nonnull;
29+
import javax.annotation.ParametersAreNonnullByDefault;
30+
31+
@ParametersAreNonnullByDefault
32+
class SnowflakeUsageException extends MetadataDumperUsageException {
33+
34+
@Override
35+
@Nonnull
36+
public final ImmutableList<String> getMessages() {
37+
return ImmutableList.of();
38+
}
39+
40+
private SnowflakeUsageException(String msg) {
41+
super(msg);
42+
}
43+
44+
@Override
45+
@Nonnull
46+
public String toString() {
47+
String className = getClass().getName();
48+
return String.format("%s: %s", className, getLocalizedMessage());
49+
}
50+
51+
@Nonnull
52+
static SnowflakeUsageException missingAssessmentException(String connector) {
53+
Stream<String> messages =
54+
Stream.of(
55+
"The " + connector + " connector only supports extraction for assessment.",
56+
"Provide the '--" + OPT_ASSESSMENT + " flag to use this connector.");
57+
return new SnowflakeUsageException(messages.collect(joining(" ")));
58+
}
59+
60+
static SnowflakeUsageException mixedAuthentication() {
61+
Stream<String> messages =
62+
Stream.of(
63+
"Private key authentication method can't be used together with user password.",
64+
"If the private key file is encrypted, please use --"
65+
+ OPT_PRIVATE_KEY_PASSWORD
66+
+ " to specify the key password.");
67+
return new SnowflakeUsageException(messages.collect(joining(" ")));
68+
}
69+
70+
@Nonnull
71+
static SnowflakeUsageException unsupportedAssessment() {
72+
String message = String.format("The --%s flag is not supported.", OPT_ASSESSMENT);
73+
return new SnowflakeUsageException(message);
74+
}
75+
76+
@Nonnull
77+
static SnowflakeUsageException unsupportedEarliestTimestamp() {
78+
String message =
79+
String.format(
80+
"Unsupported option used with --%s: please remove --%s",
81+
OPT_ASSESSMENT, OPT_QUERY_LOG_EARLIEST_TIMESTAMP);
82+
return new SnowflakeUsageException(message);
83+
}
84+
85+
@Nonnull
86+
static SnowflakeUsageException unsupportedFilter() {
87+
Stream<String> messages =
88+
Stream.of(
89+
"Trying to filter by database with the --" + OPT_ASSESSMENT + " flag.",
90+
"This is unsupported in Assessment.",
91+
"Remove either the --" + OPT_ASSESSMENT + " or the --" + OPT_DATABASE + " flag.");
92+
return new SnowflakeUsageException(messages.collect(joining(" ")));
93+
}
94+
}

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,51 @@
1717
package com.google.edwmigration.dumper.application.dumper.connector.snowflake;
1818

1919
import static org.junit.Assert.assertThrows;
20+
import static org.junit.Assert.assertTrue;
2021

2122
import com.google.common.collect.ImmutableList;
2223
import com.google.edwmigration.dumper.application.dumper.ConnectorArguments;
23-
import com.google.edwmigration.dumper.application.dumper.MetadataDumperUsageException;
2424
import org.junit.Test;
2525
import org.junit.runner.RunWith;
2626
import org.junit.runners.JUnit4;
2727

2828
@RunWith(JUnit4.class)
2929
public class SnowflakeLiteConnectorTest {
3030

31+
final SnowflakeLiteConnector connector = new SnowflakeLiteConnector();
32+
33+
@Test
34+
public void getDefaultFileName_success() {
35+
36+
String name = connector.getDefaultFileName(false, null);
37+
38+
assertTrue(name, name.contains("snowflake"));
39+
assertTrue(name, name.contains("lite"));
40+
}
41+
42+
@Test
43+
public void validate_databaseFlag_throwsException() {
44+
ImmutableList<String> list =
45+
ImmutableList.of(
46+
"--connector", "snowflake-lite", "--assessment", "--database", "SNOWFLAKE");
47+
ConnectorArguments arguments = ConnectorArguments.create(list);
48+
49+
assertThrows(SnowflakeUsageException.class, () -> connector.validate(arguments));
50+
}
51+
3152
@Test
3253
public void validate_noAssessmentFlag_throwsUsageException() {
33-
ConnectorArguments noFlagArguments =
34-
ConnectorArguments.create(ImmutableList.of("--connector", "snowflake-lite"));
35-
SnowflakeLiteConnector connector = new SnowflakeLiteConnector();
54+
ImmutableList<String> list = ImmutableList.of("--connector", "snowflake-lite");
55+
ConnectorArguments arguments = ConnectorArguments.create(list);
56+
57+
assertThrows(SnowflakeUsageException.class, () -> connector.validate(arguments));
58+
}
59+
60+
@Test
61+
public void validate_correctArguments_noException() {
62+
ImmutableList<String> list = ImmutableList.of("--connector", "snowflake-lite", "--assessment");
63+
ConnectorArguments arguments = ConnectorArguments.create(list);
3664

37-
assertThrows(MetadataDumperUsageException.class, () -> connector.validate(noFlagArguments));
65+
connector.validate(arguments);
3866
}
3967
}

0 commit comments

Comments
 (0)