diff --git a/ratpack/build.gradle b/ratpack/build.gradle new file mode 100644 index 0000000000..29d9633531 --- /dev/null +++ b/ratpack/build.gradle @@ -0,0 +1,32 @@ +buildscript { + repositories { + jcenter() + } + dependencies { + classpath "io.ratpack:ratpack-gradle:1.4.5" + } +} + +if (!JavaVersion.current().java8Compatible) { + throw new IllegalStateException("Must be built with Java 8 or higher") +} + +apply plugin: "io.ratpack.ratpack-java" +apply plugin: 'java' + +repositories { + jcenter() +} + +dependencies { + testCompile 'junit:junit:4.11' + runtime "org.slf4j:slf4j-simple:1.7.21" +} + +test { + testLogging { + events 'started', 'passed' + } +} + +mainClassName = "com.baeldung.Application" diff --git a/ratpack/pom.xml b/ratpack/pom.xml new file mode 100644 index 0000000000..0290a25d2b --- /dev/null +++ b/ratpack/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + com.baeldung + ratpack + jar + 1.0-SNAPSHOT + ratpack + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + io.ratpack + ratpack-core + 1.4.5 + + + io.ratpack + ratpack-test + 1.4.5 + + + junit + junit + 4.12 + test + + + + diff --git a/ratpack/src/main/java/com/baeldung/Application.java b/ratpack/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..3a5bf54d00 --- /dev/null +++ b/ratpack/src/main/java/com/baeldung/Application.java @@ -0,0 +1,22 @@ +package com.baeldung; + +import ratpack.http.MutableHeaders; +import ratpack.server.RatpackServer; + +public class Application { + + public static void main(String... args) throws Exception { + RatpackServer.start(server -> server.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(); + }) + .get(ctx -> ctx.render("Welcome to baeldung ratpack!!!")) + .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens().get("name") + "!!!")) + .post(":amount", ctx -> ctx.render(" Amount $" + ctx.getPathTokens().get("amount") + " added successfully !!!")) + )); + } + +} diff --git a/ratpack/src/test/java/com/baeldung/ApplicationTest.java b/ratpack/src/test/java/com/baeldung/ApplicationTest.java new file mode 100644 index 0000000000..f04a51f2bb --- /dev/null +++ b/ratpack/src/test/java/com/baeldung/ApplicationTest.java @@ -0,0 +1,31 @@ +package com.baeldung; + +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import ratpack.test.MainClassApplicationUnderTest; + +import static org.junit.Assert.assertEquals; + +@RunWith(JUnit4.class) +public class ApplicationTest { + + MainClassApplicationUnderTest appUnderTest = new MainClassApplicationUnderTest(Application.class); + + @Test + public void givenDefaultUrl_getStaticText() { + assertEquals("Welcome to baeldung ratpack!!!", appUnderTest.getHttpClient().getText("/")); + } + + @Test + public void givenDynamicUrl_getDynamicText() { + assertEquals("Hello dummybot!!!", appUnderTest.getHttpClient().getText("/dummybot")); + } + + @After + public void shutdown() { + appUnderTest.close(); + } + +} \ No newline at end of file