<commit_before>/*
 * Copyright 2018, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server.event;

import com.google.protobuf.Message;
import io.grpc.stub.StreamObserver;
import io.spine.client.TestActorRequestFactory;
import io.spine.core.Ack;
import io.spine.core.Command;
import io.spine.core.Event;
import io.spine.server.BoundedContext;
import io.spine.server.event.given.EventRootCommandIdTestEnv.ProjectAggregateRepository;
import io.spine.server.event.given.EventRootCommandIdTestEnv.ResponseObserver;
import io.spine.server.event.given.EventRootCommandIdTestEnv.TeamAggregateRepository;
import io.spine.server.event.given.EventRootCommandIdTestEnv.TeamCreationRepository;
import io.spine.server.event.given.EventRootCommandIdTestEnv.UserSignUpRepository;
import io.spine.test.event.ProjectId;
import io.spine.test.event.TeamId;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static io.spine.core.Events.getRootCommandId;
import static io.spine.grpc.StreamObservers.noOpObserver;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.acceptInvitation;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.addTasks;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.addTeamMember;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.createProject;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.inviteTeamMembers;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.newStreamObserver;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.newStreamQuery;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.projectId;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.teamId;
import static org.junit.Assert.assertEquals;

/**
 * @author <NAME>
 */
public class EventRootCommandIdShould {

    private static final TestActorRequestFactory requestFactory =
            TestActorRequestFactory.newInstance(EventRootCommandIdShould.class);

    private static final ProjectId PROJECT_ID = projectId();
    private static final TeamId TEAM_ID = teamId();

    private BoundedContext boundedContext;

    @Before
    public void setUp() {
        boundedContext = BoundedContext.newBuilder()
                                       .build();

        final ProjectAggregateRepository projectRepository = new ProjectAggregateRepository();
        final TeamAggregateRepository teamRepository = new TeamAggregateRepository();
        final TeamCreationRepository teamCreationRepository = new TeamCreationRepository();
        final UserSignUpRepository userSignUpRepository = new UserSignUpRepository();

        boundedContext.register(projectRepository);
        boundedContext.register(teamRepository);
        boundedContext.register(teamCreationRepository);
        boundedContext.register(userSignUpRepository);
    }

    @After
    public void tearDown() throws Exception {
        boundedContext.close();
    }

    @Test
    public void match_the_id_of_a_command_handled_by_an_aggregate() {
        final Command command = command(createProject(PROJECT_ID, TEAM_ID));

        postCommand(command);

        final List<Event> events = readEvents();
        // Two events should be created: one in the `ProjectAggregate` and one in the `TeamAggregate`.
        assertEquals(2, events.size());

        final Event event = events.get(0);
        assertEquals(command.getId(), getRootCommandId(event));
    }

    @Test
    public void match_the_id_of_a_command_handled_by_an_aggregate_for_multiple_events() {
        final Command command = command(addTasks(PROJECT_ID, 3));

        postCommand(command);

        final List<Event> events = readEvents();
        assertEquals(3, events.size());
        assertEquals(command.getId(), getRootCommandId(events.get(0)));
        assertEquals(command.getId(), getRootCommandId(events.get(1)));
        assertEquals(command.getId(), getRootCommandId(events.get(2)));
    }

    @Test
    public void match_the_id_of_an_external_event_handled_by_an_aggregate() {
        final Command command = command(createProject(PROJECT_ID, TEAM_ID));

        postCommand(command);

        final List<Event> events = readEvents();
        // Two events should be created: one in the `ProjectAggregate` and one in the `TeamAggregate`.
        assertEquals(2, events.size());

        final Event reaction = events.get(1);
        assertEquals(command.getId(), getRootCommandId(reaction));
    }

    @Test
    public void match_the_id_of_a_command_handled_by_a_process_manager() {
        final Command command = command(addTeamMember(TEAM_ID));

        postCommand(command);

        final List<Event> events = readEvents();
        assertEquals(1, events.size());

        final Event event = events.get(0);
        assertEquals(command.getId(), getRootCommandId(event));
    }

    @Test
    public void match_the_id_of_a_command_handled_by_a_process_manager_for_multiple_events() {
        final Command command = command(inviteTeamMembers(TEAM_ID, 3));

        postCommand(command);

        final List<Event> events = readEvents();
        assertEquals(3, events.size());
        assertEquals(command.getId(), getRootCommandId(events.get(0)));
        assertEquals(command.getId(), getRootCommandId(events.get(1)));
        assertEquals(command.getId(), getRootCommandId(events.get(2)));
    }

    @Test
    public void match_the_id_of_an_external_event_handled_by_a_process_manager() {
        final Command command = command(acceptInvitation(TEAM_ID));

        postCommand(command);

        final List<Event> events = readEvents();
        // Two events should be created: the `InvitationAccepted` and the `TeamMemberAdded`.
        assertEquals(2, events.size());

        final Event reaction = events.get(1);
        assertEquals(command.getId(), getRootCommandId(reaction));
    }

