Search in sources :

Example 1 with Response

use of io.spine.base.Response 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 Response

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

the class SubscriptionServiceShould method handle_cancellation_process_exceptions_and_call_observer_error_callback.

@Test
public void handle_cancellation_process_exceptions_and_call_observer_error_callback() {
    final BoundedContext boundedContext = setupBoundedContextWithProjectAggregateRepo();
    final SubscriptionService subscriptionService = SubscriptionService.newBuilder().add(boundedContext).build();
    final Target target = getProjectQueryTarget();
    final Topic topic = requestFactory.topic().forTarget(target);
    final MemoizeStreamObserver<Subscription> subscriptionObserver = new MemoizeStreamObserver<>();
    subscriptionService.subscribe(topic, subscriptionObserver);
    final String failureMessage = "Execution breaking exception";
    final MemoizeStreamObserver<Response> observer = new MemoizeStreamObserver<Response>() {

        @Override
        public void onNext(Response value) {
            super.onNext(value);
            throw new RuntimeException(failureMessage);
        }
    };
    subscriptionService.cancel(subscriptionObserver.streamFlowValue, observer);
    assertNotNull(observer.streamFlowValue);
    assertFalse(observer.isCompleted);
    assertNotNull(observer.throwable);
    assertInstanceOf(RuntimeException.class, observer.throwable);
    assertEquals(observer.throwable.getMessage(), failureMessage);
}
Also used : Response(io.spine.base.Response) Target(io.spine.client.Target) Topic(io.spine.client.Topic) Subscription(io.spine.client.Subscription) Test(org.junit.Test)

Example 3 with Response

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

the class StandShould method use_provided_executor_upon_update_of_watched_type.

@Test
public void use_provided_executor_upon_update_of_watched_type() {
    final Executor executor = mock(Executor.class);
    final BoundedContext boundedContext = BoundedContext.newBuilder().setStand(Stand.newBuilder().setCallbackExecutor(executor)).build();
    final Stand stand = boundedContext.getStand();
    final StandTestProjectionRepository standTestProjectionRepo = new StandTestProjectionRepository(boundedContext);
    stand.registerTypeSupplier(standTestProjectionRepo);
    final Topic projectProjections = requestFactory.topic().allOf(Project.class);
    final MemoizingObserver<Subscription> observer = memoizingObserver();
    stand.subscribe(projectProjections, observer);
    final Subscription subscription = observer.firstResponse();
    final StreamObserver<Response> noopObserver = noOpObserver();
    stand.activate(subscription, emptyUpdateCallback(), noopObserver);
    assertNotNull(subscription);
    verify(executor, never()).execute(any(Runnable.class));
    final ProjectId someId = ProjectId.getDefaultInstance();
    final Version stateVersion = Tests.newVersionWithNumber(1);
    stand.update(asEnvelope(someId, Project.getDefaultInstance(), stateVersion));
    verify(executor, times(1)).execute(any(Runnable.class));
}
Also used : QueryResponse(io.spine.client.QueryResponse) Response(io.spine.base.Response) Executor(java.util.concurrent.Executor) Version(io.spine.base.Version) ProjectId(io.spine.test.projection.ProjectId) BoundedContext(io.spine.server.BoundedContext) StandTestProjectionRepository(io.spine.server.stand.Given.StandTestProjectionRepository) Topic(io.spine.client.Topic) Subscription(io.spine.client.Subscription) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 4 with Response

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

the class SubscriptionServiceShould method cancel_subscription_on_topic.

@Test
public void cancel_subscription_on_topic() {
    final BoundedContext boundedContext = setupBoundedContextWithProjectAggregateRepo();
    final SubscriptionService subscriptionService = SubscriptionService.newBuilder().add(boundedContext).build();
    final Target target = getProjectQueryTarget();
    final Topic topic = requestFactory.topic().forTarget(target);
    // Subscribe
    final MemoizeStreamObserver<Subscription> subscribeObserver = new MemoizeStreamObserver<>();
    subscriptionService.subscribe(topic, subscribeObserver);
    // Activate subscription
    final MemoizeStreamObserver<SubscriptionUpdate> activateSubscription = spy(new MemoizeStreamObserver<SubscriptionUpdate>());
    subscriptionService.activate(subscribeObserver.streamFlowValue, activateSubscription);
    // Cancel subscription
    subscriptionService.cancel(subscribeObserver.streamFlowValue, new MemoizeStreamObserver<Response>());
    // Post update to Stand
    final ProjectId projectId = ProjectId.newBuilder().setId("some-other-id").build();
    final Message projectState = Project.newBuilder().setId(projectId).build();
    final int version = 1;
    final VersionableEntity entity = mockEntity(projectId, projectState, version);
    boundedContext.getStand().post(entity, requestFactory.createCommandContext());
    // The update must not be handled by the observer
    verify(activateSubscription, never()).onNext(any(SubscriptionUpdate.class));
    verify(activateSubscription, never()).onCompleted();
}
Also used : Message(com.google.protobuf.Message) ProjectId(io.spine.test.aggregate.ProjectId) SubscriptionUpdate(io.spine.client.SubscriptionUpdate) AbstractVersionableEntity(io.spine.server.entity.AbstractVersionableEntity) VersionableEntity(io.spine.server.entity.VersionableEntity) Response(io.spine.base.Response) Target(io.spine.client.Target) Topic(io.spine.client.Topic) Subscription(io.spine.client.Subscription) Test(org.junit.Test)

Example 5 with Response

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

the class SingleTenantCommandBusShould method propagate_failures_to_failure_bus.

@Test
public void propagate_failures_to_failure_bus() {
    final FaultyHandler faultyHandler = new FaultyHandler(eventBus);
    commandBus.register(faultyHandler);
    final Command addTaskCommand = clearTenantId(addTask());
    commandBus.post(addTaskCommand, StreamObservers.<Response>noOpObserver());
    final InvalidProjectName failureThrowable = faultyHandler.getThrowable();
    final Failure expectedFailure = failureThrowable.toFailure(addTaskCommand);
    verify(failureBus).post(eq(expectedFailure), ArgumentMatchers.<StreamObserver<Response>>any());
}
Also used : Response(io.spine.base.Response) Command(io.spine.base.Command) InvalidProjectName(io.spine.test.failure.InvalidProjectName) Failure(io.spine.base.Failure) Test(org.junit.Test)

Aggregations

Response (io.spine.base.Response)6 Test (org.junit.Test)5 Subscription (io.spine.client.Subscription)3 Topic (io.spine.client.Topic)3 Command (io.spine.base.Command)2 Target (io.spine.client.Target)2 Message (com.google.protobuf.Message)1 StreamObserver (io.grpc.stub.StreamObserver)1 Failure (io.spine.base.Failure)1 Version (io.spine.base.Version)1 QueryResponse (io.spine.client.QueryResponse)1 SubscriptionUpdate (io.spine.client.SubscriptionUpdate)1 BoundedContext (io.spine.server.BoundedContext)1 CommandBus (io.spine.server.commandbus.CommandBus)1 AbstractVersionableEntity (io.spine.server.entity.AbstractVersionableEntity)1 VersionableEntity (io.spine.server.entity.VersionableEntity)1 CommandRouter (io.spine.server.procman.CommandRouter)1 StandTestProjectionRepository (io.spine.server.stand.Given.StandTestProjectionRepository)1 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)1 ProjectId (io.spine.test.aggregate.ProjectId)1