Search in sources :

Example 41 with Router

use of com.vaadin.flow.router.legacy.Router in project flow by vaadin.

the class RouterTest method testResolverBeforeSetRoute.

@Test
public void testResolverBeforeSetRoute() {
    Router router = new Router();
    AtomicReference<String> usedHandler = new AtomicReference<>();
    router.reconfigure(configuration -> {
        configuration.setResolver(resolveEvent -> Optional.of(handlerEvent -> {
            usedHandler.set("resolver");
            return HttpServletResponse.SC_OK;
        }));
        configuration.setRoute("*", e -> {
            usedHandler.set("route");
            return HttpServletResponse.SC_OK;
        });
    });
    router.navigate(new RouterTestUI(), new Location(""), NavigationTrigger.PROGRAMMATIC);
    Assert.assertEquals("resolver", usedHandler.get());
}
Also used : Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) ErrorView(com.vaadin.flow.router.legacy.ViewRendererTest.ErrorView) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentCaptor(org.mockito.ArgumentCaptor) After(org.junit.After) Location(com.vaadin.flow.router.Location) UI(com.vaadin.flow.component.UI) TestView(com.vaadin.flow.router.legacy.ViewRendererTest.TestView) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) ServletConfig(javax.servlet.ServletConfig) NavigationHandler(com.vaadin.flow.router.NavigationHandler) VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinServlet(com.vaadin.flow.server.VaadinServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) RouterInterface(com.vaadin.flow.router.RouterInterface) Resolver(com.vaadin.flow.router.legacy.Resolver) ImmutableRouterConfiguration(com.vaadin.flow.router.legacy.ImmutableRouterConfiguration) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) NotThreadSafe(net.jcip.annotations.NotThreadSafe) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinService(com.vaadin.flow.server.VaadinService) Optional(java.util.Optional) Assert(org.junit.Assert) NavigationEvent(com.vaadin.flow.router.NavigationEvent) RouterConfiguration(com.vaadin.flow.router.legacy.RouterConfiguration) Router(com.vaadin.flow.router.legacy.Router) Router(com.vaadin.flow.router.legacy.Router) AtomicReference(java.util.concurrent.atomic.AtomicReference) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 42 with Router

use of com.vaadin.flow.router.legacy.Router in project flow by vaadin.

the class RouterTest method testNavigateToEmptyLocation_triggersErrorView.

@Test
public void testNavigateToEmptyLocation_triggersErrorView() {
    UI ui = new RouterTestUI();
    Router router = new Router();
    router.reconfigure(c -> {
        c.setErrorView(ErrorView.class);
    });
    router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC);
    Assert.assertEquals(new ErrorView().getText(), ui.getElement().getTextRecursively());
}
Also used : UI(com.vaadin.flow.component.UI) Router(com.vaadin.flow.router.legacy.Router) ErrorView(com.vaadin.flow.router.legacy.ViewRendererTest.ErrorView) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 43 with Router

use of com.vaadin.flow.router.legacy.Router in project flow by vaadin.

the class RouterTest method testResolveError.

