use of com.vaadin.flow.internal.JsonUtils 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);
}
Aggregations