diff --git a/blade/README.md b/blade/README.md new file mode 100644 index 0000000000..d823de775f --- /dev/null +++ b/blade/README.md @@ -0,0 +1,5 @@ +### Relevant Articles: + +- [Blade - A Complete GuideBook](http://www.baeldung.com/blade) + +Run Integration Tests with `mvn integration-test` \ No newline at end of file diff --git a/blade/pom.xml b/blade/pom.xml new file mode 100644 index 0000000000..6bad505f4a --- /dev/null +++ b/blade/pom.xml @@ -0,0 +1,189 @@ + + + 4.0.0 + blade + blade + + + com.baeldung + 1.0.0-SNAPSHOT + + + + + + + + + + 1.8 + 1.8 + + + + + com.bladejava + blade-mvc + 2.0.14.RELEASE + + + + org.webjars + bootstrap + 4.2.1 + + + + org.apache.commons + commons-lang3 + 3.8.1 + + + + + org.projectlombok + lombok + 1.18.4 + provided + + + + + junit + junit + 4.12 + test + + + org.assertj + assertj-core + 3.11.1 + test + + + org.apache.httpcomponents + httpclient + 4.5.6 + test + + + org.apache.httpcomponents + httpmime + 4.5.6 + test + + + org.apache.httpcomponents + httpcore + 4.4.10 + test + + + + sample-blade-app + + + + org.apache.maven.plugins + maven-surefire-plugin + + 3 + true + + **/*LiveTest.java + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.0.0-M3 + + + **/*LiveTest.java + + + + + + integration-test + verify + + + + + + + com.bazaarvoice.maven.plugins + process-exec-maven-plugin + 0.7 + + + + blade-process + pre-integration-test + + start + + + Blade + false + + java + -jar + sample-blade-app.jar + + + + + + + stop-all + post-integration-test + + stop-all + + + + + + + + maven-assembly-plugin + 3.1.0 + + ${project.build.finalName} + false + + + com.baeldung.blade.sample.App + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + maven-compiler-plugin + + 1.8 + 1.8 + UTF-8 + + + + + diff --git a/blade/src/main/java/com/baeldung/blade/sample/App.java b/blade/src/main/java/com/baeldung/blade/sample/App.java new file mode 100644 index 0000000000..f3f3d4aebd --- /dev/null +++ b/blade/src/main/java/com/baeldung/blade/sample/App.java @@ -0,0 +1,38 @@ +package com.baeldung.blade.sample; + +import com.baeldung.blade.sample.interceptors.BaeldungMiddleware; +import com.blade.Blade; +import com.blade.event.EventType; +import com.blade.mvc.WebContext; +import com.blade.mvc.http.Session; + +public class App { + + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(App.class); + + public static void main(String[] args) { + + Blade.of() + .get("/", ctx -> ctx.render("index.html")) + .get("/basic-route-example", ctx -> ctx.text("GET called")) + .post("/basic-route-example", ctx -> ctx.text("POST called")) + .put("/basic-route-example", ctx -> ctx.text("PUT called")) + .delete("/basic-route-example", ctx -> ctx.text("DELETE called")) + .addStatics("/custom-static") + // .showFileList(true) + .enableCors(true) + .before("/user/*", ctx -> log.info("[NarrowedHook] Before '/user/*', URL called: " + ctx.uri())) + .on(EventType.SERVER_STARTED, e -> { + String version = WebContext.blade() + .env("app.version") + .orElse("N/D"); + log.info("[Event::serverStarted] Loading 'app.version' from configuration, value: " + version); + }) + .on(EventType.SESSION_CREATED, e -> { + Session session = (Session) e.attribute("session"); + session.attribute("mySessionValue", "Baeldung"); + }) + .use(new BaeldungMiddleware()) + .start(App.class, args); + } +} diff --git a/blade/src/main/java/com/baeldung/blade/sample/AttributesExampleController.java b/blade/src/main/java/com/baeldung/blade/sample/AttributesExampleController.java new file mode 100644 index 0000000000..339ba701f7 --- /dev/null +++ b/blade/src/main/java/com/baeldung/blade/sample/AttributesExampleController.java @@ -0,0 +1,37 @@ +package com.baeldung.blade.sample; + +import com.blade.mvc.annotation.GetRoute; +import com.blade.mvc.annotation.Path; +import com.blade.mvc.http.Request; +import com.blade.mvc.http.Response; +import com.blade.mvc.http.Session; + +@Path +public class AttributesExampleController { + + public final static String REQUEST_VALUE = "Some Request value"; + public final static String SESSION_VALUE = "1337"; + public final static String HEADER = "Some Header"; + + @GetRoute("/request-attribute-example") + public void getRequestAttribute(Request request, Response response) { + request.attribute("request-val", REQUEST_VALUE); + String requestVal = request.attribute("request-val"); + response.text(requestVal); + } + + @GetRoute("/session-attribute-example") + public void getSessionAttribute(Request request, Response response) { + Session session = request.session(); + session.attribute("session-val", SESSION_VALUE); + String sessionVal = session.attribute("session-val"); + response.text(sessionVal); + } + + @GetRoute("/header-example") + public void getHeader(Request request, Response response) { + String headerVal = request.header("a-header", HEADER); + response.header("a-header", headerVal); + } + +} diff --git a/blade/src/main/java/com/baeldung/blade/sample/LogExampleController.java b/blade/src/main/java/com/baeldung/blade/sample/LogExampleController.java new file mode 100644 index 0000000000..f0c22c70dd --- /dev/null +++ b/blade/src/main/java/com/baeldung/blade/sample/LogExampleController.java @@ -0,0 +1,22 @@ +package com.baeldung.blade.sample; + +import com.blade.mvc.annotation.Path; +import com.blade.mvc.annotation.Route; +import com.blade.mvc.http.Response; + +@Path +public class LogExampleController { + + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleController.class); + + @Route(value = "/test-logs") + public void testLogs(Response response) { + log.trace("This is a TRACE Message"); + log.debug("This is a DEBUG Message"); + log.info("This is an INFO Message"); + log.warn("This is a WARN Message"); + log.error("This is an ERROR Message"); + response.text("Check in ./logs"); + } + +} diff --git a/blade/src/main/java/com/baeldung/blade/sample/ParameterInjectionExampleController.java b/blade/src/main/java/com/baeldung/blade/sample/ParameterInjectionExampleController.java new file mode 100644 index 0000000000..bc28244022 --- /dev/null +++ b/blade/src/main/java/com/baeldung/blade/sample/ParameterInjectionExampleController.java @@ -0,0 +1,71 @@ +package com.baeldung.blade.sample; + +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; + +import com.baeldung.blade.sample.vo.User; +import com.blade.mvc.annotation.CookieParam; +import com.blade.mvc.annotation.GetRoute; +import com.blade.mvc.annotation.HeaderParam; +import com.blade.mvc.annotation.JSON; +import com.blade.mvc.annotation.MultipartParam; +import com.blade.mvc.annotation.Param; +import com.blade.mvc.annotation.Path; +import com.blade.mvc.annotation.PathParam; +import com.blade.mvc.annotation.PostRoute; +import com.blade.mvc.http.Response; +import com.blade.mvc.multipart.FileItem; +import com.blade.mvc.ui.RestResponse; + +@Path +public class ParameterInjectionExampleController { + + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ParameterInjectionExampleController.class); + + @GetRoute("/params/form") + public void formParam(@Param String name, Response response) { + log.info("name: " + name); + response.text(name); + } + + @GetRoute("/params/path/:uid") + public void restfulParam(@PathParam Integer uid, Response response) { + log.info("uid: " + uid); + response.text(String.valueOf(uid)); + } + + @PostRoute("/params-file") // DO NOT USE A SLASH WITHIN THE ROUTE OR IT WILL BREAK (?) + @JSON + public RestResponse fileParam(@MultipartParam FileItem fileItem) throws Exception { + try { + byte[] fileContent = fileItem.getData(); + + log.debug("Saving the uploaded file"); + java.nio.file.Path tempFile = Files.createTempFile("baeldung_tempfiles", ".tmp"); + Files.write(tempFile, fileContent, StandardOpenOption.WRITE); + + return RestResponse.ok(); + } catch (Exception e) { + log.error(e.getMessage(), e); + return RestResponse.fail(e.getMessage()); + } + } + + @GetRoute("/params/header") + public void headerParam(@HeaderParam String customheader, Response response) { + log.info("Custom header: " + customheader); + response.text(customheader); + } + + @GetRoute("/params/cookie") + public void cookieParam(@CookieParam(defaultValue = "default value") String myCookie, Response response) { + log.info("myCookie: " + myCookie); + response.text(myCookie); + } + + @PostRoute("/params/vo") + public void voParam(@Param User user, Response response) { + log.info("user as voParam: " + user.toString()); + response.html(user.toString() + "

