Search in sources :

Example 1 with Version

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

the class Aggregate method restore.

/**
 * Restores the state and version from the passed snapshot.
 *
 * <p>If this method is called during a {@linkplain #play(AggregateStateRecord) replay}
 * (because the snapshot was encountered) the method uses the state
 * {@linkplain #getBuilder() builder}, which is used during the replay.
 *
 * <p>If not in replay, the method sets the state and version directly to the aggregate.
 *
 * @param snapshot the snapshot with the state to restore
 */
void restore(Snapshot snapshot) {
    final S stateToRestore = AnyPacker.unpack(snapshot.getState());
    final Version versionFromSnapshot = snapshot.getVersion();
    setInitialState(stateToRestore, versionFromSnapshot);
}
Also used : Version(io.spine.core.Version)

Example 2 with Version

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

the class Transaction method commit.

/**
 * Applies all the outstanding modifications to the enclosed entity.
 *
 * @throws InvalidEntityStateException in case the new entity state is not valid
 * @throws IllegalStateException       in case of a generic error
 */
protected void commit() throws InvalidEntityStateException, IllegalStateException {
    final TransactionListener<I, E, S, B> listener = getListener();
    final B builder = getBuilder();
    final Version pendingVersion = getVersion();
    // The state is only updated, if at least some changes were made to the builder.
    if (builder.isDirty()) {
        try {
            final S newState = builder.build();
            markStateChanged();
            listener.onBeforeCommit(getEntity(), newState, pendingVersion, getLifecycleFlags());
            entity.updateState(newState, pendingVersion);
            commitAttributeChanges();
        } catch (ValidationException exception) {
            /* Could only happen if the state
                                                                   has been injected not using
                                                                   the builder setters. */
            final InvalidEntityStateException invalidStateException = of(exception);
            rollback(invalidStateException);
            throw invalidStateException;
        } catch (RuntimeException genericException) {
            rollback(genericException);
            throw illegalStateWithCauseOf(genericException);
        } finally {
            releaseTx();
        }
    } else {
        // The state isn't modified, but other attributes may have been modified.
        final S unmodifiedState = getEntity().getState();
        listener.onBeforeCommit(getEntity(), unmodifiedState, pendingVersion, getLifecycleFlags());
        // Set the version if it has changed.
        if (!pendingVersion.equals(entity.getVersion())) {
            entity.updateState(unmodifiedState, pendingVersion);
        }
        commitAttributeChanges();
        releaseTx();
    }
}
Also used : ValidationException(io.spine.validate.ValidationException) Version(io.spine.core.Version)

Example 3 with Version

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

the class TransactionShould method propagate_exception_wrapped_as_ISE_if_commit_fails.

@Test
public void propagate_exception_wrapped_as_ISE_if_commit_fails() {
    final E entity = createEntity(someViolations());
    final S newState = createNewState();
    final Version version = someVersion();
    final Transaction<I, E, S, B> tx = createTxWithState(entity, newState, version);
    try {
        tx.commit();
        fail("Expected an exception due to a failed commit.");
    } catch (IllegalStateException e) {
        assertEquals(InvalidEntityStateException.class, e.getCause().getClass());
    }
}
Also used : Versions.newVersion(io.spine.core.Versions.newVersion) Version(io.spine.core.Version) Test(org.junit.Test)

Example 4 with Version

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

the class TransactionShould method not_propagate_changes_to_entity_on_rollback.

@Test
public void not_propagate_changes_to_entity_on_rollback() {
    final E entity = createEntity();
    final Transaction<I, E, S, B> tx = createTx(entity);
    final Event event = createEvent(createEventMessage());
    applyEvent(tx, event);
    final S stateBeforeRollback = entity.getState();
    final Version versionBeforeRollback = entity.getVersion();
    tx.rollback(new RuntimeException("that triggers rollback"));
    final S stateAfterRollback = entity.getState();
    final Version versionAfterRollback = entity.getVersion();
    assertEquals(stateBeforeRollback, stateAfterRollback);
    assertEquals(versionBeforeRollback, versionAfterRollback);
}
Also used : Versions.newVersion(io.spine.core.Versions.newVersion) Version(io.spine.core.Version) Event(io.spine.core.Event) Test(org.junit.Test)

Example 5 with Version

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

the class TransactionShould method set_txEntityVersion_from_EventContext.

@Test
public void set_txEntityVersion_from_EventContext() {
    final E entity = createEntity();
    final Transaction<I, E, S, B> tx = createTx(entity);
    final Event event = createEvent(createEventMessage());
    final Version ctxVersion = event.getContext().getVersion();
    assertNotEquals(tx.getVersion(), ctxVersion);
    applyEvent(tx, event);
    final Version modifiedVersion = tx.getVersion();
    assertEquals(modifiedVersion, tx.getVersion());
}
Also used : Versions.newVersion(io.spine.core.Versions.newVersion) Version(io.spine.core.Version) 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