From afdca95b43b29a7aea688cf2f0f74ab6870679c0 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sun, 1 Dec 2019 18:29:04 +0200 Subject: [PATCH 01/11] Initial commit --- ninja/pom.xml | 197 ++++++++++++++++++ ninja/src/main/java/assets/css/custom.css | 3 + ninja/src/main/java/conf/Filters.java | 30 +++ ninja/src/main/java/conf/Module.java | 37 ++++ ninja/src/main/java/conf/Routes.java | 47 +++++ ninja/src/main/java/conf/application.conf | 26 +++ ninja/src/main/java/conf/messages.properties | 21 ++ .../controllers/ApplicationController.java | 36 ++++ ninja/src/main/java/ehcache.xml | 15 ++ ninja/src/main/java/logback.xml | 33 +++ ninja/src/main/java/services/UserService.java | 7 + .../main/java/services/UserServiceImpl.java | 10 + .../ApplicationController/index.ftl.html | 9 + .../java/views/layout/defaultLayout.ftl.html | 58 ++++++ .../main/java/views/layout/footer.ftl.html | 5 + .../main/java/views/layout/header.ftl.html | 18 ++ .../java/views/system/403forbidden.ftl.html | 18 ++ .../java/views/system/404notFound.ftl.html | 18 ++ ninja/src/main/webapp/WEB-INF/web.xml | 41 ++++ .../ApiControllerDocTesterTest.java | 78 +++++++ 20 files changed, 707 insertions(+) create mode 100644 ninja/pom.xml create mode 100644 ninja/src/main/java/assets/css/custom.css create mode 100644 ninja/src/main/java/conf/Filters.java create mode 100644 ninja/src/main/java/conf/Module.java create mode 100644 ninja/src/main/java/conf/Routes.java create mode 100644 ninja/src/main/java/conf/application.conf create mode 100644 ninja/src/main/java/conf/messages.properties create mode 100644 ninja/src/main/java/controllers/ApplicationController.java create mode 100644 ninja/src/main/java/ehcache.xml create mode 100644 ninja/src/main/java/logback.xml create mode 100644 ninja/src/main/java/services/UserService.java create mode 100644 ninja/src/main/java/services/UserServiceImpl.java create mode 100644 ninja/src/main/java/views/ApplicationController/index.ftl.html create mode 100644 ninja/src/main/java/views/layout/defaultLayout.ftl.html create mode 100644 ninja/src/main/java/views/layout/footer.ftl.html create mode 100644 ninja/src/main/java/views/layout/header.ftl.html create mode 100644 ninja/src/main/java/views/system/403forbidden.ftl.html create mode 100644 ninja/src/main/java/views/system/404notFound.ftl.html create mode 100644 ninja/src/main/webapp/WEB-INF/web.xml create mode 100644 ninja/src/test/java/controllers/ApiControllerDocTesterTest.java diff --git a/ninja/pom.xml b/ninja/pom.xml new file mode 100644 index 0000000000..962dabf1bd --- /dev/null +++ b/ninja/pom.xml @@ -0,0 +1,197 @@ + + + 4.0.0 + + ninja + + + + + jar + com.baeldung + 1.0.0 + + http://www.ninjaframework.org + + + 6.5.0 + 9.4.18.v20190429 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.2 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-enforcer-plugin + 1.3.1 + + + enforce-banned-dependencies + + enforce + + + + + + commons-logging + + + + true + + + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.version} + + + / + + stop + 8889 + 1 + automatic + + + target/classes + + **/* + + + **/*.ftl.html + assets/** + + + + + + ninja.mode + dev + + + + + + + + + org.ninjaframework + ninja-maven-plugin + ${ninja.version} + + + org.apache.maven.plugins + maven-deploy-plugin + 2.8.2 + + true + + + + org.apache.maven.plugins + maven-shade-plugin + 2.2 + + true + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + package + + shade + + + + + + ninja.standalone.NinjaJetty + + + + + + + + + + src/main/java + + **/* + + + **/*.java + + + + src/main/resources + + **/* + + + + + + + org.webjars + bootstrap + 3.3.4 + + + org.webjars + jquery + 2.1.3 + + + com.h2database + h2 + 1.4.186 + + + + + + + org.ninjaframework + ninja-standalone + ${ninja.version} + + + org.ninjaframework + ninja-test-utilities + ${ninja.version} + test + + + \ No newline at end of file diff --git a/ninja/src/main/java/assets/css/custom.css b/ninja/src/main/java/assets/css/custom.css new file mode 100644 index 0000000000..41d249d3cb --- /dev/null +++ b/ninja/src/main/java/assets/css/custom.css @@ -0,0 +1,3 @@ +/* Add additional stylesheets below +-------------------------------------------------- */ + diff --git a/ninja/src/main/java/conf/Filters.java b/ninja/src/main/java/conf/Filters.java new file mode 100644 index 0000000000..cfd04e8ec2 --- /dev/null +++ b/ninja/src/main/java/conf/Filters.java @@ -0,0 +1,30 @@ +/** + * Copyright (C) 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package conf; + +import java.util.List; +import ninja.Filter; + +public class Filters implements ninja.application.ApplicationFilters { + + @Override + public void addFilters(List> filters) { + // Add your application - wide filters here + // filters.add(MyFilter.class); + } +} diff --git a/ninja/src/main/java/conf/Module.java b/ninja/src/main/java/conf/Module.java new file mode 100644 index 0000000000..e5ec39d2de --- /dev/null +++ b/ninja/src/main/java/conf/Module.java @@ -0,0 +1,37 @@ +/** + * Copyright (C) 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package conf; + +import com.google.inject.AbstractModule; +import com.google.inject.Singleton; + +import services.UserService; +import services.UserServiceImpl; + +@Singleton +public class Module extends AbstractModule { + + + protected void configure() { + + // bind your injections here! + bind(UserService.class).to(UserServiceImpl.class); + + } + +} diff --git a/ninja/src/main/java/conf/Routes.java b/ninja/src/main/java/conf/Routes.java new file mode 100644 index 0000000000..7db2d7a107 --- /dev/null +++ b/ninja/src/main/java/conf/Routes.java @@ -0,0 +1,47 @@ +/** + * Copyright (C) 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package conf; + + +import ninja.AssetsController; +import ninja.Router; +import ninja.application.ApplicationRoutes; +import controllers.ApplicationController; + +public class Routes implements ApplicationRoutes { + + @Override + public void init(Router router) { + + router.GET().route("/").with(ApplicationController::index); + router.GET().route("/hello_world.json").with(ApplicationController::helloWorldJson); + + + /////////////////////////////////////////////////////////////////////// + // Assets (pictures / javascript) + /////////////////////////////////////////////////////////////////////// + router.GET().route("/assets/webjars/{fileName: .*}").with(AssetsController::serveWebJars); + router.GET().route("/assets/{fileName: .*}").with(AssetsController::serveStatic); + + /////////////////////////////////////////////////////////////////////// + // Index / Catchall shows index page + /////////////////////////////////////////////////////////////////////// + router.GET().route("/.*").with(ApplicationController::index); + } + +} diff --git a/ninja/src/main/java/conf/application.conf b/ninja/src/main/java/conf/application.conf new file mode 100644 index 0000000000..3b2e96f702 --- /dev/null +++ b/ninja/src/main/java/conf/application.conf @@ -0,0 +1,26 @@ +# The main properties file to configure your application +# +# Properties can be prefixed by "%". +# "%"" matches a mode you can set as system property. For instance when you +# are using maven you can do: mvn -Dmode=test jetty:run +# +# Using that the following property: +# %test.myproperty=test property +# would overwrite the property +# myproperty=my property +# +# You can define as many environments as you want. Simply set them in the mode. +application.name=baeldung ninja application + +application.cookie.prefix=NINJA + +#ISO Language Code, optionally followed by a valid ISO Country Code. +application.languages=en + +application.session.expire_time_in_seconds=3600 +application.session.send_only_if_changed=true +application.session.transferred_over_https_only=false + +# enable ssl with self-signed cert in dev & test modes +ninja.ssl.port=8443 +application.secret = fxSjSL9Q017BSL7gBnkyo2Prln7uXaXIT35gotXRIED8c46OSa8s4QdoIQdTsEtj diff --git a/ninja/src/main/java/conf/messages.properties b/ninja/src/main/java/conf/messages.properties new file mode 100644 index 0000000000..f163d33260 --- /dev/null +++ b/ninja/src/main/java/conf/messages.properties @@ -0,0 +1,21 @@ +# +# Copyright (C) 2012 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# This file is utf-8 +header.title=Hello World! +header.home=BAM! +hello.world=Hello World! +hello.world.json=There is also Json rendering you can check out! \ No newline at end of file diff --git a/ninja/src/main/java/controllers/ApplicationController.java b/ninja/src/main/java/controllers/ApplicationController.java new file mode 100644 index 0000000000..bd62e5fb84 --- /dev/null +++ b/ninja/src/main/java/controllers/ApplicationController.java @@ -0,0 +1,36 @@ +package controllers; + +import ninja.Result; +import ninja.Results; +import services.UserService; +import javax.inject.Inject; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import com.google.inject.Singleton; + +@Singleton +public class ApplicationController { + + private static Log logger = LogFactory.getLog(ApplicationController.class); + + @Inject + UserService userService; + + public Result index() { + return Results.html(); + } + + public Result helloWorldJson() { + SimplePojo simplePojo = new SimplePojo(); + simplePojo.content = "Hello World! Hello Json!"; + logger.info(userService.getUserName()); + return Results.json().render(simplePojo); + } + + public static class SimplePojo { + public String content; + } + +} diff --git a/ninja/src/main/java/ehcache.xml b/ninja/src/main/java/ehcache.xml new file mode 100644 index 0000000000..b401b61a36 --- /dev/null +++ b/ninja/src/main/java/ehcache.xml @@ -0,0 +1,15 @@ + + + + + diff --git a/ninja/src/main/java/logback.xml b/ninja/src/main/java/logback.xml new file mode 100644 index 0000000000..ebdcf56f76 --- /dev/null +++ b/ninja/src/main/java/logback.xml @@ -0,0 +1,33 @@ + + + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + diff --git a/ninja/src/main/java/services/UserService.java b/ninja/src/main/java/services/UserService.java new file mode 100644 index 0000000000..100761a2ab --- /dev/null +++ b/ninja/src/main/java/services/UserService.java @@ -0,0 +1,7 @@ +package services; + +public interface UserService { + + String getUserName(); + +} diff --git a/ninja/src/main/java/services/UserServiceImpl.java b/ninja/src/main/java/services/UserServiceImpl.java new file mode 100644 index 0000000000..a0dec61084 --- /dev/null +++ b/ninja/src/main/java/services/UserServiceImpl.java @@ -0,0 +1,10 @@ +package services; + +public class UserServiceImpl implements UserService { + + @Override + public String getUserName() { + return "Eugen Parashic"; + } + +} diff --git a/ninja/src/main/java/views/ApplicationController/index.ftl.html b/ninja/src/main/java/views/ApplicationController/index.ftl.html new file mode 100644 index 0000000000..ab2edaf69b --- /dev/null +++ b/ninja/src/main/java/views/ApplicationController/index.ftl.html @@ -0,0 +1,9 @@ +<#import "../layout/defaultLayout.ftl.html" as layout> +<@layout.myLayout "Home page"> + + +

