JsonUtilities { public static <TEntity> Optional<byte[]> convertJsonToBytes(TEntity entity) { try { return Optional.empty().of(mapper.writeValueAsBytes(entity)); } catch(Exception e) { if(log.isErrorEnabled()) { log.error(String.format("Failed to convert entity %s to JSON", entity), e); } } return Optional.empty(); }  static Optional<byte[]> convertJsonToBytes(TEntity entity);  }
@Test public void json_is_generated_when_serialization_succeeds() throws Exception { TestEntity entity = new TestEntity(); entity.val = "Test"; Optional<byte[]> resultBytes = JsonUtilities.convertJsonToBytes(entity); Assert.assertEquals(true, resultBytes.isPresent()); Assert.assertEquals("{\"val\":\"Test\"}", new String(resultBytes.get())); }