BAEL-1026 (#2322)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
0a631ac326
commit
9e193396ef
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public interface ArticleList {
|
||||
|
||||
List<String> articles();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@Configuration
|
||||
public class Config {
|
||||
|
||||
@Bean
|
||||
public Content content() {
|
||||
return () -> "hello baeldung!";
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ArticleList articles() {
|
||||
return () -> Arrays.asList("Introduction to Ratpack", "Ratpack Google Guice Integration", "Ratpack Spring Boot Integration");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public interface Content {
|
||||
|
||||
String body();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import ratpack.func.Action;
|
||||
import ratpack.handling.Chain;
|
||||
import ratpack.server.ServerConfig;
|
||||
import ratpack.spring.config.EnableRatpack;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableRatpack
|
||||
public class EmbedRatpackApp {
|
||||
|
||||
@Autowired private Content content;
|
||||
@Autowired private ArticleList list;
|
||||
|
||||
@Bean
|
||||
public Action<Chain> hello() {
|
||||
return chain -> chain.get("hello", ctx -> ctx.render(content.body()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Action<Chain> list() {
|
||||
return chain -> chain.get("list", ctx -> ctx.render(list
|
||||
.articles()
|
||||
.toString()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServerConfig ratpackServerConfig() {
|
||||
return ServerConfig
|
||||
.builder()
|
||||
.findBaseDir("public")
|
||||
.build();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(EmbedRatpackApp.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
import ratpack.server.RatpackServer;
|
||||
|
||||
import static ratpack.spring.Spring.spring;
|
||||
|
||||
public class EmbedSpringBootApp {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
RatpackServer.start(server -> server
|
||||
.registry(spring(Config.class))
|
||||
.handlers(chain -> chain.get(ctx -> ctx.render(ctx
|
||||
.get(Content.class)
|
||||
.body()))));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Special Static Resource</title>
|
||||
</head>
|
||||
<body>
|
||||
This page is static.
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.spring;
|
||||
|
||||
import org.junit.Test;
|
||||
import ratpack.test.MainClassApplicationUnderTest;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* @author aiet
|
||||
*/
|
||||
public class EmbedRatpackAppIntegrationTest {
|
||||
|
||||
MainClassApplicationUnderTest appUnderTest = new MainClassApplicationUnderTest(EmbedRatpackApp.class);
|
||||
|
||||
@Test
|
||||
public void whenSayHello_thenGotWelcomeMessage() {
|
||||
assertEquals("hello baeldung!", appUnderTest
|
||||
.getHttpClient()
|
||||
.getText("/hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestList_thenGotArticles() throws IOException {
|
||||
assertEquals(3, appUnderTest
|
||||
.getHttpClient()
|
||||
.getText("/list")
|
||||
.split(",").length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestStaticResource_thenGotStaticContent() {
|
||||
assertThat(appUnderTest
|
||||
.getHttpClient()
|
||||
.getText("/"), containsString("page is static"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user