Search in sources :

Example 36 with Version

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

the class StandShould method select_entity_singleton_by_id_and_apply_field_masks.

@SuppressWarnings("MethodWithMultipleLoops")
@Test
public void select_entity_singleton_by_id_and_apply_field_masks() {
    final Stand stand = prepareStandWithAggregateRepo(createStandStorage());
    final String customerDescriptor = Customer.getDescriptor().getFullName();
    // clashes with non-related tests.
    @SuppressWarnings("DuplicateStringLiteralInspection") final String[] paths = { customerDescriptor + ".id", customerDescriptor + ".name" };
    final FieldMask fieldMask = FieldMask.newBuilder().addAllPaths(Arrays.asList(paths)).build();
    final List<Customer> customers = new LinkedList<>();
    final int count = 10;
    for (int i = 0; i < count; i++) {
        // Has new ID each time
        final Customer customer = getSampleCustomer();
        customers.add(customer);
        final Version stateVersion = GivenVersion.withNumber(1);
        stand.update(asEnvelope(customer.getId(), customer, stateVersion));
    }
    final Set<CustomerId> ids = Collections.singleton(customers.get(0).getId());
    final Query customerQuery = requestFactory.query().byIdsWithMask(Customer.class, ids, paths);
    final MemoizeQueryResponseObserver observer = new MemoizeQueryResponseObserver();
    stand.execute(customerQuery, observer);
    final List<Any> read = observer.responseHandled.getMessagesList();
    Verify.assertSize(1, read);
    final Customer customer = unpack(read.get(0));
    assertMatches(customer, fieldMask);
    assertTrue(ids.contains(customer.getId()));
    verifyObserver(observer);
}
Also used : Query(io.spine.client.Query) Customer(io.spine.test.commandservice.customer.Customer) CustomerId(io.spine.test.commandservice.customer.CustomerId) Any(com.google.protobuf.Any) LinkedList(java.util.LinkedList) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.Version) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 37 with Version

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

the class StandShould method retrieve_all_data_if_field_mask_is_not_set.

@Test
public void retrieve_all_data_if_field_mask_is_not_set() {
    final Stand stand = prepareStandWithAggregateRepo(createStandStorage());
    final Customer sampleCustomer = getSampleCustomer();
    final Version stateVersion = GivenVersion.withNumber(1);
    stand.update(asEnvelope(sampleCustomer.getId(), sampleCustomer, stateVersion));
    final Query customerQuery = requestFactory.query().all(Customer.class);
    // noinspection OverlyComplexAnonymousInnerClass
    final MemoizeQueryResponseObserver observer = new MemoizeQueryResponseObserver() {

        @Override
        public void onNext(QueryResponse value) {
            super.onNext(value);
            final List<Any> messages = value.getMessagesList();
            assertFalse(messages.isEmpty());
            final Customer customer = unpack(messages.get(0));
            for (Descriptors.FieldDescriptor field : customer.getDescriptorForType().getFields()) {
                assertTrue(customer.getField(field).equals(sampleCustomer.getField(field)));
            }
        }
    };
    stand.execute(customerQuery, observer);
    verifyObserver(observer);
}
Also used : Query(io.spine.client.Query) Customer(io.spine.test.commandservice.customer.Customer) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.Version) QueryResponse(io.spine.client.QueryResponse) Descriptors(com.google.protobuf.Descriptors) Any(com.google.protobuf.Any) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 38 with Version

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

the class StandShould method handle_mistakes_in_query_silently.

@Test
public void handle_mistakes_in_query_silently() {
    // noinspection ZeroLengthArrayAllocation
    final Stand stand = prepareStandWithAggregateRepo(createStandStorage());
    final Customer sampleCustomer = getSampleCustomer();
    final Version stateVersion = GivenVersion.withNumber(1);
    stand.update(asEnvelope(sampleCustomer.getId(), sampleCustomer, stateVersion));
    // FieldMask with invalid type URLs.
    final String[] paths = { "invalid_type_url_example", Project.getDescriptor().getFields().get(2).getFullName() };
    final Query customerQuery = requestFactory.query().allWithMask(Customer.class, paths);
    final MemoizeQueryResponseObserver observer = new MemoizeQueryResponseObserver() {

        @Override
        public void onNext(QueryResponse value) {
            super.onNext(value);
            final List<Any> messages = value.getMessagesList();
            assertFalse(messages.isEmpty());
            final Customer customer = unpack(messages.get(0));
            assertNotEquals(customer, null);
            assertFalse(customer.hasId());
            assertFalse(customer.hasName());
            assertTrue(customer.getNicknamesList().isEmpty());
        }
    };
    stand.execute(customerQuery, observer);
    verifyObserver(observer);
}
Also used : Query(io.spine.client.Query) Customer(io.spine.test.commandservice.customer.Customer) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.Version) QueryResponse(io.spine.client.QueryResponse) Any(com.google.protobuf.Any) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 39 with Version

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

