mirror of
https://github.com/google/nomulus.git
synced 2025-06-12 23:44: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
|
@ -256,6 +256,7 @@ dependencies {
|
||||||
compile deps['org.bouncycastle:bcpg-jdk15on']
|
compile deps['org.bouncycastle:bcpg-jdk15on']
|
||||||
testCompile deps['org.bouncycastle:bcpkix-jdk15on']
|
testCompile deps['org.bouncycastle:bcpkix-jdk15on']
|
||||||
compile deps['org.bouncycastle:bcprov-jdk15on']
|
compile deps['org.bouncycastle:bcprov-jdk15on']
|
||||||
|
testCompile deps['com.fasterxml.jackson.core:jackson-databind']
|
||||||
runtime deps['org.glassfish.jaxb:jaxb-runtime']
|
runtime deps['org.glassfish.jaxb:jaxb-runtime']
|
||||||
compile deps['org.hibernate:hibernate-core']
|
compile deps['org.hibernate:hibernate-core']
|
||||||
compile deps['org.joda:joda-money']
|
compile deps['org.joda:joda-money']
|
||||||
|
|
|
@ -68,8 +68,9 @@ final class RegistryCli implements AutoCloseable, CommandRunner {
|
||||||
|
|
||||||
@Parameter(
|
@Parameter(
|
||||||
names = {"--sql_access_info"},
|
names = {"--sql_access_info"},
|
||||||
description = "Name of a file containing space-separated SQL access info used when deploying "
|
description =
|
||||||
+ "Beam pipelines")
|
"Name of a file containing space-separated SQL access info used when deploying "
|
||||||
|
+ "Beam pipelines")
|
||||||
private String sqlAccessInfoFile = null;
|
private String sqlAccessInfoFile = null;
|
||||||
|
|
||||||
// Do not make this final - compile-time constant inlining may interfere with JCommander.
|
// Do not make this final - compile-time constant inlining may interfere with JCommander.
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
*/
|
*/
|
||||||
package google.registry.beam;
|
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.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.is;
|
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.transforms.SerializableFunction;
|
||||||
import org.apache.beam.sdk.util.common.ReflectHelpers;
|
import org.apache.beam.sdk.util.common.ReflectHelpers;
|
||||||
import org.junit.experimental.categories.Category;
|
import org.junit.experimental.categories.Category;
|
||||||
import org.junit.rules.TestRule;
|
import org.junit.jupiter.api.extension.AfterEachCallback;
|
||||||
import org.junit.runner.Description;
|
import org.junit.jupiter.api.extension.BeforeEachCallback;
|
||||||
import org.junit.runners.model.Statement;
|
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
|
// NOTE: This file is copied from the Apache Beam distribution so that it can be locally modified to
|
||||||
// support JUnit 5.
|
// 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
|
* 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.
|
* 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:
|
* <p>In order to run tests on a pipeline runner, the following conditions must be met:
|
||||||
*
|
*
|
||||||
* <ul>
|
* <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
|
* <p>See also the <a href="https://beam.apache.org/contribute/testing/">Testing</a> documentation
|
||||||
* section.
|
* section.
|
||||||
*/
|
*/
|
||||||
public class TestPipelineExtension extends Pipeline implements TestRule {
|
public class TestPipelineExtension extends Pipeline
|
||||||
|
implements BeforeEachCallback, AfterEachCallback {
|
||||||
|
|
||||||
private final PipelineOptions options;
|
private final PipelineOptions options;
|
||||||
|
|
||||||
|
@ -287,50 +284,38 @@ public class TestPipelineExtension extends Pipeline implements TestRule {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement apply(final Statement statement, final Description description) {
|
public void beforeEach(ExtensionContext context) throws Exception {
|
||||||
return new Statement() {
|
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 the enforcement level has not been set by the user do auto-inference
|
if (!enforcement.isPresent()) {
|
||||||
if (!enforcement.isPresent()) {
|
final boolean isCrashingRunner = CrashingRunner.class.isAssignableFrom(options.getRunner());
|
||||||
|
|
||||||
final boolean annotatedWithNeedsRunner =
|
checkState(
|
||||||
description.getAnnotations().stream()
|
!isCrashingRunner,
|
||||||
.filter(Annotations.Predicates.isAnnotationOfType(Category.class))
|
"Cannot test using a [%s] runner. Please re-check your configuration.",
|
||||||
.anyMatch(Annotations.Predicates.isCategoryOf(NeedsRunner.class, true));
|
CrashingRunner.class.getSimpleName());
|
||||||
|
|
||||||
final boolean crashingRunner = CrashingRunner.class.isAssignableFrom(options.getRunner());
|
enableAbandonedNodeEnforcement(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
checkState(
|
@Override
|
||||||
!(annotatedWithNeedsRunner && crashingRunner),
|
public void afterEach(ExtensionContext context) throws Exception {
|
||||||
"The test was annotated with a [@%s] / [@%s] while the runner "
|
enforcement.get().afterUserCodeFinished();
|
||||||
+ "was set to [%s]. Please re-check your configuration.",
|
}
|
||||||
NeedsRunner.class.getSimpleName(),
|
|
||||||
ValidatesRunner.class.getSimpleName(),
|
|
||||||
CrashingRunner.class.getSimpleName());
|
|
||||||
|
|
||||||
enableAbandonedNodeEnforcement(annotatedWithNeedsRunner || !crashingRunner);
|
/** Returns the class + method name of the test. */
|
||||||
}
|
private String getAppName(ExtensionContext context) {
|
||||||
}
|
String methodName = context.getRequiredTestMethod().getName();
|
||||||
|
Class<?> testClass = context.getRequiredTestClass();
|
||||||
@Override
|
if (testClass.isMemberClass()) {
|
||||||
public void evaluate() throws Throwable {
|
return String.format(
|
||||||
options.as(ApplicationNameOptions.class).setAppName(getAppName(description));
|
"%s$%s-%s",
|
||||||
|
testClass.getEnclosingClass().getSimpleName(), testClass.getSimpleName(), methodName);
|
||||||
setDeducedEnforcementLevel();
|
} else {
|
||||||
|
return String.format("%s-%s", testClass.getSimpleName(), methodName);
|
||||||
// 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();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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.
|
* 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.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Serializable;
|
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.coders.StringUtf8Coder;
|
||||||
import org.apache.beam.sdk.io.fs.MatchResult.Metadata;
|
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.testing.PAssert;
|
||||||
import org.apache.beam.sdk.transforms.Create;
|
import org.apache.beam.sdk.transforms.Create;
|
||||||
import org.apache.beam.sdk.transforms.DoFn;
|
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.KV;
|
||||||
import org.apache.beam.sdk.values.PCollection;
|
import org.apache.beam.sdk.values.PCollection;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.junit.After;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Rule;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
import org.junit.experimental.categories.Category;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
import org.junit.rules.TemporaryFolder;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.junit.runners.JUnit4;
|
|
||||||
|
|
||||||
/** Unit tests for {@link Transforms} related to loading CommitLogs. */
|
/** Unit tests for {@link Transforms} related to loading CommitLogs. */
|
||||||
@RunWith(JUnit4.class)
|
class CommitLogTransformsTest implements Serializable {
|
||||||
public class CommitLogTransformsTest implements Serializable {
|
|
||||||
|
|
||||||
private static final DateTime START_TIME = DateTime.parse("2000-01-01T00:00:00.0Z");
|
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
|
@RegisterExtension
|
||||||
public final transient TestPipelineExtension pipeline =
|
final transient TestPipelineExtension testPipeline =
|
||||||
TestPipelineExtension.create().enableAbandonedNodeEnforcement(true);
|
TestPipelineExtension.create().enableAbandonedNodeEnforcement(true);
|
||||||
|
|
||||||
private FakeClock fakeClock;
|
private FakeClock fakeClock;
|
||||||
|
@ -74,8 +73,8 @@ public class CommitLogTransformsTest implements Serializable {
|
||||||
private transient ContactResource contact;
|
private transient ContactResource contact;
|
||||||
private transient DomainBase domain;
|
private transient DomainBase domain;
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void beforeEach() throws Exception {
|
void beforeEach() throws Exception {
|
||||||
fakeClock = new FakeClock(START_TIME);
|
fakeClock = new FakeClock(START_TIME);
|
||||||
store = new BackupTestStore(fakeClock);
|
store = new BackupTestStore(fakeClock);
|
||||||
injectRule.setStaticField(Ofy.class, "clock", fakeClock);
|
injectRule.setStaticField(Ofy.class, "clock", fakeClock);
|
||||||
|
@ -91,12 +90,12 @@ public class CommitLogTransformsTest implements Serializable {
|
||||||
contact = (ContactResource) store.loadAsOfyEntity(contact);
|
contact = (ContactResource) store.loadAsOfyEntity(contact);
|
||||||
domain = (DomainBase) store.loadAsOfyEntity(domain);
|
domain = (DomainBase) store.loadAsOfyEntity(domain);
|
||||||
|
|
||||||
commitLogsDir = temporaryFolder.newFolder();
|
commitLogsDir = Files.createDirectory(tmpDir.resolve("commit_logs")).toFile();
|
||||||
firstCommitLogFile = store.saveCommitLogs(commitLogsDir.getAbsolutePath());
|
firstCommitLogFile = store.saveCommitLogs(commitLogsDir.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@AfterEach
|
||||||
public void afterEach() throws Exception {
|
void afterEach() throws Exception {
|
||||||
if (store != null) {
|
if (store != null) {
|
||||||
store.close();
|
store.close();
|
||||||
store = null;
|
store = null;
|
||||||
|
@ -104,10 +103,9 @@ public class CommitLogTransformsTest implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Category(NeedsRunner.class)
|
void getCommitLogFilePatterns() {
|
||||||
public void getCommitLogFilePatterns() {
|
|
||||||
PCollection<String> patterns =
|
PCollection<String> patterns =
|
||||||
pipeline.apply(
|
testPipeline.apply(
|
||||||
"Get CommitLog file patterns",
|
"Get CommitLog file patterns",
|
||||||
Transforms.getCommitLogFilePatterns(commitLogsDir.getAbsolutePath()));
|
Transforms.getCommitLogFilePatterns(commitLogsDir.getAbsolutePath()));
|
||||||
|
|
||||||
|
@ -116,14 +114,13 @@ public class CommitLogTransformsTest implements Serializable {
|
||||||
|
|
||||||
PAssert.that(patterns).containsInAnyOrder(expectedPatterns);
|
PAssert.that(patterns).containsInAnyOrder(expectedPatterns);
|
||||||
|
|
||||||
pipeline.run();
|
testPipeline.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Category(NeedsRunner.class)
|
void getFilesByPatterns() {
|
||||||
public void getFilesByPatterns() {
|
|
||||||
PCollection<Metadata> fileMetas =
|
PCollection<Metadata> fileMetas =
|
||||||
pipeline
|
testPipeline
|
||||||
.apply(
|
.apply(
|
||||||
"File patterns to metadata",
|
"File patterns to metadata",
|
||||||
Create.of(commitLogsDir.getAbsolutePath() + "/commit_diff_until_*")
|
Create.of(commitLogsDir.getAbsolutePath() + "/commit_diff_until_*")
|
||||||
|
@ -148,12 +145,11 @@ public class CommitLogTransformsTest implements Serializable {
|
||||||
|
|
||||||
PAssert.that(fileNames).containsInAnyOrder(expectedFilenames);
|
PAssert.that(fileNames).containsInAnyOrder(expectedFilenames);
|
||||||
|
|
||||||
pipeline.run();
|
testPipeline.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Category(NeedsRunner.class)
|
void filterCommitLogsByTime() throws IOException {
|
||||||
public void filterCommitLogsByTime() throws IOException {
|
|
||||||
ImmutableList<String> commitLogFilenames =
|
ImmutableList<String> commitLogFilenames =
|
||||||
ImmutableList.of(
|
ImmutableList.of(
|
||||||
"commit_diff_until_2000-01-01T00:00:00.000Z",
|
"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.003Z",
|
||||||
"commit_diff_until_2000-01-01T00:00:00.004Z");
|
"commit_diff_until_2000-01-01T00:00:00.004Z");
|
||||||
|
|
||||||
File commitLogDir = temporaryFolder.newFolder();
|
|
||||||
for (String name : commitLogFilenames) {
|
for (String name : commitLogFilenames) {
|
||||||
new File(commitLogDir, name).createNewFile();
|
new File(commitLogsDir, name).createNewFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
PCollection<String> filteredFilenames =
|
PCollection<String> filteredFilenames =
|
||||||
pipeline
|
testPipeline
|
||||||
.apply(
|
.apply(
|
||||||
"Get commitlog file patterns",
|
"Get commitlog file patterns",
|
||||||
Transforms.getCommitLogFilePatterns(commitLogDir.getAbsolutePath()))
|
Transforms.getCommitLogFilePatterns(commitLogsDir.getAbsolutePath()))
|
||||||
.apply("Find commitlog files", Transforms.getFilesByPatterns())
|
.apply("Find commitlog files", Transforms.getFilesByPatterns())
|
||||||
.apply(
|
.apply(
|
||||||
"Filtered by Time",
|
"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.001Z",
|
||||||
"commit_diff_until_2000-01-01T00:00:00.002Z");
|
"commit_diff_until_2000-01-01T00:00:00.002Z");
|
||||||
|
|
||||||
pipeline.run();
|
testPipeline.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Category(NeedsRunner.class)
|
void loadOneCommitLogFile() {
|
||||||
public void loadOneCommitLogFile() {
|
|
||||||
PCollection<VersionedEntity> entities =
|
PCollection<VersionedEntity> entities =
|
||||||
pipeline
|
testPipeline
|
||||||
.apply(
|
.apply(
|
||||||
"Get CommitLog file patterns",
|
"Get CommitLog file patterns",
|
||||||
Transforms.getCommitLogFilePatterns(commitLogsDir.getAbsolutePath()))
|
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(contact)),
|
||||||
KV.of(fakeClock.nowUtc().getMillis() - 1, store.loadAsDatastoreEntity(domain)));
|
KV.of(fakeClock.nowUtc().getMillis() - 1, store.loadAsDatastoreEntity(domain)));
|
||||||
|
|
||||||
pipeline.run();
|
testPipeline.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Category(NeedsRunner.class)
|
void loadOneCommitLogFile_filterByKind() {
|
||||||
public void loadOneCommitLogFile_filterByKind() {
|
|
||||||
PCollection<VersionedEntity> entities =
|
PCollection<VersionedEntity> entities =
|
||||||
pipeline
|
testPipeline
|
||||||
.apply(
|
.apply(
|
||||||
"Get CommitLog file patterns",
|
"Get CommitLog file patterns",
|
||||||
Transforms.getCommitLogFilePatterns(commitLogsDir.getAbsolutePath()))
|
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() - 2, store.loadAsDatastoreEntity(registry)),
|
||||||
KV.of(fakeClock.nowUtc().getMillis() - 1, store.loadAsDatastoreEntity(contact)));
|
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 google.registry.testing.InjectRule;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import org.apache.beam.sdk.coders.StringUtf8Coder;
|
import org.apache.beam.sdk.coders.StringUtf8Coder;
|
||||||
import org.apache.beam.sdk.io.fs.MatchResult.Metadata;
|
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.testing.PAssert;
|
||||||
import org.apache.beam.sdk.transforms.Create;
|
import org.apache.beam.sdk.transforms.Create;
|
||||||
import org.apache.beam.sdk.transforms.DoFn;
|
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.KV;
|
||||||
import org.apache.beam.sdk.values.PCollection;
|
import org.apache.beam.sdk.values.PCollection;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.junit.After;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Rule;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
import org.junit.experimental.categories.Category;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
import org.junit.rules.TemporaryFolder;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.junit.runners.JUnit4;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link Transforms} related to loading Datastore exports.
|
* 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.
|
* <p>This class implements {@link Serializable} so that test {@link DoFn} classes may be inlined.
|
||||||
*/
|
*/
|
||||||
@RunWith(JUnit4.class)
|
class ExportloadingTransformsTest implements Serializable {
|
||||||
public class ExportloadingTransformsTest implements Serializable {
|
|
||||||
private static final DateTime START_TIME = DateTime.parse("2000-01-01T00:00:00.0Z");
|
private static final DateTime START_TIME = DateTime.parse("2000-01-01T00:00:00.0Z");
|
||||||
|
|
||||||
private static final ImmutableList<Class<?>> ALL_KINDS =
|
private static final ImmutableList<Class<?>> ALL_KINDS =
|
||||||
|
@ -65,12 +62,14 @@ public class ExportloadingTransformsTest implements Serializable {
|
||||||
private static final ImmutableSet<String> ALL_KIND_STRS =
|
private static final ImmutableSet<String> ALL_KIND_STRS =
|
||||||
ALL_KINDS.stream().map(Key::getKind).collect(ImmutableSet.toImmutableSet());
|
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
|
@RegisterExtension
|
||||||
public final transient TestPipelineExtension pipeline =
|
final transient TestPipelineExtension testPipeline =
|
||||||
TestPipelineExtension.create().enableAbandonedNodeEnforcement(true);
|
TestPipelineExtension.create().enableAbandonedNodeEnforcement(true);
|
||||||
|
|
||||||
private FakeClock fakeClock;
|
private FakeClock fakeClock;
|
||||||
|
@ -82,8 +81,8 @@ public class ExportloadingTransformsTest implements Serializable {
|
||||||
private transient ContactResource contact;
|
private transient ContactResource contact;
|
||||||
private transient DomainBase domain;
|
private transient DomainBase domain;
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void beforeEach() throws Exception {
|
void beforeEach() throws Exception {
|
||||||
fakeClock = new FakeClock(START_TIME);
|
fakeClock = new FakeClock(START_TIME);
|
||||||
store = new BackupTestStore(fakeClock);
|
store = new BackupTestStore(fakeClock);
|
||||||
injectRule.setStaticField(Ofy.class, "clock", fakeClock);
|
injectRule.setStaticField(Ofy.class, "clock", fakeClock);
|
||||||
|
@ -100,12 +99,11 @@ public class ExportloadingTransformsTest implements Serializable {
|
||||||
contact = (ContactResource) store.loadAsOfyEntity(contact);
|
contact = (ContactResource) store.loadAsOfyEntity(contact);
|
||||||
domain = (DomainBase) store.loadAsOfyEntity(domain);
|
domain = (DomainBase) store.loadAsOfyEntity(domain);
|
||||||
|
|
||||||
exportDir =
|
exportDir = store.export(tmpDir.toAbsolutePath().toString(), ALL_KINDS, Collections.EMPTY_SET);
|
||||||
store.export(exportRootDir.getRoot().getAbsolutePath(), ALL_KINDS, Collections.EMPTY_SET);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@AfterEach
|
||||||
public void afterEach() throws Exception {
|
void afterEach() throws Exception {
|
||||||
if (store != null) {
|
if (store != null) {
|
||||||
store.close();
|
store.close();
|
||||||
store = null;
|
store = null;
|
||||||
|
@ -113,10 +111,9 @@ public class ExportloadingTransformsTest implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Category(NeedsRunner.class)
|
void getExportFilePatterns() {
|
||||||
public void getExportFilePatterns() {
|
|
||||||
PCollection<String> patterns =
|
PCollection<String> patterns =
|
||||||
pipeline.apply(
|
testPipeline.apply(
|
||||||
"Get Datastore file patterns",
|
"Get Datastore file patterns",
|
||||||
Transforms.getDatastoreExportFilePatterns(exportDir.getAbsolutePath(), ALL_KIND_STRS));
|
Transforms.getDatastoreExportFilePatterns(exportDir.getAbsolutePath(), ALL_KIND_STRS));
|
||||||
|
|
||||||
|
@ -128,14 +125,13 @@ public class ExportloadingTransformsTest implements Serializable {
|
||||||
|
|
||||||
PAssert.that(patterns).containsInAnyOrder(expectedPatterns);
|
PAssert.that(patterns).containsInAnyOrder(expectedPatterns);
|
||||||
|
|
||||||
pipeline.run();
|
testPipeline.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Category(NeedsRunner.class)
|
void getFilesByPatterns() {
|
||||||
public void getFilesByPatterns() {
|
|
||||||
PCollection<Metadata> fileMetas =
|
PCollection<Metadata> fileMetas =
|
||||||
pipeline
|
testPipeline
|
||||||
.apply(
|
.apply(
|
||||||
"File patterns to metadata",
|
"File patterns to metadata",
|
||||||
Create.of(
|
Create.of(
|
||||||
|
@ -167,14 +163,13 @@ public class ExportloadingTransformsTest implements Serializable {
|
||||||
|
|
||||||
PAssert.that(fileNames).containsInAnyOrder(expectedFilenames);
|
PAssert.that(fileNames).containsInAnyOrder(expectedFilenames);
|
||||||
|
|
||||||
pipeline.run();
|
testPipeline.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Category(NeedsRunner.class)
|
void loadDataFromFiles() {
|
||||||
public void loadDataFromFiles() {
|
|
||||||
PCollection<VersionedEntity> entities =
|
PCollection<VersionedEntity> entities =
|
||||||
pipeline
|
testPipeline
|
||||||
.apply(
|
.apply(
|
||||||
"Get Datastore file patterns",
|
"Get Datastore file patterns",
|
||||||
Transforms.getDatastoreExportFilePatterns(
|
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(contact)),
|
||||||
KV.of(Transforms.EXPORT_ENTITY_TIME_STAMP, store.loadAsDatastoreEntity(domain)));
|
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.FakeClock;
|
||||||
import google.registry.testing.InjectRule;
|
import google.registry.testing.InjectRule;
|
||||||
import java.io.File;
|
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.KV;
|
||||||
import org.apache.beam.sdk.values.PCollectionTuple;
|
import org.apache.beam.sdk.values.PCollectionTuple;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Rule;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
import org.junit.experimental.categories.Category;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
import org.junit.rules.TemporaryFolder;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.junit.runners.JUnit4;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit test for {@link Transforms#loadDatastoreSnapshot}.
|
* Unit test for {@link Transforms#loadDatastoreSnapshot}.
|
||||||
|
@ -71,8 +69,8 @@ import org.junit.runners.JUnit4;
|
||||||
* <li>Deletes are properly handled.
|
* <li>Deletes are properly handled.
|
||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
@RunWith(JUnit4.class)
|
class LoadDatastoreSnapshotTest {
|
||||||
public class LoadDatastoreSnapshotTest {
|
|
||||||
private static final DateTime START_TIME = DateTime.parse("2000-01-01T00:00:00.0Z");
|
private static final DateTime START_TIME = DateTime.parse("2000-01-01T00:00:00.0Z");
|
||||||
|
|
||||||
private static final ImmutableList<Class<?>> ALL_KINDS =
|
private static final ImmutableList<Class<?>> ALL_KINDS =
|
||||||
|
@ -80,12 +78,14 @@ public class LoadDatastoreSnapshotTest {
|
||||||
private static final ImmutableSet<String> ALL_KIND_STRS =
|
private static final ImmutableSet<String> ALL_KIND_STRS =
|
||||||
ALL_KINDS.stream().map(Key::getKind).collect(ImmutableSet.toImmutableSet());
|
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
|
@RegisterExtension
|
||||||
public final transient TestPipelineExtension pipeline =
|
final transient TestPipelineExtension testPipeline =
|
||||||
TestPipelineExtension.create().enableAbandonedNodeEnforcement(true);
|
TestPipelineExtension.create().enableAbandonedNodeEnforcement(true);
|
||||||
|
|
||||||
private FakeClock fakeClock;
|
private FakeClock fakeClock;
|
||||||
|
@ -102,14 +102,14 @@ public class LoadDatastoreSnapshotTest {
|
||||||
private transient DateTime contactLastUpdateTime;
|
private transient DateTime contactLastUpdateTime;
|
||||||
private transient DateTime domainLastUpdateTime;
|
private transient DateTime domainLastUpdateTime;
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void beforeEach() throws Exception {
|
void beforeEach() throws Exception {
|
||||||
fakeClock = new FakeClock(START_TIME);
|
fakeClock = new FakeClock(START_TIME);
|
||||||
try (BackupTestStore store = new BackupTestStore(fakeClock)) {
|
try (BackupTestStore store = new BackupTestStore(fakeClock)) {
|
||||||
injectRule.setStaticField(Ofy.class, "clock", fakeClock);
|
injectRule.setStaticField(Ofy.class, "clock", fakeClock);
|
||||||
|
|
||||||
exportRootDir = temporaryFolder.newFolder();
|
exportRootDir = Files.createDirectory(tmpDir.resolve("export_root")).toFile();
|
||||||
commitLogsDir = temporaryFolder.newFolder();
|
commitLogsDir = Files.createDirectory(tmpDir.resolve("commit_logs")).toFile();
|
||||||
|
|
||||||
Registry registry = newRegistry("tld1", "TLD1");
|
Registry registry = newRegistry("tld1", "TLD1");
|
||||||
ContactResource fillerContact = newContactResource("contact_filler");
|
ContactResource fillerContact = newContactResource("contact_filler");
|
||||||
|
@ -154,10 +154,9 @@ public class LoadDatastoreSnapshotTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Category(NeedsRunner.class)
|
void loadDatastoreSnapshot() {
|
||||||
public void loadDatastoreSnapshot() {
|
|
||||||
PCollectionTuple snapshot =
|
PCollectionTuple snapshot =
|
||||||
pipeline.apply(
|
testPipeline.apply(
|
||||||
Transforms.loadDatastoreSnapshot(
|
Transforms.loadDatastoreSnapshot(
|
||||||
exportDir.getAbsolutePath(),
|
exportDir.getAbsolutePath(),
|
||||||
commitLogsDir.getAbsolutePath(),
|
commitLogsDir.getAbsolutePath(),
|
||||||
|
@ -173,6 +172,6 @@ public class LoadDatastoreSnapshotTest {
|
||||||
InitSqlTestUtils.assertContainsExactlyElementsIn(
|
InitSqlTestUtils.assertContainsExactlyElementsIn(
|
||||||
snapshot.get(Transforms.createTagForKind("ContactResource")),
|
snapshot.get(Transforms.createTagForKind("ContactResource")),
|
||||||
KV.of(contactLastUpdateTime.getMillis(), dsContact));
|
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.File;
|
||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import org.apache.beam.sdk.testing.NeedsRunner;
|
|
||||||
import org.apache.beam.sdk.transforms.Create;
|
import org.apache.beam.sdk.transforms.Create;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Rule;
|
import org.junit.jupiter.api.Order;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.experimental.categories.Category;
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
import org.junit.rules.RuleChain;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
import org.junit.rules.TemporaryFolder;
|
|
||||||
|
|
||||||
/** Unit test for {@link Transforms#writeToSql}. */
|
/** 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 static final DateTime START_TIME = DateTime.parse("2000-01-01T00:00:00.0Z");
|
||||||
|
|
||||||
private final FakeClock fakeClock = new FakeClock(START_TIME);
|
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.
|
@RegisterExtension
|
||||||
public final transient JpaIntegrationTestRule database =
|
final transient JpaIntegrationTestRule database =
|
||||||
new JpaTestRules.Builder().withClock(fakeClock).buildIntegrationTestRule();
|
new JpaTestRules.Builder().withClock(fakeClock).buildIntegrationTestRule();
|
||||||
|
|
||||||
@Rule
|
@RegisterExtension
|
||||||
public final transient RuleChain jpaRules =
|
@Order(value = 1)
|
||||||
RuleChain.outerRule(new DatastoreEntityExtension()).around(database);
|
final transient DatastoreEntityExtension datastore = new DatastoreEntityExtension();
|
||||||
|
|
||||||
@Rule public transient TemporaryFolder temporaryFolder = new TemporaryFolder();
|
@SuppressWarnings("WeakerAccess")
|
||||||
|
@TempDir
|
||||||
|
transient Path tmpDir;
|
||||||
|
|
||||||
@Rule
|
@RegisterExtension
|
||||||
public final transient TestPipelineExtension pipeline =
|
final transient TestPipelineExtension testPipeline =
|
||||||
TestPipelineExtension.create().enableAbandonedNodeEnforcement(true);
|
TestPipelineExtension.create().enableAbandonedNodeEnforcement(true);
|
||||||
|
|
||||||
private ImmutableList<Entity> contacts;
|
private ImmutableList<Entity> contacts;
|
||||||
|
|
||||||
private File credentialFile;
|
private File credentialFile;
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void beforeEach() throws Exception {
|
void beforeEach() throws Exception {
|
||||||
try (BackupTestStore store = new BackupTestStore(fakeClock)) {
|
try (BackupTestStore store = new BackupTestStore(fakeClock)) {
|
||||||
injectRule.setStaticField(Ofy.class, "clock", fakeClock);
|
injectRule.setStaticField(Ofy.class, "clock", fakeClock);
|
||||||
|
|
||||||
|
@ -91,7 +93,7 @@ public class WriteToSqlTest implements Serializable {
|
||||||
}
|
}
|
||||||
contacts = builder.build();
|
contacts = builder.build();
|
||||||
}
|
}
|
||||||
credentialFile = temporaryFolder.newFile();
|
credentialFile = Files.createFile(tmpDir.resolve("credential.dat")).toFile();
|
||||||
new PrintStream(credentialFile)
|
new PrintStream(credentialFile)
|
||||||
.printf(
|
.printf(
|
||||||
"%s %s %s",
|
"%s %s %s",
|
||||||
|
@ -102,9 +104,8 @@ public class WriteToSqlTest implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Category(NeedsRunner.class)
|
void writeToSql_twoWriters() {
|
||||||
public void writeToSql_twoWriters() {
|
testPipeline
|
||||||
pipeline
|
|
||||||
.apply(
|
.apply(
|
||||||
Create.of(
|
Create.of(
|
||||||
contacts.stream()
|
contacts.stream()
|
||||||
|
@ -121,7 +122,7 @@ public class WriteToSqlTest implements Serializable {
|
||||||
.beamJpaModule(new BeamJpaModule(credentialFile.getAbsolutePath()))
|
.beamJpaModule(new BeamJpaModule(credentialFile.getAbsolutePath()))
|
||||||
.build()
|
.build()
|
||||||
.localDbJpaTransactionManager()));
|
.localDbJpaTransactionManager()));
|
||||||
pipeline.run().waitUntilFinish();
|
testPipeline.run().waitUntilFinish();
|
||||||
|
|
||||||
ImmutableList<?> sqlContacts = jpaTm().transact(() -> jpaTm().loadAll(ContactResource.class));
|
ImmutableList<?> sqlContacts = jpaTm().transact(() -> jpaTm().loadAll(ContactResource.class));
|
||||||
// TODO(weiminyu): compare load entities with originals. Note: lastUpdateTimes won't match by
|
// 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 google.registry.util.ResourceUtils;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.util.Map.Entry;
|
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.options.ValueProvider.StaticValueProvider;
|
||||||
import org.apache.beam.sdk.transforms.Create;
|
import org.apache.beam.sdk.transforms.Create;
|
||||||
import org.apache.beam.sdk.values.PCollection;
|
import org.apache.beam.sdk.values.PCollection;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Rule;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
import org.junit.rules.TemporaryFolder;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.junit.runners.JUnit4;
|
|
||||||
|
|
||||||
/** Unit tests for {@link InvoicingPipeline}. */
|
/** Unit tests for {@link InvoicingPipeline}. */
|
||||||
@RunWith(JUnit4.class)
|
class InvoicingPipelineTest {
|
||||||
public class InvoicingPipelineTest {
|
|
||||||
|
|
||||||
private static PipelineOptions pipelineOptions;
|
private static PipelineOptions pipelineOptions;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeAll
|
||||||
public static void initializePipelineOptions() {
|
static void beforeAll() {
|
||||||
pipelineOptions = PipelineOptionsFactory.create();
|
pipelineOptions = PipelineOptionsFactory.create();
|
||||||
pipelineOptions.setRunner(DirectRunner.class);
|
pipelineOptions.setRunner(DirectRunner.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Rule
|
@RegisterExtension
|
||||||
public final transient TestPipelineExtension p =
|
final transient TestPipelineExtension testPipeline =
|
||||||
TestPipelineExtension.fromOptions(pipelineOptions);
|
TestPipelineExtension.fromOptions(pipelineOptions);
|
||||||
|
|
||||||
@Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
|
@SuppressWarnings("WeakerAccess")
|
||||||
|
@TempDir
|
||||||
|
transient Path tmpDir;
|
||||||
|
|
||||||
private InvoicingPipeline invoicingPipeline;
|
private InvoicingPipeline invoicingPipeline;
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void initializePipeline() throws IOException {
|
void beforeEach() throws IOException {
|
||||||
File beamTempFolder = tempFolder.newFolder();
|
String beamTempFolder =
|
||||||
String beamTempFolderPath = beamTempFolder.getAbsolutePath();
|
Files.createDirectory(tmpDir.resolve("beam_temp")).toAbsolutePath().toString();
|
||||||
invoicingPipeline = new InvoicingPipeline(
|
invoicingPipeline =
|
||||||
"test-project",
|
new InvoicingPipeline(
|
||||||
beamTempFolderPath,
|
"test-project",
|
||||||
beamTempFolderPath + "/templates/invoicing",
|
beamTempFolder,
|
||||||
beamTempFolderPath + "/staging",
|
beamTempFolder + "/templates/invoicing",
|
||||||
tempFolder.getRoot().getAbsolutePath(),
|
beamTempFolder + "/staging",
|
||||||
"REG-INV",
|
tmpDir.toAbsolutePath().toString(),
|
||||||
GoogleCredentialsBundle.create(GoogleCredentials.create(null))
|
"REG-INV",
|
||||||
);
|
GoogleCredentialsBundle.create(GoogleCredentials.create(null)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private ImmutableList<BillingEvent> getInputEvents() {
|
private ImmutableList<BillingEvent> getInputEvents() {
|
||||||
|
@ -189,17 +190,18 @@ public class InvoicingPipelineTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEndToEndPipeline_generatesExpectedFiles() throws Exception {
|
void testEndToEndPipeline_generatesExpectedFiles() throws Exception {
|
||||||
ImmutableList<BillingEvent> inputRows = getInputEvents();
|
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"));
|
invoicingPipeline.applyTerminalTransforms(input, StaticValueProvider.of("2017-10"));
|
||||||
p.run();
|
testPipeline.run();
|
||||||
|
|
||||||
for (Entry<String, ImmutableList<String>> entry : getExpectedDetailReportMap().entrySet()) {
|
for (Entry<String, ImmutableList<String>> entry : getExpectedDetailReportMap().entrySet()) {
|
||||||
ImmutableList<String> detailReport = resultFileContents(entry.getKey());
|
ImmutableList<String> detailReport = resultFileContents(entry.getKey());
|
||||||
assertThat(detailReport.get(0))
|
assertThat(detailReport.get(0))
|
||||||
.isEqualTo("id,billingTime,eventTime,registrarId,billingId,poNumber,tld,action,"
|
.isEqualTo(
|
||||||
+ "domain,repositoryId,years,currency,amount,flags");
|
"id,billingTime,eventTime,registrarId,billingId,poNumber,tld,action,"
|
||||||
|
+ "domain,repositoryId,years,currency,amount,flags");
|
||||||
assertThat(detailReport.subList(1, detailReport.size()))
|
assertThat(detailReport.subList(1, detailReport.size()))
|
||||||
.containsExactlyElementsIn(entry.getValue());
|
.containsExactlyElementsIn(entry.getValue());
|
||||||
}
|
}
|
||||||
|
@ -218,8 +220,7 @@ public class InvoicingPipelineTest {
|
||||||
private ImmutableList<String> resultFileContents(String filename) throws Exception {
|
private ImmutableList<String> resultFileContents(String filename) throws Exception {
|
||||||
File resultFile =
|
File resultFile =
|
||||||
new File(
|
new File(
|
||||||
String.format(
|
String.format("%s/invoices/2017-10/%s", tmpDir.toAbsolutePath().toString(), filename));
|
||||||
"%s/invoices/2017-10/%s", tempFolder.getRoot().getAbsolutePath(), filename));
|
|
||||||
return ImmutableList.copyOf(
|
return ImmutableList.copyOf(
|
||||||
ResourceUtils.readResourceUtf8(resultFile.toURI().toURL()).split("\n"));
|
ResourceUtils.readResourceUtf8(resultFile.toURI().toURL()).split("\n"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,8 @@ import java.io.InputStreamReader;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import org.apache.beam.runners.direct.DirectRunner;
|
import org.apache.beam.runners.direct.DirectRunner;
|
||||||
|
@ -56,47 +58,47 @@ import org.joda.time.DateTime;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Rule;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
import org.junit.rules.TemporaryFolder;
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.junit.runners.JUnit4;
|
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
import org.mockito.stubbing.Answer;
|
import org.mockito.stubbing.Answer;
|
||||||
|
|
||||||
/** Unit tests for {@link Spec11Pipeline}. */
|
/** Unit tests for {@link Spec11Pipeline}. */
|
||||||
@RunWith(JUnit4.class)
|
class Spec11PipelineTest {
|
||||||
public class Spec11PipelineTest {
|
|
||||||
|
|
||||||
private static PipelineOptions pipelineOptions;
|
private static PipelineOptions pipelineOptions;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeAll
|
||||||
public static void initializePipelineOptions() {
|
static void beforeAll() {
|
||||||
pipelineOptions = PipelineOptionsFactory.create();
|
pipelineOptions = PipelineOptionsFactory.create();
|
||||||
pipelineOptions.setRunner(DirectRunner.class);
|
pipelineOptions.setRunner(DirectRunner.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Rule
|
@RegisterExtension
|
||||||
public final transient TestPipelineExtension p =
|
final transient TestPipelineExtension testPipeline =
|
||||||
TestPipelineExtension.fromOptions(pipelineOptions);
|
TestPipelineExtension.fromOptions(pipelineOptions);
|
||||||
|
|
||||||
@Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
|
@SuppressWarnings("WeakerAccess")
|
||||||
|
@TempDir
|
||||||
|
Path tmpDir;
|
||||||
|
|
||||||
private final Retrier retrier =
|
private final Retrier retrier =
|
||||||
new Retrier(new FakeSleeper(new FakeClock(DateTime.parse("2019-07-15TZ"))), 1);
|
new Retrier(new FakeSleeper(new FakeClock(DateTime.parse("2019-07-15TZ"))), 1);
|
||||||
private Spec11Pipeline spec11Pipeline;
|
private Spec11Pipeline spec11Pipeline;
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void initializePipeline() throws IOException {
|
void beforeEach() throws IOException {
|
||||||
File beamTempFolder = tempFolder.newFolder();
|
String beamTempFolder =
|
||||||
|
Files.createDirectory(tmpDir.resolve("beam_temp")).toAbsolutePath().toString();
|
||||||
spec11Pipeline =
|
spec11Pipeline =
|
||||||
new Spec11Pipeline(
|
new Spec11Pipeline(
|
||||||
"test-project",
|
"test-project",
|
||||||
beamTempFolder.getAbsolutePath() + "/staging",
|
beamTempFolder + "/staging",
|
||||||
beamTempFolder.getAbsolutePath() + "/templates/invoicing",
|
beamTempFolder + "/templates/invoicing",
|
||||||
tempFolder.getRoot().getAbsolutePath(),
|
tmpDir.toAbsolutePath().toString(),
|
||||||
GoogleCredentialsBundle.create(GoogleCredentials.create(null)),
|
GoogleCredentialsBundle.create(GoogleCredentials.create(null)),
|
||||||
retrier);
|
retrier);
|
||||||
}
|
}
|
||||||
|
@ -130,7 +132,7 @@ public class Spec11PipelineTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void testEndToEndPipeline_generatesExpectedFiles() throws Exception {
|
void testEndToEndPipeline_generatesExpectedFiles() throws Exception {
|
||||||
// Establish mocks for testing
|
// Establish mocks for testing
|
||||||
ImmutableList<Subdomain> inputRows = getInputDomains();
|
ImmutableList<Subdomain> inputRows = getInputDomains();
|
||||||
CloseableHttpClient httpClient = mock(CloseableHttpClient.class, withSettings().serializable());
|
CloseableHttpClient httpClient = mock(CloseableHttpClient.class, withSettings().serializable());
|
||||||
|
@ -145,9 +147,9 @@ public class Spec11PipelineTest {
|
||||||
(Serializable & Supplier) () -> httpClient);
|
(Serializable & Supplier) () -> httpClient);
|
||||||
|
|
||||||
// Apply input and evaluation transforms
|
// 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"));
|
spec11Pipeline.evaluateUrlHealth(input, evalFn, StaticValueProvider.of("2018-06-01"));
|
||||||
p.run();
|
testPipeline.run();
|
||||||
|
|
||||||
// Verify header and 4 threat matches for 3 registrars are found
|
// Verify header and 4 threat matches for 3 registrars are found
|
||||||
ImmutableList<String> generatedReport = resultFileContents();
|
ImmutableList<String> generatedReport = resultFileContents();
|
||||||
|
@ -295,7 +297,7 @@ public class Spec11PipelineTest {
|
||||||
new File(
|
new File(
|
||||||
String.format(
|
String.format(
|
||||||
"%s/icann/spec11/2018-06/SPEC11_MONTHLY_REPORT_2018-06-01",
|
"%s/icann/spec11/2018-06/SPEC11_MONTHLY_REPORT_2018-06-01",
|
||||||
tempFolder.getRoot().getAbsolutePath()));
|
tmpDir.toAbsolutePath().toString()));
|
||||||
return ImmutableList.copyOf(
|
return ImmutableList.copyOf(
|
||||||
ResourceUtils.readResourceUtf8(resultFile.toURI().toURL()).split("\n"));
|
ResourceUtils.readResourceUtf8(resultFile.toURI().toURL()).split("\n"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,6 +130,7 @@ ext {
|
||||||
'org.bouncycastle:bcpg-jdk15on:1.61',
|
'org.bouncycastle:bcpg-jdk15on:1.61',
|
||||||
'org.bouncycastle:bcpkix-jdk15on:1.61',
|
'org.bouncycastle:bcpkix-jdk15on:1.61',
|
||||||
'org.bouncycastle:bcprov-jdk15on:1.61',
|
'org.bouncycastle:bcprov-jdk15on:1.61',
|
||||||
|
'com.fasterxml.jackson.core:jackson-databind:2.9.10',
|
||||||
'org.flywaydb:flyway-core:5.2.4',
|
'org.flywaydb:flyway-core:5.2.4',
|
||||||
'org.glassfish.jaxb:jaxb-runtime:2.3.0',
|
'org.glassfish.jaxb:jaxb-runtime:2.3.0',
|
||||||
'org.hamcrest:hamcrest-all:1.3',
|
'org.hamcrest:hamcrest-all:1.3',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue