use of com.tvd12.ezyhttp.server.core.view.View in project ezyhttp by youngmonkeys.
the class ThymeleafViewContextTest method renderWithViewDecorator.
@Test
public void renderWithViewDecorator() throws Exception {
// given
TemplateResolver resolver = TemplateResolver.builder().build();
ViewDecorator viewDecorator = mock(ViewDecorator.class);
ViewContext viewContext = new ThymeleafViewContextBuilder().templateResolver(resolver).viewDecorators(Collections.singletonList(viewDecorator)).build();
ServletContext servletContext = mock(ServletContext.class);
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
PrintWriter writer = mock(PrintWriter.class);
when(response.getWriter()).thenReturn(writer);
View view = View.builder().template("index.html").build();
// when
viewContext.render(servletContext, request, response, view);
// then
Asserts.assertNotNull(viewContext);
verify(viewDecorator, times(1)).decorate(request, view);
}
use of com.tvd12.ezyhttp.server.core.view.View in project ezyfox-examples by tvd12.
the class UserViewController method getUser.
@DoGet("/{username}")
public View getUser(@PathVariable("username") String username) {
User user = userService.getUser(username);
String msg = user != null ? "User " + username + " with info: " + user : "User " + username + " not found";
return View.builder().addVariable("message", msg).template("user-info").build();
}
use of com.tvd12.ezyhttp.server.core.view.View in project ezyhttp by youngmonkeys.
the class ViewTest method putKeyValuesToVariableBuilderTest.
@Test
public void putKeyValuesToVariableBuilderTest() {
// given
Map<String, Object> map = EzyMapBuilder.mapBuilder().put("a", "1").put("b", 2).toMap();
View sut = View.builder().template("abc").putKeyValuesToVariable("hello", map).build();
// when
// then
Asserts.assertEquals(sut.getVariable("hello"), map);
}
use of com.tvd12.ezyhttp.server.core.view.View in project ezyhttp by youngmonkeys.
the class ViewTest method test.
@Test
public void test() {
// given
View sut = View.builder().template("index.html").locale(Locale.ENGLISH).locale("vi").contentType(ContentTypes.TEXT_HTML_UTF8).addHeader("foo", "bar").addHeader("hello", "world").addHeaders(Collections.singletonMap("header", "headerValue")).addCookie(new Cookie("cookie1", "cookie1Value")).addCookie(new Cookie("cookie2", "cookie2Value")).addCookie("cookie2", "cookie3Value", "/path").addVariable("variable1", "variable1Value").addVariable("variable2", "variable2Value").appendToVariable("list", "a").appendToVariable("list", "b").build();
// when
sut.setVariable("setValue", "value");
sut.setVariables(Collections.singletonMap("mapKey", "mapValue"));
sut.appendToVariable("list", "c");
// then
Asserts.assertEquals("index.html", sut.getTemplate());
Asserts.assertEquals(new Locale("vi"), sut.getLocale());
Asserts.assertEquals(ContentTypes.TEXT_HTML_UTF8, sut.getContentType());
Asserts.assertEquals(EzyMapBuilder.mapBuilder().put("foo", "bar").put("hello", "world").put("header", "headerValue").build(), sut.getHeaders());
Asserts.assertEquals("variable1Value", sut.getVariable("variable1"));
Asserts.assertEquals("variable2Value", sut.getVariable("variable2"));
Asserts.assertEquals(EzyMapBuilder.mapBuilder().put("variable1", "variable1Value").put("variable2", "variable2Value").put("setValue", "value").put("list", Arrays.asList("a", "b", "c")).put("mapKey", "mapValue").build(), sut.getVariables());
Asserts.assertEquals(sut.getVariable("setValue"), "value");
Asserts.assertEquals(sut.getVariable("list"), Arrays.asList("a", "b", "c"), false);
Asserts.assertTrue(sut.containsVariable("variable1"));
Asserts.assertFalse(sut.containsVariable("i don't know"));
Asserts.assertEquals(sut.getCookies().get(2).getPath(), "/path");
Asserts.assertEquals(sut.getVariable("mapKey"), "mapValue");
}
use of com.tvd12.ezyhttp.server.core.view.View in project ezyhttp by youngmonkeys.
the class UnhandledErrorHandlerTest method returnView.
@Test
public void returnView() {
// given
View result = View.builder().template("foo").build();
UnhandledErrorHandler sut = new UnhandledErrorHandler() {
@Override
public Object processError(HttpMethod method, HttpServletRequest request, HttpServletResponse response, int errorStatusCode, Exception exception) {
return result;
}
};
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getContentType()).thenReturn(ContentTypes.TEXT_HTML_UTF8);
// when
Object actual = sut.handleError(HttpMethod.GET, request, response, StatusCodes.BAD_REQUEST, null);
// then
Asserts.assertEquals(result, actual);
verify(response, times(1)).setContentType(ContentTypes.TEXT_HTML_UTF8);
}
Aggregations