Search in sources :

Example 1 with ModelList

use of com.vaadin.flow.internal.nodefeature.ModelList in project flow by vaadin.

the class TemplateModelTest method setListWithInclude.

@Test
public void setListWithInclude() {
    TemplateWithIncludeOnList template = new TemplateWithIncludeOnList();
    List<Bean> beans = new ArrayList<>();
    beans.add(new Bean(1));
    beans.add(new Bean(2));
    template.getModel().setBeans(beans);
    ModelList modelList = getModelList(template, "beans");
    ModelMap bean1 = ModelMap.get(modelList.get(0));
    Set<String> propertiesInMap = bean1.getKeys().collect(Collectors.toSet());
    Assert.assertTrue("Bean in model should have an 'intValue' property", propertiesInMap.remove("intValue"));
    Assert.assertEquals("All other properties should have been filtered out", 0, propertiesInMap.size());
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) ArrayList(java.util.ArrayList) Bean(com.vaadin.flow.templatemodel.Bean) Test(org.junit.Test)

Example 2 with ModelList

use of com.vaadin.flow.internal.nodefeature.ModelList in project flow by vaadin.

the class TemplateModelTest method setListWithSubBeanInclude.

@Test
public void setListWithSubBeanInclude() {
    TemplateWithIncludeOnListSubBean template = new TemplateWithIncludeOnListSubBean();
    List<BeanContainingBeans> beanContainingBeans = new ArrayList<>();
    beanContainingBeans.add(new BeanContainingBeans(new Bean(11), new Bean(12)));
    beanContainingBeans.add(new BeanContainingBeans(null, new Bean(22)));
    template.getModel().setBeanContainingBeans(beanContainingBeans);
    ModelList modelList = getModelList(template, "beanContainingBeans");
    Assert.assertEquals(2, modelList.size());
    ModelMap container1Map = ModelMap.get(modelList.get(0));
    ModelMap container2Map = ModelMap.get(modelList.get(1));
    Set<String> bean1bean2 = new HashSet<>();
    bean1bean2.add("bean1");
    bean1bean2.add("bean2");
    Assert.assertEquals(bean1bean2, container1Map.getKeys().collect(Collectors.toSet()));
    Assert.assertEquals(bean1bean2, container2Map.getKeys().collect(Collectors.toSet()));
    Set<String> container1Bean1Properties = getKeys(container1Map.resolveModelMap("bean1"));
    Assert.assertTrue(container1Bean1Properties.remove("intValue"));
    Assert.assertEquals(0, container1Bean1Properties.size());
    Set<String> container1Bean2Properties = getKeys(container1Map.resolveModelMap("bean2"));
    Assert.assertTrue(container1Bean2Properties.remove("booleanValue"));
    Assert.assertEquals(0, container1Bean2Properties.size());
    Set<String> container2Bean1Properties = getKeys(container2Map.resolveModelMap("bean1"));
    // Null value in the initial bean implies not imported or created
    Assert.assertEquals(0, container2Bean1Properties.size());
    Set<String> container2Bean2Properties = getKeys(container2Map.resolveModelMap("bean2"));
    Assert.assertTrue(container2Bean2Properties.remove("booleanValue"));
    Assert.assertEquals(0, container2Bean2Properties.size());
}
Also used : BeanContainingBeans(com.vaadin.flow.templatemodel.BeanContainingBeans) ModelList(com.vaadin.flow.internal.nodefeature.ModelList) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) ArrayList(java.util.ArrayList) Bean(com.vaadin.flow.templatemodel.Bean) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ModelList

use of com.vaadin.flow.internal.nodefeature.ModelList in project flow by vaadin.

the class TemplateModelTest method setListWithSubBeanInclude.

@Test
public void setListWithSubBeanInclude() {
    TemplateWithIncludeOnListSubBean template = new TemplateWithIncludeOnListSubBean();
    List<BeanContainingBeans> beanContainingBeans = new ArrayList<>();
    beanContainingBeans.add(new BeanContainingBeans(new Bean(11), new Bean(12)));
    beanContainingBeans.add(new BeanContainingBeans(null, new Bean(22)));
    template.getModel().setBeanContainingBeans(beanContainingBeans);
    ModelList modelList = getModelList(template, "beanContainingBeans");
    Assert.assertEquals(2, modelList.size());
    ElementPropertyMap container1Map = ElementPropertyMap.getModel(modelList.get(0));
    ElementPropertyMap container2Map = ElementPropertyMap.getModel(modelList.get(1));
    Set<String> bean1bean2 = new HashSet<>();
    bean1bean2.add("bean1");
    bean1bean2.add("bean2");
    Assert.assertEquals(bean1bean2, container1Map.getPropertyNames().collect(Collectors.toSet()));
    Assert.assertEquals(bean1bean2, container2Map.getPropertyNames().collect(Collectors.toSet()));
    Set<String> container1Bean1Properties = getKeys(container1Map.resolveModelMap("bean1"));
    Assert.assertTrue(container1Bean1Properties.remove("intValue"));
    Assert.assertEquals(0, container1Bean1Properties.size());
    Set<String> container1Bean2Properties = getKeys(container1Map.resolveModelMap("bean2"));
    Assert.assertTrue(container1Bean2Properties.remove("booleanValue"));
    Assert.assertEquals(0, container1Bean2Properties.size());
    Set<String> container2Bean1Properties = getKeys(container2Map.resolveModelMap("bean1"));
    // Null value in the initial bean implies not imported or created
    Assert.assertEquals(0, container2Bean1Properties.size());
    Set<String> container2Bean2Properties = getKeys(container2Map.resolveModelMap("bean2"));
    Assert.assertTrue(container2Bean2Properties.remove("booleanValue"));
    Assert.assertEquals(0, container2Bean2Properties.size());
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) ArrayList(java.util.ArrayList) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with ModelList

