Search in sources :

Example 1 with CommandBus

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);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Response(io.spine.base.Response) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) CommandRouter(io.spine.server.procman.CommandRouter) Command(io.spine.base.Command) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CommandBus(io.spine.server.commandbus.CommandBus)

Example 2 with CommandBus

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;
}
Also used : CommandBus(io.spine.server.commandbus.CommandBus)

Example 3 with 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);
}
Also used : Set(java.util.Set) CommandEnvelope(io.spine.core.CommandEnvelope) BoundedContext(io.spine.server.BoundedContext) CommandBus(io.spine.server.commandbus.CommandBus) StringValue(com.google.protobuf.StringValue) Before(org.junit.Before)

Example 4 with CommandBus

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();
}
Also used : Set(java.util.Set) Message(com.google.protobuf.Message) TypeConverter.toMessage(io.spine.protobuf.TypeConverter.toMessage) CommandContext(io.spine.core.CommandContext) CommandEnvelope(io.spine.core.CommandEnvelope) BoundedContext(io.spine.server.BoundedContext) CommandBus(io.spine.server.commandbus.CommandBus) StringValue(com.google.protobuf.StringValue) Test(org.junit.Test)

Example 5 with CommandBus

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);
    }
}
Also used : RejectingRepository(io.spine.server.aggregate.given.AggregateRepositoryTestEnv.RejectingRepository) RejectionReactingRepository(io.spine.server.aggregate.given.AggregateRepositoryTestEnv.RejectionReactingRepository) Command(io.spine.core.Command) RejectionReactingAggregate(io.spine.server.aggregate.given.AggregateRepositoryTestEnv.RejectionReactingAggregate) ProjectId(io.spine.test.aggregate.ProjectId) Ack(io.spine.core.Ack) CommandBus(io.spine.server.commandbus.CommandBus) Test(org.junit.Test)

Aggregations

CommandBus (io.spine.server.commandbus.CommandBus)18 Test (org.junit.Test)10 Ack (io.spine.core.Ack)7 Command (io.spine.core.Command)7 TenantId (io.spine.core.TenantId)6 ProjectId (io.spine.test.aggregate.ProjectId)4 AggregateMessageDispatcher.dispatchCommand (io.spine.server.aggregate.AggregateMessageDispatcher.dispatchCommand)3 IdempotencyGuardTestEnv.newProjectId (io.spine.server.aggregate.given.IdempotencyGuardTestEnv.newProjectId)3 IdempotencyGuardTestEnv.newTenantId (io.spine.server.aggregate.given.IdempotencyGuardTestEnv.newTenantId)3 AggregateTestEnv.newTenantId (io.spine.server.aggregate.given.aggregate.AggregateTestEnv.newTenantId)3 IgTestAggregate (io.spine.server.aggregate.given.aggregate.IgTestAggregate)3 StringValue (com.google.protobuf.StringValue)2 CommandEnvelope (io.spine.core.CommandEnvelope)2 Event (io.spine.core.Event)2 BoundedContext (io.spine.server.BoundedContext)2 TestAggregate (io.spine.server.aggregate.given.aggregate.TestAggregate)2 Set (java.util.Set)2 Message (com.google.protobuf.Message)1 StreamObserver (io.grpc.stub.StreamObserver)1 Command (io.spine.base.Command)1