Files
java-tutorials/ratpack/src/test/java/com/baeldung/AppHttpUnitTest.java
T

59 lines
2.2 KiB
Java
Raw Normal View History

2018-08-30 02:56:21 +05:30
package com.baeldung;
import com.baeldung.model.Employee;
import org.junit.Test;
import ratpack.exec.Promise;
import ratpack.func.Action;
import ratpack.handling.Chain;
2018-09-05 00:18:18 +05:30
import ratpack.handling.Handler;
2018-08-30 02:56:21 +05:30
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 {
2018-09-05 00:18:18 +05:30
Handler allHandler = ctx -> {
2018-08-30 02:56:21 +05:30
Long id = Long.valueOf(ctx.getPathTokens()
2018-09-05 00:18:18 +05:30
.get("id"));
2018-08-30 02:56:21 +05:30
Employee employee = new Employee(id, "Mr", "NY");
ctx.next(Registry.single(Employee.class, employee));
2018-09-05 00:18:18 +05:30
};
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);
});
2018-08-30 02:56:21 +05:30
EmbeddedApp.fromHandlers(chainAction)
2018-09-05 00:18:18 +05:30
.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());
});
2018-08-30 02:56:21 +05:30
}
@Test
public void givenSyncDataSource_GetDataFromPromise() throws Exception {
String value = ExecHarness.yieldSingle(execution -> Promise.sync(() -> "Foo"))
2018-09-05 00:18:18 +05:30
.getValueOrThrow();
2018-08-30 02:56:21 +05:30
assertEquals("Foo", value);
}
}