Merge branch 'master' into BAEL-16646-2

This commit is contained in:
Alessio Stalla
2019-10-24 13:20:08 +02:00
parent db85c8f275
commit c499158763
20506 changed files with 1643665 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
logs
target
/.idea
/.g8
/.idea_modules
/.classpath
/.project
/.settings
/RUNNING_PID
@@ -0,0 +1,68 @@
package controllers;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.*;
import play.twirl.api.Html;
import javax.inject.Inject;
import java.util.concurrent.CompletionStage;
import static java.util.concurrent.CompletableFuture.supplyAsync;
/**
* This controller contains an action to handle HTTP requests
* to the application's home page.
*/
public class HomeController extends Controller {
private HttpExecutionContext ec;
@Inject
public HomeController(HttpExecutionContext ec) {
this.ec = ec;
}
/**
* An action that renders an HTML page with a welcome message.
* The configuration in the <code>routes</code> file means that
* this method will be called when the application receives a
* <code>GET</code> request with a path of <code>/</code>.
*/
public Result index() {
return ok(views.html.index.render());
}
public Result applyHtml() {
return ok(Html.apply("<h1>This text will appear as a heading 1</h1>"));
}
public Result badRequestPage() {
return badRequest("Your request data has issues.");
}
public Result notFoundPage() {
return notFound("Could not find the page you requested.");
}
public Result customContentType() {
return ok("This is some text content").as("text/html");
}
public CompletionStage<Result> asyncOperation() {
return supplyAsync(() -> {
return longRunningTask();
}, ec.current())
.thenApplyAsync(s -> {
return ok("Got result -> " + s);
}, ec.current());
}
private String longRunningTask() {
return "Long running task has completed";
}
public Result setHeaders() {
return ok("This is some text content")
.as("text/html")
.withHeader("Header-Key", "Some value");
}
}
@@ -0,0 +1,5 @@
@()
@main("Welcome to Play") {
<h1>Welcome to Play!</h1>
}
@@ -0,0 +1,24 @@
@*
* This template is called from the `index` template. This template
* handles the rendering of the page header and body tags. It takes
* two arguments, a `String` for the title of the page and an `Html`
* object to insert into the body of the page.
*@
@(title: String)(content: Html)
<!DOCTYPE html>
<html lang="en">
<head>
@* Here's where we render the page title `String`. *@
<title>@title</title>
<link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
<link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">
</head>
<body>
@* And here's where we render the `Html` object containing
* the page content. *@
@content
<script src="@routes.Assets.versioned("javascripts/main.js")" type="text/javascript"></script>
</body>
</html>
+10
View File
@@ -0,0 +1,10 @@
name := """introduction"""
organization := "com.baeldung"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayJava)
scalaVersion := "2.13.0"
libraryDependencies += guice
@@ -0,0 +1,2 @@
# This is the main configuration file for the application.
# https://www.playframework.com/documentation/latest/ConfigFile
@@ -0,0 +1,35 @@
<!-- https://www.playframework.com/documentation/latest/SettingsLogger -->
<configuration>
<conversionRule conversionWord="coloredLevel" converterClass="play.api.libs.logback.ColoredLevel" />
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${application.home:-.}/logs/application.log</file>
<encoder>
<pattern>%date [%level] from %logger in %thread - %message%n%xException</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%coloredLevel %logger{15} - %message%n%xException{10}</pattern>
</encoder>
</appender>
<appender name="ASYNCFILE" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="FILE" />
</appender>
<appender name="ASYNCSTDOUT" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="STDOUT" />
</appender>
<logger name="play" level="INFO" />
<logger name="application" level="DEBUG" />
<root level="WARN">
<appender-ref ref="ASYNCFILE" />
<appender-ref ref="ASYNCSTDOUT" />
</root>
</configuration>
+15
View File
@@ -0,0 +1,15 @@
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# An example controller showing a sample home page
GET / controllers.HomeController.index
GET /baeldung/html controllers.HomeController.applyHtml
GET /baeldung/bad-req controllers.HomeController.badRequestPage
GET /baeldung/not-found controllers.HomeController.notFoundPage
GET /baeldung/custom-content-type controllers.HomeController.customContentType
GET /baeldung/async controllers.HomeController.asyncOperation
GET /baeldung/headers controllers.HomeController.setHeaders
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
@@ -0,0 +1 @@
sbt.version=1.2.8
@@ -0,0 +1,7 @@
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.3")
// Defines scaffolding (found under .g8 folder)
// http://www.foundweekends.org/giter8/scaffolding.html
// sbt "g8Scaffold form"
addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.11.0")
Binary file not shown.

After

Width:  |  Height:  |  Size: 687 B

@@ -0,0 +1,97 @@
package controllers;
import org.junit.Test;
import play.Application;
import play.inject.guice.GuiceApplicationBuilder;
import play.mvc.Http;
import play.mvc.Result;
import play.test.WithApplication;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static play.mvc.Http.Status.*;
import static play.test.Helpers.GET;
import static play.test.Helpers.route;
public class HomeControllerUnitTest extends WithApplication {
@Override
protected Application provideApplication() {
return new GuiceApplicationBuilder().build();
}
@Test
public void givenRequest_whenRootPath_ThenStatusOkay() {
Http.RequestBuilder request = new Http.RequestBuilder()
.method(GET)
.uri("/");
Result result = route(app, request);
assertEquals(OK, result.status());
}
@Test
public void givenRequest_whenHtmlPath_ThenStatusOkay() {
Http.RequestBuilder request = new Http.RequestBuilder()
.method(GET)
.uri("/baeldung/html");
Result result = route(app, request);
assertEquals(OK, result.status());
}
@Test
public void givenRequest_whenBadRequest_ThenStatusBadRequest() {
Http.RequestBuilder request = new Http.RequestBuilder()
.method(GET)
.uri("/baeldung/bad-req");
Result result = route(app, request);
assertEquals(BAD_REQUEST, result.status());
}
@Test
public void givenRequest_whenNotFound_ThenStatusNotFound() {
Http.RequestBuilder request = new Http.RequestBuilder()
.method(GET)
.uri("/baeldung/not-found");
Result result = route(app, request);
assertEquals(NOT_FOUND, result.status());
}
@Test
public void givenRequest_whenCustomContentTypePath_ThenContextTypeTextHtml() {
Http.RequestBuilder request = new Http.RequestBuilder()
.method(GET)
.uri("/baeldung/custom-content-type");
Result result = route(app, request);
assertTrue(result.contentType().isPresent());
assertEquals("text/html", result.contentType().get());
}
@Test
public void givenRequest_whenAsyncPath_ThenStatusOkay() {
Http.RequestBuilder request = new Http.RequestBuilder()
.method(GET)
.uri("/baeldung/async");
Result result = route(app, request);
assertEquals(OK, result.status());
}
@Test
public void givenRequest_whenHeadersPath_ThenCustomHeaderFieldSet() {
Http.RequestBuilder request = new Http.RequestBuilder()
.method(GET)
.uri("/baeldung/headers");
Result result = route(app, request);
final Optional<String> headerOptional = result.header("Header-Key");
assertTrue(headerOptional.isPresent());
assertEquals("Some value", headerOptional.get());
}
}