DefaultJsonEnvelope implements JsonEnvelope { @Override public String toString() { final JsonObjectBuilder builder = Json.createObjectBuilder(); if (metadata != null) { builder.add("id", String.valueOf(metadata.id())) .add("name", metadata.name()); metadata.clientCorrelationId().ifPresent(s -> builder.add(CORRELATION, s)); metadata.sessionId().ifPresent(s -> builder.add(SESSION_ID, s)); metadata.userId().ifPresent(s -> builder.add(USER_ID, s)); metadata.source().ifPresent(s -> builder.add(SOURCE, s)); final JsonArrayBuilder causationBuilder = Json.createArrayBuilder(); final List<UUID> causes = metadata.causation(); if (causes != null) { metadata.causation().forEach(uuid -> causationBuilder.add(String.valueOf(uuid))); } builder.add("causation", causationBuilder); } return builder.build().toString(); } DefaultJsonEnvelope(final Metadata metadata, final JsonValue payload); @Override Metadata metadata(); @Override JsonValue payload(); @Override JsonObject payloadAsJsonObject(); @Override JsonArray payloadAsJsonArray(); @Override JsonNumber payloadAsJsonNumber(); @Override JsonString payloadAsJsonString(); @Override JsonObject asJsonObject(); @Override String toString(); @Override String toDebugStringPrettyPrint(); @Override String toObfuscatedDebugString();  }
@Test public void shouldReturnEnvelopeAsString() { final UUID metadataId = randomUUID(); final String metadataName = "nameABC123"; final String source = "sourceName"; final JsonEnvelope envelope = envelopeFrom( metadataBuilder().withId(metadataId).withName(metadataName).withSource(source), createObjectBuilder() .add("strProperty", "valueA") .add("nested", createObjectBuilder() .add("strProperty1", "valueB") .add("uuidProperty1", randomUUID().toString()) .add("numProperty1", 34) .add("boolProperty1", true)) .add("arrayProperty", createArrayBuilder() .add("value1") .add("value2") .add("value3")) .build()); with(envelope.toString()) .assertEquals("id", metadataId.toString()) .assertEquals("name", metadataName) .assertEquals("source" , source) .assertNotDefined("strProperty") .assertNotDefined("nested.strProperty1") .assertNotDefined("nested.uuidProperty1") .assertNotDefined("nested.numProperty1") .assertNotDefined("nested.boolProperty1") .assertNotDefined("arrayProperty"); }