EventBusService extends AbstractVerticle { protected static final <T> T getTypesafeValueForField(final Class<T> clazz, final JsonObject payload, final String field) { Objects.requireNonNull(clazz); Objects.requireNonNull(payload); Objects.requireNonNull(field); final Object result = payload.getValue(field); if (clazz.isInstance(result)) { return clazz.cast(result); } return null; }  @Autowired(required = false) final void setTracer(final Tracer opentracingTracer); @Override final void start(final Promise<Void> startPromise); @Override final void stop(final Promise<Void> stopPromise);  }
@Test public void testValidType() { final JsonObject payload = new JsonObject() .put("booleanValue", true) .put("stringValue", "foo") .put("intValue", 42); final String stringValue = EventBusService.getTypesafeValueForField(String.class, payload, "stringValue"); assertEquals("foo", stringValue); final Integer intValue = EventBusService.getTypesafeValueForField(Integer.class, payload, "intValue"); assertEquals(Integer.valueOf(42), intValue); final Boolean booleanValue = EventBusService.getTypesafeValueForField(Boolean.class, payload, "booleanValue"); assertEquals(Boolean.TRUE, booleanValue); }
@Test public void testInvalidType() { final JsonObject device = new JsonObject().put("device-id", "someValue"); final String stringValue = EventBusService.getTypesafeValueForField(String.class, device, "device-id"); assertEquals("someValue", stringValue); final Integer intValue = EventBusService.getTypesafeValueForField(Integer.class, device, "device-id"); assertNull(intValue); }
@Test public void testGetNull() { final JsonObject device = new JsonObject().put("device-id", (String) null); final String value = EventBusService.getTypesafeValueForField(String.class, device, "device-id"); assertNull(value); }