diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/PAssert.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/PAssert.java index a6fb232e6b3e..259633554b77 100644 --- a/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/PAssert.java +++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/PAssert.java @@ -40,7 +40,6 @@ import org.apache.beam.sdk.coders.KvCoder; import org.apache.beam.sdk.coders.MapCoder; import org.apache.beam.sdk.coders.VarIntCoder; -import org.apache.beam.sdk.options.StreamingOptions; import org.apache.beam.sdk.runners.PipelineRunner; import org.apache.beam.sdk.runners.TransformHierarchy.Node; import org.apache.beam.sdk.transforms.Aggregator; @@ -279,7 +278,15 @@ public interface SingletonAssert { * Constructs an {@link IterableAssert} for the elements of the provided {@link PCollection}. */ public static IterableAssert that(PCollection actual) { - return new PCollectionContentsAssert<>(actual); + return that(actual.getName(), actual); + } + + /** + * Constructs an {@link IterableAssert} for the elements of the provided {@link PCollection} + * with the specified reason. + */ + public static IterableAssert that(String reason, PCollection actual) { + return new PCollectionContentsAssert<>(reason, actual); } /** @@ -288,6 +295,15 @@ public static IterableAssert that(PCollection actual) { */ public static IterableAssert thatSingletonIterable( PCollection> actual) { + return thatSingletonIterable(actual.getName(), actual); + } + + /** + * Constructs an {@link IterableAssert} for the value of the provided {@link PCollection } with + * the specified reason. The provided PCollection must contain a single {@code Iterable} value. + */ + public static IterableAssert thatSingletonIterable( + String reason, PCollection> actual) { try { } catch (NoSuchElementException | IllegalArgumentException exc) { @@ -300,7 +316,7 @@ public static IterableAssert thatSingletonIterable( @SuppressWarnings("unchecked") // Safe covariant cast PCollection> actualIterables = (PCollection>) actual; - return new PCollectionSingletonIterableAssert<>(actualIterables); + return new PCollectionSingletonIterableAssert<>(reason, actualIterables); } /** @@ -308,7 +324,16 @@ public static IterableAssert thatSingletonIterable( * {@code PCollection PCollection}, which must be a singleton. */ public static SingletonAssert thatSingleton(PCollection actual) { - return new PCollectionViewAssert<>(actual, View.asSingleton(), actual.getCoder()); + return thatSingleton(actual.getName(), actual); + } + + /** + * Constructs a {@link SingletonAssert} for the value of the provided + * {@code PCollection PCollection} with the specified reason. The provided PCollection must be + * a singleton. + */ + public static SingletonAssert thatSingleton(String reason, PCollection actual) { + return new PCollectionViewAssert<>(actual, View.asSingleton(), actual.getCoder(), reason); } /** @@ -319,12 +344,25 @@ public static SingletonAssert thatSingleton(PCollection actual) { */ public static SingletonAssert>> thatMultimap( PCollection> actual) { + return thatMultimap(actual.getName(), actual); + } + + /** + * Constructs a {@link SingletonAssert} for the value of the provided {@link PCollection} with the + * specified reason. + * + *

Note that the actual value must be coded by a {@link KvCoder}, not just any + * {@code Coder}. + */ + public static SingletonAssert>> thatMultimap( + String reason, PCollection> actual) { @SuppressWarnings("unchecked") KvCoder kvCoder = (KvCoder) actual.getCoder(); return new PCollectionViewAssert<>( actual, View.asMultimap(), - MapCoder.of(kvCoder.getKeyCoder(), IterableCoder.of(kvCoder.getValueCoder()))); + MapCoder.of(kvCoder.getKeyCoder(), IterableCoder.of(kvCoder.getValueCoder())), + reason); } /** @@ -335,10 +373,23 @@ public static SingletonAssert>> thatMultimap( * {@code Coder}. */ public static SingletonAssert> thatMap(PCollection> actual) { + return thatMap(actual.getName(), actual); + } + + /** + * Constructs a {@link SingletonAssert} for the value of the provided {@link PCollection} with + * the specified reason. The {@link PCollection} must have at most one value per key. + * + *

Note that the actual value must be coded by a {@link KvCoder}, not just any + * {@code Coder}. + */ + public static SingletonAssert> thatMap( + String reason, PCollection> actual) { @SuppressWarnings("unchecked") KvCoder kvCoder = (KvCoder) actual.getCoder(); return new PCollectionViewAssert<>( - actual, View.asMap(), MapCoder.of(kvCoder.getKeyCoder(), kvCoder.getValueCoder())); + actual, View.asMap(), MapCoder.of(kvCoder.getKeyCoder(), kvCoder.getValueCoder()), + reason); } //////////////////////////////////////////////////////////// @@ -351,18 +402,21 @@ private static class PCollectionContentsAssert implements IterableAssert { private final PCollection actual; private final AssertionWindows rewindowingStrategy; private final SimpleFunction>, Iterable> paneExtractor; + private final String reason; - public PCollectionContentsAssert(PCollection actual) { - this(actual, IntoGlobalWindow.of(), PaneExtractors.allPanes()); + public PCollectionContentsAssert(String reason, PCollection actual) { + this(actual, IntoGlobalWindow.of(), PaneExtractors.allPanes(), reason); } public PCollectionContentsAssert( PCollection actual, AssertionWindows rewindowingStrategy, - SimpleFunction>, Iterable> paneExtractor) { + SimpleFunction>, Iterable> paneExtractor, + String reason) { this.actual = actual; this.rewindowingStrategy = rewindowingStrategy; this.paneExtractor = paneExtractor; + this.reason = reason; } @Override @@ -397,7 +451,7 @@ private PCollectionContentsAssert withPane( Coder windowCoder = (Coder) actual.getWindowingStrategy().getWindowFn().windowCoder(); return new PCollectionContentsAssert<>( - actual, IntoStaticWindows.of(windowCoder, window), paneExtractor); + actual, IntoStaticWindows.of(windowCoder, window), paneExtractor, reason); } /** @@ -418,7 +472,7 @@ public final PCollectionContentsAssert containsInAnyOrder(T... expectedElemen */ @Override public PCollectionContentsAssert containsInAnyOrder(Iterable expectedElements) { - return satisfies(new AssertContainsInAnyOrderRelation(), expectedElements); + return satisfies(new AssertContainsInAnyOrderRelation(reason), expectedElements); } @Override @@ -471,7 +525,7 @@ PCollectionContentsAssert satisfies( // more flexible bounds. @SuppressWarnings({"rawtypes", "unchecked"}) SerializableFunction, Void> checkerFn = - (SerializableFunction) new MatcherCheckerFn<>(matcher); + (SerializableFunction) new MatcherCheckerFn<>(reason, matcher); actual.apply( "PAssert$" + (assertCount++), new GroupThenAssert<>(checkerFn, rewindowingStrategy, paneExtractor)); @@ -479,15 +533,17 @@ PCollectionContentsAssert satisfies( } private static class MatcherCheckerFn implements SerializableFunction { - private SerializableMatcher matcher; + private final String reason; + private final SerializableMatcher matcher; - public MatcherCheckerFn(SerializableMatcher matcher) { + public MatcherCheckerFn(String reason, SerializableMatcher matcher) { + this.reason = reason; this.matcher = matcher; } @Override public Void apply(T actual) { - assertThat(actual, matcher); + assertThat(reason, actual, matcher); return null; } } @@ -526,16 +582,19 @@ private static class PCollectionSingletonIterableAssert implements IterableAs private final AssertionWindows rewindowingStrategy; private final SimpleFunction>>, Iterable>> paneExtractor; + private final String reason; - public PCollectionSingletonIterableAssert(PCollection> actual) { - this(actual, IntoGlobalWindow.>of(), PaneExtractors.>onlyPane()); + public PCollectionSingletonIterableAssert(String reason, PCollection> actual) { + this(actual, IntoGlobalWindow.>of(), PaneExtractors.>onlyPane(), + reason); } public PCollectionSingletonIterableAssert( PCollection> actual, AssertionWindows rewindowingStrategy, SimpleFunction>>, Iterable>> - paneExtractor) { + paneExtractor, + String reason) { this.actual = actual; @SuppressWarnings("unchecked") @@ -544,6 +603,7 @@ public PCollectionSingletonIterableAssert( this.rewindowingStrategy = rewindowingStrategy; this.paneExtractor = paneExtractor; + this.reason = reason; } @Override @@ -579,7 +639,7 @@ private PCollectionSingletonIterableAssert withPanes( Coder windowCoder = (Coder) actual.getWindowingStrategy().getWindowFn().windowCoder(); return new PCollectionSingletonIterableAssert<>( - actual, IntoStaticWindows.>of(windowCoder, window), paneExtractor); + actual, IntoStaticWindows.>of(windowCoder, window), paneExtractor, reason); } @Override @@ -595,7 +655,7 @@ public PCollectionSingletonIterableAssert empty() { @Override public PCollectionSingletonIterableAssert containsInAnyOrder(Iterable expectedElements) { - return satisfies(new AssertContainsInAnyOrderRelation(), expectedElements); + return satisfies(new AssertContainsInAnyOrderRelation(reason), expectedElements); } @Override @@ -626,12 +686,15 @@ private static class PCollectionViewAssert implements SingletonAss private final SimpleFunction>, Iterable> paneExtractor; private final Coder coder; + private final String reason; protected PCollectionViewAssert( PCollection actual, PTransform, PCollectionView> view, - Coder coder) { - this(actual, view, IntoGlobalWindow.of(), PaneExtractors.onlyPane(), coder); + Coder coder, + String reason) { + this(actual, view, IntoGlobalWindow.of(), PaneExtractors.onlyPane(), coder, + reason); } private PCollectionViewAssert( @@ -639,12 +702,14 @@ private PCollectionViewAssert( PTransform, PCollectionView> view, AssertionWindows rewindowActuals, SimpleFunction>, Iterable> paneExtractor, - Coder coder) { + Coder coder, + String reason) { this.actual = actual; this.view = view; this.rewindowActuals = rewindowActuals; this.paneExtractor = paneExtractor; this.coder = coder; + this.reason = reason; } @Override @@ -671,17 +736,18 @@ private PCollectionViewAssert inPane( IntoStaticWindows.of( (Coder) actual.getWindowingStrategy().getWindowFn().windowCoder(), window), paneExtractor, - coder); + coder, + reason); } @Override public PCollectionViewAssert isEqualTo(ViewT expectedValue) { - return satisfies(new AssertIsEqualToRelation(), expectedValue); + return satisfies(new AssertIsEqualToRelation(reason), expectedValue); } @Override public PCollectionViewAssert notEqualTo(ViewT expectedValue) { - return satisfies(new AssertNotEqualToRelation(), expectedValue); + return satisfies(new AssertNotEqualToRelation(reason), expectedValue); } @Override @@ -1035,15 +1101,8 @@ private SideInputCheckerDoFn( @ProcessElement public void processElement(ProcessContext c) { - try { - ActualT actualContents = c.sideInput(actual); - doChecks(actualContents, checkerFn, success, failure); - } catch (Throwable t) { - // Suppress exception in streaming - if (!c.getPipelineOptions().as(StreamingOptions.class).isStreaming()) { - throw t; - } - } + ActualT actualContents = c.sideInput(actual); + doChecks(actualContents, checkerFn, success, failure); } } @@ -1119,15 +1178,17 @@ private static void doChecks( * value. */ private static class AssertIsEqualTo implements SerializableFunction { - private T expected; + private final String reason; + private final T expected; - public AssertIsEqualTo(T expected) { + public AssertIsEqualTo(String reason, T expected) { + this.reason = reason; this.expected = expected; } @Override public Void apply(T actual) { - assertThat(actual, equalTo(expected)); + assertThat(reason, actual, equalTo(expected)); return null; } } @@ -1137,15 +1198,17 @@ public Void apply(T actual) { * value. */ private static class AssertNotEqualTo implements SerializableFunction { + private String reason; private T expected; - public AssertNotEqualTo(T expected) { + public AssertNotEqualTo(String reason, T expected) { + this.reason = reason; this.expected = expected; } @Override public Void apply(T actual) { - assertThat(actual, not(equalTo(expected))); + assertThat(reason, actual, not(equalTo(expected))); return null; } } @@ -1156,25 +1219,27 @@ public Void apply(T actual) { */ private static class AssertContainsInAnyOrder implements SerializableFunction, Void> { - private T[] expected; + private final String reason; + private final T[] expected; @SafeVarargs - public AssertContainsInAnyOrder(T... expected) { + public AssertContainsInAnyOrder(String reason, T... expected) { + this.reason = reason; this.expected = expected; } @SuppressWarnings("unchecked") - public AssertContainsInAnyOrder(Collection expected) { - this((T[]) expected.toArray()); + public AssertContainsInAnyOrder(String reason, Collection expected) { + this(reason, (T[]) expected.toArray()); } - public AssertContainsInAnyOrder(Iterable expected) { - this(Lists.newArrayList(expected)); + public AssertContainsInAnyOrder(String reason, Iterable expected) { + this(reason, Lists.newArrayList(expected)); } @Override public Void apply(Iterable actual) { - assertThat(actual, containsInAnyOrder(expected)); + assertThat(reason, actual, containsInAnyOrder(expected)); return null; } } @@ -1194,9 +1259,15 @@ private interface AssertRelation extends Serializable { * An {@link AssertRelation} implementing the binary predicate that two objects are equal. */ private static class AssertIsEqualToRelation implements AssertRelation { + private final String reason; + + public AssertIsEqualToRelation(String reason) { + this.reason = reason; + } + @Override public SerializableFunction assertFor(T expected) { - return new AssertIsEqualTo(expected); + return new AssertIsEqualTo(reason, expected); } } @@ -1204,9 +1275,15 @@ public SerializableFunction assertFor(T expected) { * An {@link AssertRelation} implementing the binary predicate that two objects are not equal. */ private static class AssertNotEqualToRelation implements AssertRelation { + private final String reason; + + public AssertNotEqualToRelation(String reason) { + this.reason = reason; + } + @Override public SerializableFunction assertFor(T expected) { - return new AssertNotEqualTo(expected); + return new AssertNotEqualTo(reason, expected); } } @@ -1216,9 +1293,15 @@ public SerializableFunction assertFor(T expected) { */ private static class AssertContainsInAnyOrderRelation implements AssertRelation, Iterable> { + private final String reason; + + public AssertContainsInAnyOrderRelation(String reason) { + this.reason = reason; + } + @Override public SerializableFunction, Void> assertFor(Iterable expectedElements) { - return new AssertContainsInAnyOrder(expectedElements); + return new AssertContainsInAnyOrder(reason, expectedElements); } } diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/PAssertTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/PAssertTest.java index 777e1af8a13d..1603db55b1d0 100644 --- a/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/PAssertTest.java +++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/testing/PAssertTest.java @@ -296,6 +296,42 @@ public void testNotEqualTo() throws Exception { pipeline.run(); } + /** + * Test that we throw an error for false assertion on singleton. + */ + @Test + @Category(RunnableOnService.class) + public void testPAssertEqualsSingletonFalse() throws Exception { + PCollection pcollection = pipeline.apply(Create.of(42)); + PAssert.thatSingleton("The value was not equal to 44", pcollection).isEqualTo(44); + + Throwable thrown = runExpectingAssertionFailure(pipeline); + + String message = thrown.getMessage(); + + assertThat(message, containsString("The value was not equal to 44")); + assertThat(message, containsString("Expected: <44>")); + assertThat(message, containsString("but: was <42>")); + } + + /** + * Test that we throw an error for false assertion on singleton. + */ + @Test + @Category(RunnableOnService.class) + public void testPAssertEqualsSingletonFalseDefaultReasonString() throws Exception { + PCollection pcollection = pipeline.apply(Create.of(42)); + PAssert.thatSingleton(pcollection).isEqualTo(44); + + Throwable thrown = runExpectingAssertionFailure(pipeline); + + String message = thrown.getMessage(); + + assertThat(message, containsString("Create.Values/Read(CreateSource).out")); + assertThat(message, containsString("Expected: <44>")); + assertThat(message, containsString("but: was <42>")); + } + /** * Tests that {@code containsInAnyOrder} is actually order-independent. */ @@ -388,12 +424,30 @@ public void testContainsInAnyOrderFalse() throws Exception { @Test @Category(RunnableOnService.class) public void testEmptyFalse() throws Exception { + PCollection vals = pipeline.apply(CountingInput.upTo(5L)); + PAssert.that("Vals should have been empty", vals).empty(); + + Throwable thrown = runExpectingAssertionFailure(pipeline); + + String message = thrown.getMessage(); + + assertThat(message, containsString("Vals should have been empty")); + assertThat(message, containsString("Expected: iterable over [] in any order")); + } + + @Test + @Category(RunnableOnService.class) + public void testEmptyFalseDefaultReasonString() throws Exception { PCollection vals = pipeline.apply(CountingInput.upTo(5L)); PAssert.that(vals).empty(); Throwable thrown = runExpectingAssertionFailure(pipeline); - assertThat(thrown.getMessage(), containsString("Expected: iterable over [] in any order")); + String message = thrown.getMessage(); + + assertThat(message, + containsString("CountingInput.BoundedCountingInput/Read(BoundedCountingSource).out")); + assertThat(message, containsString("Expected: iterable over [] in any order")); } private static Throwable runExpectingAssertionFailure(Pipeline pipeline) {