Search in sources :

Example 1 with SubscriptionUpdate

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

the class SubscriptionServiceShould method activate_subscription.

@Test
public void activate_subscription() {
    final BoundedContext boundedContext = setupBoundedContextWithProjectAggregateRepo();
    final SubscriptionService subscriptionService = SubscriptionService.newBuilder().add(boundedContext).build();
    final Target target = getProjectQueryTarget();
    final Topic topic = requestFactory.topic().forTarget(target);
    // Subscribe to the topic
    final MemoizeStreamObserver<Subscription> subscriptionObserver = new MemoizeStreamObserver<>();
    subscriptionService.subscribe(topic, subscriptionObserver);
    subscriptionObserver.verifyState();
    // Activate subscription
    final MemoizeStreamObserver<SubscriptionUpdate> activationObserver = new MemoizeStreamObserver<>();
    subscriptionService.activate(subscriptionObserver.streamFlowValue, activationObserver);
    // Post update to Stand directly
    final ProjectId projectId = ProjectId.newBuilder().setId("some-id").build();
    final Message projectState = Project.newBuilder().setId(projectId).build();
    final int version = 1;
    final VersionableEntity entity = mockEntity(projectId, projectState, version);
    boundedContext.getStand().post(requestFactory.createCommandContext().getActorContext().getTenantId(), entity);
    // isCompleted set to false since we don't expect activationObserver::onCompleted to be called.
    activationObserver.verifyState(false);
}
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) Target(io.spine.client.Target) Topic(io.spine.client.Topic) Subscription(io.spine.client.Subscription) Test(org.junit.Test)

Example 2 with SubscriptionUpdate

use of io.spine.client.SubscriptionUpdate 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(requestFactory.createCommandContext().getActorContext().getTenantId(), entity);
    // 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.core.Response) Target(io.spine.client.Target) Topic(io.spine.client.Topic) Subscription(io.spine.client.Subscription) Test(org.junit.Test)

Example 3 with SubscriptionUpdate

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

the class SubscriptionService method activate.

@Override
public void activate(final Subscription subscription, final StreamObserver<SubscriptionUpdate> responseObserver) {
    log().debug("Activating the subscription: {}", subscription);
    try {
        final BoundedContext boundedContext = selectBoundedContext(subscription);
        final Stand.EntityUpdateCallback updateCallback = new Stand.EntityUpdateCallback() {

            @Override
            public void onStateChanged(EntityStateUpdate stateUpdate) {
                checkNotNull(subscription);
                final SubscriptionUpdate update = SubscriptionUpdate.newBuilder().setSubscription(subscription).setResponse(Responses.ok()).addEntityStateUpdates(stateUpdate).build();
                responseObserver.onNext(update);
            }
        };
        final Stand targetStand = boundedContext.getStand();
        targetStand.activate(subscription, updateCallback, StreamObservers.<Response>forwardErrorsOnly(responseObserver));
    } catch (@SuppressWarnings("OverlyBroadCatchBlock") Exception e) {
        log().error("Error activating the subscription", e);
        responseObserver.onError(e);
    }
}
Also used : Stand(io.spine.server.stand.Stand) EntityStateUpdate(io.spine.client.EntityStateUpdate) SubscriptionUpdate(io.spine.client.SubscriptionUpdate)

Aggregations

SubscriptionUpdate (io.spine.client.SubscriptionUpdate)3 Message (com.google.protobuf.Message)2 Subscription (io.spine.client.Subscription)2 Target (io.spine.client.Target)2 Topic (io.spine.client.Topic)2 AbstractVersionableEntity (io.spine.server.entity.AbstractVersionableEntity)2 VersionableEntity (io.spine.server.entity.VersionableEntity)2 ProjectId (io.spine.test.aggregate.ProjectId)2 Test (org.junit.Test)2 EntityStateUpdate (io.spine.client.EntityStateUpdate)1 Response (io.spine.core.Response)1 Stand (io.spine.server.stand.Stand)1