Skip to content

Commit f6e570c

Browse files
AbhinabKanrarmaibin
authored andcommitted
adding following modules with updated testcase : DB, Filter, Json (eugenp#1410)
* adding ratpack module * adding pom.xml * adding following modules with updated testcase : DB, Filter, Json
1 parent 5cd552a commit f6e570c

7 files changed

Lines changed: 159 additions & 28 deletions

File tree

‎ratpack/build.gradle‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ buildscript {
44
}
55
dependencies {
66
classpath "io.ratpack:ratpack-gradle:1.4.5"
7+
classpath "com.h2database:h2:1.4.193"
78
}
89
}
910

@@ -19,6 +20,8 @@ repositories {
1920
}
2021

2122
dependencies {
23+
compile ratpack.dependency('hikari')
24+
compile 'com.h2database:h2:1.4.193'
2225
testCompile 'junit:junit:4.11'
2326
runtime "org.slf4j:slf4j-simple:1.7.21"
2427
}

‎ratpack/pom.xml‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,41 @@
1515
</properties>
1616

1717
<dependencies>
18-
<dependency>
19-
<groupId>io.ratpack</groupId>
20-
<artifactId>ratpack-core</artifactId>
21-
<version>1.4.5</version>
22-
</dependency>
23-
<dependency>
24-
<groupId>io.ratpack</groupId>
25-
<artifactId>ratpack-test</artifactId>
26-
<version>1.4.5</version>
27-
</dependency>
28-
<dependency>
29-
<groupId>junit</groupId>
30-
<artifactId>junit</artifactId>
31-
<version>4.12</version>
32-
<scope>test</scope>
33-
</dependency>
18+
<dependency>
19+
<groupId>io.ratpack</groupId>
20+
<artifactId>ratpack-core</artifactId>
21+
<version>1.4.5</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>io.ratpack</groupId>
25+
<artifactId>ratpack-hikari</artifactId>
26+
<version>1.4.5</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>io.ratpack</groupId>
30+
<artifactId>ratpack-test</artifactId>
31+
<version>1.4.5</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.h2database</groupId>
35+
<artifactId>h2</artifactId>
36+
<version>1.4.193</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
<version>4.12</version>
42+
<scope>test</scope>
43+
</dependency>
3444
</dependencies>
3545

46+
<build>
47+
<finalName>${project.artifactId}</finalName>
48+
<resources>
49+
<resource>
50+
<directory>src/main/resources</directory>
51+
</resource>
52+
</resources>
53+
</build>
54+
3655
</project>
Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
11
package com.baeldung;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import com.baeldung.filter.RequestValidatorFilter;
7+
import com.baeldung.model.Employee;
8+
9+
import ratpack.guice.Guice;
10+
import ratpack.hikari.HikariModule;
11+
import ratpack.http.MutableHeaders;
12+
import ratpack.jackson.Jackson;
313
import ratpack.http.MutableHeaders;
414
import ratpack.server.RatpackServer;
515

