/*
 * Copyright 2018, TeamDev. 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;

import com.google.protobuf.Message;
import io.spine.core.Event;
import io.spine.core.Version;
import io.spine.server.entity.Transaction;
import io.spine.server.entity.TransactionListener;
import io.spine.server.entity.TransactionTest;
import io.spine.server.procman.given.PmTransactionTestEnv.PatchedProjectBuilder;
import io.spine.server.procman.given.PmTransactionTestEnv.TestProcessManager;
import io.spine.test.procman.Project;
import io.spine.test.procman.ProjectId;
import io.spine.test.procman.event.PmProjectCreated;
import io.spine.test.procman.event.PmTaskAdded;
import io.spine.validate.ConstraintViolation;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

import static io.spine.protobuf.AnyPacker.unpack;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author Alex Tymchenko
 */
class PmTransactionTest
        extends TransactionTest<ProjectId,
                                ProcessManager<ProjectId, Project, PatchedProjectBuilder>,
                                Project,
                                PatchedProjectBuilder> {

    private static final ProjectId ID = ProjectId.newBuilder()
                                                 .setId("procman-transaction-should-project")
                                                 .build();

    @Override
    protected Transaction<ProjectId,
            ProcessManager<ProjectId, Project, PatchedProjectBuilder>,
            Project,
            PatchedProjectBuilder>
    createTx(ProcessManager<ProjectId, Project, PatchedProjectBuilder> entity) {
        return new PmTransaction<>(entity);
    }

    @Override
    protected Transaction<ProjectId,
            ProcessManager<ProjectId, Project, PatchedProjectBuilder>,
            Project,
            PatchedProjectBuilder> createTxWithState(
            ProcessManager<ProjectId, Project, PatchedProjectBuilder> entity, Project state,
            Version version) {
        return new PmTransaction<>(entity, state, version);
    }

    @Override
    protected Transaction<ProjectId, ProcessManager<ProjectId, Project, PatchedProjectBuilder>,
            Project, PatchedProjectBuilder>
    createTxWithListener(ProcessManager<ProjectId, Project, PatchedProjectBuilder> entity,
                         TransactionListener<ProjectId,
                                             ProcessManager<ProjectId,
                                                            Project,
                                                            PatchedProjectBuilder>,
                                             Project,
                                             PatchedProjectBuilder> listener) {
        PmTransaction<ProjectId, Project, PatchedProjectBuilder> transaction =
                new PmTransaction<>(entity);
        transaction.setListener(listener);
        return transaction;
    }

    @Override
    protected ProcessManager<ProjectId, Project, PatchedProjectBuilder> createEntity() {
        return new TestProcessManager(ID);
    }

    @Override
    protected ProcessManager<ProjectId, Project, PatchedProjectBuilder> createEntity(
            List<ConstraintViolation> violations) {
        return new TestProcessManager(ID, violations);
    }

    @Override
    protected Project createNewState() {
        return Project.newBuilder()
                      .setId(ID)
                      .setName("The new project name for procman tx tests")
                      .build();
    }

    @Override
    protected void checkEventReceived(
            ProcessManager<ProjectId, Project, PatchedProjectBuilder> entity,
            Event event) {

        TestProcessManager aggregate = (TestProcessManager) entity;
        Message actualMessage = unpack(event.getMessage());
        assertTrue(aggregate.getReceivedEvents()
                            .contains(actualMessage));
    }

    @Override
    protected Message createEventMessage() {
        return PmProjectCreated.newBuilder()
                               .setProjectId(ID)
                               .build();
    }

    @Override
    protected Message createEventMessageThatFailsInHandler() {
        return PmTaskAdded.newBuilder()
                          .setProjectId(ID)
                          .build();
    }

    @Override
    protected void breakEntityValidation(
            ProcessManager<ProjectId, Project, PatchedProjectBuilder> entity,
            RuntimeException toThrow) {
        entity.getBuilder()
              .setShouldThrow(toThrow);
    }

    @SuppressWarnings("DuplicateStringLiteralInspection") // Test case common to ancestor.
    @Disabled // The behavior is changed. The version should be auto incremented.
    @Test
    @DisplayName("advance version from event")
    @Override
    protected void advanceVersionFromEvent() {
    }
}

@@ -5636,305 +5636,7 @@
    %7D
-%0A%0A    @SuppressWarnings(%22DuplicateStringLiteralInspection%22) // Test case common to ancestor.%0A    @Disabled // The behavior is changed. The version should be auto incremented.%0A    @Test%0A    @DisplayName(%22advance version from event%22)%0A    @Override%0A    protected void advanceVersionFromEvent() %7B%0A    %7D
 %0A%7D%0A

Remove outdated disabled test