diff --git a/core/build.gradle b/core/build.gradle index 36b58c519..58680ef0f 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -256,6 +256,7 @@ dependencies { compile deps['org.bouncycastle:bcpg-jdk15on'] testCompile deps['org.bouncycastle:bcpkix-jdk15on'] compile deps['org.bouncycastle:bcprov-jdk15on'] + testCompile deps['com.fasterxml.jackson.core:jackson-databind'] runtime deps['org.glassfish.jaxb:jaxb-runtime'] compile deps['org.hibernate:hibernate-core'] compile deps['org.joda:joda-money'] diff --git a/core/src/main/java/google/registry/tools/RegistryCli.java b/core/src/main/java/google/registry/tools/RegistryCli.java index b268bcaf2..7c2fa3678 100644 --- a/core/src/main/java/google/registry/tools/RegistryCli.java +++ b/core/src/main/java/google/registry/tools/RegistryCli.java @@ -68,8 +68,9 @@ final class RegistryCli implements AutoCloseable, CommandRunner { @Parameter( names = {"--sql_access_info"}, - description = "Name of a file containing space-separated SQL access info used when deploying " - + "Beam pipelines") + description = + "Name of a file containing space-separated SQL access info used when deploying " + + "Beam pipelines") private String sqlAccessInfoFile = null; // Do not make this final - compile-time constant inlining may interfere with JCommander. diff --git a/core/src/test/java/google/registry/beam/TestPipelineExtension.java b/core/src/test/java/google/registry/beam/TestPipelineExtension.java index ac498d090..94441733d 100644 --- a/core/src/test/java/google/registry/beam/TestPipelineExtension.java +++ b/core/src/test/java/google/registry/beam/TestPipelineExtension.java @@ -19,7 +19,7 @@ */ package google.registry.beam; -import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState; +import static com.google.common.base.Preconditions.checkState; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; @@ -60,9 +60,9 @@ import org.apache.beam.sdk.testing.ValidatesRunner; import org.apache.beam.sdk.transforms.SerializableFunction; import org.apache.beam.sdk.util.common.ReflectHelpers; import org.junit.experimental.categories.Category; -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; // NOTE: This file is copied from the Apache Beam distribution so that it can be locally modified to // support JUnit 5. @@ -71,10 +71,6 @@ import org.junit.runners.model.Statement; * A creator of test pipelines that can be used inside of tests that can be configured to run * locally or against a remote pipeline runner. * - *
It is recommended to tag hand-selected tests for this purpose using the {@link - * ValidatesRunner} {@link Category} annotation, as each test run against a pipeline runner will - * utilize resources of that pipeline runner. - * *
In order to run tests on a pipeline runner, the following conditions must be met: * *
See also the Testing documentation
* section.
*/
-public class TestPipelineExtension extends Pipeline implements TestRule {
+public class TestPipelineExtension extends Pipeline
+ implements BeforeEachCallback, AfterEachCallback {
private final PipelineOptions options;
@@ -287,50 +284,38 @@ public class TestPipelineExtension extends Pipeline implements TestRule {
}
@Override
- public Statement apply(final Statement statement, final Description description) {
- return new Statement() {
+ public void beforeEach(ExtensionContext context) throws Exception {
+ options.as(ApplicationNameOptions.class).setAppName(getAppName(context));
- private void setDeducedEnforcementLevel() {
- // if the enforcement level has not been set by the user do auto-inference
- if (!enforcement.isPresent()) {
+ // if the enforcement level has not been set by the user do auto-inference
+ if (!enforcement.isPresent()) {
+ final boolean isCrashingRunner = CrashingRunner.class.isAssignableFrom(options.getRunner());
- final boolean annotatedWithNeedsRunner =
- description.getAnnotations().stream()
- .filter(Annotations.Predicates.isAnnotationOfType(Category.class))
- .anyMatch(Annotations.Predicates.isCategoryOf(NeedsRunner.class, true));
+ checkState(
+ !isCrashingRunner,
+ "Cannot test using a [%s] runner. Please re-check your configuration.",
+ CrashingRunner.class.getSimpleName());
- final boolean crashingRunner = CrashingRunner.class.isAssignableFrom(options.getRunner());
+ enableAbandonedNodeEnforcement(true);
+ }
+ }
- checkState(
- !(annotatedWithNeedsRunner && crashingRunner),
- "The test was annotated with a [@%s] / [@%s] while the runner "
- + "was set to [%s]. Please re-check your configuration.",
- NeedsRunner.class.getSimpleName(),
- ValidatesRunner.class.getSimpleName(),
- CrashingRunner.class.getSimpleName());
+ @Override
+ public void afterEach(ExtensionContext context) throws Exception {
+ enforcement.get().afterUserCodeFinished();
+ }
- enableAbandonedNodeEnforcement(annotatedWithNeedsRunner || !crashingRunner);
- }
- }
-
- @Override
- public void evaluate() throws Throwable {
- options.as(ApplicationNameOptions.class).setAppName(getAppName(description));
-
- setDeducedEnforcementLevel();
-
- // statement.evaluate() essentially runs the user code contained in the unit test at hand.
- // Exceptions thrown during the execution of the user's test code will propagate here,
- // unless the user explicitly handles them with a "catch" clause in his code. If the
- // exception is handled by a user's "catch" clause, is does not interrupt the flow and
- // we move on to invoking the configured enforcements.
- // If the user does not handle a thrown exception, it will propagate here and interrupt
- // the flow, preventing the enforcement(s) from being activated.
- // The motivation for this is avoiding enforcements over faulty pipelines.
- statement.evaluate();
- enforcement.get().afterUserCodeFinished();
- }
- };
+ /** Returns the class + method name of the test. */
+ private String getAppName(ExtensionContext context) {
+ String methodName = context.getRequiredTestMethod().getName();
+ Class> testClass = context.getRequiredTestClass();
+ if (testClass.isMemberClass()) {
+ return String.format(
+ "%s$%s-%s",
+ testClass.getEnclosingClass().getSimpleName(), testClass.getSimpleName(), methodName);
+ } else {
+ return String.format("%s-%s", testClass.getSimpleName(), methodName);
+ }
}
/**
@@ -483,19 +468,6 @@ public class TestPipelineExtension extends Pipeline implements TestRule {
}
}
- /** Returns the class + method name of the test. */
- private String getAppName(Description description) {
- String methodName = description.getMethodName();
- Class> testClass = description.getTestClass();
- if (testClass.isMemberClass()) {
- return String.format(
- "%s$%s-%s",
- testClass.getEnclosingClass().getSimpleName(), testClass.getSimpleName(), methodName);
- } else {
- return String.format("%s-%s", testClass.getSimpleName(), methodName);
- }
- }
-
/**
* Verifies all {{@link PAssert PAsserts}} in the pipeline have been executed and were successful.
*
diff --git a/core/src/test/java/google/registry/beam/initsql/CommitLogTransformsTest.java b/core/src/test/java/google/registry/beam/initsql/CommitLogTransformsTest.java
index e1b75d6b8..a9a1f8435 100644
--- a/core/src/test/java/google/registry/beam/initsql/CommitLogTransformsTest.java
+++ b/core/src/test/java/google/registry/beam/initsql/CommitLogTransformsTest.java
@@ -31,9 +31,10 @@ import google.registry.testing.InjectRule;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
+import java.nio.file.Files;
+import java.nio.file.Path;
import org.apache.beam.sdk.coders.StringUtf8Coder;
import org.apache.beam.sdk.io.fs.MatchResult.Metadata;
-import org.apache.beam.sdk.testing.NeedsRunner;
import org.apache.beam.sdk.testing.PAssert;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.transforms.DoFn;
@@ -41,27 +42,25 @@ import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.values.PCollection;
import org.joda.time.DateTime;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-import org.junit.rules.TemporaryFolder;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.junit.jupiter.api.io.TempDir;
/** Unit tests for {@link Transforms} related to loading CommitLogs. */
-@RunWith(JUnit4.class)
-public class CommitLogTransformsTest implements Serializable {
+class CommitLogTransformsTest implements Serializable {
private static final DateTime START_TIME = DateTime.parse("2000-01-01T00:00:00.0Z");
- @Rule public final transient TemporaryFolder temporaryFolder = new TemporaryFolder();
+ @SuppressWarnings("WeakerAccess")
+ @TempDir
+ transient Path tmpDir;
- @Rule public final transient InjectRule injectRule = new InjectRule();
+ @RegisterExtension final transient InjectRule injectRule = new InjectRule();
- @Rule
- public final transient TestPipelineExtension pipeline =
+ @RegisterExtension
+ final transient TestPipelineExtension testPipeline =
TestPipelineExtension.create().enableAbandonedNodeEnforcement(true);
private FakeClock fakeClock;
@@ -74,8 +73,8 @@ public class CommitLogTransformsTest implements Serializable {
private transient ContactResource contact;
private transient DomainBase domain;
- @Before
- public void beforeEach() throws Exception {
+ @BeforeEach
+ void beforeEach() throws Exception {
fakeClock = new FakeClock(START_TIME);
store = new BackupTestStore(fakeClock);
injectRule.setStaticField(Ofy.class, "clock", fakeClock);
@@ -91,12 +90,12 @@ public class CommitLogTransformsTest implements Serializable {
contact = (ContactResource) store.loadAsOfyEntity(contact);
domain = (DomainBase) store.loadAsOfyEntity(domain);
- commitLogsDir = temporaryFolder.newFolder();
+ commitLogsDir = Files.createDirectory(tmpDir.resolve("commit_logs")).toFile();
firstCommitLogFile = store.saveCommitLogs(commitLogsDir.getAbsolutePath());
}
- @After
- public void afterEach() throws Exception {
+ @AfterEach
+ void afterEach() throws Exception {
if (store != null) {
store.close();
store = null;
@@ -104,10 +103,9 @@ public class CommitLogTransformsTest implements Serializable {
}
@Test
- @Category(NeedsRunner.class)
- public void getCommitLogFilePatterns() {
+ void getCommitLogFilePatterns() {
PCollection This class implements {@link Serializable} so that test {@link DoFn} classes may be inlined.
*/
-@RunWith(JUnit4.class)
-public class ExportloadingTransformsTest implements Serializable {
+class ExportloadingTransformsTest implements Serializable {
+
private static final DateTime START_TIME = DateTime.parse("2000-01-01T00:00:00.0Z");
private static final ImmutableList