Back"); + } +} \ No newline at end of file diff --git a/blade/src/main/java/com/baeldung/blade/sample/RouteExampleController.java b/blade/src/main/java/com/baeldung/blade/sample/RouteExampleController.java new file mode 100644 index 0000000000..7ba2a270a9 --- /dev/null +++ b/blade/src/main/java/com/baeldung/blade/sample/RouteExampleController.java @@ -0,0 +1,78 @@ +package com.baeldung.blade.sample; + +import com.baeldung.blade.sample.configuration.BaeldungException; +import com.blade.mvc.WebContext; +import com.blade.mvc.annotation.DeleteRoute; +import com.blade.mvc.annotation.GetRoute; +import com.blade.mvc.annotation.Path; +import com.blade.mvc.annotation.PostRoute; +import com.blade.mvc.annotation.PutRoute; +import com.blade.mvc.annotation.Route; +import com.blade.mvc.http.HttpMethod; +import com.blade.mvc.http.Request; +import com.blade.mvc.http.Response; + +@Path +public class RouteExampleController { + + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RouteExampleController.class); + + @GetRoute("/route-example") + public String get() { + return "get.html"; + } + + @PostRoute("/route-example") + public String post() { + return "post.html"; + } + + @PutRoute("/route-example") + public String put() { + return "put.html"; + } + + @DeleteRoute("/route-example") + public String delete() { + return "delete.html"; + } + + @Route(value = "/another-route-example", method = HttpMethod.GET) + public String anotherGet() { + return "get.html"; + } + + @Route(value = "/allmatch-route-example") + public String allmatch() { + return "allmatch.html"; + } + + @Route(value = "/triggerInternalServerError") + public void triggerInternalServerError() { + int x = 1 / 0; + } + + @Route(value = "/triggerBaeldungException") + public void triggerBaeldungException() throws BaeldungException { + throw new BaeldungException("Foobar Exception to threat differently"); + } + + @Route(value = "/user/foo") + public void urlCoveredByNarrowedWebhook(Response response) { + response.text("Check out for the WebHook covering '/user/*' in the logs"); + } + + @GetRoute("/load-configuration-in-a-route") + public void loadConfigurationInARoute(Response response) { + String authors = WebContext.blade() + .env("app.authors", "Unknown authors"); + log.info("[/load-configuration-in-a-route] Loading 'app.authors' from configuration, value: " + authors); + response.render("index.html"); + } + + @GetRoute("/template-output-test") + public void templateOutputTest(Request request, Response response) { + request.attribute("name", "Blade"); + response.render("template-output-test.html"); + } +} diff --git a/blade/src/main/java/com/baeldung/blade/sample/configuration/BaeldungException.java b/blade/src/main/java/com/baeldung/blade/sample/configuration/BaeldungException.java new file mode 100644 index 0000000000..01a030b7e7 --- /dev/null +++ b/blade/src/main/java/com/baeldung/blade/sample/configuration/BaeldungException.java @@ -0,0 +1,9 @@ +package com.baeldung.blade.sample.configuration; + +public class BaeldungException extends RuntimeException { + + public BaeldungException(String message) { + super(message); + } + +} \ No newline at end of file diff --git a/blade/src/main/java/com/baeldung/blade/sample/configuration/GlobalExceptionHandler.java b/blade/src/main/java/com/baeldung/blade/sample/configuration/GlobalExceptionHandler.java new file mode 100644 index 0000000000..ab7b81c0dc --- /dev/null +++ b/blade/src/main/java/com/baeldung/blade/sample/configuration/GlobalExceptionHandler.java @@ -0,0 +1,25 @@ +package com.baeldung.blade.sample.configuration; + +import com.blade.ioc.annotation.Bean; +import com.blade.mvc.WebContext; +import com.blade.mvc.handler.DefaultExceptionHandler; + +@Bean +public class GlobalExceptionHandler extends DefaultExceptionHandler { + + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(GlobalExceptionHandler.class); + + @Override + public void handle(Exception e) { + if (e instanceof BaeldungException) { + Exception baeldungException = (BaeldungException) e; + String msg = baeldungException.getMessage(); + log.error("[GlobalExceptionHandler] Intercepted an exception to threat with additional logic. Error message: " + msg); + WebContext.response() + .render("index.html"); + + } else { + super.handle(e); + } + } +} \ No newline at end of file diff --git a/blade/src/main/java/com/baeldung/blade/sample/configuration/LoadConfig.java b/blade/src/main/java/com/baeldung/blade/sample/configuration/LoadConfig.java new file mode 100644 index 0000000000..0f1aab1b52 --- /dev/null +++ b/blade/src/main/java/com/baeldung/blade/sample/configuration/LoadConfig.java @@ -0,0 +1,23 @@ +package com.baeldung.blade.sample.configuration; + +import com.blade.Blade; +import com.blade.ioc.annotation.Bean; +import com.blade.loader.BladeLoader; +import com.blade.mvc.WebContext; + +@Bean +public class LoadConfig implements BladeLoader { + + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LoadConfig.class); + + @Override + public void load(Blade blade) { + String version = WebContext.blade() + .env("app.version") + .orElse("N/D"); + String authors = WebContext.blade() + .env("app.authors", "Unknown authors"); + + log.info("[LoadConfig] loaded 'app.version' (" + version + ") and 'app.authors' (" + authors + ") in a configuration bean"); + } +} \ No newline at end of file diff --git a/blade/src/main/java/com/baeldung/blade/sample/configuration/ScheduleExample.java b/blade/src/main/java/com/baeldung/blade/sample/configuration/ScheduleExample.java new file mode 100644 index 0000000000..c170975818 --- /dev/null +++ b/blade/src/main/java/com/baeldung/blade/sample/configuration/ScheduleExample.java @@ -0,0 +1,15 @@ +package com.baeldung.blade.sample.configuration; + +import com.blade.ioc.annotation.Bean; +import com.blade.task.annotation.Schedule; + +@Bean +public class ScheduleExample { + + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ScheduleExample.class); + + @Schedule(name = "baeldungTask", cron = "0 */1 * * * ?") + public void runScheduledTask() { + log.info("[ScheduleExample] This is a scheduled Task running once per minute."); + } +} diff --git a/blade/src/main/java/com/baeldung/blade/sample/interceptors/BaeldungHook.java b/blade/src/main/java/com/baeldung/blade/sample/interceptors/BaeldungHook.java new file mode 100644 index 0000000000..4d0d178b0d --- /dev/null +++ b/blade/src/main/java/com/baeldung/blade/sample/interceptors/BaeldungHook.java @@ -0,0 +1,17 @@ +package com.baeldung.blade.sample.interceptors; + +import com.blade.ioc.annotation.Bean; +import com.blade.mvc.RouteContext; +import com.blade.mvc.hook.WebHook; + +@Bean +public class BaeldungHook implements WebHook { + + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BaeldungHook.class); + + @Override + public boolean before(RouteContext ctx) { + log.info("[BaeldungHook] called before Route method"); + return true; + } +} \ No newline at end of file diff --git a/blade/src/main/java/com/baeldung/blade/sample/interceptors/BaeldungMiddleware.java b/blade/src/main/java/com/baeldung/blade/sample/interceptors/BaeldungMiddleware.java new file mode 100644 index 0000000000..3342cd8b01 --- /dev/null +++ b/blade/src/main/java/com/baeldung/blade/sample/interceptors/BaeldungMiddleware.java @@ -0,0 +1,15 @@ +package com.baeldung.blade.sample.interceptors; + +import com.blade.mvc.RouteContext; +import com.blade.mvc.hook.WebHook; + +public class BaeldungMiddleware implements WebHook { + + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(BaeldungMiddleware.class); + + @Override + public boolean before(RouteContext context) { + log.info("[BaeldungMiddleware] called before Route method and other WebHooks"); + return true; + } +} \ No newline at end of file diff --git a/blade/src/main/java/com/baeldung/blade/sample/vo/User.java b/blade/src/main/java/com/baeldung/blade/sample/vo/User.java new file mode 100644 index 0000000000..b493dc3663 --- /dev/null +++ b/blade/src/main/java/com/baeldung/blade/sample/vo/User.java @@ -0,0 +1,16 @@ +package com.baeldung.blade.sample.vo; + +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; + +import lombok.Getter; +import lombok.Setter; + +public class User { + @Getter @Setter private String name; + @Getter @Setter private String site; + + @Override + public String toString() { + return ReflectionToStringBuilder.toString(this); + } +} \ No newline at end of file diff --git a/blade/src/main/resources/application.properties b/blade/src/main/resources/application.properties new file mode 100644 index 0000000000..ebf365406a --- /dev/null +++ b/blade/src/main/resources/application.properties @@ -0,0 +1,5 @@ +mvc.statics.show-list=true +mvc.view.404=my-404.html +mvc.view.500=my-500.html +app.version=0.0.1 +app.authors=Andrea Ligios diff --git a/blade/src/main/resources/custom-static/icon.png b/blade/src/main/resources/custom-static/icon.png new file mode 100644 index 0000000000..59af395afc Binary files /dev/null and b/blade/src/main/resources/custom-static/icon.png differ diff --git a/blade/src/main/resources/favicon.ico b/blade/src/main/resources/favicon.ico new file mode 100644 index 0000000000..ca63a6a890 Binary files /dev/null and b/blade/src/main/resources/favicon.ico differ diff --git a/blade/src/main/resources/static/app.css b/blade/src/main/resources/static/app.css new file mode 100644 index 0000000000..9fff13d9b6 --- /dev/null +++ b/blade/src/main/resources/static/app.css @@ -0,0 +1 @@ +/* App CSS */ \ No newline at end of file diff --git a/blade/src/main/resources/static/app.js b/blade/src/main/resources/static/app.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/blade/src/main/resources/static/file-upload.html b/blade/src/main/resources/static/file-upload.html new file mode 100644 index 0000000000..b805be81b1 --- /dev/null +++ b/blade/src/main/resources/static/file-upload.html @@ -0,0 +1,43 @@ + + + + +Title + + + + + +

