package jfditests.logictests.commandstests;

import jfdi.logic.commands.ListCommand;
import jfdi.storage.records.Task;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.Collection;
import java.util.function.Consumer;

import static org.junit.Assert.*;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.*;

/**
 * @author Liu Xinan
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({Task.class})
public class ListCommandTest {

    @Mock private Collection<Task> items;
    @Mock private Consumer<ListCommand> successHook;
    @Mock private Consumer<ListCommand> failureHook;

    private ListCommand listCommandWithTags = new ListCommand.Builder().addTag("test").build();
    private ListCommand listCommandWithoutTags = new ListCommand.Builder().build();

    @Before
    public void setUp() throws Exception {
        mockStatic(Task.class);
        when(Task.getAll()).thenReturn(items);
    }

    @Test
    public void testGetItems() throws Exception {
        // Command must be executed before there is any items to be gotten.
        listCommandWithoutTags.execute();

        assertSame(items, listCommandWithoutTags.getItems());
    }

    @Test
    public void testGetErrorType() throws Exception {
        assertNull(listCommandWithTags.getErrorType());

        listCommandWithTags.execute();
        assertEquals(listCommandWithTags.getErrorType(), ListCommand.ErrorType.UNKNOWN);
    }

    @Test
    public void testExecute() throws Exception {
        listCommandWithoutTags.execute();

        verifyStatic();
        Task.getAll(); // @CheckReturnValue
    }

    @Test
    public void testOnSuccess() throws Exception {
        ListCommand.addSuccessHook(successHook);
        listCommandWithoutTags.execute();

        verify(successHook).accept(listCommandWithoutTags);
    }

    @Test
    public void testOnFailure() throws Exception {
        ListCommand.addFailureHook(failureHook);
        listCommandWithTags.execute();

        verify(failureHook).accept(listCommandWithTags);
    }

    @Test
    public void testAddSuccessHook() throws Exception {
        ListCommand.addSuccessHook(successHook);
        listCommandWithoutTags.execute();

        verify(successHook).accept(listCommandWithoutTags);
    }

    @Test
    public void testAddFailureHook() throws Exception {
        ListCommand.addFailureHook(failureHook);
        listCommandWithTags.execute();

        verify(failureHook).accept(listCommandWithTags);
    }
}

@@ -1037,32 +1037,56 @@
 ows Exception %7B%0A
+        // Mocking Task%0A
         mockStat
@@ -1811,29 +1811,8 @@
 l();
- // @CheckReturnValue
 %0A   
@@ -1845,16 +1845,33 @@
 oid test
+AddSuccessHookAnd
 OnSucces
@@ -2082,16 +2082,33 @@
 oid test
+AddFailureHookAnd
 OnFailur
@@ -2287,449 +2287,6 @@
  %7D%0A%0A
-    @Test%0A    public void testAddSuccessHook() throws Exception %7B%0A        ListCommand.addSuccessHook(successHook);%0A        listCommandWithoutTags.execute();%0A%0A        verify(successHook).accept(listCommandWithoutTags);%0A    %7D%0A%0A    @Test%0A    public void testAddFailureHook() throws Exception %7B%0A        ListCommand.addFailureHook(failureHook);%0A        listCommandWithTags.execute();%0A%0A        verify(failureHook).accept(listCommandWithTags);%0A    %7D%0A
 %7D%0A

Refactor `ListCommandTest`