/*
 * 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 org.spine3.examples.todolist.c.aggregate.definition;

import com.google.common.base.Throwables;
import com.google.protobuf.Message;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.spine3.examples.todolist.TaskDefinition;
import org.spine3.examples.todolist.c.commands.CreateBasicTask;
import org.spine3.examples.todolist.c.commands.DeleteTask;
import org.spine3.examples.todolist.c.events.TaskDeleted;
import org.spine3.examples.todolist.c.failures.CannotDeleteTask;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.spine3.examples.todolist.TaskStatus.DELETED;
import static org.spine3.examples.todolist.testdata.TestTaskCommandFactory.DESCRIPTION;
import static org.spine3.examples.todolist.testdata.TestTaskCommandFactory.createTaskInstance;
import static org.spine3.examples.todolist.testdata.TestTaskCommandFactory.deleteTaskInstance;

/**
 * @author Illia Shepilov
 */
@DisplayName("DeleteTask command should be interpreted by TaskDefinitionPart and")
public class DeleteTaskCommand extends TaskDefinitionCommandTest<DeleteTask> {

    @Override
    @BeforeEach
    protected void setUp() {
        super.setUp();
    }

    @Test
    @DisplayName("delete the task")
    public void deleteTask() {
        dispatchCreateTaskCmd();

        final DeleteTask deleteTaskCmd = deleteTaskInstance(taskId);
        aggregate.dispatchForTest(deleteTaskCmd, commandContext);
        final TaskDefinition state = aggregate.getState();

        assertEquals(taskId, state.getId());
        assertEquals(DELETED, state.getTaskStatus());
    }

    @Test
    @DisplayName("throw CannotDeleteTask failure upon an attempt to delete the already deleted task")
    public void cannotDeleteAlreadyDeletedTask() {
        dispatchCreateTaskCmd();

        final DeleteTask deleteTaskCmd = deleteTaskInstance(taskId);
        aggregate.dispatchForTest(deleteTaskCmd, commandContext);

        try {
            aggregate.dispatchForTest(deleteTaskCmd, commandContext);
        } catch (Throwable e) {
            @SuppressWarnings("ThrowableResultOfMethodCallIgnored") // Need it for checking.
            final Throwable cause = Throwables.getRootCause(e);
            assertTrue(cause instanceof CannotDeleteTask);
        }
    }

    @Test
    @DisplayName("produce TaskDeleted event")
    public void produceEvent() {
        dispatchCreateTaskCmd();
        final DeleteTask deleteTaskCmd = deleteTaskInstance(taskId);

        final List<? extends Message> messageList =
                aggregate.dispatchForTest(deleteTaskCmd, commandContext);
        assertEquals(1, messageList.size());
        assertEquals(TaskDeleted.class, messageList.get(0)
                                                   .getClass());
        final TaskDeleted taskDeleted = (TaskDeleted) messageList.get(0);
        assertEquals(taskId, taskDeleted.getTaskId());
    }

    private void dispatchCreateTaskCmd() {
        final CreateBasicTask createTaskCmd = createTaskInstance(taskId, DESCRIPTION);
        aggregate.dispatchForTest(createTaskCmd, commandContext);
    }
}

@@ -1591,16 +1591,117 @@
 .List;%0A%0A
+import static org.hamcrest.MatcherAssert.assertThat;%0Aimport static org.hamcrest.Matchers.instanceOf;%0A
 import s
@@ -1807,19 +1807,21 @@
 .assertT
-rue
+hrows
 ;%0Aimport
@@ -2488,31 +2488,24 @@
  task%22)%0A    
-public 
 void deleteT
@@ -2959,31 +2959,24 @@
  task%22)%0A    
-public 
 void cannotD
@@ -3185,301 +3185,283 @@
     
-try %7B%0A            aggregate.dispatchForTest(deleteTaskCmd, commandContext);%0A        %7D catch (Throwable e) %7B%0A            @SuppressWarnings(%22ThrowableResultOfMethodCallIgnored%22) // Need it for checking.%0A            final Throwable cause = Throwables.getRootCause(e);%0A            assertTrue(cause
+final Throwable t = assertThrows(Throwable.class,%0A                                         () -%3E aggregate.dispatchForTest(deleteTaskCmd,%0A                                                                         commandContext));%0A        assertThat(Throwables.getRootCause(t),
  ins
@@ -3469,11 +3469,11 @@
 ance
-of 
+Of(
 Cann
@@ -3488,20 +3488,17 @@
 Task
-);%0A        %7D
+.class));
 %0A   
@@ -3561,19 +3561,12 @@
 t%22)%0A
+
     
-public 
 void

Rewrite exception instance checking in `DeleteTaskCommand` using `assertThrows`