${i18n("hello.world")}

+

${i18n("hello.world.json")}

+Hello World Json + + \ No newline at end of file diff --git a/ninja/src/main/java/views/layout/defaultLayout.ftl.html b/ninja/src/main/java/views/layout/defaultLayout.ftl.html new file mode 100644 index 0000000000..a61edd19e6 --- /dev/null +++ b/ninja/src/main/java/views/layout/defaultLayout.ftl.html @@ -0,0 +1,58 @@ +<#macro myLayout title="Layout example"> + + + + + ${title} + + + + + + + + + + + + + + + + + + +
+ + <#include "header.ftl.html"/> + + <#if (flash.error)??> +
+ ${flash.error} +
+ + + <#if (flash.success)??> +
+ ${flash.success} +
+ + + <#nested/> + + <#include "footer.ftl.html"/> + +
+ + + + + \ No newline at end of file diff --git a/ninja/src/main/java/views/layout/footer.ftl.html b/ninja/src/main/java/views/layout/footer.ftl.html new file mode 100644 index 0000000000..0a2cb2721e --- /dev/null +++ b/ninja/src/main/java/views/layout/footer.ftl.html @@ -0,0 +1,5 @@ +
+ + diff --git a/ninja/src/main/java/views/layout/header.ftl.html b/ninja/src/main/java/views/layout/header.ftl.html new file mode 100644 index 0000000000..8794ee5c68 --- /dev/null +++ b/ninja/src/main/java/views/layout/header.ftl.html @@ -0,0 +1,18 @@ + \ No newline at end of file diff --git a/ninja/src/main/java/views/system/403forbidden.ftl.html b/ninja/src/main/java/views/system/403forbidden.ftl.html new file mode 100644 index 0000000000..0f4efd160a --- /dev/null +++ b/ninja/src/main/java/views/system/403forbidden.ftl.html @@ -0,0 +1,18 @@ +<#import "../layout/defaultLayout.ftl.html" as layout> +<@layout.myLayout "Error. Forbidden."> +
+
+
+
+

