Search in sources :

Example 91 with JsonValue

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

the class UidlWriterTest method parentViewDependenciesAreAddedFirst.

@Test
public void parentViewDependenciesAreAddedFirst() {
    UI ui = initializeUIForDependenciesTest(new UI());
    UidlWriter uidlWriter = new UidlWriter();
    ui.add(new BaseClass());
    JsonObject response = uidlWriter.createUidl(ui, false);
    assertFalse("Did not expect to have lazy dependencies in uidl", response.hasKey(LoadMode.LAZY.name()));
    assertFalse("Did not expect to have inline dependencies in uidl", response.hasKey(LoadMode.INLINE.name()));
    assertTrue("Expected to have eager dependencies in uidl", response.hasKey(LoadMode.EAGER.name()));
    JsonArray eagerDependencies = response.getArray(LoadMode.EAGER.name());
    assertEquals("Expected to have exactly 3 eager dependencies in uidl, actual: %d", eagerDependencies.length(), 3);
    List<Class<?>> expectedClassOrder = Arrays.asList(SuperParentClass.class, ParentClass.class, BaseClass.class);
    for (int i = 0; i < expectedClassOrder.size(); i++) {
        Class<?> expectedClass = expectedClassOrder.get(i);
        HtmlImport htmlImport = expectedClass.getAnnotation(HtmlImport.class);
        JsonValue actualDependency = eagerDependencies.get(i);
        JsonObject expectedDependency = new Dependency(Dependency.Type.HTML_IMPORT, htmlImport.value(), htmlImport.loadMode()).toJson();
        assertTrue(String.format("Unexpected dependency. Expected: '%s', actual: '%s', class: '%s'", expectedDependency, actualDependency, expectedClass), expectedDependency.jsEquals(actualDependency));
    }
}
Also used : JsonArray(elemental.json.JsonArray) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) UI(com.vaadin.flow.component.UI) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Example 92 with JsonValue

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

the class JsonCodecTest method encodeWithTypeInfo_attachedElement.

@Test
public void encodeWithTypeInfo_attachedElement() {
    Element element = ElementFactory.createDiv();
    StateTree tree = new StateTree(new UI(), ElementChildrenList.class);
    tree.getRootNode().getFeature(ElementChildrenList.class).add(0, element.getNode());
    JsonValue json = JsonCodec.encodeWithTypeInfo(element);
    assertJsonEquals(JsonUtils.createArray(Json.create(JsonCodec.NODE_TYPE), Json.create(element.getNode().getId())), json);
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) UI(com.vaadin.flow.component.UI) Element(com.vaadin.flow.dom.Element) ElementChildrenList(com.vaadin.flow.internal.nodefeature.ElementChildrenList) JsonValue(elemental.json.JsonValue) Test(org.junit.Test)

Example 93 with JsonValue

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

the class JsonSerializerTest method serializeEmptyObjectWithObjects_returnJsonObjectWithNullProperties.

@Test
public void serializeEmptyObjectWithObjects_returnJsonObjectWithNullProperties() {
    ObjectWithOtherObjects bean = new ObjectWithOtherObjects();
    JsonValue json = JsonSerializer.toJson(bean);
    Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject);
    JsonObject jsonObject = (JsonObject) json;
    Assert.assertTrue(jsonObject.hasKey("object1"));
    Assert.assertTrue(jsonObject.get("object1") instanceof JsonNull);
    Assert.assertTrue(jsonObject.hasKey("object2"));
    Assert.assertTrue(jsonObject.get("object2") instanceof JsonNull);
    bean = JsonSerializer.toObject(ObjectWithOtherObjects.class, json);
    Assert.assertNotNull("The deserialized object should not be null", bean);
    Assert.assertNull(bean.getObject1());
    Assert.assertNull(bean.getObject2());
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) JsonNull(elemental.json.JsonNull) Test(org.junit.Test)

Example 94 with JsonValue

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

