Merge pull request #9 from eugenp/master

update with origin
This commit is contained in:
Maiklins
2019-09-09 10:57:44 +02:00
committed by GitHub
parent db85c8f275
commit 621491f8db
20076 changed files with 1628788 additions and 0 deletions
@@ -0,0 +1,58 @@
package com.baeldung;
import com.baeldung.model.Employee;
import org.junit.Test;
import ratpack.exec.Promise;
import ratpack.func.Action;
import ratpack.handling.Chain;
import ratpack.handling.Handler;
import ratpack.registry.Registry;
import ratpack.test.embed.EmbeddedApp;
import ratpack.test.exec.ExecHarness;
import static org.junit.Assert.assertEquals;
public class AppHttpUnitTest {
@Test
public void givenAnyUri_GetEmployeeFromSameRegistry() throws Exception {
Handler allHandler = ctx -> {
Long id = Long.valueOf(ctx.getPathTokens()
.get("id"));
Employee employee = new Employee(id, "Mr", "NY");
ctx.next(Registry.single(Employee.class, employee));
};
Handler empNameHandler = ctx -> {
Employee employee = ctx.get(Employee.class);
ctx.getResponse()
.send("Name of employee with ID " + employee.getId() + " is " + employee.getName());
};
Handler empTitleHandler = ctx -> {
Employee employee = ctx.get(Employee.class);
ctx.getResponse()
.send("Title of employee with ID " + employee.getId() + " is " + employee.getTitle());
};
Action<Chain> chainAction = chain -> chain.prefix("employee/:id", empChain -> {
empChain.all(allHandler)
.get("name", empNameHandler)
.get("title", empTitleHandler);
});
EmbeddedApp.fromHandlers(chainAction)
.test(testHttpClient -> {
assertEquals("Name of employee with ID 1 is NY", testHttpClient.get("employee/1/name")
.getBody()
.getText());
assertEquals("Title of employee with ID 1 is Mr", testHttpClient.get("employee/1/title")
.getBody()
.getText());
});
}
@Test
public void givenSyncDataSource_GetDataFromPromise() throws Exception {
String value = ExecHarness.yieldSingle(execution -> Promise.sync(() -> "Foo"))
.getValueOrThrow();
assertEquals("Foo", value);
}
}
@@ -0,0 +1,53 @@
package com.baeldung;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
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 static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
@RunWith(JUnit4.class)
public class ApplicationIntegrationTest {
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"));
}
@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"));
}
@Test
public void givenStaticUrl_getDynamicText() {
assertEquals(21, appUnderTest.getHttpClient().getText("/randomString").length());
}
@After
public void shutdown() {
appUnderTest.close();
}
}
@@ -0,0 +1,50 @@
package com.baeldung.hystrix;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ratpack.test.MainClassApplicationUnderTest;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
/**
* @author aiet
*/
public class RatpackHystrixAppFallbackLiveTest {
static MainClassApplicationUnderTest appUnderTest;
@BeforeClass
public static void setup() {
System.setProperty("ratpack.hystrix.timeout", "10");
appUnderTest = new MainClassApplicationUnderTest(RatpackHystrixApp.class);
}
@Test
public void whenFetchReactive_thenGotFallbackProfile() {
assertThat(appUnderTest
.getHttpClient()
.getText("rx"), containsString("reactive fallback profile"));
}
@Test
public void whenFetchAsync_thenGotFallbackProfile() {
assertThat(appUnderTest
.getHttpClient()
.getText("async"), containsString("async fallback profile"));
}
@Test
public void whenFetchSync_thenGotFallbackProfile() {
assertThat(appUnderTest
.getHttpClient()
.getText("sync"), containsString("sync fallback profile"));
}
@AfterClass
public static void clean() {
appUnderTest.close();
}
}
@@ -0,0 +1,50 @@
package com.baeldung.hystrix;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import ratpack.test.MainClassApplicationUnderTest;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
/**
* @author aiet
*/
public class RatpackHystrixAppLiveTest {
static MainClassApplicationUnderTest appUnderTest;
@BeforeClass
public static void setup() {
System.setProperty("ratpack.hystrix.timeout", "5000");
appUnderTest = new MainClassApplicationUnderTest(RatpackHystrixApp.class);
}
@Test
public void whenFetchReactive_thenGotEugenProfile() {
assertThat(appUnderTest
.getHttpClient()
.getText("rx"), containsString("www.baeldung.com"));
}
@Test
public void whenFetchAsync_thenGotEugenProfile() {
assertThat(appUnderTest
.getHttpClient()
.getText("async"), containsString("www.baeldung.com"));
}
@Test
public void whenFetchSync_thenGotEugenProfile() {
assertThat(appUnderTest
.getHttpClient()
.getText("sync"), containsString("www.baeldung.com"));
}
@AfterClass
public static void clean() {
appUnderTest.close();
}
}
@@ -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"));
}
}