adding following modules with updated testcase : DB, Filter, Json (#1410)
* adding ratpack module * adding pom.xml * adding following modules with updated testcase : DB, Filter, Json
This commit is contained in:
parent
5cd552a16b
commit
f6e570c6ca
|
@ -4,6 +4,7 @@ buildscript {
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath "io.ratpack:ratpack-gradle:1.4.5"
|
classpath "io.ratpack:ratpack-gradle:1.4.5"
|
||||||
|
classpath "com.h2database:h2:1.4.193"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +20,8 @@ repositories {
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
compile ratpack.dependency('hikari')
|
||||||
|
compile 'com.h2database:h2:1.4.193'
|
||||||
testCompile 'junit:junit:4.11'
|
testCompile 'junit:junit:4.11'
|
||||||
runtime "org.slf4j:slf4j-simple:1.7.21"
|
runtime "org.slf4j:slf4j-simple:1.7.21"
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,11 +20,21 @@
|
||||||
<artifactId>ratpack-core</artifactId>
|
<artifactId>ratpack-core</artifactId>
|
||||||
<version>1.4.5</version>
|
<version>1.4.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.ratpack</groupId>
|
||||||
|
<artifactId>ratpack-hikari</artifactId>
|
||||||
|
<version>1.4.5</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ratpack</groupId>
|
<groupId>io.ratpack</groupId>
|
||||||
<artifactId>ratpack-test</artifactId>
|
<artifactId>ratpack-test</artifactId>
|
||||||
<version>1.4.5</version>
|
<version>1.4.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<version>1.4.193</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
@ -33,4 +43,13 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>${project.artifactId}</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,22 +1,48 @@
|
||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.baeldung.filter.RequestValidatorFilter;
|
||||||
|
import com.baeldung.model.Employee;
|
||||||
|
|
||||||
|
import ratpack.guice.Guice;
|
||||||
|
import ratpack.hikari.HikariModule;
|
||||||
|
import ratpack.http.MutableHeaders;
|
||||||
|
import ratpack.jackson.Jackson;
|
||||||
import ratpack.http.MutableHeaders;
|
import ratpack.http.MutableHeaders;
|
||||||
import ratpack.server.RatpackServer;
|
import ratpack.server.RatpackServer;
|
||||||
|
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
public static void main(String... args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
RatpackServer.start(server -> server.handlers(chain -> chain.all(ctx -> {
|
|
||||||
MutableHeaders headers = ctx.getResponse().getHeaders();
|
List<Employee> employees = new ArrayList<Employee>();
|
||||||
headers.set("Access-Control-Allow-Origin", "*");
|
employees.add(new Employee(1L, "Mr", "John Doe"));
|
||||||
headers.set("Accept-Language", "en-us");
|
employees.add(new Employee(2L, "Mr", "White Snow"));
|
||||||
headers.set("Accept-Charset", "UTF-8");
|
|
||||||
ctx.next();
|
|
||||||
})
|
RatpackServer.start(
|
||||||
|
server -> server.registry(Guice.registry(bindings -> bindings.module(HikariModule.class, config -> {
|
||||||
|
config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
|
||||||
|
config.addDataSourceProperty("URL",
|
||||||
|
"jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'");
|
||||||
|
}))).handlers(chain -> chain
|
||||||
|
.all(
|
||||||
|
// ctx -> {
|
||||||
|
// MutableHeaders headers =
|
||||||
|
// ctx.getResponse().getHeaders();
|
||||||
|
// headers.set("Access-Control-Allow-Origin","*");
|
||||||
|
// headers.set("Accept-Language", "en-us");
|
||||||
|
// headers.set("Accept-Charset", "UTF-8");
|
||||||
|
// ctx.next();
|
||||||
|
// }
|
||||||
|
new RequestValidatorFilter())
|
||||||
.get(ctx -> ctx.render("Welcome to baeldung ratpack!!!"))
|
.get(ctx -> ctx.render("Welcome to baeldung ratpack!!!"))
|
||||||
|
.get("data/employees", ctx -> ctx.render(Jackson.json(employees)))
|
||||||
.get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!"))
|
.get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!"))
|
||||||
.post(":amount", ctx -> ctx.render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!"))
|
.post(":amount", ctx -> ctx
|
||||||
));
|
.render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.baeldung.filter;
|
||||||
|
|
||||||
|
import ratpack.handling.Context;
|
||||||
|
import ratpack.handling.Handler;
|
||||||
|
import ratpack.http.MutableHeaders;
|
||||||
|
|
||||||
|
public class RequestValidatorFilter implements Handler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handle(Context ctx) throws Exception {
|
||||||
|
MutableHeaders headers = ctx.getResponse().getHeaders();
|
||||||
|
headers.set("Access-Control-Allow-Origin", "*");
|
||||||
|
ctx.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class Employee implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 3077867088762010705L;
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
private String title;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Employee(Long id, String title, String name) {
|
||||||
|
super();
|
||||||
|
this.id = id;
|
||||||
|
this.title = title;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
DROP TABLE IF EXISTS employee;
|
||||||
|
CREATE TABLE employee (
|
||||||
|
id bigint auto_increment primary key,
|
||||||
|
title varchar(255),
|
||||||
|
name varchar(255)
|
||||||
|
)
|
|
@ -4,10 +4,17 @@ import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
import com.baeldung.model.Employee;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import ratpack.test.MainClassApplicationUnderTest;
|
import ratpack.test.MainClassApplicationUnderTest;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class ApplicationTest {
|
public class ApplicationTest {
|
||||||
|
|
||||||
|
@ -23,6 +30,16 @@ public class ApplicationTest {
|
||||||
assertEquals("Hello dummybot!!!", appUnderTest.getHttpClient().getText("/dummybot"));
|
assertEquals("Hello dummybot!!!", appUnderTest.getHttpClient().getText("/dummybot"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUrl_getListOfEmployee() throws JsonProcessingException {
|
||||||
|
List<Employee> employees = new ArrayList<Employee>();
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
employees.add(new Employee(1L, "Mr", "John Doe"));
|
||||||
|
employees.add(new Employee(2L, "Mr", "White Snow"));
|
||||||
|
|
||||||
|
assertEquals(mapper.writeValueAsString(employees), appUnderTest.getHttpClient().getText("/data/employees"));
|
||||||
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
appUnderTest.close();
|
appUnderTest.close();
|
||||||
|
|
Loading…
Reference in New Issue