Search in sources :

Example 16 with ProjectId

use of io.spine.test.projection.ProjectId in project core-java by SpineEventEngine.

the class ProjectionRepositoryShould method dispatch_event_and_load_projection.

/*
     * Tests
     ************/
@Test
public void dispatch_event_and_load_projection() {
    final PrjProjectStarted msg = GivenEventMessage.projectStarted();
    // Ensure no instances are present in the repository now.
    assertFalse(repository().loadAll().hasNext());
    // And no instances of `TestProjection` processed the event message we are going to post.
    assertTrue(TestProjection.whoProcessed(msg).isEmpty());
    // Post an event message and grab the ID of the projection, which processed it.
    checkDispatchesEvent(msg);
    final Set<ProjectId> projectIds = TestProjection.whoProcessed(msg);
    assertTrue(projectIds.size() == 1);
    final ProjectId receiverId = projectIds.iterator().next();
    // Check that the projection item has actually been stored and now can be loaded.
    final Iterator<TestProjection> allItems = repository().loadAll();
    assertTrue(allItems.hasNext());
    final TestProjection storedProjection = allItems.next();
    assertFalse(allItems.hasNext());
    // Check that the stored instance has the same ID as the instance that handled the event.
    assertEquals(storedProjection.getId(), receiverId);
}
Also used : PrjProjectStarted(io.spine.test.projection.event.PrjProjectStarted) ProjectId(io.spine.test.projection.ProjectId) TestProjection(io.spine.server.projection.given.ProjectionRepositoryTestEnv.TestProjection) Test(org.junit.Test)

Example 17 with ProjectId

use of io.spine.test.projection.ProjectId in project core-java by SpineEventEngine.

the class ProjectionTransactionShould method increment_version_on_event.

/**
 * Tests the version advancement strategy for the {@link Projection}s.
 *
 * <p>The versioning strategy is for {@link Projection} is
 * {@link io.spine.server.entity.EntityVersioning#AUTO_INCREMENT AUTO_INCREMENT}. This test
 * case substitutes {@link #advance_version_from_event()}, which tested the behavior of
 * {@link io.spine.server.entity.EntityVersioning#FROM_EVENT FROM_EVENT} strategy.
 */
@Test
public void increment_version_on_event() {
    final Projection<ProjectId, Project, PatchedProjectBuilder> entity = createEntity();
    final Version oldVersion = entity.getVersion();
    final Event event = createEvent(createEventMessage());
    Projection.play(entity, Collections.singleton(event));
    final Version expected = Versions.increment(oldVersion);
    assertEquals(expected.getNumber(), entity.getVersion().getNumber());
    assertNotEquals(event.getContext().getVersion(), entity.getVersion());
}
Also used : Project(io.spine.test.projection.Project) Version(io.spine.core.Version) ProjectId(io.spine.test.projection.ProjectId) Event(io.spine.core.Event) Test(org.junit.Test)

Example 18 with ProjectId

use of io.spine.test.projection.ProjectId in project core-java by SpineEventEngine.

the class StandPostShould method deliver_updates.

// **** Positive scenarios (unit) ****
@SuppressWarnings({ "OverlyComplexAnonymousInnerClass", "ConstantConditions" })
@Test
public void deliver_updates() {
    final AggregateRepository<ProjectId, Given.StandTestAggregate> repository = Given.aggregateRepo();
    final ProjectId entityId = ProjectId.newBuilder().setId("PRJ-001").build();
    final Given.StandTestAggregate entity = repository.create(entityId);
    final StringValue state = entity.getState();
    final Version version = entity.getVersion();
    final Stand innerStand = Stand.newBuilder().build();
    final Stand stand = spy(innerStand);
    stand.post(requestFactory.createCommandContext().getActorContext().getTenantId(), entity);
    final ArgumentMatcher<EntityStateEnvelope<?, ?>> argumentMatcher = new ArgumentMatcher<EntityStateEnvelope<?, ?>>() {

        @Override
        public boolean matches(EntityStateEnvelope<?, ?> argument) {
            final boolean entityIdMatches = argument.getEntityId().equals(entityId);
            final boolean versionMatches = version.equals(argument.getEntityVersion().orNull());
            final boolean stateMatches = argument.getMessage().equals(state);
            return entityIdMatches && versionMatches && stateMatches;
        }
    };
    verify(stand).update(ArgumentMatchers.argThat(argumentMatcher));
}
Also used : EntityStateEnvelope(io.spine.server.entity.EntityStateEnvelope) Versions.newVersion(io.spine.core.Versions.newVersion) Version(io.spine.core.Version) ArgumentMatcher(org.mockito.ArgumentMatcher) ProjectId(io.spine.test.projection.ProjectId) StringValue(com.google.protobuf.StringValue) Test(org.junit.Test)