use of com.vaadin.flow.internal.nodefeature.ModelList in project flow by vaadin.

the class ModelMapTest method resolveAndCreateSubSubChildList.

@Test
public void resolveAndCreateSubSubChildList() {
    ModelList child = rootMap.resolveModelList("grand.parent.child");
    ModelMap parent = getParentMapAndAssertMapping(child, "child");
    ModelMap grand = getParentMapAndAssertMapping(parent, "parent");
    ModelMap resolvedRoot = getParentMapAndAssertMapping(grand, "grand");
    Assert.assertEquals(rootMap, resolvedRoot);
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) ModelMap(com.vaadin.flow.internal.nodefeature.ModelMap) Test(org.junit.Test)

Example 5 with ModelList

use of com.vaadin.flow.internal.nodefeature.ModelList in project flow by vaadin.

the class MapSyncRpcHandlerTest method syncJSON_jsonIsForStateNodeInList_propertySetToStateNodeCopy.

@Test
public void syncJSON_jsonIsForStateNodeInList_propertySetToStateNodeCopy() throws Exception {
    // Let's use element's ElementPropertyMap for testing.
    TestComponent component = new TestComponent();
    Element element = component.getElement();
    UI ui = new UI();
    ui.add(component);
    StateNode node = element.getNode();
    // Set model value directly via ElementPropertyMap
    ElementPropertyMap propertyMap = node.getFeature(ElementPropertyMap.class);
    propertyMap.setUpdateFromClientFilter(name -> true);
    ModelList modelList = propertyMap.resolveModelList("foo");
    // fake StateNode has been created for the model
    StateNode item = new StateNode(ElementPropertyMap.class);
    modelList.add(item);
    item.getFeature(ElementPropertyMap.class).setProperty("bar", "baz");
    // Use the model node id for JSON object which represents a value to
    // update
    JsonObject json = Json.createObject();
    json.put("nodeId", item.getId());
    // send sync request
    sendSynchronizePropertyEvent(element, ui, TEST_PROPERTY, json);
    // Now the model node should be copied and available as the
    // TEST_PROPERTY value
    Serializable testPropertyValue = propertyMap.getProperty(TEST_PROPERTY);
    Assert.assertTrue(testPropertyValue instanceof StateNode);
    StateNode newNode = (StateNode) testPropertyValue;
    Assert.assertNotEquals(item.getId(), newNode.getId());
    Assert.assertEquals("baz", newNode.getFeature(ElementPropertyMap.class).getProperty("bar"));
}
Also used : ModelList(com.vaadin.flow.internal.nodefeature.ModelList) TestComponent(com.vaadin.flow.component.ComponentTest.TestComponent) Serializable(java.io.Serializable) UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) StateNode(com.vaadin.flow.internal.StateNode) JsonObject(elemental.json.JsonObject) ElementPropertyMap(com.vaadin.flow.internal.nodefeature.ElementPropertyMap) Test(org.junit.Test)

Aggregations

ModelList (com.vaadin.flow.internal.nodefeature.ModelList)14 Test (org.junit.Test)10 ModelMap (com.vaadin.flow.internal.nodefeature.ModelMap)8 ArrayList (java.util.ArrayList)6 StateNode (com.vaadin.flow.internal.StateNode)5 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)5 Bean (com.vaadin.flow.templatemodel.Bean)3 HashSet (java.util.HashSet)2 TestComponent (com.vaadin.flow.component.ComponentTest.TestComponent)1 UI (com.vaadin.flow.component.UI)1 Element (com.vaadin.flow.dom.Element)1 BeanContainingBeans (com.vaadin.flow.templatemodel.BeanContainingBeans)1 ModelType (com.vaadin.flow.templatemodel.ModelType)1 JsonObject (elemental.json.JsonObject)1 Serializable (java.io.Serializable)1