EnvelopePayloadTypeConverter { @SuppressWarnings("unchecked") public <R> Envelope<R> convert(final Envelope<?> envelope, final Class<R> clazz) { if (envelope == null) { return null; } if (envelope.payload() == null) { return envelopeFrom(envelope.metadata(), null); } if (clazz.isAssignableFrom(envelope.payload().getClass())) { return (Envelope<R>) envelope; } return envelopeFrom(envelope.metadata(), objectMapper.convertValue(envelope.payload(), clazz)); } @Inject  EnvelopePayloadTypeConverter(final ObjectMapper objectMapper);  EnvelopePayloadTypeConverter(); @SuppressWarnings("unchecked") Envelope<R> convert(final Envelope<?> envelope, final Class<R> clazz);  }
@Test public void shouldConvertPojoToJsonValue() throws IOException { final Envelope<TestObject> inputEnvelope = getEnvelope(new TestObject("test")); final Envelope<JsonValue> resultEnvelope = convert(inputEnvelope, JsonValue.class); final JsonObject payload = (JsonObject) resultEnvelope.payload(); assertThat(payload.getValueType(), is(OBJECT)); assertThat(payload.toString(), hasJsonPath("$.myString", equalTo("test"))); }
@Test public void shouldHandleNullPojo() throws IOException { final TestObject testObject = null; final Envelope<TestObject> inputEnvelope = getEnvelope(testObject); final Envelope<DifferentTestObject> differentTestObjectEnvelope = convert(inputEnvelope, DifferentTestObject.class); assertThat(differentTestObjectEnvelope.payload(), nullValue()); }
@Test public void shouldConvertJsonValueToPojo() throws IOException { final JsonObject payload = createObjectBuilder().add("myString", "newTest").build(); final Envelope<JsonValue> inputEnvelope = getEnvelope(payload); final Envelope<TestObject> resultEnvelope = convert(inputEnvelope, TestObject.class); final TestObject testObject = resultEnvelope.payload(); assertThat(testObject, isA(TestObject.class)); assertThat(testObject.getMyString(), is("newTest")); }
@Test public void shouldConvertJsonEnvelopeToJsonEnvelope() throws IOException { final JsonObject payload = createObjectBuilder().add("myString", "newTest").build(); final JsonEnvelope inputEnvelope = getJsonEnvelope(payload); final Envelope<JsonValue> resultEnvelope = convert(inputEnvelope, JsonValue.class); final JsonObject returnedPayload = (JsonObject) resultEnvelope.payload(); assertThat(returnedPayload.getValueType(), is(OBJECT)); assertThat(returnedPayload.toString(), hasJsonPath("$.myString", equalTo("newTest"))); }
@Test public void shouldConvertJsonEnvelopeToPojo() throws IOException { final JsonObject payload = createObjectBuilder().add("myString", "newTest").build(); final Envelope<JsonValue> inputEnvelope = getJsonEnvelope(payload); final Envelope<TestObject> resultEnvelope = convert(inputEnvelope, TestObject.class); final TestObject testObject = resultEnvelope.payload(); assertThat(testObject, isA(TestObject.class)); assertThat(testObject.getMyString(), is("newTest")); }
@Test public void shouldConvertPojoToPojo() throws IOException { final TestObject testObject = new TestObject("testString"); final Envelope<TestObject> inputEnvelope = getEnvelope(testObject); final Envelope<DifferentTestObject> differentTestObjectEnvelope = convert(inputEnvelope, DifferentTestObject.class); assertThat(differentTestObjectEnvelope.payload(), isA(DifferentTestObject.class)); }
@Test public void shouldConvertJsonValueToJsonValue() throws IOException { final JsonObject payload = createObjectBuilder().add("myString", "newTest").build(); final Envelope<JsonValue> inputEnvelope = getEnvelope(payload); final Envelope<JsonValue> objectEnvelope = convert(inputEnvelope, JsonValue.class); assertThat(objectEnvelope.payload(), isA(JsonValue.class)); assertThat(objectEnvelope.payload().toString(), hasJsonPath("$.myString", equalTo("newTest"))); }
@Test public void shouldReturnANull() throws IOException { final JsonEnvelope nullEnvelope = null; final Envelope<JsonValue> objectEnvelope = convert(nullEnvelope, JsonValue.class); assertThat(objectEnvelope, nullValue()); }