Skip to content

Commit

Permalink
Merge pull request #646 from GoogleCloudPlatform/600-java-template-gc…
Browse files Browse the repository at this point in the history
…stospanner-empty-validateinput-method

fix: 600 java template gcstospanner empty validateinput method
  • Loading branch information
vanshaj-bhatia authored May 11, 2023
2 parents c67ae7d + ab5a7e8 commit 40a9835
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public GCSToSpanner(GCSToSpannerConfig config) {

public static GCSToSpanner of(String... args) {
GCSToSpannerConfig config = GCSToSpannerConfig.fromProperties(PropertyUtil.getProperties());
ValidationUtil.validateOrThrow(config);
LOGGER.info("Config loaded\n{}", config);
return new GCSToSpanner(config);
}
Expand Down Expand Up @@ -82,5 +81,8 @@ public void write(Dataset<Row> dataset) {
.save();
}

public void validateInput() {}
@Override
public void validateInput() {
ValidationUtil.validateOrThrow(config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.spark.sql.SaveMode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.ThrowingSupplier;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
Expand All @@ -59,15 +58,20 @@ void setUp() {
@Test
void runTemplateWithValidParameters() {
LOGGER.info("Running test: runTemplateWithValidParameters");
assertDoesNotThrow((ThrowingSupplier<GCSToSpanner>) GCSToSpanner::of);
GCSToSpannerConfig config = GCSToSpannerConfig.fromProperties(PropertyUtil.getProperties());
GCSToSpanner template = new GCSToSpanner(config);
assertDoesNotThrow(template::validateInput);
}

@ParameterizedTest
@MethodSource("requiredPropertyKeys")
void runTemplateWithMissingRequiredParameters(String propKey) {
LOGGER.info("Running test: runTemplateWithInvalidParameters");
PropertyUtil.getProperties().setProperty(propKey, "");
ValidationException exception = assertThrows(ValidationException.class, GCSToSpanner::of);
GCSToSpannerConfig config = GCSToSpannerConfig.fromProperties(PropertyUtil.getProperties());
GCSToSpanner template = new GCSToSpanner(config);
ValidationException exception =
assertThrows(ValidationException.class, template::validateInput);
assertEquals(1, exception.getViolations().size());
ConstraintViolation<?> violation = exception.getViolations().get(0);
assertEquals("must not be empty", violation.getMessage());
Expand All @@ -83,7 +87,10 @@ void runTemplateWithMissingPrimaryKey(String saveMode) {
LOGGER.info("Running test: runTemplateWithInvalidParameters");
PropertyUtil.getProperties().setProperty(GCS_SPANNER_OUTPUT_SAVE_MODE, saveMode);
PropertyUtil.getProperties().setProperty(GCS_SPANNER_OUTPUT_PRIMARY_KEY, "");
ValidationException exception = assertThrows(ValidationException.class, GCSToSpanner::of);
GCSToSpannerConfig config = GCSToSpannerConfig.fromProperties(PropertyUtil.getProperties());
GCSToSpanner template = new GCSToSpanner(config);
ValidationException exception =
assertThrows(ValidationException.class, template::validateInput);
assertEquals(1, exception.getViolations().size());
ConstraintViolation<?> violation = exception.getViolations().get(0);
assertEquals("primaryKey", violation.getPropertyPath().toString());
Expand Down

0 comments on commit 40a9835

Please sign in to comment.