Package com.hazelcast.jet.pipeline.test
Class Assertions
java.lang.Object
com.hazelcast.jet.pipeline.test.Assertions
Various assertions which can be used to assert items passing through the
pipeline for correctness. Each assertion also returns the stage it is
attached to so the assertions could be used in-line.
The assertions in this class are to be used together with the apply()
operator on the pipeline. For assertions that can be used directly
as sinks, see AssertionSinks
.
- Since:
- Jet 3.2
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> FunctionEx<BatchStage<T>,
BatchStage<T>> assertAnyOrder
(String message, Collection<? extends T> expected) Asserts that the previous stage emitted the expected items in any order, but nothing else.static <T> FunctionEx<BatchStage<T>,
BatchStage<T>> assertAnyOrder
(Collection<? extends T> expected) Asserts that the previous stage emitted the expected items in any order, but nothing else.static <T> FunctionEx<BatchStage<T>,
BatchStage<T>> assertCollected
(ConsumerEx<? super List<T>> assertFn) Collects all the received items in a list and once the upstream stage is completed, it executes the assertion supplied byassertFn
.static <T> FunctionEx<StreamStage<T>,
StreamStage<T>> assertCollectedEventually
(int timeout, ConsumerEx<? super List<T>> assertFn) Collects all the received items into a list and runs theassertFn
every time a new item is received.static <T> FunctionEx<BatchStage<T>,
BatchStage<T>> assertContains
(String message, Collection<? extends T> expected) Asserts that the previous stage emitted all of the given items in any order.static <T> FunctionEx<BatchStage<T>,
BatchStage<T>> assertContains
(Collection<? extends T> expected) Asserts that the previous stage emitted all of the given items in any order.static <T> FunctionEx<BatchStage<T>,
BatchStage<T>> assertOrdered
(String message, Collection<? extends T> expected) Asserts that the previous stage emitted the exact sequence of expected items and nothing else.static <T> FunctionEx<BatchStage<T>,
BatchStage<T>> assertOrdered
(Collection<? extends T> expected) Asserts that the previous stage emitted the exact sequence of expected items and nothing else.
-
Method Details
-
assertOrdered
@Nonnull public static <T> FunctionEx<BatchStage<T>,BatchStage<T>> assertOrdered(@Nullable String message, @Nonnull Collection<? extends T> expected) Asserts that the previous stage emitted the exact sequence of expected items and nothing else. If the assertion fails, the job will fail with anAssertionError
with the given message.Example:
Note: Since Jet jobs are distributed, input from multiple upstream processors is merged in a non-deterministic way. Therefore this assertion is recommended only for testing of non-distributed sources.p.readFrom(TestSources.items(1, 2, 3, 4)) .apply(Assertions.assertOrdered("unexpected values", Arrays.asList(1, 2, 3, 4))) .writeTo(Sinks.logger());
-
assertOrdered
@Nonnull public static <T> FunctionEx<BatchStage<T>,BatchStage<T>> assertOrdered(@Nonnull Collection<? extends T> expected) Asserts that the previous stage emitted the exact sequence of expected items and nothing else. If the assertion fails, the job will fail with anAssertionError
.Example:
Note: Since Jet jobs are distributed, input from multiple upstream processors is merged in a non-deterministic way. Therefore this assertion is recommended only for testing of non-distributed sources.p.readFrom(TestSources.items(1, 2, 3, 4)) .apply(Assertions.assertOrderedArrays.asList(1, 2, 3, 4))) .writeTo(Sinks.logger());
-
assertAnyOrder
@Nonnull public static <T> FunctionEx<BatchStage<T>,BatchStage<T>> assertAnyOrder(@Nullable String message, @Nonnull Collection<? extends T> expected) Asserts that the previous stage emitted the expected items in any order, but nothing else. If the assertion fails, the job will fail with anAssertionError
with the given message.Example:
p.readFrom(TestSources.items(4, 3, 2, 1)) .apply(Assertions.assertAnyOrder("unexpected values", Arrays.asList(1, 2, 3, 4))) .writeTo(Sinks.logger())
-
assertAnyOrder
@Nonnull public static <T> FunctionEx<BatchStage<T>,BatchStage<T>> assertAnyOrder(@Nonnull Collection<? extends T> expected) Asserts that the previous stage emitted the expected items in any order, but nothing else. If the assertion fails, the job will fail with anAssertionError
.Example:
p.readFrom(TestSources.items(4, 3, 2, 1)) .apply(Assertions.assertAnyOrder(Arrays.asList(1, 2, 3, 4))) .writeTo(Sinks.logger())
-
assertContains
@Nonnull public static <T> FunctionEx<BatchStage<T>,BatchStage<T>> assertContains(@Nullable String message, @Nonnull Collection<? extends T> expected) Asserts that the previous stage emitted all of the given items in any order. If the assertion fails, the job will fail with anAssertionError
with the given message.Example:
p.readFrom(TestSources.items(4, 3, 2, 1)) .apply(Assertions.assertAnyOrder(Arrays.asList(1, 3))) .writeTo(Sinks.logger())
-
assertContains
@Nonnull public static <T> FunctionEx<BatchStage<T>,BatchStage<T>> assertContains(@Nonnull Collection<? extends T> expected) Asserts that the previous stage emitted all of the given items in any order. If the assertion fails, the job will fail with aAssertionError
with the given message.Example:
p.readFrom(TestSources.items(4, 3, 2, 1)) .apply(Assertions.assertContains(Arrays.asList(1, 3))) .writeTo(Sinks.logger())
-
assertCollected
@Nonnull public static <T> FunctionEx<BatchStage<T>,BatchStage<T>> assertCollected(@Nonnull ConsumerEx<? super List<T>> assertFn) Collects all the received items in a list and once the upstream stage is completed, it executes the assertion supplied byassertFn
. If no items were collected, it will be called with empty list.Example:
Note: This assertion is not usable in streaming jobs. For the streaming equivalent seep.readFrom(TestSources.items(1, 2, 3, 4)) .apply(Assertions.assertCollected(items -> assertTrue("expected minimum of 4 items", items.size() >= 4))) .writeTo(Sinks.logger())
assertCollectedEventually(int, com.hazelcast.function.ConsumerEx<? super java.util.List<T>>)
. -
assertCollectedEventually
@Nonnull public static <T> FunctionEx<StreamStage<T>,StreamStage<T>> assertCollectedEventually(int timeout, @Nonnull ConsumerEx<? super List<T>> assertFn) Collects all the received items into a list and runs theassertFn
every time a new item is received. AnAssertionError
thrown from theassertFn
will be ignored untiltimeoutSeconds
have passed, after which the lastAssertionError
will be rethrown. IfassertFn
throws any other exception, it will be rethrown immediately.When
assertFn
completes without any error, the sink will throw anAssertionCompletedException
to indicate success. Exception is used to terminate the job so that you canjoin()
it.Example:
To use this assertion in a test, you need to catch the thrown exception and validate that it's of the expected type as follows:p.readFrom(TestSources.itemStream(10)) .withoutTimestamps() .apply(assertCollectedEventually(5, c -> assertTrue("did not receive at least 20 items", c.size() > 20)));
Note: This assertions requires that there are no other assertions in the job as this one can complete the job before the other ones succeeded.try { jetInstance.newJob(p).join(); Assert.fail("Job should have completed with an AssertionCompletedException, " + "but completed normally"); } catch (CompletionException e) { String errorMsg = e.getCause().getMessage(); Assert.assertTrue( "Job was expected to complete with AssertionCompletedException, but completed with: " + e.getCause(), errorMsg.contains(AssertionCompletedException.class.getName()) ); }
-