Search in sources :

Example 56 with JsonObject

use of io.vertx.core.json.JsonObject in project vert.x by eclipse.

the class JsonObjectTest method testCopy.

@Test
public void testCopy() {
    jsonObject.put("foo", "bar");
    jsonObject.put("quux", 123);
    JsonObject obj = createJsonObject();
    jsonObject.put("wibble", obj);
    // CharSequence
    jsonObject.put("eek", new StringBuilder("blah"));
    JsonObject copy = jsonObject.copy();
    assertNotSame(jsonObject, copy);
    assertEquals(jsonObject, copy);
    copy.put("blah", "flib");
    assertFalse(jsonObject.containsKey("blah"));
    copy.remove("foo");
    assertFalse(copy.containsKey("foo"));
    assertTrue(jsonObject.containsKey("foo"));
    jsonObject.put("oob", "flarb");
    assertFalse(copy.containsKey("oob"));
    jsonObject.remove("quux");
    assertFalse(jsonObject.containsKey("quux"));
    assertTrue(copy.containsKey("quux"));
    JsonObject nested = jsonObject.getJsonObject("wibble");
    JsonObject nestedCopied = copy.getJsonObject("wibble");
    assertNotSame(nested, nestedCopied);
    assertEquals(nested, nestedCopied);
    assertEquals("blah", copy.getString("eek"));
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 57 with JsonObject

use of io.vertx.core.json.JsonObject in project vert.x by eclipse.

the class JsonObjectTest method testGetJsonObjectDefault.

@Test
public void testGetJsonObjectDefault() {
    JsonObject obj = new JsonObject().put("blah", "wibble");
    JsonObject def = new JsonObject().put("eek", "quuz");
    jsonObject.put("foo", obj);
    assertEquals(obj, jsonObject.getJsonObject("foo", def));
    assertEquals(obj, jsonObject.getJsonObject("foo", null));
    jsonObject.put("foo", "hello");
    try {
        jsonObject.getJsonObject("foo", def);
        fail();
    } catch (ClassCastException e) {
    // Ok
    }
    jsonObject.putNull("foo");
    assertNull(jsonObject.getJsonObject("foo", def));
    assertEquals(def, jsonObject.getJsonObject("absent", def));
    assertNull(jsonObject.getJsonObject("foo", null));
    assertNull(jsonObject.getJsonObject("absent", null));
    try {
        jsonObject.getJsonObject(null, null);
        fail();
    } catch (NullPointerException e) {
    // OK
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 58 with JsonObject

use of io.vertx.core.json.JsonObject in project vert.x by eclipse.

the class JsonObjectTest method testGetValue.

@Test
public void testGetValue() {
    jsonObject.put("foo", 123);
    assertEquals(123, jsonObject.getValue("foo"));
    jsonObject.put("foo", 123l);
    assertEquals(123l, jsonObject.getValue("foo"));
    jsonObject.put("foo", 123f);
    assertEquals(123f, jsonObject.getValue("foo"));
    jsonObject.put("foo", 123d);
    assertEquals(123d, jsonObject.getValue("foo"));
    jsonObject.put("foo", false);
    assertEquals(false, jsonObject.getValue("foo"));
    jsonObject.put("foo", true);
    assertEquals(true, jsonObject.getValue("foo"));
    jsonObject.put("foo", "bar");
    assertEquals("bar", jsonObject.getValue("foo"));
    JsonObject obj = new JsonObject().put("blah", "wibble");
    jsonObject.put("foo", obj);
    assertEquals(obj, jsonObject.getValue("foo"));
    JsonArray arr = new JsonArray().add("blah").add("wibble");
    jsonObject.put("foo", arr);
    assertEquals(arr, jsonObject.getValue("foo"));
    byte[] bytes = TestUtils.randomByteArray(100);
    jsonObject.put("foo", bytes);
    assertTrue(TestUtils.byteArraysEqual(bytes, Base64.getDecoder().decode((String) jsonObject.getValue("foo"))));
    jsonObject.putNull("foo");
    assertNull(jsonObject.getValue("foo"));
    assertNull(jsonObject.getValue("absent"));
    // JsonObject with inner Map
    Map<String, Object> map = new HashMap<>();
    Map<String, Object> innerMap = new HashMap<>();
    innerMap.put("blah", "wibble");
    map.put("foo", innerMap);
    jsonObject = new JsonObject(map);
    obj = (JsonObject) jsonObject.getValue("foo");
    assertEquals("wibble", obj.getString("blah"));
    // JsonObject with inner List
    map = new HashMap<>();
    List<Object> innerList = new ArrayList<>();
    innerList.add("blah");
    map.put("foo", innerList);
    jsonObject = new JsonObject(map);
    arr = (JsonArray) jsonObject.getValue("foo");
    assertEquals("blah", arr.getString(0));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 59 with JsonObject

use of io.vertx.core.json.JsonObject in project vert.x by eclipse.

the class JsonObjectTest method testIterator.

@Test
public void testIterator() {
    jsonObject.put("foo", "bar");
    jsonObject.put("quux", 123);
    JsonObject obj = createJsonObject();
    jsonObject.put("wibble", obj);
    Iterator<Map.Entry<String, Object>> iter = jsonObject.iterator();
    assertTrue(iter.hasNext());
    Map.Entry<String, Object> entry = iter.next();
    assertEquals("foo", entry.getKey());
    assertEquals("bar", entry.getValue());
    assertTrue(iter.hasNext());
    entry = iter.next();
    assertEquals("quux", entry.getKey());
    assertEquals(123, entry.getValue());
    assertTrue(iter.hasNext());
    entry = iter.next();
    assertEquals("wibble", entry.getKey());
    assertEquals(obj, entry.getValue());
    assertFalse(iter.hasNext());
    iter.remove();
    assertFalse(obj.containsKey("wibble"));
    assertEquals(2, jsonObject.size());
}
Also used : JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 60 with JsonObject

use of io.vertx.core.json.JsonObject in project vert.x by eclipse.

the class JsonObjectTest method assertNumberEquals.

private void assertNumberEquals(Number value1, Number value2) {
    JsonObject o1 = new JsonObject().put("key", value1);
    JsonObject o2 = new JsonObject().put("key", value2);
    if (!o1.equals(o2)) {
        fail("Was expecting " + value1.getClass().getSimpleName() + ":" + value1 + " == " + value2.getClass().getSimpleName() + ":" + value2);
    }
    JsonArray a1 = new JsonArray().add(value1);
    JsonArray a2 = new JsonArray().add(value2);
    if (!a1.equals(a2)) {
        fail("Was expecting " + value1.getClass().getSimpleName() + ":" + value1 + " == " + value2.getClass().getSimpleName() + ":" + value2);
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)383 Test (org.junit.Test)210 JsonArray (io.vertx.core.json.JsonArray)89 HttpURLConnection (java.net.HttpURLConnection)68 Future (io.vertx.core.Future)55 Handler (io.vertx.core.Handler)51 Async (io.vertx.ext.unit.Async)50 Vertx (io.vertx.core.Vertx)48 TestContext (io.vertx.ext.unit.TestContext)48 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)47 RunWith (org.junit.runner.RunWith)47 Timeout (org.junit.rules.Timeout)43 Before (org.junit.Before)41 Rule (org.junit.Rule)33 EventBusMessage (org.eclipse.hono.util.EventBusMessage)27 AsyncResult (io.vertx.core.AsyncResult)26 Buffer (io.vertx.core.buffer.Buffer)26 Constants (org.eclipse.hono.util.Constants)25 Mockito (org.mockito.Mockito)25 Objects (java.util.Objects)23