Search in sources :

Example 51 with EntityRecord

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);
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) EntityId(io.spine.client.EntityId) EntityIdFilter(io.spine.client.EntityIdFilter) EntityFilters(io.spine.client.EntityFilters) Any(com.google.protobuf.Any) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) Test(org.junit.Test)

Example 52 with EntityRecord

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));
}
Also used : EntityColumn(io.spine.server.entity.storage.EntityColumn) ColumnFilter(io.spine.client.ColumnFilter) QueryParameters(io.spine.server.entity.storage.QueryParameters) Any(com.google.protobuf.Any) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) CompositeQueryParameter(io.spine.server.entity.storage.CompositeQueryParameter) EntityRecord(io.spine.server.entity.EntityRecord) Project(io.spine.test.entity.Project) Test(org.junit.Test)

Example 53 with EntityRecord

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);
        }
    }
}
Also used : Task(io.spine.test.storage.Task) TaskId(io.spine.test.storage.TaskId) TypeUrl(io.spine.type.TypeUrl) Any(com.google.protobuf.Any) EntityRecord(io.spine.server.entity.EntityRecord) Project(io.spine.test.storage.Project)

Example 54 with EntityRecord

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);
    }
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Project(io.spine.test.storage.Project) ProjectId(io.spine.test.storage.ProjectId) Any(com.google.protobuf.Any) Nullable(javax.annotation.Nullable)

Example 55 with EntityRecord

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;
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) LinkedList(java.util.LinkedList)

Aggregations

EntityRecord (io.spine.server.entity.EntityRecord)55 Test (org.junit.Test)34 Any (com.google.protobuf.Any)24 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)15 Message (com.google.protobuf.Message)10 Project (io.spine.test.storage.Project)10 TypeUrl (io.spine.type.TypeUrl)9 FieldMask (com.google.protobuf.FieldMask)7 EntityFilters (io.spine.client.EntityFilters)7 ProjectId (io.spine.test.storage.ProjectId)7 ColumnFilter (io.spine.client.ColumnFilter)5 EntityIdFilter (io.spine.client.EntityIdFilter)4 LinkedList (java.util.LinkedList)4 CompositeColumnFilter (io.spine.client.CompositeColumnFilter)3 Version (io.spine.core.Version)3 CompositeQueryParameter (io.spine.server.entity.storage.CompositeQueryParameter)3 EntityColumn (io.spine.server.entity.storage.EntityColumn)3 QueryParameters (io.spine.server.entity.storage.QueryParameters)3 StandStorage (io.spine.server.stand.StandStorage)3 ImmutableList (com.google.common.collect.ImmutableList)2