File Upload and download test

+
+ + +
+ +
+ Back + + + + \ No newline at end of file diff --git a/blade/src/main/resources/static/user-post.html b/blade/src/main/resources/static/user-post.html new file mode 100644 index 0000000000..ccfc4e8d0b --- /dev/null +++ b/blade/src/main/resources/static/user-post.html @@ -0,0 +1,25 @@ + + + + +Title + + + + + +

User POJO post test

+ + +
+ Person POJO: + + + +
+ +
+ Back + + + \ No newline at end of file diff --git a/blade/src/main/resources/templates/allmatch.html b/blade/src/main/resources/templates/allmatch.html new file mode 100644 index 0000000000..7a4bfa070f --- /dev/null +++ b/blade/src/main/resources/templates/allmatch.html @@ -0,0 +1 @@ +ALLMATCH called \ No newline at end of file diff --git a/blade/src/main/resources/templates/delete.html b/blade/src/main/resources/templates/delete.html new file mode 100644 index 0000000000..1acb4b0b62 --- /dev/null +++ b/blade/src/main/resources/templates/delete.html @@ -0,0 +1 @@ +DELETE called \ No newline at end of file diff --git a/blade/src/main/resources/templates/get.html b/blade/src/main/resources/templates/get.html new file mode 100644 index 0000000000..2c37aa1058 --- /dev/null +++ b/blade/src/main/resources/templates/get.html @@ -0,0 +1 @@ +GET called \ No newline at end of file diff --git a/blade/src/main/resources/templates/index.html b/blade/src/main/resources/templates/index.html new file mode 100644 index 0000000000..6b7c2e77ad --- /dev/null +++ b/blade/src/main/resources/templates/index.html @@ -0,0 +1,30 @@ + + + + +Baeldung Blade App • Written by Andrea Ligios + + + +