616
public class Application {
717

8-
public static void main(String... args) throws Exception {
9-
RatpackServer.start(server -> server.handlers(chain -> chain.all(ctx -> {
10-
MutableHeaders headers = ctx.getResponse().getHeaders();
11-
headers.set("Access-Control-Allow-Origin", "*");
12-
headers.set("Accept-Language", "en-us");
13-
headers.set("Accept-Charset", "UTF-8");
14-
ctx.next();
15-
})
16-
.get(ctx -> ctx.render("Welcome to baeldung ratpack!!!"))
17-
.get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!"))
18-
.post(":amount", ctx -> ctx.render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!"))
19-
));
18+
public static void main(String[] args) throws Exception {
19+
20+
List<Employee> employees = new ArrayList<Employee>();
21+
employees.add(new Employee(1L, "Mr", "John Doe"));
22+
employees.add(new Employee(2L, "Mr", "White Snow"));
23+
24+
25+
RatpackServer.start(
26+
server -> server.registry(Guice.registry(bindings -> bindings.module(HikariModule.class, config -> {
27+
config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
28+
config.addDataSourceProperty("URL",
29+
"jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'");
30+
}))).handlers(chain -> chain
31+
.all(
32+
// ctx -> {
33+
// MutableHeaders headers =
34+
// ctx.getResponse().getHeaders();
35+
// headers.set("Access-Control-Allow-Origin","*");
36+
// headers.set("Accept-Language", "en-us");
37+
// headers.set("Accept-Charset", "UTF-8");
38+
// ctx.next();
39+
// }
40+
new RequestValidatorFilter())
41+
.get(ctx -> ctx.render("Welcome to baeldung ratpack!!!"))
42+
.get("data/employees", ctx -> ctx.render(Jackson.json(employees)))
43+
.get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!"))
44+
.post(":amount", ctx -> ctx
45+
.render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!"))));
2046
}
2147

2248
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.filter;
2+
3+
import ratpack.handling.Context;
4+
import ratpack.handling.Handler;
5+
import ratpack.http.MutableHeaders;
6+
7+
public class RequestValidatorFilter implements Handler {
8+
9+
@Override
10+
public void handle(Context ctx) throws Exception {
11+
MutableHeaders headers = ctx.getResponse().getHeaders();
12+
headers.set("Access-Control-Allow-Origin", "*");
13+
ctx.next();
14+
}
15+
16+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.model;
2+
3+
import java.io.Serializable;
4+
5+
public class Employee implements Serializable {
6+
7+
private static final long serialVersionUID = 3077867088762010705L;
8+
9+
private Long id;
10+
private String title;
11+
private String name;
12+
13+
public Employee(Long id, String title, String name) {
14+
super();
15+
this.id = id;
16+
this.title = title;
17+
this.name = name;
18+
}
19+
20+
public Long getId() {
21+
return id;
22+
}
23+
24+
public void setId(Long id) {
25+
this.id = id;
26+
}
27+
28+
public String getTitle() {
29+
return title;
30+
}
31+
32+
public void setTitle(String title) {
33+
this.title = title;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public void setName(String name) {
41+
this.name = name;
42+
}
43+
44+
}

‎ratpack/src/main/resources/DDL.sql‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DROP TABLE IF EXISTS employee;
2+
CREATE TABLE employee (
3+
id bigint auto_increment primary key,
4+
title varchar(255),
5+
name varchar(255)
6+
)

‎ratpack/src/test/java/com/baeldung/ApplicationTest.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@
44
import org.junit.Test;
55
import org.junit.runner.RunWith;
66
import org.junit.runners.JUnit4;
7+
import com.baeldung.model.Employee;
8+
import com.fasterxml.jackson.core.JsonProcessingException;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
711
import ratpack.test.MainClassApplicationUnderTest;
812

913
import static org.junit.Assert.assertEquals;
1014

15+
import java.util.ArrayList;
16+
import java.util.List;
17+
1118
@RunWith(JUnit4.class)
1219
public class ApplicationTest {
1320

@@ -23,6 +30,16 @@ public void givenDynamicUrl_getDynamicText() {
2330
assertEquals("Hello dummybot!!!", appUnderTest.getHttpClient().getText("/dummybot"));
2431
}
2532

33+
@Test
34+
public void givenUrl_getListOfEmployee() throws JsonProcessingException {
35+
List<Employee> employees = new ArrayList<Employee>();
36+
ObjectMapper mapper = new ObjectMapper();
37+
employees.add(new Employee(1L, "Mr", "John Doe"));
38+
employees.add(new Employee(2L, "Mr", "White Snow"));
39+
40+
assertEquals(mapper.writeValueAsString(employees), appUnderTest.getHttpClient().getText("/data/employees"));
41+
}
42+
2643
@After
2744
public void shutdown() {
2845
appUnderTest.close();

0 commit comments

Comments
 (0)