Search in sources :

Example 1 with EventFactory

use of io.spine.server.command.EventFactory in project core-java by SpineEventEngine.

the class TypeNameShould method obtain_type_name_of_event.

@Test
public void obtain_type_name_of_event() {
    final Command command = requestFactory.command().create(newUuidValue());
    final StringValue producerId = Wrapper.forString(getClass().getSimpleName());
    final EventFactory ef = EventFactory.newBuilder().setCommandId(Commands.generateId()).setProducerId(producerId).setCommandContext(command.getContext()).build();
    final Event event = ef.createEvent(Time.getCurrentTime(), Tests.<Version>nullRef());
    final TypeName typeName = TypeName.ofEvent(event);
    assertNotNull(typeName);
    assertEquals(Timestamp.class.getSimpleName(), typeName.getSimpleName());
}
Also used : Command(io.spine.base.Command) EventFactory(io.spine.server.command.EventFactory) Event(io.spine.base.Event) StringValue(com.google.protobuf.StringValue) Timestamp(com.google.protobuf.Timestamp) Test(org.junit.Test)

Example 2 with EventFactory

use of io.spine.server.command.EventFactory in project core-java by SpineEventEngine.

the class EventsShould method setUp.

@Before
public void setUp() {
    final TestActorRequestFactory requestFactory = TestActorRequestFactory.newInstance(getClass());
    final Command cmd = requestFactory.command().create(Time.getCurrentTime());
    final StringValue producerId = Wrapper.forString(getClass().getSimpleName());
    EventFactory eventFactory = EventFactory.newBuilder().setCommandId(Commands.generateId()).setProducerId(producerId).setCommandContext(cmd.getContext()).build();
    event = eventFactory.createEvent(Time.getCurrentTime(), Tests.<Version>nullRef());
    context = event.getContext();
}
Also used : TestActorRequestFactory(io.spine.test.TestActorRequestFactory) Version(io.spine.base.Version) TestEventFactory(io.spine.test.TestEventFactory) EventFactory(io.spine.server.command.EventFactory) StringValue(com.google.protobuf.StringValue) Before(org.junit.Before)

Example 3 with EventFactory

use of io.spine.server.command.EventFactory in project core-java by SpineEventEngine.

the class Aggregate method applyMessages.

/**
     * Applies the passed event message or {@code Event} to the aggregate.
     *
     * @param eventMessages the event messages or events to apply
     * @param envelope      the envelope of the command which generated the events
     * @see #ensureEventMessage(Message)
     */
private void applyMessages(Iterable<? extends Message> eventMessages, CommandEnvelope envelope) {
    final List<? extends Message> messages = newArrayList(eventMessages);
    final EventFactory eventFactory = createEventFactory(envelope, messages.size());
    final List<Event> events = newArrayListWithCapacity(messages.size());
    Version projectedEventVersion = getVersion();
    for (Message eventOrMessage : messages) {
        // Applying each message would increment the entity version.
        // Therefore we should simulate this behaviour.
        projectedEventVersion = Versions.increment(projectedEventVersion);
        final Message eventMessage = ensureEventMessage(eventOrMessage);
        final Event event;
        if (eventOrMessage instanceof Event) {
            event = importEvent((Event) eventOrMessage, envelope.getCommandContext(), projectedEventVersion);
        } else {
            event = eventFactory.createEvent(eventMessage, projectedEventVersion);
        }
        events.add(event);
    }
    play(events);
    uncommittedEvents.addAll(events);
}
Also used : Events.getMessage(io.spine.base.Events.getMessage) EventApplierMethod.forEventMessage(io.spine.server.reflect.EventApplierMethod.forEventMessage) Message(com.google.protobuf.Message) Version(io.spine.base.Version) EventFactory(io.spine.server.command.EventFactory) Event(io.spine.base.Event)

Example 4 with EventFactory

use of io.spine.server.command.EventFactory in project core-java by SpineEventEngine.

the class Given method validEvent.

static Event validEvent() {
    final Command cmd = validCommand();
    final ProjectCreated eventMessage = ProjectCreated.newBuilder().setProjectId(ProjectId.newBuilder().setId("12345AD0")).build();
    final StringValue producerId = Wrapper.forString(Given.class.getSimpleName());
    final EventFactory eventFactory = EventFactory.newBuilder().setCommandId(cmd.getId()).setProducerId(producerId).setCommandContext(cmd.getContext()).build();
    final Event event = eventFactory.createEvent(eventMessage, Tests.<Version>nullRef());
    final Event result = event.toBuilder().setContext(event.getContext().toBuilder().setEnrichment(Enrichment.newBuilder().setDoNotEnrich(true)).build()).build();
    return result;
}
Also used : Command(io.spine.base.Command) EventFactory(io.spine.server.command.EventFactory) Event(io.spine.base.Event) StringValue(com.google.protobuf.StringValue) ProjectCreated(io.spine.test.projection.event.ProjectCreated)

Example 5 with EventFactory

use of io.spine.server.command.EventFactory in project core-java by SpineEventEngine.

the class CommandHandlerMethod method toEvents.

public static List<Event> toEvents(final Any producerId, @Nullable final Version version, final List<? extends Message> eventMessages, final CommandEnvelope envelope) {
    checkNotNull(producerId);
    checkNotNull(eventMessages);
    checkNotNull(envelope);
    final EventFactory eventFactory = EventFactory.newBuilder().setCommandId(envelope.getCommandId()).setProducerId(producerId).setMaxEventCount(eventMessages.size()).setCommandContext(envelope.getCommandContext()).build();
    return Lists.transform(eventMessages, new Function<Message, Event>() {

        @Override
        public Event apply(@Nullable Message eventMessage) {
            if (eventMessage == null) {
                return Event.getDefaultInstance();
            }
            final Event result = eventFactory.createEvent(eventMessage, version);
            return result;
        }
    });
}
Also used : Message(com.google.protobuf.Message) EventFactory(io.spine.server.command.EventFactory) Event(io.spine.base.Event)

Aggregations

EventFactory (io.spine.server.command.EventFactory)5 Event (io.spine.base.Event)4 StringValue (com.google.protobuf.StringValue)3 Message (com.google.protobuf.Message)2 Command (io.spine.base.Command)2 Version (io.spine.base.Version)2 Timestamp (com.google.protobuf.Timestamp)1 Events.getMessage (io.spine.base.Events.getMessage)1 EventApplierMethod.forEventMessage (io.spine.server.reflect.EventApplierMethod.forEventMessage)1 TestActorRequestFactory (io.spine.test.TestActorRequestFactory)1 TestEventFactory (io.spine.test.TestEventFactory)1 ProjectCreated (io.spine.test.projection.event.ProjectCreated)1 Before (org.junit.Before)1 Test (org.junit.Test)1