Baeldung Blade App - Showcase

+ +

Manual tests

+

The following are tests which are not covered by integration tests, but that can be run manually in order to check the functionality, either in the browser or in the logs, depending on the case.

+ + + + +

Session value created in App.java

+

mySessionValue = ${mySessionValue}

+ + + + + \ No newline at end of file diff --git a/blade/src/main/resources/templates/my-404.html b/blade/src/main/resources/templates/my-404.html new file mode 100644 index 0000000000..0fa694f241 --- /dev/null +++ b/blade/src/main/resources/templates/my-404.html @@ -0,0 +1,10 @@ + + + + + 404 Not found + + + Custom Error 404 Page + + \ No newline at end of file diff --git a/blade/src/main/resources/templates/my-500.html b/blade/src/main/resources/templates/my-500.html new file mode 100644 index 0000000000..cc8438bfd6 --- /dev/null +++ b/blade/src/main/resources/templates/my-500.html @@ -0,0 +1,12 @@ + + + + + 500 Internal Server Error + + +

Custom Error 500 Page

+

The following error occurred: "${message}"

+
 ${stackTrace} 
+ + \ No newline at end of file diff --git a/blade/src/main/resources/templates/post.html b/blade/src/main/resources/templates/post.html new file mode 100644 index 0000000000..b7a8a931cd --- /dev/null +++ b/blade/src/main/resources/templates/post.html @@ -0,0 +1 @@ +POST called \ No newline at end of file diff --git a/blade/src/main/resources/templates/put.html b/blade/src/main/resources/templates/put.html new file mode 100644 index 0000000000..bdbe6d3285 --- /dev/null +++ b/blade/src/main/resources/templates/put.html @@ -0,0 +1 @@ +PUT called \ No newline at end of file diff --git a/blade/src/main/resources/templates/template-output-test.html b/blade/src/main/resources/templates/template-output-test.html new file mode 100644 index 0000000000..233b12fb88 --- /dev/null +++ b/blade/src/main/resources/templates/template-output-test.html @@ -0,0 +1 @@ +

