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;
}
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);
}
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);
}
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);
}
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();
}
Aggregations