Search in sources :

Example 1 with StandStorage

use of io.spine.server.stand.StandStorage in project core-java by SpineEventEngine.

the class StandShould method doCheckReadingCustomersById.

@CanIgnoreReturnValue
Stand doCheckReadingCustomersById(int numberOfCustomers) {
    // Define the types and values used as a test data.
    final TypeUrl customerType = TypeUrl.of(Customer.class);
    final Map<CustomerId, Customer> sampleCustomers = fillSampleCustomers(numberOfCustomers);
    // Prepare the stand and its storage to act.
    final StandStorage standStorage = setupStandStorageWithCustomers(sampleCustomers, customerType);
    final Stand stand = prepareStandWithAggregateRepo(standStorage);
    triggerMultipleUpdates(sampleCustomers, stand);
    final Query readMultipleCustomers = requestFactory.query().byIds(Customer.class, sampleCustomers.keySet());
    final MemoizeQueryResponseObserver responseObserver = new MemoizeQueryResponseObserver();
    stand.execute(readMultipleCustomers, responseObserver);
    final List<Any> messageList = checkAndGetMessageList(responseObserver);
    assertEquals(sampleCustomers.size(), messageList.size());
    final Collection<Customer> allCustomers = sampleCustomers.values();
    for (Any singleRecord : messageList) {
        final Customer unpackedSingleResult = AnyPacker.unpack(singleRecord);
        assertTrue(allCustomers.contains(unpackedSingleResult));
    }
    return stand;
}
Also used : Query(io.spine.client.Query) Customer(io.spine.test.commandservice.customer.Customer) StandStorage(io.spine.server.stand.StandStorage) TypeUrl(io.spine.type.TypeUrl) CustomerId(io.spine.test.commandservice.customer.CustomerId) Any(com.google.protobuf.Any) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue)

Example 2 with StandStorage

use of io.spine.server.stand.StandStorage in project core-java by SpineEventEngine.

the class StandShould method return_empty_list_for_aggregate_reads_with_filters_not_set.