the class JsonSerializerTest method serializeEmptyObjectWithBasicCollections_returnJsonObjectWithNullProperties.

@Test
public void serializeEmptyObjectWithBasicCollections_returnJsonObjectWithNullProperties() {
    ObjectWithBasicCollections bean = new ObjectWithBasicCollections();
    /*
         * private List<String> listOfStrings; private Set<Integer>
         * setOfIntegers; private LinkedList<Boolean> linkedListOfBooleans;
         * private ArrayList<Double> arrayListOfDoubles;
         * 
         */
    JsonValue json = JsonSerializer.toJson(bean);
    Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject);
    JsonObject jsonObject = (JsonObject) json;
    Assert.assertTrue(jsonObject.hasKey("listOfStrings"));
    Assert.assertTrue(jsonObject.get("listOfStrings") instanceof JsonNull);
    Assert.assertTrue(jsonObject.hasKey("setOfIntegers"));
    Assert.assertTrue(jsonObject.get("setOfIntegers") instanceof JsonNull);
    Assert.assertTrue(jsonObject.hasKey("linkedListOfBooleans"));
    Assert.assertTrue(jsonObject.get("linkedListOfBooleans") instanceof JsonNull);
    Assert.assertTrue(jsonObject.hasKey("arrayListOfDoubles"));
    Assert.assertTrue(jsonObject.get("arrayListOfDoubles") instanceof JsonNull);
    bean = JsonSerializer.toObject(ObjectWithBasicCollections.class, json);
    Assert.assertNotNull("The deserialized object should not be null", bean);
    Assert.assertNull(bean.getListOfStrings());
    Assert.assertNull(bean.getSetOfIntegers());
    Assert.assertNull(bean.getLinkedListOfBooleans());
    Assert.assertNull(bean.getArrayListOfDoubles());
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) JsonNull(elemental.json.JsonNull) Test(org.junit.Test)

Example 95 with JsonValue

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

the class JsonSerializerTest method serializeEmptyRecursiveObject_returnJsonObjectWithNullProperties.

@Test
public void serializeEmptyRecursiveObject_returnJsonObjectWithNullProperties() {
    RecursiveObject bean = new RecursiveObject();
    JsonValue json = JsonSerializer.toJson(bean);
    Assert.assertTrue("The JsonValue should be instanceof JsonObject", json instanceof JsonObject);
    JsonObject jsonObject = (JsonObject) json;
    Assert.assertTrue(jsonObject.hasKey("recursive"));
    Assert.assertTrue(jsonObject.get("recursive") instanceof JsonNull);
    Assert.assertEquals(0, jsonObject.getNumber("index"), PRECISION);
    bean = JsonSerializer.toObject(RecursiveObject.class, json);
    Assert.assertNotNull("The deserialized object should not be null", bean);
    Assert.assertNull(bean.getRecursive());
    Assert.assertEquals(0, bean.getIndex());
}
Also used : JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) JsonNull(elemental.json.JsonNull) Test(org.junit.Test)

Aggregations

JsonValue (elemental.json.JsonValue)102 Test (org.junit.Test)76 JsonObject (elemental.json.JsonObject)46 JsonArray (elemental.json.JsonArray)31 JsonString (elemental.json.JsonString)11 JsonNull (elemental.json.JsonNull)7 Element (com.vaadin.flow.dom.Element)5 Matchers.anyString (org.mockito.Matchers.anyString)5 UI (com.vaadin.flow.component.UI)4 StateNode (com.vaadin.flow.internal.StateNode)4 JsonNumber (elemental.json.JsonNumber)4 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)3 ArrayList (java.util.ArrayList)3 StateNode (com.vaadin.client.flow.StateNode)2 MapProperty (com.vaadin.client.flow.nodefeature.MapProperty)2 JsonSerializable (com.vaadin.flow.component.JsonSerializable)2 StateTree (com.vaadin.flow.internal.StateTree)2 AbstractNodeFeatureTest (com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)2 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)2 Element (elemental.dom.Element)2