use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.
the class InvalidUrlTest method initUI.
private static void initUI(UI ui, String initialLocation, ArgumentCaptor<Integer> statusCodeCaptor) throws InvalidRouteConfigurationException, ServiceException {
VaadinServletRequest request = Mockito.mock(VaadinServletRequest.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);
VaadinService service = new MockVaadinServletService() {
@Override
public VaadinContext getContext() {
return new MockVaadinContext();
}
};
service.setCurrentInstances(request, response);
MockVaadinSession session = new AlwaysLockedVaadinSession(service);
DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
Mockito.when(config.isProductionMode()).thenReturn(false);
session.lock();
session.setConfiguration(config);
ui.getInternals().setSession(session);
RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(ui.getRouter().getRegistry());
routeConfiguration.update(() -> {
routeConfiguration.getHandledRegistry().clean();
Arrays.asList(UITest.RootNavigationTarget.class, UITest.FooBarNavigationTarget.class).forEach(routeConfiguration::setAnnotatedRoute);
});
ui.doInit(request, 0);
ui.getRouter().initializeUI(ui, BootstrapHandlerTest.requestToLocation(request));
session.unlock();
if (statusCodeCaptor != null) {
Mockito.verify(response).setStatus(statusCodeCaptor.capture());
}
}
use of com.vaadin.flow.function.DeploymentConfiguration 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;
}
use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.
the class ServerRpcHandlerTest method setup.
@Before
public void setup() {
request = Mockito.mock(VaadinRequest.class);
service = Mockito.mock(VaadinService.class);
session = Mockito.mock(VaadinSession.class);
ui = Mockito.mock(UI.class);
uiInternals = Mockito.mock(UIInternals.class);
dependencyList = Mockito.mock(DependencyList.class);
Mockito.when(request.getService()).thenReturn(service);
Mockito.when(session.getService()).thenReturn(service);
Mockito.when(ui.getInternals()).thenReturn(uiInternals);
Mockito.when(ui.getSession()).thenReturn(session);
Mockito.when(ui.getCsrfToken()).thenReturn(csrfToken);
DeploymentConfiguration deploymentConfiguration = Mockito.mock(DeploymentConfiguration.class);
Mockito.when(service.getDeploymentConfiguration()).thenReturn(deploymentConfiguration);
uiTree = new StateTree(uiInternals);
Mockito.when(uiInternals.getStateTree()).thenReturn(uiTree);
Mockito.when(uiInternals.getDependencyList()).thenReturn(dependencyList);
serverRpcHandler = new ServerRpcHandler();
}
use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.
the class ApplicationRunnerServlet method findDeploymentConfiguration.
private DeploymentConfiguration findDeploymentConfiguration(DeploymentConfiguration originalConfiguration) throws Exception {
// First level of cache
DeploymentConfiguration configuration = CurrentInstance.get(DeploymentConfiguration.class);
if (configuration == null) {
// Not in cache, try to find a VaadinSession to get it from
VaadinSession session = VaadinSession.getCurrent();
if (session == null) {
/*
* There's no current session, request or response when serving
* static resources, but there's still the current request
* maintained by ApplicationRunnerServlet, and there's most
* likely also a HttpSession containing a VaadinSession for that
* request.
*/
HttpServletRequest currentRequest = VaadinServletService.getCurrentServletRequest();
if (currentRequest != null) {
HttpSession httpSession = currentRequest.getSession(false);
if (httpSession != null) {
Map<Class<?>, CurrentInstance> oldCurrent = CurrentInstance.setCurrent((VaadinSession) null);
try {
VaadinServletService service = (VaadinServletService) VaadinService.getCurrent();
session = service.findVaadinSession(new VaadinServletRequest(currentRequest, service));
} finally {
/*
* Clear some state set by findVaadinSession to
* avoid accidentally depending on it when coding on
* e.g. static request handling.
*/
CurrentInstance.restoreInstances(oldCurrent);
currentRequest.removeAttribute(VaadinSession.class.getName());
}
}
}
}
if (session != null) {
String name = ApplicationRunnerServlet.class.getName() + ".deploymentConfiguration";
try {
session.getLockInstance().lock();
/*
* Read attribute using reflection to bypass
* VaadinSesison.getAttribute which would cause an infinite
* loop when checking the production mode setting for
* determining whether to check that the session is locked.
*/
Field attributesField = VaadinSession.class.getDeclaredField("attributes");
attributesField.setAccessible(true);
Attributes sessionAttributes = (Attributes) attributesField.get(session);
configuration = (DeploymentConfiguration) sessionAttributes.getAttribute(name);
if (configuration == null) {
ApplicationRunnerServlet servlet = (ApplicationRunnerServlet) VaadinServlet.getCurrent();
Class<?> classToRun;
try {
classToRun = servlet.getClassToRun();
} catch (ClassNotFoundException e) {
/*
* This happens e.g. if the UI class defined in the
* URL is not found or if this servlet just serves
* static resources while there's some other servlet
* that serves the UI (e.g. when using /run-push/).
*/
return originalConfiguration;
}
CustomDeploymentConfiguration customDeploymentConfiguration = classToRun.getAnnotation(CustomDeploymentConfiguration.class);
if (customDeploymentConfiguration != null) {
Properties initParameters = new Properties(originalConfiguration.getInitParameters());
for (Conf entry : customDeploymentConfiguration.value()) {
initParameters.put(entry.name(), entry.value());
}
initParameters.put(VaadinSession.UI_PARAMETER, getApplicationRunnerApplicationClassName(request.get()));
configuration = new DefaultDeploymentConfiguration(ApplicationConfiguration.get(getService().getContext()), servlet.getClass(), initParameters);
} else {
configuration = originalConfiguration;
}
sessionAttributes.setAttribute(name, configuration);
}
} finally {
session.getLockInstance().unlock();
}
CurrentInstance.set(DeploymentConfiguration.class, configuration);
} else {
configuration = originalConfiguration;
}
}
return configuration;
}
use of com.vaadin.flow.function.DeploymentConfiguration in project flow by vaadin.
the class BrowserLiveReloadAccessorImplTest method getLiveReload_liveReloadDisabled_instanceIsCreated.
@Test
public void getLiveReload_liveReloadDisabled_instanceIsCreated() {
VaadinService service = Mockito.mock(VaadinService.class);
DeploymentConfiguration config = Mockito.mock(DeploymentConfiguration.class);
Mockito.when(service.getDeploymentConfiguration()).thenReturn(config);
Mockito.when(config.isProductionMode()).thenReturn(false);
Mockito.when(config.isDevModeLiveReloadEnabled()).thenReturn(false);
VaadinContext context = Mockito.mock(VaadinContext.class);
Mockito.when(context.getAttribute(Mockito.eq(BrowserLiveReload.class), Mockito.any())).thenReturn(Mockito.mock(BrowserLiveReload.class));
Mockito.when(service.getContext()).thenReturn(context);
Assert.assertNotNull(access.getLiveReload(service));
}
Aggregations