Search in sources :

Example 16 with Command

use of io.spine.base.Command 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 17 with Command

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

the class TypeUrlShould method obtain_type_of_command.

@Test
public void obtain_type_of_command() {
    final ActorRequestFactory factory = TestActorRequestFactory.newInstance(TypeUrlShould.class);
    final StringValue message = Wrapper.forString(newUuid());
    final Command command = factory.command().create(message);
    final TypeUrl typeUrl = TypeUrl.ofCommand(command);
    assertIsStringValueUrl(typeUrl);
}
Also used : Command(io.spine.base.Command) ActorRequestFactory(io.spine.client.ActorRequestFactory) TestActorRequestFactory(io.spine.test.TestActorRequestFactory) TypeUrl.composeTypeUrl(io.spine.type.TypeUrl.composeTypeUrl) StringValue(com.google.protobuf.StringValue) Test(org.junit.Test)

Example 18 with Command

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

the class CommandEnvelopeShould method have_equals.

@Test
public void have_equals() {
    final Command anotherCommand = requestFactory.command().create(Time.getCurrentTime());
    new EqualsTester().addEqualityGroup(envelope).addEqualityGroup(CommandEnvelope.of(anotherCommand)).testEquals();
}
Also used : Command(io.spine.base.Command) EqualsTester(com.google.common.testing.EqualsTester) Test(org.junit.Test)

Example 19 with Command

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

the class CommandClass method of.

/**
     * Creates a new instance for the class of the passed command.
     *
     * <p>If an instance of {@link Command} (which implements {@code Message}) is
     * passed to this method, enclosing command message will be un-wrapped to
     * determine the class of the command.
     *
     * @param command a command for which to get the class
     * @return new instance
     */
public static CommandClass of(Message command) {
    checkNotNull(command);
    if (command instanceof Command) {
        final Command commandRequest = (Command) command;
        final Message enclosed = Commands.getMessage(commandRequest);
        return of(enclosed.getClass());
    }
    final CommandClass result = of(command.getClass());
    return result;
}
Also used : Message(com.google.protobuf.Message) Command(io.spine.base.Command)

Example 20 with Command

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

the class DeadCommandFilter method accept.

@Override
public boolean accept(CommandEnvelope envelope, StreamObserver<Response> responseObserver) {
    if (!hasDispatcher(envelope.getMessageClass())) {
        final Command command = envelope.getCommand();
        final CommandException unsupported = new UnsupportedCommandException(command);
        commandBus.commandStore().storeWithError(command, unsupported);
        responseObserver.onError(invalidArgumentWithCause(unsupported, unsupported.getError()));
        return false;
    }
    return true;
}
Also used : Command(io.spine.base.Command)

Aggregations

Command (io.spine.base.Command)24 Test (org.junit.Test)14 StringValue (com.google.protobuf.StringValue)6 Message (com.google.protobuf.Message)4 ConstraintViolation (io.spine.validate.ConstraintViolation)4 Timestamp (com.google.protobuf.Timestamp)3 TestActorRequestFactory (io.spine.test.TestActorRequestFactory)3 TenantId (io.spine.users.TenantId)3 Event (io.spine.base.Event)2 Response (io.spine.base.Response)2 CommandOperation (io.spine.server.tenant.CommandOperation)2 EqualsTester (com.google.common.testing.EqualsTester)1 Any (com.google.protobuf.Any)1 StreamObserver (io.grpc.stub.StreamObserver)1 ActorContext (io.spine.base.ActorContext)1 CommandContext (io.spine.base.CommandContext)1 CommandId (io.spine.base.CommandId)1 Commands.getMessage (io.spine.base.Commands.getMessage)1 Failure (io.spine.base.Failure)1 StringChange (io.spine.change.StringChange)1