Search in sources :

Example 16 with CommandEnvelope

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

the class CommandHandlerMethodShould method set_producer_ID_if_entity.

@Test
public void set_producer_ID_if_entity() {
    final RefCreateProject commandMessage = createProject();
    final Aggregate<ProjectId, ?, ?> entity = new RejectingAggregate(commandMessage.getProjectId());
    final CommandEnvelope cmd = requestFactory.createEnvelope(commandMessage);
    try {
        AggregateMessageDispatcher.dispatchCommand(entity, cmd);
    } catch (HandlerMethodFailedException e) {
        assertCauseAndId(e, entity.getId());
    }
}
Also used : RefCreateProject(io.spine.test.reflect.command.RefCreateProject) HandlerMethodFailedException(io.spine.server.model.HandlerMethodFailedException) RejectingAggregate(io.spine.server.command.given.CommandHandlerMethodTestEnv.RejectingAggregate) ProjectId(io.spine.test.reflect.ProjectId) CommandEnvelope(io.spine.core.CommandEnvelope) Test(org.junit.Test)

Example 17 with CommandEnvelope

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

the class CommandHandlerMethodShould method set_producer_ID_if_command_handler.

@Test
public void set_producer_ID_if_command_handler() {
    final CommandHandler handler = new RejectingHandler();
    final CommandEnvelope envelope = requestFactory.createEnvelope(createProject());
    try {
        handler.dispatch(envelope);
    } catch (HandlerMethodFailedException e) {
        assertCauseAndId(e, handler.getId());
    }
}
Also used : HandlerMethodFailedException(io.spine.server.model.HandlerMethodFailedException) CommandEnvelope(io.spine.core.CommandEnvelope) RejectingHandler(io.spine.server.command.given.CommandHandlerMethodTestEnv.RejectingHandler) Test(org.junit.Test)

Example 18 with CommandEnvelope

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

the class CommandHandlerMethodShould method throw_ISE_for_not_handled_command_type.

@Test(expected = IllegalStateException.class)
public void throw_ISE_for_not_handled_command_type() {
    final CommandHandler handler = new ValidHandlerOneParam();
    final CommandEnvelope cmd = requestFactory.createEnvelope(startProject());
    handler.dispatch(cmd);
}
Also used : ValidHandlerOneParam(io.spine.server.command.given.CommandHandlerMethodTestEnv.ValidHandlerOneParam) CommandEnvelope(io.spine.core.CommandEnvelope) Test(org.junit.Test)

Example 19 with CommandEnvelope

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

the class AbstractCommandBusTestSuite method post_commands_in_bulk.

@Test
public void post_commands_in_bulk() {
    final Command first = newCommand();
    final Command second = newCommand();
    final List<Command> commands = newArrayList(first, second);
    // Some derived test suite classes may register the handler in setUp().
    // This prevents the repeating registration (which is an illegal operation).
    commandBus.unregister(createProjectHandler);
    commandBus.register(createProjectHandler);
    final CommandBus spy = spy(commandBus);
    spy.post(commands, StreamObservers.<Ack>memoizingObserver());
    @SuppressWarnings("unchecked") final ArgumentCaptor<Iterable<Command>> storingCaptor = forClass(Iterable.class);
    verify(spy).store(storingCaptor.capture());
    final Iterable<Command> storingArgs = storingCaptor.getValue();
    assertSize(commands.size(), storingArgs);
    assertContainsAll(storingArgs, first, second);
    final ArgumentCaptor<CommandEnvelope> postingCaptor = forClass(CommandEnvelope.class);
    verify(spy, times(2)).doPost(postingCaptor.capture());
    final List<CommandEnvelope> postingArgs = postingCaptor.getAllValues();
    assertSize(commands.size(), postingArgs);
    assertEquals(commands.get(0), postingArgs.get(0).getCommand());
    assertEquals(commands.get(1), postingArgs.get(1).getCommand());
    commandBus.unregister(createProjectHandler);
}
Also used : Command(io.spine.core.Command) CommandEnvelope(io.spine.core.CommandEnvelope) TenantAwareTest(io.spine.server.tenant.TenantAwareTest) Test(org.junit.Test)

Example 20 with CommandEnvelope

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

the class CommandHandlerShould method log_errors.

@Test
public void log_errors() {
    final CommandEnvelope commandEnvelope = givenCommandEnvelope();
    // Since we're in the tests mode `Environment` returns `SubstituteLogger` instance.
    final SubstituteLogger log = (SubstituteLogger) handler.log();
    // Restrict the queue size only to the number of calls we want to make.
    final Queue<SubstituteLoggingEvent> queue = Queues.newArrayBlockingQueue(1);
    log.setDelegate(new EventRecodingLogger(log, queue));
    SubstituteLoggingEvent loggingEvent;
    final RuntimeException exception = new RuntimeException("log_errors");
    handler.onError(commandEnvelope, exception);
    loggingEvent = queue.poll();
    assertEquals(Level.ERROR, loggingEvent.getLevel());
    assertEquals(commandEnvelope, handler.getLastErrorEnvelope());
    assertEquals(exception, handler.getLastException());
}
Also used : SubstituteLogger(org.slf4j.helpers.SubstituteLogger) SubstituteLoggingEvent(org.slf4j.event.SubstituteLoggingEvent) EventRecodingLogger(org.slf4j.event.EventRecodingLogger) CommandEnvelope(io.spine.core.CommandEnvelope) Test(org.junit.Test)

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