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);
}
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);
}
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();
}
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());
}
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();
}
Aggregations