Search in sources :

Example 1 with MockDeploymentConfiguration

use of com.vaadin.tests.util.MockDeploymentConfiguration in project flow by vaadin.

the class BootstrapHandlerDependenciesTest method setup.

@Before
public void setup() {
    BootstrapHandler.clientEngineFile = "foobar";
    DeploymentConfiguration deploymentConfiguration = new MockDeploymentConfiguration();
    service = Mockito.spy(new MockVaadinServletService(deploymentConfiguration));
    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));
    Mockito.when(service.getDependencyFilters()).thenReturn(Collections.emptyList());
    session = new MockVaadinSession(service);
    session.lock();
    session.setConfiguration(deploymentConfiguration);
    VaadinUriResolverFactory factory = Mockito.mock(VaadinUriResolverFactory.class);
    VaadinUriResolver vaadinUriResolver = Mockito.mock(VaadinUriResolver.class);
    Mockito.when(factory.getUriResolver(Mockito.any())).thenReturn(vaadinUriResolver);
    Mockito.when(vaadinUriResolver.resolveVaadinUri(Mockito.anyString())).thenAnswer(i -> i.getArguments()[0]);
    Mockito.doAnswer(invocation -> invocation.getArguments()[1]).when(factory).toServletContextPath(Mockito.any(), Mockito.anyString());
    session.setAttribute(VaadinUriResolverFactory.class, factory);
    VaadinSession.setCurrent(session);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Matchers.anyString(org.mockito.Matchers.anyString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Before(org.junit.Before)

Example 2 with MockDeploymentConfiguration

use of com.vaadin.tests.util.MockDeploymentConfiguration in project flow by vaadin.

the class BootstrapHandlerTest method setup.

@Before
public void setup() {
    TestRouteRegistry routeRegistry = new TestRouteRegistry();
    BootstrapHandler.clientEngineFile = "foobar";
    testUI = new TestUI();
    deploymentConfiguration = new MockDeploymentConfiguration("test/");
    service = Mockito.spy(new MockVaadinServletService(deploymentConfiguration));
    Mockito.when(service.getRouteRegistry()).thenReturn(routeRegistry);
    Mockito.when(service.getRouter()).thenReturn(new Router(routeRegistry) {

        @Override
        public void initializeUI(UI ui, VaadinRequest initRequest) {
            // injected
            if (routeRegistry.hasNavigationTargets()) {
                super.initializeUI(ui, initRequest);
            }
        }
    });
    session = Mockito.spy(new MockVaadinSession(service));
    session.lock();
    session.setConfiguration(deploymentConfiguration);
    testUI.getInternals().setSession(session);
    browser = Mockito.mock(WebBrowser.class);
    Mockito.when(browser.isEs6Supported()).thenReturn(false);
    Mockito.when(session.getBrowser()).thenReturn(browser);
}
Also used : UI(com.vaadin.flow.component.UI) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Router(com.vaadin.flow.router.Router) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) Before(org.junit.Before)

Example 3 with MockDeploymentConfiguration

use of com.vaadin.tests.util.MockDeploymentConfiguration 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 4 with MockDeploymentConfiguration

use of com.vaadin.tests.util.MockDeploymentConfiguration in project flow by vaadin.

the class ThemeUrlResolverTest method init.

@Before
public void init() throws Exception {
    assert VaadinSession.getCurrent() == null : "Required no current vaadin session.";
    assert VaadinRequest.getCurrent() == null : "Required no current vaadin request.";
    MockitoAnnotations.initMocks(this);
    servlet = new VaadinServlet() {

        @Override
        protected DeploymentConfiguration createDeploymentConfiguration() throws ServletException {
            return mockDeploymentConfiguration;
        }
    };
    Properties initParameters = new Properties();
    Mockito.when(servletConfig.getServletContext()).thenReturn(servletContext);
    Mockito.when(servletConfig.getInitParameterNames()).thenReturn((Enumeration<String>) initParameters.propertyNames());
    Mockito.when(servletContext.getInitParameterNames()).thenReturn((Enumeration<String>) initParameters.propertyNames());
    Mockito.when(servletContext.getResource(Mockito.anyString())).thenAnswer(i -> new URL("http://localhost" + i.getArguments()[0]));
    servlet.init(servletConfig);
    Mockito.when(session.getAttribute(VaadinUriResolverFactory.class)).thenReturn(uriResolverFactory);
    Mockito.when(uriResolverFactory.getUriResolver(Mockito.any())).thenReturn(vaadinUriResolver);
    Mockito.when(vaadinUriResolver.resolveVaadinUri(Mockito.anyString())).thenAnswer(i -> i.getArguments()[0]);
    VaadinSession.setCurrent(session);
    CurrentInstance.set(VaadinRequest.class, request);
}
Also used : ServletException(javax.servlet.ServletException) VaadinServlet(com.vaadin.flow.server.VaadinServlet) Properties(java.util.Properties) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) URL(java.net.URL) Before(org.junit.Before)

Aggregations

MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)4 Before (org.junit.Before)3 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 VaadinServlet (com.vaadin.flow.server.VaadinServlet)2 VaadinUriResolver (com.vaadin.flow.shared.VaadinUriResolver)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Matchers.anyString (org.mockito.Matchers.anyString)2 UI (com.vaadin.flow.component.UI)1 Router (com.vaadin.flow.router.Router)1 RouterInterface (com.vaadin.flow.router.RouterInterface)1 TestRouteRegistry (com.vaadin.flow.router.TestRouteRegistry)1 DependencyFilter (com.vaadin.flow.server.DependencyFilter)1 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)1 VaadinResponse (com.vaadin.flow.server.VaadinResponse)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1 VaadinUriResolverFactory (com.vaadin.flow.server.VaadinUriResolverFactory)1 URL (java.net.URL)1 Properties (java.util.Properties)1