Search in sources :

Example 6 with RouteRegistry

use of com.vaadin.flow.server.RouteRegistry in project flow by vaadin.

the class RouteConfigurationTest method registeredRouteWithAlias_allPathsAreRegistered.

@Test
public void registeredRouteWithAlias_allPathsAreRegistered() {
    RouteRegistry registry = mockRegistry();
    RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(registry);
    routeConfiguration.setAnnotatedRoute(MyRouteWithAliases.class);
    Mockito.verify(registry).setRoute("withAliases", MyRouteWithAliases.class, Collections.emptyList());
    Mockito.verify(registry).setRoute("version", MyRouteWithAliases.class, Collections.emptyList());
    Mockito.verify(registry).setRoute("person", MyRouteWithAliases.class, Collections.emptyList());
}
Also used : ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) RouteRegistry(com.vaadin.flow.server.RouteRegistry) SessionRouteRegistry(com.vaadin.flow.server.SessionRouteRegistry) Test(org.junit.Test)

Example 7 with RouteRegistry

use of com.vaadin.flow.server.RouteRegistry in project flow by vaadin.

the class RouteConfigurationTest method routeWithParent_parentsAreCollectedCorrectly.

@Test
public void routeWithParent_parentsAreCollectedCorrectly() {
    RouteRegistry registry = mockRegistry();
    RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(registry);
    routeConfiguration.setAnnotatedRoute(SingleLayout.class);
    Mockito.verify(registry).setRoute("single", SingleLayout.class, Collections.singletonList(MainLayout.class));
    routeConfiguration.setAnnotatedRoute(DoubleLayout.class);
    Mockito.verify(registry).setRoute("double", DoubleLayout.class, Arrays.asList(MiddleLayout.class, MainLayout.class));
}
Also used : ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) RouteRegistry(com.vaadin.flow.server.RouteRegistry) SessionRouteRegistry(com.vaadin.flow.server.SessionRouteRegistry) Test(org.junit.Test)

Example 8 with RouteRegistry

use of com.vaadin.flow.server.RouteRegistry in project flow by vaadin.

the class RouteConfigurationTest method setRoutes_allExpectedRoutesAreSet.

@Test
public void setRoutes_allExpectedRoutesAreSet() {
    RouteRegistry registry = mockRegistry();
    RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(registry);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        ((Command) args[0]).execute();
        return null;
    }).when(registry).update(Mockito.any(Command.class));
    routeConfiguration.update(() -> {
        routeConfiguration.getHandledRegistry().clean();
        Arrays.asList(MyRoute.class, MyInfo.class, MyPalace.class, MyModular.class).forEach(routeConfiguration::setAnnotatedRoute);
    });
    Mockito.verify(registry).update(Mockito.any());
    Mockito.verify(registry).setRoute("home", MyRoute.class, Collections.emptyList());
    Mockito.verify(registry).setRoute("info", MyInfo.class, Collections.emptyList());
    Mockito.verify(registry).setRoute("palace", MyPalace.class, Collections.emptyList());
    Mockito.verify(registry).setRoute("modular", MyModular.class, Collections.emptyList());
}
Also used : ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) RouteRegistry(com.vaadin.flow.server.RouteRegistry) SessionRouteRegistry(com.vaadin.flow.server.SessionRouteRegistry) Command(com.vaadin.flow.server.Command) Test(org.junit.Test)

Example 9 with RouteRegistry

use of com.vaadin.flow.server.RouteRegistry in project flow by vaadin.

the class RouteConfigurationTest method parentLayoutAnnotatedClass_parentsCorrecltCollected.

@Test
public void parentLayoutAnnotatedClass_parentsCorrecltCollected() {
    RouteRegistry registry = Mockito.mock(RouteRegistry.class);
    RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(registry);
    routeConfiguration.setParentAnnotatedRoute("middle", MiddleLayout.class);
    Mockito.verify(registry).setRoute("middle", MiddleLayout.class, Collections.singletonList(MainLayout.class));
}
Also used : ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) RouteRegistry(com.vaadin.flow.server.RouteRegistry) SessionRouteRegistry(com.vaadin.flow.server.SessionRouteRegistry) Test(org.junit.Test)

Example 10 with RouteRegistry

use of com.vaadin.flow.server.RouteRegistry in project flow by vaadin.

the class ViewAccessCheckerTest method setupRequest.

