Search in sources :

Example 1 with Json

use of elemental.json.Json in project flow by vaadin.

the class UidlWriterTest method checkAllTypesOfDependencies.

@Test
public void checkAllTypesOfDependencies() {
    UI ui = initializeUIForDependenciesTest(new TestUI());
    UidlWriter uidlWriter = new UidlWriter();
    addInitialComponentDependencies(ui, uidlWriter);
    ui.add(new ComponentWithAllDependencyTypes());
    JsonObject response = uidlWriter.createUidl(ui, false);
    Map<LoadMode, List<JsonObject>> dependenciesMap = Stream.of(LoadMode.values()).map(mode -> response.getArray(mode.name())).flatMap(JsonUtils::<JsonObject>stream).collect(Collectors.toMap(jsonObject -> LoadMode.valueOf(jsonObject.getString(Dependency.KEY_LOAD_MODE)), Collections::singletonList, (list1, list2) -> {
        List<JsonObject> result = new ArrayList<>(list1);
        result.addAll(list2);
        return result;
    }));
    assertThat("Dependencies with all types of load mode should be present in this response", dependenciesMap.size(), is(LoadMode.values().length));
    List<JsonObject> eagerDependencies = dependenciesMap.get(LoadMode.EAGER);
    assertThat("Should have 3 eager dependencies", eagerDependencies, hasSize(3));
    assertThat("Eager dependencies should not have inline contents", eagerDependencies.stream().filter(json -> json.hasKey(Dependency.KEY_CONTENTS)).collect(Collectors.toList()), hasSize(0));
    assertThat("Should have 3 different eager urls", eagerDependencies.stream().map(json -> json.getString(Dependency.KEY_URL)).map(url -> url.substring(ApplicationConstants.FRONTEND_PROTOCOL_PREFIX.length())).collect(Collectors.toList()), containsInAnyOrder("eager.js", "eager.html", "eager.css"));
    assertThat("Should have 3 different eager dependency types", eagerDependencies.stream().map(json -> json.getString(Dependency.KEY_TYPE)).map(Dependency.Type::valueOf).collect(Collectors.toList()), containsInAnyOrder(Dependency.Type.values()));
    List<JsonObject> lazyDependencies = dependenciesMap.get(LoadMode.LAZY);
    assertThat("Should have 3 lazy dependencies", lazyDependencies, hasSize(3));
    assertThat("Lazy dependencies should not have inline contents", lazyDependencies.stream().filter(json -> json.hasKey(Dependency.KEY_CONTENTS)).collect(Collectors.toList()), hasSize(0));
    assertThat("Should have 3 different lazy urls", lazyDependencies.stream().map(json -> json.getString(Dependency.KEY_URL)).map(url -> url.substring(ApplicationConstants.FRONTEND_PROTOCOL_PREFIX.length())).collect(Collectors.toList()), containsInAnyOrder("lazy.js", "lazy.html", "lazy.css"));
    assertThat("Should have 3 different lazy dependency types", lazyDependencies.stream().map(json -> json.getString(Dependency.KEY_TYPE)).map(Dependency.Type::valueOf).collect(Collectors.toList()), containsInAnyOrder(Dependency.Type.values()));
    List<JsonObject> inlineDependencies = dependenciesMap.get(LoadMode.INLINE);
    assertInlineDependencies(inlineDependencies, ApplicationConstants.FRONTEND_PROTOCOL_PREFIX);
}
Also used : Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) JavaScript(com.vaadin.flow.component.dependency.JavaScript) Json(elemental.json.Json) JsonArray(elemental.json.JsonArray) Assert.assertThat(org.junit.Assert.assertThat) JsonValue(elemental.json.JsonValue) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) After(org.junit.After) Element(com.vaadin.flow.dom.Element) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Is.is(org.hamcrest.core.Is.is) UI(com.vaadin.flow.component.UI) VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinServlet(com.vaadin.flow.server.VaadinServlet) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) RouterInterface(com.vaadin.flow.router.RouterInterface) Collectors(java.util.stream.Collectors) NotThreadSafe(net.jcip.annotations.NotThreadSafe) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Stream(java.util.stream.Stream) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assert.assertFalse(org.junit.Assert.assertFalse) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) Mockito.mock(org.mockito.Mockito.mock) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinUriResolverFactory(com.vaadin.flow.server.VaadinUriResolverFactory) HasChildView(com.vaadin.flow.router.legacy.HasChildView) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) Function(java.util.function.Function) Matchers.anyString(org.mockito.Matchers.anyString) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) Tag(com.vaadin.flow.component.Tag) View(com.vaadin.flow.router.legacy.View) DependencyFilter(com.vaadin.flow.server.DependencyFilter) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ElementFactory(com.vaadin.flow.dom.ElementFactory) VaadinResponse(com.vaadin.flow.server.VaadinResponse) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Mockito(org.mockito.Mockito) JsonUtils(com.vaadin.flow.internal.JsonUtils) ServletContext(javax.servlet.ServletContext) JsonObject(elemental.json.JsonObject) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) LoadMode(com.vaadin.flow.shared.ui.LoadMode) UI(com.vaadin.flow.component.UI) JsonObject(elemental.json.JsonObject) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Component (com.vaadin.flow.component.Component)1 Tag (com.vaadin.flow.component.Tag)1 UI (com.vaadin.flow.component.UI)1 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)1 JavaScript (com.vaadin.flow.component.dependency.JavaScript)1 StyleSheet (com.vaadin.flow.component.dependency.StyleSheet)1 JavaScriptInvocation (com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)1 Element (com.vaadin.flow.dom.Element)1 ElementFactory (com.vaadin.flow.dom.ElementFactory)1 JsonUtils (com.vaadin.flow.internal.JsonUtils)1 RouterInterface (com.vaadin.flow.router.RouterInterface)1 HasChildView (com.vaadin.flow.router.legacy.HasChildView)1 View (com.vaadin.flow.router.legacy.View)1 DependencyFilter (com.vaadin.flow.server.DependencyFilter)1 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)1 VaadinResponse (com.vaadin.flow.server.VaadinResponse)1 VaadinService (com.vaadin.flow.server.VaadinService)1 VaadinServlet (com.vaadin.flow.server.VaadinServlet)1 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1