Hello, ${name}!

\ No newline at end of file diff --git a/blade/src/test/java/com/baeldung/blade/sample/AppLiveTest.java b/blade/src/test/java/com/baeldung/blade/sample/AppLiveTest.java new file mode 100644 index 0000000000..1172e6755f --- /dev/null +++ b/blade/src/test/java/com/baeldung/blade/sample/AppLiveTest.java @@ -0,0 +1,56 @@ +package com.baeldung.blade.sample; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.Test; + +public class AppLiveTest { + + @Test + public void givenBasicRoute_whenGet_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpGet("http://localhost:9000/basic-route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("GET called"); + } + } + + @Test + public void givenBasicRoute_whenPost_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpPost("http://localhost:9000/basic-route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("POST called"); + } + } + + @Test + public void givenBasicRoute_whenPut_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpPut("http://localhost:9000/basic-route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("PUT called"); + } + } + + @Test + public void givenBasicRoute_whenDelete_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpDelete("http://localhost:9000/basic-route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("DELETE called"); + } + } +} diff --git a/blade/src/test/java/com/baeldung/blade/sample/AttributesExampleControllerLiveTest.java b/blade/src/test/java/com/baeldung/blade/sample/AttributesExampleControllerLiveTest.java new file mode 100644 index 0000000000..7cf00c2d4b --- /dev/null +++ b/blade/src/test/java/com/baeldung/blade/sample/AttributesExampleControllerLiveTest.java @@ -0,0 +1,55 @@ +package com.baeldung.blade.sample; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.Test; + +public class AttributesExampleControllerLiveTest { + + @Test + public void givenRequestAttribute_whenSet_thenRetrieveWithGet() throws Exception { + final HttpUriRequest request = new HttpGet("http://localhost:9000/request-attribute-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo(AttributesExampleController.REQUEST_VALUE); + } + } + + @Test + public void givenSessionAttribute_whenSet_thenRetrieveWithGet() throws Exception { + final HttpUriRequest request = new HttpGet("http://localhost:9000/session-attribute-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo(AttributesExampleController.SESSION_VALUE); + } + } + + @Test + public void givenHeader_whenSet_thenRetrieveWithGet() throws Exception { + final HttpUriRequest request = new HttpGet("http://localhost:9000/header-example"); + request.addHeader("a-header","foobar"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(httpResponse.getHeaders("a-header")[0].getValue()).isEqualTo("foobar"); + } + } + + @Test + public void givenNoHeader_whenSet_thenRetrieveDefaultValueWithGet() throws Exception { + final HttpUriRequest request = new HttpGet("http://localhost:9000/header-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(httpResponse.getHeaders("a-header")[0].getValue()).isEqualTo(AttributesExampleController.HEADER); + } + } + +} diff --git a/blade/src/test/java/com/baeldung/blade/sample/ParameterInjectionExampleControllerLiveTest.java b/blade/src/test/java/com/baeldung/blade/sample/ParameterInjectionExampleControllerLiveTest.java new file mode 100644 index 0000000000..fbd5280116 --- /dev/null +++ b/blade/src/test/java/com/baeldung/blade/sample/ParameterInjectionExampleControllerLiveTest.java @@ -0,0 +1,82 @@ +package com.baeldung.blade.sample; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.Test; + +public class ParameterInjectionExampleControllerLiveTest { + + @Test + public void givenFormParam_whenSet_thenRetrieveWithGet() throws Exception { + URIBuilder builder = new URIBuilder("http://localhost:9000/params/form"); + builder.setParameter("name", "Andrea Ligios"); + + final HttpUriRequest request = new HttpGet(builder.build()); + + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("Andrea Ligios"); + } + } + + @Test + public void givenPathParam_whenSet_thenRetrieveWithGet() throws Exception { + final HttpUriRequest request = new HttpGet("http://localhost:9000/params/path/1337"); + + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("1337"); + } + } + + // @Test + // public void givenFileParam_whenSet_thenRetrieveWithGet() throws Exception { + // + // byte[] data = "this is some temp file content".getBytes("UTF-8"); + // java.nio.file.Path tempFile = Files.createTempFile("baeldung_test_tempfiles", ".tmp"); + // Files.write(tempFile, data, StandardOpenOption.WRITE); + // + // //HttpEntity entity = MultipartEntityBuilder.create().addPart("file", new FileBody(tempFile.toFile())).build(); + // HttpEntity entity = MultipartEntityBuilder.create().addTextBody("field1", "value1") + // .addBinaryBody("fileItem", tempFile.toFile(), ContentType.create("application/octet-stream"), "file1.txt").build(); + // + // final HttpPost post = new HttpPost("http://localhost:9000/params-file"); + // post.setEntity(entity); + // + // try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create().build().execute(post);) { + // assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("file1.txt"); + // } + // } + + @Test + public void givenHeader_whenSet_thenRetrieveWithGet() throws Exception { + final HttpUriRequest request = new HttpGet("http://localhost:9000/params/header"); + request.addHeader("customheader", "foobar"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("foobar"); + } + } + + @Test + public void givenNoCookie_whenCalled_thenReadDefaultValue() throws Exception { + + final HttpUriRequest request = new HttpGet("http://localhost:9000/params/cookie"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("default value"); + } + + } + +} diff --git a/blade/src/test/java/com/baeldung/blade/sample/RouteExampleControllerLiveTest.java b/blade/src/test/java/com/baeldung/blade/sample/RouteExampleControllerLiveTest.java new file mode 100644 index 0000000000..df8e70c461 --- /dev/null +++ b/blade/src/test/java/com/baeldung/blade/sample/RouteExampleControllerLiveTest.java @@ -0,0 +1,117 @@ +package com.baeldung.blade.sample; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.Test; + +public class RouteExampleControllerLiveTest { + + @Test + public void givenRoute_whenGet_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpGet("http://localhost:9000/route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("GET called"); + } + } + + @Test + public void givenRoute_whenPost_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpPost("http://localhost:9000/route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("POST called"); + } + } + + @Test + public void givenRoute_whenPut_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpPut("http://localhost:9000/route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("PUT called"); + } + } + + @Test + public void givenRoute_whenDelete_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpDelete("http://localhost:9000/route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("DELETE called"); + } + } + + @Test + public void givenAnotherRoute_whenGet_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpGet("http://localhost:9000/another-route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("GET called"); + } + } + + @Test + public void givenAllMatchRoute_whenGet_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpGet("http://localhost:9000/allmatch-route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("ALLMATCH called"); + } + } + + @Test + public void givenAllMatchRoute_whenPost_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpPost("http://localhost:9000/allmatch-route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("ALLMATCH called"); + } + } + + @Test + public void givenAllMatchRoute_whenPut_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpPut("http://localhost:9000/allmatch-route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("ALLMATCH called"); + } + } + + @Test + public void givenAllMatchRoute_whenDelete_thenCorrectOutput() throws Exception { + final HttpUriRequest request = new HttpDelete("http://localhost:9000/allmatch-route-example"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("ALLMATCH called"); + } + } + + @Test + public void givenRequestAttribute_whenRenderedWithTemplate_thenCorrectlyEvaluateIt() throws Exception { + final HttpUriRequest request = new HttpGet("http://localhost:9000/template-output-test"); + try (final CloseableHttpResponse httpResponse = HttpClientBuilder.create() + .build() + .execute(request);) { + assertThat(EntityUtils.toString(httpResponse.getEntity())).isEqualTo("

Hello, Blade!

"); + } + } + +} diff --git a/pom.xml b/pom.xml index 1c0738cafb..d084d0f7af 100644 --- a/pom.xml +++ b/pom.xml @@ -367,6 +367,8 @@ axon azure + blade + bootique cas/cas-secured-app