Search in sources :

Example 31 with CommandEnvelope

use of io.spine.core.CommandEnvelope in project core-java by SpineEventEngine.

the class CommandRoutingShould method apply_custom_route.

@Test
public void apply_custom_route() {
    final TestActorRequestFactory factory = TestActorRequestFactory.newInstance(getClass());
    // Have custom route.
    commandRouting.route(StringValue.class, customRoute);
    final CommandEnvelope command = factory.generateEnvelope();
    final long id = commandRouting.apply(command.getMessage(), command.getCommandContext());
    assertEquals(CUSTOM_ANSWER, id);
}
Also used : TestActorRequestFactory(io.spine.client.TestActorRequestFactory) CommandEnvelope(io.spine.core.CommandEnvelope) Test(org.junit.Test)

Example 32 with CommandEnvelope

use of io.spine.core.CommandEnvelope in project core-java by SpineEventEngine.

the class CommandRoutingShould method apply_default_route.

@Test
public void apply_default_route() {
    final TestActorRequestFactory factory = TestActorRequestFactory.newInstance(getClass());
    // Replace the default route since we have custom command message.
    commandRouting.replaceDefault(customDefault).route(StringValue.class, customRoute);
    final CommandEnvelope command = CommandEnvelope.of(factory.createCommand(Time.getCurrentTime()));
    final long id = commandRouting.apply(command.getMessage(), command.getCommandContext());
    assertEquals(DEFAULT_ANSWER, id);
}
Also used : TestActorRequestFactory(io.spine.client.TestActorRequestFactory) CommandEnvelope(io.spine.core.CommandEnvelope) Test(org.junit.Test)

Example 33 with CommandEnvelope

use of io.spine.core.CommandEnvelope in project core-java by SpineEventEngine.

the class Aggregate method apply.

/**
 * Applies event messages.
 *
 * @param eventMessages the event messages or events to apply
 * @param origin        the envelope of a message which caused the events
 * @see #ensureEventMessage(Message)
 */
void apply(Iterable<? extends Message> eventMessages, MessageEnvelope origin) {
    final List<? extends Message> messages = newArrayList(eventMessages);
    final EventFactory eventFactory = EventFactory.on(origin, getProducerId());
    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) {
            /* If we get instances of Event, it means we are dealing with an import command,
                   which contains these events in the body. So we deal with a command envelope.
                */
            final CommandEnvelope ce = (CommandEnvelope) origin;
            event = importEvent((Event) eventOrMessage, ce.getCommandContext(), projectedEventVersion);
        } else {
            event = eventFactory.createEvent(eventMessage, projectedEventVersion);
        }
        events.add(event);
    }
    play(events);
    uncommittedEvents.addAll(events);
}
Also used : Events.getMessage(io.spine.core.Events.getMessage) Message(com.google.protobuf.Message) Version(io.spine.core.Version) EventFactory(io.spine.server.event.EventFactory) Event(io.spine.core.Event) CommandEnvelope(io.spine.core.CommandEnvelope)

Example 34 with CommandEnvelope

use of io.spine.core.CommandEnvelope in project core-java by SpineEventEngine.

the class CommandBus method postPreviouslyScheduled.

/**
 * Passes a previously scheduled command to the corresponding dispatcher.
 */
void postPreviouslyScheduled(Command command) {
    final CommandEnvelope commandEnvelope = CommandEnvelope.of(command);
    doPost(commandEnvelope);
}
Also used : CommandEnvelope(io.spine.core.CommandEnvelope)

Example 35 with CommandEnvelope

use of io.spine.core.CommandEnvelope in project core-java by SpineEventEngine.

the class CommandException method messageFormat.

/**
 * Builds a formatted string for the passed format and a command.
 *
 * <p>The first parameter of the formatted string is a {@link ClassName} of the command message.
 *
 * <p>The second parameter is a {@link TypeName} of the command message.
 */
protected static String messageFormat(String format, Command command) {
    final CommandEnvelope envelope = CommandEnvelope.of(command);
    final ClassName commandClass = envelope.getMessageClass().getClassName();
    final TypeName typeName = envelope.getTypeName();
    final String result = format(format, commandClass, typeName);
    return result;
}
Also used : TypeName(io.spine.type.TypeName) ClassName(io.spine.type.ClassName) CommandEnvelope(io.spine.core.CommandEnvelope)

Aggregations

CommandEnvelope (io.spine.core.CommandEnvelope)40 Test (org.junit.Test)25 Command (io.spine.core.Command)11 Message (com.google.protobuf.Message)7 Error (io.spine.base.Error)5 TestActorRequestFactory (io.spine.client.TestActorRequestFactory)4 StringValue (com.google.protobuf.StringValue)3 CommandValidationError (io.spine.core.CommandValidationError)3 Event (io.spine.core.Event)3 BoundedContext (io.spine.server.BoundedContext)3 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)3 CommandId (io.spine.core.CommandId)2 Commands.getMessage (io.spine.core.Commands.getMessage)2 TypeConverter.toMessage (io.spine.protobuf.TypeConverter.toMessage)2 CommandBus (io.spine.server.commandbus.CommandBus)2 HandlerMethodFailedException (io.spine.server.model.HandlerMethodFailedException)2 AggCreateProject (io.spine.test.aggregate.command.AggCreateProject)2 ProjectId (io.spine.test.procman.ProjectId)2 TypeName (io.spine.type.TypeName)2 Set (java.util.Set)2