DefaultJsonObjectEnvelopeConverter implements JsonObjectEnvelopeConverter { @Override public JsonObject fromEnvelope(final JsonEnvelope envelope) { final Metadata metadata = envelope.metadata(); if (metadata == null) { throw new IllegalArgumentException("Failed to convert envelope, no metadata present."); } final JsonObjectBuilder builder = createObjectBuilder(); builder.add(METADATA, metadata.asJsonObject()); final ValueType payloadType = envelope.payload().getValueType(); if (payloadType == OBJECT) { final JsonObject payloadAsJsonObject = envelope.payloadAsJsonObject(); payloadAsJsonObject.keySet().forEach(key -> builder.add(key, payloadAsJsonObject.get(key))); } else { throw new IllegalArgumentException(String.format("Payload type %s not supported.", payloadType)); } return builder.build(); }  @Override JsonEnvelope asEnvelope(final JsonObject envelopeJsonObject); @Override JsonEnvelope asEnvelope(final String jsonString); @Override JsonObject fromEnvelope(final JsonEnvelope envelope); @Override JsonValue extractPayloadFromEnvelope(final JsonObject envelope); @Override String asJsonString(final JsonEnvelope envelope);  }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionOnArrayPayloadType() { jsonObjectEnvelopeConverter.fromEnvelope( envelopeFrom(metadataBuilder().withId(randomUUID()).withName("name"), createArrayBuilder().add(ARRAY_ITEM_1).add(ARRAY_ITEM_2).build())); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionOnNumberPayloadType() { jsonObjectEnvelopeConverter.fromEnvelope( envelopeFrom(metadataBuilder().withId(randomUUID()).withName("name"), createObjectBuilder().add(FIELD_NUMBER, 100).build().getJsonNumber(FIELD_NUMBER))); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenProvidedEnvelopeWithoutMetadata() throws IOException { jsonObjectEnvelopeConverter.fromEnvelope(envelopeFrom((Metadata) null, createObjectBuilder().build())); }