Search in sources :

Example 1 with ViewContext

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

the class ApplicationContextBuilder method buildViewContext.

protected ViewContext buildViewContext(EzyBeanContext beanContext) {
    ViewContext viewContext = beanContext.getSingleton(ViewContext.class);
    if (viewContext == null) {
        ViewContextBuilder viewContextBuilder = beanContext.getSingleton(ViewContextBuilder.class);
        if (viewContextBuilder != null) {
            TemplateResolver templateResolver = beanContext.getSingleton(TemplateResolver.class);
            if (templateResolver == null) {
                templateResolver = TemplateResolver.of(beanContext);
            }
            viewContext = viewContextBuilder.templateResolver(templateResolver).viewDialects(beanContext.getSingletonsOf(ViewDialect.class)).viewDecorators(beanContext.getSingletonsOf(ViewDecorator.class)).messageProviders(beanContext.getSingletonsOf(MessageProvider.class)).absentMessageResolver(beanContext.getSingleton(AbsentMessageResolver.class)).build();
        }
    }
    if (viewContext != null) {
        beanContext.getSingletonFactory().addSingleton(viewContext);
    }
    return viewContext;
}
Also used : TemplateResolver(com.tvd12.ezyhttp.server.core.view.TemplateResolver) AbsentMessageResolver(com.tvd12.ezyhttp.server.core.view.AbsentMessageResolver) ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) ViewContextBuilder(com.tvd12.ezyhttp.server.core.view.ViewContextBuilder) ViewDecorator(com.tvd12.ezyhttp.server.core.view.ViewDecorator)

Example 2 with ViewContext

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

the class ApplicationContextBuilderTest method buildViewContextViewContextBuilderIsNull.

@Test
public void buildViewContextViewContextBuilderIsNull() {
    // given
    EzyBeanContext beanContext = mock(EzyBeanContext.class);
    ApplicationContextBuilder sut = new ApplicationContextBuilder();
    // when
    ViewContext actual = MethodInvoker.create().object(sut).method("buildViewContext").param(EzyBeanContext.class, beanContext).invoke(ViewContext.class);
    // then
    Asserts.assertNull(actual);
    verify(beanContext, times(1)).getSingleton(ViewContext.class);
    verify(beanContext, times(1)).getSingleton(ViewContextBuilder.class);
}
Also used : ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) ApplicationContextBuilder(com.tvd12.ezyhttp.server.core.ApplicationContextBuilder) Test(org.testng.annotations.Test)

Example 3 with ViewContext

use of com.tvd12.ezyhttp.server.core.view.ViewContext 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)

Example 4 with ViewContext

use of com.tvd12.ezyhttp.server.core.view.ViewContext 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 5 with ViewContext

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

the class BlockingServletTest method doDeleteTest.

@Test
public void doDeleteTest() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    ViewContext viewContext = mock(ViewContext.class);
    componentManager.setViewContext(viewContext);
    ServletConfig servletConfig = mock(ServletConfig.class);
    BlockingServlet sut = new BlockingServlet();
    sut.init(servletConfig);
    sut.init();
    String requestURI = "/delete";
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn(HttpMethod.DELETE.toString());
    when(request.getRequestURI()).thenReturn(requestURI);
    when(request.getServerPort()).thenReturn(PORT);
    when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
    when(request.getParameter("param")).thenReturn("ParameterValue");
    when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
    when(request.getHeader("header")).thenReturn("HeaderValue");
    when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
    RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
    DeleteRequestHandler requestHandler = new DeleteRequestHandler();
    requestHandlerManager.addHandler(new RequestURI(HttpMethod.DELETE, requestURI, false), requestHandler);
    RequestInterceptor interceptor = mock(RequestInterceptor.class);
    when(interceptor.preHandle(any(), any())).thenReturn(true);
    componentManager.getInterceptorManager().addRequestInterceptors(Collections.singletonList(interceptor));
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    // when
    sut.service(request, response);
    // then
    verify(request, times(1)).getMethod();
    verify(request, times(1)).getRequestURI();
    verify(interceptor, times(1)).preHandle(any(), any());
    verify(interceptor, times(1)).postHandle(any(), any());
    verify(viewContext, times(1)).render(any(), any(), any(), any());
    componentManager.destroy();
}
Also used : Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) ViewContext(com.tvd12.ezyhttp.server.core.view.ViewContext) ServletOutputStream(javax.servlet.ServletOutputStream) ServletConfig(javax.servlet.ServletConfig) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) ToString(lombok.ToString) RequestInterceptor(com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) Test(org.testng.annotations.Test)

Aggregations

ViewContext (com.tvd12.ezyhttp.server.core.view.ViewContext)10 Test (org.testng.annotations.Test)9 TemplateResolver (com.tvd12.ezyhttp.server.core.view.TemplateResolver)7 View (com.tvd12.ezyhttp.server.core.view.View)5 ThymeleafViewContextBuilder (com.tvd12.ezyhttp.server.thymeleaf.ThymeleafViewContextBuilder)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 PrintWriter (java.io.PrintWriter)4 ServletContext (javax.servlet.ServletContext)4 EzyBeanContext (com.tvd12.ezyfox.bean.EzyBeanContext)3 ApplicationContextBuilder (com.tvd12.ezyhttp.server.core.ApplicationContextBuilder)3 ViewContextBuilder (com.tvd12.ezyhttp.server.core.view.ViewContextBuilder)3 ViewDecorator (com.tvd12.ezyhttp.server.core.view.ViewDecorator)3 EzySingletonFactory (com.tvd12.ezyfox.bean.EzySingletonFactory)2 AbsentMessageResolver (com.tvd12.ezyhttp.server.core.view.AbsentMessageResolver)2 ViewDialect (com.tvd12.ezyhttp.server.core.view.ViewDialect)2 Cookie (javax.servlet.http.Cookie)2 MultiValueMap (com.tvd12.ezyhttp.core.data.MultiValueMap)1 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)1 RequestCookie (com.tvd12.ezyhttp.server.core.annotation.RequestCookie)1