public Batch(JsonObject jsonObject) { this.buffer = jsonObject.toBuffer(); this.type = 'O'; }
protected Map<JsonObject, Buffer> genJsonToBuffer(int size) { Map<JsonObject, Buffer> map = new HashMap<>(); for (int i = 0; i < size; i++) { JsonObject key = new JsonObject().put("key", i); map.put(key, key.toBuffer()); } return map; }
@Override public void encodeToWire(Buffer buffer, JsonObject jsonObject) { Buffer encoded = jsonObject.toBuffer(); buffer.appendInt(encoded.length()); buffer.appendBuffer(encoded); }
private Buffer loadJsonAsBuffer(URL url) { try { Buffer encoded = new JsonObject(Json.mapper.readValue(url, Map.class)).toBuffer(); return Buffer.buffer().appendInt(encoded.length()).appendBuffer(encoded); } catch (IOException e) { throw new RuntimeException(e); } }
@Test public void testEncodeToBuffer() throws Exception { jsonObject.put("mystr", "foo"); jsonObject.put("mycharsequence", new StringBuilder("oob")); jsonObject.put("myint", 123); jsonObject.put("mylong", 1234l); jsonObject.put("myfloat", 1.23f); jsonObject.put("mydouble", 2.34d); jsonObject.put("myboolean", true); byte[] bytes = TestUtils.randomByteArray(10); jsonObject.put("mybinary", bytes); Instant now = Instant.now(); jsonObject.put("myinstant", now); jsonObject.putNull("mynull"); jsonObject.put("myobj", new JsonObject().put("foo", "bar")); jsonObject.put("myarr", new JsonArray().add("foo").add(123)); String strBytes = Base64.getEncoder().encodeToString(bytes); Buffer expected = Buffer.buffer("{\"mystr\":\"foo\",\"mycharsequence\":\"oob\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"myinstant\":\"" + ISO_INSTANT.format(now) + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}", "UTF-8"); Buffer json = jsonObject.toBuffer(); assertArrayEquals(expected.getBytes(), json.getBytes()); }
@Test public void testObjectMapping() { JsonParser parser = JsonParser.newParser(); List<Object> values = new ArrayList<>(); parser.objectValueMode(); parser.handler(event -> values.add(event.mapTo(TheObject.class))); parser.handle(new JsonObject().put("f", "the-value").toBuffer()); assertEquals(Collections.singletonList(new TheObject("the-value")), values); }
private void direct(JsonObject jsonObject) throws Exception { Buffer buffer = Buffer.buffer(); Buffer encoded = jsonObject.toBuffer(); buffer.appendInt(encoded.length()); buffer.appendBuffer(encoded); consume(buffer); } }
@Test public void testObjectMappingWithTypeReference() { JsonParser parser = JsonParser.newParser(); List<Object> values = new ArrayList<>(); parser.objectValueMode(); parser.handler(event -> values.add(event.mapTo(new TypeReference<TheObject>() {}))); parser.handle(new JsonObject().put("f", "the-value").toBuffer()); assertEquals(Collections.singletonList(new TheObject("the-value")), values); }
private void testCustomMetricsOptionsFromJson(boolean jsonFile) throws Exception { JsonObject json = new JsonObject() .put("metricsOptions", new JsonObject() .put("enabled", true) .put("customProperty", "customPropertyValue") .put("nestedOptions", new JsonObject().put("nestedProperty", "nestedValue"))); String optionsArg; if (jsonFile) { File file = testFolder.newFile(); Files.write(file.toPath(), json.toBuffer().getBytes()); optionsArg = file.getPath(); } else { optionsArg = json.toString(); } MyLauncher launcher = new MyLauncher(); String[] args = {"run", "java:" + TestVerticle.class.getCanonicalName(), "-options", optionsArg}; ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(MetricsOptionsTest.createMetricsFromMetaInfLoader("io.vertx.core.CustomMetricsFactory")); try { launcher.dispatch(args); } finally { Thread.currentThread().setContextClassLoader(oldCL); } assertWaitUntil(() -> TestVerticle.instanceCount.get() == 1); VertxOptions opts = launcher.getVertxOptions(); CustomMetricsOptions custom = (CustomMetricsOptions) opts.getMetricsOptions(); assertEquals("customPropertyValue", custom.getCustomProperty()); assertEquals("nestedValue", custom.getNestedOptions().getNestedProperty()); }
@Test public void testParseObjectValue() { JsonParser parser = JsonParser.newParser(); AtomicInteger status = new AtomicInteger(); parser.objectValueMode(); JsonObject expected = new JsonObject() .put("number", 3) .put("floating", 3.5d) .put("true", true) .put("false", false) .put("string", "s") .put("object", new JsonObject().put("foo", "bar")) .put("array", new JsonArray().add(0).add(1).add(2)) .putNull("null") .put("bytes", new byte[]{1, 2, 3}); parser.handler(event -> { assertEquals(0, status.getAndIncrement()); assertEquals(JsonEventType.VALUE, event.type()); assertEquals(expected, event.value()); }); parser.handle(expected.toBuffer()); assertEquals(1, status.get()); }
private void testConfigureFromJson(boolean jsonFile) throws Exception { JsonObject json = new JsonObject() .put("eventLoopPoolSize", 123) .put("maxEventLoopExecuteTime", 123767667) .put("metricsOptions", new JsonObject().put("enabled", true)) .put("eventBusOptions", new JsonObject().put("clustered", true).put("clusterPublicHost", "mars")) .put("haGroup", "somegroup") .put("maxEventLoopExecuteTimeUnit", "SECONDS"); String optionsArg; if (jsonFile) { File file = testFolder.newFile(); Files.write(file.toPath(), json.toBuffer().getBytes()); optionsArg = file.getPath(); } else { optionsArg = json.toString(); } MyLauncher launcher = new MyLauncher(); String[] args = new String[]{"run", "java:" + TestVerticle.class.getCanonicalName(), "-options", optionsArg}; launcher.dispatch(args); assertWaitUntil(() -> TestVerticle.instanceCount.get() == 1); VertxOptions opts = launcher.getVertxOptions(); assertEquals(123, opts.getEventLoopPoolSize(), 0); assertEquals(123767667L, opts.getMaxEventLoopExecuteTime()); assertEquals(true, opts.getMetricsOptions().isEnabled()); assertEquals(true, opts.isClustered()); assertEquals("mars", opts.getClusterPublicHost()); assertEquals("somegroup", opts.getHAGroup()); assertEquals(TimeUnit.SECONDS, opts.getMaxEventLoopExecuteTimeUnit()); }
protected Map<JsonObject, Buffer> genJsonToBuffer(int size) { Map<JsonObject, Buffer> map = new HashMap<>(); for (int i = 0; i < size; i++) { JsonObject key = new JsonObject().put("key", i); map.put(key, key.toBuffer()); } return map; }
@Override public void encodeToWire(Buffer buffer, JsonObject jsonObject) { Buffer encoded = jsonObject.toBuffer(); buffer.appendInt(encoded.length()); buffer.appendBuffer(encoded); }
private Buffer loadJsonAsBuffer(URL url) { try { Buffer encoded = new JsonObject(Json.mapper.readValue(url, Map.class)).toBuffer(); return Buffer.buffer().appendInt(encoded.length()).appendBuffer(encoded); } catch (IOException e) { throw new RuntimeException(e); } }
@Test public void testEncodeToBuffer() throws Exception { jsonObject.put("mystr", "foo"); jsonObject.put("mycharsequence", new StringBuilder("oob")); jsonObject.put("myint", 123); jsonObject.put("mylong", 1234l); jsonObject.put("myfloat", 1.23f); jsonObject.put("mydouble", 2.34d); jsonObject.put("myboolean", true); byte[] bytes = TestUtils.randomByteArray(10); jsonObject.put("mybinary", bytes); Instant now = Instant.now(); jsonObject.put("myinstant", now); jsonObject.putNull("mynull"); jsonObject.put("myobj", new JsonObject().put("foo", "bar")); jsonObject.put("myarr", new JsonArray().add("foo").add(123)); String strBytes = Base64.getEncoder().encodeToString(bytes); Buffer expected = Buffer.buffer("{\"mystr\":\"foo\",\"mycharsequence\":\"oob\",\"myint\":123,\"mylong\":1234,\"myfloat\":1.23,\"mydouble\":2.34,\"" + "myboolean\":true,\"mybinary\":\"" + strBytes + "\",\"myinstant\":\"" + ISO_INSTANT.format(now) + "\",\"mynull\":null,\"myobj\":{\"foo\":\"bar\"},\"myarr\":[\"foo\",123]}", "UTF-8"); Buffer json = jsonObject.toBuffer(); assertArrayEquals(expected.getBytes(), json.getBytes()); }
@Test public void testObjectMapping() { JsonParser parser = JsonParser.newParser(); List<Object> values = new ArrayList<>(); parser.objectValueMode(); parser.handler(event -> values.add(event.mapTo(TheObject.class))); parser.handle(new JsonObject().put("f", "the-value").toBuffer()); assertEquals(Collections.singletonList(new TheObject("the-value")), values); }
@Test public void testObjectMappingWithTypeReference() { JsonParser parser = JsonParser.newParser(); List<Object> values = new ArrayList<>(); parser.objectValueMode(); parser.handler(event -> values.add(event.mapTo(new TypeReference<TheObject>() {}))); parser.handle(new JsonObject().put("f", "the-value").toBuffer()); assertEquals(Collections.singletonList(new TheObject("the-value")), values); }
private void direct(JsonObject jsonObject) throws Exception { Buffer buffer = Buffer.buffer(); Buffer encoded = jsonObject.toBuffer(); buffer.appendInt(encoded.length()); buffer.appendBuffer(encoded); consume(buffer); } }
@Test public void testFixedContent() { Buffer buffer = new JsonObject().put("toto", "titi").toBuffer(); testRoute.produces("application/json").handler(rc -> rc.response().end(buffer)); client.get(testRoute.getPath(), onSuccess(resp -> { assertEquals("application/json", contentType(resp)); assertEquals(Integer.valueOf(buffer.length()), contentLength(resp)); resp.bodyHandler(buf -> { assertEquals(buffer, buf); testComplete(); }); })).putHeader(HttpHeaders.ACCEPT, "application/json").end(); await(); }
@Test public void testChunkedContent() { Buffer buffer = new JsonObject().put("toto", "titi").toBuffer(); testRoute.produces("application/json").handler(rc -> rc.response().setChunked(true).end(buffer)); client.get(testRoute.getPath(), onSuccess(resp -> { assertEquals("application/json", contentType(resp)); assertNull(contentLength(resp)); resp.bodyHandler(buf -> { assertEquals(buffer, buf); testComplete(); }); })).putHeader(HttpHeaders.ACCEPT, "application/json").end(); await(); }