mirror of
https://github.com/google/nomulus.git
synced 2025-06-11 15:04:46 +02:00
Upgrade TestPipeline extension from JUnit 4 to 5
This commit is contained in:
parent
efc1c3640b
commit
5f6ff4c924
10 changed files with 209 additions and 243 deletions
|
@ -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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>In order to run tests on a pipeline runner, the following conditions must be met:
|
||||
*
|
||||
* <ul>
|
||||
|
@ -111,7 +107,8 @@ import org.junit.runners.model.Statement;
|
|||
* <p>See also the <a href="https://beam.apache.org/contribute/testing/">Testing</a> 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.
|
||||
*
|
||||
|
|
|
@ -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<String> patterns =
|
||||
pipeline.apply(
|
||||
testPipeline.apply(
|
||||
"Get CommitLog file patterns",
|
||||
Transforms.getCommitLogFilePatterns(commitLogsDir.getAbsolutePath()));
|
||||
|
||||
|
@ -116,14 +114,13 @@ public class CommitLogTransformsTest implements Serializable {
|
|||
|
||||
PAssert.that(patterns).containsInAnyOrder(expectedPatterns);
|
||||
|
||||
pipeline.run();
|
||||
testPipeline.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Category(NeedsRunner.class)
|
||||
public void getFilesByPatterns() {
|
||||
void getFilesByPatterns() {
|
||||
PCollection<Metadata> fileMetas =
|
||||
pipeline
|
||||
testPipeline
|
||||
.apply(
|
||||
"File patterns to metadata",
|
||||
Create.of(commitLogsDir.getAbsolutePath() + "/commit_diff_until_*")
|
||||
|
@ -148,12 +145,11 @@ public class CommitLogTransformsTest implements Serializable {
|
|||
|
||||
PAssert.that(fileNames).containsInAnyOrder(expectedFilenames);
|
||||
|
||||
pipeline.run();
|
||||
testPipeline.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Category(NeedsRunner.class)
|
||||
public void filterCommitLogsByTime() throws IOException {
|
||||
void filterCommitLogsByTime() throws IOException {
|
||||
ImmutableList<String> commitLogFilenames =
|
||||
ImmutableList.of(
|
||||
"commit_diff_until_2000-01-01T00:00:00.000Z",
|
||||
|
@ -162,16 +158,15 @@ public class CommitLogTransformsTest implements Serializable {
|
|||
"commit_diff_until_2000-01-01T00:00:00.003Z",
|
||||
"commit_diff_until_2000-01-01T00:00:00.004Z");
|
||||
|
||||
File commitLogDir = temporaryFolder.newFolder();
|
||||
for (String name : commitLogFilenames) {
|
||||
new File(commitLogDir, name).createNewFile();
|
||||
new File(commitLogsDir, name).createNewFile();
|
||||
}
|
||||
|
||||
PCollection<String> filteredFilenames =
|
||||
pipeline
|
||||
testPipeline
|
||||
.apply(
|
||||
"Get commitlog file patterns",
|
||||
Transforms.getCommitLogFilePatterns(commitLogDir.getAbsolutePath()))
|
||||
Transforms.getCommitLogFilePatterns(commitLogsDir.getAbsolutePath()))
|
||||
.apply("Find commitlog files", Transforms.getFilesByPatterns())
|
||||
.apply(
|
||||
"Filtered by Time",
|
||||
|
@ -193,14 +188,13 @@ public class CommitLogTransformsTest implements Serializable {
|
|||
"commit_diff_until_2000-01-01T00:00:00.001Z",
|
||||
"commit_diff_until_2000-01-01T00:00:00.002Z");
|
||||
|
||||
pipeline.run();
|
||||
testPipeline.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Category(NeedsRunner.class)
|
||||
public void loadOneCommitLogFile() {
|
||||
void loadOneCommitLogFile() {
|
||||
PCollection<VersionedEntity> entities =
|
||||
pipeline
|
||||
testPipeline
|
||||
.apply(
|
||||
"Get CommitLog file patterns",
|
||||
Transforms.getCommitLogFilePatterns(commitLogsDir.getAbsolutePath()))
|
||||
|
@ -215,14 +209,13 @@ public class CommitLogTransformsTest implements Serializable {
|
|||
KV.of(fakeClock.nowUtc().getMillis() - 1, store.loadAsDatastoreEntity(contact)),
|
||||
KV.of(fakeClock.nowUtc().getMillis() - 1, store.loadAsDatastoreEntity(domain)));
|
||||
|
||||
pipeline.run();
|
||||
testPipeline.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Category(NeedsRunner.class)
|
||||
public void loadOneCommitLogFile_filterByKind() {
|
||||
void loadOneCommitLogFile_filterByKind() {
|
||||
PCollection<VersionedEntity> entities =
|
||||
pipeline
|
||||
testPipeline
|
||||
.apply(
|
||||
"Get CommitLog file patterns",
|
||||
Transforms.getCommitLogFilePatterns(commitLogsDir.getAbsolutePath()))
|
||||
|
@ -235,6 +228,6 @@ public class CommitLogTransformsTest implements Serializable {
|
|||
KV.of(fakeClock.nowUtc().getMillis() - 2, store.loadAsDatastoreEntity(registry)),
|
||||
KV.of(fakeClock.nowUtc().getMillis() - 1, store.loadAsDatastoreEntity(contact)));
|
||||
|
||||
pipeline.run();
|
||||
testPipeline.run();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,10 +31,10 @@ import google.registry.testing.FakeClock;
|
|||
import google.registry.testing.InjectRule;
|
||||
import java.io.File;
|
||||
import java.io.Serializable;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
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;
|
||||
|
@ -42,22 +42,19 @@ 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 Datastore exports.
|
||||
*
|
||||
* <p>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<Class<?>> ALL_KINDS =
|
||||
|
@ -65,12 +62,14 @@ public class ExportloadingTransformsTest implements Serializable {
|
|||
private static final ImmutableSet<String> ALL_KIND_STRS =
|
||||
ALL_KINDS.stream().map(Key::getKind).collect(ImmutableSet.toImmutableSet());
|
||||
|
||||
@Rule public final transient TemporaryFolder exportRootDir = 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;
|
||||
|
@ -82,8 +81,8 @@ public class ExportloadingTransformsTest 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);
|
||||
|
@ -100,12 +99,11 @@ public class ExportloadingTransformsTest implements Serializable {
|
|||
contact = (ContactResource) store.loadAsOfyEntity(contact);
|
||||
domain = (DomainBase) store.loadAsOfyEntity(domain);
|
||||
|
||||
exportDir =
|
||||
store.export(exportRootDir.getRoot().getAbsolutePath(), ALL_KINDS, Collections.EMPTY_SET);
|
||||
exportDir = store.export(tmpDir.toAbsolutePath().toString(), ALL_KINDS, Collections.EMPTY_SET);
|
||||
}
|
||||
|
||||
@After
|
||||
public void afterEach() throws Exception {
|
||||
@AfterEach
|
||||
void afterEach() throws Exception {
|
||||
if (store != null) {
|
||||
store.close();
|
||||
store = null;
|
||||
|
@ -113,10 +111,9 @@ public class ExportloadingTransformsTest implements Serializable {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Category(NeedsRunner.class)
|
||||
public void getExportFilePatterns() {
|
||||
void getExportFilePatterns() {
|
||||
PCollection<String> patterns =
|
||||
pipeline.apply(
|
||||
testPipeline.apply(
|
||||
"Get Datastore file patterns",
|
||||
Transforms.getDatastoreExportFilePatterns(exportDir.getAbsolutePath(), ALL_KIND_STRS));
|
||||
|
||||
|
@ -128,14 +125,13 @@ public class ExportloadingTransformsTest implements Serializable {
|
|||
|
||||
PAssert.that(patterns).containsInAnyOrder(expectedPatterns);
|
||||
|
||||
pipeline.run();
|
||||
testPipeline.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Category(NeedsRunner.class)
|
||||
public void getFilesByPatterns() {
|
||||
void getFilesByPatterns() {
|
||||
PCollection<Metadata> fileMetas =
|
||||
pipeline
|
||||
testPipeline
|
||||
.apply(
|
||||
"File patterns to metadata",
|
||||
Create.of(
|
||||
|
@ -167,14 +163,13 @@ public class ExportloadingTransformsTest implements Serializable {
|
|||
|
||||
PAssert.that(fileNames).containsInAnyOrder(expectedFilenames);
|
||||
|
||||
pipeline.run();
|
||||
testPipeline.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Category(NeedsRunner.class)
|
||||
public void loadDataFromFiles() {
|
||||
void loadDataFromFiles() {
|
||||
PCollection<VersionedEntity> entities =
|
||||
pipeline
|
||||
testPipeline
|
||||
.apply(
|
||||
"Get Datastore file patterns",
|
||||
Transforms.getDatastoreExportFilePatterns(
|
||||
|
@ -188,6 +183,6 @@ public class ExportloadingTransformsTest implements Serializable {
|
|||
KV.of(Transforms.EXPORT_ENTITY_TIME_STAMP, store.loadAsDatastoreEntity(contact)),
|
||||
KV.of(Transforms.EXPORT_ENTITY_TIME_STAMP, store.loadAsDatastoreEntity(domain)));
|
||||
|
||||
pipeline.run();
|
||||
testPipeline.run();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,17 +32,15 @@ import google.registry.model.registry.Registry;
|
|||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.InjectRule;
|
||||
import java.io.File;
|
||||
import org.apache.beam.sdk.testing.NeedsRunner;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import org.apache.beam.sdk.values.KV;
|
||||
import org.apache.beam.sdk.values.PCollectionTuple;
|
||||
import org.joda.time.DateTime;
|
||||
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.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
/**
|
||||
* Unit test for {@link Transforms#loadDatastoreSnapshot}.
|
||||
|
@ -71,8 +69,8 @@ import org.junit.runners.JUnit4;
|
|||
* <li>Deletes are properly handled.
|
||||
* </ul>
|
||||
*/
|
||||
@RunWith(JUnit4.class)
|
||||
public class LoadDatastoreSnapshotTest {
|
||||
class LoadDatastoreSnapshotTest {
|
||||
|
||||
private static final DateTime START_TIME = DateTime.parse("2000-01-01T00:00:00.0Z");
|
||||
|
||||
private static final ImmutableList<Class<?>> ALL_KINDS =
|
||||
|
@ -80,12 +78,14 @@ public class LoadDatastoreSnapshotTest {
|
|||
private static final ImmutableSet<String> ALL_KIND_STRS =
|
||||
ALL_KINDS.stream().map(Key::getKind).collect(ImmutableSet.toImmutableSet());
|
||||
|
||||
@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;
|
||||
|
@ -102,14 +102,14 @@ public class LoadDatastoreSnapshotTest {
|
|||
private transient DateTime contactLastUpdateTime;
|
||||
private transient DateTime domainLastUpdateTime;
|
||||
|
||||
@Before
|
||||
public void beforeEach() throws Exception {
|
||||
@BeforeEach
|
||||
void beforeEach() throws Exception {
|
||||
fakeClock = new FakeClock(START_TIME);
|
||||
try (BackupTestStore store = new BackupTestStore(fakeClock)) {
|
||||
injectRule.setStaticField(Ofy.class, "clock", fakeClock);
|
||||
|
||||
exportRootDir = temporaryFolder.newFolder();
|
||||
commitLogsDir = temporaryFolder.newFolder();
|
||||
exportRootDir = Files.createDirectory(tmpDir.resolve("export_root")).toFile();
|
||||
commitLogsDir = Files.createDirectory(tmpDir.resolve("commit_logs")).toFile();
|
||||
|
||||
Registry registry = newRegistry("tld1", "TLD1");
|
||||
ContactResource fillerContact = newContactResource("contact_filler");
|
||||
|
@ -154,10 +154,9 @@ public class LoadDatastoreSnapshotTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Category(NeedsRunner.class)
|
||||
public void loadDatastoreSnapshot() {
|
||||
void loadDatastoreSnapshot() {
|
||||
PCollectionTuple snapshot =
|
||||
pipeline.apply(
|
||||
testPipeline.apply(
|
||||
Transforms.loadDatastoreSnapshot(
|
||||
exportDir.getAbsolutePath(),
|
||||
commitLogsDir.getAbsolutePath(),
|
||||
|
@ -173,6 +172,6 @@ public class LoadDatastoreSnapshotTest {
|
|||
InitSqlTestUtils.assertContainsExactlyElementsIn(
|
||||
snapshot.get(Transforms.createTagForKind("ContactResource")),
|
||||
KV.of(contactLastUpdateTime.getMillis(), dsContact));
|
||||
pipeline.run();
|
||||
testPipeline.run();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,46 +34,48 @@ import google.registry.testing.InjectRule;
|
|||
import java.io.File;
|
||||
import java.io.PrintStream;
|
||||
import java.io.Serializable;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.stream.Collectors;
|
||||
import org.apache.beam.sdk.testing.NeedsRunner;
|
||||
import org.apache.beam.sdk.transforms.Create;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.experimental.categories.Category;
|
||||
import org.junit.rules.RuleChain;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
/** Unit test for {@link Transforms#writeToSql}. */
|
||||
public class WriteToSqlTest implements Serializable {
|
||||
class WriteToSqlTest implements Serializable {
|
||||
|
||||
private static final DateTime START_TIME = DateTime.parse("2000-01-01T00:00:00.0Z");
|
||||
|
||||
private final FakeClock fakeClock = new FakeClock(START_TIME);
|
||||
|
||||
@Rule public final transient InjectRule injectRule = new InjectRule();
|
||||
@RegisterExtension final transient InjectRule injectRule = new InjectRule();
|
||||
|
||||
// For use in the RuleChain below. Saves a reference to retrieve Database connection config.
|
||||
public final transient JpaIntegrationTestRule database =
|
||||
@RegisterExtension
|
||||
final transient JpaIntegrationTestRule database =
|
||||
new JpaTestRules.Builder().withClock(fakeClock).buildIntegrationTestRule();
|
||||
|
||||
@Rule
|
||||
public final transient RuleChain jpaRules =
|
||||
RuleChain.outerRule(new DatastoreEntityExtension()).around(database);
|
||||
@RegisterExtension
|
||||
@Order(value = 1)
|
||||
final transient DatastoreEntityExtension datastore = new DatastoreEntityExtension();
|
||||
|
||||
@Rule public transient TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
@TempDir
|
||||
transient Path tmpDir;
|
||||
|
||||
@Rule
|
||||
public final transient TestPipelineExtension pipeline =
|
||||
@RegisterExtension
|
||||
final transient TestPipelineExtension testPipeline =
|
||||
TestPipelineExtension.create().enableAbandonedNodeEnforcement(true);
|
||||
|
||||
private ImmutableList<Entity> contacts;
|
||||
|
||||
private File credentialFile;
|
||||
|
||||
@Before
|
||||
public void beforeEach() throws Exception {
|
||||
@BeforeEach
|
||||
void beforeEach() throws Exception {
|
||||
try (BackupTestStore store = new BackupTestStore(fakeClock)) {
|
||||
injectRule.setStaticField(Ofy.class, "clock", fakeClock);
|
||||
|
||||
|
@ -91,7 +93,7 @@ public class WriteToSqlTest implements Serializable {
|
|||
}
|
||||
contacts = builder.build();
|
||||
}
|
||||
credentialFile = temporaryFolder.newFile();
|
||||
credentialFile = Files.createFile(tmpDir.resolve("credential.dat")).toFile();
|
||||
new PrintStream(credentialFile)
|
||||
.printf(
|
||||
"%s %s %s",
|
||||
|
@ -102,9 +104,8 @@ public class WriteToSqlTest implements Serializable {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Category(NeedsRunner.class)
|
||||
public void writeToSql_twoWriters() {
|
||||
pipeline
|
||||
void writeToSql_twoWriters() {
|
||||
testPipeline
|
||||
.apply(
|
||||
Create.of(
|
||||
contacts.stream()
|
||||
|
@ -121,7 +122,7 @@ public class WriteToSqlTest implements Serializable {
|
|||
.beamJpaModule(new BeamJpaModule(credentialFile.getAbsolutePath()))
|
||||
.build()
|
||||
.localDbJpaTransactionManager()));
|
||||
pipeline.run().waitUntilFinish();
|
||||
testPipeline.run().waitUntilFinish();
|
||||
|
||||
ImmutableList<?> sqlContacts = jpaTm().transact(() -> jpaTm().loadAll(ContactResource.class));
|
||||
// TODO(weiminyu): compare load entities with originals. Note: lastUpdateTimes won't match by
|
||||
|
|
|
@ -24,6 +24,8 @@ import google.registry.util.GoogleCredentialsBundle;
|
|||
import google.registry.util.ResourceUtils;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Map.Entry;
|
||||
|
@ -33,47 +35,46 @@ import org.apache.beam.sdk.options.PipelineOptionsFactory;
|
|||
import org.apache.beam.sdk.options.ValueProvider.StaticValueProvider;
|
||||
import org.apache.beam.sdk.transforms.Create;
|
||||
import org.apache.beam.sdk.values.PCollection;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
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 InvoicingPipeline}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class InvoicingPipelineTest {
|
||||
class InvoicingPipelineTest {
|
||||
|
||||
private static PipelineOptions pipelineOptions;
|
||||
|
||||
@BeforeClass
|
||||
public static void initializePipelineOptions() {
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
pipelineOptions = PipelineOptionsFactory.create();
|
||||
pipelineOptions.setRunner(DirectRunner.class);
|
||||
}
|
||||
|
||||
@Rule
|
||||
public final transient TestPipelineExtension p =
|
||||
@RegisterExtension
|
||||
final transient TestPipelineExtension testPipeline =
|
||||
TestPipelineExtension.fromOptions(pipelineOptions);
|
||||
|
||||
@Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
@TempDir
|
||||
transient Path tmpDir;
|
||||
|
||||
private InvoicingPipeline invoicingPipeline;
|
||||
|
||||
@Before
|
||||
public void initializePipeline() throws IOException {
|
||||
File beamTempFolder = tempFolder.newFolder();
|
||||
String beamTempFolderPath = beamTempFolder.getAbsolutePath();
|
||||
invoicingPipeline = new InvoicingPipeline(
|
||||
"test-project",
|
||||
beamTempFolderPath,
|
||||
beamTempFolderPath + "/templates/invoicing",
|
||||
beamTempFolderPath + "/staging",
|
||||
tempFolder.getRoot().getAbsolutePath(),
|
||||
"REG-INV",
|
||||
GoogleCredentialsBundle.create(GoogleCredentials.create(null))
|
||||
);
|
||||
@BeforeEach
|
||||
void beforeEach() throws IOException {
|
||||
String beamTempFolder =
|
||||
Files.createDirectory(tmpDir.resolve("beam_temp")).toAbsolutePath().toString();
|
||||
invoicingPipeline =
|
||||
new InvoicingPipeline(
|
||||
"test-project",
|
||||
beamTempFolder,
|
||||
beamTempFolder + "/templates/invoicing",
|
||||
beamTempFolder + "/staging",
|
||||
tmpDir.toAbsolutePath().toString(),
|
||||
"REG-INV",
|
||||
GoogleCredentialsBundle.create(GoogleCredentials.create(null)));
|
||||
}
|
||||
|
||||
private ImmutableList<BillingEvent> getInputEvents() {
|
||||
|
@ -189,17 +190,18 @@ public class InvoicingPipelineTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testEndToEndPipeline_generatesExpectedFiles() throws Exception {
|
||||
void testEndToEndPipeline_generatesExpectedFiles() throws Exception {
|
||||
ImmutableList<BillingEvent> inputRows = getInputEvents();
|
||||
PCollection<BillingEvent> input = p.apply(Create.of(inputRows));
|
||||
PCollection<BillingEvent> input = testPipeline.apply(Create.of(inputRows));
|
||||
invoicingPipeline.applyTerminalTransforms(input, StaticValueProvider.of("2017-10"));
|
||||
p.run();
|
||||
testPipeline.run();
|
||||
|
||||
for (Entry<String, ImmutableList<String>> entry : getExpectedDetailReportMap().entrySet()) {
|
||||
ImmutableList<String> detailReport = resultFileContents(entry.getKey());
|
||||
assertThat(detailReport.get(0))
|
||||
.isEqualTo("id,billingTime,eventTime,registrarId,billingId,poNumber,tld,action,"
|
||||
+ "domain,repositoryId,years,currency,amount,flags");
|
||||
.isEqualTo(
|
||||
"id,billingTime,eventTime,registrarId,billingId,poNumber,tld,action,"
|
||||
+ "domain,repositoryId,years,currency,amount,flags");
|
||||
assertThat(detailReport.subList(1, detailReport.size()))
|
||||
.containsExactlyElementsIn(entry.getValue());
|
||||
}
|
||||
|
@ -218,8 +220,7 @@ public class InvoicingPipelineTest {
|
|||
private ImmutableList<String> resultFileContents(String filename) throws Exception {
|
||||
File resultFile =
|
||||
new File(
|
||||
String.format(
|
||||
"%s/invoices/2017-10/%s", tempFolder.getRoot().getAbsolutePath(), filename));
|
||||
String.format("%s/invoices/2017-10/%s", tmpDir.toAbsolutePath().toString(), filename));
|
||||
return ImmutableList.copyOf(
|
||||
ResourceUtils.readResourceUtf8(resultFile.toURI().toURL()).split("\n"));
|
||||
}
|
||||
|
|
|
@ -38,6 +38,8 @@ import java.io.InputStreamReader;
|
|||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
import java.util.function.Supplier;
|
||||
import org.apache.beam.runners.direct.DirectRunner;
|
||||
|
@ -56,47 +58,47 @@ import org.joda.time.DateTime;
|
|||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
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;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
/** Unit tests for {@link Spec11Pipeline}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class Spec11PipelineTest {
|
||||
class Spec11PipelineTest {
|
||||
|
||||
private static PipelineOptions pipelineOptions;
|
||||
|
||||
@BeforeClass
|
||||
public static void initializePipelineOptions() {
|
||||
@BeforeAll
|
||||
static void beforeAll() {
|
||||
pipelineOptions = PipelineOptionsFactory.create();
|
||||
pipelineOptions.setRunner(DirectRunner.class);
|
||||
}
|
||||
|
||||
@Rule
|
||||
public final transient TestPipelineExtension p =
|
||||
@RegisterExtension
|
||||
final transient TestPipelineExtension testPipeline =
|
||||
TestPipelineExtension.fromOptions(pipelineOptions);
|
||||
|
||||
@Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
@TempDir
|
||||
Path tmpDir;
|
||||
|
||||
private final Retrier retrier =
|
||||
new Retrier(new FakeSleeper(new FakeClock(DateTime.parse("2019-07-15TZ"))), 1);
|
||||
private Spec11Pipeline spec11Pipeline;
|
||||
|
||||
@Before
|
||||
public void initializePipeline() throws IOException {
|
||||
File beamTempFolder = tempFolder.newFolder();
|
||||
@BeforeEach
|
||||
void beforeEach() throws IOException {
|
||||
String beamTempFolder =
|
||||
Files.createDirectory(tmpDir.resolve("beam_temp")).toAbsolutePath().toString();
|
||||
spec11Pipeline =
|
||||
new Spec11Pipeline(
|
||||
"test-project",
|
||||
beamTempFolder.getAbsolutePath() + "/staging",
|
||||
beamTempFolder.getAbsolutePath() + "/templates/invoicing",
|
||||
tempFolder.getRoot().getAbsolutePath(),
|
||||
beamTempFolder + "/staging",
|
||||
beamTempFolder + "/templates/invoicing",
|
||||
tmpDir.toAbsolutePath().toString(),
|
||||
GoogleCredentialsBundle.create(GoogleCredentials.create(null)),
|
||||
retrier);
|
||||
}
|
||||
|
@ -130,7 +132,7 @@ public class Spec11PipelineTest {
|
|||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testEndToEndPipeline_generatesExpectedFiles() throws Exception {
|
||||
void testEndToEndPipeline_generatesExpectedFiles() throws Exception {
|
||||
// Establish mocks for testing
|
||||
ImmutableList<Subdomain> inputRows = getInputDomains();
|
||||
CloseableHttpClient httpClient = mock(CloseableHttpClient.class, withSettings().serializable());
|
||||
|
@ -145,9 +147,9 @@ public class Spec11PipelineTest {
|
|||
(Serializable & Supplier) () -> httpClient);
|
||||
|
||||
// Apply input and evaluation transforms
|
||||
PCollection<Subdomain> input = p.apply(Create.of(inputRows));
|
||||
PCollection<Subdomain> input = testPipeline.apply(Create.of(inputRows));
|
||||
spec11Pipeline.evaluateUrlHealth(input, evalFn, StaticValueProvider.of("2018-06-01"));
|
||||
p.run();
|
||||
testPipeline.run();
|
||||
|
||||
// Verify header and 4 threat matches for 3 registrars are found
|
||||
ImmutableList<String> generatedReport = resultFileContents();
|
||||
|
@ -295,7 +297,7 @@ public class Spec11PipelineTest {
|
|||
new File(
|
||||
String.format(
|
||||
"%s/icann/spec11/2018-06/SPEC11_MONTHLY_REPORT_2018-06-01",
|
||||
tempFolder.getRoot().getAbsolutePath()));
|
||||
tmpDir.toAbsolutePath().toString()));
|
||||
return ImmutableList.copyOf(
|
||||
ResourceUtils.readResourceUtf8(resultFile.toURI().toURL()).split("\n"));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue