/** * Like {@link #getValue(String)} but specifying a default value to return if there is no entry. * * @param key the key to lookup * @param def the default value to use if the entry is not present * @return the value or {@code def} if no entry present */ public Object getValue(String key, Object def) { Objects.requireNonNull(key); Object val = getValue(key); return val != null || map.containsKey(key) ? val : def; }
private void sendMessage() { JsonObject jsonMessage = new JsonObject().put("message_from_sender_verticle", "hello consumer"); eventBus.send("Consumer", jsonMessage, messageAsyncResult -> { if(messageAsyncResult.succeeded()) { JsonObject jsonReply = (JsonObject) messageAsyncResult.result().body(); System.out.println("received reply: " + jsonReply.getValue("reply")); } }); } }
private void registerHandler() { MessageConsumer<JsonObject> messageConsumer = eventBus.consumer(verticleAddress); messageConsumer.handler(message -> { JsonObject jsonMessage = message.body(); System.out.println(jsonMessage.getValue("message_from_sender_verticle")); JsonObject jsonReply = new JsonObject().put("reply", "how interesting!"); message.reply(jsonReply); }); } }
@Test public void testPutInstantAsObject() { Object instant = Instant.now(); JsonObject jsonObject = new JsonObject(); jsonObject.put("instant", instant); // assert data is stored as String assertTrue(jsonObject.getValue("instant") instanceof String); }
@Override public void start() throws Exception { AmqpBridge bridge = AmqpBridge.create(vertx); // Start the bridge, then use the event loop thread to process things thereafter. bridge.start("localhost", 5672, res -> { if(!res.succeeded()) { System.out.println("Bridge startup failed: " + res.cause()); return; } // Set up a consumer using the bridge, register a handler for it. MessageConsumer<JsonObject> consumer = bridge.createConsumer("myAmqpAddress"); consumer.handler(vertxMsg -> { JsonObject amqpMsgPayload = vertxMsg.body(); Object amqpBody = amqpMsgPayload.getValue(AmqpConstants.BODY); // Print body of received AMQP message System.out.println("Received a message with body: " + amqpBody); }); }); } }
@Test public void testGetValueDefault() { jsonObject.put("foo", 123); assertEquals(123, jsonObject.getValue("foo", "blah")); assertEquals(123, jsonObject.getValue("foo", null)); jsonObject.put("foo", 123l); assertEquals(123l, jsonObject.getValue("foo", "blah")); assertEquals(123l, jsonObject.getValue("foo", null)); jsonObject.put("foo", 123f); assertEquals(123f, jsonObject.getValue("foo", "blah")); assertEquals(123f, jsonObject.getValue("foo", null)); jsonObject.put("foo", 123d); assertEquals(123d, jsonObject.getValue("foo", "blah")); assertEquals(123d, jsonObject.getValue("foo", null)); jsonObject.put("foo", false); assertEquals(false, jsonObject.getValue("foo", "blah")); assertEquals(false, jsonObject.getValue("foo", null)); jsonObject.put("foo", true); assertEquals(true, jsonObject.getValue("foo", "blah")); assertEquals(true, jsonObject.getValue("foo", null)); jsonObject.put("foo", "bar"); assertEquals("bar", jsonObject.getValue("foo", "blah")); assertEquals("bar", jsonObject.getValue("foo", null)); JsonObject obj = new JsonObject().put("blah", "wibble"); jsonObject.put("foo", obj); assertEquals(obj, jsonObject.getValue("foo", "blah")); assertEquals(obj, jsonObject.getValue("foo", null)); JsonArray arr = new JsonArray().add("blah").add("wibble"); jsonObject.put("foo", arr); assertEquals(arr, jsonObject.getValue("foo", "blah"));
@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")); map.put("foo", innerMap);
@Test public void testRemove() { jsonObject.put("mystr", "bar"); jsonObject.put("myint", 123); assertEquals("bar", jsonObject.remove("mystr")); assertNull(jsonObject.getValue("mystr")); assertEquals(123, jsonObject.remove("myint")); assertNull(jsonObject.getValue("myint")); assertTrue(jsonObject.isEmpty()); }
@Test public void testPutInstant() { Instant bin1 = Instant.now(); Instant bin2 = bin1.plus(1, ChronoUnit.DAYS); Instant bin3 = bin1.plus(1, ChronoUnit.MINUTES); assertSame(jsonObject, jsonObject.put("foo", bin1)); assertEquals(bin1, jsonObject.getInstant("foo")); assertEquals(bin1.toString(), jsonObject.getValue("foo")); jsonObject.put("quux", bin2); assertEquals(bin2, jsonObject.getInstant("quux")); assertEquals(bin2.toString(), jsonObject.getValue("quux")); assertEquals(bin1, jsonObject.getInstant("foo")); assertEquals(bin1.toString(), jsonObject.getValue("foo")); jsonObject.put("foo", bin3); assertEquals(bin3, jsonObject.getInstant("foo")); assertEquals(bin3.toString(), jsonObject.getValue("foo")); jsonObject.put("foo", (Instant) null); assertTrue(jsonObject.containsKey("foo")); try { jsonObject.put(null, bin1); fail(); } catch (NullPointerException e) { // OK } }
@Test public void testPutBinary() { byte[] bin1 = TestUtils.randomByteArray(100); byte[] bin2 = TestUtils.randomByteArray(100); byte[] bin3 = TestUtils.randomByteArray(100); assertSame(jsonObject, jsonObject.put("foo", bin1)); assertArrayEquals(bin1, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bin1), jsonObject.getValue("foo")); jsonObject.put("quux", bin2); assertArrayEquals(bin2, jsonObject.getBinary("quux")); assertEquals(Base64.getEncoder().encodeToString(bin2), jsonObject.getValue("quux")); assertArrayEquals(bin1, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bin1), jsonObject.getValue("foo")); jsonObject.put("foo", bin3); assertArrayEquals(bin3, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bin3), jsonObject.getValue("foo")); jsonObject.put("foo", (byte[]) null); assertTrue(jsonObject.containsKey("foo")); try { jsonObject.put(null, bin1); fail(); } catch (NullPointerException e) { // OK } }
assertEquals(Double.valueOf(1.23d), jsonObject.getDouble("double")); assertArrayEquals(bytes, jsonObject.getBinary("binary")); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("binary")); assertEquals(now, jsonObject.getInstant("instant")); assertEquals(now.toString(), jsonObject.getValue("instant")); assertEquals(obj, jsonObject.getJsonObject("obj")); assertEquals(arr, jsonObject.getJsonArray("arr"));
jsonObject.put("foo", now); assertEquals(now, jsonObject.getInstant("foo", later)); assertEquals(now.toString(), jsonObject.getValue("foo", later)); assertEquals(now, jsonObject.getInstant("foo", null)); assertEquals(now.toString(), jsonObject.getValue("foo", null)); assertNull(jsonObject.getInstant("foo", later)); assertEquals(later, jsonObject.getInstant("absent", later)); assertEquals(later, jsonObject.getValue("absent", later)); assertNull(jsonObject.getInstant("foo", null)); assertNull(jsonObject.getValue("foo", null)); assertNull(jsonObject.getInstant("absent", null)); assertNull(jsonObject.getValue("absent", null)); try { jsonObject.getInstant(null, null); jsonObject.getValue(null, null); fail(); } catch (NullPointerException e) {
@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")); assertArrayEquals(bytes, obj.getBinary("mybinary")); assertEquals(Base64.getEncoder().encodeToString(bytes), obj.getValue("mybinary")); assertEquals(now, obj.getInstant("myinstant")); assertEquals(now.toString(), obj.getValue("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))); }
jsonObject.put("foo", now); assertEquals(now, jsonObject.getInstant("foo")); assertEquals(now.toString(), jsonObject.getValue("foo")); assertNull(jsonObject.getValue("foo")); assertNull(jsonObject.getInstant("absent")); assertNull(jsonObject.getValue("absent")); try { jsonObject.getInstant(null); jsonObject.getValue(null); fail(); } catch (NullPointerException e) { jsonObject.getValue(null, null); fail(); } catch (NullPointerException e) {
jsonObject.put("foo", bytes); assertArrayEquals(bytes, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("foo")); assertNull(jsonObject.getValue("foo")); assertNull(jsonObject.getBinary("absent")); assertNull(jsonObject.getValue("absent")); try { jsonObject.getBinary(null);
@Test public void testGetBinaryDefault() { byte[] bytes = TestUtils.randomByteArray(100); byte[] defBytes = TestUtils.randomByteArray(100); jsonObject.put("foo", bytes); assertArrayEquals(bytes, jsonObject.getBinary("foo", defBytes)); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("foo", Base64.getEncoder().encode(defBytes))); assertArrayEquals(bytes, jsonObject.getBinary("foo", null)); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("foo", null)); jsonObject.put("foo", 123); try { jsonObject.getBinary("foo", defBytes); fail(); } catch (ClassCastException e) { // Ok } jsonObject.putNull("foo"); assertNull(jsonObject.getBinary("foo", defBytes)); assertTrue(TestUtils.byteArraysEqual(defBytes, jsonObject.getBinary("absent", defBytes))); assertNull(jsonObject.getBinary("foo", null)); assertNull(jsonObject.getBinary("absent", null)); try { jsonObject.getBinary(null, null); fail(); } catch (NullPointerException e) { // OK } }
@Override public void init(Vertx vertx, Context context) { super.init(vertx, context); this.endpoint = (Endpoint) context.config().getValue(AbstractTransport.ENDPOINT_KEY); this.endpointObject = (URIEndpointObject) endpoint.getAddress(); }
@Override public void start() throws Exception { super.start(); RestTransportClient restClient = (RestTransportClient) config().getValue(RestTransportClient.class.getName()); restClient.getClientMgr().findClientPool(false, context); } }
@Test public void testPutInstantAsObject() { Object instant = Instant.now(); JsonObject jsonObject = new JsonObject(); jsonObject.put("instant", instant); // assert data is stored as String assertTrue(jsonObject.getValue("instant") instanceof String); }