@Test
public void testResolveError() throws ServletException {
    UI ui = new RouterTestUI();
    VaadinRequest request = Mockito.mock(VaadinRequest.class);
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    ServletConfig servletConfig = new MockServletConfig();
    VaadinServlet servlet = new VaadinServlet();
    servlet.init(servletConfig);
    VaadinService service = servlet.getService();
    service.setCurrentInstances(request, response);
    Router router = new Router();
    router.reconfigure(c -> c.setResolver(event -> Optional.empty()));
    router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC);
    Assert.assertTrue(ui.getElement().getTextRecursively().contains("404"));
    // 404 code should be sent ONLY on initial request
    Mockito.verifyZeroInteractions(response);
    // to verify that the setup has been correct and the mocks work,
    // test the case where 404 should be sent
    router.initializeUI(ui, request);
    ArgumentCaptor<Integer> statusCodeCaptor = ArgumentCaptor.forClass(Integer.class);
    Mockito.verify(response).setStatus(statusCodeCaptor.capture());
    Assert.assertEquals(Integer.valueOf(HttpServletResponse.SC_NOT_FOUND), statusCodeCaptor.getValue());
}
Also used : Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) ErrorView(com.vaadin.flow.router.legacy.ViewRendererTest.ErrorView) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentCaptor(org.mockito.ArgumentCaptor) After(org.junit.After) Location(com.vaadin.flow.router.Location) UI(com.vaadin.flow.component.UI) TestView(com.vaadin.flow.router.legacy.ViewRendererTest.TestView) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) ServletConfig(javax.servlet.ServletConfig) NavigationHandler(com.vaadin.flow.router.NavigationHandler) VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinServlet(com.vaadin.flow.server.VaadinServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) RouterInterface(com.vaadin.flow.router.RouterInterface) Resolver(com.vaadin.flow.router.legacy.Resolver) ImmutableRouterConfiguration(com.vaadin.flow.router.legacy.ImmutableRouterConfiguration) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) NotThreadSafe(net.jcip.annotations.NotThreadSafe) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinService(com.vaadin.flow.server.VaadinService) Optional(java.util.Optional) Assert(org.junit.Assert) NavigationEvent(com.vaadin.flow.router.NavigationEvent) RouterConfiguration(com.vaadin.flow.router.legacy.RouterConfiguration) Router(com.vaadin.flow.router.legacy.Router) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinServlet(com.vaadin.flow.server.VaadinServlet) MockServletConfig(com.vaadin.flow.server.MockServletConfig) Router(com.vaadin.flow.router.legacy.Router) VaadinResponse(com.vaadin.flow.server.VaadinResponse) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 44 with Router

use of com.vaadin.flow.router.legacy.Router in project flow by vaadin.

the class RouterTest method testStatusCodeUpdates.

@Test
public void testStatusCodeUpdates() {
    RouterTestUI ui = new RouterTestUI();
    Router router = (Router) ui.getRouterInterface().get();
    router.reconfigure(c -> {
        c.setRoute("*", e -> 123);
    });
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    try {
        CurrentInstance.set(VaadinResponse.class, response);
        VaadinRequest request = requestWithPathInfo(null);
        router.initializeUI(ui, request);
        // Response status should be set when initializing
        Mockito.verify(response).setStatus(123);
        router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC);
        // Non-init navigation shouldn't set any status code
        Mockito.verifyNoMoreInteractions(response);
    } finally {
        CurrentInstance.clearAll();
    }
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) Router(com.vaadin.flow.router.legacy.Router) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Aggregations

Router (com.vaadin.flow.router.legacy.Router)44 Test (org.junit.Test)44 Location (com.vaadin.flow.router.Location)17 TestView (com.vaadin.flow.router.legacy.ViewRendererTest.TestView)16 UI (com.vaadin.flow.component.UI)15 NavigationEvent (com.vaadin.flow.router.NavigationEvent)8 DefaultErrorView (com.vaadin.flow.router.legacy.DefaultErrorView)7 VaadinRequest (com.vaadin.flow.server.VaadinRequest)7 HistoryStateChangeEvent (com.vaadin.flow.component.page.History.HistoryStateChangeEvent)6 ImmutableRouterConfiguration (com.vaadin.flow.router.legacy.ImmutableRouterConfiguration)6 RouterConfiguration (com.vaadin.flow.router.legacy.RouterConfiguration)6 ErrorView (com.vaadin.flow.router.legacy.ViewRendererTest.ErrorView)6 VaadinResponse (com.vaadin.flow.server.VaadinResponse)6 VaadinService (com.vaadin.flow.server.VaadinService)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)5 NavigationHandler (com.vaadin.flow.router.NavigationHandler)5 NavigationTrigger (com.vaadin.flow.router.NavigationTrigger)5 RouterInterface (com.vaadin.flow.router.RouterInterface)5 Resolver (com.vaadin.flow.router.legacy.Resolver)5