Search in sources :

Example 96 with JsonObject

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

the class DataObjectTest method testEmptyDataObjectToJson.

@Test
public void testEmptyDataObjectToJson() {
    TestDataObject obj = new TestDataObject();
    JsonObject json = new JsonObject();
    TestDataObjectConverter.toJson(obj, json);
    json = new JsonObject(json.encode());
    assertEquals(null, json.getString("stringValue"));
    assertEquals(false, json.getBoolean("booleanValue"));
    assertEquals(0, (int) json.getInteger("byteValue"));
    assertEquals(0, (int) json.getInteger("shortValue"));
    assertEquals(0, (int) json.getInteger("intValue"));
    assertEquals(0L, (long) json.getLong("longValue"));
    assertEquals(0f, (float) json.getFloat("floatValue"), 0);
    assertEquals(0d, (double) json.getFloat("doubleValue"), 0);
    assertEquals(Character.toString((char) 0), json.getString("charValue"));
    assertEquals(null, json.getBoolean("boxedBooleanValue"));
    assertEquals(null, json.getInteger("boxedByteValue"));
    assertEquals(null, json.getInteger("boxedShortValue"));
    assertEquals(null, json.getInteger("boxedIntValue"));
    assertEquals(null, json.getLong("boxedLongValue"));
    assertEquals(null, json.getFloat("boxedFloatValue"));
    assertEquals(null, json.getFloat("boxedDoubleValue"));
    assertEquals(null, json.getString("boxedCharValue"));
    assertEquals(null, json.getJsonObject("aggregatedDataObject"));
    assertEquals(null, json.getBinary("buffer"));
    assertEquals(null, json.getJsonObject("jsonObject"));
    assertEquals(null, json.getJsonArray("jsonArray"));
    assertEquals(null, json.getString("httpMethod"));
    assertEquals(null, json.getJsonArray("stringValues"));
    assertEquals(null, json.getJsonArray("boxedBooleanValues"));
    assertEquals(null, json.getJsonArray("boxedByteValues"));
    assertEquals(null, json.getJsonArray("boxedShortValues"));
    assertEquals(null, json.getJsonArray("boxedIntValues"));
    assertEquals(null, json.getJsonArray("boxedLongValues"));
    assertEquals(null, json.getJsonArray("boxedFloatValues"));
    assertEquals(null, json.getJsonArray("boxedDoubleValues"));
    assertEquals(null, json.getJsonArray("boxedCharValues"));
    assertEquals(null, json.getJsonArray("aggregatedDataObjects"));
    assertEquals(null, json.getJsonArray("buffers"));
    assertEquals(null, json.getJsonArray("jsonObjects"));
    assertEquals(null, json.getJsonArray("jsonArrays"));
    assertEquals(null, json.getJsonArray("httpMethods"));
    assertEquals(null, json.getJsonArray("objects"));
    assertEquals(new JsonArray(), json.getJsonArray("addedStringValues"));
    assertEquals(new JsonArray(), json.getJsonArray("addedBoxedBooleanValues"));
    assertEquals(new JsonArray(), json.getJsonArray("addedBoxedByteValues"));
    assertEquals(new JsonArray(), json.getJsonArray("addedBoxedShortValues"));
    assertEquals(new JsonArray(), json.getJsonArray("addedBoxedIntValues"));
    assertEquals(new JsonArray(), json.getJsonArray("addedBoxedLongValues"));
    assertEquals(new JsonArray(), json.getJsonArray("addedBoxedFloatValues"));
    assertEquals(new JsonArray(), json.getJsonArray("addedBoxedDoubleValues"));
    assertEquals(new JsonArray(), json.getJsonArray("addedBoxedCharValues"));
    assertEquals(new JsonArray(), json.getJsonArray("addedAggregatedDataObjects"));
    assertEquals(new JsonArray(), json.getJsonArray("addedBuffers"));
    assertEquals(new JsonArray(), json.getJsonArray("addedJsonObjects"));
    assertEquals(new JsonArray(), json.getJsonArray("addedJsonArrays"));
    assertEquals(new JsonArray(), json.getJsonArray("addedHttpMethods"));
    assertEquals(new JsonArray(), json.getJsonArray("addedObjects"));
    assertEquals(null, json.getJsonArray("stringValueMap"));
    assertEquals(null, json.getJsonArray("boxedBooleanValueMap"));
    assertEquals(null, json.getJsonArray("boxedByteValueMap"));
    assertEquals(null, json.getJsonArray("boxedShortValueMap"));
    assertEquals(null, json.getJsonArray("boxedIntValueMap"));
    assertEquals(null, json.getJsonArray("boxedLongValueMap"));
    assertEquals(null, json.getJsonArray("boxedFloatValueMap"));
    assertEquals(null, json.getJsonArray("boxedDoubleValueMap"));
    assertEquals(null, json.getJsonArray("boxedCharValueMap"));
    assertEquals(null, json.getJsonArray("aggregatedDataObjectMap"));
    assertEquals(null, json.getJsonArray("bufferMap"));
    assertEquals(null, json.getJsonArray("jsonObjectMap"));
    assertEquals(null, json.getJsonArray("jsonArrayMap"));
    assertEquals(null, json.getJsonArray("httpMethodMap"));
    assertEquals(null, json.getJsonArray("objectMap"));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 97 with JsonObject

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

the class DataObjectTest method testDataObjectToJson.

@Test
public void testDataObjectToJson() {
    String key = TestUtils.randomAlphaString(10);
    String stringValue = TestUtils.randomAlphaString(20);
    boolean booleanValue = TestUtils.randomBoolean();
    byte byteValue = TestUtils.randomByte();
    short shortValue = TestUtils.randomShort();
    int intValue = TestUtils.randomInt();
    long longValue = TestUtils.randomLong();
    float floatValue = TestUtils.randomFloat();
    double doubleValue = TestUtils.randomDouble();
    char charValue = TestUtils.randomChar();
    Boolean boxedBooleanValue = TestUtils.randomBoolean();
    Byte boxedByteValue = TestUtils.randomByte();
    Short boxedShortValue = TestUtils.randomShort();
    Integer boxedIntValue = TestUtils.randomInt();
    Long boxedLongValue = TestUtils.randomLong();
    Float boxedFloatValue = TestUtils.randomFloat();
    Double boxedDoubleValue = TestUtils.randomDouble();
    Character boxedCharValue = TestUtils.randomChar();
    AggregatedDataObject aggregatedDataObject = new AggregatedDataObject().setValue(TestUtils.randomAlphaString(20));
    Buffer buffer = TestUtils.randomBuffer(20);
    JsonObject jsonObject = new JsonObject().put("wibble", TestUtils.randomAlphaString(20));
    JsonArray jsonArray = new JsonArray().add(TestUtils.randomAlphaString(20));
    HttpMethod httpMethod = HttpMethod.values()[TestUtils.randomPositiveInt() % HttpMethod.values().length];
    Map<String, Object> map = new HashMap<>();
    map.put(TestUtils.randomAlphaString(10), TestUtils.randomAlphaString(20));
    map.put(TestUtils.randomAlphaString(10), TestUtils.randomBoolean());
    map.put(TestUtils.randomAlphaString(10), TestUtils.randomInt());
    List<Object> list = new ArrayList<>();
    list.add(TestUtils.randomAlphaString(20));
    list.add(TestUtils.randomBoolean());
    list.add(TestUtils.randomInt());
    TestDataObject obj = new TestDataObject();
    obj.setStringValue(stringValue);
    obj.setBooleanValue(booleanValue);
    obj.setByteValue(byteValue);
    obj.setShortValue(shortValue);
    obj.setIntValue(intValue);
    obj.setLongValue(longValue);
    obj.setFloatValue(floatValue);
    obj.setDoubleValue(doubleValue);
    obj.setCharValue(charValue);
    obj.setBoxedBooleanValue(boxedBooleanValue);
    obj.setBoxedByteValue(boxedByteValue);
    obj.setBoxedShortValue(boxedShortValue);
    obj.setBoxedIntValue(boxedIntValue);
    obj.setBoxedLongValue(boxedLongValue);
    obj.setBoxedFloatValue(boxedFloatValue);
    obj.setBoxedDoubleValue(boxedDoubleValue);
    obj.setBoxedCharValue(boxedCharValue);
    obj.setAggregatedDataObject(aggregatedDataObject);
    obj.setBuffer(buffer);
    obj.setJsonObject(jsonObject);
    obj.setJsonArray(jsonArray);
    obj.setHttpMethod(httpMethod);
    obj.setStringValues(Collections.singletonList(stringValue));
    obj.setBoxedBooleanValues(Collections.singletonList(boxedBooleanValue));
    obj.setBoxedByteValues(Collections.singletonList(boxedByteValue));
    obj.setBoxedShortValues(Collections.singletonList(boxedShortValue));
    obj.setBoxedIntValues(Collections.singletonList(boxedIntValue));
    obj.setBoxedLongValues(Collections.singletonList(boxedLongValue));
    obj.setBoxedFloatValues(Collections.singletonList(boxedFloatValue));
    obj.setBoxedDoubleValues(Collections.singletonList(boxedDoubleValue));
    obj.setBoxedCharValues(Collections.singletonList(boxedCharValue));
    obj.setAggregatedDataObjects(Collections.singletonList(aggregatedDataObject));
    obj.setBuffers(Collections.singletonList(buffer));
    obj.setJsonObjects(Collections.singletonList(jsonObject));
    obj.setJsonArrays(Collections.singletonList(jsonArray));
    obj.setHttpMethods(Collections.singletonList(httpMethod));
    obj.setObjects(list);
    obj.setStringValueMap(Collections.singletonMap(key, stringValue));
    obj.setBoxedBooleanValueMap(Collections.singletonMap(key, boxedBooleanValue));
    obj.setBoxedByteValueMap(Collections.singletonMap(key, boxedByteValue));
    obj.setBoxedShortValueMap(Collections.singletonMap(key, boxedShortValue));
    obj.setBoxedIntValueMap(Collections.singletonMap(key, boxedIntValue));
    obj.setBoxedLongValueMap(Collections.singletonMap(key, boxedLongValue));
    obj.setBoxedFloatValueMap(Collections.singletonMap(key, boxedFloatValue));
    obj.setBoxedDoubleValueMap(Collections.singletonMap(key, boxedDoubleValue));
    obj.setBoxedCharValueMap(Collections.singletonMap(key, boxedCharValue));
    obj.setAggregatedDataObjectMap(Collections.singletonMap(key, aggregatedDataObject));
    obj.setBufferMap(Collections.singletonMap(key, buffer));
    obj.setJsonObjectMap(Collections.singletonMap(key, jsonObject));
    obj.setJsonArrayMap(Collections.singletonMap(key, jsonArray));
    obj.setHttpMethodMap(Collections.singletonMap(key, httpMethod));
    obj.setObjectMap(map);
    obj.addKeyedStringValue(key, stringValue);
    obj.addKeyedBoxedBooleanValue(key, boxedBooleanValue);
    obj.addKeyedBoxedByteValue(key, boxedByteValue);
    obj.addKeyedBoxedShortValue(key, boxedShortValue);
    obj.addKeyedBoxedIntValue(key, boxedIntValue);
    obj.addKeyedBoxedLongValue(key, boxedLongValue);
    obj.addKeyedBoxedFloatValue(key, boxedFloatValue);
    obj.addKeyedBoxedDoubleValue(key, boxedDoubleValue);
    obj.addKeyedBoxedCharValue(key, boxedCharValue);
    obj.addKeyedDataObjectValue(key, aggregatedDataObject);
    obj.addKeyedBufferValue(key, buffer);
    obj.addKeyedJsonObjectValue(key, jsonObject);
    obj.addKeyedJsonArrayValue(key, jsonArray);
    obj.addKeyedEnumValue(key, httpMethod);
    map.forEach(obj::addKeyedObjectValue);
    JsonObject json = new JsonObject();
    TestDataObjectConverter.toJson(obj, json);
    json = new JsonObject(json.encode());
    assertEquals(stringValue, json.getString("stringValue"));
    assertEquals(booleanValue, json.getBoolean("booleanValue"));
    assertEquals((int) byteValue, (int) json.getInteger("byteValue"));
    assertEquals((int) shortValue, (int) json.getInteger("shortValue"));
    assertEquals(intValue, (int) json.getInteger("intValue"));
    assertEquals(longValue, (long) json.getLong("longValue"));
    assertEquals(floatValue, json.getFloat("floatValue"), 0.001);
    assertEquals(doubleValue, (double) json.getFloat("doubleValue"), 0.001);
    assertEquals(Character.toString(charValue), json.getString("charValue"));
    assertEquals(boxedBooleanValue, json.getBoolean("boxedBooleanValue"));
    assertEquals((int) boxedByteValue, (int) json.getInteger("boxedByteValue"));
    assertEquals((int) boxedShortValue, (int) json.getInteger("boxedShortValue"));
    assertEquals(boxedIntValue, json.getInteger("boxedIntValue"));
    assertEquals(boxedLongValue, json.getLong("boxedLongValue"));
    assertEquals(boxedFloatValue, json.getFloat("boxedFloatValue"), 0.001);
    assertEquals(boxedDoubleValue, (double) json.getFloat("boxedDoubleValue"), 0.001);
    assertEquals(Character.toString(boxedCharValue), json.getString("boxedCharValue"));
    assertEquals(aggregatedDataObject.toJson(), json.getJsonObject("aggregatedDataObject"));
    assertEquals(buffer, Buffer.buffer(json.getBinary("buffer")));
    assertEquals(jsonObject, json.getJsonObject("jsonObject"));
    assertEquals(jsonArray, json.getJsonArray("jsonArray"));
    assertEquals(httpMethod.name(), json.getString("httpMethod"));
    assertEquals(new JsonArray().add(stringValue), json.getJsonArray("stringValues"));
    assertEquals(new JsonArray().add(boxedBooleanValue), json.getJsonArray("boxedBooleanValues"));
    assertEquals(new JsonArray().add(boxedByteValue), json.getJsonArray("boxedByteValues"));
    assertEquals(new JsonArray().add(boxedShortValue), json.getJsonArray("boxedShortValues"));
    assertEquals(new JsonArray().add(boxedIntValue), json.getJsonArray("boxedIntValues"));
    assertEquals(new JsonArray().add(boxedLongValue), json.getJsonArray("boxedLongValues"));
    assertEquals(1, json.getJsonArray("boxedFloatValues").size());
    assertEquals(boxedFloatValue, json.getJsonArray("boxedFloatValues").getFloat(0), 0.001);
    assertEquals(1, json.getJsonArray("boxedDoubleValues").size());
    assertEquals(boxedDoubleValue, json.getJsonArray("boxedDoubleValues").getDouble(0), 0.001);
    assertEquals(new JsonArray().add(Character.toString(boxedCharValue)), json.getJsonArray("boxedCharValues"));
    assertEquals(new JsonArray().add(aggregatedDataObject.toJson()), json.getJsonArray("aggregatedDataObjects"));
    assertEquals(new JsonArray().add(toBase64(buffer)), json.getJsonArray("buffers"));
    assertEquals(new JsonArray().add(jsonObject), json.getJsonArray("jsonObjects"));
    assertEquals(new JsonArray().add(jsonArray), json.getJsonArray("jsonArrays"));
    assertEquals(new JsonArray().add(httpMethod.name()), json.getJsonArray("httpMethods"));
    assertEquals(new JsonArray().add(list.get(0)).add(list.get(1)).add(list.get(2)), json.getJsonArray("objects"));
    assertEquals(new JsonObject().put(key, stringValue), json.getJsonObject("stringValueMap"));
    assertEquals(new JsonObject().put(key, boxedBooleanValue), json.getJsonObject("boxedBooleanValueMap"));
    assertEquals(new JsonObject().put(key, boxedByteValue), json.getJsonObject("boxedByteValueMap"));
    assertEquals(new JsonObject().put(key, boxedShortValue), json.getJsonObject("boxedShortValueMap"));
    assertEquals(new JsonObject().put(key, boxedIntValue), json.getJsonObject("boxedIntValueMap"));
    assertEquals(new JsonObject().put(key, boxedLongValue), json.getJsonObject("boxedLongValueMap"));
    assertEquals(1, json.getJsonObject("boxedFloatValueMap").size());
    assertEquals(boxedFloatValue, json.getJsonObject("boxedFloatValueMap").getFloat(key), 0.001);
    assertEquals(1, json.getJsonObject("boxedDoubleValueMap").size());
    assertEquals(boxedDoubleValue, json.getJsonObject("boxedDoubleValueMap").getDouble(key), 0.001);
    assertEquals(new JsonObject().put(key, Character.toString(boxedCharValue)), json.getJsonObject("boxedCharValueMap"));
    assertEquals(new JsonObject().put(key, aggregatedDataObject.toJson()), json.getJsonObject("aggregatedDataObjectMap"));
    assertEquals(new JsonObject().put(key, toBase64(buffer)), json.getJsonObject("bufferMap"));
    assertEquals(new JsonObject().put(key, jsonObject), json.getJsonObject("jsonObjectMap"));
    assertEquals(new JsonObject().put(key, jsonArray), json.getJsonObject("jsonArrayMap"));
    assertEquals(new JsonObject().put(key, httpMethod.name()), json.getJsonObject("httpMethodMap"));
    assertEquals(toJson(map), json.getJsonObject("objectMap"));
    assertEquals(new JsonObject().put(key, stringValue), json.getJsonObject("keyedStringValues"));
    assertEquals(new JsonObject().put(key, boxedBooleanValue), json.getJsonObject("keyedBoxedBooleanValues"));
    assertEquals(new JsonObject().put(key, boxedByteValue), json.getJsonObject("keyedBoxedByteValues"));
    assertEquals(new JsonObject().put(key, boxedShortValue), json.getJsonObject("keyedBoxedShortValues"));
    assertEquals(new JsonObject().put(key, boxedIntValue), json.getJsonObject("keyedBoxedIntValues"));
    assertEquals(new JsonObject().put(key, boxedLongValue), json.getJsonObject("keyedBoxedLongValues"));
    assertEquals(1, json.getJsonObject("keyedBoxedFloatValues").size());
    assertEquals(boxedFloatValue, json.getJsonObject("keyedBoxedFloatValues").getFloat(key), 0.001);
    assertEquals(1, json.getJsonObject("keyedBoxedDoubleValues").size());
    assertEquals(boxedDoubleValue, json.getJsonObject("keyedBoxedDoubleValues").getDouble(key), 0.001);
    assertEquals(new JsonObject().put(key, Character.toString(boxedCharValue)), json.getJsonObject("keyedBoxedCharValues"));
    assertEquals(new JsonObject().put(key, aggregatedDataObject.toJson()), json.getJsonObject("keyedDataObjectValues"));
    assertEquals(new JsonObject().put(key, toBase64(buffer)), json.getJsonObject("keyedBufferValues"));
    assertEquals(new JsonObject().put(key, jsonObject), json.getJsonObject("keyedJsonObjectValues"));
    assertEquals(new JsonObject().put(key, jsonArray), json.getJsonObject("keyedJsonArrayValues"));
    assertEquals(new JsonObject().put(key, httpMethod.name()), json.getJsonObject("keyedEnumValues"));
    assertEquals(toJson(map), json.getJsonObject("keyedObjectValues"));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Buffer(io.vertx.core.buffer.Buffer) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) HttpMethod(io.vertx.core.http.HttpMethod) Test(org.junit.Test)

Example 98 with JsonObject

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

the class DataObjectTest method testNotInherit.

@Test
public void testNotInherit() {
    ChildNotInheritingDataObject obj = new ChildNotInheritingDataObject();
    JsonObject expectedJson = new JsonObject();
    expectedJson.put("childProperty", "childProperty_value");
    expectedJson.put("parentProperty", "parentProperty_value");
    ChildNotInheritingDataObjectConverter.fromJson(expectedJson, obj);
    assertEquals("childProperty_value", obj.getChildProperty());
    assertEquals(null, obj.getParentProperty());
    JsonObject json = new JsonObject();
    ChildNotInheritingDataObjectConverter.toJson(obj, json);
    expectedJson.remove("parentProperty");
    assertEquals(expectedJson, json);
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 99 with JsonObject

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

the class AbstractVerticleTest method testFieldsSet.

@Test
public void testFieldsSet() {
    JsonObject config = new JsonObject().put("foo", "bar");
    vertx.deployVerticle(verticle, new DeploymentOptions().setConfig(config), onSuccess(res -> {
        assertEquals(res, verticle.getDeploymentID());
        assertEquals(config, verticle.getConfig());
        testComplete();
    }));
    await();
}
Also used : DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test) DeploymentOptions(io.vertx.core.DeploymentOptions) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 100 with JsonObject

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

the class LauncherExtensibilityTest method testThatCustomLauncherCanUpdateConfigurationWhenNoneArePassed.

@Test
public void testThatCustomLauncherCanUpdateConfigurationWhenNoneArePassed() throws IOException {
    long time = System.nanoTime();
    Launcher myLauncher = new Launcher() {

        @Override
        protected String getMainVerticle() {
            return HttpTestVerticle.class.getName();
        }

        @Override
        public void afterStartingVertx(Vertx vertx) {
            LauncherExtensibilityTest.this.vertx = vertx;
        }

        @Override
        public void afterConfigParsed(JsonObject config) {
            config.put("time", time);
        }
    };
    myLauncher.dispatch(new String[0]);
    waitUntil(() -> {
        try {
            return RunCommandTest.getHttpCode() == 200;
        } catch (IOException e) {
            return false;
        }
    });
    assertThat(RunCommandTest.getContent().getJsonObject("conf").getLong("time")).isEqualTo(time);
}
Also used : Launcher(io.vertx.core.Launcher) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) RunCommandTest(io.vertx.core.impl.launcher.commands.RunCommandTest)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)185 Test (org.junit.Test)136 JsonArray (io.vertx.core.json.JsonArray)44 ArrayList (java.util.ArrayList)11 DeploymentOptions (io.vertx.core.DeploymentOptions)8 HashMap (java.util.HashMap)8 Launcher (io.vertx.core.Launcher)7 Buffer (io.vertx.core.buffer.Buffer)7 IOException (java.io.IOException)7 Instant (java.time.Instant)7 AsciiString (io.netty.util.AsciiString)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Message (io.vertx.core.eventbus.Message)4 File (java.io.File)4 Random (java.util.Random)4 AbstractVerticle (io.vertx.core.AbstractVerticle)3 Vertx (io.vertx.core.Vertx)3 VertxOptions (io.vertx.core.VertxOptions)3 VertxInternal (io.vertx.core.impl.VertxInternal)3 DecodeException (io.vertx.core.json.DecodeException)3