Search in sources :

Example 61 with JsonObject

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

the class JsonObjectTest method testGetMap.

@Test
public void testGetMap() {
    jsonObject.put("foo", "bar");
    jsonObject.put("quux", 123);
    JsonObject obj = createJsonObject();
    jsonObject.put("wibble", obj);
    Map<String, Object> map = jsonObject.getMap();
    map.remove("foo");
    assertFalse(jsonObject.containsKey("foo"));
    map.put("bleep", "flarp");
    assertTrue(jsonObject.containsKey("bleep"));
    jsonObject.remove("quux");
    assertFalse(map.containsKey("quux"));
    jsonObject.put("wooble", "plink");
    assertTrue(map.containsKey("wooble"));
    assertSame(obj, map.get("wibble"));
}
Also used : JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 62 with JsonObject

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

the class JsonObjectTest method testPutJsonObject.

@Test
public void testPutJsonObject() {
    JsonObject obj1 = new JsonObject().put("blah", "wibble");
    JsonObject obj2 = new JsonObject().put("eeek", "flibb");
    JsonObject obj3 = new JsonObject().put("floob", "plarp");
    assertSame(jsonObject, jsonObject.put("foo", obj1));
    assertEquals(obj1, jsonObject.getJsonObject("foo"));
    jsonObject.put("quux", obj2);
    assertEquals(obj2, jsonObject.getJsonObject("quux"));
    assertEquals(obj1, jsonObject.getJsonObject("foo"));
    jsonObject.put("foo", obj3);
    assertEquals(obj3, jsonObject.getJsonObject("foo"));
    jsonObject.put("foo", (JsonObject) null);
    assertTrue(jsonObject.containsKey("foo"));
    try {
        jsonObject.put(null, new JsonObject());
        fail();
    } catch (NullPointerException e) {
    // OK
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 63 with JsonObject

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

the class KeyStoreTest method testDefaultTrustOptionsJson.

@Test
public void testDefaultTrustOptionsJson() {
    PemTrustOptions def = new PemTrustOptions();
    PemTrustOptions json = new PemTrustOptions(new JsonObject());
    assertEquals(def.getCertPaths(), json.getCertPaths());
    assertEquals(def.getCertValues(), json.getCertValues());
}
Also used : JsonObject(io.vertx.core.json.JsonObject) PemTrustOptions(io.vertx.core.net.PemTrustOptions) Test(org.junit.Test)

Example 64 with JsonObject

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

the class JsonObjectTest method testGetJsonObject.

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

Example 65 with JsonObject

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

the class JsonObjectTest method testDecode.

@Test
public void testDecode() throws Exception {
    byte[] bytes = TestUtils.randomByteArray(10);
    String strBytes = Base64.getEncoder().encodeToString(bytes);
    Instant now = Instant.now();
    String strInstant = ISO_INSTANT.format(now);
    String json = "{\"mystr\":\"foo\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"myinstant\":\"" + strInstant + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}";
    JsonObject obj = new JsonObject(json);
    assertEquals(json, obj.encode());
    assertEquals("foo", obj.getString("mystr"));
    assertEquals(Integer.valueOf(123), obj.getInteger("myint"));
    assertEquals(Long.valueOf(1234), obj.getLong("mylong"));
    assertEquals(Float.valueOf(1.23f), obj.getFloat("myfloat"));
    assertEquals(Double.valueOf(2.34d), obj.getDouble("mydouble"));
    assertTrue(obj.getBoolean("myboolean"));
    assertTrue(TestUtils.byteArraysEqual(bytes, obj.getBinary("mybinary")));
    assertEquals(now, obj.getInstant("myinstant"));
    assertTrue(obj.containsKey("mynull"));
    JsonObject nestedObj = obj.getJsonObject("myobj");
    assertEquals("bar", nestedObj.getString("foo"));
    JsonArray nestedArr = obj.getJsonArray("myarr");
    assertEquals("foo", nestedArr.getString(0));
    assertEquals(Integer.valueOf(123), Integer.valueOf(nestedArr.getInteger(1)));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Instant(java.time.Instant) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

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