Search in sources :

Example 6 with Subscription

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

the class SubscriptionServiceShould method subscribe_to_topic.

/*
    * Subscription tests
    * ------------------
    */
@Test
public void subscribe_to_topic() {
    final BoundedContext boundedContext = setupBoundedContextWithProjectAggregateRepo();
    final SubscriptionService subscriptionService = SubscriptionService.newBuilder().add(boundedContext).build();
    final String type = boundedContext.getStand().getExposedTypes().iterator().next().value();
    final Target target = getProjectQueryTarget();
    assertEquals(type, target.getType());
    final Topic topic = requestFactory.topic().forTarget(target);
    final MemoizeStreamObserver<Subscription> observer = new MemoizeStreamObserver<>();
    subscriptionService.subscribe(topic, observer);
    assertNotNull(observer.streamFlowValue);
    assertTrue(observer.streamFlowValue.isInitialized());
    assertEquals(observer.streamFlowValue.getTopic().getTarget().getType(), type);
    assertNull(observer.throwable);
    assertTrue(observer.isCompleted);
}
Also used : Target(io.spine.client.Target) Topic(io.spine.client.Topic) Subscription(io.spine.client.Subscription) Test(org.junit.Test)

Example 7 with Subscription

use of io.spine.client.Subscription 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 8 with Subscription

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

the class Stand method subscribe.

/**
 * Subscribes for all further changes of an entity state, which satisfies the {@link Topic}.
 *
 * <p>Once this instance of {@code Stand} receives an update of an entity
 * with the given {@code TypeUrl}, all such callbacks are executed.
 *
 * @param topic an instance {@link Topic}, defining the entity and criteria,
 *              which changes should be propagated to the {@code callback}
 */
public void subscribe(final Topic topic, final StreamObserver<Subscription> responseObserver) {
    topicValidator.validate(topic, responseObserver);
    final TenantId tenantId = topic.getContext().getTenantId();
    final TenantAwareOperation op = new TenantAwareOperation(tenantId) {

        @Override
        public void run() {
            final Subscription subscription = subscriptionRegistry.add(topic);
            responseObserver.onNext(subscription);
            responseObserver.onCompleted();
        }
    };
    op.execute();
}
Also used : TenantId(io.spine.core.TenantId) Subscription(io.spine.client.Subscription) TenantAwareOperation(io.spine.server.tenant.TenantAwareOperation)

Example 9 with Subscription

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

the class SubscriptionValidator method checkInRegistry.

private boolean checkInRegistry(Subscription request) {
    final TenantId tenantId = request.getTopic().getContext().getTenantId();
    final Boolean result = new TenantAwareFunction<Subscription, Boolean>(tenantId) {

        @Override
        public Boolean apply(@Nullable Subscription input) {
            checkNotNull(input);
            final boolean result = registry.containsId(input.getId());
            return result;
        }
    }.execute(request);
    checkNotNull(result);
    return result;
}
Also used : TenantId(io.spine.core.TenantId) Subscription(io.spine.client.Subscription)

Example 10 with Subscription

use of io.spine.client.Subscription 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();
    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 = GivenVersion.withNumber(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.core.Response) Executor(java.util.concurrent.Executor) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.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)

Aggregations

Subscription (io.spine.client.Subscription)15 Test (org.junit.Test)12 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)7 Topic (io.spine.client.Topic)6 Target (io.spine.client.Target)4 Response (io.spine.core.Response)3 Message (com.google.protobuf.Message)2 SubscriptionUpdate (io.spine.client.SubscriptionUpdate)2 TenantId (io.spine.core.TenantId)2 Version (io.spine.core.Version)2 GivenVersion (io.spine.core.given.GivenVersion)2 AbstractVersionableEntity (io.spine.server.entity.AbstractVersionableEntity)2 VersionableEntity (io.spine.server.entity.VersionableEntity)2 ProjectId (io.spine.test.aggregate.ProjectId)2 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)1 CanIgnoreReturnValue (com.google.errorprone.annotations.CanIgnoreReturnValue)1 QueryResponse (io.spine.client.QueryResponse)1 SubscriptionId (io.spine.client.SubscriptionId)1 BoundedContext (io.spine.server.BoundedContext)1 StandTestProjectionRepository (io.spine.server.stand.Given.StandTestProjectionRepository)1