@Test
public void return_empty_list_for_aggregate_reads_with_filters_not_set() {
    final StandStorage standStorageMock = mock(StandStorage.class);
    // Return non-empty results on any storage read call.
    final EntityRecord someRecord = EntityRecord.getDefaultInstance();
    final ImmutableList<EntityRecord> nonEmptyList = ImmutableList.<EntityRecord>builder().add(someRecord).build();
    when(standStorageMock.readAllByType(any(TypeUrl.class))).thenReturn(nonEmptyList);
    when(standStorageMock.read(any(AggregateStateId.class))).thenReturn(Optional.of(someRecord));
    when(standStorageMock.readAll()).thenReturn(Maps.<AggregateStateId, EntityRecord>newHashMap());
    when(standStorageMock.readMultiple(ArgumentMatchers.<AggregateStateId>anyIterable())).thenReturn(nonEmptyList);
    final Stand stand = prepareStandWithAggregateRepo(standStorageMock);
    final Query noneOfCustomersQuery = requestFactory.query().byIds(Customer.class, Collections.<Message>emptySet());
    final MemoizeQueryResponseObserver responseObserver = new MemoizeQueryResponseObserver();
    stand.execute(noneOfCustomersQuery, responseObserver);
    verifyObserver(responseObserver);
    final List<Any> messageList = checkAndGetMessageList(responseObserver);
    assertTrue("Query returned a non-empty response message list " + "though the filter was not set", messageList.isEmpty());
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Query(io.spine.client.Query) StandStorage(io.spine.server.stand.StandStorage) TypeUrl(io.spine.type.TypeUrl) Any(com.google.protobuf.Any) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 3 with StandStorage

use of io.spine.server.stand.StandStorage in project core-java by SpineEventEngine.

the class StandShould method operate_with_storage_provided_through_builder.

@SuppressWarnings("OverlyCoupledMethod")
@Test
public void operate_with_storage_provided_through_builder() {
    final StandStorage standStorageMock = mock(StandStorage.class);
    final BoundedContext boundedContext = BoundedContext.newBuilder().setStand(Stand.newBuilder().setStorage(standStorageMock)).build();
    final Stand stand = boundedContext.getStand();
    assertNotNull(stand);
    final io.spine.server.Given.CustomerAggregateRepository customerAggregateRepo = new io.spine.server.Given.CustomerAggregateRepository(boundedContext);
    stand.registerTypeSupplier(customerAggregateRepo);
    final int numericIdValue = 17;
    final CustomerId customerId = customerIdFor(numericIdValue);
    final io.spine.server.Given.CustomerAggregate customerAggregate = customerAggregateRepo.create(customerId);
    final Customer customerState = customerAggregate.getState();
    final TypeUrl customerType = TypeUrl.of(Customer.class);
    final Version stateVersion = Tests.newVersionWithNumber(1);
    verify(standStorageMock, never()).write(any(AggregateStateId.class), any(EntityRecordWithColumns.class));
    stand.update(asEnvelope(customerId, customerState, stateVersion));
    final AggregateStateId expectedAggregateStateId = AggregateStateId.of(customerId, customerType);
    final Any packedState = AnyPacker.pack(customerState);
    final EntityRecord expectedRecord = EntityRecord.newBuilder().setState(packedState).build();
    verify(standStorageMock, times(1)).write(eq(expectedAggregateStateId), recordStateMatcher(expectedRecord));
}
Also used : StreamObservers.noOpObserver(io.spine.io.StreamObservers.noOpObserver) StreamObserver(io.grpc.stub.StreamObserver) MemoizingObserver(io.spine.io.StreamObservers.MemoizingObserver) StreamObservers.memoizingObserver(io.spine.io.StreamObservers.memoizingObserver) Customer(io.spine.test.commandservice.customer.Customer) TypeUrl(io.spine.type.TypeUrl) CustomerId(io.spine.test.commandservice.customer.CustomerId) Any(com.google.protobuf.Any) EntityRecordWithColumns(io.spine.server.entity.storage.EntityRecordWithColumns) EntityRecord(io.spine.server.entity.EntityRecord) Version(io.spine.base.Version) StandStorage(io.spine.server.stand.StandStorage) BoundedContext(io.spine.server.BoundedContext) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 4 with StandStorage

use of io.spine.server.stand.StandStorage in project core-java by SpineEventEngine.

the class StandShould method setupStandStorageWithCustomers.

private StandStorage setupStandStorageWithCustomers(Map<CustomerId, Customer> sampleCustomers, TypeUrl customerType) {
    final StandStorage standStorage = StorageFactorySwitch.get(isMultitenant()).createStandStorage();
    final ImmutableList.Builder<AggregateStateId> stateIdsBuilder = ImmutableList.builder();
    final ImmutableList.Builder<EntityRecord> recordsBuilder = ImmutableList.builder();
    for (CustomerId customerId : sampleCustomers.keySet()) {
        final AggregateStateId stateId = AggregateStateId.of(customerId, customerType);
        final Customer customer = sampleCustomers.get(customerId);
        final Any customerState = AnyPacker.pack(customer);
        final EntityRecord entityRecord = EntityRecord.newBuilder().setState(customerState).build();
        stateIdsBuilder.add(stateId);
        recordsBuilder.add(entityRecord);
        standStorage.write(stateId, entityRecord);
    }
    return standStorage;
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) Customer(io.spine.test.commandservice.customer.Customer) StandStorage(io.spine.server.stand.StandStorage) ImmutableList(com.google.common.collect.ImmutableList) CustomerId(io.spine.test.commandservice.customer.CustomerId) Any(com.google.protobuf.Any)

Example 5 with StandStorage

use of io.spine.server.stand.StandStorage in project core-java by SpineEventEngine.

the class StandStorageShould method retrieve_records_by_ids.

@Test
public void retrieve_records_by_ids() {
    final StandStorage storage = getStorage();
    // Use a subset of IDs
    final List<AggregateStateId> ids = fill(storage, 10, DEFAULT_ID_SUPPLIER).subList(0, 5);
    final Collection<EntityRecord> records = (Collection<EntityRecord>) storage.readMultiple(ids);
    checkIds(ids, records);
}
Also used : EntityRecord(io.spine.server.entity.EntityRecord) StandStorage(io.spine.server.stand.StandStorage) ImmutableCollection(com.google.common.collect.ImmutableCollection) Collection(java.util.Collection) Test(org.junit.Test)

Aggregations

StandStorage (io.spine.server.stand.StandStorage)9 Any (com.google.protobuf.Any)7 EntityRecord (io.spine.server.entity.EntityRecord)7 TypeUrl (io.spine.type.TypeUrl)5 Test (org.junit.Test)5 Customer (io.spine.test.commandservice.customer.Customer)4 CustomerId (io.spine.test.commandservice.customer.CustomerId)4 Version (io.spine.base.Version)2 Query (io.spine.client.Query)2 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)2 ImmutableCollection (com.google.common.collect.ImmutableCollection)1 ImmutableList (com.google.common.collect.ImmutableList)1 CanIgnoreReturnValue (com.google.errorprone.annotations.CanIgnoreReturnValue)1 StreamObserver (io.grpc.stub.StreamObserver)1 ActorRequestFactory (io.spine.client.ActorRequestFactory)1 MemoizingObserver (io.spine.io.StreamObservers.MemoizingObserver)1 StreamObservers.memoizingObserver (io.spine.io.StreamObservers.memoizingObserver)1 StreamObservers.noOpObserver (io.spine.io.StreamObservers.noOpObserver)1 BoundedContext (io.spine.server.BoundedContext)1 EntityRecordWithColumns (io.spine.server.entity.storage.EntityRecordWithColumns)1