Search in sources :

Example 6 with View

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TemplateResolver(com.tvd12.ezyhttp.server.core.view.TemplateResolver) ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) ThymeleafViewContextBuilder(com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) ViewDecorator(com.tvd12.ezyhttp.server.core.view.ViewDecorator) View(com.tvd12.ezyhttp.server.core.view.View) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 7 with 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();
}
Also used : User(org.youngmonkeys.example.ezyhttp.website.user_management.entity.User) DoGet(com.tvd12.ezyhttp.server.core.annotation.DoGet)

Example 8 with View

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);
}
Also used : View(com.tvd12.ezyhttp.server.core.view.View) Test(org.testng.annotations.Test)

Example 9 with View

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");
}
Also used : Cookie(javax.servlet.http.Cookie) Locale(java.util.Locale) View(com.tvd12.ezyhttp.server.core.view.View) Test(org.testng.annotations.Test)

Example 10 with View

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UnhandledErrorHandler(com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) View(com.tvd12.ezyhttp.server.core.view.View) HttpMethod(com.tvd12.ezyhttp.core.constant.HttpMethod) Test(org.testng.annotations.Test)

Aggregations

View (com.tvd12.ezyhttp.server.core.view.View)11 Test (org.testng.annotations.Test)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 TemplateResolver (com.tvd12.ezyhttp.server.core.view.TemplateResolver)4 ViewContext (com.tvd12.ezyhttp.server.core.view.ViewContext)4 ThymeleafViewContextBuilder (com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder)4 PrintWriter (java.io.PrintWriter)4 ServletContext (javax.servlet.ServletContext)4 ViewDecorator (com.tvd12.ezyhttp.server.core.view.ViewDecorator)2 Cookie (javax.servlet.http.Cookie)2 HttpMethod (com.tvd12.ezyhttp.core.constant.HttpMethod)1 MultiValueMap (com.tvd12.ezyhttp.core.data.MultiValueMap)1 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)1 DoGet (com.tvd12.ezyhttp.server.core.annotation.DoGet)1 UnhandledErrorHandler (com.tvd12.ezyhttp.server.core.handler.UnhandledErrorHandler)1 Redirect (com.tvd12.ezyhttp.server.core.view.Redirect)1 ViewDialect (com.tvd12.ezyhttp.server.core.view.ViewDialect)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1