use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.
the class RecordStorageShould method allow_by_single_id_queries_with_no_columns.
@Test
public void allow_by_single_id_queries_with_no_columns() {
// Create the test data
final I idMatching = newId();
final I idWrong1 = newId();
final I idWrong2 = newId();
final TestCounterEntity<I> matchingEntity = new TestCounterEntity<>(idMatching);
final TestCounterEntity<I> wrongEntity1 = new TestCounterEntity<>(idWrong1);
final TestCounterEntity<I> wrongEntity2 = new TestCounterEntity<>(idWrong2);
final EntityRecord fineRecord = newStorageRecord(idMatching, newState(idMatching));
final EntityRecord notFineRecord1 = newStorageRecord(idWrong1, newState(idWrong1));
final EntityRecord notFineRecord2 = newStorageRecord(idWrong2, newState(idWrong2));
final RecordStorage<I> storage = getStorage();
final EntityRecordWithColumns recordRight = create(fineRecord, matchingEntity, storage);
final EntityRecordWithColumns recordWrong1 = create(notFineRecord1, wrongEntity1, storage);
final EntityRecordWithColumns recordWrong2 = create(notFineRecord2, wrongEntity2, storage);
// Fill the storage
storage.write(idWrong1, recordWrong1);
storage.write(idMatching, recordRight);
storage.write(idWrong2, recordWrong2);
// Prepare the query
final Any matchingIdPacked = TypeConverter.toAny(idMatching);
final EntityId entityId = EntityId.newBuilder().setId(matchingIdPacked).build();
final EntityIdFilter idFilter = EntityIdFilter.newBuilder().addIds(entityId).build();
final EntityFilters filters = EntityFilters.newBuilder().setIdFilter(idFilter).build();
final EntityQuery<I> query = EntityQueries.from(filters, storage);
// Perform the query
final Iterator<EntityRecord> readRecords = storage.readAll(query, FieldMask.getDefaultInstance());
// Check results
assertSingleRecord(fineRecord, readRecords);
}
use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.
the class EntityQueryMatcherShould method match_Any_instances.
@Test
public void match_Any_instances() {
final String columnName = "column";
final Project someMessage = Sample.messageOfType(Project.class);
final Any actualValue = AnyPacker.pack(someMessage);
final EntityColumn column = mock(EntityColumn.class);
when(column.getType()).thenReturn(Any.class);
when(column.getStoredName()).thenReturn(columnName);
final EntityColumn.MemoizedValue value = mock(EntityColumn.MemoizedValue.class);
when(value.getSourceColumn()).thenReturn(column);
when(value.getValue()).thenReturn(actualValue);
final EntityRecord record = Sample.messageOfType(EntityRecord.class);
final Map<String, EntityColumn.MemoizedValue> columns = singletonMap(columnName, value);
final EntityRecordWithColumns recordWithColumns = createRecord(record, columns);
final Multimap<EntityColumn, ColumnFilter> filters = of(column, eq(columnName, actualValue));
final CompositeQueryParameter parameter = createParams(filters, ALL);
final QueryParameters parameters = QueryParameters.newBuilder().add(parameter).build();
final EntityQuery<?> query = createQuery(emptySet(), parameters);
final EntityQueryMatcher<?> matcher = new EntityQueryMatcher<>(query);
assertTrue(matcher.apply(recordWithColumns));
}
use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.
the class StandStorageShould method checkByTypeRead.
// OK for this test.
@SuppressWarnings({ "MethodWithMultipleLoops", "ConstantConditions" })
private void checkByTypeRead(FieldMask fieldMask) {
final boolean withFieldMask = !fieldMask.equals(FieldMask.getDefaultInstance());
final StandStorage storage = getStorage();
final TypeUrl type = TypeUrl.from(Project.getDescriptor());
final int projectsCount = 4;
final List<AggregateStateId> projectIds = fill(storage, projectsCount, DEFAULT_ID_SUPPLIER);
final int tasksCount = 5;
for (int i = 0; i < tasksCount; i++) {
final TaskId genericId = TaskId.newBuilder().setId(i).build();
final AggregateStateId id = AggregateStateId.of(genericId, TypeUrl.from(Task.getDescriptor()));
final Task task = Task.newBuilder().setTaskId(genericId).setTitle("Test task").setDescription("With description").build();
final EntityRecord record = newRecord(task);
storage.write(id, record);
}
final Iterator<EntityRecord> readRecords = withFieldMask ? storage.readAllByType(TypeUrl.from(Project.getDescriptor()), fieldMask) : storage.readAllByType(TypeUrl.from(Project.getDescriptor()));
final Set<EntityRecord> readDistinct = Sets.newHashSet(readRecords);
assertSize(projectsCount, readDistinct);
for (EntityRecord record : readDistinct) {
final Any state = record.getState();
final Project project = AnyPacker.unpack(state);
final AggregateStateId restored = AggregateStateId.of(project.getId(), type);
assertContains(restored, projectIds);
if (withFieldMask) {
assertMatchesMask(project, fieldMask);
}
}
}
use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.
the class StandStorageShould method checkIds.
protected void checkIds(List<AggregateStateId> ids, Iterator<EntityRecord> records) {
final Collection<EntityRecord> recordsToCheck = newArrayList(records);
assertSize(ids.size(), recordsToCheck);
final Collection<ProjectId> projectIds = Collections2.transform(ids, new Function<AggregateStateId, ProjectId>() {
@Nullable
@Override
public ProjectId apply(@Nullable AggregateStateId input) {
if (input == null) {
return null;
}
return (ProjectId) input.getAggregateId();
}
});
for (EntityRecord record : recordsToCheck) {
final Any packedState = record.getState();
final Project state = AnyPacker.unpack(packedState);
final ProjectId id = state.getId();
assertContains(id, projectIds);
}
}
use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.
the class StandStorageShould method fill.
// Converter nullability issues
@SuppressWarnings("ConstantConditions")
protected List<AggregateStateId> fill(StandStorage storage, int count, Supplier<AggregateStateId<ProjectId>> idSupplier) {
final List<AggregateStateId> ids = new LinkedList<>();
for (int i = 0; i < count; i++) {
final AggregateStateId genericId = idSupplier.get();
final Message state = newState(genericId);
final EntityRecord record = newRecord(state);
storage.write(genericId, record);
ids.add(genericId);
}
return ids;
}
Aggregations