+doc #20466 Include Future Directives Examples for Java. (#20619)

This commit is contained in:
Felipe Fernández 2016-05-30 15:42:14 +01:00 committed by Konrad Malawski
parent 5417942c5d
commit 2d6ec9c4cf
4 changed files with 103 additions and 3 deletions

View file

@ -0,0 +1,100 @@
/*
* Copyright (C) 2016-2016 Lightbend Inc. <http://www.lightbend.com>
*/
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<Throwable, Route>()
.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
}
}

View file

@ -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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FutureDirectivesExamplesTest.java#completeOrRecoverWith

View file

@ -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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FutureDirectivesExamplesTest.java#onComplete

View file

@ -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 <https://github.com/akka/akka/issues/20466>`_.
.. includecode:: ../../../../code/docs/http/javadsl/server/directives/FutureDirectivesExamplesTest.java#onSuccess