Search in sources :

Example 6 with Version

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

the class TransactionShould method init_state_and_version_if_specified_in_ctor.

@Test
public void init_state_and_version_if_specified_in_ctor() {
    final E entity = createEntity();
    final S newState = createNewState();
    final Version newVersion = someVersion();
    assertNotEquals(entity.getState(), newState);
    assertNotEquals(entity.getVersion(), newVersion);
    final Transaction<I, E, S, B> tx = createTxWithState(entity, newState, newVersion);
    assertEquals(newState, tx.getBuilder().build());
    assertEquals(newVersion, tx.getVersion());
    assertNotEquals(entity.getState(), newState);
    assertNotEquals(entity.getVersion(), newVersion);
    tx.commit();
    assertEquals(entity.getState(), newState);
    assertEquals(entity.getVersion(), newVersion);
}
Also used : Versions.newVersion(io.spine.core.Versions.newVersion) Version(io.spine.core.Version) Test(org.junit.Test)

Example 7 with Version

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

the class TransactionShould method propagate_changes_to_entity_upon_commit.

@Test
public void propagate_changes_to_entity_upon_commit() {
    final E entity = createEntity();
    final Transaction<I, E, S, B> tx = createTx(entity);
    final Event event = createEvent(createEventMessage());
    applyEvent(tx, event);
    final S stateBeforeCommit = entity.getState();
    final Version versionBeforeCommit = entity.getVersion();
    tx.commit();
    final S modifiedState = entity.getState();
    final Version modifiedVersion = entity.getVersion();
    assertNotEquals(stateBeforeCommit, modifiedState);
    assertNotEquals(versionBeforeCommit, modifiedVersion);
}
Also used : Versions.newVersion(io.spine.core.Versions.newVersion) Version(io.spine.core.Version) Event(io.spine.core.Event) Test(org.junit.Test)

Example 8 with Version

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

the class TransactionShould method throw_InvalidEntityStateException_if_constraints_violated_on_commit.

@Test(expected = InvalidEntityStateException.class)
public void throw_InvalidEntityStateException_if_constraints_violated_on_commit() {
    final E entity = createEntity();
    final S newState = createNewState();
    final Version version = someVersion();
    final Transaction<I, E, S, B> tx = createTxWithState(entity, newState, version);
    final ValidationException toThrow = validationException();
    breakEntityValidation(entity, toThrow);
    tx.commit();
}
Also used : ValidationException(io.spine.validate.ValidationException) Versions.newVersion(io.spine.core.Versions.newVersion) Version(io.spine.core.Version) Test(org.junit.Test)

Example 9 with Version

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

the class ColumnShould method invoke_getter.

@Test
public void invoke_getter() {
    final String entityId = "entity-id";
    final int version = 2;
    final EntityColumn column = forMethod("getVersion", VersionableEntity.class);
    final TestEntity entity = Given.entityOfClass(TestEntity.class).withId(entityId).withVersion(version).build();
    final Version actualVersion = (Version) column.getFor(entity);
    assertEquals(version, actualVersion.getNumber());
}
Also used : TestEntity(io.spine.server.entity.storage.given.ColumnTestEnv.TestEntity) BrokenTestEntity(io.spine.server.entity.storage.given.ColumnTestEnv.BrokenTestEntity) Version(io.spine.core.Version) Test(org.junit.Test)

Example 10 with Version

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

the class Stand method update.

/**
 * Updates the state of an entity inside of the current instance of {@code Stand}.
 *
 * <p>In case the entity update represents the new
 * {@link io.spine.server.aggregate.Aggregate Aggregate} state,
 * store the new value for the {@code Aggregate} to each of the configured instances of
 * {@link StandStorage}.
 *
 * <p>Each {@code Aggregate} state value is stored as one-to-one to its
 * {@link TypeUrl TypeUrl} obtained via {@link Any#getTypeUrl()}.
 *
 * <p>In case {@code Stand} already contains the state for this {@code Aggregate},
 * the value will be replaced.
 *
 * <p>The state updates which are not originated from the {@code Aggregate} are not
 * stored in the {@code Stand}.
 *
 * <p>In any case, the state update is then propagated to the callbacks.
 * The set of matched callbacks is determined by filtering all the registered callbacks
 * by the entity {@code TypeUrl}.
 *
 * <p>The matching callbacks are executed with the {@link #callbackExecutor}.
 *
 * @param envelope the updated entity state,
 *                 packed as {@linkplain EntityStateEnvelope envelope}
 */
void update(final EntityStateEnvelope<?, ?> envelope) {
    final EntityUpdateOperation op = new EntityUpdateOperation(envelope) {

        @Override
        public void run() {
            final Object id = envelope.getEntityId();
            final Message entityState = envelope.getMessage();
            final Any packedState = AnyPacker.pack(entityState);
            final TypeUrl entityTypeUrl = TypeUrl.of(entityState);
            final boolean aggregateUpdate = typeRegistry.hasAggregateType(entityTypeUrl);
            if (aggregateUpdate) {
                final Optional<Version> entityVersion = envelope.getEntityVersion();
                checkState(entityVersion.isPresent(), "The aggregate version must be set in order to update Stand. " + "Actual envelope: {}", envelope);
                // checked above.
                @SuppressWarnings("OptionalGetWithoutIsPresent") final Version versionValue = entityVersion.get();
                final AggregateStateId aggregateStateId = AggregateStateId.of(id, entityTypeUrl);
                final EntityRecord record = EntityRecord.newBuilder().setState(packedState).setVersion(versionValue).build();
                getStorage().write(aggregateStateId, record);
            }
            notifyMatchingSubscriptions(id, packedState, entityTypeUrl);
        }
    };
    op.execute();
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) Version(io.spine.core.Version) EntityUpdateOperation(io.spine.server.tenant.EntityUpdateOperation) TypeUrl(io.spine.type.TypeUrl) TypeConverter.toAny(io.spine.protobuf.TypeConverter.toAny) Any(com.google.protobuf.Any)

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