Search in sources :

Example 6 with CustomerId

use of io.spine.test.commandservice.customer.CustomerId 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 Map<CustomerId, Customer> callbackStates = newHashMap();
    final MemoizeEntityUpdateCallback callback = new MemoizeEntityUpdateCallback() {

        @Override
        public void onStateChanged(EntityStateUpdate update) {
            super.onStateChanged(update);
            final Customer customerInCallback = unpack(update.getState());
            final CustomerId customerIdInCallback = unpack(update.getId());
            callbackStates.put(customerIdInCallback, 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 = GivenVersion.withNumber(1);
        stand.update(asEnvelope(customerId, customer, stateVersion));
    }
    assertEquals(newHashMap(sampleCustomers), callbackStates);
}
Also used : Customer(io.spine.test.commandservice.customer.Customer) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.Version) EntityStateUpdate(io.spine.client.EntityStateUpdate) CustomerId(io.spine.test.commandservice.customer.CustomerId) Topic(io.spine.client.Topic) Map(java.util.Map) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 7 with CustomerId

use of io.spine.test.commandservice.customer.CustomerId in project core-java by SpineEventEngine.

the class StandShould method trigger_each_subscription_callback_once_for_multiple_subscriptions.

@SuppressWarnings("MethodWithMultipleLoops")
@Test
public void trigger_each_subscription_callback_once_for_multiple_subscriptions() {
    final Stand stand = prepareStandWithAggregateRepo(mock(StandStorage.class));
    final Target allCustomers = Targets.allOf(Customer.class);
    final Set<MemoizeEntityUpdateCallback> callbacks = newHashSet();
    final int totalCallbacks = 100;
    for (int callbackIndex = 0; callbackIndex < totalCallbacks; callbackIndex++) {
        final MemoizeEntityUpdateCallback callback = subscribeWithCallback(stand, allCustomers);
        callbacks.add(callback);
    }
    final Map.Entry<CustomerId, Customer> sampleData = fillSampleCustomers(1).entrySet().iterator().next();
    final CustomerId customerId = sampleData.getKey();
    final Customer customer = sampleData.getValue();
    final Version stateVersion = GivenVersion.withNumber(1);
    stand.update(asEnvelope(customerId, customer, stateVersion));
    final Any packedState = AnyPacker.pack(customer);
    for (MemoizeEntityUpdateCallback callback : callbacks) {
        assertEquals(packedState, callback.newEntityState);
        verify(callback, times(1)).onStateChanged(any(EntityStateUpdate.class));
    }
}
Also used : Customer(io.spine.test.commandservice.customer.Customer) CustomerId(io.spine.test.commandservice.customer.CustomerId) Any(com.google.protobuf.Any) Target(io.spine.client.Target) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.Version) EntityStateUpdate(io.spine.client.EntityStateUpdate) 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 CustomerId

use of io.spine.test.commandservice.customer.CustomerId in project core-java by SpineEventEngine.

the class StandShould method do_not_trigger_subscription_callbacks_in_case_of_another_type_criterion_mismatch.

@Test
public void do_not_trigger_subscription_callbacks_in_case_of_another_type_criterion_mismatch() {
    final Stand stand = prepareStandWithAggregateRepo(mock(StandStorage.class));
    final Target allProjects = Targets.allOf(Project.class);
    final MemoizeEntityUpdateCallback callback = subscribeWithCallback(stand, allProjects);
    final Map.Entry<CustomerId, Customer> sampleData = fillSampleCustomers(1).entrySet().iterator().next();
    final CustomerId customerId = sampleData.getKey();
    final Customer customer = sampleData.getValue();
    final Version stateVersion = GivenVersion.withNumber(1);
    stand.update(asEnvelope(customerId, customer, stateVersion));
    verify(callback, never()).onStateChanged(any(EntityStateUpdate.class));
}
Also used : Target(io.spine.client.Target) Customer(io.spine.test.commandservice.customer.Customer) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.Version) EntityStateUpdate(io.spine.client.EntityStateUpdate) CustomerId(io.spine.test.commandservice.customer.CustomerId) 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 CustomerId

