Search in sources :

Example 1 with View

use of com.tvd12.ezyhttp.server.core.view.View in project ezyhttp by youngmonkeys.

the class ViewTest method putKeyValuesToVariableTest.

@Test
public void putKeyValuesToVariableTest() {
    // given
    Map<String, Object> map = EzyMapBuilder.mapBuilder().put("a", "1").put("b", 2).toMap();
    View sut = View.builder().template("abc").build();
    sut.putKeyValuesToVariable("hello", map);
    // when
    // then
    Asserts.assertEquals(sut.getVariable("hello"), map);
}
Also used : View(com.tvd12.ezyhttp.server.core.view.View) Test(org.testng.annotations.Test)

Example 2 with View

use of com.tvd12.ezyhttp.server.core.view.View in project ezyhttp by youngmonkeys.

the class ViewTest method ofTest.

@Test
public void ofTest() {
    // given
    View sut = View.of("home.html");
    sut.setLocale(Locale.CANADA);
    // when
    // then
    Asserts.assertEquals("home.html", sut.getTemplate());
    Asserts.assertEquals(sut.getLocale(), Locale.CANADA);
}
Also used : View(com.tvd12.ezyhttp.server.core.view.View) Test(org.testng.annotations.Test)

Example 3 with View

use of com.tvd12.ezyhttp.server.core.view.View in project ezyhttp by youngmonkeys.

the class ViewTest method putKeyValuesToVariableByBuilderTest.

@Test
public void putKeyValuesToVariableByBuilderTest() {
    // given
    View sut = View.builder().template("abc").putKeyValueToVariable("hello", "a", "1").putKeyValueToVariable("hello", "b", 2).build();
    // when
    // then
    Map<String, Object> map = EzyMapBuilder.mapBuilder().put("a", "1").put("b", 2).toMap();
    Asserts.assertEquals(sut.getVariable("hello"), map);
}
Also used : View(com.tvd12.ezyhttp.server.core.view.View) Test(org.testng.annotations.Test)

Example 4 with View

use of com.tvd12.ezyhttp.server.core.view.View in project ezyhttp by youngmonkeys.

the class ThymeleafViewContext method render.

@Override
public void render(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response, View view) throws IOException {
    for (ViewDecorator viewDecorator : viewDecorators) {
        viewDecorator.decorate(request, view);
    }
    WebContext ctx = new WebContext(request, response, servletContext, view.getLocale());
    ctx.setVariables(view.getVariables());
    templateEngine.process(view.getTemplate(), ctx, response.getWriter());
}
Also used : WebContext(org.thymeleaf.context.WebContext) ViewDecorator(com.tvd12.ezyhttp.server.core.view.ViewDecorator)

Example 5 with View

use of com.tvd12.ezyhttp.server.core.view.View in project ezyhttp by youngmonkeys.

the class ThymeleafViewContextTest method test.

@Test
public void test() throws Exception {
    // given
    TemplateResolver resolver = TemplateResolver.builder().build();
    ViewContext viewContext = new ThymeleafViewContextBuilder().templateResolver(resolver).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);
}
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) View(com.tvd12.ezyhttp.server.core.view.View) PrintWriter(java.io.PrintWriter) 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