diff --git a/libraries-3/pom.xml b/libraries-3/pom.xml index 17897e5118..3bacdbe1b3 100644 --- a/libraries-3/pom.xml +++ b/libraries-3/pom.xml @@ -87,29 +87,12 @@ org.takes takes - 1.19 + ${takes.version} org.apache.velocity velocity-engine-core - 2.2 - - - com.zaxxer - HikariCP - - - com.h2database - h2 - - - javax.json - javax.json-api - - - org.glassfish - javax.json - 1.1 + ${velocity-engine-core.version} @@ -149,10 +132,6 @@ - - src/main/resources - true - src/main/webapp true @@ -160,47 +139,6 @@ - - - hit-refresh - - - - src/main/resources - true - - - src/main/webapp - true - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - start-server - pre-integration-test - - java - - - - - com.baeldung.takes.TakesApp - false - - --port=${port} - - - - - - - - 1.78 1.18.6 @@ -218,5 +156,8 @@ 0.14.1 1.9.2 1.9.2 + + 1.19 + 2.2 diff --git a/libraries-3/src/main/java/com/baeldung/takes/TakesApp.java b/libraries-3/src/main/java/com/baeldung/takes/TakesApp.java index ea81612b86..3c1407c291 100644 --- a/libraries-3/src/main/java/com/baeldung/takes/TakesApp.java +++ b/libraries-3/src/main/java/com/baeldung/takes/TakesApp.java @@ -1,7 +1,6 @@ package com.baeldung.takes; import java.io.IOException; -import java.sql.Connection; import java.sql.SQLException; import org.takes.Response; @@ -16,51 +15,28 @@ import org.takes.http.Exit; import org.takes.http.FtBasic; import org.takes.misc.Opt; import org.takes.rs.RsText; -import org.takes.tk.TkSlf4j; - -import com.zaxxer.hikari.HikariConfig; -import com.zaxxer.hikari.HikariDataSource; public final class TakesApp { public static void main(final String... args) throws IOException, SQLException { new FtBasic( new TkFallback( - new TkSlf4j( - new TkFork( - new FkRegex("/", new TakesHelloWorld()), - new FkRegex("/index", new TakesIndex()), - new FkRegex("/contact", new TakesContact()), - new FkRegex("/createUser", new TakesCreateUser(TakesApp.dbConnection())), - new FkRegex("\\A/readUser", new TakesReadUser(TakesApp.dbConnection())) - ) + new TkFork( + new FkRegex("/", new TakesHelloWorld()), + new FkRegex("/index", new TakesIndex()), + new FkRegex("/contact", new TakesContact()) ), new FbChain( - new FbStatus(404, new RsText("sorry, page is absent")), - new FbStatus(405, new RsText("this method is not allowed here")), + new FbStatus(404, new RsText("Page Not Found")), + new FbStatus(405, new RsText("Method Not Allowed")), new Fallback() { @Override public Opt route(final RqFallback req) { - return new Opt.Single( - new RsText(req.throwable().getMessage()) - ); + return new Opt.Single(new RsText(req.throwable().getMessage())); } }) ), 6060 ).start(Exit.NEVER); } - private static Connection dbConnection() throws SQLException { - HikariConfig config = new HikariConfig(); - - config.setJdbcUrl("jdbc:h2:mem:devDB;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:/db.sql'"); - config.setUsername("sa"); - config.setPassword(""); - - @SuppressWarnings("resource") - HikariDataSource ds = new HikariDataSource(config); - - return ds.getConnection(); - } - } \ No newline at end of file diff --git a/libraries-3/src/main/java/com/baeldung/takes/TakesContact.java b/libraries-3/src/main/java/com/baeldung/takes/TakesContact.java index 6e2900ce67..4749929676 100644 --- a/libraries-3/src/main/java/com/baeldung/takes/TakesContact.java +++ b/libraries-3/src/main/java/com/baeldung/takes/TakesContact.java @@ -1,5 +1,7 @@ package com.baeldung.takes; +import java.io.IOException; + import org.takes.Request; import org.takes.Response; import org.takes.Take; @@ -10,13 +12,8 @@ import org.takes.rs.RsWithType; public final class TakesContact implements Take { @Override - public Response act(Request req) { - return new RsWithStatus(new RsWithType(new RsWithBody("" - + "" - + "Takes Application - Contact" - + "" - + "Contact us at @baeldung.com" - + ""), "text/html"), 200); + public Response act(Request req) throws IOException { + return new RsWithStatus(new RsWithType(new RsWithBody("Contact us at https://www.baeldung.com"), "text/html"), 200); } } diff --git a/libraries-3/src/main/java/com/baeldung/takes/TakesCreateUser.java b/libraries-3/src/main/java/com/baeldung/takes/TakesCreateUser.java deleted file mode 100644 index 3a17f96ca3..0000000000 --- a/libraries-3/src/main/java/com/baeldung/takes/TakesCreateUser.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.baeldung.takes; - -import java.io.IOException; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; - -import org.takes.Request; -import org.takes.Response; -import org.takes.Take; -import org.takes.rq.RqForm; -import org.takes.rq.form.RqFormSmart; -import org.takes.rs.RsWithStatus; - -public final class TakesCreateUser implements Take { - - public static Connection con; - - TakesCreateUser(Connection connection) { - con = connection; - } - - @Override - public Response act(final Request req) throws IOException { - - RqForm form = new RqFormSmart(req); - Iterable idParam = form.param("id"); - Iterable userParam = form.param("user"); - - int id = Integer.parseInt(idParam.iterator().next()); - String user = userParam.iterator().next(); - - final String INSERT_SQL_QUERY = "insert into take_user values ("+id+", "+"'"+user+"')"; - - try (PreparedStatement pst = con.prepareStatement(INSERT_SQL_QUERY)) { - System.out.println(pst.executeUpdate()); - - } catch (SQLException e) { - e.printStackTrace(); - } - - return new RsWithStatus(200); - } - -} diff --git a/libraries-3/src/main/java/com/baeldung/takes/TakesIndex.java b/libraries-3/src/main/java/com/baeldung/takes/TakesIndex.java index 93c61b51ff..1134f8ea3d 100644 --- a/libraries-3/src/main/java/com/baeldung/takes/TakesIndex.java +++ b/libraries-3/src/main/java/com/baeldung/takes/TakesIndex.java @@ -5,6 +5,7 @@ import java.io.IOException; import org.takes.Request; import org.takes.Response; import org.takes.Take; +import org.takes.rq.form.RqFormSmart; import org.takes.rs.RsHtml; import org.takes.rs.RsVelocity; @@ -12,8 +13,9 @@ public final class TakesIndex implements Take { @Override public Response act(final Request req) throws IOException { - - return new RsHtml(new RsVelocity(this.getClass().getResource("/templates/index.vm") ,new RsVelocity.Pair("userName", "Anshul"))); + RqFormSmart form = new RqFormSmart(req); + String username = form.single("username"); + return new RsHtml(new RsVelocity(this.getClass().getResource("/templates/index.vm"), new RsVelocity.Pair("username", username))); } } diff --git a/libraries-3/src/main/java/com/baeldung/takes/TakesReadUser.java b/libraries-3/src/main/java/com/baeldung/takes/TakesReadUser.java deleted file mode 100644 index b06ae53428..0000000000 --- a/libraries-3/src/main/java/com/baeldung/takes/TakesReadUser.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.takes; - -import java.io.IOException; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; - -import javax.json.Json; -import javax.json.JsonStructure; - -import org.takes.Request; -import org.takes.Response; -import org.takes.Take; -import org.takes.misc.Href; -import org.takes.rq.RqHref; -import org.takes.rs.RsJson; - - -public final class TakesReadUser implements Take { - - public static Connection con; - - TakesReadUser(Connection connection) { - con = connection; - } - - @Override - public Response act(final Request req) throws IOException { - Href href = new RqHref.Base(req).href(); - Iterable ids = href.param("id"); - int id = Integer.parseInt((String) ids.iterator().next()); - - final String SELECT_SQL_QUERY = "select id, user from take_user where id = " + id; - JsonStructure json = null; - try (PreparedStatement pst = con.prepareStatement(SELECT_SQL_QUERY); ResultSet rs = pst.executeQuery();) { - while (rs.next()) { - json = Json.createObjectBuilder() - .add("id", rs.getInt("id")) - .add("user", rs.getString("user")) - .build(); - } - } catch (SQLException e) { - e.printStackTrace(); - } - - return new RsJson(json); - } - -} diff --git a/libraries-3/src/main/resources/db.sql b/libraries-3/src/main/resources/db.sql deleted file mode 100644 index 3b73446537..0000000000 --- a/libraries-3/src/main/resources/db.sql +++ /dev/null @@ -1,7 +0,0 @@ -drop table if exists take_user; - -create table take_user( - id numeric, - user varchar(255), - constraint pk_take_user primary key (id) -); \ No newline at end of file diff --git a/libraries-3/src/main/webapp/templates/index.vm b/libraries-3/src/main/webapp/templates/index.vm index 1eaf8fa53b..5a97f654ce 100644 --- a/libraries-3/src/main/webapp/templates/index.vm +++ b/libraries-3/src/main/webapp/templates/index.vm @@ -4,6 +4,6 @@

Takes Web Application

-

Welcome, ${userName}

+

Welcome, ${username}

diff --git a/libraries-3/src/test/java/com/baeldung/takes/TakesAppIntegrationTest.java b/libraries-3/src/test/java/com/baeldung/takes/TakesAppIntegrationTest.java new file mode 100644 index 0000000000..8b869d0742 --- /dev/null +++ b/libraries-3/src/test/java/com/baeldung/takes/TakesAppIntegrationTest.java @@ -0,0 +1,36 @@ +package com.baeldung.takes; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.net.URI; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.Test; +import org.takes.http.FtRemote; + +public class TakesAppIntegrationTest { + + @Test + public void givenTake_whenRunRemoteServer_thenRespond() throws Exception { + new FtRemote(new TakesContact()).exec( + new FtRemote.Script() { + @Override + public void exec(final URI home) throws IOException { + HttpClient client = HttpClientBuilder.create().build(); + HttpResponse response = client.execute(new HttpGet(home)); + int statusCode = response.getStatusLine().getStatusCode(); + HttpEntity entity = response.getEntity(); + String result = EntityUtils.toString(entity); + + assertEquals(200, statusCode); + assertEquals("Contact us at https://www.baeldung.com", result); + } + }); + } +} diff --git a/libraries-3/src/test/java/com/baeldung/takes/TakesContactUnitTest.java b/libraries-3/src/test/java/com/baeldung/takes/TakesContactUnitTest.java new file mode 100644 index 0000000000..2f1948f100 --- /dev/null +++ b/libraries-3/src/test/java/com/baeldung/takes/TakesContactUnitTest.java @@ -0,0 +1,17 @@ +package com.baeldung.takes; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.takes.rq.RqFake; +import org.takes.rs.RsPrint; + +public class TakesContactUnitTest { + + @Test + public void givenTake_whenInvokeActMethod_thenRespond() throws Exception { + String resp = new RsPrint(new TakesContact().act(new RqFake())).printBody(); + assertEquals("Contact us at https://www.baeldung.com", resp); + } + +}