Search in sources :

Example 1 with MockVaadinSession

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;
}
Also used : MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinServletService(com.vaadin.flow.server.VaadinServletService) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Matchers.anyString(org.mockito.Matchers.anyString) VaadinUriResolverFactory(com.vaadin.flow.server.VaadinUriResolverFactory) HttpServletRequest(javax.servlet.http.HttpServletRequest) VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletContext(javax.servlet.ServletContext) RouterInterface(com.vaadin.flow.router.RouterInterface) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver)

Example 2 with MockVaadinSession

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);
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) Router(com.vaadin.flow.router.Router) Text(com.vaadin.flow.component.Text) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) MockInstantiator(com.vaadin.flow.server.MockInstantiator) Component(com.vaadin.flow.component.Component) Location(com.vaadin.flow.router.Location) NavigationStateRenderer(com.vaadin.flow.router.internal.NavigationStateRenderer) Test(org.junit.Test)

Example 3 with MockVaadinSession

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);
    }
}
Also used : ServletException(javax.servlet.ServletException) VaadinResponse(com.vaadin.flow.server.VaadinResponse) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinService(com.vaadin.flow.server.VaadinService) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Properties(java.util.Properties)

Aggregations

MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)3 VaadinResponse (com.vaadin.flow.server.VaadinResponse)2 VaadinServlet (com.vaadin.flow.server.VaadinServlet)2 Component (com.vaadin.flow.component.Component)1 Text (com.vaadin.flow.component.Text)1 Location (com.vaadin.flow.router.Location)1 NavigationEvent (com.vaadin.flow.router.NavigationEvent)1 Router (com.vaadin.flow.router.Router)1 RouterInterface (com.vaadin.flow.router.RouterInterface)1 TestRouteRegistry (com.vaadin.flow.router.TestRouteRegistry)1 NavigationStateRenderer (com.vaadin.flow.router.internal.NavigationStateRenderer)1 DependencyFilter (com.vaadin.flow.server.DependencyFilter)1 MockInstantiator (com.vaadin.flow.server.MockInstantiator)1 MockServletConfig (com.vaadin.flow.server.MockServletConfig)1 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)1 VaadinRequest (com.vaadin.flow.server.VaadinRequest)1 VaadinService (com.vaadin.flow.server.VaadinService)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1 VaadinUriResolverFactory (com.vaadin.flow.server.VaadinUriResolverFactory)1