use of io.spine.test.commandservice.customer.CustomerId in project core-java by SpineEventEngine.

the class StandShould method fillSampleCustomers.

protected static Map<CustomerId, Customer> fillSampleCustomers(int numberOfCustomers) {
    final Map<CustomerId, Customer> sampleCustomers = newHashMap();
    @SuppressWarnings("UnsecureRandomNumberGeneration") final Random randomizer = new Random(Integer.MAX_VALUE);
    for (int customerIndex = 0; customerIndex < numberOfCustomers; customerIndex++) {
        final int numericId = randomizer.nextInt();
        final CustomerId customerId = customerIdFor(numericId);
        final Customer customer = Customer.newBuilder().setName(PersonName.newBuilder().setGivenName(String.valueOf(numericId))).build();
        sampleCustomers.put(customerId, customer);
    }
    return sampleCustomers;
}
Also used : Random(java.util.Random) Customer(io.spine.test.commandservice.customer.Customer) CustomerId(io.spine.test.commandservice.customer.CustomerId)

Example 10 with CustomerId

use of io.spine.test.commandservice.customer.CustomerId in project core-java by SpineEventEngine.

the class MultiTenantStandShould method not_trigger_updates_of_aggregate_records_for_another_tenant_subscriptions.

@Test
public void not_trigger_updates_of_aggregate_records_for_another_tenant_subscriptions() {
    final StandStorage standStorage = InMemoryStorageFactory.newInstance(newName(getClass().getSimpleName()), isMultitenant()).createStandStorage();
    final Stand stand = prepareStandWithAggregateRepo(standStorage);
    // --- Default Tenant
    final ActorRequestFactory requestFactory = getRequestFactory();
    final MemoizeEntityUpdateCallback defaultTenantCallback = subscribeToAllOf(stand, requestFactory, Customer.class);
    // --- Another Tenant
    final TenantId anotherTenant = newUuid();
    final ActorRequestFactory anotherFactory = createRequestFactory(anotherTenant);
    final MemoizeEntityUpdateCallback anotherCallback = subscribeToAllOf(stand, anotherFactory, Customer.class);
    // Trigger updates in Default Tenant.
    final Map.Entry<CustomerId, Customer> sampleData = fillSampleCustomers(1).entrySet().iterator().next();
    final CustomerId customerId = sampleData.getKey();
    final Customer customer = sampleData.getValue();
    final Version stateVersion = GivenVersion.withNumber(1);
    stand.update(asEnvelope(customerId, customer, stateVersion));
    final Any packedState = AnyPacker.pack(customer);
    // Verify that Default Tenant callback has got the update.
    assertEquals(packedState, defaultTenantCallback.getNewEntityState());
    // And Another Tenant callback has not been called.
    assertEquals(null, anotherCallback.getNewEntityState());
}
Also used : TenantId(io.spine.core.TenantId) Customer(io.spine.test.commandservice.customer.Customer) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.Version) ActorRequestFactory(io.spine.client.ActorRequestFactory) CustomerId(io.spine.test.commandservice.customer.CustomerId) Map(java.util.Map) Any(com.google.protobuf.Any) Test(org.junit.Test)

Aggregations

Customer (io.spine.test.commandservice.customer.Customer)13 CustomerId (io.spine.test.commandservice.customer.CustomerId)13 Version (io.spine.core.Version)10 GivenVersion (io.spine.core.given.GivenVersion)10 Any (com.google.protobuf.Any)8 Test (org.junit.Test)8 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)7 Map (java.util.Map)6 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)5 EntityStateUpdate (io.spine.client.EntityStateUpdate)3 Query (io.spine.client.Query)3 Topic (io.spine.client.Topic)3 FieldMask (com.google.protobuf.FieldMask)2 Target (io.spine.client.Target)2 BoundedContext (io.spine.server.BoundedContext)2 EntityRecord (io.spine.server.entity.EntityRecord)2 TypeUrl (io.spine.type.TypeUrl)2 ImmutableList (com.google.common.collect.ImmutableList)1 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 CanIgnoreReturnValue (com.google.errorprone.annotations.CanIgnoreReturnValue)1