private Result setupRequest(Class navigationTarget, User user, boolean productionMode) {
    CurrentInstance.clearAll();
    Principal principal;
    String[] roles;
    if (user == User.USER_NO_ROLES) {
        principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
        roles = new String[0];
    } else if (user == User.NORMAL_USER) {
        principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
        roles = new String[] { "user" };
    } else if (user == User.ADMIN) {
        principal = AccessAnnotationCheckerTest.USER_PRINCIPAL;
        roles = new String[] { "admin" };
    } else {
        principal = null;
        roles = new String[0];
    }
    VaadinServletRequest vaadinServletRequest = Mockito.mock(VaadinServletRequest.class);
    HttpServletRequest httpServletRequest = AccessAnnotationCheckerTest.createRequest(principal, roles);
    Mockito.when(vaadinServletRequest.getHttpServletRequest()).thenReturn(httpServletRequest);
    CurrentInstance.set(VaadinRequest.class, vaadinServletRequest);
    Router router = Mockito.mock(Router.class);
    UI ui = Mockito.mock(UI.class);
    Page page = Mockito.mock(Page.class);
    Mockito.when(ui.getPage()).thenReturn(page);
    VaadinSession vaadinSession = Mockito.mock(VaadinSession.class);
    Mockito.when(ui.getSession()).thenReturn(vaadinSession);
    DeploymentConfiguration configuration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(vaadinSession.getConfiguration()).thenReturn(configuration);
    Mockito.when(configuration.isProductionMode()).thenReturn(productionMode);
    UIInternals uiInternals = Mockito.mock(UIInternals.class);
    Mockito.when(ui.getInternals()).thenReturn(uiInternals);
    Mockito.when(uiInternals.getRouter()).thenReturn(router);
    Mockito.when(router.getErrorNavigationTarget(Mockito.any())).thenAnswer(invocation -> {
        Class<?> exceptionClass = invocation.getArguments()[0].getClass();
        if (exceptionClass == NotFoundException.class) {
            return Optional.of(new ErrorTargetEntry(RouteNotFoundError.class, NotFoundException.class));
        } else {
            return Optional.empty();
        }
    });
    Location location = new Location(getRoute(navigationTarget));
    NavigationEvent navigationEvent = new NavigationEvent(router, location, ui, NavigationTrigger.ROUTER_LINK);
    BeforeEnterEvent event = new BeforeEnterEvent(navigationEvent, navigationTarget, new ArrayList<>());
    RouteRegistry routeRegistry = Mockito.mock(RouteRegistry.class);
    Mockito.when(router.getRegistry()).thenReturn(routeRegistry);
    Mockito.when(routeRegistry.getNavigationTarget(Mockito.anyString())).thenAnswer(invocation -> {
        String url = (String) invocation.getArguments()[0];
        if (location.getPath().equals(url)) {
            return Optional.of(navigationTarget);
        } else {
            return Optional.empty();
        }
    });
    HttpSession session = Mockito.mock(HttpSession.class);
    Map<String, Object> sessionAttributes = new HashMap<>();
    Mockito.when(httpServletRequest.getSession()).thenReturn(session);
    Mockito.doAnswer(invocation -> {
        String key = (String) invocation.getArguments()[0];
        Object value = invocation.getArguments()[1];
        sessionAttributes.put(key, value);
        return null;
    }).when(session).setAttribute(Mockito.anyString(), Mockito.any());
    Result info = new Result();
    info.event = event;
    info.sessionAttributes = sessionAttributes;
    Mockito.doAnswer(invocation -> {
        info.redirectUsingPageLocation = (String) invocation.getArguments()[0];
        return null;
    }).when(page).setLocation(Mockito.anyString());
    return info;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) RouteRegistry(com.vaadin.flow.server.RouteRegistry) HashMap(java.util.HashMap) NotFoundException(com.vaadin.flow.router.NotFoundException) Page(com.vaadin.flow.component.page.Page) HttpServletRequest(javax.servlet.http.HttpServletRequest) UI(com.vaadin.flow.component.UI) NavigationEvent(com.vaadin.flow.router.NavigationEvent) HttpSession(javax.servlet.http.HttpSession) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Router(com.vaadin.flow.router.Router) UIInternals(com.vaadin.flow.component.internal.UIInternals) BeforeEnterEvent(com.vaadin.flow.router.BeforeEnterEvent) ErrorTargetEntry(com.vaadin.flow.router.internal.ErrorTargetEntry) RouteNotFoundError(com.vaadin.flow.router.RouteNotFoundError) Principal(java.security.Principal) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Location(com.vaadin.flow.router.Location)

Aggregations

RouteRegistry (com.vaadin.flow.server.RouteRegistry)13 ApplicationRouteRegistry (com.vaadin.flow.server.startup.ApplicationRouteRegistry)6 SessionRouteRegistry (com.vaadin.flow.server.SessionRouteRegistry)5 Test (org.junit.Test)5 Router (com.vaadin.flow.router.Router)4 UI (com.vaadin.flow.component.UI)3 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 NotFoundException (com.vaadin.flow.router.NotFoundException)2 Route (com.vaadin.flow.router.Route)2 NavigationRouteTarget (com.vaadin.flow.router.internal.NavigationRouteTarget)2 RouteTarget (com.vaadin.flow.router.internal.RouteTarget)2 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)2 VaadinContext (com.vaadin.flow.server.VaadinContext)2 VaadinSession (com.vaadin.flow.server.VaadinSession)2 SpringServlet (com.vaadin.flow.spring.SpringServlet)2 Component (com.vaadin.flow.component.Component)1 UIInternals (com.vaadin.flow.component.internal.UIInternals)1 Page (com.vaadin.flow.component.page.Page)1 Lookup (com.vaadin.flow.di.Lookup)1 AnnotationReader (com.vaadin.flow.internal.AnnotationReader)1