Search in sources :

Example 26 with Version

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

the class AggregateStorageShould method continue_history_reading_if_snapshot_was_not_found_in_first_batch.

@Test
public void continue_history_reading_if_snapshot_was_not_found_in_first_batch() {
    Version currentVersion = zero();
    final Snapshot snapshot = Snapshot.newBuilder().setVersion(currentVersion).build();
    storage.writeSnapshot(id, snapshot);
    final int eventCountAfterSnapshot = 10;
    for (int i = 0; i < eventCountAfterSnapshot; i++) {
        currentVersion = increment(currentVersion);
        final Project state = Project.getDefaultInstance();
        final Event event = eventFactory.createEvent(state, currentVersion);
        storage.writeEvent(id, event);
    }
    final int batchSize = 1;
    final AggregateReadRequest<ProjectId> request = new AggregateReadRequest<>(id, batchSize);
    final Optional<AggregateStateRecord> optionalStateRecord = storage.read(request);
    assertTrue(optionalStateRecord.isPresent());
    final AggregateStateRecord stateRecord = optionalStateRecord.get();
    assertEquals(snapshot, stateRecord.getSnapshot());
    assertEquals(eventCountAfterSnapshot, stateRecord.getEventCount());
}
Also used : Project(io.spine.test.aggregate.Project) Version(io.spine.core.Version) ProjectId(io.spine.test.aggregate.ProjectId) Event(io.spine.core.Event) Test(org.junit.Test)

Example 27 with Version

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

the class AggregateStorageShould method sort_by_version_rather_then_by_timestamp.

@Test
public void sort_by_version_rather_then_by_timestamp() {
    final Project state = Project.getDefaultInstance();
    final Version minVersion = zero();
    final Version maxVersion = increment(minVersion);
    final Timestamp minTimestamp = Timestamps.MIN_VALUE;
    final Timestamp maxTimestamp = Timestamps.MAX_VALUE;
    // The first event is an event, which is the oldest, i.e. with the minimal version.
    final Event expectedFirst = eventFactory.createEvent(state, minVersion, maxTimestamp);
    final Event expectedSecond = eventFactory.createEvent(state, maxVersion, minTimestamp);
    storage.writeEvent(id, expectedSecond);
    storage.writeEvent(id, expectedFirst);
    final List<Event> events = storage.read(newReadRequest(id)).get().getEventList();
    assertTrue(events.indexOf(expectedFirst) < events.indexOf(expectedSecond));
}
Also used : Project(io.spine.test.aggregate.Project) Version(io.spine.core.Version) Event(io.spine.core.Event) Timestamp(com.google.protobuf.Timestamp) Test(org.junit.Test)

Example 28 with Version

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

the class AggregateStorageShould method write_records_and_return_sorted_by_version_descending.

@Test
public void write_records_and_return_sorted_by_version_descending() {
    final int eventsNumber = 5;
    final List<AggregateEventRecord> records = newLinkedList();
    final Timestamp timestamp = getCurrentTime();
    Version currentVersion = zero();
    for (int i = 0; i < eventsNumber; i++) {
        final Project state = Project.getDefaultInstance();
        final Event event = eventFactory.createEvent(state, currentVersion, timestamp);
        final AggregateEventRecord record = StorageRecord.create(timestamp, event);
        records.add(record);
        currentVersion = increment(currentVersion);
    }
    writeAll(id, records);
    final Iterator<AggregateEventRecord> iterator = historyBackward();
    final List<AggregateEventRecord> actual = newArrayList(iterator);
    // expected records should be in a reverse order
    reverse(records);
    assertEquals(records, actual);
}
Also used : Project(io.spine.test.aggregate.Project) Version(io.spine.core.Version) Event(io.spine.core.Event) Timestamp(com.google.protobuf.Timestamp) Test(org.junit.Test)

Example 29 with Version

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

the class ProjectionShould method expose_playing_events_to_the_package.

@Test
public void expose_playing_events_to_the_package() {
    final TestEventFactory eventFactory = TestEventFactory.newInstance(getClass());
    final StringValue strValue = StringValue.newBuilder().setValue("eins zwei drei").build();
    final Int32Value intValue = Int32Value.newBuilder().setValue(123).build();
    final Version nextVersion = Versions.increment(projection.getVersion());
    final Event e1 = eventFactory.createEvent(strValue, nextVersion);
    final Event e2 = eventFactory.createEvent(intValue, Versions.increment(nextVersion));
    final boolean projectionChanged = Projection.play(projection, ImmutableList.of(e1, e2));
    final String projectionState = projection.getState().getValue();
    assertTrue(projectionChanged);
    assertTrue(projectionState.contains(strValue.getValue()));
    assertTrue(projectionState.contains(String.valueOf(intValue.getValue())));
}
Also used : TestEventFactory(io.spine.server.command.TestEventFactory) Version(io.spine.core.Version) Int32Value(com.google.protobuf.Int32Value) Event(io.spine.core.Event) StringValue(com.google.protobuf.StringValue) Test(org.junit.Test)

Example 30 with Version

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

the class ProjectionTransactionShould method increment_version_on_event.

/**
 * Tests the version advancement strategy for the {@link Projection}s.
 *
 * <p>The versioning strategy is for {@link Projection} is
 * {@link io.spine.server.entity.EntityVersioning#AUTO_INCREMENT AUTO_INCREMENT}. This test
 * case substitutes {@link #advance_version_from_event()}, which tested the behavior of
 * {@link io.spine.server.entity.EntityVersioning#FROM_EVENT FROM_EVENT} strategy.
 */
@Test
public void increment_version_on_event() {
    final Projection<ProjectId, Project, PatchedProjectBuilder> entity = createEntity();
    final Version oldVersion = entity.getVersion();
    final Event event = createEvent(createEventMessage());
    Projection.play(entity, Collections.singleton(event));
    final Version expected = Versions.increment(oldVersion);
    assertEquals(expected.getNumber(), entity.getVersion().getNumber());
    assertNotEquals(event.getContext().getVersion(), entity.getVersion());
}
Also used : Project(io.spine.test.projection.Project) Version(io.spine.core.Version) ProjectId(io.spine.test.projection.ProjectId) Event(io.spine.core.Event) Test(org.junit.Test)

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