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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations