diff --git a/akka-docs/rst/java/code/docs/http/javadsl/server/directives/FutureDirectivesExamplesTest.java b/akka-docs/rst/java/code/docs/http/javadsl/server/directives/FutureDirectivesExamplesTest.java new file mode 100644 index 0000000000..726cfc5754 --- /dev/null +++ b/akka-docs/rst/java/code/docs/http/javadsl/server/directives/FutureDirectivesExamplesTest.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2016-2016 Lightbend Inc. + */ + +package docs.http.javadsl.server.directives; + +import java.util.concurrent.CompletableFuture; +import akka.http.javadsl.model.HttpRequest; +import akka.http.javadsl.server.Marshaller; +import akka.http.javadsl.server.Route; +import akka.http.javadsl.testkit.JUnitRouteTest; +import akka.http.scaladsl.model.StatusCodes; +import akka.japi.pf.PFBuilder; +import org.junit.Test; + +import static akka.http.javadsl.server.PathMatchers.*; +import static scala.compat.java8.JFunction.func; + +public class FutureDirectivesExamplesTest extends JUnitRouteTest { + + @Test + public void testOnComplete() { + //#onComplete + // import static scala.compat.java8.JFunction.func; + // import static akka.http.javadsl.server.PathMatchers.*; + + final Route route = path(segment("divide").slash(integerSegment()).slash(integerSegment()), + (a, b) -> onComplete( + () -> CompletableFuture.supplyAsync(() -> a / b), + maybeResult -> maybeResult + .map(func(result -> complete("The result was " + result))) + .recover(new PFBuilder() + .matchAny(ex -> complete(StatusCodes.InternalServerError(), + "An error occurred: " + ex.getMessage()) + ) + .build()) + .get() + ) + ); + + testRoute(route).run(HttpRequest.GET("/divide/10/2")) + .assertEntity("The result was 5"); + + testRoute(route).run(HttpRequest.GET("/divide/10/0")) + .assertStatusCode(StatusCodes.InternalServerError()) + .assertEntity("An error occurred: / by zero"); + //#onComplete + } + + @Test + public void testOnSuccess() { + //#onSuccess + final Route route = path("success", () -> + onSuccess(() -> CompletableFuture.supplyAsync(() -> "Ok"), + extraction -> complete(extraction) + ) + ).orElse(path("failure", () -> + onSuccess(() -> CompletableFuture.supplyAsync(() -> { + throw new RuntimeException(); + }), + extraction -> complete("never reaches here")) + )); + + testRoute(route).run(HttpRequest.GET("/success")) + .assertEntity("Ok"); + + testRoute(route).run(HttpRequest.GET("/failure")) + .assertStatusCode(StatusCodes.InternalServerError()) + .assertEntity("There was an internal server error."); + //#onSuccess + } + + @Test + public void testCompleteOrRecoverWith() { + //#completeOrRecoverWith + final Route route = path("success", () -> + completeOrRecoverWith( + () -> CompletableFuture.supplyAsync(() -> "Ok"), + Marshaller.stringToEntity(), + extraction -> failWith(extraction) // not executed + ) + ).orElse(path("failure", () -> + completeOrRecoverWith( + () -> CompletableFuture.supplyAsync(() -> { + throw new RuntimeException(); + }), + Marshaller.stringToEntity(), + extraction -> failWith(extraction)) + )); + + testRoute(route).run(HttpRequest.GET("/success")) + .assertEntity("Ok"); + + testRoute(route).run(HttpRequest.GET("/failure")) + .assertStatusCode(StatusCodes.InternalServerError()) + .assertEntity("There was an internal server error."); + //#completeOrRecoverWith + } + +} diff --git a/akka-docs/rst/java/http/routing-dsl/directives/future-directives/completeOrRecoverWith.rst b/akka-docs/rst/java/http/routing-dsl/directives/future-directives/completeOrRecoverWith.rst index 9f309a5678..d889436972 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/future-directives/completeOrRecoverWith.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/future-directives/completeOrRecoverWith.rst @@ -15,4 +15,4 @@ To handle the successful case manually as well, use the :ref:`-onComplete-java-` Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FutureDirectivesExamplesTest.java#completeOrRecoverWith diff --git a/akka-docs/rst/java/http/routing-dsl/directives/future-directives/onComplete.rst b/akka-docs/rst/java/http/routing-dsl/directives/future-directives/onComplete.rst index 5af35e8922..c521a84838 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/future-directives/onComplete.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/future-directives/onComplete.rst @@ -15,4 +15,4 @@ To complete with a successful result automatically and just handle the failure r Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FutureDirectivesExamplesTest.java#onComplete diff --git a/akka-docs/rst/java/http/routing-dsl/directives/future-directives/onSuccess.rst b/akka-docs/rst/java/http/routing-dsl/directives/future-directives/onSuccess.rst index 55a9522713..569a875f1b 100644 --- a/akka-docs/rst/java/http/routing-dsl/directives/future-directives/onSuccess.rst +++ b/akka-docs/rst/java/http/routing-dsl/directives/future-directives/onSuccess.rst @@ -14,4 +14,4 @@ To handle the ``Failure`` case manually as well, use :ref:`-onComplete-java-`, i Example ------- -TODO: Example snippets for JavaDSL are subject to community contributions! Help us complete the docs, read more about it here: `write example snippets for Akka HTTP Java DSL #20466 `_. +.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FutureDirectivesExamplesTest.java#onSuccess