DefaultEnveloper implements Enveloper { @Override public Function<Object, JsonEnvelope> withMetadataFrom(final JsonEnvelope envelope) { return x -> envelopeFrom(buildMetaData(x, envelope.metadata(), clock), objectToJsonValueConverter.convert(x)); } DefaultEnveloper(); @Inject  DefaultEnveloper(final Clock clock, final ObjectToJsonValueConverter objectToJsonValueConverter); void register(@Observes final EventFoundEvent event); @Override Function<Object, JsonEnvelope> withMetadataFrom(final JsonEnvelope envelope); @Override Function<Object, JsonEnvelope> withMetadataFrom(final JsonEnvelope envelope, final String name); Function<Object, JsonEnvelope> toEnvelopeWithMetadataFrom(final Envelope<?> envelope); @SuppressWarnings("unchecked") EnveloperBuilder<T> envelop(final T payload);  }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionOnNullEvent() throws Exception { enveloper.withMetadataFrom(envelopeFrom(metadataBuilder().withId(randomUUID()).withName("name"), createObjectBuilder())).apply(null); }
@Test public void shouldEnvelopeObjectWithName() throws Exception { final JsonEnvelope event = enveloper.withMetadataFrom( envelopeFrom( metadataBuilder() .withId(COMMAND_UUID) .withName(TEST_EVENT_NAME) .withCausation(OLD_CAUSATION_ID), createObjectBuilder()), TEST_NAME) .apply(new TestEvent()); assertThat(event.metadata().id(), notNullValue()); assertThat(event.metadata().name(), equalTo(TEST_NAME)); assertThat(event.metadata().causation().size(), equalTo(2)); assertThat(event.metadata().causation().get(0), equalTo(OLD_CAUSATION_ID)); assertThat(event.metadata().causation().get(1), equalTo(COMMAND_UUID)); }
@Test public void shouldEnvelopeMapNullObjectWithName() throws Exception { final JsonEnvelope event = enveloper.withMetadataFrom( envelopeFrom( metadataBuilder() .withId(COMMAND_UUID) .withName(TEST_EVENT_NAME) .withCausation(OLD_CAUSATION_ID), createObjectBuilder()), TEST_NAME) .apply(null); assertThat(event.payload(), equalTo(JsonValue.NULL)); assertThat(event.metadata().id(), notNullValue()); assertThat(event.metadata().name(), equalTo(TEST_NAME)); assertThat(event.metadata().causation().size(), equalTo(2)); assertThat(event.metadata().causation().get(0), equalTo(OLD_CAUSATION_ID)); assertThat(event.metadata().causation().get(1), equalTo(COMMAND_UUID)); }
@Test public void shouldThrowExceptionIfProvidedInvalidEventObject() { exception.expect(InvalidEventException.class); exception.expectMessage("Failed to map event. No event registered for class java.lang.String"); enveloper.withMetadataFrom(envelopeFrom(metadataBuilder().withId(randomUUID()).withName("name"), createObjectBuilder())).apply("InvalidEventObject"); }