Search in sources :

Example 11 with EntityRecord

use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.

the class RecordStorageShould method write_record_bulk.

@Test
public void write_record_bulk() {
    final RecordStorage<I> storage = getStorage();
    final int bulkSize = 5;
    final Map<I, EntityRecordWithColumns> initial = new HashMap<>(bulkSize);
    for (int i = 0; i < bulkSize; i++) {
        final I id = newId();
        final EntityRecord record = newStorageRecord(id);
        initial.put(id, EntityRecordWithColumns.of(record));
    }
    storage.write(initial);
    final Collection<EntityRecord> actual = newLinkedList(storage.readMultiple(initial.keySet()));
    final Collection<EntityRecord> expected = Collections2.transform(initial.values(), RECORD_EXTRACTOR_FUNCTION);
    assertEquals(expected.size(), actual.size());
    assertTrue(actual.containsAll(expected));
    close(storage);
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) HashMap(java.util.HashMap) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) Test(org.junit.Test)

Example 12 with EntityRecord

use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.

the class RecordStorageShould method newStorageRecord.

private static EntityRecord newStorageRecord(Message state) {
    final Any wrappedState = AnyPacker.pack(state);
    final EntityRecord record = EntityRecord.newBuilder().setState(wrappedState).setVersion(Tests.newVersionWithNumber(0)).build();
    return record;
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Identifiers.idToAny(io.spine.base.Identifiers.idToAny) Any(com.google.protobuf.Any)

Example 13 with EntityRecord

use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.

the class RecordStorageShould method read_multiple_records_with_field_mask.

@SuppressWarnings({ "MethodWithMultipleLoops", "ConstantConditions" })
// Converter nullability issues
@Test
public void read_multiple_records_with_field_mask() {
    final RecordStorage<I> storage = getStorage();
    final int count = 10;
    final List<I> ids = new LinkedList<>();
    Descriptors.Descriptor typeDescriptor = null;
    for (int i = 0; i < count; i++) {
        final I id = newId();
        final Message state = newState(id);
        final EntityRecord record = newStorageRecord(state);
        storage.write(id, record);
        ids.add(id);
        if (typeDescriptor == null) {
            typeDescriptor = state.getDescriptorForType();
        }
    }
    final int bulkCount = count / 2;
    final FieldMask fieldMask = FieldMasks.maskOf(typeDescriptor, 2);
    final Iterable<EntityRecord> readRecords = storage.readMultiple(ids.subList(0, bulkCount), fieldMask);
    final List<EntityRecord> readList = newLinkedList(readRecords);
    Verify.assertSize(bulkCount, readList);
    for (EntityRecord record : readRecords) {
        final Message state = unpack(record.getState());
        assertMatchesMask(state, fieldMask);
    }
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Message(com.google.protobuf.Message) Descriptors(com.google.protobuf.Descriptors) LinkedList(java.util.LinkedList) Lists.newLinkedList(com.google.common.collect.Lists.newLinkedList) FieldMask(com.google.protobuf.FieldMask) Test(org.junit.Test)

Example 14 with EntityRecord

use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.

the class RecordStorageShould method filter_records_by_columns.

// Complex test case (still tests a single operation)
@SuppressWarnings("OverlyLongMethod")
@Test
public void filter_records_by_columns() {
    final Project.Status requiredValue = Project.Status.DONE;
    final Int32Value wrappedValue = Int32Value.newBuilder().setValue(requiredValue.getNumber()).build();
    final Version versionValue = Version.newBuilder().setNumber(// Value of the counter after one columns
    2).build();
    final EntityFilters filters = EntityFilters.newBuilder().putColumnFilter("projectStatusValue", AnyPacker.pack(wrappedValue)).putColumnFilter("counterVersion", AnyPacker.pack(versionValue)).build();
    final EntityQuery<I> query = EntityQueries.from(filters, TestCounterEntity.class);
    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);
    // 2 of 3 have required values
    matchingEntity.setStatus(requiredValue);
    wrongEntity1.setStatus(requiredValue);
    wrongEntity2.setStatus(Project.Status.CANCELLED);
    // Change internal Entity state
    wrongEntity1.getCounter();
    // After the mutation above the single matching record is the one under the `idMatching` ID
    final EntityRecord fineRecord = newStorageRecord(idMatching, newState(idMatching));
    final EntityRecord notFineRecord1 = newStorageRecord(idWrong1, newState(idWrong1));
    final EntityRecord notFineRecord2 = newStorageRecord(idWrong2, newState(idWrong2));
    final EntityRecordWithColumns recordRight = create(fineRecord, matchingEntity);
    final EntityRecordWithColumns recordWrong1 = create(notFineRecord1, wrongEntity1);
    final EntityRecordWithColumns recordWrong2 = create(notFineRecord2, wrongEntity2);
    final RecordStorage<I> storage = getStorage();
    storage.write(idMatching, recordRight);
    storage.write(idWrong1, recordWrong1);
    storage.write(idWrong2, recordWrong2);
    final Map<I, EntityRecord> readRecords = storage.readAll(query, FieldMask.getDefaultInstance());
    Verify.assertSize(1, readRecords);
    final I singleId = readRecords.keySet().iterator().next();
    assertEquals(idMatching, singleId);
    final EntityRecord singleRecord = readRecords.values().iterator().next();
    assertEquals(fineRecord, singleRecord);
}
Also used : EntityFilters(io.spine.client.EntityFilters) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) EntityRecord(io.spine.server.entity.EntityRecord) Project(io.spine.test.storage.Project) Version(io.spine.base.Version) Int32Value(com.google.protobuf.Int32Value) Test(org.junit.Test)

Example 15 with EntityRecord

use of io.spine.server.entity.EntityRecord in project core-java by SpineEventEngine.

the class RecordStorageShould method newStorageRecord.

private EntityRecord newStorageRecord(I id, Message state) {
    final Any wrappedState = AnyPacker.pack(state);
    final EntityRecord record = EntityRecord.newBuilder().setEntityId(idToAny(id)).setState(wrappedState).setVersion(Tests.newVersionWithNumber(0)).build();
    return record;
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Identifiers.idToAny(io.spine.base.Identifiers.idToAny) Any(com.google.protobuf.Any)

Aggregations

EntityRecord (io.spine.server.entity.EntityRecord)41 Test (org.junit.Test)24 Any (com.google.protobuf.Any)19 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)9 Message (com.google.protobuf.Message)8 TypeUrl (io.spine.type.TypeUrl)8 StandStorage (io.spine.server.stand.StandStorage)7 FieldMask (com.google.protobuf.FieldMask)6 Identifiers.idToAny (io.spine.base.Identifiers.idToAny)4 Project (io.spine.test.storage.Project)4 LinkedList (java.util.LinkedList)4 Version (io.spine.base.Version)3 EntityFilters (io.spine.client.EntityFilters)3 Project (io.spine.test.projection.Project)3 ImmutableList (com.google.common.collect.ImmutableList)2 Descriptors (com.google.protobuf.Descriptors)2 EntityIdFilter (io.spine.client.EntityIdFilter)2 LifecycleFlags (io.spine.server.entity.LifecycleFlags)2 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)2 Customer (io.spine.test.commandservice.customer.Customer)2