Example 19 with ProjectId

use of io.spine.test.projection.ProjectId in project core-java by SpineEventEngine.

the class StandShould method setupExpectedFindAllBehaviour.

@SuppressWarnings("ConstantConditions")
private static void setupExpectedFindAllBehaviour(Map<ProjectId, Project> sampleProjects, StandTestProjectionRepository projectionRepository) {
    final Set<ProjectId> projectIds = sampleProjects.keySet();
    final ImmutableCollection<Given.StandTestProjection> allResults = toProjectionCollection(projectIds);
    for (ProjectId projectId : projectIds) {
        when(projectionRepository.find(eq(projectId))).thenReturn(Optional.of(new StandTestProjection(projectId)));
    }
    final Iterable<ProjectId> matchingIds = argThat(projectionIdsIterableMatcher(projectIds));
    when(projectionRepository.loadAll(matchingIds, any(FieldMask.class))).thenReturn(allResults.iterator());
    when(projectionRepository.loadAll()).thenReturn(allResults.iterator());
    final EntityFilters matchingFilter = argThat(entityFilterMatcher(projectIds));
    when(projectionRepository.find(matchingFilter, any(FieldMask.class))).thenReturn(allResults.iterator());
}
Also used : StandTestProjection(io.spine.server.stand.Given.StandTestProjection) EntityFilters(io.spine.client.EntityFilters) ProjectId(io.spine.test.projection.ProjectId) FieldMask(com.google.protobuf.FieldMask)

Example 20 with ProjectId

use of io.spine.test.projection.ProjectId in project core-java by SpineEventEngine.

the class StandShould method trigger_subscription_callback_upon_update_of_projection.

@Test
public void trigger_subscription_callback_upon_update_of_projection() {
    final Stand stand = prepareStandWithAggregateRepo(mock(StandStorage.class));
    final Topic allProjects = requestFactory.topic().allOf(Project.class);
    final MemoizeEntityUpdateCallback memoizeCallback = new MemoizeEntityUpdateCallback();
    subscribeAndActivate(stand, allProjects, memoizeCallback);
    assertNull(memoizeCallback.newEntityState);
    final Map.Entry<ProjectId, Project> sampleData = fillSampleProjects(1).entrySet().iterator().next();
    final ProjectId projectId = sampleData.getKey();
    final Project project = sampleData.getValue();
    final Version stateVersion = GivenVersion.withNumber(1);
    stand.update(asEnvelope(projectId, project, stateVersion));
    final Any packedState = AnyPacker.pack(project);
    assertEquals(packedState, memoizeCallback.newEntityState);
}
Also used : Project(io.spine.test.projection.Project) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.Version) ProjectId(io.spine.test.projection.ProjectId) Topic(io.spine.client.Topic) Map(java.util.Map) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Any(com.google.protobuf.Any) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Aggregations

ProjectId (io.spine.test.projection.ProjectId)21 Test (org.junit.Test)13 Project (io.spine.test.projection.Project)6 Any (com.google.protobuf.Any)4 Version (io.spine.core.Version)4 Message (com.google.protobuf.Message)3 Event (io.spine.base.Event)3 Event (io.spine.core.Event)3 TestProjection (io.spine.server.projection.given.ProjectionRepositoryTestEnv.TestProjection)3 Duration (com.google.protobuf.Duration)2 EntityFilters (io.spine.client.EntityFilters)2 Topic (io.spine.client.Topic)2 GivenVersion (io.spine.core.given.GivenVersion)2 BoundedContext (io.spine.server.BoundedContext)2 StandTestProjection (io.spine.server.stand.Given.StandTestProjection)2 StandTestProjectionRepository (io.spine.server.stand.Given.StandTestProjectionRepository)2 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)2 Task (io.spine.test.projection.Task)2 PrjProjectCreated (io.spine.test.projection.event.PrjProjectCreated)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2