    private static Command command(Message message) {<commit_msg>Remove static PROJECT_ID and TEAM_ID instances from EventRootCommandIdShould.<commit_after>/*
 * Copyright 2018, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server.event;

import com.google.protobuf.Message;
import io.grpc.stub.StreamObserver;
import io.spine.client.TestActorRequestFactory;
import io.spine.core.Ack;
import io.spine.core.Command;
import io.spine.core.Event;
import io.spine.server.BoundedContext;
import io.spine.server.event.given.EventRootCommandIdTestEnv.ProjectAggregateRepository;
import io.spine.server.event.given.EventRootCommandIdTestEnv.ResponseObserver;
import io.spine.server.event.given.EventRootCommandIdTestEnv.TeamAggregateRepository;
import io.spine.server.event.given.EventRootCommandIdTestEnv.TeamCreationRepository;
import io.spine.server.event.given.EventRootCommandIdTestEnv.UserSignUpRepository;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static io.spine.core.Events.getRootCommandId;
import static io.spine.grpc.StreamObservers.noOpObserver;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.acceptInvitation;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.addTasks;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.addTeamMember;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.createProject;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.inviteTeamMembers;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.newStreamObserver;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.newStreamQuery;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.projectId;
import static io.spine.server.event.given.EventRootCommandIdTestEnv.teamId;
import static org.junit.Assert.assertEquals;

/**
 * @author <NAME>
 */
public class EventRootCommandIdShould {

    private static final TestActorRequestFactory requestFactory =
            TestActorRequestFactory.newInstance(EventRootCommandIdShould.class);

    private BoundedContext boundedContext;

    @Before
    public void setUp() {
        boundedContext = BoundedContext.newBuilder()
                                       .build();

        final ProjectAggregateRepository projectRepository = new ProjectAggregateRepository();
        final TeamAggregateRepository teamRepository = new TeamAggregateRepository();
        final TeamCreationRepository teamCreationRepository = new TeamCreationRepository();
        final UserSignUpRepository userSignUpRepository = new UserSignUpRepository();

        boundedContext.register(projectRepository);
        boundedContext.register(teamRepository);
        boundedContext.register(teamCreationRepository);
        boundedContext.register(userSignUpRepository);
    }

    @After
    public void tearDown() throws Exception {
        boundedContext.close();
    }

    @Test
    public void match_the_id_of_a_command_handled_by_an_aggregate() {
        final Command command = command(createProject(projectId(), teamId()));

        postCommand(command);

        final List<Event> events = readEvents();
        // Two events should be created: one in the `ProjectAggregate` and one in the `TeamAggregate`.
        assertEquals(2, events.size());

        final Event event = events.get(0);
        assertEquals(command.getId(), getRootCommandId(event));
    }

    @Test
    public void match_the_id_of_a_command_handled_by_an_aggregate_for_multiple_events() {
        final Command command = command(addTasks(projectId(), 3));

        postCommand(command);

        final List<Event> events = readEvents();
        assertEquals(3, events.size());
        assertEquals(command.getId(), getRootCommandId(events.get(0)));
        assertEquals(command.getId(), getRootCommandId(events.get(1)));
        assertEquals(command.getId(), getRootCommandId(events.get(2)));
    }

    @Test
    public void match_the_id_of_an_external_event_handled_by_an_aggregate() {
        final Command command = command(createProject(projectId(), teamId()));

        postCommand(command);

        final List<Event> events = readEvents();
        // Two events should be created: one in the `ProjectAggregate` and one in the `TeamAggregate`.
        assertEquals(2, events.size());

        final Event reaction = events.get(1);
        assertEquals(command.getId(), getRootCommandId(reaction));
    }

    @Test
    public void match_the_id_of_a_command_handled_by_a_process_manager() {
        final Command command = command(addTeamMember(teamId()));

        postCommand(command);

        final List<Event> events = readEvents();
        assertEquals(1, events.size());

        final Event event = events.get(0);
        assertEquals(command.getId(), getRootCommandId(event));
    }

    @Test
    public void match_the_id_of_a_command_handled_by_a_process_manager_for_multiple_events() {
        final Command command = command(inviteTeamMembers(teamId(), 3));

        postCommand(command);

        final List<Event> events = readEvents();
        assertEquals(3, events.size());
        assertEquals(command.getId(), getRootCommandId(events.get(0)));
        assertEquals(command.getId(), getRootCommandId(events.get(1)));
        assertEquals(command.getId(), getRootCommandId(events.get(2)));
    }

    @Test
    public void match_the_id_of_an_external_event_handled_by_a_process_manager() {
        final Command command = command(acceptInvitation(teamId()));

        postCommand(command);

        final List<Event> events = readEvents();
        // Two events should be created: the `InvitationAccepted` and the `TeamMemberAdded`.
        assertEquals(2, events.size());

        final Event reaction = events.get(1);
        assertEquals(command.getId(), getRootCommandId(reaction));
    }

    private static Command command(Message message) {
{'fraction_non_alphanumeric': 0.09001518328392741, 'fraction_numerical': 0.0031812594895524546, 'mean_word_length': 4.207831325301205, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2655209', 'n_tokens_mistral': 3873, 'n_tokens_neox': 3711, 'n_words': 905}