Takes - unit and integration test
This commit is contained in:
parent
10ae695227
commit
a099b358a7
|
@ -87,29 +87,12 @@
|
|||
<dependency>
|
||||
<groupId>org.takes</groupId>
|
||||
<artifactId>takes</artifactId>
|
||||
<version>1.19</version>
|
||||
<version>${takes.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity-engine-core</artifactId>
|
||||
<version>2.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>HikariCP</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.json</groupId>
|
||||
<artifactId>javax.json-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.json</artifactId>
|
||||
<version>1.1</version>
|
||||
<version>${velocity-engine-core.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -149,10 +132,6 @@
|
|||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/webapp</directory>
|
||||
<filtering>true</filtering>
|
||||
|
@ -160,47 +139,6 @@
|
|||
</resources>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>hit-refresh</id>
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/webapp</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>1.6.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>start-server</id>
|
||||
<phase>pre-integration-test</phase>
|
||||
<goals>
|
||||
<goal>java</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.takes.TakesApp</mainClass>
|
||||
<cleanupDaemonThreads>false</cleanupDaemonThreads>
|
||||
<arguments>
|
||||
<argument>--port=${port}</argument>
|
||||
</arguments>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<jcommander.version>1.78</jcommander.version>
|
||||
<lombok.version>1.18.6</lombok.version>
|
||||
|
@ -218,5 +156,8 @@
|
|||
<jcabi-maven-plugin.version>0.14.1</jcabi-maven-plugin.version>
|
||||
<aspectjtools.version>1.9.2</aspectjtools.version>
|
||||
<aspectjweaver.version>1.9.2</aspectjweaver.version>
|
||||
|
||||
<takes.version>1.19</takes.version>
|
||||
<velocity-engine-core.version>2.2</velocity-engine-core.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -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<Response> route(final RqFallback req) {
|
||||
return new Opt.Single<Response>(
|
||||
new RsText(req.throwable().getMessage())
|
||||
);
|
||||
return new Opt.Single<Response>(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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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("<html>"
|
||||
+ "<head>"
|
||||
+ "<title>Takes Application - Contact</title></head>"
|
||||
+ "<body style='color:green'>"
|
||||
+ "Contact us at @baeldung.com"
|
||||
+ "</body></html>"), "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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<String> idParam = form.param("id");
|
||||
Iterable<String> 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<String> 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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)
|
||||
);
|
|
@ -4,6 +4,6 @@
|
|||
</head>
|
||||
<body>
|
||||
<h1>Takes Web Application</h1>
|
||||
<h2>Welcome, ${userName}</h2>
|
||||
<h2>Welcome, ${username}</h2>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue