Add Java DSL Flow#mapError version without PartialFunction (#24992) (#26310)

* Add an overloaded version of the Flow#mapError (Java DSL) which does not use a Scala PartialFunction.
* Add test verifying mapError matching on parent class
* Add to Source, SubSource and SubFlow as well
This commit is contained in:
Andrey Yamshchikov 2019-12-10 07:05:56 -05:00 committed by Patrik Nordwall
parent 653d05e7d6
commit 87b94b65fd
5 changed files with 147 additions and 0 deletions

View file

@ -13,6 +13,7 @@ import akka.japi.function.*;
import akka.japi.pf.PFBuilder;
import akka.stream.*;
import akka.stream.scaladsl.FlowSpec;
import akka.stream.testkit.javadsl.TestSink;
import akka.util.ConstantFun;
import akka.stream.javadsl.GraphDSL.Builder;
import akka.stream.stage.*;
@ -1080,6 +1081,54 @@ public class FlowTest extends StreamTest {
future.toCompletableFuture().get(3, TimeUnit.SECONDS);
}
@Test
public void mustBeAbleToMapErrorClass() {
final String head = "foo";
final Source<Optional<String>, NotUsed> source =
Source.from(Arrays.asList(Optional.of(head), Optional.empty()));
final IllegalArgumentException boom = new IllegalArgumentException("boom");
final Flow<Optional<String>, String, NotUsed> flow =
Flow.<Optional<String>, String>fromFunction(Optional::get)
.mapError(NoSuchElementException.class, (NoSuchElementException e) -> boom);
source
.via(flow)
.runWith(TestSink.probe(system), system)
.request(2)
.expectNext(head)
.expectError(boom);
}
@Test
public void mustBeAbleToMapErrorClassExactly() {
final Source<String, NotUsed> source = Source.single("foo");
final Flow<String, Character, NotUsed> flow =
Flow.<String, Character>fromFunction(str -> str.charAt(-1))
.mapError(NoSuchElementException.class, IllegalArgumentException::new);
final Throwable actual =
source.via(flow).runWith(TestSink.probe(system), system).request(1).expectError();
org.junit.Assert.assertTrue(actual instanceof IndexOutOfBoundsException);
}
@Test
public void mustBeAbleToMapErrorSuperClass() {
final String head = "foo";
final Source<Optional<String>, NotUsed> source =
Source.from(Arrays.asList(Optional.of(head), Optional.empty()));
final IllegalArgumentException boom = new IllegalArgumentException("boom");
final Flow<Optional<String>, String, NotUsed> flow =
Flow.<Optional<String>, String>fromFunction(Optional::get)
.mapError(RuntimeException.class, (RuntimeException e) -> boom);
source
.via(flow)
.runWith(TestSink.probe(system), system)
.request(2)
.expectNext(head)
.expectError(boom);
}
@Test
public void mustBeAbleToMaterializeIdentityWithJavaFlow() throws Exception {
final TestKit probe = new TestKit(system);