<commit_before>/*
 * Copyright 2017, 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.procman.given;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.protobuf.Empty;
import com.google.protobuf.Message;
import io.spine.core.CommandContext;
import io.spine.core.EventContext;
import io.spine.core.React;
import io.spine.server.command.Assign;
import io.spine.server.entity.TestEntityWithStringColumn;
import io.spine.server.entity.rejection.EntityAlreadyArchived;
import io.spine.server.entity.rejection.StandardRejections;
import io.spine.server.procman.CommandRouted;
import io.spine.server.procman.ProcessManager;
import io.spine.server.procman.ProcessManagerRepository;
import io.spine.test.procman.Project;
import io.spine.test.procman.ProjectId;
import io.spine.test.procman.ProjectVBuilder;
import io.spine.test.procman.Task;
import io.spine.test.procman.command.PmAddTask;
import io.spine.test.procman.command.PmCreateProject;
import io.spine.test.procman.command.PmDoNothing;
import io.spine.test.procman.command.PmStartProject;
import io.spine.test.procman.command.PmThrowEntityAlreadyArchived;
import io.spine.test.procman.event.PmProjectCreated;
import io.spine.test.procman.event.PmProjectStarted;
import io.spine.test.procman.event.PmTaskAdded;
import io.spine.testdata.Sample;

import java.util.List;

import static io.spine.protobuf.AnyPacker.pack;
import static java.util.Collections.emptyList;

public class ProcessManagerRepositoryTestEnv {

    /** Prevents instantiation of this utility class. */
    private ProcessManagerRepositoryTestEnv() {}

    public static class TestProcessManagerRepository
            extends ProcessManagerRepository<ProjectId, TestProcessManager, Project> {

        public TestProcessManagerRepository() {
            super();
        }
    }

    @SuppressWarnings({
            "OverlyCoupledClass",
            "UnusedParameters" /* The parameter left to show that a projection subscriber can have
                                two parameters. */})
    public static class TestProcessManager
            extends ProcessManager<ProjectId, Project, ProjectVBuilder>
            implements TestEntityWithStringColumn {

        /** The event message we store for inspecting in delivery tests. */
        private static final Multimap<ProjectId, Message> messagesDelivered = HashMultimap.create();

        public TestProcessManager(ProjectId id) {
            super(id);
        }

        public static boolean processed(Message message) {
            final boolean result = messagesDelivered.containsValue(message);
            return result;
        }

        public static void clearMessageDeliveryHistory() {
            messagesDelivered.clear();
        }

        /** Keeps the event message for further inspection in tests. */
        private void keep(Message commandOrEventMsg) {
            messagesDelivered.put(getState().getId(), commandOrEventMsg);
        }

        private static Empty withNothing() {
            return Empty.getDefaultInstance();
        }

        private void handleProjectCreated(ProjectId projectId) {
            final Project newState = getState().toBuilder()
                                               .setId(projectId)
                                               .setStatus(Project.Status.CREATED)
                                               .build();
            getBuilder().mergeFrom(newState);
        }

        private void handleTaskAdded(Task task) {
            final Project newState = getState().toBuilder()
                                               .addTask(task)
                                               .build();
            getBuilder().mergeFrom(newState);
        }

        private void handleProjectStarted() {
            final Project newState = getState().toBuilder()
                                               .setStatus(Project.Status.STARTED)
                                               .build();
            getBuilder().mergeFrom(newState);
        }

        @Assign
        PmProjectCreated handle(PmCreateProject command, CommandContext ignored) {
            keep(command);

            final PmProjectCreated event = ((PmProjectCreated.Builder)
                    Sample.builderForType(PmProjectCreated.class))
                          .setProjectId(command.getProjectId())
                          .build();
            return event;
        }

        @Assign
        PmTaskAdded handle(PmAddTask command, CommandContext ignored) {
            keep(command);

            final PmTaskAdded event = ((PmTaskAdded.Builder)
                    Sample.builderForType(PmTaskAdded.class))
                          .setProjectId(command.getProjectId())
                          .build();
            return event;
        }

        @Assign
        CommandRouted handle(PmStartProject command, CommandContext context) {
            keep(command);

            final Message addTask = ((PmAddTask.Builder)
                    Sample.builderForType(PmAddTask.class))
                          .setProjectId(command.getProjectId())
                          .build();
            return newRouterFor(command, context)
                    .add(addTask)
                    .routeAll();
        }

        @Assign
        List<Message> handle(PmDoNothing command, CommandContext ignored) {
            keep(command);
            return emptyList();
        }

        @Assign
        Empty handle(PmThrowEntityAlreadyArchived command) throws EntityAlreadyArchived {
            keep(command);
            throw new EntityAlreadyArchived(pack(command.getProjectId()));
        }

        @React
        Empty on(StandardRejections.EntityAlreadyArchived rejection) {
            keep(rejection);
            return withNothing();
        }

        @React
        Empty on(StandardRejections.EntityAlreadyDeleted rejection) {<commit_msg>Change test ProcMan handler method return type for consistency.<commit_after>/*
 * Copyright 2017, 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.procman.given;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.protobuf.Empty;
import com.google.protobuf.Message;
import io.spine.core.CommandContext;
import io.spine.core.EventContext;
import io.spine.core.React;
import io.spine.server.command.Assign;
import io.spine.server.entity.TestEntityWithStringColumn;
import io.spine.server.entity.rejection.EntityAlreadyArchived;
import io.spine.server.entity.rejection.StandardRejections;
import io.spine.server.procman.CommandRouted;
import io.spine.server.procman.ProcessManager;
import io.spine.server.procman.ProcessManagerRepository;
import io.spine.test.procman.Project;
import io.spine.test.procman.ProjectId;
import io.spine.test.procman.ProjectVBuilder;
import io.spine.test.procman.Task;
import io.spine.test.procman.command.PmAddTask;
import io.spine.test.procman.command.PmCreateProject;
import io.spine.test.procman.command.PmDoNothing;
import io.spine.test.procman.command.PmStartProject;
import io.spine.test.procman.command.PmThrowEntityAlreadyArchived;
import io.spine.test.procman.event.PmProjectCreated;
import io.spine.test.procman.event.PmProjectStarted;
import io.spine.test.procman.event.PmTaskAdded;
import io.spine.testdata.Sample;

import java.util.List;

import static io.spine.protobuf.AnyPacker.pack;
import static java.util.Collections.emptyList;

public class ProcessManagerRepositoryTestEnv {

    /** Prevents instantiation of this utility class. */
    private ProcessManagerRepositoryTestEnv() {}

    public static class TestProcessManagerRepository
            extends ProcessManagerRepository<ProjectId, TestProcessManager, Project> {

        public TestProcessManagerRepository() {
            super();
        }
    }

    @SuppressWarnings({
            "OverlyCoupledClass",
            "UnusedParameters" /* The parameter left to show that a projection subscriber can have
                                two parameters. */})
    public static class TestProcessManager
            extends ProcessManager<ProjectId, Project, ProjectVBuilder>
            implements TestEntityWithStringColumn {

        /** The event message we store for inspecting in delivery tests. */
        private static final Multimap<ProjectId, Message> messagesDelivered = HashMultimap.create();

        public TestProcessManager(ProjectId id) {
            super(id);
        }

        public static boolean processed(Message message) {
            final boolean result = messagesDelivered.containsValue(message);
            return result;
        }

        public static void clearMessageDeliveryHistory() {
            messagesDelivered.clear();
        }

        /** Keeps the event message for further inspection in tests. */
        private void keep(Message commandOrEventMsg) {
            messagesDelivered.put(getState().getId(), commandOrEventMsg);
        }

        private static Empty withNothing() {
            return Empty.getDefaultInstance();
        }

        private void handleProjectCreated(ProjectId projectId) {
            final Project newState = getState().toBuilder()
                                               .setId(projectId)
                                               .setStatus(Project.Status.CREATED)
                                               .build();
            getBuilder().mergeFrom(newState);
        }

        private void handleTaskAdded(Task task) {
            final Project newState = getState().toBuilder()
                                               .addTask(task)
                                               .build();
            getBuilder().mergeFrom(newState);
        }

        private void handleProjectStarted() {
            final Project newState = getState().toBuilder()
                                               .setStatus(Project.Status.STARTED)
                                               .build();
            getBuilder().mergeFrom(newState);
        }

        @Assign
        PmProjectCreated handle(PmCreateProject command, CommandContext ignored) {
            keep(command);

            final PmProjectCreated event = ((PmProjectCreated.Builder)
                    Sample.builderForType(PmProjectCreated.class))
                          .setProjectId(command.getProjectId())
                          .build();
            return event;
        }

        @Assign
        PmTaskAdded handle(PmAddTask command, CommandContext ignored) {
            keep(command);

            final PmTaskAdded event = ((PmTaskAdded.Builder)
                    Sample.builderForType(PmTaskAdded.class))
                          .setProjectId(command.getProjectId())
                          .build();
            return event;
        }

        @Assign
        CommandRouted handle(PmStartProject command, CommandContext context) {
            keep(command);

            final Message addTask = ((PmAddTask.Builder)
                    Sample.builderForType(PmAddTask.class))
                          .setProjectId(command.getProjectId())
                          .build();
            return newRouterFor(command, context)
                    .add(addTask)
                    .routeAll();
        }

        @Assign
        List<Message> handle(PmDoNothing command, CommandContext ignored) {
            keep(command);
            return emptyList();
        }

        @Assign
        List<Message> handle(PmThrowEntityAlreadyArchived command) throws EntityAlreadyArchived {
            keep(command);
            throw new EntityAlreadyArchived(pack(command.getProjectId()));
        }

        @React
        Empty on(StandardRejections.EntityAlreadyArchived rejection) {
            keep(rejection);
            return withNothing();
        }

        @React
        Empty on(StandardRejections.EntityAlreadyDeleted rejection) {
{'fraction_non_alphanumeric': 0.07881099755290054, 'fraction_numerical': 0.000575788109975529, 'mean_word_length': 2.2989078822412155, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2148346', 'n_tokens_mistral': 3527, 'n_tokens_neox': 3342, 'n_words': 868}