+ Oops!

+

+ 403 Forbidden

+
+ Sorry, an error has occured. Requested page is forbidden! +
+
+
+
+
+ \ No newline at end of file diff --git a/ninja/src/main/java/views/system/404notFound.ftl.html b/ninja/src/main/java/views/system/404notFound.ftl.html new file mode 100644 index 0000000000..c10e7c3949 --- /dev/null +++ b/ninja/src/main/java/views/system/404notFound.ftl.html @@ -0,0 +1,18 @@ +<#import "../layout/defaultLayout.ftl.html" as layout> +<@layout.myLayout "Error. Not found."> +
+
+
+
+

+ Oops!

+

+ 404 Not Found

+
+ Sorry, an error has occured. Requested page not found! +
+
+
+
+
+ \ No newline at end of file diff --git a/ninja/src/main/webapp/WEB-INF/web.xml b/ninja/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..fe4b2c95bf --- /dev/null +++ b/ninja/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,41 @@ + + + + + + ninja + + + ninja.servlet.NinjaServletListener + + + + guiceFilter + com.google.inject.servlet.GuiceFilter + + + guiceFilter + /* + + + diff --git a/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java b/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java new file mode 100644 index 0000000000..af4f5ba85f --- /dev/null +++ b/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java @@ -0,0 +1,78 @@ +/** + * Copyright (C) 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Copyright (C) 2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package controllers; + + +import org.junit.Test; + +import ninja.NinjaDocTester; +import org.doctester.testbrowser.Request; +import org.doctester.testbrowser.Response; +import org.hamcrest.CoreMatchers; +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertThat; + +public class ApiControllerDocTesterTest extends NinjaDocTester { + + String URL_INDEX = "/"; + String URL_HELLO_WORLD_JSON = "/hello_world.json"; + + @Test + public void testGetIndex() { + + Response response = makeRequest( + Request.GET().url( + testServerUrl().path(URL_INDEX))); + + assertThat(response.payload, containsString("Hello World!")); + assertThat(response.payload, containsString("BAM!")); + + + } + + @Test + public void testGetHelloWorldJson() { + + Response response = makeRequest( + Request.GET().url( + testServerUrl().path(URL_HELLO_WORLD_JSON))); + + ApplicationController.SimplePojo simplePojo + = response.payloadJsonAs(ApplicationController.SimplePojo.class); + + assertThat(simplePojo.content, CoreMatchers.equalTo("Hello World! Hello Json!")); + + + } + +} From 29657eb0817e0450fde1a0e74a58489e5acd438f Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sun, 8 Dec 2019 14:40:42 +0200 Subject: [PATCH 02/11] Ninja framework changes --- ninja/src/main/java/META-INF/persistence.xml | 33 ++++++++ ninja/src/main/java/conf/Filters.java | 17 ---- ninja/src/main/java/conf/Module.java | 19 ----- ninja/src/main/java/conf/Routes.java | 32 ++----- ninja/src/main/java/conf/application.conf | 12 ++- ninja/src/main/java/conf/messages.properties | 23 +---- .../src/main/java/conf/messages_fr.properties | 2 + .../controllers/ApplicationController.java | 65 ++++++++++++-- ninja/src/main/java/models/User.java | 22 +++++ ninja/src/main/java/services/UserService.java | 4 +- .../main/java/services/UserServiceImpl.java | 11 ++- .../ApplicationController/index.ftl.html | 6 +- .../main/java/views/layout/header.ftl.html | 2 +- .../ApiControllerDocTesterTest.java | 84 +++++-------------- .../controllers/ApiControllerMockTest.java | 41 +++++++++ 15 files changed, 209 insertions(+), 164 deletions(-) create mode 100644 ninja/src/main/java/META-INF/persistence.xml create mode 100644 ninja/src/main/java/conf/messages_fr.properties create mode 100644 ninja/src/main/java/models/User.java create mode 100644 ninja/src/test/java/controllers/ApiControllerMockTest.java diff --git a/ninja/src/main/java/META-INF/persistence.xml b/ninja/src/main/java/META-INF/persistence.xml new file mode 100644 index 0000000000..bfdd6ad8e8 --- /dev/null +++ b/ninja/src/main/java/META-INF/persistence.xml @@ -0,0 +1,33 @@ + + + + + + + org.hibernate.jpa.HibernatePersistenceProvider + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ninja/src/main/java/conf/Filters.java b/ninja/src/main/java/conf/Filters.java index cfd04e8ec2..b201780fa1 100644 --- a/ninja/src/main/java/conf/Filters.java +++ b/ninja/src/main/java/conf/Filters.java @@ -1,20 +1,3 @@ -/** - * Copyright (C) 2012-2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - package conf; import java.util.List; diff --git a/ninja/src/main/java/conf/Module.java b/ninja/src/main/java/conf/Module.java index e5ec39d2de..f7c03a4b26 100644 --- a/ninja/src/main/java/conf/Module.java +++ b/ninja/src/main/java/conf/Module.java @@ -1,20 +1,3 @@ -/** - * Copyright (C) 2012-2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - package conf; import com.google.inject.AbstractModule; @@ -26,10 +9,8 @@ import services.UserServiceImpl; @Singleton public class Module extends AbstractModule { - protected void configure() { - // bind your injections here! bind(UserService.class).to(UserServiceImpl.class); } diff --git a/ninja/src/main/java/conf/Routes.java b/ninja/src/main/java/conf/Routes.java index 7db2d7a107..4f23fd37fe 100644 --- a/ninja/src/main/java/conf/Routes.java +++ b/ninja/src/main/java/conf/Routes.java @@ -1,23 +1,5 @@ -/** - * Copyright (C) 2012-2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - - package conf; - import ninja.AssetsController; import ninja.Router; import ninja.application.ApplicationRoutes; @@ -29,19 +11,17 @@ public class Routes implements ApplicationRoutes { public void init(Router router) { router.GET().route("/").with(ApplicationController::index); - router.GET().route("/hello_world.json").with(ApplicationController::helloWorldJson); - + router.GET().route("/hello").with(ApplicationController::helloWorld); + router.GET().route("/userJson").with(ApplicationController::userJson); + router.GET().route("/users").with(ApplicationController::fetchUsers); - /////////////////////////////////////////////////////////////////////// - // Assets (pictures / javascript) - /////////////////////////////////////////////////////////////////////// + //Assets router.GET().route("/assets/webjars/{fileName: .*}").with(AssetsController::serveWebJars); router.GET().route("/assets/{fileName: .*}").with(AssetsController::serveStatic); - /////////////////////////////////////////////////////////////////////// - // Index / Catchall shows index page - /////////////////////////////////////////////////////////////////////// + //Index router.GET().route("/.*").with(ApplicationController::index); + } } diff --git a/ninja/src/main/java/conf/application.conf b/ninja/src/main/java/conf/application.conf index 3b2e96f702..3bfbc70e1a 100644 --- a/ninja/src/main/java/conf/application.conf +++ b/ninja/src/main/java/conf/application.conf @@ -15,12 +15,20 @@ application.name=baeldung ninja application application.cookie.prefix=NINJA #ISO Language Code, optionally followed by a valid ISO Country Code. -application.languages=en +application.languages=fr,en application.session.expire_time_in_seconds=3600 application.session.send_only_if_changed=true application.session.transferred_over_https_only=false +ninja.port=8000 + # enable ssl with self-signed cert in dev & test modes -ninja.ssl.port=8443 +ninja.ssl.port=8001 application.secret = fxSjSL9Q017BSL7gBnkyo2Prln7uXaXIT35gotXRIED8c46OSa8s4QdoIQdTsEtj + +# h2 jpa configuration +ninja.jpa.persistence_unit_name=dev_unit +db.connection.url=jdbc:h2:./devDb +db.connection.username=sa +db.connection.password= diff --git a/ninja/src/main/java/conf/messages.properties b/ninja/src/main/java/conf/messages.properties index f163d33260..3bddfcd8c7 100644 --- a/ninja/src/main/java/conf/messages.properties +++ b/ninja/src/main/java/conf/messages.properties @@ -1,21 +1,2 @@ -# -# Copyright (C) 2012 the original author or authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -# This file is utf-8 -header.title=Hello World! -header.home=BAM! -hello.world=Hello World! -hello.world.json=There is also Json rendering you can check out! \ No newline at end of file +header.home=Home! +helloMsg=Hello, welcome to Ninja Framework! \ No newline at end of file diff --git a/ninja/src/main/java/conf/messages_fr.properties b/ninja/src/main/java/conf/messages_fr.properties new file mode 100644 index 0000000000..89264e0cb9 --- /dev/null +++ b/ninja/src/main/java/conf/messages_fr.properties @@ -0,0 +1,2 @@ +header.home=Accueil! +helloMsg=Bonjour, bienvenue dans Ninja Framework! \ No newline at end of file diff --git a/ninja/src/main/java/controllers/ApplicationController.java b/ninja/src/main/java/controllers/ApplicationController.java index bd62e5fb84..65c95e9204 100644 --- a/ninja/src/main/java/controllers/ApplicationController.java +++ b/ninja/src/main/java/controllers/ApplicationController.java @@ -1,36 +1,83 @@ package controllers; +import ninja.Context; import ninja.Result; import ninja.Results; +import ninja.i18n.Lang; +import ninja.i18n.Messages; +import ninja.jpa.UnitOfWork; import services.UserService; + +import java.util.List; +import java.util.Optional; + import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.Query; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import com.google.inject.Provider; import com.google.inject.Singleton; +import models.User; +import com.google.inject.persist.Transactional; + @Singleton public class ApplicationController { + @Inject + Lang lang; + + @Inject + Messages msg; + private static Log logger = LogFactory.getLog(ApplicationController.class); + @Inject + Provider entitiyManagerProvider; + @Inject UserService userService; public Result index() { - return Results.html(); + Result result = Results.html(); + lang.setLanguage("fr", result); + return result; + + } + + public Result userJson() { + User user = userService.getUser(); + logger.info(user); + return Results.json().render(user); + } + + public Result helloWorld(Context context) { + Optional language = Optional.of("en"); + String helloMsg = msg.get("helloMsg", language).get(); + return Results.text().render(helloMsg); } - public Result helloWorldJson() { - SimplePojo simplePojo = new SimplePojo(); - simplePojo.content = "Hello World! Hello Json!"; - logger.info(userService.getUserName()); - return Results.json().render(simplePojo); + @UnitOfWork + public Result fetchUsers() { + insertUser(); + EntityManager entityManager = entitiyManagerProvider.get(); + Query q = entityManager.createQuery("SELECT x FROM User x"); + List users = (List) q.getResultList(); + System.out.println(users); + return Results.text().render("Done"); } - public static class SimplePojo { - public String content; + @Transactional + public Result insertUser() { + EntityManager entityManager = entitiyManagerProvider.get(); + User user = userService.getUser(); + entityManager.persist(user); + entityManager.flush(); + + return Results.text().render("Inserted"); } - + } diff --git a/ninja/src/main/java/models/User.java b/ninja/src/main/java/models/User.java new file mode 100644 index 0000000000..d1b18cfa83 --- /dev/null +++ b/ninja/src/main/java/models/User.java @@ -0,0 +1,22 @@ +package models; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class User { + + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + Long id; + + public String firstName; + public String email; + + public String toString() { + return firstName + " : " + email; + } + +} diff --git a/ninja/src/main/java/services/UserService.java b/ninja/src/main/java/services/UserService.java index 100761a2ab..a549b48e48 100644 --- a/ninja/src/main/java/services/UserService.java +++ b/ninja/src/main/java/services/UserService.java @@ -1,7 +1,9 @@ package services; +import models.User; + public interface UserService { - String getUserName(); + User getUser(); } diff --git a/ninja/src/main/java/services/UserServiceImpl.java b/ninja/src/main/java/services/UserServiceImpl.java index a0dec61084..3ffa53400f 100644 --- a/ninja/src/main/java/services/UserServiceImpl.java +++ b/ninja/src/main/java/services/UserServiceImpl.java @@ -1,10 +1,15 @@ package services; +import models.User; + public class UserServiceImpl implements UserService { - + @Override - public String getUserName() { - return "Eugen Parashic"; + public User getUser() { + User user = new User(); + user.firstName = "Norman"; + user.email = "norman@email.com"; + return user; } } diff --git a/ninja/src/main/java/views/ApplicationController/index.ftl.html b/ninja/src/main/java/views/ApplicationController/index.ftl.html index ab2edaf69b..10f6612d54 100644 --- a/ninja/src/main/java/views/ApplicationController/index.ftl.html +++ b/ninja/src/main/java/views/ApplicationController/index.ftl.html @@ -2,8 +2,8 @@ <@layout.myLayout "Home page"> -

${i18n("hello.world")}

-

${i18n("hello.world.json")}

-Hello World Json +

${i18n("helloMsg")}

+ +User Json \ No newline at end of file diff --git a/ninja/src/main/java/views/layout/header.ftl.html b/ninja/src/main/java/views/layout/header.ftl.html index 8794ee5c68..ebd55a9f4c 100644 --- a/ninja/src/main/java/views/layout/header.ftl.html +++ b/ninja/src/main/java/views/layout/header.ftl.html @@ -6,11 +6,11 @@ - ${i18n("header.title")} diff --git a/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java b/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java index af4f5ba85f..e383859dfb 100644 --- a/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java +++ b/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java @@ -1,78 +1,38 @@ -/** - * Copyright (C) 2012-2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Copyright (C) 2013 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package controllers; - -import org.junit.Test; - -import ninja.NinjaDocTester; -import org.doctester.testbrowser.Request; -import org.doctester.testbrowser.Response; -import org.hamcrest.CoreMatchers; import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertThat; +import javax.inject.Inject; + +import org.doctester.testbrowser.Request; +import org.doctester.testbrowser.Response; +import org.junit.Test; +import org.mockito.Mock; + +import ninja.NinjaDocTester; +import services.UserService; + public class ApiControllerDocTesterTest extends NinjaDocTester { String URL_INDEX = "/"; - String URL_HELLO_WORLD_JSON = "/hello_world.json"; + String URL_HELLO = "/hello"; + String URL_USER_JSON = "/userJson"; + String URL_USERS = "/users"; + + @Mock + UserService userService; @Test public void testGetIndex() { - - Response response = makeRequest( - Request.GET().url( - testServerUrl().path(URL_INDEX))); - - assertThat(response.payload, containsString("Hello World!")); - assertThat(response.payload, containsString("BAM!")); - - + Response response = makeRequest(Request.GET().url(testServerUrl().path(URL_INDEX))); + assertThat(response.payload, containsString("Bonjour, bienvenue dans Ninja Framework!")); } @Test - public void testGetHelloWorldJson() { - - Response response = makeRequest( - Request.GET().url( - testServerUrl().path(URL_HELLO_WORLD_JSON))); - - ApplicationController.SimplePojo simplePojo - = response.payloadJsonAs(ApplicationController.SimplePojo.class); - - assertThat(simplePojo.content, CoreMatchers.equalTo("Hello World! Hello Json!")); - - + public void testGetHello() { + Response response = makeRequest(Request.GET().url(testServerUrl().path(URL_HELLO))); + assertThat(response.payload, containsString("Hello, welcome to Ninja Framework!")); } - + } diff --git a/ninja/src/test/java/controllers/ApiControllerMockTest.java b/ninja/src/test/java/controllers/ApiControllerMockTest.java new file mode 100644 index 0000000000..3057613d2e --- /dev/null +++ b/ninja/src/test/java/controllers/ApiControllerMockTest.java @@ -0,0 +1,41 @@ +package controllers; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThat; + +import javax.inject.Inject; + +import org.doctester.testbrowser.Request; +import org.doctester.testbrowser.Response; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; + +import ninja.NinjaRunner; +import ninja.Result; +import services.UserService; + +@RunWith(NinjaRunner.class) +public class ApiControllerMockTest { + + @Inject private UserService userService; + + ApplicationController applicationController; + + @Before + public void setupTest() { + applicationController = new ApplicationController(); + applicationController.userService = userService; + } + + @Test + public void testThatGetUserJson() { + Result result = applicationController.userJson(); + System.out.println(result.getRenderable()); + assertEquals(userService.getUser().toString(), result.getRenderable().toString()); + } + +} From 7daa791c2b906ef8e5beae3cc05d2e6ef14bccd8 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sat, 14 Dec 2019 16:34:34 +0200 Subject: [PATCH 03/11] BAEL-3464 - Ninja framework --- ninja/pom.xml | 14 ---- ninja/src/main/java/META-INF/persistence.xml | 30 ++++---- ninja/src/main/java/conf/Routes.java | 7 +- ninja/src/main/java/conf/application.conf | 19 +---- .../controllers/ApplicationController.java | 73 ++++++++++++------- ninja/src/main/java/models/User.java | 3 + ninja/src/main/java/services/UserService.java | 4 +- .../main/java/services/UserServiceImpl.java | 12 +-- .../ApplicationController/createUser.ftl.html | 12 +++ .../views/ApplicationController/home.ftl.html | 9 +++ .../ApplicationController/index.ftl.html | 18 ++--- .../main/java/views/layout/header.ftl.html | 2 + .../ApiControllerDocTesterTest.java | 15 +--- .../controllers/ApiControllerMockTest.java | 11 +-- 14 files changed, 114 insertions(+), 115 deletions(-) create mode 100644 ninja/src/main/java/views/ApplicationController/createUser.ftl.html create mode 100644 ninja/src/main/java/views/ApplicationController/home.ftl.html diff --git a/ninja/pom.xml b/ninja/pom.xml index 962dabf1bd..8ec2422d9f 100644 --- a/ninja/pom.xml +++ b/ninja/pom.xml @@ -4,10 +4,6 @@ 4.0.0 ninja - - - - jar com.baeldung 1.0.0 @@ -172,16 +168,6 @@ h2 1.4.186 - - - - org.ninjaframework ninja-standalone diff --git a/ninja/src/main/java/META-INF/persistence.xml b/ninja/src/main/java/META-INF/persistence.xml index bfdd6ad8e8..e57cd5ecc0 100644 --- a/ninja/src/main/java/META-INF/persistence.xml +++ b/ninja/src/main/java/META-INF/persistence.xml @@ -1,33 +1,29 @@ - - - - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" + version="2.0"> + + + org.hibernate.jpa.HibernatePersistenceProvider - - + - - - - + + + - - + - + \ No newline at end of file diff --git a/ninja/src/main/java/conf/Routes.java b/ninja/src/main/java/conf/Routes.java index 4f23fd37fe..a1727d55b0 100644 --- a/ninja/src/main/java/conf/Routes.java +++ b/ninja/src/main/java/conf/Routes.java @@ -10,10 +10,15 @@ public class Routes implements ApplicationRoutes { @Override public void init(Router router) { - router.GET().route("/").with(ApplicationController::index); + router.GET().route("/index").with(ApplicationController::index); + router.GET().route("/home").with(ApplicationController::home); router.GET().route("/hello").with(ApplicationController::helloWorld); router.GET().route("/userJson").with(ApplicationController::userJson); + router.GET().route("/createUser").with(ApplicationController::createUser); + router.GET().route("/flash").with(ApplicationController::showFlashMsg); + router.GET().route("/users").with(ApplicationController::fetchUsers); + router.POST().route("/users").with(ApplicationController::insertUser); //Assets router.GET().route("/assets/webjars/{fileName: .*}").with(AssetsController::serveWebJars); diff --git a/ninja/src/main/java/conf/application.conf b/ninja/src/main/java/conf/application.conf index 3bfbc70e1a..0ae4c7ec40 100644 --- a/ninja/src/main/java/conf/application.conf +++ b/ninja/src/main/java/conf/application.conf @@ -1,20 +1,9 @@ -# The main properties file to configure your application -# -# Properties can be prefixed by "%". -# "%"" matches a mode you can set as system property. For instance when you -# are using maven you can do: mvn -Dmode=test jetty:run -# -# Using that the following property: -# %test.myproperty=test property -# would overwrite the property -# myproperty=my property -# -# You can define as many environments as you want. Simply set them in the mode. -application.name=baeldung ninja application +application.name=baeldung ninja dev application +%test.application.name=baeldung ninja test application +%prod.application.name=baeldung ninja application application.cookie.prefix=NINJA -#ISO Language Code, optionally followed by a valid ISO Country Code. application.languages=fr,en application.session.expire_time_in_seconds=3600 @@ -22,8 +11,6 @@ application.session.send_only_if_changed=true application.session.transferred_over_https_only=false ninja.port=8000 - -# enable ssl with self-signed cert in dev & test modes ninja.ssl.port=8001 application.secret = fxSjSL9Q017BSL7gBnkyo2Prln7uXaXIT35gotXRIED8c46OSa8s4QdoIQdTsEtj diff --git a/ninja/src/main/java/controllers/ApplicationController.java b/ninja/src/main/java/controllers/ApplicationController.java index 65c95e9204..cd8ad9d160 100644 --- a/ninja/src/main/java/controllers/ApplicationController.java +++ b/ninja/src/main/java/controllers/ApplicationController.java @@ -1,14 +1,8 @@ package controllers; -import ninja.Context; -import ninja.Result; -import ninja.Results; -import ninja.i18n.Lang; -import ninja.i18n.Messages; -import ninja.jpa.UnitOfWork; -import services.UserService; - +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import javax.inject.Inject; @@ -20,9 +14,19 @@ import org.apache.commons.logging.LogFactory; import com.google.inject.Provider; import com.google.inject.Singleton; +import com.google.inject.persist.Transactional; import models.User; -import com.google.inject.persist.Transactional; +import ninja.Context; +import ninja.Result; +import ninja.Results; +import ninja.i18n.Lang; +import ninja.i18n.Messages; +import ninja.jpa.UnitOfWork; +import ninja.session.FlashScope; +import ninja.validation.JSR303Validation; +import ninja.validation.Validation; +import services.UserService; @Singleton public class ApplicationController { @@ -40,44 +44,59 @@ public class ApplicationController { @Inject UserService userService; - + public Result index() { - Result result = Results.html(); - lang.setLanguage("fr", result); - return result; - + return Results.html(); } public Result userJson() { - User user = userService.getUser(); - logger.info(user); - return Results.json().render(user); + HashMap userMap = userService.getUserMap(); + logger.info(userMap); + return Results.json().render(userMap); } public Result helloWorld(Context context) { - Optional language = Optional.of("en"); + Optional language = Optional.of("fr"); String helloMsg = msg.get("helloMsg", language).get(); return Results.text().render(helloMsg); } + public Result showFlashMsg(FlashScope flashScope) { + flashScope.success("Success message"); + flashScope.error("Error message"); + return Results.redirect("/home"); + } + + public Result home() { + return Results.html(); + } + + public Result createUser() { + return Results.html(); + } + @UnitOfWork public Result fetchUsers() { - insertUser(); EntityManager entityManager = entitiyManagerProvider.get(); Query q = entityManager.createQuery("SELECT x FROM User x"); List users = (List) q.getResultList(); - System.out.println(users); - return Results.text().render("Done"); + return Results.json().render(users); } @Transactional - public Result insertUser() { - EntityManager entityManager = entitiyManagerProvider.get(); - User user = userService.getUser(); - entityManager.persist(user); - entityManager.flush(); + public Result insertUser(FlashScope flashScope, @JSR303Validation User user, Validation validation) { + logger.info("Inserting User : " +user); + + if (validation.getViolations().size() > 0) { + flashScope.error("Validation Error: User can't be created"); + } else { + EntityManager entityManager = entitiyManagerProvider.get(); + entityManager.persist(user); + entityManager.flush(); + flashScope.success("User '" + user + "' is created successfully"); + } - return Results.text().render("Inserted"); + return Results.redirect("/home"); } } diff --git a/ninja/src/main/java/models/User.java b/ninja/src/main/java/models/User.java index d1b18cfa83..e021567bca 100644 --- a/ninja/src/main/java/models/User.java +++ b/ninja/src/main/java/models/User.java @@ -4,6 +4,7 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.validation.constraints.NotNull; @Entity public class User { @@ -12,7 +13,9 @@ public class User { @GeneratedValue(strategy=GenerationType.AUTO) Long id; + @NotNull public String firstName; + public String email; public String toString() { diff --git a/ninja/src/main/java/services/UserService.java b/ninja/src/main/java/services/UserService.java index a549b48e48..10d625c66f 100644 --- a/ninja/src/main/java/services/UserService.java +++ b/ninja/src/main/java/services/UserService.java @@ -1,9 +1,9 @@ package services; -import models.User; +import java.util.HashMap; public interface UserService { - User getUser(); + HashMap getUserMap(); } diff --git a/ninja/src/main/java/services/UserServiceImpl.java b/ninja/src/main/java/services/UserServiceImpl.java index 3ffa53400f..0f8c2214cf 100644 --- a/ninja/src/main/java/services/UserServiceImpl.java +++ b/ninja/src/main/java/services/UserServiceImpl.java @@ -1,15 +1,15 @@ package services; -import models.User; +import java.util.HashMap; public class UserServiceImpl implements UserService { @Override - public User getUser() { - User user = new User(); - user.firstName = "Norman"; - user.email = "norman@email.com"; - return user; + public HashMap getUserMap() { + HashMap userMap = new HashMap<>(); + userMap.put("name", "Norman Lewis"); + userMap.put("email", "norman@email.com"); + return userMap; } } diff --git a/ninja/src/main/java/views/ApplicationController/createUser.ftl.html b/ninja/src/main/java/views/ApplicationController/createUser.ftl.html new file mode 100644 index 0000000000..9156f7dbf2 --- /dev/null +++ b/ninja/src/main/java/views/ApplicationController/createUser.ftl.html @@ -0,0 +1,12 @@ +<#import "../layout/defaultLayout.ftl.html" as layout> +<@layout.myLayout "Create User"> + +
+ + First Name : +
+ Email : +
+ + + \ No newline at end of file diff --git a/ninja/src/main/java/views/ApplicationController/home.ftl.html b/ninja/src/main/java/views/ApplicationController/home.ftl.html new file mode 100644 index 0000000000..10f6612d54 --- /dev/null +++ b/ninja/src/main/java/views/ApplicationController/home.ftl.html @@ -0,0 +1,9 @@ +<#import "../layout/defaultLayout.ftl.html" as layout> +<@layout.myLayout "Home page"> + + +

${i18n("helloMsg")}

+ +User Json + + \ No newline at end of file diff --git a/ninja/src/main/java/views/ApplicationController/index.ftl.html b/ninja/src/main/java/views/ApplicationController/index.ftl.html index 10f6612d54..25d0a31229 100644 --- a/ninja/src/main/java/views/ApplicationController/index.ftl.html +++ b/ninja/src/main/java/views/ApplicationController/index.ftl.html @@ -1,9 +1,9 @@ -<#import "../layout/defaultLayout.ftl.html" as layout> -<@layout.myLayout "Home page"> - - -

${i18n("helloMsg")}

- -User Json - - \ No newline at end of file + + + Ninja: Index + + +

${i18n("helloMsg")}

+ User Json + + \ No newline at end of file diff --git a/ninja/src/main/java/views/layout/header.ftl.html b/ninja/src/main/java/views/layout/header.ftl.html index ebd55a9f4c..af79449bf2 100644 --- a/ninja/src/main/java/views/layout/header.ftl.html +++ b/ninja/src/main/java/views/layout/header.ftl.html @@ -11,6 +11,8 @@ diff --git a/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java b/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java index e383859dfb..d5f4f1811c 100644 --- a/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java +++ b/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java @@ -2,37 +2,26 @@ package controllers; import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertThat; - -import javax.inject.Inject; - import org.doctester.testbrowser.Request; import org.doctester.testbrowser.Response; import org.junit.Test; -import org.mockito.Mock; - import ninja.NinjaDocTester; -import services.UserService; public class ApiControllerDocTesterTest extends NinjaDocTester { String URL_INDEX = "/"; String URL_HELLO = "/hello"; - String URL_USER_JSON = "/userJson"; - String URL_USERS = "/users"; - - @Mock - UserService userService; @Test public void testGetIndex() { Response response = makeRequest(Request.GET().url(testServerUrl().path(URL_INDEX))); - assertThat(response.payload, containsString("Bonjour, bienvenue dans Ninja Framework!")); + assertThat(response.payload, containsString("Hello, welcome to Ninja Framework!")); } @Test public void testGetHello() { Response response = makeRequest(Request.GET().url(testServerUrl().path(URL_HELLO))); - assertThat(response.payload, containsString("Hello, welcome to Ninja Framework!")); + assertThat(response.payload, containsString("Bonjour, bienvenue dans Ninja Framework!")); } } diff --git a/ninja/src/test/java/controllers/ApiControllerMockTest.java b/ninja/src/test/java/controllers/ApiControllerMockTest.java index 3057613d2e..2466e54b2a 100644 --- a/ninja/src/test/java/controllers/ApiControllerMockTest.java +++ b/ninja/src/test/java/controllers/ApiControllerMockTest.java @@ -1,19 +1,10 @@ package controllers; -import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertThat; - import javax.inject.Inject; - -import org.doctester.testbrowser.Request; -import org.doctester.testbrowser.Response; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mock; - import ninja.NinjaRunner; import ninja.Result; import services.UserService; @@ -35,7 +26,7 @@ public class ApiControllerMockTest { public void testThatGetUserJson() { Result result = applicationController.userJson(); System.out.println(result.getRenderable()); - assertEquals(userService.getUser().toString(), result.getRenderable().toString()); + assertEquals(userService.getUserMap().toString(), result.getRenderable().toString()); } } From dad9fd657997fa4a88db56c5877e2d3def95c877 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Mon, 16 Dec 2019 11:51:03 +0200 Subject: [PATCH 04/11] ninja module added to the pom.xml --- pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 508b6921a7..b841b757d8 100644 --- a/pom.xml +++ b/pom.xml @@ -589,7 +589,7 @@ mustache mybatis - + ninja netflix optaplanner @@ -670,6 +670,7 @@ + ninja netflix parent-boot-1 From 30dbf823f9786f1536c313f455bdb31291f81218 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Mon, 16 Dec 2019 12:27:31 +0200 Subject: [PATCH 05/11] ninja module added to the pom.xml --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index b841b757d8..0d4b11fc45 100644 --- a/pom.xml +++ b/pom.xml @@ -670,7 +670,7 @@ - ninja + netflix parent-boot-1 @@ -1224,7 +1224,7 @@ mustache mybatis - + ninja netflix optaplanner From 1ff24803d032948a9520a8c02e8b69e7e65fb554 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Thu, 19 Dec 2019 10:51:22 +0200 Subject: [PATCH 06/11] code indentation fixed --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 0d4b11fc45..64ecfb6925 100644 --- a/pom.xml +++ b/pom.xml @@ -589,7 +589,7 @@ mustache mybatis - ninja + ninja netflix optaplanner @@ -1224,7 +1224,7 @@ mustache mybatis - ninja + ninja netflix optaplanner From c63eeb2e7b7107b7f15aff5159dbcf68285cf729 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Thu, 19 Dec 2019 10:52:45 +0200 Subject: [PATCH 07/11] typo corrected --- ninja/src/main/java/controllers/ApplicationController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ninja/src/main/java/controllers/ApplicationController.java b/ninja/src/main/java/controllers/ApplicationController.java index cd8ad9d160..38dd598694 100644 --- a/ninja/src/main/java/controllers/ApplicationController.java +++ b/ninja/src/main/java/controllers/ApplicationController.java @@ -40,7 +40,7 @@ public class ApplicationController { private static Log logger = LogFactory.getLog(ApplicationController.class); @Inject - Provider entitiyManagerProvider; + Provider entityManagerProvider; @Inject UserService userService; @@ -77,7 +77,7 @@ public class ApplicationController { @UnitOfWork public Result fetchUsers() { - EntityManager entityManager = entitiyManagerProvider.get(); + EntityManager entityManager = entityManagerProvider.get(); Query q = entityManager.createQuery("SELECT x FROM User x"); List users = (List) q.getResultList(); return Results.json().render(users); @@ -90,7 +90,7 @@ public class ApplicationController { if (validation.getViolations().size() > 0) { flashScope.error("Validation Error: User can't be created"); } else { - EntityManager entityManager = entitiyManagerProvider.get(); + EntityManager entityManager = entityManagerProvider.get(); entityManager.persist(user); entityManager.flush(); flashScope.success("User '" + user + "' is created successfully"); From f49080b917ce34c0144e7a9df453d4fbf88a97c0 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Thu, 19 Dec 2019 11:13:18 +0200 Subject: [PATCH 08/11] code indentation --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index 64ecfb6925..b3405e2f53 100644 --- a/pom.xml +++ b/pom.xml @@ -670,7 +670,6 @@ - netflix parent-boot-1 From 4f5d451d9546ef27c4bd2a6fef3fe118c0cf0f1d Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Thu, 19 Dec 2019 11:14:02 +0200 Subject: [PATCH 09/11] Updated ApiControllerDocTesterUnitTest --- ninja/src/test/java/controllers/ApiControllerDocTesterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java b/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java index d5f4f1811c..969073d5d8 100644 --- a/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java +++ b/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java @@ -7,7 +7,7 @@ import org.doctester.testbrowser.Response; import org.junit.Test; import ninja.NinjaDocTester; -public class ApiControllerDocTesterTest extends NinjaDocTester { +public class ApiControllerDocTesterUnitTest extends NinjaDocTester { String URL_INDEX = "/"; String URL_HELLO = "/hello"; From edcded43247711851eea8bae76caffc8a7ed3fa9 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Thu, 19 Dec 2019 11:14:30 +0200 Subject: [PATCH 10/11] Updated ApiControllerMockUnitTest --- ninja/src/test/java/controllers/ApiControllerMockTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ninja/src/test/java/controllers/ApiControllerMockTest.java b/ninja/src/test/java/controllers/ApiControllerMockTest.java index 2466e54b2a..cb53965678 100644 --- a/ninja/src/test/java/controllers/ApiControllerMockTest.java +++ b/ninja/src/test/java/controllers/ApiControllerMockTest.java @@ -10,7 +10,7 @@ import ninja.Result; import services.UserService; @RunWith(NinjaRunner.class) -public class ApiControllerMockTest { +public class ApiControllerMockUnitTest { @Inject private UserService userService; From 6b93a328113d44132fa6e1d413aa2445ed5de593 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Thu, 19 Dec 2019 11:46:23 +0200 Subject: [PATCH 11/11] Test name fixes --- ninja/src/test/java/controllers/ApiControllerDocTesterTest.java | 2 +- ...piControllerMockTest.java => ApiControllerMockUnitTest.java} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename ninja/src/test/java/controllers/{ApiControllerMockTest.java => ApiControllerMockUnitTest.java} (100%) diff --git a/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java b/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java index 969073d5d8..d5f4f1811c 100644 --- a/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java +++ b/ninja/src/test/java/controllers/ApiControllerDocTesterTest.java @@ -7,7 +7,7 @@ import org.doctester.testbrowser.Response; import org.junit.Test; import ninja.NinjaDocTester; -public class ApiControllerDocTesterUnitTest extends NinjaDocTester { +public class ApiControllerDocTesterTest extends NinjaDocTester { String URL_INDEX = "/"; String URL_HELLO = "/hello"; diff --git a/ninja/src/test/java/controllers/ApiControllerMockTest.java b/ninja/src/test/java/controllers/ApiControllerMockUnitTest.java similarity index 100% rename from ninja/src/test/java/controllers/ApiControllerMockTest.java rename to ninja/src/test/java/controllers/ApiControllerMockUnitTest.java