package com.atlassian.stash.internal.task;

import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class CompositeTaskPostProcessorTest {

    @Mock
    private TaskPostProcessor processor1;
    @Mock
    private TaskPostProcessor processor2;

    private CompositeTaskPostProcessor composite;

    @Before
    public void setUp() throws Exception {
        composite = new CompositeTaskPostProcessor(ImmutableList.of(processor1, processor2));
    }

    @Test
    public void testProcess() throws Exception {
        InternalTask task1 = new InternalTask();
        InternalTask task2 = new InternalTask();
        InternalTask task3 = new InternalTask();
        when(processor1.process(same(task1))).thenReturn(task2);
        when(processor2.process(same(task2))).thenReturn(task3);

        assertSame(task3, composite.process(task1));
    }

    @Test
    public void testProcessAll() throws Exception {
        InternalTaskContext context = mock(InternalTaskContext.class);
        InternalTask task1 = new InternalTask();
        InternalTask task2 = new InternalTask();
        InternalTask task3 = new InternalTask();
        when(processor1.processAll(same(context), eq(ImmutableList.of(task1)))).thenReturn(ImmutableList.of(task2));
        when(processor2.processAll(same(context), eq(ImmutableList.of(task2)))).thenReturn(ImmutableList.of(task3));

        assertThat(composite.processAll(context, ImmutableList.of(task1)), contains(sameInstance(task3)));
    }

}