the class StandShould method trigger_subscription_callback_upon_update_of_aggregate.

@Test
public void trigger_subscription_callback_upon_update_of_aggregate() {
    final Stand stand = prepareStandWithAggregateRepo(mock(StandStorage.class));
    final Topic allCustomers = requestFactory.topic().allOf(Customer.class);
    final MemoizeEntityUpdateCallback memoizeCallback = new MemoizeEntityUpdateCallback();
    subscribeAndActivate(stand, allCustomers, memoizeCallback);
    assertNull(memoizeCallback.newEntityState);
    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);
    assertEquals(packedState, memoizeCallback.newEntityState);
}
Also used : Customer(io.spine.test.commandservice.customer.Customer) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.Version) CustomerId(io.spine.test.commandservice.customer.CustomerId) Topic(io.spine.client.Topic) Map(java.util.Map) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Any(com.google.protobuf.Any) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 40 with Version

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

the class Aggregate method apply.

/**
 * Applies event messages.
 *
 * @param eventMessages the event messages or events to apply
 * @param origin        the envelope of a message which caused the events
 * @see #ensureEventMessage(Message)
 */
void apply(Iterable<? extends Message> eventMessages, MessageEnvelope origin) {
    final List<? extends Message> messages = newArrayList(eventMessages);
    final EventFactory eventFactory = EventFactory.on(origin, getProducerId());
    final List<Event> events = newArrayListWithCapacity(messages.size());
    Version projectedEventVersion = getVersion();
    for (Message eventOrMessage : messages) {
        /* Applying each message would increment the entity version.
               Therefore, we should simulate this behaviour. */
        projectedEventVersion = Versions.increment(projectedEventVersion);
        final Message eventMessage = ensureEventMessage(eventOrMessage);
        final Event event;
        if (eventOrMessage instanceof Event) {
            /* If we get instances of Event, it means we are dealing with an import command,
                   which contains these events in the body. So we deal with a command envelope.
                */
            final CommandEnvelope ce = (CommandEnvelope) origin;
            event = importEvent((Event) eventOrMessage, ce.getCommandContext(), projectedEventVersion);
        } else {
            event = eventFactory.createEvent(eventMessage, projectedEventVersion);
        }
        events.add(event);
    }
    play(events);
    uncommittedEvents.addAll(events);
}
Also used : Events.getMessage(io.spine.core.Events.getMessage) Message(com.google.protobuf.Message) Version(io.spine.core.Version) EventFactory(io.spine.server.event.EventFactory) Event(io.spine.core.Event) CommandEnvelope(io.spine.core.CommandEnvelope)

Aggregations

Version (io.spine.core.Version)41 Test (org.junit.Test)33 GivenVersion (io.spine.core.given.GivenVersion)16 Customer (io.spine.test.commandservice.customer.Customer)13 Any (com.google.protobuf.Any)11 Versions.newVersion (io.spine.core.Versions.newVersion)11 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)11 Event (io.spine.core.Event)10 CustomerId (io.spine.test.commandservice.customer.CustomerId)10 Map (java.util.Map)7 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)6 Query (io.spine.client.Query)5 Topic (io.spine.client.Topic)5 QueryResponse (io.spine.client.QueryResponse)4 ProjectId (io.spine.test.projection.ProjectId)4 Message (com.google.protobuf.Message)3 Timestamp (com.google.protobuf.Timestamp)3 EntityStateUpdate (io.spine.client.EntityStateUpdate)3 EntityRecord (io.spine.server.entity.EntityRecord)3 Project (io.spine.test.aggregate.Project)3