use of io.spine.server.commandbus.CommandBus in project core-java by SpineEventEngine.
the class CommandRouterOnErrorShould method createRouter.
/**
* Creates a router with mocked {@code CommandBus} which always calls
* {@link StreamObserver#onError(Throwable) StreamObserver.onError()} when
* {@link CommandBus#post(Command, StreamObserver) CommandBus.post()} is invoked.
*/
@Override
CommandRouter createRouter(CommandBus ignored, Message sourceMessage, CommandContext commandContext) {
final CommandBus mockBus = mock(CommandBus.class);
doAnswer(new Answer() {
// is OK for Answer
@SuppressWarnings("ReturnOfNull")
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
final StreamObserver<Response> observer = invocation.getArgument(1);
observer.onError(new RuntimeException("simulate error"));
return null;
}
}).when(mockBus).post(any(Command.class), ArgumentMatchers.<StreamObserver<Response>>any());
return new CommandRouter(mockBus, sourceMessage, commandContext);
}
use of io.spine.server.commandbus.CommandBus in project core-java by SpineEventEngine.
the class ProcessManager method ensureCommandBus.
private CommandBus ensureCommandBus() {
final CommandBus commandBus = getCommandBus();
checkState(commandBus != null, "CommandBus must be initialized");
return commandBus;
}
use of io.spine.server.commandbus.CommandBus in project core-java by SpineEventEngine.
the class AbstractCommandRouterShould method setUp.
@Before
public void setUp() {
final BoundedContext boundedContext = BoundedContext.newBuilder().build();
final CommandBus commandBus = boundedContext.getCommandBus();
// Register dispatcher for `StringValue` message type.
// Otherwise we won't be able to post.
commandBus.register(new CommandDispatcher<String>() {
@Override
public Set<CommandClass> getMessageClasses() {
return CommandClass.setOf(StringValue.class);
}
@Override
public String dispatch(CommandEnvelope envelope) {
// Do nothing.
return "Anonymous";
}
@Override
public void onError(CommandEnvelope envelope, RuntimeException exception) {
// Do nothing.
}
});
sourceMessage = toMessage(getClass().getSimpleName());
sourceContext = requestFactory.createCommandContext();
router = createRouter(commandBus, sourceMessage, sourceContext);
router.addAll(messages);
}
use of io.spine.server.commandbus.CommandBus in project core-java by SpineEventEngine.
the class CommandRouterOnErrorShould method throw_IllegalStateException_when_caught_error_when_posting.
@Test(expected = IllegalStateException.class)
public void throw_IllegalStateException_when_caught_error_when_posting() {
final BoundedContext boundedContext = BoundedContext.newBuilder().build();
final CommandBus commandBus = boundedContext.getCommandBus();
// Register dispatcher for `StringValue` message type.
// Fails each time of dispatch().
commandBus.register(new CommandDispatcher<Message>() {
@Override
public Set<CommandClass> getMessageClasses() {
return CommandClass.setOf(StringValue.class);
}
@Override
public Message dispatch(CommandEnvelope envelope) {
throw new IllegalStateException("I am faulty!");
}
@Override
public void onError(CommandEnvelope envelope, RuntimeException exception) {
// Do nothing.
}
});
final StringValue sourceMessage = toMessage(getClass().getSimpleName());
final CommandContext sourceContext = getRequestFactory().createCommandContext();
final CommandRouter router = createRouter(commandBus, sourceMessage, sourceContext);
router.addAll(getMessages());
router.routeAll();
}
use of io.spine.server.commandbus.CommandBus in project core-java by SpineEventEngine.
the class AggregateRepositoryShould method allow_aggregates_react_on_rejections.
@Test
public void allow_aggregates_react_on_rejections() {
boundedContext.register(new RejectingRepository());
final RejectionReactingRepository repository = new RejectionReactingRepository();
boundedContext.register(repository);
final ProjectId parentId = givenAggregateId("rejectingParent");
final ProjectId childId1 = givenAggregateId("acceptingChild-1");
final ProjectId childId2 = givenAggregateId("acceptingChild-2");
final ProjectId childId3 = givenAggregateId("acceptingChild-3");
final StreamObserver<Ack> observer = StreamObservers.noOpObserver();
final CommandBus commandBus = boundedContext.getCommandBus();
// Create the parent project.
final ImmutableSet<ProjectId> childProjects = ImmutableSet.of(childId1, childId2, childId3);
final Command createParent = requestFactory.createCommand(AggCreateProjectWithChildren.newBuilder().setProjectId(parentId).addAllChildProjectId(childProjects).build());
commandBus.post(createParent, observer);
// Fire a command which would cause rejection.
final Command startProject = requestFactory.createCommand(AggStartProjectWithChildren.newBuilder().setProjectId(parentId).build());
commandBus.post(startProject, observer);
for (ProjectId childProject : childProjects) {
final Optional<RejectionReactingAggregate> optional = repository.find(childProject);
assertTrue(optional.isPresent());
// Check that all the aggregates:
// 1. got Rejections.AggCannotStartArchivedProject;
// 2. produced the state the event;
// 3. applied the event.
final String value = optional.get().getState().getValue();
assertEquals(RejectionReactingAggregate.PARENT_ARCHIVED, value);
}
}
Aggregations