Search in sources :

Example 6 with Topic

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

the class StandShould method throw_invalid_topic_ex_packed_as_IAE_if_subscribing_to_unknown_type_changes.

@Test
public void throw_invalid_topic_ex_packed_as_IAE_if_subscribing_to_unknown_type_changes() {
    final Stand stand = Stand.newBuilder().build();
    checkTypesEmpty(stand);
    // Customer type was NOT registered.
    // So create a topic for an unknown type.
    final Topic allCustomersTopic = requestFactory.topic().allOf(Customer.class);
    try {
        stand.subscribe(allCustomersTopic, StreamObservers.<Subscription>noOpObserver());
        fail("Expected IllegalArgumentException upon subscribing to a topic " + "with unknown target, but got nothing");
    } catch (IllegalArgumentException e) {
        verifyUnsupportedTopicException(allCustomersTopic, e);
    }
}
Also used : Topic(io.spine.client.Topic) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 7 with Topic

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

the class StandShould method trigger_subscription_callbacks_matching_by_id.

@Test
public void trigger_subscription_callbacks_matching_by_id() {
    final Stand stand = prepareStandWithAggregateRepo(mock(StandStorage.class));
    final Map<CustomerId, Customer> sampleCustomers = fillSampleCustomers(10);
    final Topic someCustomers = requestFactory.topic().someOf(Customer.class, sampleCustomers.keySet());
    final Set<Customer> callbackStates = newHashSet();
    final MemoizeEntityUpdateCallback callback = new MemoizeEntityUpdateCallback() {

        @Override
        public void onStateChanged(Any newEntityState) {
            super.onStateChanged(newEntityState);
            final Customer customerInCallback = AnyPacker.unpack(newEntityState);
            callbackStates.add(customerInCallback);
        }
    };
    subscribeAndActivate(stand, someCustomers, callback);
    for (Map.Entry<CustomerId, Customer> sampleEntry : sampleCustomers.entrySet()) {
        final CustomerId customerId = sampleEntry.getKey();
        final Customer customer = sampleEntry.getValue();
        final Version stateVersion = Tests.newVersionWithNumber(1);
        stand.update(asEnvelope(customerId, customer, stateVersion));
    }
    assertEquals(newHashSet(sampleCustomers.values()), callbackStates);
}
Also used : Customer(io.spine.test.commandservice.customer.Customer) Version(io.spine.base.Version) StandStorage(io.spine.server.stand.StandStorage) CustomerId(io.spine.test.commandservice.customer.CustomerId) Topic(io.spine.client.Topic) Any(com.google.protobuf.Any) Map(java.util.Map) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 8 with Topic

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

the class StandShould method allow_cancelling_subscriptions.

@Test
public void allow_cancelling_subscriptions() {
    final Stand stand = prepareStandWithAggregateRepo(mock(StandStorage.class));
    final Topic allCustomers = requestFactory.topic().allOf(Customer.class);
    final MemoizeEntityUpdateCallback memoizeCallback = new MemoizeEntityUpdateCallback();
    final Subscription subscription = subscribeAndActivate(stand, allCustomers, memoizeCallback);
    stand.cancel(subscription, StreamObservers.<Response>noOpObserver());
    final Map.Entry<CustomerId, Customer> sampleData = fillSampleCustomers(1).entrySet().iterator().next();
    final CustomerId customerId = sampleData.getKey();
    final Customer customer = sampleData.getValue();
    final Version stateVersion = Tests.newVersionWithNumber(1);
    stand.update(asEnvelope(customerId, customer, stateVersion));
    assertNull(memoizeCallback.newEntityState);
}
Also used : Customer(io.spine.test.commandservice.customer.Customer) Version(io.spine.base.Version) StandStorage(io.spine.server.stand.StandStorage) CustomerId(io.spine.test.commandservice.customer.CustomerId) Topic(io.spine.client.Topic) Subscription(io.spine.client.Subscription) Map(java.util.Map) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 9 with Topic

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

the class StandShould method throw_invalid_topic_exception_packed_as_IAE_if_invalid_topic_message_passed.

@Test
public void throw_invalid_topic_exception_packed_as_IAE_if_invalid_topic_message_passed() {
    final Stand stand = Stand.newBuilder().build();
    final Topic invalidTopic = Topic.getDefaultInstance();
    try {
        stand.subscribe(invalidTopic, StreamObservers.<Subscription>noOpObserver());
        fail("Expected IllegalArgumentException due to an invalid topic message, " + "but got nothing");
    } catch (IllegalArgumentException e) {
        verifyInvalidTopicException(invalidTopic, e);
    }
}
Also used : Topic(io.spine.client.Topic) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 10 with Topic

use of io.spine.client.Topic 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)

Aggregations

Topic (io.spine.client.Topic)13 Test (org.junit.Test)11 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)7 Subscription (io.spine.client.Subscription)6 Version (io.spine.base.Version)5 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)4 Target (io.spine.client.Target)4 StandStorage (io.spine.server.stand.StandStorage)4 Map (java.util.Map)4 Any (com.google.protobuf.Any)3 Response (io.spine.base.Response)3 Customer (io.spine.test.commandservice.customer.Customer)3 CustomerId (io.spine.test.commandservice.customer.CustomerId)3 Message (com.google.protobuf.Message)2 SubscriptionUpdate (io.spine.client.SubscriptionUpdate)2 AbstractVersionableEntity (io.spine.server.entity.AbstractVersionableEntity)2 VersionableEntity (io.spine.server.entity.VersionableEntity)2 ProjectId (io.spine.test.aggregate.ProjectId)2 ProjectId (io.spine.test.projection.ProjectId)2 QueryResponse (io.spine.client.QueryResponse)1