use of com.vaadin.flow.server.MockVaadinSession in project flow by vaadin.
the class UidlWriterTest method initializeUIForDependenciesTest.
private UI initializeUIForDependenciesTest(UI ui) {
ServletContext context = Mockito.mock(ServletContext.class);
VaadinServletService service = new VaadinServletService(new VaadinServlet() {
@Override
public ServletContext getServletContext() {
return context;
}
}, new MockDeploymentConfiguration()) {
RouterInterface router = new com.vaadin.flow.router.legacy.Router();
@Override
public RouterInterface getRouter() {
return router;
}
@Override
public Iterable<DependencyFilter> getDependencyFilters() {
return Collections.emptyList();
}
};
service.getRouter().reconfigure(conf -> {
conf.setRoute("", BaseClass.class);
conf.setParentView(BaseClass.class, ParentClass.class);
conf.setParentView(ParentClass.class, SuperParentClass.class);
});
MockVaadinSession session = new MockVaadinSession(service);
session.lock();
ui.getInternals().setSession(session);
when(service.getResourceAsStream(anyString())).thenAnswer(invocation -> new ByteArrayInputStream(((String) invocation.getArguments()[0]).getBytes()));
HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
VaadinServletRequest vaadinRequestMock = mock(VaadinServletRequest.class);
when(vaadinRequestMock.getHttpServletRequest()).thenReturn(servletRequestMock);
service.setCurrentInstances(vaadinRequestMock, mock(VaadinResponse.class));
ui.doInit(vaadinRequestMock, 1);
factory = mock(VaadinUriResolverFactory.class);
VaadinUriResolver vaadinUriResolver = Mockito.mock(VaadinUriResolver.class);
Mockito.when(factory.getUriResolver(any())).thenReturn(vaadinUriResolver);
Mockito.when(vaadinUriResolver.resolveVaadinUri(anyString())).thenAnswer(invocation -> invocation.getArguments()[0]);
doAnswer(invocation -> invocation.getArguments()[1]).when(factory).toServletContextPath(any(), anyString());
session.setAttribute(VaadinUriResolverFactory.class, factory);
VaadinSession.setCurrent(session);
return ui;
}
use of com.vaadin.flow.server.MockVaadinSession in project flow by vaadin.
the class NavigationStateRendererTest method instantiatorUse.
@Test
public void instantiatorUse() throws ServiceException {
MockVaadinServletService service = new MockVaadinServletService();
service.init(new MockInstantiator() {
@Override
public <T extends HasElement> T createRouteTarget(Class<T> routeTargetType, NavigationEvent event) {
Assert.assertEquals(Component.class, routeTargetType);
return (T) new Text("foo");
}
});
MockUI ui = new MockUI(new MockVaadinSession(service));
NavigationEvent event = new NavigationEvent(new Router(new TestRouteRegistry()), new Location(""), ui, NavigationTrigger.PAGE_LOAD);
NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(ChildConfiguration.class));
Component routeTarget = renderer.getRouteTarget(Component.class, event);
Assert.assertEquals(Text.class, routeTarget.getClass());
UI.setCurrent(null);
}
use of com.vaadin.flow.server.MockVaadinSession in project flow by vaadin.
the class UITest method initUI.
private static void initUI(UI ui, String initialLocation, ArgumentCaptor<Integer> statusCodeCaptor) {
try {
VaadinRequest request = Mockito.mock(VaadinRequest.class);
VaadinResponse response = Mockito.mock(VaadinResponse.class);
String pathInfo;
if (initialLocation.isEmpty()) {
pathInfo = null;
} else {
Assert.assertFalse(initialLocation.startsWith("/"));
pathInfo = "/" + initialLocation;
}
Mockito.when(request.getPathInfo()).thenReturn(pathInfo);
Properties initParams = new Properties();
initParams.setProperty(Constants.SERVLET_PARAMETER_USING_NEW_ROUTING, "false");
ServletConfig servletConfig = new MockServletConfig(initParams);
VaadinServlet servlet = new VaadinServlet();
servlet.init(servletConfig);
VaadinService service = servlet.getService();
service.setCurrentInstances(request, response);
service.getRouter().reconfigure(c -> {
});
MockVaadinSession session = new MockVaadinSession(service);
ui.getInternals().setSession(session);
ui.doInit(request, 0);
if (statusCodeCaptor != null) {
Mockito.verify(response).setStatus(statusCodeCaptor.capture());
}
} catch (ServletException e) {
throw new RuntimeException(e);
}
}
Aggregations